blob: 5d34389e5d89fc862d648772390fe7265c2f3dda [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 Tarreau5be7c192022-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 Tarreau5be7c192022-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 Tarreau5be7c192022-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 Tarreau55542642021-10-08 09:33:24 +020032#include <haproxy/clock.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020033#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/fd.h>
35#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020036#include <haproxy/hlua.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020037#include <haproxy/http_ana.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020038#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/net_helper.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020041#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020042#include <haproxy/thread.h>
Willy Tarreau55542642021-10-08 09:33:24 +020043#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020046
Willy Tarreau4e2b6462019-05-16 17:44:30 +020047
Willy Tarreaua37cb182019-07-31 19:20:39 +020048/* mask of threads still having to dump, used to respect ordering. Only used
49 * when USE_THREAD_DUMP is set.
50 */
51volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020052unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020053
Willy Tarreau123fc972021-01-22 13:52:41 +010054/* dumps a backtrace of the current thread that is appended to buffer <buf>.
55 * Lines are prefixed with the string <prefix> which may be empty (used for
56 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010057 * the function does not appear in the call stack. The <dump> argument
58 * indicates what dump state to start from, and should usually be zero. It
59 * may be among the following values:
60 * - 0: search usual callers before step 1, or directly jump to 2
61 * - 1: skip usual callers before step 2
62 * - 2: dump until polling loop, scheduler, or main() (excluded)
63 * - 3: end
64 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010065 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010066void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010067{
68 struct buffer bak;
69 char pfx2[100];
70 void *callers[100];
71 int j, nptrs;
72 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010073
74 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
75 if (!nptrs)
76 return;
77
78 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
79 pfx2[0] = 0;
80
81 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
82 * produce similar output to the following:
83 */
84 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010085 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
86 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010087 /* we failed to spot the starting point of the
88 * dump, let's start over dumping everything we
89 * have.
90 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010091 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010092 j = 0;
93 }
94 bak = *buf;
95 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
96 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010097 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010098 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010099 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +0100100 */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100101 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
102 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100103 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100104 *buf = bak;
105 continue;
106 }
107
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100108 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100109 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100110 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
111 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100112 *buf = bak;
113 continue;
114 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100115 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100116 }
117
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100118 if ((dump & 3) == 2) {
119 /* still dumping */
120 if (dump == 6) {
121 /* we only stop *after* main and we must send the LF */
122 if (addr == main) {
123 j = nptrs;
124 dump++;
125 }
126 }
127 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
128 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100129 *buf = bak;
130 break;
131 }
132 }
133 /* OK, line dumped */
134 chunk_appendf(buf, "\n");
135 }
136}
137
Willy Tarreaua8459b22021-01-22 14:12:27 +0100138/* dump a backtrace of current thread's stack to stderr. */
139void ha_backtrace_to_stderr()
140{
141 char area[2048];
142 struct buffer b = b_make(area, sizeof(area), 0, 0);
143
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100144 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100145 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100146 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100147}
148
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200149/* Dumps to the buffer some known information for the desired thread, and
150 * optionally extra info for the current thread. The dump will be appended to
151 * the buffer, so the caller is responsible for preliminary initializing it.
152 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200153 * in front of the calling thread's line (usually it's tid). Any stuck thread
154 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200155 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200156void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200157{
158 unsigned long thr_bit = 1UL << thr;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200159 unsigned long long p = ha_thread_ctx[thr].prev_cpu_time;
Willy Tarreau21694982021-10-08 15:09:17 +0200160 unsigned long long n = now_cpu_time_thread(thr);
Willy Tarreaua0b99532021-09-30 18:48:37 +0200161 int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200162
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200163 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200164 "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
Willy Tarreaua3870b72021-09-13 19:24:47 +0200165 " %2u/%-2u stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200166 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200167 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200168 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200169 !!(global_tasks_mask & thr_bit),
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200170 !eb_is_empty(&ha_thread_ctx[thr].timers),
171 !eb_is_empty(&ha_thread_ctx[thr].rqueue),
172 !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) &&
173 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) &&
174 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) &&
175 MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)),
176 ha_thread_ctx[thr].tasks_in_list,
177 ha_thread_ctx[thr].rq_total,
Willy Tarreaua3870b72021-09-13 19:24:47 +0200178 ha_thread_info[thr].tg->tgid, ha_thread_info[thr].ltid + 1,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200179 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200180 !!(task_profiling_mask & thr_bit));
181
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200182 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200183 " harmless=%d wantrdv=%d",
184 !!(threads_harmless_mask & thr_bit),
185 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200186
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200187 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200188 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200189
Willy Tarreaua3870b72021-09-13 19:24:47 +0200190 /* this is the end of what we can dump from outside the current thread */
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200191
192 if (thr != tid)
193 return;
194
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200195 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200196 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100197
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100198 if (stuck) {
199 /* We only emit the backtrace for stuck threads in order not to
200 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100201 * Please leave this as the last instruction in this function
202 * so that the compiler uses tail merging and the current
203 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100204 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100205 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100206 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200207}
208
209
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200210/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200211 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200212 * with <pfx>. The buffer is only appended and the first output starts by the
213 * pointer itself. The caller is responsible for making sure the task is not
214 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200215 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200216void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200217{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200218 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200219 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200220 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200221
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200222 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200223 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200224 return;
225 }
226
Willy Tarreau20db9112019-05-17 14:14:35 +0200227 if (TASK_IS_TASKLET(task))
228 chunk_appendf(buf,
229 "%p (tasklet) calls=%u\n",
230 task,
231 task->calls);
232 else
233 chunk_appendf(buf,
234 "%p (task) calls=%u last=%llu%s\n",
235 task,
236 task->calls,
237 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
238 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200239
Willy Tarreau2e89b092020-03-03 17:13:02 +0100240 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
241 resolve_sym_name(buf, NULL, task->process);
242 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200243
Willy Tarreaua512b022019-08-21 14:12:19 +0200244 if (task->process == task_run_applet && (appctx = task->context))
245 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
246 else
247 chunk_appendf(buf, "\n");
248
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200249 if (task->process == process_stream && task->context)
250 s = (struct stream *)task->context;
251 else if (task->process == task_run_applet && task->context)
Christopher Faulet86e1c332021-12-20 17:09:39 +0100252 s = si_strm(cs_si(((struct appctx *)task->context)->owner));
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200253 else if (task->process == si_cs_io_cb && task->context)
254 s = si_strm((struct stream_interface *)task->context);
255
256 if (s)
257 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200258
259#ifdef USE_LUA
260 hlua = NULL;
261 if (s && (hlua = s->hlua)) {
262 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
263 }
264 else if (task->process == hlua_process_task && (hlua = task->context)) {
265 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
266 }
267 else if (task->process == task_run_applet && (appctx = task->context) &&
268 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
269 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
270 }
271 else if (task->process == task_run_applet && (appctx = task->context) &&
272 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
273 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
274 }
275
Christopher Faulet471425f2020-07-24 19:08:05 +0200276 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100277 chunk_appendf(buf, "stack traceback:\n ");
278 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
279 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200280 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200281 else
282 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{
Christopher Faulet86e1c332021-12-20 17:09:39 +0100291 struct stream_interface *si = cs_si(appctx->owner);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200292 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 Tarreau6ab7b212021-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{
332 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200333 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200334 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100335 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200336 for (;;)
337 abort();
338}
339
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200340/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
341static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
342{
343 int code = atoi(args[3]);
344
345 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
346 return 1;
347
Willy Tarreau4781b152021-04-06 13:53:36 +0200348 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200349 exit(code);
350 return 1;
351}
352
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100353/* parse a "debug dev bug" command. It always returns 1, though it should never return.
354 * Note: we make sure not to make the function static so that it appears in the trace.
355 */
356int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
357{
358 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
359 return 1;
360
Willy Tarreau4781b152021-04-06 13:53:36 +0200361 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100362 BUG_ON(one > zero);
363 return 1;
364}
365
Willy Tarreau305cfbd2022-02-25 08:52:39 +0100366/* parse a "debug dev warn" command. It always returns 1.
367 * Note: we make sure not to make the function static so that it appears in the trace.
368 */
369int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
370{
371 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
372 return 1;
373
374 _HA_ATOMIC_INC(&debug_commands_issued);
375 WARN_ON(one > zero);
376 return 1;
377}
378
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379/* parse a "debug dev close" command. It always returns 1. */
380static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
381{
382 int fd;
383
384 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
385 return 1;
386
Willy Tarreau9d008692019-08-09 11:21:01 +0200387 if (!*args[3])
388 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200389
390 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200391 if (fd < 0 || fd >= global.maxsock)
392 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200393
Willy Tarreau9d008692019-08-09 11:21:01 +0200394 if (!fdtab[fd].owner)
395 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200396
Willy Tarreau4781b152021-04-06 13:53:36 +0200397 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200398 fd_delete(fd);
399 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200400}
401
402/* parse a "debug dev delay" command. It always returns 1. */
403static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
404{
405 int delay = atoi(args[3]);
406
407 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
408 return 1;
409
Willy Tarreau4781b152021-04-06 13:53:36 +0200410 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200411 usleep((long)delay * 1000);
412 return 1;
413}
414
415/* parse a "debug dev log" command. It always returns 1. */
416static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
417{
418 int arg;
419
420 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
421 return 1;
422
Willy Tarreau4781b152021-04-06 13:53:36 +0200423 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424 chunk_reset(&trash);
425 for (arg = 3; *args[arg]; arg++) {
426 if (arg > 3)
427 chunk_strcat(&trash, " ");
428 chunk_strcat(&trash, args[arg]);
429 }
430
431 send_log(NULL, LOG_INFO, "%s\n", trash.area);
432 return 1;
433}
434
435/* parse a "debug dev loop" command. It always returns 1. */
436static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
437{
438 struct timeval deadline, curr;
439 int loop = atoi(args[3]);
440
441 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
442 return 1;
443
Willy Tarreau4781b152021-04-06 13:53:36 +0200444 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200445 gettimeofday(&curr, NULL);
446 tv_ms_add(&deadline, &curr, loop);
447
448 while (tv_ms_cmp(&curr, &deadline) < 0)
449 gettimeofday(&curr, NULL);
450
451 return 1;
452}
453
454/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
455static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
456{
457 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
458 return 1;
459
Willy Tarreau4781b152021-04-06 13:53:36 +0200460 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200461 ha_panic();
462 return 1;
463}
464
465/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200466#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200467static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
468{
Willy Tarreau368bff42019-12-06 17:18:28 +0100469 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200470 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100471 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200472
473 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
474 return 1;
475
Willy Tarreau4781b152021-04-06 13:53:36 +0200476 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200477 chunk_reset(&trash);
478 for (arg = 3; *args[arg]; arg++) {
479 if (arg > 3)
480 chunk_strcat(&trash, " ");
481 chunk_strcat(&trash, args[arg]);
482 }
483
Willy Tarreau368bff42019-12-06 17:18:28 +0100484 thread_isolate();
485 if (pipe(pipefd) < 0)
486 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200487
Willy Tarreau368bff42019-12-06 17:18:28 +0100488 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
489 goto fail_fcntl;
490
491 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
492 goto fail_fcntl;
493
494 pid = fork();
495
496 if (pid < 0)
497 goto fail_fork;
498 else if (pid == 0) {
499 /* child */
500 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
501
502 close(0);
503 dup2(pipefd[1], 1);
504 dup2(pipefd[1], 2);
505
506 cmd[2] = trash.area;
507 execvp(cmd[0], cmd);
508 printf("execvp() failed\n");
509 exit(1);
510 }
511
512 /* parent */
513 thread_release();
514 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200515 chunk_reset(&trash);
516 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100517 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
518 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200519 break;
520 trash.data += ret;
521 if (trash.data + 20 == trash.size) {
522 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
523 break;
524 }
525 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100526 close(pipefd[0]);
527 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200528 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200529 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100530
531 fail_fork:
532 fail_fcntl:
533 close(pipefd[0]);
534 close(pipefd[1]);
535 fail_pipe:
536 thread_release();
537 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200538}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200539#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200540
541/* parse a "debug dev hex" command. It always returns 1. */
542static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
543{
544 unsigned long start, len;
545
546 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
547 return 1;
548
Willy Tarreau9d008692019-08-09 11:21:01 +0200549 if (!*args[3])
550 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200551
552 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200553 if (!start)
554 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200555
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200557
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200558 /* by default, dump ~128 till next block of 16 */
559 len = strtoul(args[4], NULL, 0);
560 if (!len)
561 len = ((start + 128) & -16) - start;
562
563 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200564 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200565 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200566 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200567}
568
Willy Tarreau48129be2021-05-04 18:40:50 +0200569/* parse a "debug dev sym <addr>" command. It always returns 1. */
570static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
571{
572 unsigned long addr;
573
574 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
575 return 1;
576
577 if (!*args[3])
578 return cli_err(appctx, "Missing memory address to be resolved.\n");
579
580 _HA_ATOMIC_INC(&debug_commands_issued);
581
582 addr = strtoul(args[3], NULL, 0);
583 chunk_printf(&trash, "%#lx resolves to ", addr);
584 resolve_sym_name(&trash, NULL, (const void *)addr);
585 chunk_appendf(&trash, "\n");
586
587 return cli_msg(appctx, LOG_INFO, trash.area);
588}
589
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200590/* parse a "debug dev tkill" command. It always returns 1. */
591static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
592{
593 int thr = 0;
594 int sig = SIGABRT;
595
596 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
597 return 1;
598
599 if (*args[3])
600 thr = atoi(args[3]);
601
Willy Tarreau9d008692019-08-09 11:21:01 +0200602 if (thr < 0 || thr > global.nbthread)
603 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200604
605 if (*args[4])
606 sig = atoi(args[4]);
607
Willy Tarreau4781b152021-04-06 13:53:36 +0200608 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200609 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200610 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200611 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200612 raise(sig);
613 return 1;
614}
615
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100616/* parse a "debug dev write" command. It always returns 1. */
617static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
618{
619 unsigned long len;
620
621 if (!*args[3])
622 return cli_err(appctx, "Missing output size.\n");
623
624 len = strtoul(args[3], NULL, 0);
625 if (len >= trash.size)
626 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
627
Willy Tarreau4781b152021-04-06 13:53:36 +0200628 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100629
630 chunk_reset(&trash);
631 trash.data = len;
632 memset(trash.area, '.', trash.data);
633 trash.area[trash.data] = 0;
634 for (len = 64; len < trash.data; len += 64)
635 trash.area[len] = '\n';
636 return cli_msg(appctx, LOG_INFO, trash.area);
637}
638
Willy Tarreau68680bb2019-10-23 17:23:25 +0200639/* parse a "debug dev stream" command */
640/*
641 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
642 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
643 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
644 * [sif.s[=<state>]] [sib.s[=<state>]]
645 */
646static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
647{
Christopher Faulet86e1c332021-12-20 17:09:39 +0100648 struct stream *s = si_strm(cs_si(appctx->owner));
Willy Tarreau68680bb2019-10-23 17:23:25 +0200649 int arg;
650 void *ptr;
651 int size;
652 const char *word, *end;
653 struct ist name;
654 char *msg = NULL;
655 char *endarg;
656 unsigned long long old, new;
657
658 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
659 return 1;
660
661 ptr = NULL; size = 0;
662
663 if (!*args[3]) {
664 return cli_err(appctx,
665 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
666 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
667 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
668 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
669 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
670 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
671 );
672 }
673
Willy Tarreau4781b152021-04-06 13:53:36 +0200674 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200675 for (arg = 3; *args[arg]; arg++) {
676 old = 0;
677 end = word = args[arg];
678 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
679 end++;
680 name = ist2(word, end - word);
681 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200682 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200683 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200684 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200685 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200686 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200687 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200688 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200689 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200690 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200691 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200692 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200693 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200694 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200695 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200696 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200697 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200698 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200699 } else if (isteq(name, ist("sif.f"))) {
Christopher Faulet5d3c8aa2021-12-23 13:38:12 +0100700 ptr = (!s || !may_access(s)) ? NULL : &cs_si(s->csf)->flags; size = sizeof(cs_si(s->csf)->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200701 } else if (isteq(name, ist("sib.f"))) {
Christopher Faulet5d3c8aa2021-12-23 13:38:12 +0100702 ptr = (!s || !may_access(s)) ? NULL : &cs_si(s->csb)->flags; size = sizeof(cs_si(s->csb)->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200703 } else if (isteq(name, ist("sif.x"))) {
Christopher Faulet5d3c8aa2021-12-23 13:38:12 +0100704 ptr = (!s || !may_access(s)) ? NULL : &cs_si(s->csf)->exp; size = sizeof(cs_si(s->csf)->exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200705 } else if (isteq(name, ist("sib.x"))) {
Christopher Faulet5d3c8aa2021-12-23 13:38:12 +0100706 ptr = (!s || !may_access(s)) ? NULL : &cs_si(s->csb)->exp; size = sizeof(cs_si(s->csb)->exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200707 } else if (isteq(name, ist("sif.s"))) {
Christopher Faulet5d3c8aa2021-12-23 13:38:12 +0100708 ptr = (!s || !may_access(s)) ? NULL : &cs_si(s->csf)->state; size = sizeof(cs_si(s->csf)->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200709 } else if (isteq(name, ist("sib.s"))) {
Christopher Faulet5d3c8aa2021-12-23 13:38:12 +0100710 ptr = (!s || !may_access(s)) ? NULL : &cs_si(s->csf)->state; size = sizeof(cs_si(s->csb)->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200711 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200712 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200713 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
714 continue;
715 } else
716 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
717
718 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200719 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200720 if (size == 8)
721 old = read_u64(ptr);
722 else if (size == 4)
723 old = read_u32(ptr);
724 else if (size == 2)
725 old = read_u16(ptr);
726 else
727 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200728 } else {
729 memprintf(&msg,
730 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
731 msg ? msg : "", ptr, (int)(end - word), word);
732 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200733 }
734
735 /* parse the new value . */
736 new = strtoll(end + 1, &endarg, 0);
737 if (end[1] && *endarg) {
738 if (strcmp(end + 1, "now") == 0)
739 new = now_ms;
740 else {
741 memprintf(&msg,
742 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
743 msg ? msg : "", end + 1, (int)(end - word), word);
744 continue;
745 }
746 }
747
748 switch (*end) {
749 case '\0': /* show */
750 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
751 new = old; // do not change the value
752 break;
753
754 case '=': /* set */
755 break;
756
757 case '^': /* XOR */
758 new = old ^ new;
759 break;
760
761 case '+': /* OR */
762 new = old | new;
763 break;
764
765 case '-': /* AND NOT */
766 new = old & ~new;
767 break;
768
769 default:
770 break;
771 }
772
773 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200774 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200775 if (size == 8)
776 write_u64(ptr, new);
777 else if (size == 4)
778 write_u32(ptr, new);
779 else if (size == 2)
780 write_u16(ptr, new);
781 else
782 *(uint8_t *)ptr = new;
783 }
784 }
785
786 if (msg && *msg)
787 return cli_dynmsg(appctx, LOG_INFO, msg);
788 return 1;
789}
790
Willy Tarreau144f84a2021-03-02 16:09:26 +0100791static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100792{
793 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
794 unsigned long inter = tctx[1];
795 unsigned long rnd;
796
797 t->expire = tick_add(now_ms, inter);
798
799 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100800 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100801 if (rnd & 1) {
802 rnd >>= 1;
803 rnd %= tctx[0];
804 rnd = tctx[rnd + 2];
805
806 if (rnd & 1)
807 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
808 else
809 tasklet_wakeup((struct tasklet *)rnd);
810 }
811 return t;
812}
813
Willy Tarreau144f84a2021-03-02 16:09:26 +0100814static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100815{
816 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
817 unsigned long rnd;
818 int i;
819
820 /* wake up two random entries */
821 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100822 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100823 rnd = tctx[rnd + 2];
824
825 if (rnd & 1)
826 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
827 else
828 tasklet_wakeup((struct tasklet *)rnd);
829 }
830 return t;
831}
832
833/* parse a "debug dev sched" command
834 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
835 */
836static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
837{
838 int arg;
839 void *ptr;
840 int size;
841 const char *word, *end;
842 struct ist name;
843 char *msg = NULL;
844 char *endarg;
845 unsigned long long new;
846 unsigned long count = 0;
847 unsigned long thrid = 0;
848 unsigned int inter = 0;
849 unsigned long mask, tmask;
850 unsigned long i;
851 int mode = 0; // 0 = tasklet; 1 = task
852 int single = 0;
853 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
854
855 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
856 return 1;
857
858 ptr = NULL; size = 0;
859 mask = all_threads_mask;
860
861 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
862 return cli_err(appctx,
863 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
864 " <obj> = {count | mask | inter | single }\n"
865 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
866 );
867 }
868
869 mode = strcmp(args[3], "task") == 0;
870
Willy Tarreau4781b152021-04-06 13:53:36 +0200871 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100872 for (arg = 4; *args[arg]; arg++) {
873 end = word = args[arg];
874 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
875 end++;
876 name = ist2(word, end - word);
877 if (isteq(name, ist("count"))) {
878 ptr = &count; size = sizeof(count);
879 } else if (isteq(name, ist("mask"))) {
880 ptr = &mask; size = sizeof(mask);
881 } else if (isteq(name, ist("tid"))) {
882 ptr = &thrid; size = sizeof(thrid);
883 } else if (isteq(name, ist("inter"))) {
884 ptr = &inter; size = sizeof(inter);
885 } else if (isteq(name, ist("single"))) {
886 ptr = &single; size = sizeof(single);
887 } else
888 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
889
890 /* parse the new value . */
891 new = strtoll(end + 1, &endarg, 0);
892 if (end[1] && *endarg) {
893 memprintf(&msg,
894 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
895 msg ? msg : "", end + 1, (int)(end - word), word);
896 continue;
897 }
898
899 /* write the new value */
900 if (size == 8)
901 write_u64(ptr, new);
902 else if (size == 4)
903 write_u32(ptr, new);
904 else if (size == 2)
905 write_u16(ptr, new);
906 else
907 *(uint8_t *)ptr = new;
908 }
909
910 tctx = calloc(sizeof(*tctx), count + 2);
911 if (!tctx)
912 goto fail;
913
914 tctx[0] = (unsigned long)count;
915 tctx[1] = (unsigned long)inter;
916
917 mask &= all_threads_mask;
918 if (!mask)
919 mask = tid_bit;
920
921 tmask = 0;
922 for (i = 0; i < count; i++) {
923 if (single || mode == 0) {
924 /* look for next bit matching a bit in mask or loop back to zero */
925 for (tmask <<= 1; !(mask & tmask); ) {
926 if (!(mask & -tmask))
927 tmask = 1;
928 else
929 tmask <<= 1;
930 }
931 } else {
932 /* multi-threaded task */
933 tmask = mask;
934 }
935
936 /* now, if poly or mask was set, tmask corresponds to the
937 * valid thread mask to use, otherwise it remains zero.
938 */
939 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
940 if (mode == 0) {
941 struct tasklet *tl = tasklet_new();
942
943 if (!tl)
944 goto fail;
945
946 if (tmask)
947 tl->tid = my_ffsl(tmask) - 1;
948 tl->process = debug_tasklet_handler;
949 tl->context = tctx;
950 tctx[i + 2] = (unsigned long)tl;
951 } else {
952 struct task *task = task_new(tmask ? tmask : tid_bit);
953
954 if (!task)
955 goto fail;
956
957 task->process = debug_task_handler;
958 task->context = tctx;
959 tctx[i + 2] = (unsigned long)task + 1;
960 }
961 }
962
963 /* start the tasks and tasklets */
964 for (i = 0; i < count; i++) {
965 unsigned long ctx = tctx[i + 2];
966
967 if (ctx & 1)
968 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
969 else
970 tasklet_wakeup((struct tasklet *)ctx);
971 }
972
973 if (msg && *msg)
974 return cli_dynmsg(appctx, LOG_INFO, msg);
975 return 1;
976
977 fail:
978 /* free partially allocated entries */
979 for (i = 0; tctx && i < count; i++) {
980 unsigned long ctx = tctx[i + 2];
981
982 if (!ctx)
983 break;
984
985 if (ctx & 1)
986 task_destroy((struct task *)(ctx - 1));
987 else
988 tasklet_free((struct tasklet *)ctx);
989 }
990
991 free(tctx);
992 return cli_err(appctx, "Not enough memory");
993}
994
Willy Tarreau5be7c192022-01-24 20:26:09 +0100995/* CLI parser for the "debug dev fd" command. The current FD to restart from is
996 * stored in i0.
997 */
998static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
999{
1000 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1001 return 1;
1002
1003 /* start at fd #0 */
1004 appctx->ctx.cli.i0 = 0;
1005 return 0;
1006}
1007
1008/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1009 * accessible from the process but not known from fdtab. The FD number to
1010 * restart from is stored in i0.
1011 */
1012static int debug_iohandler_fd(struct appctx *appctx)
1013{
1014 struct stream_interface *si = appctx->owner;
1015 struct sockaddr_storage sa;
1016 struct stat statbuf;
1017 socklen_t salen, vlen;
1018 int ret1, ret2, port;
1019 char *addrstr;
1020 int ret = 1;
1021 int i, fd;
1022
1023 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1024 goto end;
1025
1026 chunk_reset(&trash);
1027
1028 thread_isolate();
1029
1030 /* we have two inner loops here, one for the proxy, the other one for
1031 * the buffer.
1032 */
1033 for (fd = appctx->ctx.cli.i0; fd < global.maxsock; fd++) {
1034 /* check for FD's existence */
1035 ret1 = fcntl(fd, F_GETFD, 0);
1036 if (ret1 == -1)
1037 continue; // not known to the process
1038 if (fdtab[fd].owner)
1039 continue; // well-known
1040
1041 /* OK we're seeing an orphan let's try to retrieve as much
1042 * information as possible about it.
1043 */
1044 chunk_printf(&trash, "%5d", fd);
1045
1046 if (fstat(fd, &statbuf) != -1) {
1047 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1048 isatty(fd) ? "tty.":
1049 S_ISREG(statbuf.st_mode) ? "file":
1050 S_ISDIR(statbuf.st_mode) ? "dir.":
1051 S_ISCHR(statbuf.st_mode) ? "chr.":
1052 S_ISBLK(statbuf.st_mode) ? "blk.":
1053 S_ISFIFO(statbuf.st_mode) ? "pipe":
1054 S_ISLNK(statbuf.st_mode) ? "link":
1055 S_ISSOCK(statbuf.st_mode) ? "sock":
1056#ifdef USE_EPOLL
1057 epoll_wait(fd, NULL, 0, 0) != -1 || errno != EBADF ? "epol":
1058#endif
1059 "????",
1060 (uint)statbuf.st_mode & 07777,
1061
1062 (ullong)statbuf.st_rdev,
1063 (ullong)statbuf.st_size,
1064 (ullong)statbuf.st_uid,
1065 (ullong)statbuf.st_gid,
1066
1067 (ullong)statbuf.st_dev,
1068 (ullong)statbuf.st_ino);
1069 }
1070
1071 chunk_appendf(&trash, " getfd=%s+%#x",
1072 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1073 ret1 &~ FD_CLOEXEC);
1074
1075 /* FD options */
1076 ret2 = fcntl(fd, F_GETFL, 0);
1077 if (ret2) {
1078 chunk_appendf(&trash, " getfl=%s",
1079 (ret1 & 3) >= 2 ? "O_RDWR" :
1080 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1081
1082 for (i = 2; i < 32; i++) {
1083 if (!(ret2 & (1UL << i)))
1084 continue;
1085 switch (1UL << i) {
1086 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1087 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1088 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1089 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1090 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001091#ifdef O_ASYNC
Willy Tarreau5be7c192022-01-24 20:26:09 +01001092 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001093#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001094#ifdef O_DIRECT
1095 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1096#endif
1097#ifdef O_NOATIME
1098 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1099#endif
1100 }
1101 }
1102 }
1103
1104 vlen = sizeof(ret2);
1105 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1106 if (ret1 != -1)
1107 chunk_appendf(&trash, " so_type=%d", ret2);
1108
1109 vlen = sizeof(ret2);
1110 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1111 if (ret1 != -1)
1112 chunk_appendf(&trash, " so_accept=%d", ret2);
1113
1114 vlen = sizeof(ret2);
1115 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1116 if (ret1 != -1)
1117 chunk_appendf(&trash, " so_error=%d", ret2);
1118
1119 salen = sizeof(sa);
1120 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1121 if (sa.ss_family == AF_INET)
1122 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1123 else if (sa.ss_family == AF_INET6)
1124 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1125 else
1126 port = 0;
1127 addrstr = sa2str(&sa, port, 0);
1128 chunk_appendf(&trash, " laddr=%s", addrstr);
1129 free(addrstr);
1130 }
1131
1132 salen = sizeof(sa);
1133 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1134 if (sa.ss_family == AF_INET)
1135 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1136 else if (sa.ss_family == AF_INET6)
1137 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1138 else
1139 port = 0;
1140 addrstr = sa2str(&sa, port, 0);
1141 chunk_appendf(&trash, " raddr=%s", addrstr);
1142 free(addrstr);
1143 }
1144
1145 chunk_appendf(&trash, "\n");
1146
1147 if (ci_putchk(si_ic(si), &trash) == -1) {
1148 si_rx_room_blk(si);
1149 appctx->ctx.cli.i0 = fd;
1150 ret = 0;
1151 break;
1152 }
1153 }
1154
1155 thread_release();
1156 end:
1157 return ret;
1158}
1159
Willy Tarreaua6026a02020-07-02 09:14:48 +02001160#if defined(DEBUG_MEM_STATS)
1161/* CLI parser for the "debug dev memstats" command */
1162static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1163{
1164 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1165 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1166
1167 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1168 return 1;
1169
1170 if (strcmp(args[3], "reset") == 0) {
1171 struct mem_stats *ptr;
1172
1173 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1174 return 1;
1175
1176 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1177 _HA_ATOMIC_STORE(&ptr->calls, 0);
1178 _HA_ATOMIC_STORE(&ptr->size, 0);
1179 }
1180 return 1;
1181 }
1182
1183 if (strcmp(args[3], "all") == 0)
1184 appctx->ctx.cli.i0 = 1;
1185
1186 /* otherwise proceed with the dump from p0 to p1 */
1187 appctx->ctx.cli.p0 = &__start_mem_stats;
1188 appctx->ctx.cli.p1 = &__stop_mem_stats;
1189 return 0;
1190}
1191
1192/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
1193 * structs referenced by pointers located between p0 and p1. Dumps all entries
1194 * if i0 > 0, otherwise only non-zero calls.
1195 */
1196static int debug_iohandler_memstats(struct appctx *appctx)
1197{
Christopher Faulet86e1c332021-12-20 17:09:39 +01001198 struct stream_interface *si = cs_si(appctx->owner);
Willy Tarreaua6026a02020-07-02 09:14:48 +02001199 struct mem_stats *ptr = appctx->ctx.cli.p0;
1200 int ret = 1;
1201
1202 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1203 goto end;
1204
1205 chunk_reset(&trash);
1206
1207 /* we have two inner loops here, one for the proxy, the other one for
1208 * the buffer.
1209 */
1210 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1211 const char *type;
1212 const char *name;
1213 const char *p;
1214
1215 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1216 continue;
1217
1218 /* basename only */
1219 for (p = name = ptr->file; *p; p++) {
1220 if (*p == '/')
1221 name = p + 1;
1222 }
1223
1224 switch (ptr->type) {
1225 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1226 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1227 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1228 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1229 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1230 default: type = "UNSET"; break;
1231 }
1232
1233 //chunk_printf(&trash,
1234 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1235 // name, ptr->line, type,
1236 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1237 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1238
1239 chunk_printf(&trash, "%s:%d", name, ptr->line);
1240 while (trash.data < 25)
1241 trash.area[trash.data++] = ' ';
1242 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1243 type,
1244 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1245 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1246
1247 if (ci_putchk(si_ic(si), &trash) == -1) {
1248 si_rx_room_blk(si);
1249 appctx->ctx.cli.p0 = ptr;
1250 ret = 0;
1251 break;
1252 }
1253 }
1254
1255 end:
1256 return ret;
1257}
1258
1259#endif
1260
Willy Tarreauc7091d82019-05-17 10:08:49 +02001261#ifndef USE_THREAD_DUMP
1262
1263/* This function dumps all threads' state to the trash. This version is the
1264 * most basic one, which doesn't inspect other threads.
1265 */
1266void ha_thread_dump_all_to_trash()
1267{
1268 unsigned int thr;
1269
1270 for (thr = 0; thr < global.nbthread; thr++)
1271 ha_thread_dump(&trash, thr, tid);
1272}
1273
1274#else /* below USE_THREAD_DUMP is set */
1275
Willy Tarreauc7091d82019-05-17 10:08:49 +02001276/* ID of the thread requesting the dump */
1277static unsigned int thread_dump_tid;
1278
1279/* points to the buffer where the dump functions should write. It must
1280 * have already been initialized by the requester. Nothing is done if
1281 * it's NULL.
1282 */
1283struct buffer *thread_dump_buffer = NULL;
1284
1285void ha_thread_dump_all_to_trash()
1286{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001287 unsigned long old;
1288
1289 while (1) {
1290 old = 0;
1291 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1292 break;
1293 ha_thread_relax();
1294 }
1295
1296 thread_dump_buffer = &trash;
1297 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001298 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001299}
1300
1301/* handles DEBUGSIG to dump the state of the thread it's working on */
1302void debug_handler(int sig, siginfo_t *si, void *arg)
1303{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001304 /* first, let's check it's really for us and that we didn't just get
1305 * a spurious DEBUGSIG.
1306 */
1307 if (!(threads_to_dump & tid_bit))
1308 return;
1309
Willy Tarreauc7091d82019-05-17 10:08:49 +02001310 /* There are 4 phases in the dump process:
1311 * 1- wait for our turn, i.e. when all lower bits are gone.
1312 * 2- perform the action if our bit is set
1313 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001314 * the last one and have to put them all as a signal
1315 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001316 */
1317
1318 /* wait for all previous threads to finish first */
1319 while (threads_to_dump & (tid_bit - 1))
1320 ha_thread_relax();
1321
1322 /* dump if needed */
1323 if (threads_to_dump & tid_bit) {
1324 if (thread_dump_buffer)
1325 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1326 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1327 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001328 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001329 thread_dump_buffer = NULL;
1330 }
1331 else
1332 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1333 }
1334
1335 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001336 * bits again to broadcast the leaving condition so we'll see ourselves
1337 * present again. This way the threads_to_dump variable never passes to
1338 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001339 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001340 while (!(threads_to_dump & tid_bit))
1341 ha_thread_relax();
1342 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001343
1344 /* mark the current thread as stuck to detect it upon next invocation
1345 * if it didn't move.
1346 */
1347 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
Willy Tarreaua0b99532021-09-30 18:48:37 +02001348 th_ctx->flags |= TH_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001349}
1350
1351static int init_debug_per_thread()
1352{
1353 sigset_t set;
1354
1355 /* unblock the DEBUGSIG signal we intend to use */
1356 sigemptyset(&set);
1357 sigaddset(&set, DEBUGSIG);
1358 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1359 return 1;
1360}
1361
1362static int init_debug()
1363{
1364 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001365 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001366
Willy Tarreau0214b452020-03-04 06:01:40 +01001367 /* calling backtrace() will access libgcc at runtime. We don't want to
1368 * do it after the chroot, so let's perform a first call to have it
1369 * ready in memory for later use.
1370 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001371 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001372 sa.sa_handler = NULL;
1373 sa.sa_sigaction = debug_handler;
1374 sigemptyset(&sa.sa_mask);
1375 sa.sa_flags = SA_SIGINFO;
1376 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001377 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001378}
1379
1380REGISTER_POST_CHECK(init_debug);
1381REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1382
1383#endif /* USE_THREAD_DUMP */
1384
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001385/* register cli keywords */
1386static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001387 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1388 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1389 {{ "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 +02001390#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001391 {{ "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 +02001392#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001393 {{ "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 +02001394 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1395 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1396 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1397 {{ "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 +02001398#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001399 {{ "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 +02001400#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001401 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1402 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1403 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1404 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1405 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau305cfbd2022-02-25 08:52:39 +01001406 {{ "debug", "dev", "warn", NULL }, "debug dev warn : call WARN_ON() and possibly crash", debug_parse_cli_warn, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001407 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6ab7b212021-12-28 09:57:10 +01001408#if defined(HA_HAVE_DUMP_LIBS)
1409 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1410#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001411 {{ "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 +02001412 {{},}
1413}};
1414
1415INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);