blob: 9546ed2077be6749dc624fe78ccf93320bb47d35 [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 Tarreau476252b2022-01-24 20:26:09 +010014#include <errno.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010015#include <fcntl.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020016#include <signal.h>
17#include <time.h>
18#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020019#include <stdlib.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020020#include <syslog.h>
Willy Tarreau476252b2022-01-24 20:26:09 +010021#include <sys/stat.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010022#include <sys/types.h>
23#include <sys/wait.h>
Willy Tarreau476252b2022-01-24 20:26:09 +010024#include <unistd.h>
25#ifdef USE_EPOLL
26#include <sys/epoll.h>
27#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +020028
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020030#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020032#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/fd.h>
34#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020035#include <haproxy/hlua.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/net_helper.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020038#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020039#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020040#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020041#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020043
Willy Tarreau4e2b6462019-05-16 17:44:30 +020044
Willy Tarreaua37cb182019-07-31 19:20:39 +020045/* mask of threads still having to dump, used to respect ordering. Only used
46 * when USE_THREAD_DUMP is set.
47 */
48volatile unsigned long threads_to_dump = 0;
Willy Tarreauc2f999a2022-07-15 12:48:58 +020049unsigned int panic_started = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020050unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020051
Willy Tarreau123fc972021-01-22 13:52:41 +010052/* dumps a backtrace of the current thread that is appended to buffer <buf>.
53 * Lines are prefixed with the string <prefix> which may be empty (used for
54 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010055 * the function does not appear in the call stack. The <dump> argument
56 * indicates what dump state to start from, and should usually be zero. It
57 * may be among the following values:
58 * - 0: search usual callers before step 1, or directly jump to 2
59 * - 1: skip usual callers before step 2
60 * - 2: dump until polling loop, scheduler, or main() (excluded)
61 * - 3: end
62 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010063 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010064void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010065{
66 struct buffer bak;
67 char pfx2[100];
68 void *callers[100];
69 int j, nptrs;
70 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010071
72 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
73 if (!nptrs)
74 return;
75
76 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
77 pfx2[0] = 0;
78
79 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
80 * produce similar output to the following:
81 */
82 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010083 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
84 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010085 /* we failed to spot the starting point of the
86 * dump, let's start over dumping everything we
87 * have.
88 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010089 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010090 j = 0;
91 }
92 bak = *buf;
93 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
94 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010095 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010096 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010097 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +010098 */
Willy Tarreaua8459b22021-01-22 14:12:27 +010099 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
100 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100101 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100102 *buf = bak;
103 continue;
104 }
105
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100106 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100107 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100108 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
109 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100110 *buf = bak;
111 continue;
112 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100113 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100114 }
115
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100116 if ((dump & 3) == 2) {
117 /* still dumping */
118 if (dump == 6) {
119 /* we only stop *after* main and we must send the LF */
120 if (addr == main) {
121 j = nptrs;
122 dump++;
123 }
124 }
125 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
126 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100127 *buf = bak;
128 break;
129 }
130 }
131 /* OK, line dumped */
132 chunk_appendf(buf, "\n");
133 }
134}
135
Willy Tarreaua8459b22021-01-22 14:12:27 +0100136/* dump a backtrace of current thread's stack to stderr. */
137void ha_backtrace_to_stderr()
138{
139 char area[2048];
140 struct buffer b = b_make(area, sizeof(area), 0, 0);
141
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100142 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100143 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100144 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100145}
146
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200147/* Dumps to the buffer some known information for the desired thread, and
148 * optionally extra info for the current thread. The dump will be appended to
149 * the buffer, so the caller is responsible for preliminary initializing it.
150 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200151 * in front of the calling thread's line (usually it's tid). Any stuck thread
152 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200153 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200154void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200155{
156 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +0100157 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
158 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
159 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200160
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200161 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200162 "%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 +0200163 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200164 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200165 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200166 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200167 !!(global_tasks_mask & thr_bit),
168 !eb_is_empty(&task_per_thread[thr].timers),
169 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +0100170 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
171 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
172 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
173 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100174 task_per_thread[thr].tasks_in_list,
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100175 task_per_thread[thr].rq_total,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200176 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200177 !!(task_profiling_mask & thr_bit));
178
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200179 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200180 " harmless=%d wantrdv=%d",
181 !!(threads_harmless_mask & thr_bit),
182 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200183
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200184 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200185 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200186
187 /* this is the end of what we can dump from outside the thread */
188
189 if (thr != tid)
190 return;
191
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200192 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200193 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100194
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100195 if (stuck) {
196 /* We only emit the backtrace for stuck threads in order not to
197 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100198 * Please leave this as the last instruction in this function
199 * so that the compiler uses tail merging and the current
200 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100201 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100202 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100203 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200204}
205
206
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200207/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200208 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200209 * with <pfx>. The buffer is only appended and the first output starts by the
210 * pointer itself. The caller is responsible for making sure the task is not
211 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200212 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200213void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200214{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200215 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200216 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200217 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200218
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200219 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200220 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200221 return;
222 }
223
Willy Tarreau20db9112019-05-17 14:14:35 +0200224 if (TASK_IS_TASKLET(task))
225 chunk_appendf(buf,
226 "%p (tasklet) calls=%u\n",
227 task,
228 task->calls);
229 else
230 chunk_appendf(buf,
231 "%p (task) calls=%u last=%llu%s\n",
232 task,
233 task->calls,
234 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
235 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200236
Willy Tarreau2e89b092020-03-03 17:13:02 +0100237 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
238 resolve_sym_name(buf, NULL, task->process);
239 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200240
Willy Tarreaua512b022019-08-21 14:12:19 +0200241 if (task->process == task_run_applet && (appctx = task->context))
242 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
243 else
244 chunk_appendf(buf, "\n");
245
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200246 if (task->process == process_stream && task->context)
247 s = (struct stream *)task->context;
248 else if (task->process == task_run_applet && task->context)
249 s = si_strm(((struct appctx *)task->context)->owner);
250 else if (task->process == si_cs_io_cb && task->context)
251 s = si_strm((struct stream_interface *)task->context);
252
253 if (s)
254 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200255
256#ifdef USE_LUA
257 hlua = NULL;
Aurelien DARRAGONbe7b7ab2024-03-12 17:05:54 +0100258 if (s && ((s->hlua[0] && HLUA_IS_BUSY(s->hlua[0])) ||
259 (s->hlua[1] && HLUA_IS_BUSY(s->hlua[1])))) {
260 hlua = (s->hlua[0] && HLUA_IS_BUSY(s->hlua[0])) ? s->hlua[0] : s->hlua[1];
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200261 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
262 }
263 else if (task->process == hlua_process_task && (hlua = task->context)) {
264 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
265 }
266 else if (task->process == task_run_applet && (appctx = task->context) &&
267 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
268 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
269 }
270 else if (task->process == task_run_applet && (appctx = task->context) &&
271 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
272 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
273 }
274
Christopher Faulet471425f2020-07-24 19:08:05 +0200275 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100276 chunk_appendf(buf, "stack traceback:\n ");
277 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200278 }
Willy Tarreaub50ae912023-05-04 16:28:30 +0200279
280 /* we may need to terminate the current line */
281 if (*b_peek(buf, b_data(buf)-1) != '\n')
Christopher Faulet471425f2020-07-24 19:08:05 +0200282 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200283#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200284}
285
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200286/* This function dumps all profiling settings. It returns 0 if the output
287 * buffer is full and it needs to be called again, otherwise non-zero.
288 */
289static int cli_io_handler_show_threads(struct appctx *appctx)
290{
291 struct stream_interface *si = appctx->owner;
292 int thr;
293
294 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
295 return 1;
296
297 if (appctx->st0)
298 thr = appctx->st1;
299 else
300 thr = 0;
301
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200302 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200303 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200304
305 if (ci_putchk(si_ic(si), &trash) == -1) {
306 /* failed, try again */
307 si_rx_room_blk(si);
308 appctx->st1 = thr;
309 return 0;
310 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200311 return 1;
312}
313
Willy Tarreau37857332021-12-28 09:57:10 +0100314#if defined(HA_HAVE_DUMP_LIBS)
315/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
316static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
317{
318 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
319 return 1;
320
321 chunk_reset(&trash);
322 if (dump_libs(&trash, 1))
323 return cli_msg(appctx, LOG_INFO, trash.area);
324 else
325 return 0;
326}
327#endif
328
Willy Tarreau56131ca2019-05-20 13:48:29 +0200329/* dumps a state of all threads into the trash and on fd #2, then aborts. */
330void ha_panic()
331{
Willy Tarreauc2f999a2022-07-15 12:48:58 +0200332 if (HA_ATOMIC_FETCH_ADD(&panic_started, 1) != 0) {
333 /* a panic dump is already in progress, let's not disturb it,
334 * we'll be called via signal DEBUGSIG. By returning we may be
335 * able to leave a current signal handler (e.g. WDT) so that
336 * this will ensure more reliable signal delivery.
337 */
338 return;
339 }
Willy Tarreau56131ca2019-05-20 13:48:29 +0200340 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200341 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200342 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100343 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200344 for (;;)
345 abort();
346}
347
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200348/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
349static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
350{
351 int code = atoi(args[3]);
352
353 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
354 return 1;
355
Willy Tarreau4781b152021-04-06 13:53:36 +0200356 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200357 exit(code);
358 return 1;
359}
360
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100361/* parse a "debug dev bug" command. It always returns 1, though it should never return.
362 * Note: we make sure not to make the function static so that it appears in the trace.
363 */
364int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
365{
366 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
367 return 1;
368
Willy Tarreau4781b152021-04-06 13:53:36 +0200369 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100370 BUG_ON(one > zero);
371 return 1;
372}
373
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200374/* parse a "debug dev close" command. It always returns 1. */
375static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
376{
377 int fd;
378
379 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
380 return 1;
381
Willy Tarreau9d008692019-08-09 11:21:01 +0200382 if (!*args[3])
383 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200384
385 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200386 if (fd < 0 || fd >= global.maxsock)
387 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200388
Willy Tarreau9d008692019-08-09 11:21:01 +0200389 if (!fdtab[fd].owner)
390 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391
Willy Tarreau4781b152021-04-06 13:53:36 +0200392 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200393 fd_delete(fd);
394 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200395}
396
397/* parse a "debug dev delay" command. It always returns 1. */
398static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
399{
400 int delay = atoi(args[3]);
401
402 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
403 return 1;
404
Willy Tarreau4781b152021-04-06 13:53:36 +0200405 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200406 usleep((long)delay * 1000);
407 return 1;
408}
409
410/* parse a "debug dev log" command. It always returns 1. */
411static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
412{
413 int arg;
414
415 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
416 return 1;
417
Willy Tarreau4781b152021-04-06 13:53:36 +0200418 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200419 chunk_reset(&trash);
420 for (arg = 3; *args[arg]; arg++) {
421 if (arg > 3)
422 chunk_strcat(&trash, " ");
423 chunk_strcat(&trash, args[arg]);
424 }
425
426 send_log(NULL, LOG_INFO, "%s\n", trash.area);
427 return 1;
428}
429
430/* parse a "debug dev loop" command. It always returns 1. */
431static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
432{
433 struct timeval deadline, curr;
434 int loop = atoi(args[3]);
435
436 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
437 return 1;
438
Willy Tarreau4781b152021-04-06 13:53:36 +0200439 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200440 gettimeofday(&curr, NULL);
441 tv_ms_add(&deadline, &curr, loop);
442
443 while (tv_ms_cmp(&curr, &deadline) < 0)
444 gettimeofday(&curr, NULL);
445
446 return 1;
447}
448
449/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
450static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
451{
452 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
453 return 1;
454
Willy Tarreau4781b152021-04-06 13:53:36 +0200455 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200456 ha_panic();
457 return 1;
458}
459
460/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200461#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200462static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
463{
Willy Tarreau368bff42019-12-06 17:18:28 +0100464 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200465 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100466 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200467
468 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
469 return 1;
470
Willy Tarreau4781b152021-04-06 13:53:36 +0200471 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200472 chunk_reset(&trash);
473 for (arg = 3; *args[arg]; arg++) {
474 if (arg > 3)
475 chunk_strcat(&trash, " ");
476 chunk_strcat(&trash, args[arg]);
477 }
478
Willy Tarreau368bff42019-12-06 17:18:28 +0100479 thread_isolate();
480 if (pipe(pipefd) < 0)
481 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200482
Willy Tarreau368bff42019-12-06 17:18:28 +0100483 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
484 goto fail_fcntl;
485
486 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
487 goto fail_fcntl;
488
489 pid = fork();
490
491 if (pid < 0)
492 goto fail_fork;
493 else if (pid == 0) {
494 /* child */
495 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
496
497 close(0);
498 dup2(pipefd[1], 1);
499 dup2(pipefd[1], 2);
500
501 cmd[2] = trash.area;
502 execvp(cmd[0], cmd);
503 printf("execvp() failed\n");
504 exit(1);
505 }
506
507 /* parent */
508 thread_release();
509 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200510 chunk_reset(&trash);
511 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100512 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
513 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200514 break;
515 trash.data += ret;
516 if (trash.data + 20 == trash.size) {
517 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
518 break;
519 }
520 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100521 close(pipefd[0]);
522 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200523 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200524 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100525
526 fail_fork:
527 fail_fcntl:
528 close(pipefd[0]);
529 close(pipefd[1]);
530 fail_pipe:
531 thread_release();
532 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200533}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200534#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200535
536/* parse a "debug dev hex" command. It always returns 1. */
537static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
538{
539 unsigned long start, len;
540
541 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
542 return 1;
543
Willy Tarreau9d008692019-08-09 11:21:01 +0200544 if (!*args[3])
545 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200546
547 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200548 if (!start)
549 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200550
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200552
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200553 /* by default, dump ~128 till next block of 16 */
554 len = strtoul(args[4], NULL, 0);
555 if (!len)
556 len = ((start + 128) & -16) - start;
557
558 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200559 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200560 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200561 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200562}
563
Willy Tarreau48129be2021-05-04 18:40:50 +0200564/* parse a "debug dev sym <addr>" command. It always returns 1. */
565static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
566{
567 unsigned long addr;
568
569 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
570 return 1;
571
572 if (!*args[3])
573 return cli_err(appctx, "Missing memory address to be resolved.\n");
574
575 _HA_ATOMIC_INC(&debug_commands_issued);
576
577 addr = strtoul(args[3], NULL, 0);
578 chunk_printf(&trash, "%#lx resolves to ", addr);
579 resolve_sym_name(&trash, NULL, (const void *)addr);
580 chunk_appendf(&trash, "\n");
581
582 return cli_msg(appctx, LOG_INFO, trash.area);
583}
584
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200585/* parse a "debug dev tkill" command. It always returns 1. */
586static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
587{
588 int thr = 0;
589 int sig = SIGABRT;
590
591 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
592 return 1;
593
594 if (*args[3])
595 thr = atoi(args[3]);
596
Willy Tarreau9d008692019-08-09 11:21:01 +0200597 if (thr < 0 || thr > global.nbthread)
598 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200599
600 if (*args[4])
601 sig = atoi(args[4]);
602
Willy Tarreau4781b152021-04-06 13:53:36 +0200603 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200604 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200605 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200606 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200607 raise(sig);
608 return 1;
609}
610
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100611/* parse a "debug dev write" command. It always returns 1. */
612static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
613{
614 unsigned long len;
615
616 if (!*args[3])
617 return cli_err(appctx, "Missing output size.\n");
618
619 len = strtoul(args[3], NULL, 0);
620 if (len >= trash.size)
621 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
622
Willy Tarreau4781b152021-04-06 13:53:36 +0200623 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100624
625 chunk_reset(&trash);
626 trash.data = len;
627 memset(trash.area, '.', trash.data);
628 trash.area[trash.data] = 0;
629 for (len = 64; len < trash.data; len += 64)
630 trash.area[len] = '\n';
631 return cli_msg(appctx, LOG_INFO, trash.area);
632}
633
Willy Tarreau68680bb2019-10-23 17:23:25 +0200634/* parse a "debug dev stream" command */
635/*
636 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
637 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
638 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
639 * [sif.s[=<state>]] [sib.s[=<state>]]
640 */
641static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
642{
643 struct stream *s = si_strm(appctx->owner);
644 int arg;
645 void *ptr;
646 int size;
647 const char *word, *end;
648 struct ist name;
649 char *msg = NULL;
650 char *endarg;
651 unsigned long long old, new;
652
653 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
654 return 1;
655
656 ptr = NULL; size = 0;
657
658 if (!*args[3]) {
659 return cli_err(appctx,
660 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
661 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
662 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
663 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
664 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
665 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
666 );
667 }
668
Willy Tarreau4781b152021-04-06 13:53:36 +0200669 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200670 for (arg = 3; *args[arg]; arg++) {
671 old = 0;
672 end = word = args[arg];
673 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
674 end++;
675 name = ist2(word, end - word);
676 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200677 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200678 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200679 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200680 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200681 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200682 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200683 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200684 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200685 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200686 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200687 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200688 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200689 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200690 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200691 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200692 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200693 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200694 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200695 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200696 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200697 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200698 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200699 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200700 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200701 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200702 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200703 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200704 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200705 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200706 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200707 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200708 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
709 continue;
710 } else
711 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
712
713 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200714 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200715 if (size == 8)
716 old = read_u64(ptr);
717 else if (size == 4)
718 old = read_u32(ptr);
719 else if (size == 2)
720 old = read_u16(ptr);
721 else
722 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200723 } else {
724 memprintf(&msg,
725 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
726 msg ? msg : "", ptr, (int)(end - word), word);
727 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200728 }
729
730 /* parse the new value . */
731 new = strtoll(end + 1, &endarg, 0);
732 if (end[1] && *endarg) {
733 if (strcmp(end + 1, "now") == 0)
734 new = now_ms;
735 else {
736 memprintf(&msg,
737 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
738 msg ? msg : "", end + 1, (int)(end - word), word);
739 continue;
740 }
741 }
742
743 switch (*end) {
744 case '\0': /* show */
745 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
746 new = old; // do not change the value
747 break;
748
749 case '=': /* set */
750 break;
751
752 case '^': /* XOR */
753 new = old ^ new;
754 break;
755
756 case '+': /* OR */
757 new = old | new;
758 break;
759
760 case '-': /* AND NOT */
761 new = old & ~new;
762 break;
763
764 default:
765 break;
766 }
767
768 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200769 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200770 if (size == 8)
771 write_u64(ptr, new);
772 else if (size == 4)
773 write_u32(ptr, new);
774 else if (size == 2)
775 write_u16(ptr, new);
776 else
777 *(uint8_t *)ptr = new;
778 }
779 }
780
781 if (msg && *msg)
782 return cli_dynmsg(appctx, LOG_INFO, msg);
783 return 1;
784}
785
Willy Tarreau144f84a2021-03-02 16:09:26 +0100786static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100787{
788 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
789 unsigned long inter = tctx[1];
790 unsigned long rnd;
791
792 t->expire = tick_add(now_ms, inter);
793
794 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100795 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100796 if (rnd & 1) {
797 rnd >>= 1;
798 rnd %= tctx[0];
799 rnd = tctx[rnd + 2];
800
801 if (rnd & 1)
802 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
803 else
804 tasklet_wakeup((struct tasklet *)rnd);
805 }
806 return t;
807}
808
Willy Tarreau144f84a2021-03-02 16:09:26 +0100809static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100810{
811 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
812 unsigned long rnd;
813 int i;
814
815 /* wake up two random entries */
816 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100817 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100818 rnd = tctx[rnd + 2];
819
820 if (rnd & 1)
821 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
822 else
823 tasklet_wakeup((struct tasklet *)rnd);
824 }
825 return t;
826}
827
828/* parse a "debug dev sched" command
829 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
830 */
831static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
832{
833 int arg;
834 void *ptr;
835 int size;
836 const char *word, *end;
837 struct ist name;
838 char *msg = NULL;
839 char *endarg;
840 unsigned long long new;
841 unsigned long count = 0;
842 unsigned long thrid = 0;
843 unsigned int inter = 0;
844 unsigned long mask, tmask;
845 unsigned long i;
846 int mode = 0; // 0 = tasklet; 1 = task
847 int single = 0;
848 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
849
850 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
851 return 1;
852
853 ptr = NULL; size = 0;
854 mask = all_threads_mask;
855
856 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
857 return cli_err(appctx,
858 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
859 " <obj> = {count | mask | inter | single }\n"
860 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
861 );
862 }
863
864 mode = strcmp(args[3], "task") == 0;
865
Willy Tarreau4781b152021-04-06 13:53:36 +0200866 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100867 for (arg = 4; *args[arg]; arg++) {
868 end = word = args[arg];
869 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
870 end++;
871 name = ist2(word, end - word);
872 if (isteq(name, ist("count"))) {
873 ptr = &count; size = sizeof(count);
874 } else if (isteq(name, ist("mask"))) {
875 ptr = &mask; size = sizeof(mask);
876 } else if (isteq(name, ist("tid"))) {
877 ptr = &thrid; size = sizeof(thrid);
878 } else if (isteq(name, ist("inter"))) {
879 ptr = &inter; size = sizeof(inter);
880 } else if (isteq(name, ist("single"))) {
881 ptr = &single; size = sizeof(single);
882 } else
883 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
884
885 /* parse the new value . */
886 new = strtoll(end + 1, &endarg, 0);
887 if (end[1] && *endarg) {
888 memprintf(&msg,
889 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
890 msg ? msg : "", end + 1, (int)(end - word), word);
891 continue;
892 }
893
894 /* write the new value */
895 if (size == 8)
896 write_u64(ptr, new);
897 else if (size == 4)
898 write_u32(ptr, new);
899 else if (size == 2)
900 write_u16(ptr, new);
901 else
902 *(uint8_t *)ptr = new;
903 }
904
Willy Tarreau1cc96512024-02-10 11:35:07 +0100905 tctx = calloc(count + 2, sizeof(*tctx));
Willy Tarreaua5a44792020-11-29 17:12:15 +0100906 if (!tctx)
907 goto fail;
908
909 tctx[0] = (unsigned long)count;
910 tctx[1] = (unsigned long)inter;
911
912 mask &= all_threads_mask;
913 if (!mask)
914 mask = tid_bit;
915
916 tmask = 0;
917 for (i = 0; i < count; i++) {
918 if (single || mode == 0) {
919 /* look for next bit matching a bit in mask or loop back to zero */
920 for (tmask <<= 1; !(mask & tmask); ) {
921 if (!(mask & -tmask))
922 tmask = 1;
923 else
924 tmask <<= 1;
925 }
926 } else {
927 /* multi-threaded task */
928 tmask = mask;
929 }
930
931 /* now, if poly or mask was set, tmask corresponds to the
932 * valid thread mask to use, otherwise it remains zero.
933 */
934 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
935 if (mode == 0) {
936 struct tasklet *tl = tasklet_new();
937
938 if (!tl)
939 goto fail;
940
941 if (tmask)
942 tl->tid = my_ffsl(tmask) - 1;
943 tl->process = debug_tasklet_handler;
944 tl->context = tctx;
945 tctx[i + 2] = (unsigned long)tl;
946 } else {
947 struct task *task = task_new(tmask ? tmask : tid_bit);
948
949 if (!task)
950 goto fail;
951
952 task->process = debug_task_handler;
953 task->context = tctx;
954 tctx[i + 2] = (unsigned long)task + 1;
955 }
956 }
957
958 /* start the tasks and tasklets */
959 for (i = 0; i < count; i++) {
960 unsigned long ctx = tctx[i + 2];
961
962 if (ctx & 1)
963 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
964 else
965 tasklet_wakeup((struct tasklet *)ctx);
966 }
967
968 if (msg && *msg)
969 return cli_dynmsg(appctx, LOG_INFO, msg);
970 return 1;
971
972 fail:
973 /* free partially allocated entries */
974 for (i = 0; tctx && i < count; i++) {
975 unsigned long ctx = tctx[i + 2];
976
977 if (!ctx)
978 break;
979
980 if (ctx & 1)
981 task_destroy((struct task *)(ctx - 1));
982 else
983 tasklet_free((struct tasklet *)ctx);
984 }
985
986 free(tctx);
987 return cli_err(appctx, "Not enough memory");
988}
989
Willy Tarreau476252b2022-01-24 20:26:09 +0100990/* CLI parser for the "debug dev fd" command. The current FD to restart from is
991 * stored in i0.
992 */
993static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
994{
995 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
996 return 1;
997
998 /* start at fd #0 */
999 appctx->ctx.cli.i0 = 0;
1000 return 0;
1001}
1002
1003/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1004 * accessible from the process but not known from fdtab. The FD number to
1005 * restart from is stored in i0.
1006 */
1007static int debug_iohandler_fd(struct appctx *appctx)
1008{
1009 struct stream_interface *si = appctx->owner;
1010 struct sockaddr_storage sa;
1011 struct stat statbuf;
1012 socklen_t salen, vlen;
1013 int ret1, ret2, port;
1014 char *addrstr;
1015 int ret = 1;
1016 int i, fd;
1017
1018 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1019 goto end;
1020
1021 chunk_reset(&trash);
1022
1023 thread_isolate();
1024
1025 /* we have two inner loops here, one for the proxy, the other one for
1026 * the buffer.
1027 */
1028 for (fd = appctx->ctx.cli.i0; fd < global.maxsock; fd++) {
1029 /* check for FD's existence */
1030 ret1 = fcntl(fd, F_GETFD, 0);
1031 if (ret1 == -1)
1032 continue; // not known to the process
1033 if (fdtab[fd].owner)
1034 continue; // well-known
1035
1036 /* OK we're seeing an orphan let's try to retrieve as much
1037 * information as possible about it.
1038 */
1039 chunk_printf(&trash, "%5d", fd);
1040
1041 if (fstat(fd, &statbuf) != -1) {
1042 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1043 isatty(fd) ? "tty.":
1044 S_ISREG(statbuf.st_mode) ? "file":
1045 S_ISDIR(statbuf.st_mode) ? "dir.":
1046 S_ISCHR(statbuf.st_mode) ? "chr.":
1047 S_ISBLK(statbuf.st_mode) ? "blk.":
1048 S_ISFIFO(statbuf.st_mode) ? "pipe":
1049 S_ISLNK(statbuf.st_mode) ? "link":
1050 S_ISSOCK(statbuf.st_mode) ? "sock":
1051#ifdef USE_EPOLL
Willy Tarreaub367a672023-07-02 10:49:49 +02001052 /* trick: epoll_ctl() will return -ENOENT when trying
1053 * to remove from a valid epoll FD an FD that was not
1054 * registered against it. But we don't want to risk
1055 * disabling a random FD. Instead we'll create a new
1056 * one by duplicating 0 (it should be valid since
1057 * pointing to a terminal or /dev/null), and try to
1058 * remove it.
1059 */
1060 ({
1061 int fd2 = dup(0);
1062 int ret = fd2;
1063 if (ret >= 0) {
1064 ret = epoll_ctl(fd, EPOLL_CTL_DEL, fd2, NULL);
1065 if (ret == -1 && errno == ENOENT)
1066 ret = 0; // that's a real epoll
1067 else
1068 ret = -1; // it's something else
1069 close(fd2);
1070 }
1071 ret;
1072 }) == 0 ? "epol" :
Willy Tarreau476252b2022-01-24 20:26:09 +01001073#endif
1074 "????",
1075 (uint)statbuf.st_mode & 07777,
1076
1077 (ullong)statbuf.st_rdev,
1078 (ullong)statbuf.st_size,
1079 (ullong)statbuf.st_uid,
1080 (ullong)statbuf.st_gid,
1081
1082 (ullong)statbuf.st_dev,
1083 (ullong)statbuf.st_ino);
1084 }
1085
1086 chunk_appendf(&trash, " getfd=%s+%#x",
1087 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1088 ret1 &~ FD_CLOEXEC);
1089
1090 /* FD options */
1091 ret2 = fcntl(fd, F_GETFL, 0);
1092 if (ret2) {
1093 chunk_appendf(&trash, " getfl=%s",
1094 (ret1 & 3) >= 2 ? "O_RDWR" :
1095 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1096
1097 for (i = 2; i < 32; i++) {
1098 if (!(ret2 & (1UL << i)))
1099 continue;
1100 switch (1UL << i) {
1101 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1102 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1103 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1104 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1105 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreauc899f0f2022-01-25 14:51:53 +01001106#ifdef O_ASYNC
Willy Tarreau476252b2022-01-24 20:26:09 +01001107 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreauc899f0f2022-01-25 14:51:53 +01001108#endif
Willy Tarreau476252b2022-01-24 20:26:09 +01001109#ifdef O_DIRECT
1110 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1111#endif
1112#ifdef O_NOATIME
1113 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1114#endif
1115 }
1116 }
1117 }
1118
1119 vlen = sizeof(ret2);
1120 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1121 if (ret1 != -1)
1122 chunk_appendf(&trash, " so_type=%d", ret2);
1123
1124 vlen = sizeof(ret2);
1125 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1126 if (ret1 != -1)
1127 chunk_appendf(&trash, " so_accept=%d", ret2);
1128
1129 vlen = sizeof(ret2);
1130 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1131 if (ret1 != -1)
1132 chunk_appendf(&trash, " so_error=%d", ret2);
1133
1134 salen = sizeof(sa);
1135 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1136 if (sa.ss_family == AF_INET)
1137 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1138 else if (sa.ss_family == AF_INET6)
1139 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1140 else
1141 port = 0;
1142 addrstr = sa2str(&sa, port, 0);
1143 chunk_appendf(&trash, " laddr=%s", addrstr);
1144 free(addrstr);
1145 }
1146
1147 salen = sizeof(sa);
1148 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1149 if (sa.ss_family == AF_INET)
1150 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1151 else if (sa.ss_family == AF_INET6)
1152 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1153 else
1154 port = 0;
1155 addrstr = sa2str(&sa, port, 0);
1156 chunk_appendf(&trash, " raddr=%s", addrstr);
1157 free(addrstr);
1158 }
1159
1160 chunk_appendf(&trash, "\n");
1161
1162 if (ci_putchk(si_ic(si), &trash) == -1) {
1163 si_rx_room_blk(si);
1164 appctx->ctx.cli.i0 = fd;
1165 ret = 0;
1166 break;
1167 }
1168 }
1169
1170 thread_release();
1171 end:
1172 return ret;
1173}
1174
Willy Tarreaua6026a02020-07-02 09:14:48 +02001175#if defined(DEBUG_MEM_STATS)
1176/* CLI parser for the "debug dev memstats" command */
1177static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1178{
1179 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1180 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1181
1182 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1183 return 1;
1184
1185 if (strcmp(args[3], "reset") == 0) {
1186 struct mem_stats *ptr;
1187
1188 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1189 return 1;
1190
1191 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1192 _HA_ATOMIC_STORE(&ptr->calls, 0);
1193 _HA_ATOMIC_STORE(&ptr->size, 0);
1194 }
1195 return 1;
1196 }
1197
1198 if (strcmp(args[3], "all") == 0)
1199 appctx->ctx.cli.i0 = 1;
1200
1201 /* otherwise proceed with the dump from p0 to p1 */
1202 appctx->ctx.cli.p0 = &__start_mem_stats;
1203 appctx->ctx.cli.p1 = &__stop_mem_stats;
1204 return 0;
1205}
1206
1207/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
1208 * structs referenced by pointers located between p0 and p1. Dumps all entries
1209 * if i0 > 0, otherwise only non-zero calls.
1210 */
1211static int debug_iohandler_memstats(struct appctx *appctx)
1212{
1213 struct stream_interface *si = appctx->owner;
1214 struct mem_stats *ptr = appctx->ctx.cli.p0;
1215 int ret = 1;
1216
1217 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1218 goto end;
1219
1220 chunk_reset(&trash);
1221
1222 /* we have two inner loops here, one for the proxy, the other one for
1223 * the buffer.
1224 */
1225 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1226 const char *type;
1227 const char *name;
1228 const char *p;
1229
1230 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1231 continue;
1232
1233 /* basename only */
1234 for (p = name = ptr->file; *p; p++) {
1235 if (*p == '/')
1236 name = p + 1;
1237 }
1238
1239 switch (ptr->type) {
1240 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1241 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1242 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1243 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1244 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1245 default: type = "UNSET"; break;
1246 }
1247
1248 //chunk_printf(&trash,
1249 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1250 // name, ptr->line, type,
1251 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1252 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1253
1254 chunk_printf(&trash, "%s:%d", name, ptr->line);
1255 while (trash.data < 25)
1256 trash.area[trash.data++] = ' ';
1257 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1258 type,
1259 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1260 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1261
1262 if (ci_putchk(si_ic(si), &trash) == -1) {
1263 si_rx_room_blk(si);
1264 appctx->ctx.cli.p0 = ptr;
1265 ret = 0;
1266 break;
1267 }
1268 }
1269
1270 end:
1271 return ret;
1272}
1273
1274#endif
1275
Willy Tarreauc7091d82019-05-17 10:08:49 +02001276#ifndef USE_THREAD_DUMP
1277
1278/* This function dumps all threads' state to the trash. This version is the
1279 * most basic one, which doesn't inspect other threads.
1280 */
1281void ha_thread_dump_all_to_trash()
1282{
1283 unsigned int thr;
1284
1285 for (thr = 0; thr < global.nbthread; thr++)
1286 ha_thread_dump(&trash, thr, tid);
1287}
1288
1289#else /* below USE_THREAD_DUMP is set */
1290
Willy Tarreauc7091d82019-05-17 10:08:49 +02001291/* ID of the thread requesting the dump */
1292static unsigned int thread_dump_tid;
1293
1294/* points to the buffer where the dump functions should write. It must
1295 * have already been initialized by the requester. Nothing is done if
1296 * it's NULL.
1297 */
1298struct buffer *thread_dump_buffer = NULL;
1299
1300void ha_thread_dump_all_to_trash()
1301{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001302 unsigned long old;
1303
1304 while (1) {
1305 old = 0;
1306 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1307 break;
1308 ha_thread_relax();
1309 }
1310
1311 thread_dump_buffer = &trash;
1312 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001313 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001314}
1315
1316/* handles DEBUGSIG to dump the state of the thread it's working on */
1317void debug_handler(int sig, siginfo_t *si, void *arg)
1318{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001319 /* first, let's check it's really for us and that we didn't just get
1320 * a spurious DEBUGSIG.
1321 */
1322 if (!(threads_to_dump & tid_bit))
1323 return;
1324
Willy Tarreauc7091d82019-05-17 10:08:49 +02001325 /* There are 4 phases in the dump process:
1326 * 1- wait for our turn, i.e. when all lower bits are gone.
1327 * 2- perform the action if our bit is set
1328 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001329 * the last one and have to put them all as a signal
1330 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001331 */
1332
1333 /* wait for all previous threads to finish first */
1334 while (threads_to_dump & (tid_bit - 1))
1335 ha_thread_relax();
1336
1337 /* dump if needed */
1338 if (threads_to_dump & tid_bit) {
1339 if (thread_dump_buffer)
1340 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1341 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1342 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001343 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001344 thread_dump_buffer = NULL;
1345 }
1346 else
1347 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1348 }
1349
1350 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001351 * bits again to broadcast the leaving condition so we'll see ourselves
1352 * present again. This way the threads_to_dump variable never passes to
1353 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001354 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001355 while (!(threads_to_dump & tid_bit))
1356 ha_thread_relax();
1357 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001358
1359 /* mark the current thread as stuck to detect it upon next invocation
1360 * if it didn't move.
1361 */
1362 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1363 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001364}
1365
1366static int init_debug_per_thread()
1367{
1368 sigset_t set;
1369
1370 /* unblock the DEBUGSIG signal we intend to use */
1371 sigemptyset(&set);
1372 sigaddset(&set, DEBUGSIG);
1373 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1374 return 1;
1375}
1376
1377static int init_debug()
1378{
1379 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001380 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001381
Willy Tarreau0214b452020-03-04 06:01:40 +01001382 /* calling backtrace() will access libgcc at runtime. We don't want to
1383 * do it after the chroot, so let's perform a first call to have it
1384 * ready in memory for later use.
1385 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001386 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001387 sa.sa_handler = NULL;
1388 sa.sa_sigaction = debug_handler;
1389 sigemptyset(&sa.sa_mask);
1390 sa.sa_flags = SA_SIGINFO;
1391 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001392 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001393}
1394
1395REGISTER_POST_CHECK(init_debug);
1396REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1397
1398#endif /* USE_THREAD_DUMP */
1399
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001400/* register cli keywords */
1401static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001402 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1403 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1404 {{ "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 +02001405#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001406 {{ "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 +02001407#endif
Willy Tarreau476252b2022-01-24 20:26:09 +01001408 {{ "debug", "dev", "fd", NULL }, "debug dev fd : scan for rogue/unhandled FDs", debug_parse_cli_fd, debug_iohandler_fd, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001409 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1410 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1411 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1412 {{ "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 +02001413#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001414 {{ "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 +02001415#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001416 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1417 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1418 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1419 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1420 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
1421 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau37857332021-12-28 09:57:10 +01001422#if defined(HA_HAVE_DUMP_LIBS)
1423 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1424#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001425 {{ "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 +02001426 {{},}
1427}};
1428
1429INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);