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