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