blob: 6582381e5b63369c45bf1f0b89623e6a026fc53d [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 Tarreau9b013702019-10-24 18:18:02 +020049unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020050
Willy Tarreau123fc972021-01-22 13:52:41 +010051/* dumps a backtrace of the current thread that is appended to buffer <buf>.
52 * Lines are prefixed with the string <prefix> which may be empty (used for
53 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010054 * the function does not appear in the call stack. The <dump> argument
55 * indicates what dump state to start from, and should usually be zero. It
56 * may be among the following values:
57 * - 0: search usual callers before step 1, or directly jump to 2
58 * - 1: skip usual callers before step 2
59 * - 2: dump until polling loop, scheduler, or main() (excluded)
60 * - 3: end
61 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010062 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010063void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010064{
65 struct buffer bak;
66 char pfx2[100];
67 void *callers[100];
68 int j, nptrs;
69 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010070
71 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
72 if (!nptrs)
73 return;
74
75 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
76 pfx2[0] = 0;
77
78 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
79 * produce similar output to the following:
80 */
81 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010082 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
83 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010084 /* we failed to spot the starting point of the
85 * dump, let's start over dumping everything we
86 * have.
87 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010088 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010089 j = 0;
90 }
91 bak = *buf;
92 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
93 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010094 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010095 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010096 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +010097 */
Willy Tarreaua8459b22021-01-22 14:12:27 +010098 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
99 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100100 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100101 *buf = bak;
102 continue;
103 }
104
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100105 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100106 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100107 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
108 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100109 *buf = bak;
110 continue;
111 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100112 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100113 }
114
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100115 if ((dump & 3) == 2) {
116 /* still dumping */
117 if (dump == 6) {
118 /* we only stop *after* main and we must send the LF */
119 if (addr == main) {
120 j = nptrs;
121 dump++;
122 }
123 }
124 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
125 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100126 *buf = bak;
127 break;
128 }
129 }
130 /* OK, line dumped */
131 chunk_appendf(buf, "\n");
132 }
133}
134
Willy Tarreaua8459b22021-01-22 14:12:27 +0100135/* dump a backtrace of current thread's stack to stderr. */
136void ha_backtrace_to_stderr()
137{
138 char area[2048];
139 struct buffer b = b_make(area, sizeof(area), 0, 0);
140
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100141 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100142 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100143 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100144}
145
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200146/* Dumps to the buffer some known information for the desired thread, and
147 * optionally extra info for the current thread. The dump will be appended to
148 * the buffer, so the caller is responsible for preliminary initializing it.
149 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200150 * in front of the calling thread's line (usually it's tid). Any stuck thread
151 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200152 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200153void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200154{
155 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +0100156 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
157 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
158 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200159
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200160 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200161 "%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 +0200162 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200163 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200164 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200165 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200166 !!(global_tasks_mask & thr_bit),
167 !eb_is_empty(&task_per_thread[thr].timers),
168 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +0100169 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
170 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
171 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
172 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100173 task_per_thread[thr].tasks_in_list,
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100174 task_per_thread[thr].rq_total,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200175 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176 !!(task_profiling_mask & thr_bit));
177
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200178 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200179 " harmless=%d wantrdv=%d",
180 !!(threads_harmless_mask & thr_bit),
181 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200182
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200183 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200184 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200185
186 /* this is the end of what we can dump from outside the thread */
187
188 if (thr != tid)
189 return;
190
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200191 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200192 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100193
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100194 if (stuck) {
195 /* We only emit the backtrace for stuck threads in order not to
196 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100197 * Please leave this as the last instruction in this function
198 * so that the compiler uses tail merging and the current
199 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100200 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100201 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100202 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200203}
204
205
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200206/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200207 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200208 * with <pfx>. The buffer is only appended and the first output starts by the
209 * pointer itself. The caller is responsible for making sure the task is not
210 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200211 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200212void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200213{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200214 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200215 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200216 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200217
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200218 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200219 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200220 return;
221 }
222
Willy Tarreau20db9112019-05-17 14:14:35 +0200223 if (TASK_IS_TASKLET(task))
224 chunk_appendf(buf,
225 "%p (tasklet) calls=%u\n",
226 task,
227 task->calls);
228 else
229 chunk_appendf(buf,
230 "%p (task) calls=%u last=%llu%s\n",
231 task,
232 task->calls,
233 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
234 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200235
Willy Tarreau2e89b092020-03-03 17:13:02 +0100236 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
237 resolve_sym_name(buf, NULL, task->process);
238 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200239
Willy Tarreaua512b022019-08-21 14:12:19 +0200240 if (task->process == task_run_applet && (appctx = task->context))
241 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
242 else
243 chunk_appendf(buf, "\n");
244
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200245 if (task->process == process_stream && task->context)
246 s = (struct stream *)task->context;
247 else if (task->process == task_run_applet && task->context)
248 s = si_strm(((struct appctx *)task->context)->owner);
249 else if (task->process == si_cs_io_cb && task->context)
250 s = si_strm((struct stream_interface *)task->context);
251
252 if (s)
253 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200254
255#ifdef USE_LUA
256 hlua = NULL;
257 if (s && (hlua = s->hlua)) {
258 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
259 }
260 else if (task->process == hlua_process_task && (hlua = task->context)) {
261 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
262 }
263 else if (task->process == task_run_applet && (appctx = task->context) &&
264 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
265 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
266 }
267 else if (task->process == task_run_applet && (appctx = task->context) &&
268 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
269 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
270 }
271
Christopher Faulet471425f2020-07-24 19:08:05 +0200272 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100273 chunk_appendf(buf, "stack traceback:\n ");
274 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200275 }
Willy Tarreaub50ae912023-05-04 16:28:30 +0200276
277 /* we may need to terminate the current line */
278 if (*b_peek(buf, b_data(buf)-1) != '\n')
Christopher Faulet471425f2020-07-24 19:08:05 +0200279 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200280#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200281}
282
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200283/* This function dumps all profiling settings. It returns 0 if the output
284 * buffer is full and it needs to be called again, otherwise non-zero.
285 */
286static int cli_io_handler_show_threads(struct appctx *appctx)
287{
288 struct stream_interface *si = appctx->owner;
289 int thr;
290
291 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
292 return 1;
293
294 if (appctx->st0)
295 thr = appctx->st1;
296 else
297 thr = 0;
298
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200299 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200300 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200301
302 if (ci_putchk(si_ic(si), &trash) == -1) {
303 /* failed, try again */
304 si_rx_room_blk(si);
305 appctx->st1 = thr;
306 return 0;
307 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200308 return 1;
309}
310
Willy Tarreau37857332021-12-28 09:57:10 +0100311#if defined(HA_HAVE_DUMP_LIBS)
312/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
313static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
314{
315 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
316 return 1;
317
318 chunk_reset(&trash);
319 if (dump_libs(&trash, 1))
320 return cli_msg(appctx, LOG_INFO, trash.area);
321 else
322 return 0;
323}
324#endif
325
Willy Tarreau56131ca2019-05-20 13:48:29 +0200326/* dumps a state of all threads into the trash and on fd #2, then aborts. */
327void ha_panic()
328{
329 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200330 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200331 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100332 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200333 for (;;)
334 abort();
335}
336
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200337/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
338static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
339{
340 int code = atoi(args[3]);
341
342 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
343 return 1;
344
Willy Tarreau4781b152021-04-06 13:53:36 +0200345 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200346 exit(code);
347 return 1;
348}
349
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100350/* parse a "debug dev bug" command. It always returns 1, though it should never return.
351 * Note: we make sure not to make the function static so that it appears in the trace.
352 */
353int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
354{
355 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
356 return 1;
357
Willy Tarreau4781b152021-04-06 13:53:36 +0200358 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100359 BUG_ON(one > zero);
360 return 1;
361}
362
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363/* parse a "debug dev close" command. It always returns 1. */
364static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
365{
366 int fd;
367
368 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
369 return 1;
370
Willy Tarreau9d008692019-08-09 11:21:01 +0200371 if (!*args[3])
372 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200373
374 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200375 if (fd < 0 || fd >= global.maxsock)
376 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377
Willy Tarreau9d008692019-08-09 11:21:01 +0200378 if (!fdtab[fd].owner)
379 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200380
Willy Tarreau4781b152021-04-06 13:53:36 +0200381 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200382 fd_delete(fd);
383 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200384}
385
386/* parse a "debug dev delay" command. It always returns 1. */
387static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
388{
389 int delay = atoi(args[3]);
390
391 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
392 return 1;
393
Willy Tarreau4781b152021-04-06 13:53:36 +0200394 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200395 usleep((long)delay * 1000);
396 return 1;
397}
398
399/* parse a "debug dev log" command. It always returns 1. */
400static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
401{
402 int arg;
403
404 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
405 return 1;
406
Willy Tarreau4781b152021-04-06 13:53:36 +0200407 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200408 chunk_reset(&trash);
409 for (arg = 3; *args[arg]; arg++) {
410 if (arg > 3)
411 chunk_strcat(&trash, " ");
412 chunk_strcat(&trash, args[arg]);
413 }
414
415 send_log(NULL, LOG_INFO, "%s\n", trash.area);
416 return 1;
417}
418
419/* parse a "debug dev loop" command. It always returns 1. */
420static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
421{
422 struct timeval deadline, curr;
423 int loop = atoi(args[3]);
424
425 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
426 return 1;
427
Willy Tarreau4781b152021-04-06 13:53:36 +0200428 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200429 gettimeofday(&curr, NULL);
430 tv_ms_add(&deadline, &curr, loop);
431
432 while (tv_ms_cmp(&curr, &deadline) < 0)
433 gettimeofday(&curr, NULL);
434
435 return 1;
436}
437
438/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
439static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
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 ha_panic();
446 return 1;
447}
448
449/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200450#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200451static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
452{
Willy Tarreau368bff42019-12-06 17:18:28 +0100453 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200454 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100455 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200456
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 chunk_reset(&trash);
462 for (arg = 3; *args[arg]; arg++) {
463 if (arg > 3)
464 chunk_strcat(&trash, " ");
465 chunk_strcat(&trash, args[arg]);
466 }
467
Willy Tarreau368bff42019-12-06 17:18:28 +0100468 thread_isolate();
469 if (pipe(pipefd) < 0)
470 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200471
Willy Tarreau368bff42019-12-06 17:18:28 +0100472 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
473 goto fail_fcntl;
474
475 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
476 goto fail_fcntl;
477
478 pid = fork();
479
480 if (pid < 0)
481 goto fail_fork;
482 else if (pid == 0) {
483 /* child */
484 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
485
486 close(0);
487 dup2(pipefd[1], 1);
488 dup2(pipefd[1], 2);
489
490 cmd[2] = trash.area;
491 execvp(cmd[0], cmd);
492 printf("execvp() failed\n");
493 exit(1);
494 }
495
496 /* parent */
497 thread_release();
498 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200499 chunk_reset(&trash);
500 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100501 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
502 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200503 break;
504 trash.data += ret;
505 if (trash.data + 20 == trash.size) {
506 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
507 break;
508 }
509 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100510 close(pipefd[0]);
511 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200512 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200513 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100514
515 fail_fork:
516 fail_fcntl:
517 close(pipefd[0]);
518 close(pipefd[1]);
519 fail_pipe:
520 thread_release();
521 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200522}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200523#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200524
525/* parse a "debug dev hex" command. It always returns 1. */
526static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
527{
528 unsigned long start, len;
529
530 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
531 return 1;
532
Willy Tarreau9d008692019-08-09 11:21:01 +0200533 if (!*args[3])
534 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200535
536 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200537 if (!start)
538 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200539
Willy Tarreau4781b152021-04-06 13:53:36 +0200540 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200541
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200542 /* by default, dump ~128 till next block of 16 */
543 len = strtoul(args[4], NULL, 0);
544 if (!len)
545 len = ((start + 128) & -16) - start;
546
547 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200548 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200549 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200550 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200551}
552
Willy Tarreau48129be2021-05-04 18:40:50 +0200553/* parse a "debug dev sym <addr>" command. It always returns 1. */
554static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
555{
556 unsigned long addr;
557
558 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
559 return 1;
560
561 if (!*args[3])
562 return cli_err(appctx, "Missing memory address to be resolved.\n");
563
564 _HA_ATOMIC_INC(&debug_commands_issued);
565
566 addr = strtoul(args[3], NULL, 0);
567 chunk_printf(&trash, "%#lx resolves to ", addr);
568 resolve_sym_name(&trash, NULL, (const void *)addr);
569 chunk_appendf(&trash, "\n");
570
571 return cli_msg(appctx, LOG_INFO, trash.area);
572}
573
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200574/* parse a "debug dev tkill" command. It always returns 1. */
575static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
576{
577 int thr = 0;
578 int sig = SIGABRT;
579
580 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
581 return 1;
582
583 if (*args[3])
584 thr = atoi(args[3]);
585
Willy Tarreau9d008692019-08-09 11:21:01 +0200586 if (thr < 0 || thr > global.nbthread)
587 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200588
589 if (*args[4])
590 sig = atoi(args[4]);
591
Willy Tarreau4781b152021-04-06 13:53:36 +0200592 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200593 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200594 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200595 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200596 raise(sig);
597 return 1;
598}
599
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100600/* parse a "debug dev write" command. It always returns 1. */
601static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
602{
603 unsigned long len;
604
605 if (!*args[3])
606 return cli_err(appctx, "Missing output size.\n");
607
608 len = strtoul(args[3], NULL, 0);
609 if (len >= trash.size)
610 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
611
Willy Tarreau4781b152021-04-06 13:53:36 +0200612 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100613
614 chunk_reset(&trash);
615 trash.data = len;
616 memset(trash.area, '.', trash.data);
617 trash.area[trash.data] = 0;
618 for (len = 64; len < trash.data; len += 64)
619 trash.area[len] = '\n';
620 return cli_msg(appctx, LOG_INFO, trash.area);
621}
622
Willy Tarreau68680bb2019-10-23 17:23:25 +0200623/* parse a "debug dev stream" command */
624/*
625 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
626 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
627 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
628 * [sif.s[=<state>]] [sib.s[=<state>]]
629 */
630static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
631{
632 struct stream *s = si_strm(appctx->owner);
633 int arg;
634 void *ptr;
635 int size;
636 const char *word, *end;
637 struct ist name;
638 char *msg = NULL;
639 char *endarg;
640 unsigned long long old, new;
641
642 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
643 return 1;
644
645 ptr = NULL; size = 0;
646
647 if (!*args[3]) {
648 return cli_err(appctx,
649 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
650 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
651 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
652 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
653 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
654 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
655 );
656 }
657
Willy Tarreau4781b152021-04-06 13:53:36 +0200658 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200659 for (arg = 3; *args[arg]; arg++) {
660 old = 0;
661 end = word = args[arg];
662 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
663 end++;
664 name = ist2(word, end - word);
665 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200666 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200667 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200668 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200669 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200670 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200671 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200672 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200673 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200674 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200675 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200676 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200677 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200678 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200679 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200680 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200681 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200682 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200683 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200684 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200685 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200686 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200687 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200688 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200689 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200690 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200691 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200692 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200693 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200694 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200695 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200696 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200697 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
698 continue;
699 } else
700 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
701
702 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200703 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200704 if (size == 8)
705 old = read_u64(ptr);
706 else if (size == 4)
707 old = read_u32(ptr);
708 else if (size == 2)
709 old = read_u16(ptr);
710 else
711 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200712 } else {
713 memprintf(&msg,
714 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
715 msg ? msg : "", ptr, (int)(end - word), word);
716 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200717 }
718
719 /* parse the new value . */
720 new = strtoll(end + 1, &endarg, 0);
721 if (end[1] && *endarg) {
722 if (strcmp(end + 1, "now") == 0)
723 new = now_ms;
724 else {
725 memprintf(&msg,
726 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
727 msg ? msg : "", end + 1, (int)(end - word), word);
728 continue;
729 }
730 }
731
732 switch (*end) {
733 case '\0': /* show */
734 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
735 new = old; // do not change the value
736 break;
737
738 case '=': /* set */
739 break;
740
741 case '^': /* XOR */
742 new = old ^ new;
743 break;
744
745 case '+': /* OR */
746 new = old | new;
747 break;
748
749 case '-': /* AND NOT */
750 new = old & ~new;
751 break;
752
753 default:
754 break;
755 }
756
757 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200758 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200759 if (size == 8)
760 write_u64(ptr, new);
761 else if (size == 4)
762 write_u32(ptr, new);
763 else if (size == 2)
764 write_u16(ptr, new);
765 else
766 *(uint8_t *)ptr = new;
767 }
768 }
769
770 if (msg && *msg)
771 return cli_dynmsg(appctx, LOG_INFO, msg);
772 return 1;
773}
774
Willy Tarreau144f84a2021-03-02 16:09:26 +0100775static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100776{
777 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
778 unsigned long inter = tctx[1];
779 unsigned long rnd;
780
781 t->expire = tick_add(now_ms, inter);
782
783 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100784 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100785 if (rnd & 1) {
786 rnd >>= 1;
787 rnd %= tctx[0];
788 rnd = tctx[rnd + 2];
789
790 if (rnd & 1)
791 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
792 else
793 tasklet_wakeup((struct tasklet *)rnd);
794 }
795 return t;
796}
797
Willy Tarreau144f84a2021-03-02 16:09:26 +0100798static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100799{
800 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
801 unsigned long rnd;
802 int i;
803
804 /* wake up two random entries */
805 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100806 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100807 rnd = tctx[rnd + 2];
808
809 if (rnd & 1)
810 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
811 else
812 tasklet_wakeup((struct tasklet *)rnd);
813 }
814 return t;
815}
816
817/* parse a "debug dev sched" command
818 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
819 */
820static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
821{
822 int arg;
823 void *ptr;
824 int size;
825 const char *word, *end;
826 struct ist name;
827 char *msg = NULL;
828 char *endarg;
829 unsigned long long new;
830 unsigned long count = 0;
831 unsigned long thrid = 0;
832 unsigned int inter = 0;
833 unsigned long mask, tmask;
834 unsigned long i;
835 int mode = 0; // 0 = tasklet; 1 = task
836 int single = 0;
837 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
838
839 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
840 return 1;
841
842 ptr = NULL; size = 0;
843 mask = all_threads_mask;
844
845 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
846 return cli_err(appctx,
847 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
848 " <obj> = {count | mask | inter | single }\n"
849 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
850 );
851 }
852
853 mode = strcmp(args[3], "task") == 0;
854
Willy Tarreau4781b152021-04-06 13:53:36 +0200855 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100856 for (arg = 4; *args[arg]; arg++) {
857 end = word = args[arg];
858 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
859 end++;
860 name = ist2(word, end - word);
861 if (isteq(name, ist("count"))) {
862 ptr = &count; size = sizeof(count);
863 } else if (isteq(name, ist("mask"))) {
864 ptr = &mask; size = sizeof(mask);
865 } else if (isteq(name, ist("tid"))) {
866 ptr = &thrid; size = sizeof(thrid);
867 } else if (isteq(name, ist("inter"))) {
868 ptr = &inter; size = sizeof(inter);
869 } else if (isteq(name, ist("single"))) {
870 ptr = &single; size = sizeof(single);
871 } else
872 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
873
874 /* parse the new value . */
875 new = strtoll(end + 1, &endarg, 0);
876 if (end[1] && *endarg) {
877 memprintf(&msg,
878 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
879 msg ? msg : "", end + 1, (int)(end - word), word);
880 continue;
881 }
882
883 /* write the new value */
884 if (size == 8)
885 write_u64(ptr, new);
886 else if (size == 4)
887 write_u32(ptr, new);
888 else if (size == 2)
889 write_u16(ptr, new);
890 else
891 *(uint8_t *)ptr = new;
892 }
893
894 tctx = calloc(sizeof(*tctx), count + 2);
895 if (!tctx)
896 goto fail;
897
898 tctx[0] = (unsigned long)count;
899 tctx[1] = (unsigned long)inter;
900
901 mask &= all_threads_mask;
902 if (!mask)
903 mask = tid_bit;
904
905 tmask = 0;
906 for (i = 0; i < count; i++) {
907 if (single || mode == 0) {
908 /* look for next bit matching a bit in mask or loop back to zero */
909 for (tmask <<= 1; !(mask & tmask); ) {
910 if (!(mask & -tmask))
911 tmask = 1;
912 else
913 tmask <<= 1;
914 }
915 } else {
916 /* multi-threaded task */
917 tmask = mask;
918 }
919
920 /* now, if poly or mask was set, tmask corresponds to the
921 * valid thread mask to use, otherwise it remains zero.
922 */
923 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
924 if (mode == 0) {
925 struct tasklet *tl = tasklet_new();
926
927 if (!tl)
928 goto fail;
929
930 if (tmask)
931 tl->tid = my_ffsl(tmask) - 1;
932 tl->process = debug_tasklet_handler;
933 tl->context = tctx;
934 tctx[i + 2] = (unsigned long)tl;
935 } else {
936 struct task *task = task_new(tmask ? tmask : tid_bit);
937
938 if (!task)
939 goto fail;
940
941 task->process = debug_task_handler;
942 task->context = tctx;
943 tctx[i + 2] = (unsigned long)task + 1;
944 }
945 }
946
947 /* start the tasks and tasklets */
948 for (i = 0; i < count; i++) {
949 unsigned long ctx = tctx[i + 2];
950
951 if (ctx & 1)
952 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
953 else
954 tasklet_wakeup((struct tasklet *)ctx);
955 }
956
957 if (msg && *msg)
958 return cli_dynmsg(appctx, LOG_INFO, msg);
959 return 1;
960
961 fail:
962 /* free partially allocated entries */
963 for (i = 0; tctx && i < count; i++) {
964 unsigned long ctx = tctx[i + 2];
965
966 if (!ctx)
967 break;
968
969 if (ctx & 1)
970 task_destroy((struct task *)(ctx - 1));
971 else
972 tasklet_free((struct tasklet *)ctx);
973 }
974
975 free(tctx);
976 return cli_err(appctx, "Not enough memory");
977}
978
Willy Tarreau476252b2022-01-24 20:26:09 +0100979/* CLI parser for the "debug dev fd" command. The current FD to restart from is
980 * stored in i0.
981 */
982static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
983{
984 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
985 return 1;
986
987 /* start at fd #0 */
988 appctx->ctx.cli.i0 = 0;
989 return 0;
990}
991
992/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
993 * accessible from the process but not known from fdtab. The FD number to
994 * restart from is stored in i0.
995 */
996static int debug_iohandler_fd(struct appctx *appctx)
997{
998 struct stream_interface *si = appctx->owner;
999 struct sockaddr_storage sa;
1000 struct stat statbuf;
1001 socklen_t salen, vlen;
1002 int ret1, ret2, port;
1003 char *addrstr;
1004 int ret = 1;
1005 int i, fd;
1006
1007 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1008 goto end;
1009
1010 chunk_reset(&trash);
1011
1012 thread_isolate();
1013
1014 /* we have two inner loops here, one for the proxy, the other one for
1015 * the buffer.
1016 */
1017 for (fd = appctx->ctx.cli.i0; fd < global.maxsock; fd++) {
1018 /* check for FD's existence */
1019 ret1 = fcntl(fd, F_GETFD, 0);
1020 if (ret1 == -1)
1021 continue; // not known to the process
1022 if (fdtab[fd].owner)
1023 continue; // well-known
1024
1025 /* OK we're seeing an orphan let's try to retrieve as much
1026 * information as possible about it.
1027 */
1028 chunk_printf(&trash, "%5d", fd);
1029
1030 if (fstat(fd, &statbuf) != -1) {
1031 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1032 isatty(fd) ? "tty.":
1033 S_ISREG(statbuf.st_mode) ? "file":
1034 S_ISDIR(statbuf.st_mode) ? "dir.":
1035 S_ISCHR(statbuf.st_mode) ? "chr.":
1036 S_ISBLK(statbuf.st_mode) ? "blk.":
1037 S_ISFIFO(statbuf.st_mode) ? "pipe":
1038 S_ISLNK(statbuf.st_mode) ? "link":
1039 S_ISSOCK(statbuf.st_mode) ? "sock":
1040#ifdef USE_EPOLL
Willy Tarreaub367a672023-07-02 10:49:49 +02001041 /* trick: epoll_ctl() will return -ENOENT when trying
1042 * to remove from a valid epoll FD an FD that was not
1043 * registered against it. But we don't want to risk
1044 * disabling a random FD. Instead we'll create a new
1045 * one by duplicating 0 (it should be valid since
1046 * pointing to a terminal or /dev/null), and try to
1047 * remove it.
1048 */
1049 ({
1050 int fd2 = dup(0);
1051 int ret = fd2;
1052 if (ret >= 0) {
1053 ret = epoll_ctl(fd, EPOLL_CTL_DEL, fd2, NULL);
1054 if (ret == -1 && errno == ENOENT)
1055 ret = 0; // that's a real epoll
1056 else
1057 ret = -1; // it's something else
1058 close(fd2);
1059 }
1060 ret;
1061 }) == 0 ? "epol" :
Willy Tarreau476252b2022-01-24 20:26:09 +01001062#endif
1063 "????",
1064 (uint)statbuf.st_mode & 07777,
1065
1066 (ullong)statbuf.st_rdev,
1067 (ullong)statbuf.st_size,
1068 (ullong)statbuf.st_uid,
1069 (ullong)statbuf.st_gid,
1070
1071 (ullong)statbuf.st_dev,
1072 (ullong)statbuf.st_ino);
1073 }
1074
1075 chunk_appendf(&trash, " getfd=%s+%#x",
1076 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1077 ret1 &~ FD_CLOEXEC);
1078
1079 /* FD options */
1080 ret2 = fcntl(fd, F_GETFL, 0);
1081 if (ret2) {
1082 chunk_appendf(&trash, " getfl=%s",
1083 (ret1 & 3) >= 2 ? "O_RDWR" :
1084 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1085
1086 for (i = 2; i < 32; i++) {
1087 if (!(ret2 & (1UL << i)))
1088 continue;
1089 switch (1UL << i) {
1090 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1091 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1092 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1093 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1094 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreauc899f0f2022-01-25 14:51:53 +01001095#ifdef O_ASYNC
Willy Tarreau476252b2022-01-24 20:26:09 +01001096 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreauc899f0f2022-01-25 14:51:53 +01001097#endif
Willy Tarreau476252b2022-01-24 20:26:09 +01001098#ifdef O_DIRECT
1099 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1100#endif
1101#ifdef O_NOATIME
1102 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1103#endif
1104 }
1105 }
1106 }
1107
1108 vlen = sizeof(ret2);
1109 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1110 if (ret1 != -1)
1111 chunk_appendf(&trash, " so_type=%d", ret2);
1112
1113 vlen = sizeof(ret2);
1114 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1115 if (ret1 != -1)
1116 chunk_appendf(&trash, " so_accept=%d", ret2);
1117
1118 vlen = sizeof(ret2);
1119 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1120 if (ret1 != -1)
1121 chunk_appendf(&trash, " so_error=%d", ret2);
1122
1123 salen = sizeof(sa);
1124 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1125 if (sa.ss_family == AF_INET)
1126 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1127 else if (sa.ss_family == AF_INET6)
1128 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1129 else
1130 port = 0;
1131 addrstr = sa2str(&sa, port, 0);
1132 chunk_appendf(&trash, " laddr=%s", addrstr);
1133 free(addrstr);
1134 }
1135
1136 salen = sizeof(sa);
1137 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1138 if (sa.ss_family == AF_INET)
1139 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1140 else if (sa.ss_family == AF_INET6)
1141 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1142 else
1143 port = 0;
1144 addrstr = sa2str(&sa, port, 0);
1145 chunk_appendf(&trash, " raddr=%s", addrstr);
1146 free(addrstr);
1147 }
1148
1149 chunk_appendf(&trash, "\n");
1150
1151 if (ci_putchk(si_ic(si), &trash) == -1) {
1152 si_rx_room_blk(si);
1153 appctx->ctx.cli.i0 = fd;
1154 ret = 0;
1155 break;
1156 }
1157 }
1158
1159 thread_release();
1160 end:
1161 return ret;
1162}
1163
Willy Tarreaua6026a02020-07-02 09:14:48 +02001164#if defined(DEBUG_MEM_STATS)
1165/* CLI parser for the "debug dev memstats" command */
1166static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1167{
1168 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1169 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1170
1171 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1172 return 1;
1173
1174 if (strcmp(args[3], "reset") == 0) {
1175 struct mem_stats *ptr;
1176
1177 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1178 return 1;
1179
1180 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1181 _HA_ATOMIC_STORE(&ptr->calls, 0);
1182 _HA_ATOMIC_STORE(&ptr->size, 0);
1183 }
1184 return 1;
1185 }
1186
1187 if (strcmp(args[3], "all") == 0)
1188 appctx->ctx.cli.i0 = 1;
1189
1190 /* otherwise proceed with the dump from p0 to p1 */
1191 appctx->ctx.cli.p0 = &__start_mem_stats;
1192 appctx->ctx.cli.p1 = &__stop_mem_stats;
1193 return 0;
1194}
1195
1196/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
1197 * structs referenced by pointers located between p0 and p1. Dumps all entries
1198 * if i0 > 0, otherwise only non-zero calls.
1199 */
1200static int debug_iohandler_memstats(struct appctx *appctx)
1201{
1202 struct stream_interface *si = appctx->owner;
1203 struct mem_stats *ptr = appctx->ctx.cli.p0;
1204 int ret = 1;
1205
1206 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1207 goto end;
1208
1209 chunk_reset(&trash);
1210
1211 /* we have two inner loops here, one for the proxy, the other one for
1212 * the buffer.
1213 */
1214 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1215 const char *type;
1216 const char *name;
1217 const char *p;
1218
1219 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1220 continue;
1221
1222 /* basename only */
1223 for (p = name = ptr->file; *p; p++) {
1224 if (*p == '/')
1225 name = p + 1;
1226 }
1227
1228 switch (ptr->type) {
1229 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1230 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1231 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1232 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1233 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1234 default: type = "UNSET"; break;
1235 }
1236
1237 //chunk_printf(&trash,
1238 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1239 // name, ptr->line, type,
1240 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1241 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1242
1243 chunk_printf(&trash, "%s:%d", name, ptr->line);
1244 while (trash.data < 25)
1245 trash.area[trash.data++] = ' ';
1246 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1247 type,
1248 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1249 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1250
1251 if (ci_putchk(si_ic(si), &trash) == -1) {
1252 si_rx_room_blk(si);
1253 appctx->ctx.cli.p0 = ptr;
1254 ret = 0;
1255 break;
1256 }
1257 }
1258
1259 end:
1260 return ret;
1261}
1262
1263#endif
1264
Willy Tarreauc7091d82019-05-17 10:08:49 +02001265#ifndef USE_THREAD_DUMP
1266
1267/* This function dumps all threads' state to the trash. This version is the
1268 * most basic one, which doesn't inspect other threads.
1269 */
1270void ha_thread_dump_all_to_trash()
1271{
1272 unsigned int thr;
1273
1274 for (thr = 0; thr < global.nbthread; thr++)
1275 ha_thread_dump(&trash, thr, tid);
1276}
1277
1278#else /* below USE_THREAD_DUMP is set */
1279
Willy Tarreauc7091d82019-05-17 10:08:49 +02001280/* ID of the thread requesting the dump */
1281static unsigned int thread_dump_tid;
1282
1283/* points to the buffer where the dump functions should write. It must
1284 * have already been initialized by the requester. Nothing is done if
1285 * it's NULL.
1286 */
1287struct buffer *thread_dump_buffer = NULL;
1288
1289void ha_thread_dump_all_to_trash()
1290{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001291 unsigned long old;
1292
1293 while (1) {
1294 old = 0;
1295 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1296 break;
1297 ha_thread_relax();
1298 }
1299
1300 thread_dump_buffer = &trash;
1301 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001302 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001303}
1304
1305/* handles DEBUGSIG to dump the state of the thread it's working on */
1306void debug_handler(int sig, siginfo_t *si, void *arg)
1307{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001308 /* first, let's check it's really for us and that we didn't just get
1309 * a spurious DEBUGSIG.
1310 */
1311 if (!(threads_to_dump & tid_bit))
1312 return;
1313
Willy Tarreauc7091d82019-05-17 10:08:49 +02001314 /* There are 4 phases in the dump process:
1315 * 1- wait for our turn, i.e. when all lower bits are gone.
1316 * 2- perform the action if our bit is set
1317 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001318 * the last one and have to put them all as a signal
1319 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001320 */
1321
1322 /* wait for all previous threads to finish first */
1323 while (threads_to_dump & (tid_bit - 1))
1324 ha_thread_relax();
1325
1326 /* dump if needed */
1327 if (threads_to_dump & tid_bit) {
1328 if (thread_dump_buffer)
1329 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1330 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1331 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001332 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001333 thread_dump_buffer = NULL;
1334 }
1335 else
1336 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1337 }
1338
1339 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001340 * bits again to broadcast the leaving condition so we'll see ourselves
1341 * present again. This way the threads_to_dump variable never passes to
1342 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001343 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001344 while (!(threads_to_dump & tid_bit))
1345 ha_thread_relax();
1346 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001347
1348 /* mark the current thread as stuck to detect it upon next invocation
1349 * if it didn't move.
1350 */
1351 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1352 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001353}
1354
1355static int init_debug_per_thread()
1356{
1357 sigset_t set;
1358
1359 /* unblock the DEBUGSIG signal we intend to use */
1360 sigemptyset(&set);
1361 sigaddset(&set, DEBUGSIG);
1362 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1363 return 1;
1364}
1365
1366static int init_debug()
1367{
1368 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001369 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001370
Willy Tarreau0214b452020-03-04 06:01:40 +01001371 /* calling backtrace() will access libgcc at runtime. We don't want to
1372 * do it after the chroot, so let's perform a first call to have it
1373 * ready in memory for later use.
1374 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001375 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001376 sa.sa_handler = NULL;
1377 sa.sa_sigaction = debug_handler;
1378 sigemptyset(&sa.sa_mask);
1379 sa.sa_flags = SA_SIGINFO;
1380 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001381 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001382}
1383
1384REGISTER_POST_CHECK(init_debug);
1385REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1386
1387#endif /* USE_THREAD_DUMP */
1388
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001389/* register cli keywords */
1390static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001391 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1392 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1393 {{ "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 +02001394#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001395 {{ "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 +02001396#endif
Willy Tarreau476252b2022-01-24 20:26:09 +01001397 {{ "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 +02001398 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1399 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1400 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1401 {{ "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 +02001402#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001403 {{ "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 +02001404#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001405 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1406 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1407 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1408 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1409 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
1410 {{ "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 +01001411#if defined(HA_HAVE_DUMP_LIBS)
1412 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1413#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001414 {{ "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 +02001415 {{},}
1416}};
1417
1418INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);