Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Process debugging functions. |
| 3 | * |
| 4 | * Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <signal.h> |
| 14 | #include <time.h> |
| 15 | #include <stdio.h> |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 16 | #include <stdlib.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 17 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 18 | #include <common/buf.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 19 | #include <common/config.h> |
| 20 | #include <common/debug.h> |
| 21 | #include <common/hathreads.h> |
| 22 | #include <common/initcall.h> |
| 23 | #include <common/standard.h> |
| 24 | |
| 25 | #include <types/global.h> |
Olivier Houchard | 7589309 | 2020-03-18 13:07:19 +0100 | [diff] [blame] | 26 | #include <types/signal.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 27 | |
| 28 | #include <proto/cli.h> |
| 29 | #include <proto/fd.h> |
Willy Tarreau | 4856b36 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 30 | #include <proto/hlua.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 31 | #include <proto/stream_interface.h> |
| 32 | #include <proto/task.h> |
| 33 | |
Willy Tarreau | 445b2b7 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 34 | /* mask of threads still having to dump, used to respect ordering. Only used |
| 35 | * when USE_THREAD_DUMP is set. |
| 36 | */ |
| 37 | volatile unsigned long threads_to_dump = 0; |
| 38 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 39 | /* Dumps to the buffer some known information for the desired thread, and |
| 40 | * optionally extra info for the current thread. The dump will be appended to |
| 41 | * the buffer, so the caller is responsible for preliminary initializing it. |
| 42 | * The calling thread ID needs to be passed in <calling_tid> to display a star |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 43 | * in front of the calling thread's line (usually it's tid). Any stuck thread |
| 44 | * is also prefixed with a '>'. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 45 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 46 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 47 | { |
| 48 | unsigned long thr_bit = 1UL << thr; |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 49 | unsigned long long p = thread_info[thr].prev_cpu_time; |
| 50 | unsigned long long n = now_cpu_time_thread(&thread_info[thr]); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 51 | int stuck = !!(thread_info[thr].flags & TI_FL_STUCK); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 53 | chunk_appendf(buf, |
Willy Tarreau | 4a3768b | 2020-05-01 12:26:03 +0200 | [diff] [blame] | 54 | "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n" |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 55 | " stuck=%d fdcache=%d prof=%d", |
| 56 | (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Willy Tarreau | ab12d19 | 2020-05-01 11:28:49 +0200 | [diff] [blame] | 57 | ha_get_pthread_id(thr), |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 58 | thread_has_tasks(), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 59 | !!(global_tasks_mask & thr_bit), |
| 60 | !eb_is_empty(&task_per_thread[thr].timers), |
| 61 | !eb_is_empty(&task_per_thread[thr].rqueue), |
| 62 | !LIST_ISEMPTY(&task_per_thread[thr].task_list), |
| 63 | task_per_thread[thr].task_list_size, |
| 64 | task_per_thread[thr].rqueue_size, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 65 | stuck, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 66 | !!(fd_cache_mask & thr_bit), |
| 67 | !!(task_profiling_mask & thr_bit)); |
| 68 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 69 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 70 | " harmless=%d wantrdv=%d", |
| 71 | !!(threads_harmless_mask & thr_bit), |
| 72 | !!(threads_want_rdv_mask & thr_bit)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 74 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 75 | chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 76 | |
| 77 | /* this is the end of what we can dump from outside the thread */ |
| 78 | |
| 79 | if (thr != tid) |
| 80 | return; |
| 81 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 82 | chunk_appendf(buf, " curr_task="); |
| 83 | ha_task_dump(buf, curr_task, " "); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 87 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 88 | * either be a task or a tasklet, and prepend each line except the first one |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 89 | * with <pfx>. The buffer is only appended and the first output starts by the |
| 90 | * pointer itself. The caller is responsible for making sure the task is not |
| 91 | * going to vanish during the dump. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 92 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 93 | void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 94 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 95 | const struct stream *s = NULL; |
Willy Tarreau | 3a76168 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 96 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 4856b36 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 97 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 99 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 100 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 104 | if (TASK_IS_TASKLET(task)) |
| 105 | chunk_appendf(buf, |
| 106 | "%p (tasklet) calls=%u\n", |
| 107 | task, |
| 108 | task->calls); |
| 109 | else |
| 110 | chunk_appendf(buf, |
| 111 | "%p (task) calls=%u last=%llu%s\n", |
| 112 | task, |
| 113 | task->calls, |
| 114 | task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0, |
| 115 | task->call_date ? " ns ago" : ""); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 117 | chunk_appendf(buf, "%s" |
Willy Tarreau | d4f00f7 | 2020-03-03 07:04:42 +0100 | [diff] [blame] | 118 | " fct=%p=main%s%ld (%s) ctx=%p", |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 119 | pfx, |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 120 | task->process, |
Willy Tarreau | d4f00f7 | 2020-03-03 07:04:42 +0100 | [diff] [blame] | 121 | ((void *)task->process - (void *)main) < 0 ? "" : "+", |
| 122 | (long)((void *)task->process - (void *)main), |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 123 | task->process == process_stream ? "process_stream" : |
| 124 | task->process == task_run_applet ? "task_run_applet" : |
| 125 | task->process == si_cs_io_cb ? "si_cs_io_cb" : |
Willy Tarreau | 4856b36 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 126 | #ifdef USE_LUA |
| 127 | task->process == hlua_process_task ? "hlua_process_task" : |
| 128 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 129 | "?", |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 130 | task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 131 | |
Willy Tarreau | 3a76168 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 132 | if (task->process == task_run_applet && (appctx = task->context)) |
| 133 | chunk_appendf(buf, "(%s)\n", appctx->applet->name); |
| 134 | else |
| 135 | chunk_appendf(buf, "\n"); |
| 136 | |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 137 | if (task->process == process_stream && task->context) |
| 138 | s = (struct stream *)task->context; |
| 139 | else if (task->process == task_run_applet && task->context) |
| 140 | s = si_strm(((struct appctx *)task->context)->owner); |
| 141 | else if (task->process == si_cs_io_cb && task->context) |
| 142 | s = si_strm((struct stream_interface *)task->context); |
| 143 | |
| 144 | if (s) |
| 145 | stream_dump(buf, s, pfx, '\n'); |
Willy Tarreau | 4856b36 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 146 | |
| 147 | #ifdef USE_LUA |
| 148 | hlua = NULL; |
| 149 | if (s && (hlua = s->hlua)) { |
| 150 | chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); |
| 151 | } |
| 152 | else if (task->process == hlua_process_task && (hlua = task->context)) { |
| 153 | chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx); |
| 154 | } |
| 155 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 156 | (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) { |
| 157 | chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx); |
| 158 | } |
| 159 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 160 | (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) { |
| 161 | chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx); |
| 162 | } |
| 163 | |
Christopher Faulet | 90bccc7 | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 164 | if (hlua && hlua->T) { |
Willy Tarreau | 4856b36 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 165 | luaL_traceback(hlua->T, hlua->T, NULL, 0); |
| 166 | if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1)) |
| 167 | b_putchr(buf, '\n'); |
| 168 | } |
Christopher Faulet | 90bccc7 | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 169 | else |
| 170 | b_putchr(buf, '\n'); |
Willy Tarreau | 4856b36 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 171 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 174 | /* This function dumps all profiling settings. It returns 0 if the output |
| 175 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 176 | */ |
| 177 | static int cli_io_handler_show_threads(struct appctx *appctx) |
| 178 | { |
| 179 | struct stream_interface *si = appctx->owner; |
| 180 | int thr; |
| 181 | |
| 182 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 183 | return 1; |
| 184 | |
| 185 | if (appctx->st0) |
| 186 | thr = appctx->st1; |
| 187 | else |
| 188 | thr = 0; |
| 189 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 190 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 191 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 192 | |
| 193 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 194 | /* failed, try again */ |
| 195 | si_rx_room_blk(si); |
| 196 | appctx->st1 = thr; |
| 197 | return 0; |
| 198 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 199 | return 1; |
| 200 | } |
| 201 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 202 | /* dumps a state of all threads into the trash and on fd #2, then aborts. */ |
| 203 | void ha_panic() |
| 204 | { |
| 205 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 206 | chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 207 | ha_thread_dump_all_to_trash(); |
Tim Duesterhus | dda1155 | 2019-06-12 20:47:30 +0200 | [diff] [blame] | 208 | shut_your_big_mouth_gcc(write(2, trash.area, trash.data)); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 209 | for (;;) |
| 210 | abort(); |
| 211 | } |
| 212 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 213 | #if defined(DEBUG_DEV) |
| 214 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 215 | static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private) |
| 216 | { |
| 217 | int code = atoi(args[3]); |
| 218 | |
| 219 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 220 | return 1; |
| 221 | |
| 222 | exit(code); |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | /* parse a "debug dev close" command. It always returns 1. */ |
| 227 | static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private) |
| 228 | { |
| 229 | int fd; |
| 230 | |
| 231 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 232 | return 1; |
| 233 | |
| 234 | if (!*args[3]) { |
| 235 | appctx->ctx.cli.msg = "Missing file descriptor number.\n"; |
| 236 | goto reterr; |
| 237 | } |
| 238 | |
| 239 | fd = atoi(args[3]); |
| 240 | if (fd < 0 || fd >= global.maxsock) { |
| 241 | appctx->ctx.cli.msg = "File descriptor out of range.\n"; |
| 242 | goto reterr; |
| 243 | } |
| 244 | |
| 245 | if (!fdtab[fd].owner) { |
| 246 | appctx->ctx.cli.msg = "File descriptor was already closed.\n"; |
| 247 | goto retinfo; |
| 248 | } |
| 249 | |
| 250 | fd_delete(fd); |
| 251 | return 1; |
| 252 | retinfo: |
| 253 | appctx->ctx.cli.severity = LOG_INFO; |
| 254 | appctx->st0 = CLI_ST_PRINT; |
| 255 | return 1; |
| 256 | reterr: |
| 257 | appctx->ctx.cli.severity = LOG_ERR; |
| 258 | appctx->st0 = CLI_ST_PRINT; |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 263 | static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private) |
| 264 | { |
| 265 | int delay = atoi(args[3]); |
| 266 | |
| 267 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 268 | return 1; |
| 269 | |
| 270 | usleep((long)delay * 1000); |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | /* parse a "debug dev log" command. It always returns 1. */ |
| 275 | static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private) |
| 276 | { |
| 277 | int arg; |
| 278 | |
| 279 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 280 | return 1; |
| 281 | |
| 282 | chunk_reset(&trash); |
| 283 | for (arg = 3; *args[arg]; arg++) { |
| 284 | if (arg > 3) |
| 285 | chunk_strcat(&trash, " "); |
| 286 | chunk_strcat(&trash, args[arg]); |
| 287 | } |
| 288 | |
| 289 | send_log(NULL, LOG_INFO, "%s\n", trash.area); |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | /* parse a "debug dev loop" command. It always returns 1. */ |
| 294 | static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private) |
| 295 | { |
| 296 | struct timeval deadline, curr; |
| 297 | int loop = atoi(args[3]); |
| 298 | |
| 299 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 300 | return 1; |
| 301 | |
| 302 | gettimeofday(&curr, NULL); |
| 303 | tv_ms_add(&deadline, &curr, loop); |
| 304 | |
| 305 | while (tv_ms_cmp(&curr, &deadline) < 0) |
| 306 | gettimeofday(&curr, NULL); |
| 307 | |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | /* parse a "debug dev panic" command. It always returns 1, though it should never return. */ |
| 312 | static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private) |
| 313 | { |
| 314 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 315 | return 1; |
| 316 | |
| 317 | ha_panic(); |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | /* parse a "debug dev exec" command. It always returns 1. */ |
| 322 | static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private) |
| 323 | { |
| 324 | FILE *f; |
| 325 | int arg; |
| 326 | |
| 327 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 328 | return 1; |
| 329 | |
| 330 | chunk_reset(&trash); |
| 331 | for (arg = 3; *args[arg]; arg++) { |
| 332 | if (arg > 3) |
| 333 | chunk_strcat(&trash, " "); |
| 334 | chunk_strcat(&trash, args[arg]); |
| 335 | } |
| 336 | |
| 337 | f = popen(trash.area, "re"); |
| 338 | if (!f) { |
| 339 | appctx->ctx.cli.severity = LOG_ERR; |
| 340 | appctx->ctx.cli.msg = "Failed to execute command.\n"; |
| 341 | appctx->st0 = CLI_ST_PRINT; |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | chunk_reset(&trash); |
| 346 | while (1) { |
| 347 | size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f); |
| 348 | if (!ret) |
| 349 | break; |
| 350 | trash.data += ret; |
| 351 | if (trash.data + 20 == trash.size) { |
| 352 | chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n"); |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | fclose(f); |
| 358 | trash.area[trash.data] = 0; |
| 359 | appctx->ctx.cli.severity = LOG_INFO; |
| 360 | appctx->ctx.cli.msg = trash.area; |
| 361 | appctx->st0 = CLI_ST_PRINT; |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 366 | static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private) |
| 367 | { |
| 368 | unsigned long start, len; |
| 369 | |
| 370 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 371 | return 1; |
| 372 | |
| 373 | if (!*args[3]) { |
| 374 | appctx->ctx.cli.msg = "Missing memory address to dump from.\n"; |
| 375 | goto reterr; |
| 376 | } |
| 377 | |
| 378 | start = strtoul(args[3], NULL, 0); |
| 379 | if (!start) { |
| 380 | appctx->ctx.cli.msg = "Will not dump from NULL address.\n"; |
| 381 | goto reterr; |
| 382 | } |
| 383 | |
| 384 | /* by default, dump ~128 till next block of 16 */ |
| 385 | len = strtoul(args[4], NULL, 0); |
| 386 | if (!len) |
| 387 | len = ((start + 128) & -16) - start; |
| 388 | |
| 389 | chunk_reset(&trash); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 390 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 391 | trash.area[trash.data] = 0; |
| 392 | appctx->ctx.cli.severity = LOG_INFO; |
| 393 | appctx->ctx.cli.msg = trash.area; |
| 394 | appctx->st0 = CLI_ST_PRINT; |
| 395 | return 1; |
| 396 | reterr: |
| 397 | appctx->ctx.cli.severity = LOG_ERR; |
| 398 | appctx->st0 = CLI_ST_PRINT; |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 403 | static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private) |
| 404 | { |
| 405 | int thr = 0; |
| 406 | int sig = SIGABRT; |
| 407 | |
| 408 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 409 | return 1; |
| 410 | |
| 411 | if (*args[3]) |
| 412 | thr = atoi(args[3]); |
| 413 | |
| 414 | if (thr < 0 || thr > global.nbthread) { |
| 415 | appctx->ctx.cli.severity = LOG_ERR; |
| 416 | appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n"; |
| 417 | appctx->st0 = CLI_ST_PRINT; |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | if (*args[4]) |
| 422 | sig = atoi(args[4]); |
| 423 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 424 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 425 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 426 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 427 | raise(sig); |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | #endif |
| 432 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 433 | #ifndef USE_THREAD_DUMP |
| 434 | |
| 435 | /* This function dumps all threads' state to the trash. This version is the |
| 436 | * most basic one, which doesn't inspect other threads. |
| 437 | */ |
| 438 | void ha_thread_dump_all_to_trash() |
| 439 | { |
| 440 | unsigned int thr; |
| 441 | |
| 442 | for (thr = 0; thr < global.nbthread; thr++) |
| 443 | ha_thread_dump(&trash, thr, tid); |
| 444 | } |
| 445 | |
| 446 | #else /* below USE_THREAD_DUMP is set */ |
| 447 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 448 | /* ID of the thread requesting the dump */ |
| 449 | static unsigned int thread_dump_tid; |
| 450 | |
| 451 | /* points to the buffer where the dump functions should write. It must |
| 452 | * have already been initialized by the requester. Nothing is done if |
| 453 | * it's NULL. |
| 454 | */ |
| 455 | struct buffer *thread_dump_buffer = NULL; |
| 456 | |
| 457 | void ha_thread_dump_all_to_trash() |
| 458 | { |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 459 | unsigned long old; |
| 460 | |
| 461 | while (1) { |
| 462 | old = 0; |
| 463 | if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask)) |
| 464 | break; |
| 465 | ha_thread_relax(); |
| 466 | } |
| 467 | |
| 468 | thread_dump_buffer = &trash; |
| 469 | thread_dump_tid = tid; |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 470 | ha_tkillall(DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* handles DEBUGSIG to dump the state of the thread it's working on */ |
| 474 | void debug_handler(int sig, siginfo_t *si, void *arg) |
| 475 | { |
Willy Tarreau | bce97b4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 476 | /* first, let's check it's really for us and that we didn't just get |
| 477 | * a spurious DEBUGSIG. |
| 478 | */ |
| 479 | if (!(threads_to_dump & tid_bit)) |
| 480 | return; |
| 481 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 482 | /* There are 4 phases in the dump process: |
| 483 | * 1- wait for our turn, i.e. when all lower bits are gone. |
| 484 | * 2- perform the action if our bit is set |
| 485 | * 3- remove our bit to let the next one go, unless we're |
Willy Tarreau | da767ea | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 486 | * the last one and have to put them all as a signal |
| 487 | * 4- wait out bit to re-appear, then clear it and quit. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 488 | */ |
| 489 | |
| 490 | /* wait for all previous threads to finish first */ |
| 491 | while (threads_to_dump & (tid_bit - 1)) |
| 492 | ha_thread_relax(); |
| 493 | |
| 494 | /* dump if needed */ |
| 495 | if (threads_to_dump & tid_bit) { |
| 496 | if (thread_dump_buffer) |
| 497 | ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid); |
| 498 | if ((threads_to_dump & all_threads_mask) == tid_bit) { |
| 499 | /* last one */ |
Willy Tarreau | da767ea | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 500 | HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 501 | thread_dump_buffer = NULL; |
| 502 | } |
| 503 | else |
| 504 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
| 505 | } |
| 506 | |
| 507 | /* now wait for all others to finish dumping. The last one will set all |
Willy Tarreau | da767ea | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 508 | * bits again to broadcast the leaving condition so we'll see ourselves |
| 509 | * present again. This way the threads_to_dump variable never passes to |
| 510 | * zero until all visitors have stopped waiting. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 511 | */ |
Willy Tarreau | da767ea | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 512 | while (!(threads_to_dump & tid_bit)) |
| 513 | ha_thread_relax(); |
| 514 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 515 | |
| 516 | /* mark the current thread as stuck to detect it upon next invocation |
| 517 | * if it didn't move. |
| 518 | */ |
| 519 | if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit)) |
| 520 | ti->flags |= TI_FL_STUCK; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static int init_debug_per_thread() |
| 524 | { |
| 525 | sigset_t set; |
| 526 | |
| 527 | /* unblock the DEBUGSIG signal we intend to use */ |
| 528 | sigemptyset(&set); |
| 529 | sigaddset(&set, DEBUGSIG); |
| 530 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | static int init_debug() |
| 535 | { |
| 536 | struct sigaction sa; |
| 537 | |
| 538 | sa.sa_handler = NULL; |
| 539 | sa.sa_sigaction = debug_handler; |
| 540 | sigemptyset(&sa.sa_mask); |
| 541 | sa.sa_flags = SA_SIGINFO; |
| 542 | sigaction(DEBUGSIG, &sa, NULL); |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | REGISTER_POST_CHECK(init_debug); |
| 547 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 548 | |
| 549 | #endif /* USE_THREAD_DUMP */ |
| 550 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 551 | /* register cli keywords */ |
| 552 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 553 | #if defined(DEBUG_DEV) |
| 554 | {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL }, |
| 555 | {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL }, |
| 556 | {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL }, |
| 557 | {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL }, |
| 558 | {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL }, |
| 559 | {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL }, |
| 560 | {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL }, |
| 561 | {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL }, |
| 562 | {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL }, |
| 563 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 564 | { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL }, |
| 565 | {{},} |
| 566 | }}; |
| 567 | |
| 568 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |