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