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