blob: 285def16c4bb0b585a888ffb05d213bf36220bb0 [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>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020030#include <haproxy/applet.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020031#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010033#include <haproxy/conn_stream.h>
34#include <haproxy/cs_utils.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/fd.h>
38#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020039#include <haproxy/hlua.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020040#include <haproxy/http_ana.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020041#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/net_helper.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020043#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020044#include <haproxy/thread.h>
Willy Tarreau55542642021-10-08 09:33:24 +020045#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020046#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020048
Willy Tarreau4e2b6462019-05-16 17:44:30 +020049
Willy Tarreaua37cb182019-07-31 19:20:39 +020050/* mask of threads still having to dump, used to respect ordering. Only used
51 * when USE_THREAD_DUMP is set.
52 */
53volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020054unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020055
Willy Tarreau123fc972021-01-22 13:52:41 +010056/* dumps a backtrace of the current thread that is appended to buffer <buf>.
57 * Lines are prefixed with the string <prefix> which may be empty (used for
58 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010059 * the function does not appear in the call stack. The <dump> argument
60 * indicates what dump state to start from, and should usually be zero. It
61 * may be among the following values:
62 * - 0: search usual callers before step 1, or directly jump to 2
63 * - 1: skip usual callers before step 2
64 * - 2: dump until polling loop, scheduler, or main() (excluded)
65 * - 3: end
66 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010067 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010068void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010069{
70 struct buffer bak;
71 char pfx2[100];
72 void *callers[100];
73 int j, nptrs;
74 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010075
76 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
77 if (!nptrs)
78 return;
79
80 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
81 pfx2[0] = 0;
82
83 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
84 * produce similar output to the following:
85 */
86 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010087 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
88 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010089 /* we failed to spot the starting point of the
90 * dump, let's start over dumping everything we
91 * have.
92 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010093 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010094 j = 0;
95 }
96 bak = *buf;
97 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
98 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010099 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100100 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +0100101 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +0100102 */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100103 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
104 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100105 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100106 *buf = bak;
107 continue;
108 }
109
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100110 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100111 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100112 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
113 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100114 *buf = bak;
115 continue;
116 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100117 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100118 }
119
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100120 if ((dump & 3) == 2) {
121 /* still dumping */
122 if (dump == 6) {
123 /* we only stop *after* main and we must send the LF */
124 if (addr == main) {
125 j = nptrs;
126 dump++;
127 }
128 }
129 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
130 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100131 *buf = bak;
132 break;
133 }
134 }
135 /* OK, line dumped */
136 chunk_appendf(buf, "\n");
137 }
138}
139
Willy Tarreaua8459b22021-01-22 14:12:27 +0100140/* dump a backtrace of current thread's stack to stderr. */
141void ha_backtrace_to_stderr()
142{
143 char area[2048];
144 struct buffer b = b_make(area, sizeof(area), 0, 0);
145
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100146 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100147 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100148 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100149}
150
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200151/* Dumps to the buffer some known information for the desired thread, and
152 * optionally extra info for the current thread. The dump will be appended to
153 * the buffer, so the caller is responsible for preliminary initializing it.
154 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200155 * in front of the calling thread's line (usually it's tid). Any stuck thread
156 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200157 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200158void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200159{
160 unsigned long thr_bit = 1UL << thr;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200161 unsigned long long p = ha_thread_ctx[thr].prev_cpu_time;
Willy Tarreau21694982021-10-08 15:09:17 +0200162 unsigned long long n = now_cpu_time_thread(thr);
Willy Tarreaua0b99532021-09-30 18:48:37 +0200163 int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200164
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200165 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200166 "%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 +0200167 " %2u/%-2u stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200168 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200169 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200170 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200171 !!(global_tasks_mask & thr_bit),
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200172 !eb_is_empty(&ha_thread_ctx[thr].timers),
173 !eb_is_empty(&ha_thread_ctx[thr].rqueue),
174 !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) &&
175 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) &&
176 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) &&
177 MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)),
178 ha_thread_ctx[thr].tasks_in_list,
179 ha_thread_ctx[thr].rq_total,
Willy Tarreaua3870b72021-09-13 19:24:47 +0200180 ha_thread_info[thr].tg->tgid, ha_thread_info[thr].ltid + 1,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200181 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200182 !!(task_profiling_mask & thr_bit));
183
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200184 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200185 " harmless=%d wantrdv=%d",
186 !!(threads_harmless_mask & thr_bit),
187 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200188
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200189 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200190 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200191
Willy Tarreaua3870b72021-09-13 19:24:47 +0200192 /* this is the end of what we can dump from outside the current thread */
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200193
194 if (thr != tid)
195 return;
196
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200197 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200198 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100199
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100200 if (stuck) {
201 /* We only emit the backtrace for stuck threads in order not to
202 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100203 * Please leave this as the last instruction in this function
204 * so that the compiler uses tail merging and the current
205 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100206 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100207 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100208 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200209}
210
211
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200212/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200213 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200214 * with <pfx>. The buffer is only appended and the first output starts by the
215 * pointer itself. The caller is responsible for making sure the task is not
216 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200217 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200218void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200219{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200220 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200221 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200222 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200223
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200224 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200225 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200226 return;
227 }
228
Willy Tarreau20db9112019-05-17 14:14:35 +0200229 if (TASK_IS_TASKLET(task))
230 chunk_appendf(buf,
231 "%p (tasklet) calls=%u\n",
232 task,
233 task->calls);
234 else
235 chunk_appendf(buf,
236 "%p (task) calls=%u last=%llu%s\n",
237 task,
238 task->calls,
239 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
240 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200241
Willy Tarreau2e89b092020-03-03 17:13:02 +0100242 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
243 resolve_sym_name(buf, NULL, task->process);
244 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200245
Willy Tarreaua512b022019-08-21 14:12:19 +0200246 if (task->process == task_run_applet && (appctx = task->context))
247 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
248 else
249 chunk_appendf(buf, "\n");
250
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200251 if (task->process == process_stream && task->context)
252 s = (struct stream *)task->context;
253 else if (task->process == task_run_applet && task->context)
Christopher Faulet908628c2022-03-25 16:43:49 +0100254 s = cs_strm(((struct appctx *)task->context)->owner);
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200255 else if (task->process == cs_conn_io_cb && task->context)
256 s = cs_strm(((struct conn_stream *)task->context));
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200257
258 if (s)
259 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200260
261#ifdef USE_LUA
262 hlua = NULL;
263 if (s && (hlua = s->hlua)) {
264 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
265 }
266 else if (task->process == hlua_process_task && (hlua = task->context)) {
267 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
268 }
269 else if (task->process == task_run_applet && (appctx = task->context) &&
270 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
271 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
272 }
273 else if (task->process == task_run_applet && (appctx = task->context) &&
274 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
275 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
276 }
277
Christopher Faulet471425f2020-07-24 19:08:05 +0200278 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100279 chunk_appendf(buf, "stack traceback:\n ");
280 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
281 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200282 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200283 else
284 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200285#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200286}
287
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200288/* This function dumps all profiling settings. It returns 0 if the output
289 * buffer is full and it needs to be called again, otherwise non-zero.
290 */
291static int cli_io_handler_show_threads(struct appctx *appctx)
292{
Christopher Faulet908628c2022-03-25 16:43:49 +0100293 struct conn_stream *cs = appctx->owner;
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200294 int thr;
295
Christopher Faulet908628c2022-03-25 16:43:49 +0100296 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200297 return 1;
298
299 if (appctx->st0)
300 thr = appctx->st1;
301 else
302 thr = 0;
303
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200304 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200305 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200306
Christopher Faulet908628c2022-03-25 16:43:49 +0100307 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200308 /* failed, try again */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200309 cs_rx_room_blk(cs);
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200310 appctx->st1 = thr;
311 return 0;
312 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200313 return 1;
314}
315
Willy Tarreau6ab7b212021-12-28 09:57:10 +0100316#if defined(HA_HAVE_DUMP_LIBS)
317/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
318static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
319{
320 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
321 return 1;
322
323 chunk_reset(&trash);
324 if (dump_libs(&trash, 1))
325 return cli_msg(appctx, LOG_INFO, trash.area);
326 else
327 return 0;
328}
329#endif
330
Willy Tarreau56131ca2019-05-20 13:48:29 +0200331/* dumps a state of all threads into the trash and on fd #2, then aborts. */
332void ha_panic()
333{
334 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200335 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200336 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100337 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200338 for (;;)
339 abort();
340}
341
Willy Tarreau06e66c82022-03-02 15:52:03 +0100342/* Complain with message <msg> on stderr. If <counter> is not NULL, it is
343 * atomically incremented, and the message is only printed when the counter
344 * was zero, so that the message is only printed once. <taint> is only checked
345 * on bit 1, and will taint the process either for a bug (2) or warn (0).
346 */
347void complain(int *counter, const char *msg, int taint)
348{
349 if (counter && _HA_ATOMIC_FETCH_ADD(counter, 1))
350 return;
351 DISGUISE(write(2, msg, strlen(msg)));
352 if (taint & 2)
353 mark_tainted(TAINTED_BUG);
354 else
355 mark_tainted(TAINTED_WARN);
356}
357
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200358/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
359static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
360{
361 int code = atoi(args[3]);
362
363 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
364 return 1;
365
Willy Tarreau4781b152021-04-06 13:53:36 +0200366 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200367 exit(code);
368 return 1;
369}
370
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100371/* parse a "debug dev bug" command. It always returns 1, though it should never return.
372 * Note: we make sure not to make the function static so that it appears in the trace.
373 */
374int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
375{
376 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
377 return 1;
378
Willy Tarreau4781b152021-04-06 13:53:36 +0200379 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100380 BUG_ON(one > zero);
381 return 1;
382}
383
Willy Tarreau305cfbd2022-02-25 08:52:39 +0100384/* parse a "debug dev warn" command. It always returns 1.
385 * Note: we make sure not to make the function static so that it appears in the trace.
386 */
387int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
388{
389 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
390 return 1;
391
392 _HA_ATOMIC_INC(&debug_commands_issued);
393 WARN_ON(one > zero);
394 return 1;
395}
396
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100397/* parse a "debug dev check" command. It always returns 1.
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100398 * Note: we make sure not to make the function static so that it appears in the trace.
399 */
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100400int debug_parse_cli_check(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100401{
402 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
403 return 1;
404
405 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100406 CHECK_IF(one > zero);
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100407 return 1;
408}
409
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200410/* parse a "debug dev close" command. It always returns 1. */
411static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
412{
413 int fd;
414
415 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
416 return 1;
417
Willy Tarreau9d008692019-08-09 11:21:01 +0200418 if (!*args[3])
419 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200420
421 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200422 if (fd < 0 || fd >= global.maxsock)
423 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424
Willy Tarreau9d008692019-08-09 11:21:01 +0200425 if (!fdtab[fd].owner)
426 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200427
Willy Tarreau4781b152021-04-06 13:53:36 +0200428 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200429 fd_delete(fd);
430 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200431}
432
433/* parse a "debug dev delay" command. It always returns 1. */
434static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
435{
436 int delay = atoi(args[3]);
437
438 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
439 return 1;
440
Willy Tarreau4781b152021-04-06 13:53:36 +0200441 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200442 usleep((long)delay * 1000);
443 return 1;
444}
445
446/* parse a "debug dev log" command. It always returns 1. */
447static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
448{
449 int arg;
450
451 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
452 return 1;
453
Willy Tarreau4781b152021-04-06 13:53:36 +0200454 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200455 chunk_reset(&trash);
456 for (arg = 3; *args[arg]; arg++) {
457 if (arg > 3)
458 chunk_strcat(&trash, " ");
459 chunk_strcat(&trash, args[arg]);
460 }
461
462 send_log(NULL, LOG_INFO, "%s\n", trash.area);
463 return 1;
464}
465
466/* parse a "debug dev loop" command. It always returns 1. */
467static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
468{
469 struct timeval deadline, curr;
470 int loop = atoi(args[3]);
471
472 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
473 return 1;
474
Willy Tarreau4781b152021-04-06 13:53:36 +0200475 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200476 gettimeofday(&curr, NULL);
477 tv_ms_add(&deadline, &curr, loop);
478
479 while (tv_ms_cmp(&curr, &deadline) < 0)
480 gettimeofday(&curr, NULL);
481
482 return 1;
483}
484
485/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
486static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
487{
488 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
489 return 1;
490
Willy Tarreau4781b152021-04-06 13:53:36 +0200491 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200492 ha_panic();
493 return 1;
494}
495
496/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200497#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200498static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
499{
Willy Tarreau368bff42019-12-06 17:18:28 +0100500 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200501 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100502 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200503
504 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
505 return 1;
506
Willy Tarreau4781b152021-04-06 13:53:36 +0200507 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200508 chunk_reset(&trash);
509 for (arg = 3; *args[arg]; arg++) {
510 if (arg > 3)
511 chunk_strcat(&trash, " ");
512 chunk_strcat(&trash, args[arg]);
513 }
514
Willy Tarreau368bff42019-12-06 17:18:28 +0100515 thread_isolate();
516 if (pipe(pipefd) < 0)
517 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200518
Willy Tarreau368bff42019-12-06 17:18:28 +0100519 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
520 goto fail_fcntl;
521
522 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
523 goto fail_fcntl;
524
525 pid = fork();
526
527 if (pid < 0)
528 goto fail_fork;
529 else if (pid == 0) {
530 /* child */
531 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
532
533 close(0);
534 dup2(pipefd[1], 1);
535 dup2(pipefd[1], 2);
536
537 cmd[2] = trash.area;
538 execvp(cmd[0], cmd);
539 printf("execvp() failed\n");
540 exit(1);
541 }
542
543 /* parent */
544 thread_release();
545 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200546 chunk_reset(&trash);
547 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100548 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
549 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200550 break;
551 trash.data += ret;
552 if (trash.data + 20 == trash.size) {
553 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
554 break;
555 }
556 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100557 close(pipefd[0]);
558 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200559 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200560 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100561
562 fail_fork:
563 fail_fcntl:
564 close(pipefd[0]);
565 close(pipefd[1]);
566 fail_pipe:
567 thread_release();
568 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200569}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200570#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200571
572/* parse a "debug dev hex" command. It always returns 1. */
573static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
574{
575 unsigned long start, len;
576
577 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
578 return 1;
579
Willy Tarreau9d008692019-08-09 11:21:01 +0200580 if (!*args[3])
581 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200582
583 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200584 if (!start)
585 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200586
Willy Tarreau4781b152021-04-06 13:53:36 +0200587 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200588
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200589 /* by default, dump ~128 till next block of 16 */
590 len = strtoul(args[4], NULL, 0);
591 if (!len)
592 len = ((start + 128) & -16) - start;
593
594 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200595 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200596 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200597 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200598}
599
Willy Tarreau48129be2021-05-04 18:40:50 +0200600/* parse a "debug dev sym <addr>" command. It always returns 1. */
601static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
602{
603 unsigned long addr;
604
605 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
606 return 1;
607
608 if (!*args[3])
609 return cli_err(appctx, "Missing memory address to be resolved.\n");
610
611 _HA_ATOMIC_INC(&debug_commands_issued);
612
613 addr = strtoul(args[3], NULL, 0);
614 chunk_printf(&trash, "%#lx resolves to ", addr);
615 resolve_sym_name(&trash, NULL, (const void *)addr);
616 chunk_appendf(&trash, "\n");
617
618 return cli_msg(appctx, LOG_INFO, trash.area);
619}
620
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200621/* parse a "debug dev tkill" command. It always returns 1. */
622static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
623{
624 int thr = 0;
625 int sig = SIGABRT;
626
627 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
628 return 1;
629
630 if (*args[3])
631 thr = atoi(args[3]);
632
Willy Tarreau9d008692019-08-09 11:21:01 +0200633 if (thr < 0 || thr > global.nbthread)
634 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200635
636 if (*args[4])
637 sig = atoi(args[4]);
638
Willy Tarreau4781b152021-04-06 13:53:36 +0200639 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200640 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200641 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200642 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200643 raise(sig);
644 return 1;
645}
646
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100647/* parse a "debug dev write" command. It always returns 1. */
648static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
649{
650 unsigned long len;
651
652 if (!*args[3])
653 return cli_err(appctx, "Missing output size.\n");
654
655 len = strtoul(args[3], NULL, 0);
656 if (len >= trash.size)
657 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
658
Willy Tarreau4781b152021-04-06 13:53:36 +0200659 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100660
661 chunk_reset(&trash);
662 trash.data = len;
663 memset(trash.area, '.', trash.data);
664 trash.area[trash.data] = 0;
665 for (len = 64; len < trash.data; len += 64)
666 trash.area[len] = '\n';
667 return cli_msg(appctx, LOG_INFO, trash.area);
668}
669
Willy Tarreau68680bb2019-10-23 17:23:25 +0200670/* parse a "debug dev stream" command */
671/*
672 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
673 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
674 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
675 * [sif.s[=<state>]] [sib.s[=<state>]]
676 */
677static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
678{
Christopher Faulet908628c2022-03-25 16:43:49 +0100679 struct stream *s = __cs_strm(appctx->owner);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200680 int arg;
681 void *ptr;
682 int size;
683 const char *word, *end;
684 struct ist name;
685 char *msg = NULL;
686 char *endarg;
687 unsigned long long old, new;
688
689 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
690 return 1;
691
692 ptr = NULL; size = 0;
693
694 if (!*args[3]) {
695 return cli_err(appctx,
696 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
Christopher Faulet582a2262022-04-04 11:25:59 +0200697 " <obj> = {strm | strm.f | strm.x | csf.s | csb.s |\n"
Willy Tarreau68680bb2019-10-23 17:23:25 +0200698 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
699 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
700 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
701 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
702 );
703 }
704
Willy Tarreau4781b152021-04-06 13:53:36 +0200705 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200706 for (arg = 3; *args[arg]; arg++) {
707 old = 0;
708 end = word = args[arg];
709 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
710 end++;
711 name = ist2(word, end - word);
712 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200713 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200714 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200715 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200716 } else if (isteq(name, ist("strm.x"))) {
717 ptr = (!s || !may_access(s)) ? NULL : &s->conn_exp; size = sizeof(s->conn_exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200718 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200719 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200720 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200721 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200722 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200723 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200724 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200725 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200726 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200727 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200728 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200729 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200730 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200731 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Christopher Faulet62e75742022-03-31 09:16:34 +0200732 } else if (isteq(name, ist("csf.s"))) {
733 ptr = (!s || !may_access(s)) ? NULL : &s->csf->state; size = sizeof(s->csf->state);
734 } else if (isteq(name, ist("csb.s"))) {
735 ptr = (!s || !may_access(s)) ? NULL : &s->csf->state; size = sizeof(s->csb->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200736 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200737 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200738 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
739 continue;
740 } else
741 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
742
743 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200744 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200745 if (size == 8)
746 old = read_u64(ptr);
747 else if (size == 4)
748 old = read_u32(ptr);
749 else if (size == 2)
750 old = read_u16(ptr);
751 else
752 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200753 } else {
754 memprintf(&msg,
755 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
756 msg ? msg : "", ptr, (int)(end - word), word);
757 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200758 }
759
760 /* parse the new value . */
761 new = strtoll(end + 1, &endarg, 0);
762 if (end[1] && *endarg) {
763 if (strcmp(end + 1, "now") == 0)
764 new = now_ms;
765 else {
766 memprintf(&msg,
767 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
768 msg ? msg : "", end + 1, (int)(end - word), word);
769 continue;
770 }
771 }
772
773 switch (*end) {
774 case '\0': /* show */
775 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
776 new = old; // do not change the value
777 break;
778
779 case '=': /* set */
780 break;
781
782 case '^': /* XOR */
783 new = old ^ new;
784 break;
785
786 case '+': /* OR */
787 new = old | new;
788 break;
789
790 case '-': /* AND NOT */
791 new = old & ~new;
792 break;
793
794 default:
795 break;
796 }
797
798 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200799 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200800 if (size == 8)
801 write_u64(ptr, new);
802 else if (size == 4)
803 write_u32(ptr, new);
804 else if (size == 2)
805 write_u16(ptr, new);
806 else
807 *(uint8_t *)ptr = new;
808 }
809 }
810
811 if (msg && *msg)
812 return cli_dynmsg(appctx, LOG_INFO, msg);
813 return 1;
814}
815
Willy Tarreau144f84a2021-03-02 16:09:26 +0100816static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100817{
818 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
819 unsigned long inter = tctx[1];
820 unsigned long rnd;
821
822 t->expire = tick_add(now_ms, inter);
823
824 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100825 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100826 if (rnd & 1) {
827 rnd >>= 1;
828 rnd %= tctx[0];
829 rnd = tctx[rnd + 2];
830
831 if (rnd & 1)
832 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
833 else
834 tasklet_wakeup((struct tasklet *)rnd);
835 }
836 return t;
837}
838
Willy Tarreau144f84a2021-03-02 16:09:26 +0100839static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100840{
841 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
842 unsigned long rnd;
843 int i;
844
845 /* wake up two random entries */
846 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100847 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100848 rnd = tctx[rnd + 2];
849
850 if (rnd & 1)
851 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
852 else
853 tasklet_wakeup((struct tasklet *)rnd);
854 }
855 return t;
856}
857
858/* parse a "debug dev sched" command
859 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
860 */
861static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
862{
863 int arg;
864 void *ptr;
865 int size;
866 const char *word, *end;
867 struct ist name;
868 char *msg = NULL;
869 char *endarg;
870 unsigned long long new;
871 unsigned long count = 0;
872 unsigned long thrid = 0;
873 unsigned int inter = 0;
874 unsigned long mask, tmask;
875 unsigned long i;
876 int mode = 0; // 0 = tasklet; 1 = task
877 int single = 0;
878 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
879
880 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
881 return 1;
882
883 ptr = NULL; size = 0;
884 mask = all_threads_mask;
885
886 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
887 return cli_err(appctx,
888 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
889 " <obj> = {count | mask | inter | single }\n"
890 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
891 );
892 }
893
894 mode = strcmp(args[3], "task") == 0;
895
Willy Tarreau4781b152021-04-06 13:53:36 +0200896 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100897 for (arg = 4; *args[arg]; arg++) {
898 end = word = args[arg];
899 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
900 end++;
901 name = ist2(word, end - word);
902 if (isteq(name, ist("count"))) {
903 ptr = &count; size = sizeof(count);
904 } else if (isteq(name, ist("mask"))) {
905 ptr = &mask; size = sizeof(mask);
906 } else if (isteq(name, ist("tid"))) {
907 ptr = &thrid; size = sizeof(thrid);
908 } else if (isteq(name, ist("inter"))) {
909 ptr = &inter; size = sizeof(inter);
910 } else if (isteq(name, ist("single"))) {
911 ptr = &single; size = sizeof(single);
912 } else
913 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
914
915 /* parse the new value . */
916 new = strtoll(end + 1, &endarg, 0);
917 if (end[1] && *endarg) {
918 memprintf(&msg,
919 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
920 msg ? msg : "", end + 1, (int)(end - word), word);
921 continue;
922 }
923
924 /* write the new value */
925 if (size == 8)
926 write_u64(ptr, new);
927 else if (size == 4)
928 write_u32(ptr, new);
929 else if (size == 2)
930 write_u16(ptr, new);
931 else
932 *(uint8_t *)ptr = new;
933 }
934
935 tctx = calloc(sizeof(*tctx), count + 2);
936 if (!tctx)
937 goto fail;
938
939 tctx[0] = (unsigned long)count;
940 tctx[1] = (unsigned long)inter;
941
942 mask &= all_threads_mask;
943 if (!mask)
944 mask = tid_bit;
945
946 tmask = 0;
947 for (i = 0; i < count; i++) {
948 if (single || mode == 0) {
949 /* look for next bit matching a bit in mask or loop back to zero */
950 for (tmask <<= 1; !(mask & tmask); ) {
951 if (!(mask & -tmask))
952 tmask = 1;
953 else
954 tmask <<= 1;
955 }
956 } else {
957 /* multi-threaded task */
958 tmask = mask;
959 }
960
961 /* now, if poly or mask was set, tmask corresponds to the
962 * valid thread mask to use, otherwise it remains zero.
963 */
964 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
965 if (mode == 0) {
966 struct tasklet *tl = tasklet_new();
967
968 if (!tl)
969 goto fail;
970
971 if (tmask)
972 tl->tid = my_ffsl(tmask) - 1;
973 tl->process = debug_tasklet_handler;
974 tl->context = tctx;
975 tctx[i + 2] = (unsigned long)tl;
976 } else {
977 struct task *task = task_new(tmask ? tmask : tid_bit);
978
979 if (!task)
980 goto fail;
981
982 task->process = debug_task_handler;
983 task->context = tctx;
984 tctx[i + 2] = (unsigned long)task + 1;
985 }
986 }
987
988 /* start the tasks and tasklets */
989 for (i = 0; i < count; i++) {
990 unsigned long ctx = tctx[i + 2];
991
992 if (ctx & 1)
993 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
994 else
995 tasklet_wakeup((struct tasklet *)ctx);
996 }
997
998 if (msg && *msg)
999 return cli_dynmsg(appctx, LOG_INFO, msg);
1000 return 1;
1001
1002 fail:
1003 /* free partially allocated entries */
1004 for (i = 0; tctx && i < count; i++) {
1005 unsigned long ctx = tctx[i + 2];
1006
1007 if (!ctx)
1008 break;
1009
1010 if (ctx & 1)
1011 task_destroy((struct task *)(ctx - 1));
1012 else
1013 tasklet_free((struct tasklet *)ctx);
1014 }
1015
1016 free(tctx);
1017 return cli_err(appctx, "Not enough memory");
1018}
1019
Willy Tarreau5be7c192022-01-24 20:26:09 +01001020/* CLI parser for the "debug dev fd" command. The current FD to restart from is
1021 * stored in i0.
1022 */
1023static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
1024{
1025 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1026 return 1;
1027
1028 /* start at fd #0 */
1029 appctx->ctx.cli.i0 = 0;
1030 return 0;
1031}
1032
1033/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1034 * accessible from the process but not known from fdtab. The FD number to
1035 * restart from is stored in i0.
1036 */
1037static int debug_iohandler_fd(struct appctx *appctx)
1038{
Christopher Faulet908628c2022-03-25 16:43:49 +01001039 struct conn_stream *cs = appctx->owner;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001040 struct sockaddr_storage sa;
1041 struct stat statbuf;
1042 socklen_t salen, vlen;
1043 int ret1, ret2, port;
1044 char *addrstr;
1045 int ret = 1;
1046 int i, fd;
1047
Christopher Faulet908628c2022-03-25 16:43:49 +01001048 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau5be7c192022-01-24 20:26:09 +01001049 goto end;
1050
1051 chunk_reset(&trash);
1052
1053 thread_isolate();
1054
1055 /* we have two inner loops here, one for the proxy, the other one for
1056 * the buffer.
1057 */
1058 for (fd = appctx->ctx.cli.i0; fd < global.maxsock; fd++) {
1059 /* check for FD's existence */
1060 ret1 = fcntl(fd, F_GETFD, 0);
1061 if (ret1 == -1)
1062 continue; // not known to the process
1063 if (fdtab[fd].owner)
1064 continue; // well-known
1065
1066 /* OK we're seeing an orphan let's try to retrieve as much
1067 * information as possible about it.
1068 */
1069 chunk_printf(&trash, "%5d", fd);
1070
1071 if (fstat(fd, &statbuf) != -1) {
1072 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1073 isatty(fd) ? "tty.":
1074 S_ISREG(statbuf.st_mode) ? "file":
1075 S_ISDIR(statbuf.st_mode) ? "dir.":
1076 S_ISCHR(statbuf.st_mode) ? "chr.":
1077 S_ISBLK(statbuf.st_mode) ? "blk.":
1078 S_ISFIFO(statbuf.st_mode) ? "pipe":
1079 S_ISLNK(statbuf.st_mode) ? "link":
1080 S_ISSOCK(statbuf.st_mode) ? "sock":
1081#ifdef USE_EPOLL
1082 epoll_wait(fd, NULL, 0, 0) != -1 || errno != EBADF ? "epol":
1083#endif
1084 "????",
1085 (uint)statbuf.st_mode & 07777,
1086
1087 (ullong)statbuf.st_rdev,
1088 (ullong)statbuf.st_size,
1089 (ullong)statbuf.st_uid,
1090 (ullong)statbuf.st_gid,
1091
1092 (ullong)statbuf.st_dev,
1093 (ullong)statbuf.st_ino);
1094 }
1095
1096 chunk_appendf(&trash, " getfd=%s+%#x",
1097 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1098 ret1 &~ FD_CLOEXEC);
1099
1100 /* FD options */
1101 ret2 = fcntl(fd, F_GETFL, 0);
1102 if (ret2) {
1103 chunk_appendf(&trash, " getfl=%s",
1104 (ret1 & 3) >= 2 ? "O_RDWR" :
1105 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1106
1107 for (i = 2; i < 32; i++) {
1108 if (!(ret2 & (1UL << i)))
1109 continue;
1110 switch (1UL << i) {
1111 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1112 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1113 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1114 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1115 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001116#ifdef O_ASYNC
Willy Tarreau5be7c192022-01-24 20:26:09 +01001117 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001118#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001119#ifdef O_DIRECT
1120 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1121#endif
1122#ifdef O_NOATIME
1123 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1124#endif
1125 }
1126 }
1127 }
1128
1129 vlen = sizeof(ret2);
1130 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1131 if (ret1 != -1)
1132 chunk_appendf(&trash, " so_type=%d", ret2);
1133
1134 vlen = sizeof(ret2);
1135 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1136 if (ret1 != -1)
1137 chunk_appendf(&trash, " so_accept=%d", ret2);
1138
1139 vlen = sizeof(ret2);
1140 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1141 if (ret1 != -1)
1142 chunk_appendf(&trash, " so_error=%d", ret2);
1143
1144 salen = sizeof(sa);
1145 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1146 if (sa.ss_family == AF_INET)
1147 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1148 else if (sa.ss_family == AF_INET6)
1149 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1150 else
1151 port = 0;
1152 addrstr = sa2str(&sa, port, 0);
1153 chunk_appendf(&trash, " laddr=%s", addrstr);
1154 free(addrstr);
1155 }
1156
1157 salen = sizeof(sa);
1158 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1159 if (sa.ss_family == AF_INET)
1160 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1161 else if (sa.ss_family == AF_INET6)
1162 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1163 else
1164 port = 0;
1165 addrstr = sa2str(&sa, port, 0);
1166 chunk_appendf(&trash, " raddr=%s", addrstr);
1167 free(addrstr);
1168 }
1169
1170 chunk_appendf(&trash, "\n");
1171
Christopher Faulet908628c2022-03-25 16:43:49 +01001172 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001173 cs_rx_room_blk(cs);
Willy Tarreau5be7c192022-01-24 20:26:09 +01001174 appctx->ctx.cli.i0 = fd;
1175 ret = 0;
1176 break;
1177 }
1178 }
1179
1180 thread_release();
1181 end:
1182 return ret;
1183}
1184
Willy Tarreaua6026a02020-07-02 09:14:48 +02001185#if defined(DEBUG_MEM_STATS)
1186/* CLI parser for the "debug dev memstats" command */
1187static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1188{
1189 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1190 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1191
1192 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1193 return 1;
1194
1195 if (strcmp(args[3], "reset") == 0) {
1196 struct mem_stats *ptr;
1197
1198 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1199 return 1;
1200
1201 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1202 _HA_ATOMIC_STORE(&ptr->calls, 0);
1203 _HA_ATOMIC_STORE(&ptr->size, 0);
1204 }
1205 return 1;
1206 }
1207
1208 if (strcmp(args[3], "all") == 0)
1209 appctx->ctx.cli.i0 = 1;
1210
1211 /* otherwise proceed with the dump from p0 to p1 */
1212 appctx->ctx.cli.p0 = &__start_mem_stats;
1213 appctx->ctx.cli.p1 = &__stop_mem_stats;
1214 return 0;
1215}
1216
1217/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
1218 * structs referenced by pointers located between p0 and p1. Dumps all entries
1219 * if i0 > 0, otherwise only non-zero calls.
1220 */
1221static int debug_iohandler_memstats(struct appctx *appctx)
1222{
Christopher Faulet908628c2022-03-25 16:43:49 +01001223 struct conn_stream *cs = appctx->owner;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001224 struct mem_stats *ptr = appctx->ctx.cli.p0;
1225 int ret = 1;
1226
Christopher Faulet908628c2022-03-25 16:43:49 +01001227 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreaua6026a02020-07-02 09:14:48 +02001228 goto end;
1229
1230 chunk_reset(&trash);
1231
1232 /* we have two inner loops here, one for the proxy, the other one for
1233 * the buffer.
1234 */
1235 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1236 const char *type;
1237 const char *name;
1238 const char *p;
1239
1240 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1241 continue;
1242
1243 /* basename only */
1244 for (p = name = ptr->file; *p; p++) {
1245 if (*p == '/')
1246 name = p + 1;
1247 }
1248
1249 switch (ptr->type) {
1250 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1251 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1252 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1253 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1254 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1255 default: type = "UNSET"; break;
1256 }
1257
1258 //chunk_printf(&trash,
1259 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1260 // name, ptr->line, type,
1261 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1262 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1263
1264 chunk_printf(&trash, "%s:%d", name, ptr->line);
1265 while (trash.data < 25)
1266 trash.area[trash.data++] = ' ';
1267 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1268 type,
1269 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1270 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1271
Christopher Faulet908628c2022-03-25 16:43:49 +01001272 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001273 cs_rx_room_blk(cs);
Willy Tarreaua6026a02020-07-02 09:14:48 +02001274 appctx->ctx.cli.p0 = ptr;
1275 ret = 0;
1276 break;
1277 }
1278 }
1279
1280 end:
1281 return ret;
1282}
1283
1284#endif
1285
Willy Tarreauc7091d82019-05-17 10:08:49 +02001286#ifndef USE_THREAD_DUMP
1287
1288/* This function dumps all threads' state to the trash. This version is the
1289 * most basic one, which doesn't inspect other threads.
1290 */
1291void ha_thread_dump_all_to_trash()
1292{
1293 unsigned int thr;
1294
1295 for (thr = 0; thr < global.nbthread; thr++)
1296 ha_thread_dump(&trash, thr, tid);
1297}
1298
1299#else /* below USE_THREAD_DUMP is set */
1300
Willy Tarreauc7091d82019-05-17 10:08:49 +02001301/* ID of the thread requesting the dump */
1302static unsigned int thread_dump_tid;
1303
1304/* points to the buffer where the dump functions should write. It must
1305 * have already been initialized by the requester. Nothing is done if
1306 * it's NULL.
1307 */
1308struct buffer *thread_dump_buffer = NULL;
1309
1310void ha_thread_dump_all_to_trash()
1311{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001312 unsigned long old;
1313
1314 while (1) {
1315 old = 0;
1316 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1317 break;
1318 ha_thread_relax();
1319 }
1320
1321 thread_dump_buffer = &trash;
1322 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001323 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001324}
1325
1326/* handles DEBUGSIG to dump the state of the thread it's working on */
1327void debug_handler(int sig, siginfo_t *si, void *arg)
1328{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001329 /* first, let's check it's really for us and that we didn't just get
1330 * a spurious DEBUGSIG.
1331 */
1332 if (!(threads_to_dump & tid_bit))
1333 return;
1334
Willy Tarreauc7091d82019-05-17 10:08:49 +02001335 /* There are 4 phases in the dump process:
1336 * 1- wait for our turn, i.e. when all lower bits are gone.
1337 * 2- perform the action if our bit is set
1338 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001339 * the last one and have to put them all as a signal
1340 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001341 */
1342
1343 /* wait for all previous threads to finish first */
1344 while (threads_to_dump & (tid_bit - 1))
1345 ha_thread_relax();
1346
1347 /* dump if needed */
1348 if (threads_to_dump & tid_bit) {
1349 if (thread_dump_buffer)
1350 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1351 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1352 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001353 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001354 thread_dump_buffer = NULL;
1355 }
1356 else
1357 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1358 }
1359
1360 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001361 * bits again to broadcast the leaving condition so we'll see ourselves
1362 * present again. This way the threads_to_dump variable never passes to
1363 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001364 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001365 while (!(threads_to_dump & tid_bit))
1366 ha_thread_relax();
1367 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001368
1369 /* mark the current thread as stuck to detect it upon next invocation
1370 * if it didn't move.
1371 */
1372 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
Willy Tarreaua0b99532021-09-30 18:48:37 +02001373 th_ctx->flags |= TH_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001374}
1375
1376static int init_debug_per_thread()
1377{
1378 sigset_t set;
1379
1380 /* unblock the DEBUGSIG signal we intend to use */
1381 sigemptyset(&set);
1382 sigaddset(&set, DEBUGSIG);
1383 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1384 return 1;
1385}
1386
1387static int init_debug()
1388{
1389 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001390 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001391
Willy Tarreau0214b452020-03-04 06:01:40 +01001392 /* calling backtrace() will access libgcc at runtime. We don't want to
1393 * do it after the chroot, so let's perform a first call to have it
1394 * ready in memory for later use.
1395 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001396 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001397 sa.sa_handler = NULL;
1398 sa.sa_sigaction = debug_handler;
1399 sigemptyset(&sa.sa_mask);
1400 sa.sa_flags = SA_SIGINFO;
1401 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001402 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001403}
1404
1405REGISTER_POST_CHECK(init_debug);
1406REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1407
1408#endif /* USE_THREAD_DUMP */
1409
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001410/* register cli keywords */
1411static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001412 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6d3f1e32022-02-28 11:51:23 +01001413 {{ "debug", "dev", "check", NULL }, "debug dev check : call CHECK_IF() and possibly crash", debug_parse_cli_check, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001414 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1415 {{ "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 +02001416#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001417 {{ "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 +02001418#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001419 {{ "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 +02001420 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1421 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1422 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1423 {{ "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 +02001424#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001425 {{ "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 +02001426#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001427 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1428 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1429 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1430 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1431 {{ "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 +01001432 {{ "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 +02001433 {{ "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 +01001434#if defined(HA_HAVE_DUMP_LIBS)
1435 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1436#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001437 {{ "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 +02001438 {{},}
1439}};
1440
1441INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);