blob: ed7f971e741b276fceba52a0aaf6fa3c74aca6ad [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>
Willy Tarreau55542642021-10-08 09:33:24 +020033#include <haproxy/clock.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020034#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/fd.h>
36#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020037#include <haproxy/hlua.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020038#include <haproxy/http_ana.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020039#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/net_helper.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020041#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020042#include <haproxy/stconn.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 Tarreauf0c86dd2022-07-15 13:14:03 +020050/* The dump state is made of:
51 * - num_thread on the lowest 15 bits
52 * - a SYNC flag on bit 15 (waiting for sync start)
53 * - number of participating threads on bits 16-30
54 * Initiating a dump consists in setting it to SYNC and incrementing the
55 * num_thread part when entering the function. The first thread periodically
56 * recounts active threads and compares it to the ready ones, and clears SYNC
57 * and sets the number of participants to the value found, which serves as a
58 * start signal. A thread finished dumping looks up the TID of the next active
59 * thread after it and writes it in the lowest part. If there's none, it sets
60 * the thread counter to the number of participants and resets that part,
61 * which serves as an end-of-dump signal. All threads decrement the num_thread
62 * part. Then all threads wait for the value to reach zero. Only used when
63 * USE_THREAD_DUMP is set.
Willy Tarreaua37cb182019-07-31 19:20:39 +020064 */
Willy Tarreauf0c86dd2022-07-15 13:14:03 +020065#define THREAD_DUMP_TMASK 0x00007FFFU
66#define THREAD_DUMP_FSYNC 0x00008000U
67#define THREAD_DUMP_PMASK 0x7FFF0000U
68
Willy Tarreau89ed89e2022-07-01 21:18:03 +020069volatile unsigned int thread_dump_state = 0;
Willy Tarreau55433f92022-07-15 12:48:58 +020070unsigned int panic_started = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020071unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020072
Willy Tarreau123fc972021-01-22 13:52:41 +010073/* dumps a backtrace of the current thread that is appended to buffer <buf>.
74 * Lines are prefixed with the string <prefix> which may be empty (used for
75 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010076 * the function does not appear in the call stack. The <dump> argument
77 * indicates what dump state to start from, and should usually be zero. It
78 * may be among the following values:
79 * - 0: search usual callers before step 1, or directly jump to 2
80 * - 1: skip usual callers before step 2
81 * - 2: dump until polling loop, scheduler, or main() (excluded)
82 * - 3: end
83 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010084 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010085void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010086{
87 struct buffer bak;
88 char pfx2[100];
89 void *callers[100];
90 int j, nptrs;
91 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010092
93 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
94 if (!nptrs)
95 return;
96
97 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
98 pfx2[0] = 0;
99
100 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
101 * produce similar output to the following:
102 */
103 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100104 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
105 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100106 /* we failed to spot the starting point of the
107 * dump, let's start over dumping everything we
108 * have.
109 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100110 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +0100111 j = 0;
112 }
113 bak = *buf;
114 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
115 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100116 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100117 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +0100118 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +0100119 */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100120 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
121 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100122 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100123 *buf = bak;
124 continue;
125 }
126
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100127 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100128 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100129 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
130 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100131 *buf = bak;
132 continue;
133 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100134 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100135 }
136
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100137 if ((dump & 3) == 2) {
138 /* still dumping */
139 if (dump == 6) {
140 /* we only stop *after* main and we must send the LF */
141 if (addr == main) {
142 j = nptrs;
143 dump++;
144 }
145 }
146 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
147 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100148 *buf = bak;
149 break;
150 }
151 }
152 /* OK, line dumped */
153 chunk_appendf(buf, "\n");
154 }
155}
156
Willy Tarreaua8459b22021-01-22 14:12:27 +0100157/* dump a backtrace of current thread's stack to stderr. */
Willy Tarreau7831e022022-05-06 15:16:19 +0200158void ha_backtrace_to_stderr(void)
Willy Tarreaua8459b22021-01-22 14:12:27 +0100159{
160 char area[2048];
161 struct buffer b = b_make(area, sizeof(area), 0, 0);
162
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100163 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100164 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100165 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100166}
167
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200168/* Dumps to the buffer some known information for the desired thread, and
169 * optionally extra info for the current thread. The dump will be appended to
170 * the buffer, so the caller is responsible for preliminary initializing it.
171 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200172 * in front of the calling thread's line (usually it's tid). Any stuck thread
173 * is also prefixed with a '>'.
Willy Tarreaubdcd3252022-06-22 09:19:46 +0200174 * It must be called under thread isolation.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200175 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200176void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200177{
Willy Tarreau38d07122022-06-27 16:13:50 +0200178 unsigned long thr_bit = ha_thread_info[thr].ltid_bit;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200179 unsigned long long p = ha_thread_ctx[thr].prev_cpu_time;
Willy Tarreau21694982021-10-08 15:09:17 +0200180 unsigned long long n = now_cpu_time_thread(thr);
Willy Tarreaua0b99532021-09-30 18:48:37 +0200181 int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK);
Willy Tarreau03f9b352022-06-27 16:02:24 +0200182 int tgrp = ha_thread_info[thr].tgid;
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200183
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200184 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200185 "%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 +0200186 " %2u/%-2u stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200187 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200188 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200189 thread_has_tasks(),
Willy Tarreauc958c702022-06-16 15:59:36 +0200190 !eb_is_empty(&ha_thread_ctx[thr].rqueue_shared),
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200191 !eb_is_empty(&ha_thread_ctx[thr].timers),
192 !eb_is_empty(&ha_thread_ctx[thr].rqueue),
193 !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) &&
194 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) &&
195 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) &&
196 MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)),
197 ha_thread_ctx[thr].tasks_in_list,
198 ha_thread_ctx[thr].rq_total,
Willy Tarreau66ad98a2022-06-28 10:49:57 +0200199 ha_thread_info[thr].tgid, ha_thread_info[thr].ltid + 1,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200200 stuck,
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200201 !!(th_ctx->flags & TH_FL_TASK_PROFILING));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200202
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200203 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200204 " harmless=%d wantrdv=%d",
Willy Tarreau03f9b352022-06-27 16:02:24 +0200205 !!(_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp-1].threads_harmless) & thr_bit),
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200206 !!(th_ctx->flags & TH_FL_TASK_PROFILING));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200207
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200208 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200209 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200210
Willy Tarreaua3870b72021-09-13 19:24:47 +0200211 /* this is the end of what we can dump from outside the current thread */
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200212
213 if (thr != tid)
214 return;
215
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200216 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200217 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100218
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100219 if (stuck) {
220 /* We only emit the backtrace for stuck threads in order not to
221 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100222 * Please leave this as the last instruction in this function
223 * so that the compiler uses tail merging and the current
224 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100225 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100226 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100227 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200228}
229
230
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200231/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200232 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200233 * with <pfx>. The buffer is only appended and the first output starts by the
234 * pointer itself. The caller is responsible for making sure the task is not
235 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200236 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200237void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200238{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200239 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200240 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200241 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau52f238d2022-07-15 12:08:40 +0200242 const struct stconn *sc;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200243
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200244 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200245 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200246 return;
247 }
248
Willy Tarreau20db9112019-05-17 14:14:35 +0200249 if (TASK_IS_TASKLET(task))
250 chunk_appendf(buf,
251 "%p (tasklet) calls=%u\n",
252 task,
253 task->calls);
254 else
255 chunk_appendf(buf,
256 "%p (task) calls=%u last=%llu%s\n",
257 task,
258 task->calls,
Willy Tarreau04e50b32022-09-07 14:49:50 +0200259 task->wake_date ? (unsigned long long)(now_mono_time() - task->wake_date) : 0,
260 task->wake_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200261
Willy Tarreau2e89b092020-03-03 17:13:02 +0100262 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
263 resolve_sym_name(buf, NULL, task->process);
264 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200265
Willy Tarreaua512b022019-08-21 14:12:19 +0200266 if (task->process == task_run_applet && (appctx = task->context))
267 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
268 else
269 chunk_appendf(buf, "\n");
270
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200271 if (task->process == process_stream && task->context)
272 s = (struct stream *)task->context;
Willy Tarreau52f238d2022-07-15 12:08:40 +0200273 else if (task->process == task_run_applet && task->context && (sc = appctx_sc((struct appctx *)task->context)))
274 s = sc_strm(sc);
Willy Tarreau462b9892022-05-18 18:06:53 +0200275 else if (task->process == sc_conn_io_cb && task->context)
Willy Tarreauea27f482022-05-18 16:10:52 +0200276 s = sc_strm(((struct stconn *)task->context));
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200277
278 if (s)
279 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200280
281#ifdef USE_LUA
282 hlua = NULL;
283 if (s && (hlua = s->hlua)) {
284 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
285 }
286 else if (task->process == hlua_process_task && (hlua = task->context)) {
287 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
288 }
289 else if (task->process == task_run_applet && (appctx = task->context) &&
Willy Tarreaue23f33b2022-05-06 14:07:13 +0200290 (appctx->applet->fct == hlua_applet_tcp_fct)) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200291 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
292 }
293 else if (task->process == task_run_applet && (appctx = task->context) &&
Willy Tarreauaa229cc2022-05-06 14:26:10 +0200294 (appctx->applet->fct == hlua_applet_http_fct)) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200295 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
296 }
297
Christopher Faulet471425f2020-07-24 19:08:05 +0200298 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100299 chunk_appendf(buf, "stack traceback:\n ");
300 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
301 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200302 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200303 else
304 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200305#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200306}
307
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200308/* This function dumps all profiling settings. It returns 0 if the output
309 * buffer is full and it needs to be called again, otherwise non-zero.
310 */
311static int cli_io_handler_show_threads(struct appctx *appctx)
312{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200313 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200314 int thr;
315
Christopher Faulet87633c32023-04-03 18:32:50 +0200316 /* FIXME: Don't watch the other side !*/
Christopher Faulet7faac7c2023-04-04 10:05:27 +0200317 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUTW))
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200318 return 1;
319
320 if (appctx->st0)
321 thr = appctx->st1;
322 else
323 thr = 0;
324
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200325 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200326 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200327
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200328 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200329 /* failed, try again */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200330 appctx->st1 = thr;
331 return 0;
332 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200333 return 1;
334}
335
Willy Tarreau6ab7b212021-12-28 09:57:10 +0100336#if defined(HA_HAVE_DUMP_LIBS)
337/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
338static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
339{
340 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
341 return 1;
342
343 chunk_reset(&trash);
344 if (dump_libs(&trash, 1))
345 return cli_msg(appctx, LOG_INFO, trash.area);
346 else
347 return 0;
348}
349#endif
350
Willy Tarreau56131ca2019-05-20 13:48:29 +0200351/* dumps a state of all threads into the trash and on fd #2, then aborts. */
352void ha_panic()
353{
Willy Tarreau55433f92022-07-15 12:48:58 +0200354 if (HA_ATOMIC_FETCH_ADD(&panic_started, 1) != 0) {
355 /* a panic dump is already in progress, let's not disturb it,
356 * we'll be called via signal DEBUGSIG. By returning we may be
357 * able to leave a current signal handler (e.g. WDT) so that
358 * this will ensure more reliable signal delivery.
359 */
360 return;
361 }
Willy Tarreau56131ca2019-05-20 13:48:29 +0200362 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200363 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200364 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100365 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200366 for (;;)
367 abort();
368}
369
Willy Tarreau06e66c82022-03-02 15:52:03 +0100370/* Complain with message <msg> on stderr. If <counter> is not NULL, it is
371 * atomically incremented, and the message is only printed when the counter
372 * was zero, so that the message is only printed once. <taint> is only checked
373 * on bit 1, and will taint the process either for a bug (2) or warn (0).
374 */
375void complain(int *counter, const char *msg, int taint)
376{
377 if (counter && _HA_ATOMIC_FETCH_ADD(counter, 1))
378 return;
379 DISGUISE(write(2, msg, strlen(msg)));
380 if (taint & 2)
381 mark_tainted(TAINTED_BUG);
382 else
383 mark_tainted(TAINTED_WARN);
384}
385
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200386/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
387static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
388{
389 int code = 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 exit(code);
396 return 1;
397}
398
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100399/* parse a "debug dev bug" command. It always returns 1, though it should never return.
400 * Note: we make sure not to make the function static so that it appears in the trace.
401 */
402int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
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 Tarreau5baf4fe2021-01-22 14:15:46 +0100408 BUG_ON(one > zero);
409 return 1;
410}
411
Willy Tarreau305cfbd2022-02-25 08:52:39 +0100412/* parse a "debug dev warn" command. It always returns 1.
413 * Note: we make sure not to make the function static so that it appears in the trace.
414 */
415int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
416{
417 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
418 return 1;
419
420 _HA_ATOMIC_INC(&debug_commands_issued);
421 WARN_ON(one > zero);
422 return 1;
423}
424
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100425/* parse a "debug dev check" command. It always returns 1.
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100426 * Note: we make sure not to make the function static so that it appears in the trace.
427 */
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100428int debug_parse_cli_check(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100429{
430 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
431 return 1;
432
433 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100434 CHECK_IF(one > zero);
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100435 return 1;
436}
437
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200438/* parse a "debug dev close" command. It always returns 1. */
439static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
440{
441 int fd;
442
443 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
444 return 1;
445
Willy Tarreau9d008692019-08-09 11:21:01 +0200446 if (!*args[3])
447 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200448
449 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200450 if (fd < 0 || fd >= global.maxsock)
451 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200452
Willy Tarreau9d008692019-08-09 11:21:01 +0200453 if (!fdtab[fd].owner)
454 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200455
Willy Tarreau4781b152021-04-06 13:53:36 +0200456 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200457 fd_delete(fd);
458 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200459}
460
Willy Tarreaudadf00e2022-07-15 08:25:03 +0200461/* this is meant to cause a deadlock when more than one task is running it or when run twice */
462static struct task *debug_run_cli_deadlock(struct task *task, void *ctx, unsigned int state)
463{
464 static HA_SPINLOCK_T lock __maybe_unused;
465
466 HA_SPIN_LOCK(OTHER_LOCK, &lock);
467 return NULL;
468}
469
470/* parse a "debug dev deadlock" command. It always returns 1. */
471static int debug_parse_cli_deadlock(char **args, char *payload, struct appctx *appctx, void *private)
472{
473 int tasks;
474
475 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
476 return 1;
477
478 _HA_ATOMIC_INC(&debug_commands_issued);
479 for (tasks = atoi(args[3]); tasks > 0; tasks--) {
480 struct task *t = task_new_on(tasks % global.nbthread);
481 if (!t)
482 continue;
483 t->process = debug_run_cli_deadlock;
484 t->context = NULL;
485 task_wakeup(t, TASK_WOKEN_INIT);
486 }
487
488 return 1;
489}
490
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200491/* parse a "debug dev delay" command. It always returns 1. */
492static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
493{
494 int delay = atoi(args[3]);
495
496 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
497 return 1;
498
Willy Tarreau4781b152021-04-06 13:53:36 +0200499 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200500 usleep((long)delay * 1000);
501 return 1;
502}
503
504/* parse a "debug dev log" command. It always returns 1. */
505static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
506{
507 int arg;
508
509 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
510 return 1;
511
Willy Tarreau4781b152021-04-06 13:53:36 +0200512 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200513 chunk_reset(&trash);
514 for (arg = 3; *args[arg]; arg++) {
515 if (arg > 3)
516 chunk_strcat(&trash, " ");
517 chunk_strcat(&trash, args[arg]);
518 }
519
520 send_log(NULL, LOG_INFO, "%s\n", trash.area);
521 return 1;
522}
523
524/* parse a "debug dev loop" command. It always returns 1. */
525static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
526{
527 struct timeval deadline, curr;
528 int loop = atoi(args[3]);
529
530 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
531 return 1;
532
Willy Tarreau4781b152021-04-06 13:53:36 +0200533 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200534 gettimeofday(&curr, NULL);
535 tv_ms_add(&deadline, &curr, loop);
536
537 while (tv_ms_cmp(&curr, &deadline) < 0)
538 gettimeofday(&curr, NULL);
539
540 return 1;
541}
542
543/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
544static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
545{
546 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
547 return 1;
548
Willy Tarreau4781b152021-04-06 13:53:36 +0200549 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200550 ha_panic();
551 return 1;
552}
553
554/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200555#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200556static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
557{
Willy Tarreau368bff42019-12-06 17:18:28 +0100558 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200559 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100560 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200561
562 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
563 return 1;
564
Willy Tarreau4781b152021-04-06 13:53:36 +0200565 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200566 chunk_reset(&trash);
567 for (arg = 3; *args[arg]; arg++) {
568 if (arg > 3)
569 chunk_strcat(&trash, " ");
570 chunk_strcat(&trash, args[arg]);
571 }
572
Willy Tarreau368bff42019-12-06 17:18:28 +0100573 thread_isolate();
574 if (pipe(pipefd) < 0)
575 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200576
Willy Tarreau38247432022-04-26 10:24:14 +0200577 if (fd_set_cloexec(pipefd[0]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100578 goto fail_fcntl;
579
Willy Tarreau38247432022-04-26 10:24:14 +0200580 if (fd_set_cloexec(pipefd[1]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100581 goto fail_fcntl;
582
583 pid = fork();
584
585 if (pid < 0)
586 goto fail_fork;
587 else if (pid == 0) {
588 /* child */
589 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
590
591 close(0);
592 dup2(pipefd[1], 1);
593 dup2(pipefd[1], 2);
594
595 cmd[2] = trash.area;
596 execvp(cmd[0], cmd);
597 printf("execvp() failed\n");
598 exit(1);
599 }
600
601 /* parent */
602 thread_release();
603 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200604 chunk_reset(&trash);
605 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100606 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
607 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200608 break;
609 trash.data += ret;
610 if (trash.data + 20 == trash.size) {
611 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
612 break;
613 }
614 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100615 close(pipefd[0]);
616 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200617 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200618 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100619
620 fail_fork:
621 fail_fcntl:
622 close(pipefd[0]);
623 close(pipefd[1]);
624 fail_pipe:
625 thread_release();
626 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200627}
Willy Tarreaubd3b44e2023-03-09 08:25:01 +0100628
629/* handles SIGRTMAX to inject random delays on the receiving thread in order
630 * to try to increase the likelihood to reproduce inter-thread races. The
631 * signal is periodically sent by a task initiated by "debug dev delay-inj".
632 */
633void debug_delay_inj_sighandler(int sig, siginfo_t *si, void *arg)
634{
635 volatile int i = statistical_prng_range(10000);
636
637 while (i--)
638 __ha_cpu_relax();
639}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200640#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200641
642/* parse a "debug dev hex" command. It always returns 1. */
643static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
644{
645 unsigned long start, len;
646
647 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
648 return 1;
649
Willy Tarreau9d008692019-08-09 11:21:01 +0200650 if (!*args[3])
651 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200652
653 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200654 if (!start)
655 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200656
Willy Tarreau4781b152021-04-06 13:53:36 +0200657 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200658
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200659 /* by default, dump ~128 till next block of 16 */
660 len = strtoul(args[4], NULL, 0);
661 if (!len)
662 len = ((start + 128) & -16) - start;
663
664 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200665 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200666 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200667 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200668}
669
Willy Tarreau48129be2021-05-04 18:40:50 +0200670/* parse a "debug dev sym <addr>" command. It always returns 1. */
671static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
672{
673 unsigned long addr;
674
675 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
676 return 1;
677
678 if (!*args[3])
679 return cli_err(appctx, "Missing memory address to be resolved.\n");
680
681 _HA_ATOMIC_INC(&debug_commands_issued);
682
683 addr = strtoul(args[3], NULL, 0);
684 chunk_printf(&trash, "%#lx resolves to ", addr);
685 resolve_sym_name(&trash, NULL, (const void *)addr);
686 chunk_appendf(&trash, "\n");
687
688 return cli_msg(appctx, LOG_INFO, trash.area);
689}
690
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200691/* parse a "debug dev tkill" command. It always returns 1. */
692static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
693{
694 int thr = 0;
695 int sig = SIGABRT;
696
697 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
698 return 1;
699
700 if (*args[3])
701 thr = atoi(args[3]);
702
Willy Tarreau9d008692019-08-09 11:21:01 +0200703 if (thr < 0 || thr > global.nbthread)
704 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200705
706 if (*args[4])
707 sig = atoi(args[4]);
708
Willy Tarreau4781b152021-04-06 13:53:36 +0200709 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200710 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200711 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200712 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200713 raise(sig);
714 return 1;
715}
716
Erwan Le Goas54966df2022-09-14 17:24:22 +0200717/* hashes 'word' in "debug dev hash 'word' ". */
718static int debug_parse_cli_hash(char **args, char *payload, struct appctx *appctx, void *private)
719{
720 char *msg = NULL;
721
722 cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", HA_ANON_CLI(args[3])));
723 return 1;
724}
725
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100726/* parse a "debug dev write" command. It always returns 1. */
727static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
728{
729 unsigned long len;
730
731 if (!*args[3])
732 return cli_err(appctx, "Missing output size.\n");
733
734 len = strtoul(args[3], NULL, 0);
735 if (len >= trash.size)
736 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
737
Willy Tarreau4781b152021-04-06 13:53:36 +0200738 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100739
740 chunk_reset(&trash);
741 trash.data = len;
742 memset(trash.area, '.', trash.data);
743 trash.area[trash.data] = 0;
744 for (len = 64; len < trash.data; len += 64)
745 trash.area[len] = '\n';
746 return cli_msg(appctx, LOG_INFO, trash.area);
747}
748
Willy Tarreau68680bb2019-10-23 17:23:25 +0200749/* parse a "debug dev stream" command */
750/*
751 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
752 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
753 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
754 * [sif.s[=<state>]] [sib.s[=<state>]]
755 */
756static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
757{
Willy Tarreau0698c802022-05-11 14:09:57 +0200758 struct stream *s = appctx_strm(appctx);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200759 int arg;
760 void *ptr;
761 int size;
762 const char *word, *end;
763 struct ist name;
764 char *msg = NULL;
765 char *endarg;
766 unsigned long long old, new;
767
768 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
769 return 1;
770
771 ptr = NULL; size = 0;
772
773 if (!*args[3]) {
774 return cli_err(appctx,
775 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100776 " <obj> = {strm | strm.f | strm.x |\n"
Christopher Faulet15315d62023-02-20 08:23:51 +0100777 " scf.s | scb.s |\n"
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100778 " txn.f | req.f | res.f}\n"
Willy Tarreau68680bb2019-10-23 17:23:25 +0200779 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
780 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
781 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
782 );
783 }
784
Willy Tarreau4781b152021-04-06 13:53:36 +0200785 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200786 for (arg = 3; *args[arg]; arg++) {
787 old = 0;
788 end = word = args[arg];
789 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
790 end++;
791 name = ist2(word, end - word);
792 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200793 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200794 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200795 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200796 } else if (isteq(name, ist("strm.x"))) {
797 ptr = (!s || !may_access(s)) ? NULL : &s->conn_exp; size = sizeof(s->conn_exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200798 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200799 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200800 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200801 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200802 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200803 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200804 } else if (isteq(name, ist("scf.s"))) {
805 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scf->state);
806 } else if (isteq(name, ist("scb.s"))) {
807 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scb->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200808 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200809 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200810 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
811 continue;
812 } else
813 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
814
815 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200816 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200817 if (size == 8)
818 old = read_u64(ptr);
819 else if (size == 4)
820 old = read_u32(ptr);
821 else if (size == 2)
822 old = read_u16(ptr);
823 else
824 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200825 } else {
826 memprintf(&msg,
827 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
828 msg ? msg : "", ptr, (int)(end - word), word);
829 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200830 }
831
832 /* parse the new value . */
833 new = strtoll(end + 1, &endarg, 0);
834 if (end[1] && *endarg) {
835 if (strcmp(end + 1, "now") == 0)
836 new = now_ms;
837 else {
838 memprintf(&msg,
839 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
840 msg ? msg : "", end + 1, (int)(end - word), word);
841 continue;
842 }
843 }
844
845 switch (*end) {
846 case '\0': /* show */
847 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
848 new = old; // do not change the value
849 break;
850
851 case '=': /* set */
852 break;
853
854 case '^': /* XOR */
855 new = old ^ new;
856 break;
857
858 case '+': /* OR */
859 new = old | new;
860 break;
861
862 case '-': /* AND NOT */
863 new = old & ~new;
864 break;
865
866 default:
867 break;
868 }
869
870 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200871 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200872 if (size == 8)
873 write_u64(ptr, new);
874 else if (size == 4)
875 write_u32(ptr, new);
876 else if (size == 2)
877 write_u16(ptr, new);
878 else
879 *(uint8_t *)ptr = new;
880 }
881 }
882
883 if (msg && *msg)
884 return cli_dynmsg(appctx, LOG_INFO, msg);
885 return 1;
886}
887
Willy Tarreaubd3b44e2023-03-09 08:25:01 +0100888#if defined(DEBUG_DEV)
889static struct task *debug_delay_inj_task(struct task *t, void *ctx, unsigned int state)
890{
891 unsigned long *tctx = ctx; // [0] = interval, [1] = nbwakeups
892 unsigned long inter = tctx[0];
893 unsigned long count = tctx[1];
894 unsigned long rnd;
895
896 if (inter)
897 t->expire = tick_add(now_ms, inter);
898 else
899 task_wakeup(t, TASK_WOKEN_MSG);
900
901 /* wake a random thread */
902 while (count--) {
903 rnd = statistical_prng_range(global.nbthread);
904 ha_tkill(rnd, SIGRTMAX);
905 }
906 return t;
907}
908
909/* parse a "debug dev delay-inj" command
910 * debug dev delay-inj <inter> <count>
911 */
912static int debug_parse_delay_inj(char **args, char *payload, struct appctx *appctx, void *private)
913{
914 unsigned long *tctx; // [0] = inter, [2] = count
915 struct task *task;
916
917 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
918 return 1;
919
920 if (!*args[4])
921 return cli_err(appctx, "Usage: debug dev delay-inj <inter_ms> <count>*\n");
922
923 _HA_ATOMIC_INC(&debug_commands_issued);
924
925 tctx = calloc(sizeof(*tctx), 2);
926 if (!tctx)
927 goto fail;
928
929 tctx[0] = atoi(args[3]);
930 tctx[1] = atoi(args[4]);
931
932 task = task_new_here/*anywhere*/();
933 if (!task)
934 goto fail;
935
936 task->process = debug_delay_inj_task;
937 task->context = tctx;
938 task_wakeup(task, TASK_WOKEN_INIT);
939 return 1;
940
941 fail:
942 free(tctx);
943 return cli_err(appctx, "Not enough memory");
944}
945#endif // DEBUG_DEV
946
Willy Tarreau144f84a2021-03-02 16:09:26 +0100947static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100948{
949 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
950 unsigned long inter = tctx[1];
951 unsigned long rnd;
952
953 t->expire = tick_add(now_ms, inter);
954
955 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100956 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100957 if (rnd & 1) {
958 rnd >>= 1;
959 rnd %= tctx[0];
960 rnd = tctx[rnd + 2];
961
962 if (rnd & 1)
963 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
964 else
965 tasklet_wakeup((struct tasklet *)rnd);
966 }
967 return t;
968}
969
Willy Tarreau144f84a2021-03-02 16:09:26 +0100970static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100971{
972 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
973 unsigned long rnd;
974 int i;
975
976 /* wake up two random entries */
977 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100978 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100979 rnd = tctx[rnd + 2];
980
981 if (rnd & 1)
982 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
983 else
984 tasklet_wakeup((struct tasklet *)rnd);
985 }
986 return t;
987}
988
989/* parse a "debug dev sched" command
990 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
991 */
992static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
993{
994 int arg;
995 void *ptr;
996 int size;
997 const char *word, *end;
998 struct ist name;
999 char *msg = NULL;
1000 char *endarg;
1001 unsigned long long new;
1002 unsigned long count = 0;
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001003 unsigned long thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001004 unsigned int inter = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001005 unsigned long i;
1006 int mode = 0; // 0 = tasklet; 1 = task
Willy Tarreaua5a44792020-11-29 17:12:15 +01001007 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1008
1009 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1010 return 1;
1011
1012 ptr = NULL; size = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001013
1014 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
1015 return cli_err(appctx,
1016 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001017 " <obj> = {count | tid | inter }\n"
Willy Tarreaua5a44792020-11-29 17:12:15 +01001018 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
1019 );
1020 }
1021
1022 mode = strcmp(args[3], "task") == 0;
1023
Willy Tarreau4781b152021-04-06 13:53:36 +02001024 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001025 for (arg = 4; *args[arg]; arg++) {
1026 end = word = args[arg];
1027 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
1028 end++;
1029 name = ist2(word, end - word);
1030 if (isteq(name, ist("count"))) {
1031 ptr = &count; size = sizeof(count);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001032 } else if (isteq(name, ist("tid"))) {
1033 ptr = &thrid; size = sizeof(thrid);
1034 } else if (isteq(name, ist("inter"))) {
1035 ptr = &inter; size = sizeof(inter);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001036 } else
1037 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
1038
1039 /* parse the new value . */
1040 new = strtoll(end + 1, &endarg, 0);
1041 if (end[1] && *endarg) {
1042 memprintf(&msg,
1043 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
1044 msg ? msg : "", end + 1, (int)(end - word), word);
1045 continue;
1046 }
1047
1048 /* write the new value */
1049 if (size == 8)
1050 write_u64(ptr, new);
1051 else if (size == 4)
1052 write_u32(ptr, new);
1053 else if (size == 2)
1054 write_u16(ptr, new);
1055 else
1056 *(uint8_t *)ptr = new;
1057 }
1058
1059 tctx = calloc(sizeof(*tctx), count + 2);
1060 if (!tctx)
1061 goto fail;
1062
1063 tctx[0] = (unsigned long)count;
1064 tctx[1] = (unsigned long)inter;
1065
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001066 if (thrid >= global.nbthread)
1067 thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001068
Willy Tarreaua5a44792020-11-29 17:12:15 +01001069 for (i = 0; i < count; i++) {
Willy Tarreaua5a44792020-11-29 17:12:15 +01001070 /* now, if poly or mask was set, tmask corresponds to the
1071 * valid thread mask to use, otherwise it remains zero.
1072 */
1073 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
1074 if (mode == 0) {
1075 struct tasklet *tl = tasklet_new();
1076
1077 if (!tl)
1078 goto fail;
1079
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001080 tl->tid = thrid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001081 tl->process = debug_tasklet_handler;
1082 tl->context = tctx;
1083 tctx[i + 2] = (unsigned long)tl;
1084 } else {
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001085 struct task *task = task_new_on(thrid);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001086
1087 if (!task)
1088 goto fail;
1089
1090 task->process = debug_task_handler;
1091 task->context = tctx;
1092 tctx[i + 2] = (unsigned long)task + 1;
1093 }
1094 }
1095
1096 /* start the tasks and tasklets */
1097 for (i = 0; i < count; i++) {
1098 unsigned long ctx = tctx[i + 2];
1099
1100 if (ctx & 1)
1101 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
1102 else
1103 tasklet_wakeup((struct tasklet *)ctx);
1104 }
1105
1106 if (msg && *msg)
1107 return cli_dynmsg(appctx, LOG_INFO, msg);
1108 return 1;
1109
1110 fail:
1111 /* free partially allocated entries */
1112 for (i = 0; tctx && i < count; i++) {
1113 unsigned long ctx = tctx[i + 2];
1114
1115 if (!ctx)
1116 break;
1117
1118 if (ctx & 1)
1119 task_destroy((struct task *)(ctx - 1));
1120 else
1121 tasklet_free((struct tasklet *)ctx);
1122 }
1123
1124 free(tctx);
1125 return cli_err(appctx, "Not enough memory");
1126}
1127
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001128/* CLI state for "debug dev fd" */
1129struct dev_fd_ctx {
1130 int start_fd;
1131};
1132
Willy Tarreau5be7c192022-01-24 20:26:09 +01001133/* CLI parser for the "debug dev fd" command. The current FD to restart from is
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001134 * stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001135 */
1136static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
1137{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001138 struct dev_fd_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1139
Willy Tarreau5be7c192022-01-24 20:26:09 +01001140 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1141 return 1;
1142
1143 /* start at fd #0 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001144 ctx->start_fd = 0;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001145 return 0;
1146}
1147
1148/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1149 * accessible from the process but not known from fdtab. The FD number to
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001150 * restart from is stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001151 */
1152static int debug_iohandler_fd(struct appctx *appctx)
1153{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001154 struct dev_fd_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001155 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau5be7c192022-01-24 20:26:09 +01001156 struct sockaddr_storage sa;
1157 struct stat statbuf;
1158 socklen_t salen, vlen;
1159 int ret1, ret2, port;
1160 char *addrstr;
1161 int ret = 1;
1162 int i, fd;
1163
Christopher Faulet87633c32023-04-03 18:32:50 +02001164 /* FIXME: Don't watch the other side !*/
Christopher Faulet7faac7c2023-04-04 10:05:27 +02001165 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUTW))
Willy Tarreau5be7c192022-01-24 20:26:09 +01001166 goto end;
1167
1168 chunk_reset(&trash);
1169
1170 thread_isolate();
1171
1172 /* we have two inner loops here, one for the proxy, the other one for
1173 * the buffer.
1174 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001175 for (fd = ctx->start_fd; fd < global.maxsock; fd++) {
Willy Tarreau5be7c192022-01-24 20:26:09 +01001176 /* check for FD's existence */
1177 ret1 = fcntl(fd, F_GETFD, 0);
1178 if (ret1 == -1)
1179 continue; // not known to the process
1180 if (fdtab[fd].owner)
1181 continue; // well-known
1182
1183 /* OK we're seeing an orphan let's try to retrieve as much
1184 * information as possible about it.
1185 */
1186 chunk_printf(&trash, "%5d", fd);
1187
1188 if (fstat(fd, &statbuf) != -1) {
1189 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1190 isatty(fd) ? "tty.":
1191 S_ISREG(statbuf.st_mode) ? "file":
1192 S_ISDIR(statbuf.st_mode) ? "dir.":
1193 S_ISCHR(statbuf.st_mode) ? "chr.":
1194 S_ISBLK(statbuf.st_mode) ? "blk.":
1195 S_ISFIFO(statbuf.st_mode) ? "pipe":
1196 S_ISLNK(statbuf.st_mode) ? "link":
1197 S_ISSOCK(statbuf.st_mode) ? "sock":
1198#ifdef USE_EPOLL
1199 epoll_wait(fd, NULL, 0, 0) != -1 || errno != EBADF ? "epol":
1200#endif
1201 "????",
1202 (uint)statbuf.st_mode & 07777,
1203
1204 (ullong)statbuf.st_rdev,
1205 (ullong)statbuf.st_size,
1206 (ullong)statbuf.st_uid,
1207 (ullong)statbuf.st_gid,
1208
1209 (ullong)statbuf.st_dev,
1210 (ullong)statbuf.st_ino);
1211 }
1212
1213 chunk_appendf(&trash, " getfd=%s+%#x",
1214 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1215 ret1 &~ FD_CLOEXEC);
1216
1217 /* FD options */
1218 ret2 = fcntl(fd, F_GETFL, 0);
1219 if (ret2) {
1220 chunk_appendf(&trash, " getfl=%s",
1221 (ret1 & 3) >= 2 ? "O_RDWR" :
1222 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1223
1224 for (i = 2; i < 32; i++) {
1225 if (!(ret2 & (1UL << i)))
1226 continue;
1227 switch (1UL << i) {
1228 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1229 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1230 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1231 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1232 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001233#ifdef O_ASYNC
Willy Tarreau5be7c192022-01-24 20:26:09 +01001234 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001235#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001236#ifdef O_DIRECT
1237 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1238#endif
1239#ifdef O_NOATIME
1240 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1241#endif
1242 }
1243 }
1244 }
1245
1246 vlen = sizeof(ret2);
1247 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1248 if (ret1 != -1)
1249 chunk_appendf(&trash, " so_type=%d", ret2);
1250
1251 vlen = sizeof(ret2);
1252 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1253 if (ret1 != -1)
1254 chunk_appendf(&trash, " so_accept=%d", ret2);
1255
1256 vlen = sizeof(ret2);
1257 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1258 if (ret1 != -1)
1259 chunk_appendf(&trash, " so_error=%d", ret2);
1260
1261 salen = sizeof(sa);
1262 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1263 if (sa.ss_family == AF_INET)
1264 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1265 else if (sa.ss_family == AF_INET6)
1266 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1267 else
1268 port = 0;
1269 addrstr = sa2str(&sa, port, 0);
1270 chunk_appendf(&trash, " laddr=%s", addrstr);
1271 free(addrstr);
1272 }
1273
1274 salen = sizeof(sa);
1275 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1276 if (sa.ss_family == AF_INET)
1277 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1278 else if (sa.ss_family == AF_INET6)
1279 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1280 else
1281 port = 0;
1282 addrstr = sa2str(&sa, port, 0);
1283 chunk_appendf(&trash, " raddr=%s", addrstr);
1284 free(addrstr);
1285 }
1286
1287 chunk_appendf(&trash, "\n");
1288
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001289 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001290 ctx->start_fd = fd;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001291 ret = 0;
1292 break;
1293 }
1294 }
1295
1296 thread_release();
1297 end:
1298 return ret;
1299}
1300
Willy Tarreaua6026a02020-07-02 09:14:48 +02001301#if defined(DEBUG_MEM_STATS)
Willy Tarreau40e952f2022-05-05 14:39:51 +02001302
1303/* CLI state for "debug dev memstats" */
1304struct dev_mem_ctx {
1305 struct mem_stats *start, *stop; /* begin/end of dump */
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001306 char *match; /* non-null if a name prefix is specified */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001307 int show_all; /* show all entries if non-null */
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001308 int width; /* 1st column width */
1309 long tot_size; /* sum of alloc-free */
1310 ulong tot_calls; /* sum of calls */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001311};
1312
1313/* CLI parser for the "debug dev memstats" command. Sets a dev_mem_ctx shown above. */
Willy Tarreaua6026a02020-07-02 09:14:48 +02001314static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1315{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001316 struct dev_mem_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau334d0912022-11-30 16:50:48 +01001317 int arg;
Willy Tarreau40e952f2022-05-05 14:39:51 +02001318
Willy Tarreaua6026a02020-07-02 09:14:48 +02001319 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1320 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1321
1322 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1323 return 1;
1324
Willy Tarreau334d0912022-11-30 16:50:48 +01001325 for (arg = 3; *args[arg]; arg++) {
1326 if (strcmp(args[arg], "reset") == 0) {
1327 struct mem_stats *ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001328
Willy Tarreau334d0912022-11-30 16:50:48 +01001329 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1330 return 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001331
Willy Tarreau334d0912022-11-30 16:50:48 +01001332 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1333 _HA_ATOMIC_STORE(&ptr->calls, 0);
1334 _HA_ATOMIC_STORE(&ptr->size, 0);
1335 }
1336 return 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001337 }
Willy Tarreau334d0912022-11-30 16:50:48 +01001338 else if (strcmp(args[arg], "all") == 0) {
1339 ctx->show_all = 1;
1340 continue;
1341 }
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001342 else if (strcmp(args[arg], "match") == 0 && *args[arg + 1]) {
1343 ha_free(&ctx->match);
1344 ctx->match = strdup(args[arg + 1]);
1345 arg++;
1346 continue;
1347 }
Willy Tarreau334d0912022-11-30 16:50:48 +01001348 else
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001349 return cli_err(appctx, "Expects either 'reset', 'all', or 'match <pfx>'.\n");
Willy Tarreaua6026a02020-07-02 09:14:48 +02001350 }
1351
Willy Tarreaua6026a02020-07-02 09:14:48 +02001352 /* otherwise proceed with the dump from p0 to p1 */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001353 ctx->start = &__start_mem_stats;
1354 ctx->stop = &__stop_mem_stats;
Willy Tarreau4a426e22022-08-09 08:51:08 +02001355 ctx->width = 0;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001356 return 0;
1357}
1358
Willy Tarreau40e952f2022-05-05 14:39:51 +02001359/* CLI I/O handler for the "debug dev memstats" command using a dev_mem_ctx
1360 * found in appctx->svcctx. Dumps all mem_stats structs referenced by pointers
1361 * located between ->start and ->stop. Dumps all entries if ->show_all != 0,
1362 * otherwise only non-zero calls.
Willy Tarreaua6026a02020-07-02 09:14:48 +02001363 */
1364static int debug_iohandler_memstats(struct appctx *appctx)
1365{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001366 struct dev_mem_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001367 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001368 struct mem_stats *ptr;
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001369 const char *pfx = ctx->match;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001370 int ret = 1;
1371
Christopher Faulet87633c32023-04-03 18:32:50 +02001372 /* FIXME: Don't watch the other side !*/
Christopher Faulet7faac7c2023-04-04 10:05:27 +02001373 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUTW))
Willy Tarreaua6026a02020-07-02 09:14:48 +02001374 goto end;
1375
Willy Tarreau4a426e22022-08-09 08:51:08 +02001376 if (!ctx->width) {
1377 /* we don't know the first column's width, let's compute it
1378 * now based on a first pass on printable entries and their
1379 * expected width (approximated).
1380 */
1381 for (ptr = ctx->start; ptr != ctx->stop; ptr++) {
1382 const char *p, *name;
1383 int w = 0;
1384 char tmp;
1385
1386 if (!ptr->size && !ptr->calls && !ctx->show_all)
1387 continue;
1388
Willy Tarreaud96d2142022-09-06 08:05:59 +02001389 for (p = name = ptr->caller.file; *p; p++) {
Willy Tarreau4a426e22022-08-09 08:51:08 +02001390 if (*p == '/')
1391 name = p + 1;
1392 }
1393
1394 if (ctx->show_all)
Willy Tarreaud96d2142022-09-06 08:05:59 +02001395 w = snprintf(&tmp, 0, "%s(%s:%d) ", ptr->caller.func, name, ptr->caller.line);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001396 else
Willy Tarreaud96d2142022-09-06 08:05:59 +02001397 w = snprintf(&tmp, 0, "%s:%d ", name, ptr->caller.line);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001398
1399 if (w > ctx->width)
1400 ctx->width = w;
1401 }
1402 }
1403
Willy Tarreaua6026a02020-07-02 09:14:48 +02001404 /* we have two inner loops here, one for the proxy, the other one for
1405 * the buffer.
1406 */
Willy Tarreau4a426e22022-08-09 08:51:08 +02001407 for (ptr = ctx->start; ptr != ctx->stop; ptr++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001408 const char *type;
1409 const char *name;
1410 const char *p;
Willy Tarreau55c950b2022-08-09 08:15:27 +02001411 const char *info = NULL;
Willy Tarreau17200dd2022-08-09 08:40:08 +02001412 const char *func = NULL;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001413 int direction = 0; // neither alloc nor free (e.g. realloc)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001414
Willy Tarreau40e952f2022-05-05 14:39:51 +02001415 if (!ptr->size && !ptr->calls && !ctx->show_all)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001416 continue;
1417
1418 /* basename only */
Willy Tarreaud96d2142022-09-06 08:05:59 +02001419 for (p = name = ptr->caller.file; *p; p++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001420 if (*p == '/')
1421 name = p + 1;
1422 }
1423
Willy Tarreaud96d2142022-09-06 08:05:59 +02001424 func = ptr->caller.func;
Willy Tarreau17200dd2022-08-09 08:40:08 +02001425
Willy Tarreaud96d2142022-09-06 08:05:59 +02001426 switch (ptr->caller.what) {
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001427 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; direction = 1; break;
1428 case MEM_STATS_TYPE_FREE: type = "FREE"; direction = -1; break;
1429 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; direction = 1; break;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001430 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001431 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; direction = 1; break;
1432 case MEM_STATS_TYPE_P_ALLOC: type = "P_ALLOC"; direction = 1; if (ptr->extra) info = ((const struct pool_head *)ptr->extra)->name; break;
1433 case MEM_STATS_TYPE_P_FREE: type = "P_FREE"; direction = -1; if (ptr->extra) info = ((const struct pool_head *)ptr->extra)->name; break;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001434 default: type = "UNSET"; break;
1435 }
1436
1437 //chunk_printf(&trash,
1438 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1439 // name, ptr->line, type,
1440 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1441 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1442
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001443 /* only match requested prefixes */
1444 if (pfx && (!info || strncmp(info, pfx, strlen(pfx)) != 0))
1445 continue;
1446
Willy Tarreau17200dd2022-08-09 08:40:08 +02001447 chunk_reset(&trash);
1448 if (ctx->show_all)
1449 chunk_appendf(&trash, "%s(", func);
1450
Willy Tarreaud96d2142022-09-06 08:05:59 +02001451 chunk_appendf(&trash, "%s:%d", name, ptr->caller.line);
Willy Tarreau17200dd2022-08-09 08:40:08 +02001452
1453 if (ctx->show_all)
1454 chunk_appendf(&trash, ")");
1455
Willy Tarreau4a426e22022-08-09 08:51:08 +02001456 while (trash.data < ctx->width)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001457 trash.area[trash.data++] = ' ';
Willy Tarreau17200dd2022-08-09 08:40:08 +02001458
Willy Tarreau55c950b2022-08-09 08:15:27 +02001459 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu %s\n",
Willy Tarreaua6026a02020-07-02 09:14:48 +02001460 type,
1461 (unsigned long)ptr->size, (unsigned long)ptr->calls,
Willy Tarreau55c950b2022-08-09 08:15:27 +02001462 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0),
1463 info ? info : "");
Willy Tarreaua6026a02020-07-02 09:14:48 +02001464
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001465 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau40e952f2022-05-05 14:39:51 +02001466 ctx->start = ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001467 ret = 0;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001468 goto end;
1469 }
1470 if (direction > 0) {
1471 ctx->tot_size += (ulong)ptr->size;
1472 ctx->tot_calls += (ulong)ptr->calls;
1473 }
1474 else if (direction < 0) {
1475 ctx->tot_size -= (ulong)ptr->size;
1476 ctx->tot_calls += (ulong)ptr->calls;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001477 }
1478 }
1479
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001480 /* now dump a summary */
1481 chunk_reset(&trash);
1482 chunk_appendf(&trash, "Total");
1483 while (trash.data < ctx->width)
1484 trash.area[trash.data++] = ' ';
1485
1486 chunk_appendf(&trash, "%7s size: %12ld calls: %9lu size/call: %6ld %s\n",
1487 "BALANCE",
1488 ctx->tot_size, ctx->tot_calls,
1489 (long)(ctx->tot_calls ? (ctx->tot_size / ctx->tot_calls) : 0),
1490 "(excl. realloc)");
1491
1492 if (applet_putchk(appctx, &trash) == -1) {
1493 ctx->start = ptr;
1494 ret = 0;
1495 goto end;
1496 }
Willy Tarreaua6026a02020-07-02 09:14:48 +02001497 end:
1498 return ret;
1499}
1500
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001501/* release the "show pools" context */
1502static void debug_release_memstats(struct appctx *appctx)
1503{
1504 struct dev_mem_ctx *ctx = appctx->svcctx;
1505
1506 ha_free(&ctx->match);
1507}
Willy Tarreaua6026a02020-07-02 09:14:48 +02001508#endif
1509
Willy Tarreauc7091d82019-05-17 10:08:49 +02001510#ifndef USE_THREAD_DUMP
1511
1512/* This function dumps all threads' state to the trash. This version is the
1513 * most basic one, which doesn't inspect other threads.
1514 */
1515void ha_thread_dump_all_to_trash()
1516{
1517 unsigned int thr;
1518
1519 for (thr = 0; thr < global.nbthread; thr++)
1520 ha_thread_dump(&trash, thr, tid);
1521}
1522
1523#else /* below USE_THREAD_DUMP is set */
1524
Willy Tarreauc7091d82019-05-17 10:08:49 +02001525/* ID of the thread requesting the dump */
1526static unsigned int thread_dump_tid;
1527
1528/* points to the buffer where the dump functions should write. It must
1529 * have already been initialized by the requester. Nothing is done if
1530 * it's NULL.
1531 */
1532struct buffer *thread_dump_buffer = NULL;
1533
Willy Tarreau00c27b52022-07-01 19:08:56 +02001534/* initiates a thread dump */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001535void ha_thread_dump_all_to_trash()
1536{
Willy Tarreau039972b2022-07-01 19:37:42 +02001537 unsigned int old;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001538
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001539 /* initiate a dump starting from first thread. Use a CAS so that we do
1540 * not wait if we're not the first one, but we wait for a previous dump
1541 * to finish.
Willy Tarreau67297262022-07-13 08:59:39 +02001542 */
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001543 while (1) {
1544 old = 0;
1545 if (HA_ATOMIC_CAS(&thread_dump_state, &old, THREAD_DUMP_FSYNC))
1546 break;
1547 ha_thread_relax();
1548 }
Willy Tarreauc7091d82019-05-17 10:08:49 +02001549 thread_dump_buffer = &trash;
1550 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001551 ha_tkillall(DEBUGSIG);
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001552
1553 /* the call above contains a raise() so we're certain to return after
1554 * returning from the sighandler, hence when the dump is complete.
1555 */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001556}
1557
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001558/* handles DEBUGSIG to dump the state of the thread it's working on. This is
1559 * appended at the end of thread_dump_buffer which must be protected against
1560 * reentrance from different threads (a thread-local buffer works fine).
1561 */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001562void debug_handler(int sig, siginfo_t *si, void *arg)
1563{
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001564 int harmless = is_thread_harmless();
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001565 int running = 0;
1566 uint prev;
Willy Tarreau00c27b52022-07-01 19:08:56 +02001567 uint next;
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001568
Willy Tarreau82aafc42020-03-03 08:31:34 +01001569 /* first, let's check it's really for us and that we didn't just get
1570 * a spurious DEBUGSIG.
1571 */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001572 if (!_HA_ATOMIC_LOAD(&thread_dump_state))
Willy Tarreau82aafc42020-03-03 08:31:34 +01001573 return;
1574
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001575 /* There are 5 phases in the dump process:
1576 * 1- wait for all threads to sync or the first one to start
1577 * 2- wait for our turn, i.e. when tid appears in lower bits.
1578 * 3- perform the action if our tid is there
1579 * 4- pass tid to the number of the next thread to dump or
1580 * reset running counter if we're last one.
1581 * 5- wait for running to be zero and decrement the count
Willy Tarreauc7091d82019-05-17 10:08:49 +02001582 */
1583
1584 /* wait for all previous threads to finish first */
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001585 if (!harmless)
1586 thread_harmless_now();
1587
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001588 if (HA_ATOMIC_FETCH_ADD(&thread_dump_state, 1) == THREAD_DUMP_FSYNC) {
1589 /* the first one which lands here is responsible for constantly
1590 * recounting the number of active theads and switching from
1591 * SYNC to DUMP.
1592 */
1593 while (1) {
1594 int first = -1; // first tid to dump
1595 int thr;
1596
1597 running = 0;
1598 for (thr = 0; thr < global.nbthread; thr++) {
1599 if (ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit) {
1600 running++;
1601 if (first < 0)
1602 first = thr;
1603 }
1604 }
1605
1606 if ((HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_TMASK) == running) {
1607 /* all threads are there, let's try to start */
1608 prev = THREAD_DUMP_FSYNC | running;
1609 next = (running << 16) | first;
1610 if (HA_ATOMIC_CAS(&thread_dump_state, &prev, next))
1611 break;
1612 /* it failed! maybe a thread appeared late (e.g. during boot), let's
1613 * recount.
1614 */
1615 }
1616 ha_thread_relax();
1617 }
1618 }
1619
1620 /* all threads: let's wait for the SYNC flag to disappear; tid is reset at
1621 * the same time to the first valid tid to dump and pmask will reflect the
1622 * number of participants.
1623 */
1624 while (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_FSYNC)
1625 ha_thread_relax();
1626
1627 /* wait for our turn */
1628 while ((HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_TMASK) != tid)
Willy Tarreauc7091d82019-05-17 10:08:49 +02001629 ha_thread_relax();
1630
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001631 if (!harmless)
Willy Tarreau9debe0f2023-01-19 18:52:31 +01001632 thread_harmless_end_sig();
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001633
Willy Tarreauc7091d82019-05-17 10:08:49 +02001634 /* dump if needed */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001635 if (thread_dump_buffer)
1636 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1637
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001638 /* figure which is the next thread ID to dump among enabled ones. Note
1639 * that this relies on the fact that we're not creating new threads in
1640 * the middle of a dump, which is normally granted by the harmless bits
1641 * anyway.
1642 */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001643 for (next = tid + 1; next < global.nbthread; next++) {
1644 if (unlikely(next >= MAX_THREADS)) {
1645 /* just to please gcc 6.5 who guesses the ranges wrong. */
1646 continue;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001647 }
Willy Tarreau00c27b52022-07-01 19:08:56 +02001648
1649 if (ha_thread_info[next].tg &&
1650 ha_thread_info[next].tg->threads_enabled & ha_thread_info[next].ltid_bit)
1651 break;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001652 }
1653
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001654 /* if there are threads left to dump, we atomically set the next one,
1655 * otherwise we'll clear dump and set the thread part to the number of
1656 * threads that need to disappear.
1657 */
1658 if (next < global.nbthread) {
1659 next = (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_PMASK) | next;
1660 HA_ATOMIC_STORE(&thread_dump_state, next);
1661 } else {
Willy Tarreau00c27b52022-07-01 19:08:56 +02001662 thread_dump_buffer = NULL; // was the last one
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001663 running = (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_PMASK) >> 16;
1664 HA_ATOMIC_STORE(&thread_dump_state, running);
1665 }
Willy Tarreau00c27b52022-07-01 19:08:56 +02001666
1667 /* now wait for all others to finish dumping: the lowest part will turn
Willy Tarreau9debe0f2023-01-19 18:52:31 +01001668 * to zero. Then all others decrement the done part. We must not change
1669 * the harmless status anymore because one of the other threads might
1670 * have been interrupted in thread_isolate() waiting for all others to
1671 * become harmless, and changing the situation here would break that
1672 * promise.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001673 */
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001674
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001675 /* wait for everyone to finish*/
1676 while (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_PMASK)
Willy Tarreauc0773622019-07-31 19:15:45 +02001677 ha_thread_relax();
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001678
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001679 /* we're gone. Past this point anything can happen including another
1680 * thread trying to re-trigger a dump, so thread_dump_buffer and
1681 * thread_dump_tid may become invalid immediately after this call.
1682 */
1683 HA_ATOMIC_SUB(&thread_dump_state, 1);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001684
1685 /* mark the current thread as stuck to detect it upon next invocation
1686 * if it didn't move.
1687 */
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001688 if (!harmless &&
Willy Tarreaue7475c82022-06-20 09:23:24 +02001689 !(_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_SLEEPING))
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001690 _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_STUCK);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001691}
1692
1693static int init_debug_per_thread()
1694{
1695 sigset_t set;
1696
1697 /* unblock the DEBUGSIG signal we intend to use */
1698 sigemptyset(&set);
1699 sigaddset(&set, DEBUGSIG);
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001700#if defined(DEBUG_DEV)
1701 sigaddset(&set, SIGRTMAX);
1702#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +02001703 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1704 return 1;
1705}
1706
1707static int init_debug()
1708{
1709 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001710 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001711
Willy Tarreau0214b452020-03-04 06:01:40 +01001712 /* calling backtrace() will access libgcc at runtime. We don't want to
1713 * do it after the chroot, so let's perform a first call to have it
1714 * ready in memory for later use.
1715 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001716 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001717 sa.sa_handler = NULL;
1718 sa.sa_sigaction = debug_handler;
1719 sigemptyset(&sa.sa_mask);
1720 sa.sa_flags = SA_SIGINFO;
1721 sigaction(DEBUGSIG, &sa, NULL);
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001722
1723#if defined(DEBUG_DEV)
1724 sa.sa_handler = NULL;
1725 sa.sa_sigaction = debug_delay_inj_sighandler;
1726 sigemptyset(&sa.sa_mask);
1727 sa.sa_flags = SA_SIGINFO;
1728 sigaction(SIGRTMAX, &sa, NULL);
1729#endif
Christopher Fauletfc633b62020-11-06 15:24:23 +01001730 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001731}
1732
1733REGISTER_POST_CHECK(init_debug);
1734REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1735
1736#endif /* USE_THREAD_DUMP */
1737
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001738/* register cli keywords */
1739static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001740 {{ "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 +01001741 {{ "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 +02001742 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaudadf00e2022-07-15 08:25:03 +02001743 {{ "debug", "dev", "deadlock", NULL }, "debug dev deadlock [nbtask] : deadlock between this number of tasks", debug_parse_cli_deadlock, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001744 {{ "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 +02001745#if defined(DEBUG_DEV)
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001746 {{ "debug", "dev", "delay-inj", NULL },"debug dev delay-inj <inter> <count> : inject random delays into threads", debug_parse_delay_inj, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001747 {{ "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 +02001748#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001749 {{ "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 +02001750 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau111c7832022-11-30 17:51:47 +01001751 {{ "debug", "dev", "hash", NULL }, "debug dev hash [msg] : return msg hashed if anon is set", debug_parse_cli_hash, NULL, NULL, NULL, 0 },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001752 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1753 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1754 {{ "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 +02001755#if defined(DEBUG_MEM_STATS)
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001756 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all|match ...]: dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, debug_release_memstats, NULL, 0 },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001757#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001758 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1759 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1760 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1761 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1762 {{ "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 +01001763 {{ "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 +02001764 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Erwan Le Goas54966df2022-09-14 17:24:22 +02001765
Willy Tarreau6ab7b212021-12-28 09:57:10 +01001766#if defined(HA_HAVE_DUMP_LIBS)
1767 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1768#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001769 {{ "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 +02001770 {{},}
1771}};
1772
1773INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);