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