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