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 | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 19 | #include <syslog.h> |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 23 | #include <haproxy/api.h> |
Willy Tarreau | 8dabda7 | 2020-05-27 17:22:10 +0200 | [diff] [blame] | 24 | #include <haproxy/buf.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Willy Tarreau | 2a83d60 | 2020-05-27 16:58:08 +0200 | [diff] [blame] | 26 | #include <haproxy/debug.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/fd.h> |
| 28 | #include <haproxy/global.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 29 | #include <haproxy/hlua.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/net_helper.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 32 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 33 | #include <haproxy/task.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 34 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 35 | #include <haproxy/tools.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 36 | #include <import/ist.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 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 | 8a069eb | 2020-11-30 16:17:33 +0100 | [diff] [blame] | 45 | /* Xorshift RNGs from http://www.jstatsoft.org/v08/i14/paper */ |
Willy Tarreau | c7ead07 | 2020-12-18 16:26:36 +0100 | [diff] [blame] | 46 | static THREAD_LOCAL unsigned int y = 2463534242U; |
Willy Tarreau | 8a069eb | 2020-11-30 16:17:33 +0100 | [diff] [blame] | 47 | static unsigned int debug_prng() |
| 48 | { |
Willy Tarreau | 8a069eb | 2020-11-30 16:17:33 +0100 | [diff] [blame] | 49 | y ^= y << 13; |
| 50 | y ^= y >> 17; |
| 51 | y ^= y << 5; |
| 52 | return y; |
| 53 | } |
| 54 | |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 55 | /* dumps a backtrace of the current thread that is appended to buffer <buf>. |
| 56 | * Lines are prefixed with the string <prefix> which may be empty (used for |
| 57 | * indenting). It is recommended to use this at a function's tail so that |
| 58 | * the function does not appear in the call stack. |
| 59 | */ |
| 60 | void ha_dump_backtrace(struct buffer *buf, const char *prefix) |
| 61 | { |
| 62 | struct buffer bak; |
| 63 | char pfx2[100]; |
| 64 | void *callers[100]; |
| 65 | int j, nptrs; |
| 66 | const void *addr; |
| 67 | int dump = 0; |
| 68 | |
| 69 | nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
| 70 | if (!nptrs) |
| 71 | return; |
| 72 | |
| 73 | if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2)) |
| 74 | pfx2[0] = 0; |
| 75 | |
| 76 | /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would |
| 77 | * produce similar output to the following: |
| 78 | */ |
| 79 | chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs); |
| 80 | for (j = 0; (j < nptrs || dump < 2); j++) { |
| 81 | if (j == nptrs && !dump) { |
| 82 | /* we failed to spot the starting point of the |
| 83 | * dump, let's start over dumping everything we |
| 84 | * have. |
| 85 | */ |
| 86 | dump = 2; |
| 87 | j = 0; |
| 88 | } |
| 89 | bak = *buf; |
| 90 | dump_addr_and_bytes(buf, pfx2, callers[j], 8); |
| 91 | addr = resolve_sym_name(buf, ": ", callers[j]); |
| 92 | if (dump == 0) { |
| 93 | /* dump not started, will start *after* |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 94 | * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 95 | */ |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 96 | if (addr == ha_thread_dump_all_to_trash || addr == ha_panic || |
| 97 | addr == ha_backtrace_to_stderr) |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 98 | dump = 1; |
| 99 | *buf = bak; |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | if (dump == 1) { |
| 104 | /* starting */ |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 105 | if (addr == ha_thread_dump_all_to_trash || addr == ha_panic || |
| 106 | addr == ha_backtrace_to_stderr) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 107 | *buf = bak; |
| 108 | continue; |
| 109 | } |
| 110 | dump = 2; |
| 111 | } |
| 112 | |
| 113 | if (dump == 2) { |
| 114 | /* dumping */ |
| 115 | if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) { |
| 116 | dump = 3; |
| 117 | *buf = bak; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | /* OK, line dumped */ |
| 122 | chunk_appendf(buf, "\n"); |
| 123 | } |
| 124 | } |
| 125 | |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 126 | /* dump a backtrace of current thread's stack to stderr. */ |
| 127 | void ha_backtrace_to_stderr() |
| 128 | { |
| 129 | char area[2048]; |
| 130 | struct buffer b = b_make(area, sizeof(area), 0, 0); |
| 131 | |
| 132 | ha_dump_backtrace(&b, " "); |
| 133 | if (b.data) |
| 134 | write(2, b.area, b.data); |
| 135 | } |
| 136 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 137 | /* Dumps to the buffer some known information for the desired thread, and |
| 138 | * optionally extra info for the current thread. The dump will be appended to |
| 139 | * the buffer, so the caller is responsible for preliminary initializing it. |
| 140 | * 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] | 141 | * in front of the calling thread's line (usually it's tid). Any stuck thread |
| 142 | * is also prefixed with a '>'. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 143 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 144 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 145 | { |
| 146 | unsigned long thr_bit = 1UL << thr; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 147 | unsigned long long p = ha_thread_info[thr].prev_cpu_time; |
| 148 | unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]); |
| 149 | int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 150 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 151 | chunk_appendf(buf, |
Willy Tarreau | f0e5da2 | 2020-05-01 12:26:03 +0200 | [diff] [blame] | 152 | "%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] | 153 | " stuck=%d prof=%d", |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 154 | (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Willy Tarreau | ff64d3b | 2020-05-01 11:28:49 +0200 | [diff] [blame] | 155 | ha_get_pthread_id(thr), |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 156 | thread_has_tasks(), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 157 | !!(global_tasks_mask & thr_bit), |
| 158 | !eb_is_empty(&task_per_thread[thr].timers), |
| 159 | !eb_is_empty(&task_per_thread[thr].rqueue), |
Willy Tarreau | a62917b | 2020-01-30 18:37:28 +0100 | [diff] [blame] | 160 | !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) && |
| 161 | LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) && |
| 162 | LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) && |
| 163 | MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 164 | task_per_thread[thr].task_list_size, |
| 165 | task_per_thread[thr].rqueue_size, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 166 | stuck, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 167 | !!(task_profiling_mask & thr_bit)); |
| 168 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 169 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 170 | " harmless=%d wantrdv=%d", |
| 171 | !!(threads_harmless_mask & thr_bit), |
| 172 | !!(threads_want_rdv_mask & thr_bit)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 173 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 174 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 175 | 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] | 176 | |
| 177 | /* this is the end of what we can dump from outside the thread */ |
| 178 | |
| 179 | if (thr != tid) |
| 180 | return; |
| 181 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 182 | chunk_appendf(buf, " curr_task="); |
Willy Tarreau | d022e9c | 2019-09-24 08:25:15 +0200 | [diff] [blame] | 183 | ha_task_dump(buf, sched->current, " "); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 184 | |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 185 | if (stuck) { |
| 186 | /* We only emit the backtrace for stuck threads in order not to |
| 187 | * waste precious output buffer space with non-interesting data. |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 188 | * Please leave this as the last instruction in this function |
| 189 | * so that the compiler uses tail merging and the current |
| 190 | * function does not appear in the stack. |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 191 | */ |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 192 | ha_dump_backtrace(buf, " "); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 193 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 197 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 198 | * 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] | 199 | * with <pfx>. The buffer is only appended and the first output starts by the |
| 200 | * pointer itself. The caller is responsible for making sure the task is not |
| 201 | * going to vanish during the dump. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 202 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 203 | 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] | 204 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 205 | const struct stream *s = NULL; |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 206 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 207 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 209 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 210 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 214 | if (TASK_IS_TASKLET(task)) |
| 215 | chunk_appendf(buf, |
| 216 | "%p (tasklet) calls=%u\n", |
| 217 | task, |
| 218 | task->calls); |
| 219 | else |
| 220 | chunk_appendf(buf, |
| 221 | "%p (task) calls=%u last=%llu%s\n", |
| 222 | task, |
| 223 | task->calls, |
| 224 | task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0, |
| 225 | task->call_date ? " ns ago" : ""); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 226 | |
Willy Tarreau | 2e89b09 | 2020-03-03 17:13:02 +0100 | [diff] [blame] | 227 | chunk_appendf(buf, "%s fct=%p(", pfx, task->process); |
| 228 | resolve_sym_name(buf, NULL, task->process); |
| 229 | chunk_appendf(buf,") ctx=%p", task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 231 | if (task->process == task_run_applet && (appctx = task->context)) |
| 232 | chunk_appendf(buf, "(%s)\n", appctx->applet->name); |
| 233 | else |
| 234 | chunk_appendf(buf, "\n"); |
| 235 | |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 236 | if (task->process == process_stream && task->context) |
| 237 | s = (struct stream *)task->context; |
| 238 | else if (task->process == task_run_applet && task->context) |
| 239 | s = si_strm(((struct appctx *)task->context)->owner); |
| 240 | else if (task->process == si_cs_io_cb && task->context) |
| 241 | s = si_strm((struct stream_interface *)task->context); |
| 242 | |
| 243 | if (s) |
| 244 | stream_dump(buf, s, pfx, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 245 | |
| 246 | #ifdef USE_LUA |
| 247 | hlua = NULL; |
| 248 | if (s && (hlua = s->hlua)) { |
| 249 | chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); |
| 250 | } |
| 251 | else if (task->process == hlua_process_task && (hlua = task->context)) { |
| 252 | chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx); |
| 253 | } |
| 254 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 255 | (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) { |
| 256 | chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx); |
| 257 | } |
| 258 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 259 | (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) { |
| 260 | chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx); |
| 261 | } |
| 262 | |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 263 | if (hlua && hlua->T) { |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 264 | luaL_traceback(hlua->T, hlua->T, NULL, 0); |
| 265 | if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1)) |
| 266 | b_putchr(buf, '\n'); |
| 267 | } |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 268 | else |
| 269 | b_putchr(buf, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 270 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 273 | /* This function dumps all profiling settings. It returns 0 if the output |
| 274 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 275 | */ |
| 276 | static int cli_io_handler_show_threads(struct appctx *appctx) |
| 277 | { |
| 278 | struct stream_interface *si = appctx->owner; |
| 279 | int thr; |
| 280 | |
| 281 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 282 | return 1; |
| 283 | |
| 284 | if (appctx->st0) |
| 285 | thr = appctx->st1; |
| 286 | else |
| 287 | thr = 0; |
| 288 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 289 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 290 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 291 | |
| 292 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 293 | /* failed, try again */ |
| 294 | si_rx_room_blk(si); |
| 295 | appctx->st1 = thr; |
| 296 | return 0; |
| 297 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 298 | return 1; |
| 299 | } |
| 300 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 301 | /* dumps a state of all threads into the trash and on fd #2, then aborts. */ |
| 302 | void ha_panic() |
| 303 | { |
| 304 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 305 | 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] | 306 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 307 | DISGUISE(write(2, trash.area, trash.data)); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 308 | for (;;) |
| 309 | abort(); |
| 310 | } |
| 311 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 312 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 313 | static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private) |
| 314 | { |
| 315 | int code = atoi(args[3]); |
| 316 | |
| 317 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 318 | return 1; |
| 319 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 320 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 321 | exit(code); |
| 322 | return 1; |
| 323 | } |
| 324 | |
Willy Tarreau | 5baf4fe | 2021-01-22 14:15:46 +0100 | [diff] [blame^] | 325 | /* parse a "debug dev bug" command. It always returns 1, though it should never return. |
| 326 | * Note: we make sure not to make the function static so that it appears in the trace. |
| 327 | */ |
| 328 | int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private) |
| 329 | { |
| 330 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 331 | return 1; |
| 332 | |
| 333 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 334 | BUG_ON(one > zero); |
| 335 | return 1; |
| 336 | } |
| 337 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 338 | /* parse a "debug dev close" command. It always returns 1. */ |
| 339 | static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private) |
| 340 | { |
| 341 | int fd; |
| 342 | |
| 343 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 344 | return 1; |
| 345 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 346 | if (!*args[3]) |
| 347 | return cli_err(appctx, "Missing file descriptor number.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 348 | |
| 349 | fd = atoi(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 350 | if (fd < 0 || fd >= global.maxsock) |
| 351 | return cli_err(appctx, "File descriptor out of range.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 352 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 353 | if (!fdtab[fd].owner) |
| 354 | return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 355 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 356 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 357 | fd_delete(fd); |
| 358 | return 1; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 362 | static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private) |
| 363 | { |
| 364 | int delay = atoi(args[3]); |
| 365 | |
| 366 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 367 | return 1; |
| 368 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 369 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 370 | usleep((long)delay * 1000); |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | /* parse a "debug dev log" command. It always returns 1. */ |
| 375 | static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private) |
| 376 | { |
| 377 | int arg; |
| 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 | |
| 390 | send_log(NULL, LOG_INFO, "%s\n", trash.area); |
| 391 | return 1; |
| 392 | } |
| 393 | |
| 394 | /* parse a "debug dev loop" command. It always returns 1. */ |
| 395 | static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private) |
| 396 | { |
| 397 | struct timeval deadline, curr; |
| 398 | int loop = atoi(args[3]); |
| 399 | |
| 400 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 401 | return 1; |
| 402 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 403 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 404 | gettimeofday(&curr, NULL); |
| 405 | tv_ms_add(&deadline, &curr, loop); |
| 406 | |
| 407 | while (tv_ms_cmp(&curr, &deadline) < 0) |
| 408 | gettimeofday(&curr, NULL); |
| 409 | |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | /* parse a "debug dev panic" command. It always returns 1, though it should never return. */ |
| 414 | static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private) |
| 415 | { |
| 416 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 417 | return 1; |
| 418 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 419 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 420 | ha_panic(); |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | /* parse a "debug dev exec" command. It always returns 1. */ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 425 | #if defined(DEBUG_DEV) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 426 | static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private) |
| 427 | { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 428 | int pipefd[2]; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 429 | int arg; |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 430 | int pid; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 431 | |
| 432 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 433 | return 1; |
| 434 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 435 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 436 | chunk_reset(&trash); |
| 437 | for (arg = 3; *args[arg]; arg++) { |
| 438 | if (arg > 3) |
| 439 | chunk_strcat(&trash, " "); |
| 440 | chunk_strcat(&trash, args[arg]); |
| 441 | } |
| 442 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 443 | thread_isolate(); |
| 444 | if (pipe(pipefd) < 0) |
| 445 | goto fail_pipe; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 446 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 447 | if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 448 | goto fail_fcntl; |
| 449 | |
| 450 | if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 451 | goto fail_fcntl; |
| 452 | |
| 453 | pid = fork(); |
| 454 | |
| 455 | if (pid < 0) |
| 456 | goto fail_fork; |
| 457 | else if (pid == 0) { |
| 458 | /* child */ |
| 459 | char *cmd[4] = { "/bin/sh", "-c", 0, 0 }; |
| 460 | |
| 461 | close(0); |
| 462 | dup2(pipefd[1], 1); |
| 463 | dup2(pipefd[1], 2); |
| 464 | |
| 465 | cmd[2] = trash.area; |
| 466 | execvp(cmd[0], cmd); |
| 467 | printf("execvp() failed\n"); |
| 468 | exit(1); |
| 469 | } |
| 470 | |
| 471 | /* parent */ |
| 472 | thread_release(); |
| 473 | close(pipefd[1]); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 474 | chunk_reset(&trash); |
| 475 | while (1) { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 476 | size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data); |
| 477 | if (ret <= 0) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 478 | break; |
| 479 | trash.data += ret; |
| 480 | if (trash.data + 20 == trash.size) { |
| 481 | chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n"); |
| 482 | break; |
| 483 | } |
| 484 | } |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 485 | close(pipefd[0]); |
| 486 | waitpid(pid, NULL, WNOHANG); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 487 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 488 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 489 | |
| 490 | fail_fork: |
| 491 | fail_fcntl: |
| 492 | close(pipefd[0]); |
| 493 | close(pipefd[1]); |
| 494 | fail_pipe: |
| 495 | thread_release(); |
| 496 | return cli_err(appctx, "Failed to execute command.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 497 | } |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 498 | #endif |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 499 | |
| 500 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 501 | static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private) |
| 502 | { |
| 503 | unsigned long start, len; |
| 504 | |
| 505 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 506 | return 1; |
| 507 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 508 | if (!*args[3]) |
| 509 | return cli_err(appctx, "Missing memory address to dump from.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 510 | |
| 511 | start = strtoul(args[3], NULL, 0); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 512 | if (!start) |
| 513 | return cli_err(appctx, "Will not dump from NULL address.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 515 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 516 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 517 | /* by default, dump ~128 till next block of 16 */ |
| 518 | len = strtoul(args[4], NULL, 0); |
| 519 | if (!len) |
| 520 | len = ((start + 128) & -16) - start; |
| 521 | |
| 522 | chunk_reset(&trash); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 523 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 524 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 525 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 529 | static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private) |
| 530 | { |
| 531 | int thr = 0; |
| 532 | int sig = SIGABRT; |
| 533 | |
| 534 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 535 | return 1; |
| 536 | |
| 537 | if (*args[3]) |
| 538 | thr = atoi(args[3]); |
| 539 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 540 | if (thr < 0 || thr > global.nbthread) |
| 541 | 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] | 542 | |
| 543 | if (*args[4]) |
| 544 | sig = atoi(args[4]); |
| 545 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 546 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 547 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 548 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 549 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 550 | raise(sig); |
| 551 | return 1; |
| 552 | } |
| 553 | |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 554 | /* parse a "debug dev write" command. It always returns 1. */ |
| 555 | static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private) |
| 556 | { |
| 557 | unsigned long len; |
| 558 | |
| 559 | if (!*args[3]) |
| 560 | return cli_err(appctx, "Missing output size.\n"); |
| 561 | |
| 562 | len = strtoul(args[3], NULL, 0); |
| 563 | if (len >= trash.size) |
| 564 | return cli_err(appctx, "Output too large, must be <tune.bufsize.\n"); |
| 565 | |
| 566 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 567 | |
| 568 | chunk_reset(&trash); |
| 569 | trash.data = len; |
| 570 | memset(trash.area, '.', trash.data); |
| 571 | trash.area[trash.data] = 0; |
| 572 | for (len = 64; len < trash.data; len += 64) |
| 573 | trash.area[len] = '\n'; |
| 574 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 575 | } |
| 576 | |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 577 | /* parse a "debug dev stream" command */ |
| 578 | /* |
| 579 | * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \ |
| 580 | * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \ |
| 581 | * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \ |
| 582 | * [sif.s[=<state>]] [sib.s[=<state>]] |
| 583 | */ |
| 584 | static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private) |
| 585 | { |
| 586 | struct stream *s = si_strm(appctx->owner); |
| 587 | int arg; |
| 588 | void *ptr; |
| 589 | int size; |
| 590 | const char *word, *end; |
| 591 | struct ist name; |
| 592 | char *msg = NULL; |
| 593 | char *endarg; |
| 594 | unsigned long long old, new; |
| 595 | |
| 596 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 597 | return 1; |
| 598 | |
| 599 | ptr = NULL; size = 0; |
| 600 | |
| 601 | if (!*args[3]) { |
| 602 | return cli_err(appctx, |
| 603 | "Usage: debug dev stream { <obj> <op> <value> | wake }*\n" |
| 604 | " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n" |
| 605 | " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n" |
| 606 | " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n" |
| 607 | " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n" |
| 608 | " 'wake' wakes the stream asssigned to 'strm' (default: current)\n" |
| 609 | ); |
| 610 | } |
| 611 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 612 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 613 | for (arg = 3; *args[arg]; arg++) { |
| 614 | old = 0; |
| 615 | end = word = args[arg]; |
| 616 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 617 | end++; |
| 618 | name = ist2(word, end - word); |
| 619 | if (isteq(name, ist("strm"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 620 | ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 621 | } else if (isteq(name, ist("strm.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 622 | ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 623 | } else if (isteq(name, ist("txn.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 624 | 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] | 625 | } else if (isteq(name, ist("req.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 626 | 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] | 627 | } else if (isteq(name, ist("res.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 628 | 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] | 629 | } else if (isteq(name, ist("req.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 630 | 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] | 631 | } else if (isteq(name, ist("res.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 632 | 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] | 633 | } else if (isteq(name, ist("req.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 634 | 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] | 635 | } else if (isteq(name, ist("res.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 636 | 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] | 637 | } else if (isteq(name, ist("sif.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 638 | 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] | 639 | } else if (isteq(name, ist("sib.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 640 | 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] | 641 | } else if (isteq(name, ist("sif.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 642 | 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] | 643 | } else if (isteq(name, ist("sib.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 644 | 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] | 645 | } else if (isteq(name, ist("sif.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 646 | 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] | 647 | } else if (isteq(name, ist("sib.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 648 | 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] | 649 | } else if (isteq(name, ist("wake"))) { |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 650 | if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1)) |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 651 | task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG); |
| 652 | continue; |
| 653 | } else |
| 654 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word)); |
| 655 | |
| 656 | /* read previous value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 657 | 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] | 658 | if (size == 8) |
| 659 | old = read_u64(ptr); |
| 660 | else if (size == 4) |
| 661 | old = read_u32(ptr); |
| 662 | else if (size == 2) |
| 663 | old = read_u16(ptr); |
| 664 | else |
| 665 | old = *(const uint8_t *)ptr; |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 666 | } else { |
| 667 | memprintf(&msg, |
| 668 | "%sSkipping inaccessible pointer %p for field '%.*s'.\n", |
| 669 | msg ? msg : "", ptr, (int)(end - word), word); |
| 670 | continue; |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* parse the new value . */ |
| 674 | new = strtoll(end + 1, &endarg, 0); |
| 675 | if (end[1] && *endarg) { |
| 676 | if (strcmp(end + 1, "now") == 0) |
| 677 | new = now_ms; |
| 678 | else { |
| 679 | memprintf(&msg, |
| 680 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 681 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 682 | continue; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | switch (*end) { |
| 687 | case '\0': /* show */ |
| 688 | memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old); |
| 689 | new = old; // do not change the value |
| 690 | break; |
| 691 | |
| 692 | case '=': /* set */ |
| 693 | break; |
| 694 | |
| 695 | case '^': /* XOR */ |
| 696 | new = old ^ new; |
| 697 | break; |
| 698 | |
| 699 | case '+': /* OR */ |
| 700 | new = old | new; |
| 701 | break; |
| 702 | |
| 703 | case '-': /* AND NOT */ |
| 704 | new = old & ~new; |
| 705 | break; |
| 706 | |
| 707 | default: |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | /* write the new value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 712 | if (new != old) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 713 | if (size == 8) |
| 714 | write_u64(ptr, new); |
| 715 | else if (size == 4) |
| 716 | write_u32(ptr, new); |
| 717 | else if (size == 2) |
| 718 | write_u16(ptr, new); |
| 719 | else |
| 720 | *(uint8_t *)ptr = new; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | if (msg && *msg) |
| 725 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 726 | return 1; |
| 727 | } |
| 728 | |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 729 | static struct task *debug_task_handler(struct task *t, void *ctx, unsigned short state) |
| 730 | { |
| 731 | unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 732 | unsigned long inter = tctx[1]; |
| 733 | unsigned long rnd; |
| 734 | |
| 735 | t->expire = tick_add(now_ms, inter); |
| 736 | |
| 737 | /* half of the calls will wake up another entry */ |
Willy Tarreau | 8a069eb | 2020-11-30 16:17:33 +0100 | [diff] [blame] | 738 | rnd = debug_prng(); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 739 | if (rnd & 1) { |
| 740 | rnd >>= 1; |
| 741 | rnd %= tctx[0]; |
| 742 | rnd = tctx[rnd + 2]; |
| 743 | |
| 744 | if (rnd & 1) |
| 745 | task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG); |
| 746 | else |
| 747 | tasklet_wakeup((struct tasklet *)rnd); |
| 748 | } |
| 749 | return t; |
| 750 | } |
| 751 | |
| 752 | static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned short state) |
| 753 | { |
| 754 | unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 755 | unsigned long rnd; |
| 756 | int i; |
| 757 | |
| 758 | /* wake up two random entries */ |
| 759 | for (i = 0; i < 2; i++) { |
Willy Tarreau | 8a069eb | 2020-11-30 16:17:33 +0100 | [diff] [blame] | 760 | rnd = debug_prng() % tctx[0]; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 761 | rnd = tctx[rnd + 2]; |
| 762 | |
| 763 | if (rnd & 1) |
| 764 | task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG); |
| 765 | else |
| 766 | tasklet_wakeup((struct tasklet *)rnd); |
| 767 | } |
| 768 | return t; |
| 769 | } |
| 770 | |
| 771 | /* parse a "debug dev sched" command |
| 772 | * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>] |
| 773 | */ |
| 774 | static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private) |
| 775 | { |
| 776 | int arg; |
| 777 | void *ptr; |
| 778 | int size; |
| 779 | const char *word, *end; |
| 780 | struct ist name; |
| 781 | char *msg = NULL; |
| 782 | char *endarg; |
| 783 | unsigned long long new; |
| 784 | unsigned long count = 0; |
| 785 | unsigned long thrid = 0; |
| 786 | unsigned int inter = 0; |
| 787 | unsigned long mask, tmask; |
| 788 | unsigned long i; |
| 789 | int mode = 0; // 0 = tasklet; 1 = task |
| 790 | int single = 0; |
| 791 | unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 792 | |
| 793 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 794 | return 1; |
| 795 | |
| 796 | ptr = NULL; size = 0; |
| 797 | mask = all_threads_mask; |
| 798 | |
| 799 | if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) { |
| 800 | return cli_err(appctx, |
| 801 | "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n" |
| 802 | " <obj> = {count | mask | inter | single }\n" |
| 803 | " <value> = 64-bit dec/hex integer (0x prefix supported)\n" |
| 804 | ); |
| 805 | } |
| 806 | |
| 807 | mode = strcmp(args[3], "task") == 0; |
| 808 | |
| 809 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 810 | for (arg = 4; *args[arg]; arg++) { |
| 811 | end = word = args[arg]; |
| 812 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 813 | end++; |
| 814 | name = ist2(word, end - word); |
| 815 | if (isteq(name, ist("count"))) { |
| 816 | ptr = &count; size = sizeof(count); |
| 817 | } else if (isteq(name, ist("mask"))) { |
| 818 | ptr = &mask; size = sizeof(mask); |
| 819 | } else if (isteq(name, ist("tid"))) { |
| 820 | ptr = &thrid; size = sizeof(thrid); |
| 821 | } else if (isteq(name, ist("inter"))) { |
| 822 | ptr = &inter; size = sizeof(inter); |
| 823 | } else if (isteq(name, ist("single"))) { |
| 824 | ptr = &single; size = sizeof(single); |
| 825 | } else |
| 826 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word)); |
| 827 | |
| 828 | /* parse the new value . */ |
| 829 | new = strtoll(end + 1, &endarg, 0); |
| 830 | if (end[1] && *endarg) { |
| 831 | memprintf(&msg, |
| 832 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 833 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 834 | continue; |
| 835 | } |
| 836 | |
| 837 | /* write the new value */ |
| 838 | if (size == 8) |
| 839 | write_u64(ptr, new); |
| 840 | else if (size == 4) |
| 841 | write_u32(ptr, new); |
| 842 | else if (size == 2) |
| 843 | write_u16(ptr, new); |
| 844 | else |
| 845 | *(uint8_t *)ptr = new; |
| 846 | } |
| 847 | |
| 848 | tctx = calloc(sizeof(*tctx), count + 2); |
| 849 | if (!tctx) |
| 850 | goto fail; |
| 851 | |
| 852 | tctx[0] = (unsigned long)count; |
| 853 | tctx[1] = (unsigned long)inter; |
| 854 | |
| 855 | mask &= all_threads_mask; |
| 856 | if (!mask) |
| 857 | mask = tid_bit; |
| 858 | |
| 859 | tmask = 0; |
| 860 | for (i = 0; i < count; i++) { |
| 861 | if (single || mode == 0) { |
| 862 | /* look for next bit matching a bit in mask or loop back to zero */ |
| 863 | for (tmask <<= 1; !(mask & tmask); ) { |
| 864 | if (!(mask & -tmask)) |
| 865 | tmask = 1; |
| 866 | else |
| 867 | tmask <<= 1; |
| 868 | } |
| 869 | } else { |
| 870 | /* multi-threaded task */ |
| 871 | tmask = mask; |
| 872 | } |
| 873 | |
| 874 | /* now, if poly or mask was set, tmask corresponds to the |
| 875 | * valid thread mask to use, otherwise it remains zero. |
| 876 | */ |
| 877 | //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask); |
| 878 | if (mode == 0) { |
| 879 | struct tasklet *tl = tasklet_new(); |
| 880 | |
| 881 | if (!tl) |
| 882 | goto fail; |
| 883 | |
| 884 | if (tmask) |
| 885 | tl->tid = my_ffsl(tmask) - 1; |
| 886 | tl->process = debug_tasklet_handler; |
| 887 | tl->context = tctx; |
| 888 | tctx[i + 2] = (unsigned long)tl; |
| 889 | } else { |
| 890 | struct task *task = task_new(tmask ? tmask : tid_bit); |
| 891 | |
| 892 | if (!task) |
| 893 | goto fail; |
| 894 | |
| 895 | task->process = debug_task_handler; |
| 896 | task->context = tctx; |
| 897 | tctx[i + 2] = (unsigned long)task + 1; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /* start the tasks and tasklets */ |
| 902 | for (i = 0; i < count; i++) { |
| 903 | unsigned long ctx = tctx[i + 2]; |
| 904 | |
| 905 | if (ctx & 1) |
| 906 | task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT); |
| 907 | else |
| 908 | tasklet_wakeup((struct tasklet *)ctx); |
| 909 | } |
| 910 | |
| 911 | if (msg && *msg) |
| 912 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 913 | return 1; |
| 914 | |
| 915 | fail: |
| 916 | /* free partially allocated entries */ |
| 917 | for (i = 0; tctx && i < count; i++) { |
| 918 | unsigned long ctx = tctx[i + 2]; |
| 919 | |
| 920 | if (!ctx) |
| 921 | break; |
| 922 | |
| 923 | if (ctx & 1) |
| 924 | task_destroy((struct task *)(ctx - 1)); |
| 925 | else |
| 926 | tasklet_free((struct tasklet *)ctx); |
| 927 | } |
| 928 | |
| 929 | free(tctx); |
| 930 | return cli_err(appctx, "Not enough memory"); |
| 931 | } |
| 932 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 933 | #if defined(DEBUG_MEM_STATS) |
| 934 | /* CLI parser for the "debug dev memstats" command */ |
| 935 | static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private) |
| 936 | { |
| 937 | extern __attribute__((__weak__)) struct mem_stats __start_mem_stats; |
| 938 | extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats; |
| 939 | |
| 940 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 941 | return 1; |
| 942 | |
| 943 | if (strcmp(args[3], "reset") == 0) { |
| 944 | struct mem_stats *ptr; |
| 945 | |
| 946 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 947 | return 1; |
| 948 | |
| 949 | for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) { |
| 950 | _HA_ATOMIC_STORE(&ptr->calls, 0); |
| 951 | _HA_ATOMIC_STORE(&ptr->size, 0); |
| 952 | } |
| 953 | return 1; |
| 954 | } |
| 955 | |
| 956 | if (strcmp(args[3], "all") == 0) |
| 957 | appctx->ctx.cli.i0 = 1; |
| 958 | |
| 959 | /* otherwise proceed with the dump from p0 to p1 */ |
| 960 | appctx->ctx.cli.p0 = &__start_mem_stats; |
| 961 | appctx->ctx.cli.p1 = &__stop_mem_stats; |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | /* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats |
| 966 | * structs referenced by pointers located between p0 and p1. Dumps all entries |
| 967 | * if i0 > 0, otherwise only non-zero calls. |
| 968 | */ |
| 969 | static int debug_iohandler_memstats(struct appctx *appctx) |
| 970 | { |
| 971 | struct stream_interface *si = appctx->owner; |
| 972 | struct mem_stats *ptr = appctx->ctx.cli.p0; |
| 973 | int ret = 1; |
| 974 | |
| 975 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 976 | goto end; |
| 977 | |
| 978 | chunk_reset(&trash); |
| 979 | |
| 980 | /* we have two inner loops here, one for the proxy, the other one for |
| 981 | * the buffer. |
| 982 | */ |
| 983 | for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) { |
| 984 | const char *type; |
| 985 | const char *name; |
| 986 | const char *p; |
| 987 | |
| 988 | if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0) |
| 989 | continue; |
| 990 | |
| 991 | /* basename only */ |
| 992 | for (p = name = ptr->file; *p; p++) { |
| 993 | if (*p == '/') |
| 994 | name = p + 1; |
| 995 | } |
| 996 | |
| 997 | switch (ptr->type) { |
| 998 | case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break; |
| 999 | case MEM_STATS_TYPE_FREE: type = "FREE"; break; |
| 1000 | case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break; |
| 1001 | case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break; |
| 1002 | case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break; |
| 1003 | default: type = "UNSET"; break; |
| 1004 | } |
| 1005 | |
| 1006 | //chunk_printf(&trash, |
| 1007 | // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 1008 | // name, ptr->line, type, |
| 1009 | // (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 1010 | // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 1011 | |
| 1012 | chunk_printf(&trash, "%s:%d", name, ptr->line); |
| 1013 | while (trash.data < 25) |
| 1014 | trash.area[trash.data++] = ' '; |
| 1015 | chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 1016 | type, |
| 1017 | (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 1018 | (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 1019 | |
| 1020 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 1021 | si_rx_room_blk(si); |
| 1022 | appctx->ctx.cli.p0 = ptr; |
| 1023 | ret = 0; |
| 1024 | break; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | end: |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | #endif |
| 1033 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1034 | #ifndef USE_THREAD_DUMP |
| 1035 | |
| 1036 | /* This function dumps all threads' state to the trash. This version is the |
| 1037 | * most basic one, which doesn't inspect other threads. |
| 1038 | */ |
| 1039 | void ha_thread_dump_all_to_trash() |
| 1040 | { |
| 1041 | unsigned int thr; |
| 1042 | |
| 1043 | for (thr = 0; thr < global.nbthread; thr++) |
| 1044 | ha_thread_dump(&trash, thr, tid); |
| 1045 | } |
| 1046 | |
| 1047 | #else /* below USE_THREAD_DUMP is set */ |
| 1048 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1049 | /* ID of the thread requesting the dump */ |
| 1050 | static unsigned int thread_dump_tid; |
| 1051 | |
| 1052 | /* points to the buffer where the dump functions should write. It must |
| 1053 | * have already been initialized by the requester. Nothing is done if |
| 1054 | * it's NULL. |
| 1055 | */ |
| 1056 | struct buffer *thread_dump_buffer = NULL; |
| 1057 | |
| 1058 | void ha_thread_dump_all_to_trash() |
| 1059 | { |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1060 | unsigned long old; |
| 1061 | |
| 1062 | while (1) { |
| 1063 | old = 0; |
| 1064 | if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask)) |
| 1065 | break; |
| 1066 | ha_thread_relax(); |
| 1067 | } |
| 1068 | |
| 1069 | thread_dump_buffer = &trash; |
| 1070 | thread_dump_tid = tid; |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 1071 | ha_tkillall(DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* handles DEBUGSIG to dump the state of the thread it's working on */ |
| 1075 | void debug_handler(int sig, siginfo_t *si, void *arg) |
| 1076 | { |
Willy Tarreau | 82aafc4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 1077 | /* first, let's check it's really for us and that we didn't just get |
| 1078 | * a spurious DEBUGSIG. |
| 1079 | */ |
| 1080 | if (!(threads_to_dump & tid_bit)) |
| 1081 | return; |
| 1082 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1083 | /* There are 4 phases in the dump process: |
| 1084 | * 1- wait for our turn, i.e. when all lower bits are gone. |
| 1085 | * 2- perform the action if our bit is set |
| 1086 | * 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] | 1087 | * the last one and have to put them all as a signal |
| 1088 | * 4- wait out bit to re-appear, then clear it and quit. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1089 | */ |
| 1090 | |
| 1091 | /* wait for all previous threads to finish first */ |
| 1092 | while (threads_to_dump & (tid_bit - 1)) |
| 1093 | ha_thread_relax(); |
| 1094 | |
| 1095 | /* dump if needed */ |
| 1096 | if (threads_to_dump & tid_bit) { |
| 1097 | if (thread_dump_buffer) |
| 1098 | ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid); |
| 1099 | if ((threads_to_dump & all_threads_mask) == tid_bit) { |
| 1100 | /* last one */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 1101 | HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1102 | thread_dump_buffer = NULL; |
| 1103 | } |
| 1104 | else |
| 1105 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
| 1106 | } |
| 1107 | |
| 1108 | /* 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] | 1109 | * bits again to broadcast the leaving condition so we'll see ourselves |
| 1110 | * present again. This way the threads_to_dump variable never passes to |
| 1111 | * zero until all visitors have stopped waiting. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1112 | */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 1113 | while (!(threads_to_dump & tid_bit)) |
| 1114 | ha_thread_relax(); |
| 1115 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 1116 | |
| 1117 | /* mark the current thread as stuck to detect it upon next invocation |
| 1118 | * if it didn't move. |
| 1119 | */ |
| 1120 | if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit)) |
| 1121 | ti->flags |= TI_FL_STUCK; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | static int init_debug_per_thread() |
| 1125 | { |
| 1126 | sigset_t set; |
| 1127 | |
| 1128 | /* unblock the DEBUGSIG signal we intend to use */ |
| 1129 | sigemptyset(&set); |
| 1130 | sigaddset(&set, DEBUGSIG); |
| 1131 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 1132 | return 1; |
| 1133 | } |
| 1134 | |
| 1135 | static int init_debug() |
| 1136 | { |
| 1137 | struct sigaction sa; |
Willy Tarreau | 2f1227e | 2021-01-22 12:12:29 +0100 | [diff] [blame] | 1138 | void *callers[1]; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1139 | |
Willy Tarreau | 0214b45 | 2020-03-04 06:01:40 +0100 | [diff] [blame] | 1140 | /* calling backtrace() will access libgcc at runtime. We don't want to |
| 1141 | * do it after the chroot, so let's perform a first call to have it |
| 1142 | * ready in memory for later use. |
| 1143 | */ |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 1144 | my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1145 | sa.sa_handler = NULL; |
| 1146 | sa.sa_sigaction = debug_handler; |
| 1147 | sigemptyset(&sa.sa_mask); |
| 1148 | sa.sa_flags = SA_SIGINFO; |
| 1149 | sigaction(DEBUGSIG, &sa, NULL); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1150 | return ERR_NONE; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | REGISTER_POST_CHECK(init_debug); |
| 1154 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 1155 | |
| 1156 | #endif /* USE_THREAD_DUMP */ |
| 1157 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1158 | /* register cli keywords */ |
| 1159 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 5baf4fe | 2021-01-22 14:15:46 +0100 | [diff] [blame^] | 1160 | {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON()", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 1161 | {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1162 | {{ "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] | 1163 | #if defined(DEBUG_DEV) |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 1164 | {{ "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] | 1165 | #endif |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 1166 | {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1167 | {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1168 | {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1169 | {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1170 | #if defined(DEBUG_MEM_STATS) |
| 1171 | {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT }, |
| 1172 | #endif |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 1173 | {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1174 | {{ "debug", "dev", "sched", NULL }, "debug dev sched ... : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 1175 | {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1176 | {{ "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] | 1177 | {{ "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] | 1178 | {{ "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] | 1179 | {{},} |
| 1180 | }}; |
| 1181 | |
| 1182 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |