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