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