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