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 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 13 | #include <fcntl.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 14 | #include <signal.h> |
| 15 | #include <time.h> |
| 16 | #include <stdio.h> |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <sys/wait.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 21 | #include <common/buf.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 22 | #include <common/config.h> |
| 23 | #include <common/debug.h> |
| 24 | #include <common/hathreads.h> |
| 25 | #include <common/initcall.h> |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 26 | #include <common/ist.h> |
| 27 | #include <common/net_helper.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
| 29 | |
| 30 | #include <types/global.h> |
| 31 | |
| 32 | #include <proto/cli.h> |
| 33 | #include <proto/fd.h> |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 34 | #include <proto/hlua.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 35 | #include <proto/stream_interface.h> |
| 36 | #include <proto/task.h> |
| 37 | |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 38 | /* mask of threads still having to dump, used to respect ordering. Only used |
| 39 | * when USE_THREAD_DUMP is set. |
| 40 | */ |
| 41 | volatile unsigned long threads_to_dump = 0; |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 42 | unsigned int debug_commands_issued = 0; |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 44 | /* Dumps to the buffer some known information for the desired thread, and |
| 45 | * optionally extra info for the current thread. The dump will be appended to |
| 46 | * the buffer, so the caller is responsible for preliminary initializing it. |
| 47 | * 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] | 48 | * in front of the calling thread's line (usually it's tid). Any stuck thread |
| 49 | * is also prefixed with a '>'. |
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 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 52 | { |
| 53 | unsigned long thr_bit = 1UL << thr; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 54 | unsigned long long p = ha_thread_info[thr].prev_cpu_time; |
| 55 | unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]); |
| 56 | int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 58 | chunk_appendf(buf, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 59 | "%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] | 60 | " stuck=%d prof=%d", |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 61 | (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 62 | thread_has_tasks(), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 63 | !!(global_tasks_mask & thr_bit), |
| 64 | !eb_is_empty(&task_per_thread[thr].timers), |
| 65 | !eb_is_empty(&task_per_thread[thr].rqueue), |
Willy Tarreau | a62917b | 2020-01-30 18:37:28 +0100 | [diff] [blame] | 66 | !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) && |
| 67 | LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) && |
| 68 | LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) && |
| 69 | MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 70 | task_per_thread[thr].task_list_size, |
| 71 | task_per_thread[thr].rqueue_size, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 72 | stuck, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 73 | !!(task_profiling_mask & thr_bit)); |
| 74 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 75 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 76 | " harmless=%d wantrdv=%d", |
| 77 | !!(threads_harmless_mask & thr_bit), |
| 78 | !!(threads_want_rdv_mask & thr_bit)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 79 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 80 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 81 | 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] | 82 | |
| 83 | /* this is the end of what we can dump from outside the thread */ |
| 84 | |
| 85 | if (thr != tid) |
| 86 | return; |
| 87 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 88 | chunk_appendf(buf, " curr_task="); |
Willy Tarreau | d022e9c | 2019-09-24 08:25:15 +0200 | [diff] [blame] | 89 | ha_task_dump(buf, sched->current, " "); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 93 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 94 | * 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] | 95 | * with <pfx>. The buffer is only appended and the first output starts by the |
| 96 | * pointer itself. The caller is responsible for making sure the task is not |
| 97 | * going to vanish during the dump. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 98 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 99 | 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] | 100 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 101 | const struct stream *s = NULL; |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 102 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 103 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 104 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 105 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 106 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 110 | if (TASK_IS_TASKLET(task)) |
| 111 | chunk_appendf(buf, |
| 112 | "%p (tasklet) calls=%u\n", |
| 113 | task, |
| 114 | task->calls); |
| 115 | else |
| 116 | chunk_appendf(buf, |
| 117 | "%p (task) calls=%u last=%llu%s\n", |
| 118 | task, |
| 119 | task->calls, |
| 120 | task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0, |
| 121 | task->call_date ? " ns ago" : ""); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 122 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 123 | chunk_appendf(buf, "%s" |
Willy Tarreau | 516853f | 2020-03-03 07:04:42 +0100 | [diff] [blame] | 124 | " fct=%p=main%s%ld (%s) ctx=%p", |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 125 | pfx, |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 126 | task->process, |
Willy Tarreau | 516853f | 2020-03-03 07:04:42 +0100 | [diff] [blame] | 127 | ((void *)task->process - (void *)main) < 0 ? "" : "+", |
| 128 | (long)((void *)task->process - (void *)main), |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 129 | task->process == process_stream ? "process_stream" : |
| 130 | task->process == task_run_applet ? "task_run_applet" : |
| 131 | task->process == si_cs_io_cb ? "si_cs_io_cb" : |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 132 | #ifdef USE_LUA |
| 133 | task->process == hlua_process_task ? "hlua_process_task" : |
| 134 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 135 | "?", |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 136 | task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 137 | |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 138 | if (task->process == task_run_applet && (appctx = task->context)) |
| 139 | chunk_appendf(buf, "(%s)\n", appctx->applet->name); |
| 140 | else |
| 141 | chunk_appendf(buf, "\n"); |
| 142 | |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 143 | if (task->process == process_stream && task->context) |
| 144 | s = (struct stream *)task->context; |
| 145 | else if (task->process == task_run_applet && task->context) |
| 146 | s = si_strm(((struct appctx *)task->context)->owner); |
| 147 | else if (task->process == si_cs_io_cb && task->context) |
| 148 | s = si_strm((struct stream_interface *)task->context); |
| 149 | |
| 150 | if (s) |
| 151 | stream_dump(buf, s, pfx, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 152 | |
| 153 | #ifdef USE_LUA |
| 154 | hlua = NULL; |
| 155 | if (s && (hlua = s->hlua)) { |
| 156 | chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); |
| 157 | } |
| 158 | else if (task->process == hlua_process_task && (hlua = task->context)) { |
| 159 | chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx); |
| 160 | } |
| 161 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 162 | (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) { |
| 163 | chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx); |
| 164 | } |
| 165 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 166 | (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) { |
| 167 | chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx); |
| 168 | } |
| 169 | |
| 170 | if (hlua) { |
| 171 | luaL_traceback(hlua->T, hlua->T, NULL, 0); |
| 172 | if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1)) |
| 173 | b_putchr(buf, '\n'); |
| 174 | } |
| 175 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 178 | /* This function dumps all profiling settings. It returns 0 if the output |
| 179 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 180 | */ |
| 181 | static int cli_io_handler_show_threads(struct appctx *appctx) |
| 182 | { |
| 183 | struct stream_interface *si = appctx->owner; |
| 184 | int thr; |
| 185 | |
| 186 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 187 | return 1; |
| 188 | |
| 189 | if (appctx->st0) |
| 190 | thr = appctx->st1; |
| 191 | else |
| 192 | thr = 0; |
| 193 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 194 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 195 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 196 | |
| 197 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 198 | /* failed, try again */ |
| 199 | si_rx_room_blk(si); |
| 200 | appctx->st1 = thr; |
| 201 | return 0; |
| 202 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 203 | return 1; |
| 204 | } |
| 205 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 206 | /* dumps a state of all threads into the trash and on fd #2, then aborts. */ |
| 207 | void ha_panic() |
| 208 | { |
| 209 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 210 | 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] | 211 | ha_thread_dump_all_to_trash(); |
Tim Duesterhus | dda1155 | 2019-06-12 20:47:30 +0200 | [diff] [blame] | 212 | shut_your_big_mouth_gcc(write(2, trash.area, trash.data)); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 213 | for (;;) |
| 214 | abort(); |
| 215 | } |
| 216 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 217 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 218 | static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private) |
| 219 | { |
| 220 | int code = atoi(args[3]); |
| 221 | |
| 222 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 223 | return 1; |
| 224 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 225 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 226 | exit(code); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | /* parse a "debug dev close" command. It always returns 1. */ |
| 231 | static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private) |
| 232 | { |
| 233 | int fd; |
| 234 | |
| 235 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 236 | return 1; |
| 237 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 238 | if (!*args[3]) |
| 239 | return cli_err(appctx, "Missing file descriptor number.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 240 | |
| 241 | fd = atoi(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 242 | if (fd < 0 || fd >= global.maxsock) |
| 243 | return cli_err(appctx, "File descriptor out of range.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 244 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 245 | if (!fdtab[fd].owner) |
| 246 | return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 247 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 248 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 249 | fd_delete(fd); |
| 250 | return 1; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 254 | static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private) |
| 255 | { |
| 256 | int delay = atoi(args[3]); |
| 257 | |
| 258 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 259 | return 1; |
| 260 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 261 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 262 | usleep((long)delay * 1000); |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | /* parse a "debug dev log" command. It always returns 1. */ |
| 267 | static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private) |
| 268 | { |
| 269 | int arg; |
| 270 | |
| 271 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 272 | return 1; |
| 273 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 274 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 275 | chunk_reset(&trash); |
| 276 | for (arg = 3; *args[arg]; arg++) { |
| 277 | if (arg > 3) |
| 278 | chunk_strcat(&trash, " "); |
| 279 | chunk_strcat(&trash, args[arg]); |
| 280 | } |
| 281 | |
| 282 | send_log(NULL, LOG_INFO, "%s\n", trash.area); |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | /* parse a "debug dev loop" command. It always returns 1. */ |
| 287 | static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private) |
| 288 | { |
| 289 | struct timeval deadline, curr; |
| 290 | int loop = atoi(args[3]); |
| 291 | |
| 292 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 293 | return 1; |
| 294 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 295 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 311 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 312 | ha_panic(); |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | /* parse a "debug dev exec" command. It always returns 1. */ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 317 | #if defined(DEBUG_DEV) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 318 | static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private) |
| 319 | { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 320 | int pipefd[2]; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 321 | int arg; |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 322 | int pid; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 323 | |
| 324 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 325 | return 1; |
| 326 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 327 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 328 | chunk_reset(&trash); |
| 329 | for (arg = 3; *args[arg]; arg++) { |
| 330 | if (arg > 3) |
| 331 | chunk_strcat(&trash, " "); |
| 332 | chunk_strcat(&trash, args[arg]); |
| 333 | } |
| 334 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 335 | thread_isolate(); |
| 336 | if (pipe(pipefd) < 0) |
| 337 | goto fail_pipe; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 338 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 339 | if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 340 | goto fail_fcntl; |
| 341 | |
| 342 | if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 343 | goto fail_fcntl; |
| 344 | |
| 345 | pid = fork(); |
| 346 | |
| 347 | if (pid < 0) |
| 348 | goto fail_fork; |
| 349 | else if (pid == 0) { |
| 350 | /* child */ |
| 351 | char *cmd[4] = { "/bin/sh", "-c", 0, 0 }; |
| 352 | |
| 353 | close(0); |
| 354 | dup2(pipefd[1], 1); |
| 355 | dup2(pipefd[1], 2); |
| 356 | |
| 357 | cmd[2] = trash.area; |
| 358 | execvp(cmd[0], cmd); |
| 359 | printf("execvp() failed\n"); |
| 360 | exit(1); |
| 361 | } |
| 362 | |
| 363 | /* parent */ |
| 364 | thread_release(); |
| 365 | close(pipefd[1]); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 366 | chunk_reset(&trash); |
| 367 | while (1) { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 368 | size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data); |
| 369 | if (ret <= 0) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 370 | break; |
| 371 | trash.data += ret; |
| 372 | if (trash.data + 20 == trash.size) { |
| 373 | chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n"); |
| 374 | break; |
| 375 | } |
| 376 | } |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 377 | close(pipefd[0]); |
| 378 | waitpid(pid, NULL, WNOHANG); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 379 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 380 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 381 | |
| 382 | fail_fork: |
| 383 | fail_fcntl: |
| 384 | close(pipefd[0]); |
| 385 | close(pipefd[1]); |
| 386 | fail_pipe: |
| 387 | thread_release(); |
| 388 | return cli_err(appctx, "Failed to execute command.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 389 | } |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 390 | #endif |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 391 | |
| 392 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 393 | static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private) |
| 394 | { |
| 395 | unsigned long start, len; |
| 396 | |
| 397 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 398 | return 1; |
| 399 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 400 | if (!*args[3]) |
| 401 | return cli_err(appctx, "Missing memory address to dump from.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 402 | |
| 403 | start = strtoul(args[3], NULL, 0); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 404 | if (!start) |
| 405 | return cli_err(appctx, "Will not dump from NULL address.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 406 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 407 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 408 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 409 | /* by default, dump ~128 till next block of 16 */ |
| 410 | len = strtoul(args[4], NULL, 0); |
| 411 | if (!len) |
| 412 | len = ((start + 128) & -16) - start; |
| 413 | |
| 414 | chunk_reset(&trash); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 415 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 416 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 417 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 421 | static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private) |
| 422 | { |
| 423 | int thr = 0; |
| 424 | int sig = SIGABRT; |
| 425 | |
| 426 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 427 | return 1; |
| 428 | |
| 429 | if (*args[3]) |
| 430 | thr = atoi(args[3]); |
| 431 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 432 | if (thr < 0 || thr > global.nbthread) |
| 433 | 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] | 434 | |
| 435 | if (*args[4]) |
| 436 | sig = atoi(args[4]); |
| 437 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 438 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 439 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 440 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 441 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 442 | raise(sig); |
| 443 | return 1; |
| 444 | } |
| 445 | |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 446 | /* parse a "debug dev stream" command */ |
| 447 | /* |
| 448 | * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \ |
| 449 | * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \ |
| 450 | * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \ |
| 451 | * [sif.s[=<state>]] [sib.s[=<state>]] |
| 452 | */ |
| 453 | static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private) |
| 454 | { |
| 455 | struct stream *s = si_strm(appctx->owner); |
| 456 | int arg; |
| 457 | void *ptr; |
| 458 | int size; |
| 459 | const char *word, *end; |
| 460 | struct ist name; |
| 461 | char *msg = NULL; |
| 462 | char *endarg; |
| 463 | unsigned long long old, new; |
| 464 | |
| 465 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 466 | return 1; |
| 467 | |
| 468 | ptr = NULL; size = 0; |
| 469 | |
| 470 | if (!*args[3]) { |
| 471 | return cli_err(appctx, |
| 472 | "Usage: debug dev stream { <obj> <op> <value> | wake }*\n" |
| 473 | " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n" |
| 474 | " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n" |
| 475 | " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n" |
| 476 | " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n" |
| 477 | " 'wake' wakes the stream asssigned to 'strm' (default: current)\n" |
| 478 | ); |
| 479 | } |
| 480 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 481 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 482 | for (arg = 3; *args[arg]; arg++) { |
| 483 | old = 0; |
| 484 | end = word = args[arg]; |
| 485 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 486 | end++; |
| 487 | name = ist2(word, end - word); |
| 488 | if (isteq(name, ist("strm"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 489 | ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 490 | } else if (isteq(name, ist("strm.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 491 | ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 492 | } else if (isteq(name, ist("txn.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 493 | ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 494 | } else if (isteq(name, ist("req.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 495 | ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 496 | } else if (isteq(name, ist("res.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 497 | ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 498 | } else if (isteq(name, ist("req.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 499 | ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 500 | } else if (isteq(name, ist("res.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 501 | ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 502 | } else if (isteq(name, ist("req.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 503 | ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 504 | } else if (isteq(name, ist("res.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 505 | ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 506 | } else if (isteq(name, ist("sif.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 507 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 508 | } else if (isteq(name, ist("sib.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 509 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 510 | } else if (isteq(name, ist("sif.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 511 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 512 | } else if (isteq(name, ist("sib.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 513 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 514 | } else if (isteq(name, ist("sif.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 515 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 516 | } else if (isteq(name, ist("sib.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 517 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 518 | } else if (isteq(name, ist("wake"))) { |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 519 | if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1)) |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 520 | task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG); |
| 521 | continue; |
| 522 | } else |
| 523 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word)); |
| 524 | |
| 525 | /* read previous value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 526 | if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 527 | if (size == 8) |
| 528 | old = read_u64(ptr); |
| 529 | else if (size == 4) |
| 530 | old = read_u32(ptr); |
| 531 | else if (size == 2) |
| 532 | old = read_u16(ptr); |
| 533 | else |
| 534 | old = *(const uint8_t *)ptr; |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 535 | } else { |
| 536 | memprintf(&msg, |
| 537 | "%sSkipping inaccessible pointer %p for field '%.*s'.\n", |
| 538 | msg ? msg : "", ptr, (int)(end - word), word); |
| 539 | continue; |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* parse the new value . */ |
| 543 | new = strtoll(end + 1, &endarg, 0); |
| 544 | if (end[1] && *endarg) { |
| 545 | if (strcmp(end + 1, "now") == 0) |
| 546 | new = now_ms; |
| 547 | else { |
| 548 | memprintf(&msg, |
| 549 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 550 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 551 | continue; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | switch (*end) { |
| 556 | case '\0': /* show */ |
| 557 | memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old); |
| 558 | new = old; // do not change the value |
| 559 | break; |
| 560 | |
| 561 | case '=': /* set */ |
| 562 | break; |
| 563 | |
| 564 | case '^': /* XOR */ |
| 565 | new = old ^ new; |
| 566 | break; |
| 567 | |
| 568 | case '+': /* OR */ |
| 569 | new = old | new; |
| 570 | break; |
| 571 | |
| 572 | case '-': /* AND NOT */ |
| 573 | new = old & ~new; |
| 574 | break; |
| 575 | |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | /* write the new value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 581 | if (new != old) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 582 | if (size == 8) |
| 583 | write_u64(ptr, new); |
| 584 | else if (size == 4) |
| 585 | write_u32(ptr, new); |
| 586 | else if (size == 2) |
| 587 | write_u16(ptr, new); |
| 588 | else |
| 589 | *(uint8_t *)ptr = new; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (msg && *msg) |
| 594 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 595 | return 1; |
| 596 | } |
| 597 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 598 | #ifndef USE_THREAD_DUMP |
| 599 | |
| 600 | /* This function dumps all threads' state to the trash. This version is the |
| 601 | * most basic one, which doesn't inspect other threads. |
| 602 | */ |
| 603 | void ha_thread_dump_all_to_trash() |
| 604 | { |
| 605 | unsigned int thr; |
| 606 | |
| 607 | for (thr = 0; thr < global.nbthread; thr++) |
| 608 | ha_thread_dump(&trash, thr, tid); |
| 609 | } |
| 610 | |
| 611 | #else /* below USE_THREAD_DUMP is set */ |
| 612 | |
Willy Tarreau | ddd8533 | 2019-05-22 06:28:54 +0200 | [diff] [blame] | 613 | /* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit |
| 614 | * of not stopping gdb by default, so that issuing "show threads" in a process |
| 615 | * being debugged has no adverse effect. |
| 616 | */ |
| 617 | #define DEBUGSIG SIGURG |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 618 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 619 | /* ID of the thread requesting the dump */ |
| 620 | static unsigned int thread_dump_tid; |
| 621 | |
| 622 | /* points to the buffer where the dump functions should write. It must |
| 623 | * have already been initialized by the requester. Nothing is done if |
| 624 | * it's NULL. |
| 625 | */ |
| 626 | struct buffer *thread_dump_buffer = NULL; |
| 627 | |
| 628 | void ha_thread_dump_all_to_trash() |
| 629 | { |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 630 | unsigned long old; |
| 631 | |
| 632 | while (1) { |
| 633 | old = 0; |
| 634 | if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask)) |
| 635 | break; |
| 636 | ha_thread_relax(); |
| 637 | } |
| 638 | |
| 639 | thread_dump_buffer = &trash; |
| 640 | thread_dump_tid = tid; |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 641 | ha_tkillall(DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* handles DEBUGSIG to dump the state of the thread it's working on */ |
| 645 | void debug_handler(int sig, siginfo_t *si, void *arg) |
| 646 | { |
Willy Tarreau | 82aafc4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 647 | /* first, let's check it's really for us and that we didn't just get |
| 648 | * a spurious DEBUGSIG. |
| 649 | */ |
| 650 | if (!(threads_to_dump & tid_bit)) |
| 651 | return; |
| 652 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 653 | /* There are 4 phases in the dump process: |
| 654 | * 1- wait for our turn, i.e. when all lower bits are gone. |
| 655 | * 2- perform the action if our bit is set |
| 656 | * 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] | 657 | * the last one and have to put them all as a signal |
| 658 | * 4- wait out bit to re-appear, then clear it and quit. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 659 | */ |
| 660 | |
| 661 | /* wait for all previous threads to finish first */ |
| 662 | while (threads_to_dump & (tid_bit - 1)) |
| 663 | ha_thread_relax(); |
| 664 | |
| 665 | /* dump if needed */ |
| 666 | if (threads_to_dump & tid_bit) { |
| 667 | if (thread_dump_buffer) |
| 668 | ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid); |
| 669 | if ((threads_to_dump & all_threads_mask) == tid_bit) { |
| 670 | /* last one */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 671 | HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 672 | thread_dump_buffer = NULL; |
| 673 | } |
| 674 | else |
| 675 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
| 676 | } |
| 677 | |
| 678 | /* 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] | 679 | * bits again to broadcast the leaving condition so we'll see ourselves |
| 680 | * present again. This way the threads_to_dump variable never passes to |
| 681 | * zero until all visitors have stopped waiting. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 682 | */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 683 | while (!(threads_to_dump & tid_bit)) |
| 684 | ha_thread_relax(); |
| 685 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 686 | |
| 687 | /* mark the current thread as stuck to detect it upon next invocation |
| 688 | * if it didn't move. |
| 689 | */ |
| 690 | if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit)) |
| 691 | ti->flags |= TI_FL_STUCK; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static int init_debug_per_thread() |
| 695 | { |
| 696 | sigset_t set; |
| 697 | |
| 698 | /* unblock the DEBUGSIG signal we intend to use */ |
| 699 | sigemptyset(&set); |
| 700 | sigaddset(&set, DEBUGSIG); |
| 701 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | static int init_debug() |
| 706 | { |
| 707 | struct sigaction sa; |
| 708 | |
| 709 | sa.sa_handler = NULL; |
| 710 | sa.sa_sigaction = debug_handler; |
| 711 | sigemptyset(&sa.sa_mask); |
| 712 | sa.sa_flags = SA_SIGINFO; |
| 713 | sigaction(DEBUGSIG, &sa, NULL); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | REGISTER_POST_CHECK(init_debug); |
| 718 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 719 | |
| 720 | #endif /* USE_THREAD_DUMP */ |
| 721 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 722 | /* register cli keywords */ |
| 723 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 724 | {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 725 | {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 726 | #if defined(DEBUG_DEV) |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 727 | {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 728 | #endif |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 729 | {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 730 | {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 731 | {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 732 | {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 733 | {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 734 | {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT }, |
| 735 | {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 736 | {{ "show", "threads", NULL, NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL }, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 737 | {{},} |
| 738 | }}; |
| 739 | |
| 740 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |