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