blob: aafa378394c639056d3fe03bdb46b6ca24ae78ab [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);
David Carliera92c5ce2019-09-13 05:03:12 +0100155 int stuck = !!(ha_thread_info[thr].flags & TI_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"
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200159 " 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 Tarreaue6a02fa2019-05-22 07:06:44 +0200172 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200173 !!(task_profiling_mask & thr_bit));
174
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200175 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176 " harmless=%d wantrdv=%d",
177 !!(threads_harmless_mask & thr_bit),
178 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200179
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200180 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200181 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200182
183 /* this is the end of what we can dump from outside the thread */
184
185 if (thr != tid)
186 return;
187
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200188 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200189 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100190
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100191 if (stuck) {
192 /* We only emit the backtrace for stuck threads in order not to
193 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100194 * Please leave this as the last instruction in this function
195 * so that the compiler uses tail merging and the current
196 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100197 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100198 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100199 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200200}
201
202
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200203/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200204 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200205 * with <pfx>. The buffer is only appended and the first output starts by the
206 * pointer itself. The caller is responsible for making sure the task is not
207 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200208 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200209void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200210{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200211 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200212 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200213 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200214
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200215 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200216 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200217 return;
218 }
219
Willy Tarreau20db9112019-05-17 14:14:35 +0200220 if (TASK_IS_TASKLET(task))
221 chunk_appendf(buf,
222 "%p (tasklet) calls=%u\n",
223 task,
224 task->calls);
225 else
226 chunk_appendf(buf,
227 "%p (task) calls=%u last=%llu%s\n",
228 task,
229 task->calls,
230 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
231 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200232
Willy Tarreau2e89b092020-03-03 17:13:02 +0100233 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
234 resolve_sym_name(buf, NULL, task->process);
235 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200236
Willy Tarreaua512b022019-08-21 14:12:19 +0200237 if (task->process == task_run_applet && (appctx = task->context))
238 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
239 else
240 chunk_appendf(buf, "\n");
241
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200242 if (task->process == process_stream && task->context)
243 s = (struct stream *)task->context;
244 else if (task->process == task_run_applet && task->context)
245 s = si_strm(((struct appctx *)task->context)->owner);
246 else if (task->process == si_cs_io_cb && task->context)
247 s = si_strm((struct stream_interface *)task->context);
248
249 if (s)
250 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200251
252#ifdef USE_LUA
253 hlua = NULL;
254 if (s && (hlua = s->hlua)) {
255 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
256 }
257 else if (task->process == hlua_process_task && (hlua = task->context)) {
258 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
259 }
260 else if (task->process == task_run_applet && (appctx = task->context) &&
261 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
262 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
263 }
264 else if (task->process == task_run_applet && (appctx = task->context) &&
265 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
266 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
267 }
268
Christopher Faulet471425f2020-07-24 19:08:05 +0200269 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100270 chunk_appendf(buf, "stack traceback:\n ");
271 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
272 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200273 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200274 else
275 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200276#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200277}
278
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200279/* This function dumps all profiling settings. It returns 0 if the output
280 * buffer is full and it needs to be called again, otherwise non-zero.
281 */
282static int cli_io_handler_show_threads(struct appctx *appctx)
283{
284 struct stream_interface *si = appctx->owner;
285 int thr;
286
287 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
288 return 1;
289
290 if (appctx->st0)
291 thr = appctx->st1;
292 else
293 thr = 0;
294
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200295 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200296 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200297
298 if (ci_putchk(si_ic(si), &trash) == -1) {
299 /* failed, try again */
300 si_rx_room_blk(si);
301 appctx->st1 = thr;
302 return 0;
303 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200304 return 1;
305}
306
Willy Tarreau56131ca2019-05-20 13:48:29 +0200307/* dumps a state of all threads into the trash and on fd #2, then aborts. */
308void ha_panic()
309{
310 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200311 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200312 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100313 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200314 for (;;)
315 abort();
316}
317
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200318/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
319static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
320{
321 int code = atoi(args[3]);
322
323 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
324 return 1;
325
Willy Tarreau4781b152021-04-06 13:53:36 +0200326 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200327 exit(code);
328 return 1;
329}
330
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100331/* parse a "debug dev bug" command. It always returns 1, though it should never return.
332 * Note: we make sure not to make the function static so that it appears in the trace.
333 */
334int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
335{
336 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
337 return 1;
338
Willy Tarreau4781b152021-04-06 13:53:36 +0200339 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100340 BUG_ON(one > zero);
341 return 1;
342}
343
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200344/* parse a "debug dev close" command. It always returns 1. */
345static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
346{
347 int fd;
348
349 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
350 return 1;
351
Willy Tarreau9d008692019-08-09 11:21:01 +0200352 if (!*args[3])
353 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200354
355 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200356 if (fd < 0 || fd >= global.maxsock)
357 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200358
Willy Tarreau9d008692019-08-09 11:21:01 +0200359 if (!fdtab[fd].owner)
360 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200361
Willy Tarreau4781b152021-04-06 13:53:36 +0200362 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363 fd_delete(fd);
364 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200365}
366
367/* parse a "debug dev delay" command. It always returns 1. */
368static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
369{
370 int delay = atoi(args[3]);
371
372 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
373 return 1;
374
Willy Tarreau4781b152021-04-06 13:53:36 +0200375 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200376 usleep((long)delay * 1000);
377 return 1;
378}
379
380/* parse a "debug dev log" command. It always returns 1. */
381static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
382{
383 int arg;
384
385 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
386 return 1;
387
Willy Tarreau4781b152021-04-06 13:53:36 +0200388 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200389 chunk_reset(&trash);
390 for (arg = 3; *args[arg]; arg++) {
391 if (arg > 3)
392 chunk_strcat(&trash, " ");
393 chunk_strcat(&trash, args[arg]);
394 }
395
396 send_log(NULL, LOG_INFO, "%s\n", trash.area);
397 return 1;
398}
399
400/* parse a "debug dev loop" command. It always returns 1. */
401static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
402{
403 struct timeval deadline, curr;
404 int loop = atoi(args[3]);
405
406 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
407 return 1;
408
Willy Tarreau4781b152021-04-06 13:53:36 +0200409 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200410 gettimeofday(&curr, NULL);
411 tv_ms_add(&deadline, &curr, loop);
412
413 while (tv_ms_cmp(&curr, &deadline) < 0)
414 gettimeofday(&curr, NULL);
415
416 return 1;
417}
418
419/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
420static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
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 ha_panic();
427 return 1;
428}
429
430/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200431#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200432static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
433{
Willy Tarreau368bff42019-12-06 17:18:28 +0100434 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200435 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100436 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200437
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 chunk_reset(&trash);
443 for (arg = 3; *args[arg]; arg++) {
444 if (arg > 3)
445 chunk_strcat(&trash, " ");
446 chunk_strcat(&trash, args[arg]);
447 }
448
Willy Tarreau368bff42019-12-06 17:18:28 +0100449 thread_isolate();
450 if (pipe(pipefd) < 0)
451 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200452
Willy Tarreau368bff42019-12-06 17:18:28 +0100453 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
454 goto fail_fcntl;
455
456 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
457 goto fail_fcntl;
458
459 pid = fork();
460
461 if (pid < 0)
462 goto fail_fork;
463 else if (pid == 0) {
464 /* child */
465 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
466
467 close(0);
468 dup2(pipefd[1], 1);
469 dup2(pipefd[1], 2);
470
471 cmd[2] = trash.area;
472 execvp(cmd[0], cmd);
473 printf("execvp() failed\n");
474 exit(1);
475 }
476
477 /* parent */
478 thread_release();
479 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200480 chunk_reset(&trash);
481 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100482 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
483 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200484 break;
485 trash.data += ret;
486 if (trash.data + 20 == trash.size) {
487 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
488 break;
489 }
490 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100491 close(pipefd[0]);
492 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200493 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200494 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100495
496 fail_fork:
497 fail_fcntl:
498 close(pipefd[0]);
499 close(pipefd[1]);
500 fail_pipe:
501 thread_release();
502 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200503}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200504#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200505
506/* parse a "debug dev hex" command. It always returns 1. */
507static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
508{
509 unsigned long start, len;
510
511 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
512 return 1;
513
Willy Tarreau9d008692019-08-09 11:21:01 +0200514 if (!*args[3])
515 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200516
517 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200518 if (!start)
519 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200520
Willy Tarreau4781b152021-04-06 13:53:36 +0200521 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200522
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200523 /* by default, dump ~128 till next block of 16 */
524 len = strtoul(args[4], NULL, 0);
525 if (!len)
526 len = ((start + 128) & -16) - start;
527
528 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200529 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200530 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200531 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200532}
533
Willy Tarreau48129be2021-05-04 18:40:50 +0200534/* parse a "debug dev sym <addr>" command. It always returns 1. */
535static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
536{
537 unsigned long addr;
538
539 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
540 return 1;
541
542 if (!*args[3])
543 return cli_err(appctx, "Missing memory address to be resolved.\n");
544
545 _HA_ATOMIC_INC(&debug_commands_issued);
546
547 addr = strtoul(args[3], NULL, 0);
548 chunk_printf(&trash, "%#lx resolves to ", addr);
549 resolve_sym_name(&trash, NULL, (const void *)addr);
550 chunk_appendf(&trash, "\n");
551
552 return cli_msg(appctx, LOG_INFO, trash.area);
553}
554
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200555/* parse a "debug dev tkill" command. It always returns 1. */
556static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
557{
558 int thr = 0;
559 int sig = SIGABRT;
560
561 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
562 return 1;
563
564 if (*args[3])
565 thr = atoi(args[3]);
566
Willy Tarreau9d008692019-08-09 11:21:01 +0200567 if (thr < 0 || thr > global.nbthread)
568 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200569
570 if (*args[4])
571 sig = atoi(args[4]);
572
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200574 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200575 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200576 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200577 raise(sig);
578 return 1;
579}
580
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100581/* parse a "debug dev write" command. It always returns 1. */
582static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
583{
584 unsigned long len;
585
586 if (!*args[3])
587 return cli_err(appctx, "Missing output size.\n");
588
589 len = strtoul(args[3], NULL, 0);
590 if (len >= trash.size)
591 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
592
Willy Tarreau4781b152021-04-06 13:53:36 +0200593 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100594
595 chunk_reset(&trash);
596 trash.data = len;
597 memset(trash.area, '.', trash.data);
598 trash.area[trash.data] = 0;
599 for (len = 64; len < trash.data; len += 64)
600 trash.area[len] = '\n';
601 return cli_msg(appctx, LOG_INFO, trash.area);
602}
603
Willy Tarreau68680bb2019-10-23 17:23:25 +0200604/* parse a "debug dev stream" command */
605/*
606 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
607 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
608 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
609 * [sif.s[=<state>]] [sib.s[=<state>]]
610 */
611static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
612{
613 struct stream *s = si_strm(appctx->owner);
614 int arg;
615 void *ptr;
616 int size;
617 const char *word, *end;
618 struct ist name;
619 char *msg = NULL;
620 char *endarg;
621 unsigned long long old, new;
622
623 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
624 return 1;
625
626 ptr = NULL; size = 0;
627
628 if (!*args[3]) {
629 return cli_err(appctx,
630 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
631 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
632 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
633 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
634 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
635 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
636 );
637 }
638
Willy Tarreau4781b152021-04-06 13:53:36 +0200639 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200640 for (arg = 3; *args[arg]; arg++) {
641 old = 0;
642 end = word = args[arg];
643 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
644 end++;
645 name = ist2(word, end - word);
646 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200647 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200648 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200649 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200650 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200651 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200652 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200653 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200654 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200655 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200656 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200657 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200658 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200659 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200660 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200661 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200662 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200663 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200664 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200665 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200666 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200667 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200668 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200669 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200670 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200671 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200672 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200673 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200674 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200675 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200676 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200677 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200678 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
679 continue;
680 } else
681 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
682
683 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200684 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200685 if (size == 8)
686 old = read_u64(ptr);
687 else if (size == 4)
688 old = read_u32(ptr);
689 else if (size == 2)
690 old = read_u16(ptr);
691 else
692 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200693 } else {
694 memprintf(&msg,
695 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
696 msg ? msg : "", ptr, (int)(end - word), word);
697 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200698 }
699
700 /* parse the new value . */
701 new = strtoll(end + 1, &endarg, 0);
702 if (end[1] && *endarg) {
703 if (strcmp(end + 1, "now") == 0)
704 new = now_ms;
705 else {
706 memprintf(&msg,
707 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
708 msg ? msg : "", end + 1, (int)(end - word), word);
709 continue;
710 }
711 }
712
713 switch (*end) {
714 case '\0': /* show */
715 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
716 new = old; // do not change the value
717 break;
718
719 case '=': /* set */
720 break;
721
722 case '^': /* XOR */
723 new = old ^ new;
724 break;
725
726 case '+': /* OR */
727 new = old | new;
728 break;
729
730 case '-': /* AND NOT */
731 new = old & ~new;
732 break;
733
734 default:
735 break;
736 }
737
738 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200739 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200740 if (size == 8)
741 write_u64(ptr, new);
742 else if (size == 4)
743 write_u32(ptr, new);
744 else if (size == 2)
745 write_u16(ptr, new);
746 else
747 *(uint8_t *)ptr = new;
748 }
749 }
750
751 if (msg && *msg)
752 return cli_dynmsg(appctx, LOG_INFO, msg);
753 return 1;
754}
755
Willy Tarreau144f84a2021-03-02 16:09:26 +0100756static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100757{
758 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
759 unsigned long inter = tctx[1];
760 unsigned long rnd;
761
762 t->expire = tick_add(now_ms, inter);
763
764 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100765 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100766 if (rnd & 1) {
767 rnd >>= 1;
768 rnd %= tctx[0];
769 rnd = tctx[rnd + 2];
770
771 if (rnd & 1)
772 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
773 else
774 tasklet_wakeup((struct tasklet *)rnd);
775 }
776 return t;
777}
778
Willy Tarreau144f84a2021-03-02 16:09:26 +0100779static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100780{
781 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
782 unsigned long rnd;
783 int i;
784
785 /* wake up two random entries */
786 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100787 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100788 rnd = tctx[rnd + 2];
789
790 if (rnd & 1)
791 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
792 else
793 tasklet_wakeup((struct tasklet *)rnd);
794 }
795 return t;
796}
797
798/* parse a "debug dev sched" command
799 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
800 */
801static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
802{
803 int arg;
804 void *ptr;
805 int size;
806 const char *word, *end;
807 struct ist name;
808 char *msg = NULL;
809 char *endarg;
810 unsigned long long new;
811 unsigned long count = 0;
812 unsigned long thrid = 0;
813 unsigned int inter = 0;
814 unsigned long mask, tmask;
815 unsigned long i;
816 int mode = 0; // 0 = tasklet; 1 = task
817 int single = 0;
818 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
819
820 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
821 return 1;
822
823 ptr = NULL; size = 0;
824 mask = all_threads_mask;
825
826 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
827 return cli_err(appctx,
828 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
829 " <obj> = {count | mask | inter | single }\n"
830 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
831 );
832 }
833
834 mode = strcmp(args[3], "task") == 0;
835
Willy Tarreau4781b152021-04-06 13:53:36 +0200836 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100837 for (arg = 4; *args[arg]; arg++) {
838 end = word = args[arg];
839 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
840 end++;
841 name = ist2(word, end - word);
842 if (isteq(name, ist("count"))) {
843 ptr = &count; size = sizeof(count);
844 } else if (isteq(name, ist("mask"))) {
845 ptr = &mask; size = sizeof(mask);
846 } else if (isteq(name, ist("tid"))) {
847 ptr = &thrid; size = sizeof(thrid);
848 } else if (isteq(name, ist("inter"))) {
849 ptr = &inter; size = sizeof(inter);
850 } else if (isteq(name, ist("single"))) {
851 ptr = &single; size = sizeof(single);
852 } else
853 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
854
855 /* parse the new value . */
856 new = strtoll(end + 1, &endarg, 0);
857 if (end[1] && *endarg) {
858 memprintf(&msg,
859 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
860 msg ? msg : "", end + 1, (int)(end - word), word);
861 continue;
862 }
863
864 /* write the new value */
865 if (size == 8)
866 write_u64(ptr, new);
867 else if (size == 4)
868 write_u32(ptr, new);
869 else if (size == 2)
870 write_u16(ptr, new);
871 else
872 *(uint8_t *)ptr = new;
873 }
874
875 tctx = calloc(sizeof(*tctx), count + 2);
876 if (!tctx)
877 goto fail;
878
879 tctx[0] = (unsigned long)count;
880 tctx[1] = (unsigned long)inter;
881
882 mask &= all_threads_mask;
883 if (!mask)
884 mask = tid_bit;
885
886 tmask = 0;
887 for (i = 0; i < count; i++) {
888 if (single || mode == 0) {
889 /* look for next bit matching a bit in mask or loop back to zero */
890 for (tmask <<= 1; !(mask & tmask); ) {
891 if (!(mask & -tmask))
892 tmask = 1;
893 else
894 tmask <<= 1;
895 }
896 } else {
897 /* multi-threaded task */
898 tmask = mask;
899 }
900
901 /* now, if poly or mask was set, tmask corresponds to the
902 * valid thread mask to use, otherwise it remains zero.
903 */
904 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
905 if (mode == 0) {
906 struct tasklet *tl = tasklet_new();
907
908 if (!tl)
909 goto fail;
910
911 if (tmask)
912 tl->tid = my_ffsl(tmask) - 1;
913 tl->process = debug_tasklet_handler;
914 tl->context = tctx;
915 tctx[i + 2] = (unsigned long)tl;
916 } else {
917 struct task *task = task_new(tmask ? tmask : tid_bit);
918
919 if (!task)
920 goto fail;
921
922 task->process = debug_task_handler;
923 task->context = tctx;
924 tctx[i + 2] = (unsigned long)task + 1;
925 }
926 }
927
928 /* start the tasks and tasklets */
929 for (i = 0; i < count; i++) {
930 unsigned long ctx = tctx[i + 2];
931
932 if (ctx & 1)
933 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
934 else
935 tasklet_wakeup((struct tasklet *)ctx);
936 }
937
938 if (msg && *msg)
939 return cli_dynmsg(appctx, LOG_INFO, msg);
940 return 1;
941
942 fail:
943 /* free partially allocated entries */
944 for (i = 0; tctx && i < count; i++) {
945 unsigned long ctx = tctx[i + 2];
946
947 if (!ctx)
948 break;
949
950 if (ctx & 1)
951 task_destroy((struct task *)(ctx - 1));
952 else
953 tasklet_free((struct tasklet *)ctx);
954 }
955
956 free(tctx);
957 return cli_err(appctx, "Not enough memory");
958}
959
Willy Tarreaua6026a02020-07-02 09:14:48 +0200960#if defined(DEBUG_MEM_STATS)
961/* CLI parser for the "debug dev memstats" command */
962static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
963{
964 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
965 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
966
967 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
968 return 1;
969
970 if (strcmp(args[3], "reset") == 0) {
971 struct mem_stats *ptr;
972
973 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
974 return 1;
975
976 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
977 _HA_ATOMIC_STORE(&ptr->calls, 0);
978 _HA_ATOMIC_STORE(&ptr->size, 0);
979 }
980 return 1;
981 }
982
983 if (strcmp(args[3], "all") == 0)
984 appctx->ctx.cli.i0 = 1;
985
986 /* otherwise proceed with the dump from p0 to p1 */
987 appctx->ctx.cli.p0 = &__start_mem_stats;
988 appctx->ctx.cli.p1 = &__stop_mem_stats;
989 return 0;
990}
991
992/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
993 * structs referenced by pointers located between p0 and p1. Dumps all entries
994 * if i0 > 0, otherwise only non-zero calls.
995 */
996static int debug_iohandler_memstats(struct appctx *appctx)
997{
998 struct stream_interface *si = appctx->owner;
999 struct mem_stats *ptr = appctx->ctx.cli.p0;
1000 int ret = 1;
1001
1002 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1003 goto end;
1004
1005 chunk_reset(&trash);
1006
1007 /* we have two inner loops here, one for the proxy, the other one for
1008 * the buffer.
1009 */
1010 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1011 const char *type;
1012 const char *name;
1013 const char *p;
1014
1015 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1016 continue;
1017
1018 /* basename only */
1019 for (p = name = ptr->file; *p; p++) {
1020 if (*p == '/')
1021 name = p + 1;
1022 }
1023
1024 switch (ptr->type) {
1025 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1026 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1027 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1028 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1029 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1030 default: type = "UNSET"; break;
1031 }
1032
1033 //chunk_printf(&trash,
1034 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1035 // name, ptr->line, type,
1036 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1037 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1038
1039 chunk_printf(&trash, "%s:%d", name, ptr->line);
1040 while (trash.data < 25)
1041 trash.area[trash.data++] = ' ';
1042 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1043 type,
1044 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1045 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1046
1047 if (ci_putchk(si_ic(si), &trash) == -1) {
1048 si_rx_room_blk(si);
1049 appctx->ctx.cli.p0 = ptr;
1050 ret = 0;
1051 break;
1052 }
1053 }
1054
1055 end:
1056 return ret;
1057}
1058
1059#endif
1060
Willy Tarreauc7091d82019-05-17 10:08:49 +02001061#ifndef USE_THREAD_DUMP
1062
1063/* This function dumps all threads' state to the trash. This version is the
1064 * most basic one, which doesn't inspect other threads.
1065 */
1066void ha_thread_dump_all_to_trash()
1067{
1068 unsigned int thr;
1069
1070 for (thr = 0; thr < global.nbthread; thr++)
1071 ha_thread_dump(&trash, thr, tid);
1072}
1073
1074#else /* below USE_THREAD_DUMP is set */
1075
Willy Tarreauc7091d82019-05-17 10:08:49 +02001076/* ID of the thread requesting the dump */
1077static unsigned int thread_dump_tid;
1078
1079/* points to the buffer where the dump functions should write. It must
1080 * have already been initialized by the requester. Nothing is done if
1081 * it's NULL.
1082 */
1083struct buffer *thread_dump_buffer = NULL;
1084
1085void ha_thread_dump_all_to_trash()
1086{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001087 unsigned long old;
1088
1089 while (1) {
1090 old = 0;
1091 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1092 break;
1093 ha_thread_relax();
1094 }
1095
1096 thread_dump_buffer = &trash;
1097 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001098 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001099}
1100
1101/* handles DEBUGSIG to dump the state of the thread it's working on */
1102void debug_handler(int sig, siginfo_t *si, void *arg)
1103{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001104 /* first, let's check it's really for us and that we didn't just get
1105 * a spurious DEBUGSIG.
1106 */
1107 if (!(threads_to_dump & tid_bit))
1108 return;
1109
Willy Tarreauc7091d82019-05-17 10:08:49 +02001110 /* There are 4 phases in the dump process:
1111 * 1- wait for our turn, i.e. when all lower bits are gone.
1112 * 2- perform the action if our bit is set
1113 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001114 * the last one and have to put them all as a signal
1115 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001116 */
1117
1118 /* wait for all previous threads to finish first */
1119 while (threads_to_dump & (tid_bit - 1))
1120 ha_thread_relax();
1121
1122 /* dump if needed */
1123 if (threads_to_dump & tid_bit) {
1124 if (thread_dump_buffer)
1125 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1126 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1127 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001128 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001129 thread_dump_buffer = NULL;
1130 }
1131 else
1132 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1133 }
1134
1135 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001136 * bits again to broadcast the leaving condition so we'll see ourselves
1137 * present again. This way the threads_to_dump variable never passes to
1138 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001139 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001140 while (!(threads_to_dump & tid_bit))
1141 ha_thread_relax();
1142 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001143
1144 /* mark the current thread as stuck to detect it upon next invocation
1145 * if it didn't move.
1146 */
1147 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1148 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001149}
1150
1151static int init_debug_per_thread()
1152{
1153 sigset_t set;
1154
1155 /* unblock the DEBUGSIG signal we intend to use */
1156 sigemptyset(&set);
1157 sigaddset(&set, DEBUGSIG);
1158 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1159 return 1;
1160}
1161
1162static int init_debug()
1163{
1164 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001165 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001166
Willy Tarreau0214b452020-03-04 06:01:40 +01001167 /* calling backtrace() will access libgcc at runtime. We don't want to
1168 * do it after the chroot, so let's perform a first call to have it
1169 * ready in memory for later use.
1170 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001171 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001172 sa.sa_handler = NULL;
1173 sa.sa_sigaction = debug_handler;
1174 sigemptyset(&sa.sa_mask);
1175 sa.sa_flags = SA_SIGINFO;
1176 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001177 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001178}
1179
1180REGISTER_POST_CHECK(init_debug);
1181REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1182
1183#endif /* USE_THREAD_DUMP */
1184
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001185/* register cli keywords */
1186static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001187 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1188 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1189 {{ "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 +02001190#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001191 {{ "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 +02001192#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001193 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1194 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1195 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1196 {{ "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 +02001197#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001198 {{ "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 +02001199#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001200 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1201 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1202 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1203 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1204 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
1205 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
1206 {{ "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 +02001207 {{},}
1208}};
1209
1210INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);