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