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