blob: 5accb0bd5feb0e8979ec95cfac4337b3b8148501 [file] [log] [blame]
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001/*
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 Tarreauf5b4e062020-03-03 15:40:23 +010013
Willy Tarreau368bff42019-12-06 17:18:28 +010014#include <fcntl.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020015#include <signal.h>
16#include <time.h>
17#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020018#include <stdlib.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020019#include <syslog.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010020#include <sys/types.h>
21#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020022
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020024#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020026#include <haproxy/clock.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020027#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/fd.h>
29#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020030#include <haproxy/hlua.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020031#include <haproxy/http_ana.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/net_helper.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020035#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020036#include <haproxy/thread.h>
Willy Tarreau55542642021-10-08 09:33:24 +020037#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020040
Willy Tarreau4e2b6462019-05-16 17:44:30 +020041
Willy Tarreaua37cb182019-07-31 19:20:39 +020042/* mask of threads still having to dump, used to respect ordering. Only used
43 * when USE_THREAD_DUMP is set.
44 */
45volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020046unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020047
Willy Tarreau123fc972021-01-22 13:52:41 +010048/* dumps a backtrace of the current thread that is appended to buffer <buf>.
49 * Lines are prefixed with the string <prefix> which may be empty (used for
50 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010051 * the function does not appear in the call stack. The <dump> argument
52 * indicates what dump state to start from, and should usually be zero. It
53 * may be among the following values:
54 * - 0: search usual callers before step 1, or directly jump to 2
55 * - 1: skip usual callers before step 2
56 * - 2: dump until polling loop, scheduler, or main() (excluded)
57 * - 3: end
58 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010059 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010060void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010061{
62 struct buffer bak;
63 char pfx2[100];
64 void *callers[100];
65 int j, nptrs;
66 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010067
68 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
69 if (!nptrs)
70 return;
71
72 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
73 pfx2[0] = 0;
74
75 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
76 * produce similar output to the following:
77 */
78 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010079 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
80 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010081 /* we failed to spot the starting point of the
82 * dump, let's start over dumping everything we
83 * have.
84 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010085 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010086 j = 0;
87 }
88 bak = *buf;
89 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
90 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010091 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010092 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010093 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +010094 */
Willy Tarreaua8459b22021-01-22 14:12:27 +010095 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
96 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010097 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +010098 *buf = bak;
99 continue;
100 }
101
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100102 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100103 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100104 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
105 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100106 *buf = bak;
107 continue;
108 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100109 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100110 }
111
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100112 if ((dump & 3) == 2) {
113 /* still dumping */
114 if (dump == 6) {
115 /* we only stop *after* main and we must send the LF */
116 if (addr == main) {
117 j = nptrs;
118 dump++;
119 }
120 }
121 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
122 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100123 *buf = bak;
124 break;
125 }
126 }
127 /* OK, line dumped */
128 chunk_appendf(buf, "\n");
129 }
130}
131
Willy Tarreaua8459b22021-01-22 14:12:27 +0100132/* dump a backtrace of current thread's stack to stderr. */
133void ha_backtrace_to_stderr()
134{
135 char area[2048];
136 struct buffer b = b_make(area, sizeof(area), 0, 0);
137
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100138 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100139 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100140 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100141}
142
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200143/* Dumps to the buffer some known information for the desired thread, and
144 * optionally extra info for the current thread. The dump will be appended to
145 * the buffer, so the caller is responsible for preliminary initializing it.
146 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200147 * in front of the calling thread's line (usually it's tid). Any stuck thread
148 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200149 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200150void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200151{
152 unsigned long thr_bit = 1UL << thr;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200153 unsigned long long p = ha_thread_ctx[thr].prev_cpu_time;
Willy Tarreau21694982021-10-08 15:09:17 +0200154 unsigned long long n = now_cpu_time_thread(thr);
Willy Tarreaua0b99532021-09-30 18:48:37 +0200155 int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200156
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200157 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200158 "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
Willy Tarreaua3870b72021-09-13 19:24:47 +0200159 " %2u/%-2u stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200160 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200161 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200162 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200163 !!(global_tasks_mask & thr_bit),
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200164 !eb_is_empty(&ha_thread_ctx[thr].timers),
165 !eb_is_empty(&ha_thread_ctx[thr].rqueue),
166 !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) &&
167 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) &&
168 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) &&
169 MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)),
170 ha_thread_ctx[thr].tasks_in_list,
171 ha_thread_ctx[thr].rq_total,
Willy Tarreaua3870b72021-09-13 19:24:47 +0200172 ha_thread_info[thr].tg->tgid, ha_thread_info[thr].ltid + 1,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200173 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200174 !!(task_profiling_mask & thr_bit));
175
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200176 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200177 " harmless=%d wantrdv=%d",
178 !!(threads_harmless_mask & thr_bit),
179 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200180
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200181 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200182 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200183
Willy Tarreaua3870b72021-09-13 19:24:47 +0200184 /* this is the end of what we can dump from outside the current thread */
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200185
186 if (thr != tid)
187 return;
188
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200189 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200190 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100191
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100192 if (stuck) {
193 /* We only emit the backtrace for stuck threads in order not to
194 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100195 * Please leave this as the last instruction in this function
196 * so that the compiler uses tail merging and the current
197 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100198 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100199 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100200 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200201}
202
203
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200204/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200205 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200206 * with <pfx>. The buffer is only appended and the first output starts by the
207 * pointer itself. The caller is responsible for making sure the task is not
208 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200209 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200210void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200211{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200212 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200213 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200214 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200215
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200216 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200217 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200218 return;
219 }
220
Willy Tarreau20db9112019-05-17 14:14:35 +0200221 if (TASK_IS_TASKLET(task))
222 chunk_appendf(buf,
223 "%p (tasklet) calls=%u\n",
224 task,
225 task->calls);
226 else
227 chunk_appendf(buf,
228 "%p (task) calls=%u last=%llu%s\n",
229 task,
230 task->calls,
231 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
232 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200233
Willy Tarreau2e89b092020-03-03 17:13:02 +0100234 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
235 resolve_sym_name(buf, NULL, task->process);
236 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200237
Willy Tarreaua512b022019-08-21 14:12:19 +0200238 if (task->process == task_run_applet && (appctx = task->context))
239 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
240 else
241 chunk_appendf(buf, "\n");
242
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200243 if (task->process == process_stream && task->context)
244 s = (struct stream *)task->context;
245 else if (task->process == task_run_applet && task->context)
246 s = si_strm(((struct appctx *)task->context)->owner);
247 else if (task->process == si_cs_io_cb && task->context)
248 s = si_strm((struct stream_interface *)task->context);
249
250 if (s)
251 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200252
253#ifdef USE_LUA
254 hlua = NULL;
255 if (s && (hlua = s->hlua)) {
256 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
257 }
258 else if (task->process == hlua_process_task && (hlua = task->context)) {
259 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
260 }
261 else if (task->process == task_run_applet && (appctx = task->context) &&
262 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
263 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
264 }
265 else if (task->process == task_run_applet && (appctx = task->context) &&
266 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
267 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
268 }
269
Christopher Faulet471425f2020-07-24 19:08:05 +0200270 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100271 chunk_appendf(buf, "stack traceback:\n ");
272 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
273 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200274 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200275 else
276 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200277#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200278}
279
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200280/* This function dumps all profiling settings. It returns 0 if the output
281 * buffer is full and it needs to be called again, otherwise non-zero.
282 */
283static int cli_io_handler_show_threads(struct appctx *appctx)
284{
285 struct stream_interface *si = appctx->owner;
286 int thr;
287
288 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
289 return 1;
290
291 if (appctx->st0)
292 thr = appctx->st1;
293 else
294 thr = 0;
295
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200296 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200297 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200298
299 if (ci_putchk(si_ic(si), &trash) == -1) {
300 /* failed, try again */
301 si_rx_room_blk(si);
302 appctx->st1 = thr;
303 return 0;
304 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200305 return 1;
306}
307
Willy Tarreau6ab7b212021-12-28 09:57:10 +0100308#if defined(HA_HAVE_DUMP_LIBS)
309/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
310static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
311{
312 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
313 return 1;
314
315 chunk_reset(&trash);
316 if (dump_libs(&trash, 1))
317 return cli_msg(appctx, LOG_INFO, trash.area);
318 else
319 return 0;
320}
321#endif
322
Willy Tarreau56131ca2019-05-20 13:48:29 +0200323/* dumps a state of all threads into the trash and on fd #2, then aborts. */
324void ha_panic()
325{
326 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200327 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200328 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100329 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200330 for (;;)
331 abort();
332}
333
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200334/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
335static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
336{
337 int code = atoi(args[3]);
338
339 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
340 return 1;
341
Willy Tarreau4781b152021-04-06 13:53:36 +0200342 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200343 exit(code);
344 return 1;
345}
346
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100347/* parse a "debug dev bug" command. It always returns 1, though it should never return.
348 * Note: we make sure not to make the function static so that it appears in the trace.
349 */
350int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
351{
352 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
353 return 1;
354
Willy Tarreau4781b152021-04-06 13:53:36 +0200355 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100356 BUG_ON(one > zero);
357 return 1;
358}
359
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200360/* parse a "debug dev close" command. It always returns 1. */
361static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
362{
363 int fd;
364
365 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
366 return 1;
367
Willy Tarreau9d008692019-08-09 11:21:01 +0200368 if (!*args[3])
369 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200370
371 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200372 if (fd < 0 || fd >= global.maxsock)
373 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200374
Willy Tarreau9d008692019-08-09 11:21:01 +0200375 if (!fdtab[fd].owner)
376 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377
Willy Tarreau4781b152021-04-06 13:53:36 +0200378 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379 fd_delete(fd);
380 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200381}
382
383/* parse a "debug dev delay" command. It always returns 1. */
384static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
385{
386 int delay = atoi(args[3]);
387
388 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
389 return 1;
390
Willy Tarreau4781b152021-04-06 13:53:36 +0200391 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200392 usleep((long)delay * 1000);
393 return 1;
394}
395
396/* parse a "debug dev log" command. It always returns 1. */
397static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
398{
399 int arg;
400
401 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
402 return 1;
403
Willy Tarreau4781b152021-04-06 13:53:36 +0200404 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200405 chunk_reset(&trash);
406 for (arg = 3; *args[arg]; arg++) {
407 if (arg > 3)
408 chunk_strcat(&trash, " ");
409 chunk_strcat(&trash, args[arg]);
410 }
411
412 send_log(NULL, LOG_INFO, "%s\n", trash.area);
413 return 1;
414}
415
416/* parse a "debug dev loop" command. It always returns 1. */
417static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
418{
419 struct timeval deadline, curr;
420 int loop = atoi(args[3]);
421
422 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
423 return 1;
424
Willy Tarreau4781b152021-04-06 13:53:36 +0200425 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200426 gettimeofday(&curr, NULL);
427 tv_ms_add(&deadline, &curr, loop);
428
429 while (tv_ms_cmp(&curr, &deadline) < 0)
430 gettimeofday(&curr, NULL);
431
432 return 1;
433}
434
435/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
436static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
437{
438 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
439 return 1;
440
Willy Tarreau4781b152021-04-06 13:53:36 +0200441 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200442 ha_panic();
443 return 1;
444}
445
446/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200447#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200448static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
449{
Willy Tarreau368bff42019-12-06 17:18:28 +0100450 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200451 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100452 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200453
454 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
455 return 1;
456
Willy Tarreau4781b152021-04-06 13:53:36 +0200457 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200458 chunk_reset(&trash);
459 for (arg = 3; *args[arg]; arg++) {
460 if (arg > 3)
461 chunk_strcat(&trash, " ");
462 chunk_strcat(&trash, args[arg]);
463 }
464
Willy Tarreau368bff42019-12-06 17:18:28 +0100465 thread_isolate();
466 if (pipe(pipefd) < 0)
467 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200468
Willy Tarreau368bff42019-12-06 17:18:28 +0100469 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
470 goto fail_fcntl;
471
472 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
473 goto fail_fcntl;
474
475 pid = fork();
476
477 if (pid < 0)
478 goto fail_fork;
479 else if (pid == 0) {
480 /* child */
481 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
482
483 close(0);
484 dup2(pipefd[1], 1);
485 dup2(pipefd[1], 2);
486
487 cmd[2] = trash.area;
488 execvp(cmd[0], cmd);
489 printf("execvp() failed\n");
490 exit(1);
491 }
492
493 /* parent */
494 thread_release();
495 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200496 chunk_reset(&trash);
497 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100498 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
499 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200500 break;
501 trash.data += ret;
502 if (trash.data + 20 == trash.size) {
503 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
504 break;
505 }
506 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100507 close(pipefd[0]);
508 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200509 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200510 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100511
512 fail_fork:
513 fail_fcntl:
514 close(pipefd[0]);
515 close(pipefd[1]);
516 fail_pipe:
517 thread_release();
518 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200519}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200520#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200521
522/* parse a "debug dev hex" command. It always returns 1. */
523static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
524{
525 unsigned long start, len;
526
527 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
528 return 1;
529
Willy Tarreau9d008692019-08-09 11:21:01 +0200530 if (!*args[3])
531 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200532
533 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200534 if (!start)
535 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200536
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200538
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200539 /* by default, dump ~128 till next block of 16 */
540 len = strtoul(args[4], NULL, 0);
541 if (!len)
542 len = ((start + 128) & -16) - start;
543
544 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200545 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200546 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200547 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200548}
549
Willy Tarreau48129be2021-05-04 18:40:50 +0200550/* parse a "debug dev sym <addr>" command. It always returns 1. */
551static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
552{
553 unsigned long addr;
554
555 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
556 return 1;
557
558 if (!*args[3])
559 return cli_err(appctx, "Missing memory address to be resolved.\n");
560
561 _HA_ATOMIC_INC(&debug_commands_issued);
562
563 addr = strtoul(args[3], NULL, 0);
564 chunk_printf(&trash, "%#lx resolves to ", addr);
565 resolve_sym_name(&trash, NULL, (const void *)addr);
566 chunk_appendf(&trash, "\n");
567
568 return cli_msg(appctx, LOG_INFO, trash.area);
569}
570
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200571/* parse a "debug dev tkill" command. It always returns 1. */
572static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
573{
574 int thr = 0;
575 int sig = SIGABRT;
576
577 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
578 return 1;
579
580 if (*args[3])
581 thr = atoi(args[3]);
582
Willy Tarreau9d008692019-08-09 11:21:01 +0200583 if (thr < 0 || thr > global.nbthread)
584 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200585
586 if (*args[4])
587 sig = atoi(args[4]);
588
Willy Tarreau4781b152021-04-06 13:53:36 +0200589 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200590 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200591 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200592 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200593 raise(sig);
594 return 1;
595}
596
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100597/* parse a "debug dev write" command. It always returns 1. */
598static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
599{
600 unsigned long len;
601
602 if (!*args[3])
603 return cli_err(appctx, "Missing output size.\n");
604
605 len = strtoul(args[3], NULL, 0);
606 if (len >= trash.size)
607 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
608
Willy Tarreau4781b152021-04-06 13:53:36 +0200609 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100610
611 chunk_reset(&trash);
612 trash.data = len;
613 memset(trash.area, '.', trash.data);
614 trash.area[trash.data] = 0;
615 for (len = 64; len < trash.data; len += 64)
616 trash.area[len] = '\n';
617 return cli_msg(appctx, LOG_INFO, trash.area);
618}
619
Willy Tarreau68680bb2019-10-23 17:23:25 +0200620/* parse a "debug dev stream" command */
621/*
622 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
623 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
624 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
625 * [sif.s[=<state>]] [sib.s[=<state>]]
626 */
627static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
628{
629 struct stream *s = si_strm(appctx->owner);
630 int arg;
631 void *ptr;
632 int size;
633 const char *word, *end;
634 struct ist name;
635 char *msg = NULL;
636 char *endarg;
637 unsigned long long old, new;
638
639 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
640 return 1;
641
642 ptr = NULL; size = 0;
643
644 if (!*args[3]) {
645 return cli_err(appctx,
646 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
647 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
648 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
649 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
650 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
651 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
652 );
653 }
654
Willy Tarreau4781b152021-04-06 13:53:36 +0200655 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200656 for (arg = 3; *args[arg]; arg++) {
657 old = 0;
658 end = word = args[arg];
659 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
660 end++;
661 name = ist2(word, end - word);
662 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200663 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200664 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200665 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200666 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200667 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200668 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200669 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200670 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200671 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200672 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200673 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200674 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200675 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200676 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200677 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200678 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200679 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200680 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200681 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200682 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200683 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200684 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200685 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200686 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200687 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200688 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200689 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200690 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200691 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200692 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200693 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200694 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
695 continue;
696 } else
697 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
698
699 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200700 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200701 if (size == 8)
702 old = read_u64(ptr);
703 else if (size == 4)
704 old = read_u32(ptr);
705 else if (size == 2)
706 old = read_u16(ptr);
707 else
708 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200709 } else {
710 memprintf(&msg,
711 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
712 msg ? msg : "", ptr, (int)(end - word), word);
713 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200714 }
715
716 /* parse the new value . */
717 new = strtoll(end + 1, &endarg, 0);
718 if (end[1] && *endarg) {
719 if (strcmp(end + 1, "now") == 0)
720 new = now_ms;
721 else {
722 memprintf(&msg,
723 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
724 msg ? msg : "", end + 1, (int)(end - word), word);
725 continue;
726 }
727 }
728
729 switch (*end) {
730 case '\0': /* show */
731 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
732 new = old; // do not change the value
733 break;
734
735 case '=': /* set */
736 break;
737
738 case '^': /* XOR */
739 new = old ^ new;
740 break;
741
742 case '+': /* OR */
743 new = old | new;
744 break;
745
746 case '-': /* AND NOT */
747 new = old & ~new;
748 break;
749
750 default:
751 break;
752 }
753
754 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200755 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200756 if (size == 8)
757 write_u64(ptr, new);
758 else if (size == 4)
759 write_u32(ptr, new);
760 else if (size == 2)
761 write_u16(ptr, new);
762 else
763 *(uint8_t *)ptr = new;
764 }
765 }
766
767 if (msg && *msg)
768 return cli_dynmsg(appctx, LOG_INFO, msg);
769 return 1;
770}
771
Willy Tarreau144f84a2021-03-02 16:09:26 +0100772static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100773{
774 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
775 unsigned long inter = tctx[1];
776 unsigned long rnd;
777
778 t->expire = tick_add(now_ms, inter);
779
780 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100781 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100782 if (rnd & 1) {
783 rnd >>= 1;
784 rnd %= tctx[0];
785 rnd = tctx[rnd + 2];
786
787 if (rnd & 1)
788 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
789 else
790 tasklet_wakeup((struct tasklet *)rnd);
791 }
792 return t;
793}
794
Willy Tarreau144f84a2021-03-02 16:09:26 +0100795static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100796{
797 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
798 unsigned long rnd;
799 int i;
800
801 /* wake up two random entries */
802 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100803 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100804 rnd = tctx[rnd + 2];
805
806 if (rnd & 1)
807 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
808 else
809 tasklet_wakeup((struct tasklet *)rnd);
810 }
811 return t;
812}
813
814/* parse a "debug dev sched" command
815 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
816 */
817static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
818{
819 int arg;
820 void *ptr;
821 int size;
822 const char *word, *end;
823 struct ist name;
824 char *msg = NULL;
825 char *endarg;
826 unsigned long long new;
827 unsigned long count = 0;
828 unsigned long thrid = 0;
829 unsigned int inter = 0;
830 unsigned long mask, tmask;
831 unsigned long i;
832 int mode = 0; // 0 = tasklet; 1 = task
833 int single = 0;
834 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
835
836 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
837 return 1;
838
839 ptr = NULL; size = 0;
840 mask = all_threads_mask;
841
842 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
843 return cli_err(appctx,
844 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
845 " <obj> = {count | mask | inter | single }\n"
846 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
847 );
848 }
849
850 mode = strcmp(args[3], "task") == 0;
851
Willy Tarreau4781b152021-04-06 13:53:36 +0200852 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100853 for (arg = 4; *args[arg]; arg++) {
854 end = word = args[arg];
855 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
856 end++;
857 name = ist2(word, end - word);
858 if (isteq(name, ist("count"))) {
859 ptr = &count; size = sizeof(count);
860 } else if (isteq(name, ist("mask"))) {
861 ptr = &mask; size = sizeof(mask);
862 } else if (isteq(name, ist("tid"))) {
863 ptr = &thrid; size = sizeof(thrid);
864 } else if (isteq(name, ist("inter"))) {
865 ptr = &inter; size = sizeof(inter);
866 } else if (isteq(name, ist("single"))) {
867 ptr = &single; size = sizeof(single);
868 } else
869 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
870
871 /* parse the new value . */
872 new = strtoll(end + 1, &endarg, 0);
873 if (end[1] && *endarg) {
874 memprintf(&msg,
875 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
876 msg ? msg : "", end + 1, (int)(end - word), word);
877 continue;
878 }
879
880 /* write the new value */
881 if (size == 8)
882 write_u64(ptr, new);
883 else if (size == 4)
884 write_u32(ptr, new);
885 else if (size == 2)
886 write_u16(ptr, new);
887 else
888 *(uint8_t *)ptr = new;
889 }
890
891 tctx = calloc(sizeof(*tctx), count + 2);
892 if (!tctx)
893 goto fail;
894
895 tctx[0] = (unsigned long)count;
896 tctx[1] = (unsigned long)inter;
897
898 mask &= all_threads_mask;
899 if (!mask)
900 mask = tid_bit;
901
902 tmask = 0;
903 for (i = 0; i < count; i++) {
904 if (single || mode == 0) {
905 /* look for next bit matching a bit in mask or loop back to zero */
906 for (tmask <<= 1; !(mask & tmask); ) {
907 if (!(mask & -tmask))
908 tmask = 1;
909 else
910 tmask <<= 1;
911 }
912 } else {
913 /* multi-threaded task */
914 tmask = mask;
915 }
916
917 /* now, if poly or mask was set, tmask corresponds to the
918 * valid thread mask to use, otherwise it remains zero.
919 */
920 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
921 if (mode == 0) {
922 struct tasklet *tl = tasklet_new();
923
924 if (!tl)
925 goto fail;
926
927 if (tmask)
928 tl->tid = my_ffsl(tmask) - 1;
929 tl->process = debug_tasklet_handler;
930 tl->context = tctx;
931 tctx[i + 2] = (unsigned long)tl;
932 } else {
933 struct task *task = task_new(tmask ? tmask : tid_bit);
934
935 if (!task)
936 goto fail;
937
938 task->process = debug_task_handler;
939 task->context = tctx;
940 tctx[i + 2] = (unsigned long)task + 1;
941 }
942 }
943
944 /* start the tasks and tasklets */
945 for (i = 0; i < count; i++) {
946 unsigned long ctx = tctx[i + 2];
947
948 if (ctx & 1)
949 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
950 else
951 tasklet_wakeup((struct tasklet *)ctx);
952 }
953
954 if (msg && *msg)
955 return cli_dynmsg(appctx, LOG_INFO, msg);
956 return 1;
957
958 fail:
959 /* free partially allocated entries */
960 for (i = 0; tctx && i < count; i++) {
961 unsigned long ctx = tctx[i + 2];
962
963 if (!ctx)
964 break;
965
966 if (ctx & 1)
967 task_destroy((struct task *)(ctx - 1));
968 else
969 tasklet_free((struct tasklet *)ctx);
970 }
971
972 free(tctx);
973 return cli_err(appctx, "Not enough memory");
974}
975
Willy Tarreaua6026a02020-07-02 09:14:48 +0200976#if defined(DEBUG_MEM_STATS)
977/* CLI parser for the "debug dev memstats" command */
978static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
979{
980 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
981 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
982
983 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
984 return 1;
985
986 if (strcmp(args[3], "reset") == 0) {
987 struct mem_stats *ptr;
988
989 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
990 return 1;
991
992 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
993 _HA_ATOMIC_STORE(&ptr->calls, 0);
994 _HA_ATOMIC_STORE(&ptr->size, 0);
995 }
996 return 1;
997 }
998
999 if (strcmp(args[3], "all") == 0)
1000 appctx->ctx.cli.i0 = 1;
1001
1002 /* otherwise proceed with the dump from p0 to p1 */
1003 appctx->ctx.cli.p0 = &__start_mem_stats;
1004 appctx->ctx.cli.p1 = &__stop_mem_stats;
1005 return 0;
1006}
1007
1008/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
1009 * structs referenced by pointers located between p0 and p1. Dumps all entries
1010 * if i0 > 0, otherwise only non-zero calls.
1011 */
1012static int debug_iohandler_memstats(struct appctx *appctx)
1013{
1014 struct stream_interface *si = appctx->owner;
1015 struct mem_stats *ptr = appctx->ctx.cli.p0;
1016 int ret = 1;
1017
1018 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1019 goto end;
1020
1021 chunk_reset(&trash);
1022
1023 /* we have two inner loops here, one for the proxy, the other one for
1024 * the buffer.
1025 */
1026 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1027 const char *type;
1028 const char *name;
1029 const char *p;
1030
1031 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1032 continue;
1033
1034 /* basename only */
1035 for (p = name = ptr->file; *p; p++) {
1036 if (*p == '/')
1037 name = p + 1;
1038 }
1039
1040 switch (ptr->type) {
1041 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1042 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1043 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1044 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1045 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1046 default: type = "UNSET"; break;
1047 }
1048
1049 //chunk_printf(&trash,
1050 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1051 // name, ptr->line, type,
1052 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1053 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1054
1055 chunk_printf(&trash, "%s:%d", name, ptr->line);
1056 while (trash.data < 25)
1057 trash.area[trash.data++] = ' ';
1058 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1059 type,
1060 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1061 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1062
1063 if (ci_putchk(si_ic(si), &trash) == -1) {
1064 si_rx_room_blk(si);
1065 appctx->ctx.cli.p0 = ptr;
1066 ret = 0;
1067 break;
1068 }
1069 }
1070
1071 end:
1072 return ret;
1073}
1074
1075#endif
1076
Willy Tarreauc7091d82019-05-17 10:08:49 +02001077#ifndef USE_THREAD_DUMP
1078
1079/* This function dumps all threads' state to the trash. This version is the
1080 * most basic one, which doesn't inspect other threads.
1081 */
1082void ha_thread_dump_all_to_trash()
1083{
1084 unsigned int thr;
1085
1086 for (thr = 0; thr < global.nbthread; thr++)
1087 ha_thread_dump(&trash, thr, tid);
1088}
1089
1090#else /* below USE_THREAD_DUMP is set */
1091
Willy Tarreauc7091d82019-05-17 10:08:49 +02001092/* ID of the thread requesting the dump */
1093static unsigned int thread_dump_tid;
1094
1095/* points to the buffer where the dump functions should write. It must
1096 * have already been initialized by the requester. Nothing is done if
1097 * it's NULL.
1098 */
1099struct buffer *thread_dump_buffer = NULL;
1100
1101void ha_thread_dump_all_to_trash()
1102{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001103 unsigned long old;
1104
1105 while (1) {
1106 old = 0;
1107 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1108 break;
1109 ha_thread_relax();
1110 }
1111
1112 thread_dump_buffer = &trash;
1113 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001114 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001115}
1116
1117/* handles DEBUGSIG to dump the state of the thread it's working on */
1118void debug_handler(int sig, siginfo_t *si, void *arg)
1119{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001120 /* first, let's check it's really for us and that we didn't just get
1121 * a spurious DEBUGSIG.
1122 */
1123 if (!(threads_to_dump & tid_bit))
1124 return;
1125
Willy Tarreauc7091d82019-05-17 10:08:49 +02001126 /* There are 4 phases in the dump process:
1127 * 1- wait for our turn, i.e. when all lower bits are gone.
1128 * 2- perform the action if our bit is set
1129 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001130 * the last one and have to put them all as a signal
1131 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001132 */
1133
1134 /* wait for all previous threads to finish first */
1135 while (threads_to_dump & (tid_bit - 1))
1136 ha_thread_relax();
1137
1138 /* dump if needed */
1139 if (threads_to_dump & tid_bit) {
1140 if (thread_dump_buffer)
1141 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1142 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1143 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001144 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001145 thread_dump_buffer = NULL;
1146 }
1147 else
1148 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1149 }
1150
1151 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001152 * bits again to broadcast the leaving condition so we'll see ourselves
1153 * present again. This way the threads_to_dump variable never passes to
1154 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001155 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001156 while (!(threads_to_dump & tid_bit))
1157 ha_thread_relax();
1158 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001159
1160 /* mark the current thread as stuck to detect it upon next invocation
1161 * if it didn't move.
1162 */
1163 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
Willy Tarreaua0b99532021-09-30 18:48:37 +02001164 th_ctx->flags |= TH_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001165}
1166
1167static int init_debug_per_thread()
1168{
1169 sigset_t set;
1170
1171 /* unblock the DEBUGSIG signal we intend to use */
1172 sigemptyset(&set);
1173 sigaddset(&set, DEBUGSIG);
1174 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1175 return 1;
1176}
1177
1178static int init_debug()
1179{
1180 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001181 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001182
Willy Tarreau0214b452020-03-04 06:01:40 +01001183 /* calling backtrace() will access libgcc at runtime. We don't want to
1184 * do it after the chroot, so let's perform a first call to have it
1185 * ready in memory for later use.
1186 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001187 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001188 sa.sa_handler = NULL;
1189 sa.sa_sigaction = debug_handler;
1190 sigemptyset(&sa.sa_mask);
1191 sa.sa_flags = SA_SIGINFO;
1192 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001193 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001194}
1195
1196REGISTER_POST_CHECK(init_debug);
1197REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1198
1199#endif /* USE_THREAD_DUMP */
1200
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001201/* register cli keywords */
1202static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001203 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1204 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1205 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6bdf3e92019-05-20 14:25:05 +02001206#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001207 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6bdf3e92019-05-20 14:25:05 +02001208#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001209 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1210 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1211 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1212 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001213#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001214 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001215#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001216 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1217 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1218 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1219 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1220 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
1221 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6ab7b212021-12-28 09:57:10 +01001222#if defined(HA_HAVE_DUMP_LIBS)
1223 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1224#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001225 {{ "show", "threads", NULL, NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001226 {{},}
1227}};
1228
1229INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);