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