blob: 6f691d9fef9bcf3b7d8009838d7a56c2ab24bca1 [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 Tarreaub30ced32023-05-04 11:30:55 +0200201 !!(ha_thread_ctx[thr].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 Tarreaub30ced32023-05-04 11:30:55 +0200204 " harmless=%d isolated=%d",
Willy Tarreau03f9b352022-06-27 16:02:24 +0200205 !!(_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp-1].threads_harmless) & thr_bit),
Willy Tarreaub30ced32023-05-04 11:30:55 +0200206 isolated_thread == thr);
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);
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200301 }
Willy Tarreaucb01f5d2023-05-04 16:28:30 +0200302
303 /* we may need to terminate the current line */
304 if (*b_peek(buf, b_data(buf)-1) != '\n')
Christopher Faulet471425f2020-07-24 19:08:05 +0200305 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200306#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200307}
308
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200309/* This function dumps all profiling settings. It returns 0 if the output
310 * buffer is full and it needs to be called again, otherwise non-zero.
311 */
312static int cli_io_handler_show_threads(struct appctx *appctx)
313{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200314 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200315 int thr;
316
Christopher Faulet87633c32023-04-03 18:32:50 +0200317 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +0200318 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200319 return 1;
320
321 if (appctx->st0)
322 thr = appctx->st1;
323 else
324 thr = 0;
325
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200326 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200327 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200328
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200329 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200330 /* failed, try again */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200331 appctx->st1 = thr;
332 return 0;
333 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200334 return 1;
335}
336
Willy Tarreau6ab7b212021-12-28 09:57:10 +0100337#if defined(HA_HAVE_DUMP_LIBS)
338/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
339static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
340{
341 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
342 return 1;
343
344 chunk_reset(&trash);
345 if (dump_libs(&trash, 1))
346 return cli_msg(appctx, LOG_INFO, trash.area);
347 else
348 return 0;
349}
350#endif
351
Willy Tarreau56131ca2019-05-20 13:48:29 +0200352/* dumps a state of all threads into the trash and on fd #2, then aborts. */
353void ha_panic()
354{
Willy Tarreau55433f92022-07-15 12:48:58 +0200355 if (HA_ATOMIC_FETCH_ADD(&panic_started, 1) != 0) {
356 /* a panic dump is already in progress, let's not disturb it,
357 * we'll be called via signal DEBUGSIG. By returning we may be
358 * able to leave a current signal handler (e.g. WDT) so that
359 * this will ensure more reliable signal delivery.
360 */
361 return;
362 }
Willy Tarreau56131ca2019-05-20 13:48:29 +0200363 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200364 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200365 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100366 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200367 for (;;)
368 abort();
369}
370
Willy Tarreau06e66c82022-03-02 15:52:03 +0100371/* Complain with message <msg> on stderr. If <counter> is not NULL, it is
372 * atomically incremented, and the message is only printed when the counter
373 * was zero, so that the message is only printed once. <taint> is only checked
374 * on bit 1, and will taint the process either for a bug (2) or warn (0).
375 */
376void complain(int *counter, const char *msg, int taint)
377{
378 if (counter && _HA_ATOMIC_FETCH_ADD(counter, 1))
379 return;
380 DISGUISE(write(2, msg, strlen(msg)));
381 if (taint & 2)
382 mark_tainted(TAINTED_BUG);
383 else
384 mark_tainted(TAINTED_WARN);
385}
386
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200387/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
388static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
389{
390 int code = atoi(args[3]);
391
392 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
393 return 1;
394
Willy Tarreau4781b152021-04-06 13:53:36 +0200395 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200396 exit(code);
397 return 1;
398}
399
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100400/* parse a "debug dev bug" command. It always returns 1, though it should never return.
401 * Note: we make sure not to make the function static so that it appears in the trace.
402 */
403int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
404{
405 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
406 return 1;
407
Willy Tarreau4781b152021-04-06 13:53:36 +0200408 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100409 BUG_ON(one > zero);
410 return 1;
411}
412
Willy Tarreau305cfbd2022-02-25 08:52:39 +0100413/* parse a "debug dev warn" command. It always returns 1.
414 * Note: we make sure not to make the function static so that it appears in the trace.
415 */
416int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
417{
418 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
419 return 1;
420
421 _HA_ATOMIC_INC(&debug_commands_issued);
422 WARN_ON(one > zero);
423 return 1;
424}
425
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100426/* parse a "debug dev check" command. It always returns 1.
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100427 * Note: we make sure not to make the function static so that it appears in the trace.
428 */
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100429int debug_parse_cli_check(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100430{
431 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
432 return 1;
433
434 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100435 CHECK_IF(one > zero);
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100436 return 1;
437}
438
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200439/* parse a "debug dev close" command. It always returns 1. */
440static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
441{
442 int fd;
443
444 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
445 return 1;
446
Willy Tarreau9d008692019-08-09 11:21:01 +0200447 if (!*args[3])
448 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200449
450 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200451 if (fd < 0 || fd >= global.maxsock)
452 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200453
Willy Tarreau9d008692019-08-09 11:21:01 +0200454 if (!fdtab[fd].owner)
455 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200456
Willy Tarreau4781b152021-04-06 13:53:36 +0200457 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200458 fd_delete(fd);
459 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200460}
461
Willy Tarreaudadf00e2022-07-15 08:25:03 +0200462/* this is meant to cause a deadlock when more than one task is running it or when run twice */
463static struct task *debug_run_cli_deadlock(struct task *task, void *ctx, unsigned int state)
464{
465 static HA_SPINLOCK_T lock __maybe_unused;
466
467 HA_SPIN_LOCK(OTHER_LOCK, &lock);
468 return NULL;
469}
470
471/* parse a "debug dev deadlock" command. It always returns 1. */
472static int debug_parse_cli_deadlock(char **args, char *payload, struct appctx *appctx, void *private)
473{
474 int tasks;
475
476 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
477 return 1;
478
479 _HA_ATOMIC_INC(&debug_commands_issued);
480 for (tasks = atoi(args[3]); tasks > 0; tasks--) {
481 struct task *t = task_new_on(tasks % global.nbthread);
482 if (!t)
483 continue;
484 t->process = debug_run_cli_deadlock;
485 t->context = NULL;
486 task_wakeup(t, TASK_WOKEN_INIT);
487 }
488
489 return 1;
490}
491
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200492/* parse a "debug dev delay" command. It always returns 1. */
493static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
494{
495 int delay = atoi(args[3]);
496
497 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
498 return 1;
499
Willy Tarreau4781b152021-04-06 13:53:36 +0200500 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200501 usleep((long)delay * 1000);
502 return 1;
503}
504
505/* parse a "debug dev log" command. It always returns 1. */
506static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
507{
508 int arg;
509
510 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
511 return 1;
512
Willy Tarreau4781b152021-04-06 13:53:36 +0200513 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200514 chunk_reset(&trash);
515 for (arg = 3; *args[arg]; arg++) {
516 if (arg > 3)
517 chunk_strcat(&trash, " ");
518 chunk_strcat(&trash, args[arg]);
519 }
520
521 send_log(NULL, LOG_INFO, "%s\n", trash.area);
522 return 1;
523}
524
525/* parse a "debug dev loop" command. It always returns 1. */
526static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
527{
528 struct timeval deadline, curr;
529 int loop = atoi(args[3]);
Willy Tarreaue5e62232023-05-04 11:50:26 +0200530 int isolate;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200531
532 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
533 return 1;
534
Willy Tarreaue5e62232023-05-04 11:50:26 +0200535 isolate = strcmp(args[4], "isolated") == 0;
536
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200538 gettimeofday(&curr, NULL);
539 tv_ms_add(&deadline, &curr, loop);
540
Willy Tarreaue5e62232023-05-04 11:50:26 +0200541 if (isolate)
542 thread_isolate();
543
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200544 while (tv_ms_cmp(&curr, &deadline) < 0)
545 gettimeofday(&curr, NULL);
546
Willy Tarreaue5e62232023-05-04 11:50:26 +0200547 if (isolate)
548 thread_release();
549
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200550 return 1;
551}
552
553/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
554static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
555{
556 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
557 return 1;
558
Willy Tarreau4781b152021-04-06 13:53:36 +0200559 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200560 ha_panic();
561 return 1;
562}
563
564/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200565#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200566static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
567{
Willy Tarreau368bff42019-12-06 17:18:28 +0100568 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200569 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100570 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200571
572 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
573 return 1;
574
Willy Tarreau4781b152021-04-06 13:53:36 +0200575 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200576 chunk_reset(&trash);
577 for (arg = 3; *args[arg]; arg++) {
578 if (arg > 3)
579 chunk_strcat(&trash, " ");
580 chunk_strcat(&trash, args[arg]);
581 }
582
Willy Tarreau368bff42019-12-06 17:18:28 +0100583 thread_isolate();
584 if (pipe(pipefd) < 0)
585 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200586
Willy Tarreau38247432022-04-26 10:24:14 +0200587 if (fd_set_cloexec(pipefd[0]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100588 goto fail_fcntl;
589
Willy Tarreau38247432022-04-26 10:24:14 +0200590 if (fd_set_cloexec(pipefd[1]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100591 goto fail_fcntl;
592
593 pid = fork();
594
595 if (pid < 0)
596 goto fail_fork;
597 else if (pid == 0) {
598 /* child */
599 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
600
601 close(0);
602 dup2(pipefd[1], 1);
603 dup2(pipefd[1], 2);
604
605 cmd[2] = trash.area;
606 execvp(cmd[0], cmd);
607 printf("execvp() failed\n");
608 exit(1);
609 }
610
611 /* parent */
612 thread_release();
613 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200614 chunk_reset(&trash);
615 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100616 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
617 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200618 break;
619 trash.data += ret;
620 if (trash.data + 20 == trash.size) {
621 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
622 break;
623 }
624 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100625 close(pipefd[0]);
626 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200627 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200628 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100629
630 fail_fork:
631 fail_fcntl:
632 close(pipefd[0]);
633 close(pipefd[1]);
634 fail_pipe:
635 thread_release();
636 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200637}
Willy Tarreaubd3b44e2023-03-09 08:25:01 +0100638
639/* handles SIGRTMAX to inject random delays on the receiving thread in order
640 * to try to increase the likelihood to reproduce inter-thread races. The
641 * signal is periodically sent by a task initiated by "debug dev delay-inj".
642 */
643void debug_delay_inj_sighandler(int sig, siginfo_t *si, void *arg)
644{
645 volatile int i = statistical_prng_range(10000);
646
647 while (i--)
648 __ha_cpu_relax();
649}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200650#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200651
652/* parse a "debug dev hex" command. It always returns 1. */
653static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
654{
655 unsigned long start, len;
656
657 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
658 return 1;
659
Willy Tarreau9d008692019-08-09 11:21:01 +0200660 if (!*args[3])
661 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200662
663 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200664 if (!start)
665 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200666
Willy Tarreau4781b152021-04-06 13:53:36 +0200667 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200668
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200669 /* by default, dump ~128 till next block of 16 */
670 len = strtoul(args[4], NULL, 0);
671 if (!len)
672 len = ((start + 128) & -16) - start;
673
674 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200675 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200676 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200677 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200678}
679
Willy Tarreau48129be2021-05-04 18:40:50 +0200680/* parse a "debug dev sym <addr>" command. It always returns 1. */
681static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
682{
683 unsigned long addr;
684
685 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
686 return 1;
687
688 if (!*args[3])
689 return cli_err(appctx, "Missing memory address to be resolved.\n");
690
691 _HA_ATOMIC_INC(&debug_commands_issued);
692
693 addr = strtoul(args[3], NULL, 0);
694 chunk_printf(&trash, "%#lx resolves to ", addr);
695 resolve_sym_name(&trash, NULL, (const void *)addr);
696 chunk_appendf(&trash, "\n");
697
698 return cli_msg(appctx, LOG_INFO, trash.area);
699}
700
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200701/* parse a "debug dev tkill" command. It always returns 1. */
702static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
703{
704 int thr = 0;
705 int sig = SIGABRT;
706
707 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
708 return 1;
709
710 if (*args[3])
711 thr = atoi(args[3]);
712
Willy Tarreau9d008692019-08-09 11:21:01 +0200713 if (thr < 0 || thr > global.nbthread)
714 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200715
716 if (*args[4])
717 sig = atoi(args[4]);
718
Willy Tarreau4781b152021-04-06 13:53:36 +0200719 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200720 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200721 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200722 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200723 raise(sig);
724 return 1;
725}
726
Erwan Le Goas54966df2022-09-14 17:24:22 +0200727/* hashes 'word' in "debug dev hash 'word' ". */
728static int debug_parse_cli_hash(char **args, char *payload, struct appctx *appctx, void *private)
729{
730 char *msg = NULL;
731
732 cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", HA_ANON_CLI(args[3])));
733 return 1;
734}
735
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100736/* parse a "debug dev write" command. It always returns 1. */
737static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
738{
739 unsigned long len;
740
741 if (!*args[3])
742 return cli_err(appctx, "Missing output size.\n");
743
744 len = strtoul(args[3], NULL, 0);
745 if (len >= trash.size)
746 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
747
Willy Tarreau4781b152021-04-06 13:53:36 +0200748 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100749
750 chunk_reset(&trash);
751 trash.data = len;
752 memset(trash.area, '.', trash.data);
753 trash.area[trash.data] = 0;
754 for (len = 64; len < trash.data; len += 64)
755 trash.area[len] = '\n';
756 return cli_msg(appctx, LOG_INFO, trash.area);
757}
758
Willy Tarreau68680bb2019-10-23 17:23:25 +0200759/* parse a "debug dev stream" command */
760/*
761 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
762 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
763 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
764 * [sif.s[=<state>]] [sib.s[=<state>]]
765 */
766static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
767{
Willy Tarreau0698c802022-05-11 14:09:57 +0200768 struct stream *s = appctx_strm(appctx);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200769 int arg;
770 void *ptr;
771 int size;
772 const char *word, *end;
773 struct ist name;
774 char *msg = NULL;
775 char *endarg;
776 unsigned long long old, new;
777
778 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
779 return 1;
780
781 ptr = NULL; size = 0;
782
783 if (!*args[3]) {
784 return cli_err(appctx,
Willy Tarreaudd014482023-05-02 16:37:13 +0200785 "Usage: debug dev stream [ strm=<ptr> ] { <obj> <op> <value> | wake }*\n"
786 " <obj> = { strm.f | strm.x | scf.s | scb.s | txn.f | req.f | res.f }\n"
Willy Tarreau68680bb2019-10-23 17:23:25 +0200787 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
788 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
789 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
790 );
791 }
792
Willy Tarreau4781b152021-04-06 13:53:36 +0200793 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200794 for (arg = 3; *args[arg]; arg++) {
795 old = 0;
796 end = word = args[arg];
797 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
798 end++;
799 name = ist2(word, end - word);
800 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200801 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200802 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200803 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200804 } else if (isteq(name, ist("strm.x"))) {
805 ptr = (!s || !may_access(s)) ? NULL : &s->conn_exp; size = sizeof(s->conn_exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200806 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200807 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200808 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200809 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200810 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200811 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200812 } else if (isteq(name, ist("scf.s"))) {
813 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scf->state);
814 } else if (isteq(name, ist("scb.s"))) {
815 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scb->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200816 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200817 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200818 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
819 continue;
820 } else
821 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
822
823 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200824 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200825 if (size == 8)
826 old = read_u64(ptr);
827 else if (size == 4)
828 old = read_u32(ptr);
829 else if (size == 2)
830 old = read_u16(ptr);
831 else
832 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200833 } else {
834 memprintf(&msg,
835 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
836 msg ? msg : "", ptr, (int)(end - word), word);
837 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200838 }
839
840 /* parse the new value . */
841 new = strtoll(end + 1, &endarg, 0);
842 if (end[1] && *endarg) {
843 if (strcmp(end + 1, "now") == 0)
844 new = now_ms;
845 else {
846 memprintf(&msg,
847 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
848 msg ? msg : "", end + 1, (int)(end - word), word);
849 continue;
850 }
851 }
852
853 switch (*end) {
854 case '\0': /* show */
855 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
856 new = old; // do not change the value
857 break;
858
859 case '=': /* set */
860 break;
861
862 case '^': /* XOR */
863 new = old ^ new;
864 break;
865
866 case '+': /* OR */
867 new = old | new;
868 break;
869
870 case '-': /* AND NOT */
871 new = old & ~new;
872 break;
873
874 default:
875 break;
876 }
877
878 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200879 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200880 if (size == 8)
881 write_u64(ptr, new);
882 else if (size == 4)
883 write_u32(ptr, new);
884 else if (size == 2)
885 write_u16(ptr, new);
886 else
887 *(uint8_t *)ptr = new;
888 }
889 }
890
891 if (msg && *msg)
892 return cli_dynmsg(appctx, LOG_INFO, msg);
893 return 1;
894}
895
Willy Tarreau98679872023-05-03 11:22:45 +0200896/* parse a "debug dev stream" command */
897/*
898 * debug dev task <ptr> [ "wake" | "expire" | "kill" ]
899 * Show/change status of a task/tasklet
900 */
901static int debug_parse_cli_task(char **args, char *payload, struct appctx *appctx, void *private)
902{
903 const struct ha_caller *caller;
904 struct task *t;
905 char *endarg;
906 char *msg;
907 void *ptr;
908 int ret = 1;
909 int task_ok;
910 int arg;
911
912 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
913 return 1;
914
915 /* parse the pointer value */
916 ptr = (void *)strtoll(args[3], &endarg, 0);
917 if (!*args[3] || *endarg)
918 goto usage;
919
920 _HA_ATOMIC_INC(&debug_commands_issued);
921
922 /* everything below must run under thread isolation till reaching label "leave" */
923 thread_isolate();
924
925 /* struct tasklet is smaller than struct task and is sufficient to check
926 * the TASK_COMMON part.
927 */
928 if (!may_access(ptr) || !may_access(ptr + sizeof(struct tasklet) - 1) ||
929 ((const struct tasklet *)ptr)->tid < -1 ||
930 ((const struct tasklet *)ptr)->tid >= (int)MAX_THREADS) {
931 ret = cli_err(appctx, "The designated memory area doesn't look like a valid task/tasklet\n");
932 goto leave;
933 }
934
935 t = ptr;
936 caller = t->caller;
937 msg = NULL;
938 task_ok = may_access(t + sizeof(*t) - 1);
939
940 chunk_reset(&trash);
941 resolve_sym_name(&trash, NULL, (const void *)t->process);
942
943 /* we need to be careful here because we may dump a freed task that's
944 * still in the pool cache, containing garbage in pointers.
945 */
946 if (!*args[4]) {
947 memprintf(&msg, "%s%p: %s state=%#x tid=%d process=%s ctx=%p calls=%d last=%s:%d intl=%d",
948 msg ? msg : "", t, (t->state & TASK_F_TASKLET) ? "tasklet" : "task",
949 t->state, t->tid, trash.area, t->context, t->calls,
Willy Tarreauff508f12023-05-03 16:28:54 +0200950 caller && may_access(caller) && may_access(caller->func) && isalnum((uchar)*caller->func) ? caller->func : "0",
Willy Tarreau98679872023-05-03 11:22:45 +0200951 caller ? t->caller->line : 0,
952 (t->state & TASK_F_TASKLET) ? LIST_INLIST(&((const struct tasklet *)t)->list) : 0);
953
954 if (task_ok && !(t->state & TASK_F_TASKLET))
955 memprintf(&msg, "%s inrq=%d inwq=%d exp=%d nice=%d",
956 msg ? msg : "", task_in_rq(t), task_in_wq(t), t->expire, t->nice);
957
958 memprintf(&msg, "%s\n", msg ? msg : "");
959 }
960
961 for (arg = 4; *args[arg]; arg++) {
962 if (strcmp(args[arg], "expire") == 0) {
963 if (t->state & TASK_F_TASKLET) {
964 /* do nothing for tasklets */
965 }
966 else if (task_ok) {
967 /* unlink task and wake with timer flag */
968 __task_unlink_wq(t);
969 t->expire = now_ms;
970 task_wakeup(t, TASK_WOKEN_TIMER);
971 }
972 } else if (strcmp(args[arg], "wake") == 0) {
973 /* wake with all flags but init / timer */
974 if (t->state & TASK_F_TASKLET)
975 tasklet_wakeup((struct tasklet *)t);
976 else if (task_ok)
977 task_wakeup(t, TASK_WOKEN_ANY & ~(TASK_WOKEN_INIT|TASK_WOKEN_TIMER));
978 } else if (strcmp(args[arg], "kill") == 0) {
979 /* Kill the task. This is not idempotent! */
980 if (!(t->state & TASK_KILLED)) {
981 if (t->state & TASK_F_TASKLET)
982 tasklet_kill((struct tasklet *)t);
983 else if (task_ok)
984 task_kill(t);
985 }
986 } else {
987 thread_release();
988 goto usage;
989 }
990 }
991
992 if (msg && *msg)
993 ret = cli_dynmsg(appctx, LOG_INFO, msg);
994 leave:
995 thread_release();
996 return ret;
997 usage:
998 return cli_err(appctx,
999 "Usage: debug dev task <ptr> [ wake | expire | kill ]\n"
1000 " By default, dumps some info on task/tasklet <ptr>. 'wake' will wake it up\n"
1001 " with all conditions flags but init/exp. 'expire' will expire the entry, and\n"
1002 " 'kill' will kill it (warning: may crash since later not idempotent!). All\n"
1003 " changes may crash the process if performed on a wrong object!\n"
1004 );
1005}
1006
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001007#if defined(DEBUG_DEV)
1008static struct task *debug_delay_inj_task(struct task *t, void *ctx, unsigned int state)
1009{
1010 unsigned long *tctx = ctx; // [0] = interval, [1] = nbwakeups
1011 unsigned long inter = tctx[0];
1012 unsigned long count = tctx[1];
1013 unsigned long rnd;
1014
1015 if (inter)
1016 t->expire = tick_add(now_ms, inter);
1017 else
1018 task_wakeup(t, TASK_WOKEN_MSG);
1019
1020 /* wake a random thread */
1021 while (count--) {
1022 rnd = statistical_prng_range(global.nbthread);
1023 ha_tkill(rnd, SIGRTMAX);
1024 }
1025 return t;
1026}
1027
1028/* parse a "debug dev delay-inj" command
1029 * debug dev delay-inj <inter> <count>
1030 */
1031static int debug_parse_delay_inj(char **args, char *payload, struct appctx *appctx, void *private)
1032{
1033 unsigned long *tctx; // [0] = inter, [2] = count
1034 struct task *task;
1035
1036 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1037 return 1;
1038
1039 if (!*args[4])
1040 return cli_err(appctx, "Usage: debug dev delay-inj <inter_ms> <count>*\n");
1041
1042 _HA_ATOMIC_INC(&debug_commands_issued);
1043
1044 tctx = calloc(sizeof(*tctx), 2);
1045 if (!tctx)
1046 goto fail;
1047
1048 tctx[0] = atoi(args[3]);
1049 tctx[1] = atoi(args[4]);
1050
1051 task = task_new_here/*anywhere*/();
1052 if (!task)
1053 goto fail;
1054
1055 task->process = debug_delay_inj_task;
1056 task->context = tctx;
1057 task_wakeup(task, TASK_WOKEN_INIT);
1058 return 1;
1059
1060 fail:
1061 free(tctx);
1062 return cli_err(appctx, "Not enough memory");
1063}
1064#endif // DEBUG_DEV
1065
Willy Tarreau144f84a2021-03-02 16:09:26 +01001066static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +01001067{
1068 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1069 unsigned long inter = tctx[1];
1070 unsigned long rnd;
1071
1072 t->expire = tick_add(now_ms, inter);
1073
1074 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +01001075 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +01001076 if (rnd & 1) {
1077 rnd >>= 1;
1078 rnd %= tctx[0];
1079 rnd = tctx[rnd + 2];
1080
1081 if (rnd & 1)
1082 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
1083 else
1084 tasklet_wakeup((struct tasklet *)rnd);
1085 }
1086 return t;
1087}
1088
Willy Tarreau144f84a2021-03-02 16:09:26 +01001089static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +01001090{
1091 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1092 unsigned long rnd;
1093 int i;
1094
1095 /* wake up two random entries */
1096 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +01001097 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +01001098 rnd = tctx[rnd + 2];
1099
1100 if (rnd & 1)
1101 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
1102 else
1103 tasklet_wakeup((struct tasklet *)rnd);
1104 }
1105 return t;
1106}
1107
1108/* parse a "debug dev sched" command
1109 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
1110 */
1111static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
1112{
1113 int arg;
1114 void *ptr;
1115 int size;
1116 const char *word, *end;
1117 struct ist name;
1118 char *msg = NULL;
1119 char *endarg;
1120 unsigned long long new;
1121 unsigned long count = 0;
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001122 unsigned long thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001123 unsigned int inter = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001124 unsigned long i;
1125 int mode = 0; // 0 = tasklet; 1 = task
Willy Tarreaua5a44792020-11-29 17:12:15 +01001126 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1127
1128 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1129 return 1;
1130
1131 ptr = NULL; size = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001132
1133 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
1134 return cli_err(appctx,
1135 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001136 " <obj> = {count | tid | inter }\n"
Willy Tarreaua5a44792020-11-29 17:12:15 +01001137 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
1138 );
1139 }
1140
1141 mode = strcmp(args[3], "task") == 0;
1142
Willy Tarreau4781b152021-04-06 13:53:36 +02001143 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001144 for (arg = 4; *args[arg]; arg++) {
1145 end = word = args[arg];
1146 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
1147 end++;
1148 name = ist2(word, end - word);
1149 if (isteq(name, ist("count"))) {
1150 ptr = &count; size = sizeof(count);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001151 } else if (isteq(name, ist("tid"))) {
1152 ptr = &thrid; size = sizeof(thrid);
1153 } else if (isteq(name, ist("inter"))) {
1154 ptr = &inter; size = sizeof(inter);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001155 } else
1156 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
1157
1158 /* parse the new value . */
1159 new = strtoll(end + 1, &endarg, 0);
1160 if (end[1] && *endarg) {
1161 memprintf(&msg,
1162 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
1163 msg ? msg : "", end + 1, (int)(end - word), word);
1164 continue;
1165 }
1166
1167 /* write the new value */
1168 if (size == 8)
1169 write_u64(ptr, new);
1170 else if (size == 4)
1171 write_u32(ptr, new);
1172 else if (size == 2)
1173 write_u16(ptr, new);
1174 else
1175 *(uint8_t *)ptr = new;
1176 }
1177
1178 tctx = calloc(sizeof(*tctx), count + 2);
1179 if (!tctx)
1180 goto fail;
1181
1182 tctx[0] = (unsigned long)count;
1183 tctx[1] = (unsigned long)inter;
1184
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001185 if (thrid >= global.nbthread)
1186 thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001187
Willy Tarreaua5a44792020-11-29 17:12:15 +01001188 for (i = 0; i < count; i++) {
Willy Tarreaua5a44792020-11-29 17:12:15 +01001189 /* now, if poly or mask was set, tmask corresponds to the
1190 * valid thread mask to use, otherwise it remains zero.
1191 */
1192 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
1193 if (mode == 0) {
1194 struct tasklet *tl = tasklet_new();
1195
1196 if (!tl)
1197 goto fail;
1198
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001199 tl->tid = thrid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001200 tl->process = debug_tasklet_handler;
1201 tl->context = tctx;
1202 tctx[i + 2] = (unsigned long)tl;
1203 } else {
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001204 struct task *task = task_new_on(thrid);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001205
1206 if (!task)
1207 goto fail;
1208
1209 task->process = debug_task_handler;
1210 task->context = tctx;
1211 tctx[i + 2] = (unsigned long)task + 1;
1212 }
1213 }
1214
1215 /* start the tasks and tasklets */
1216 for (i = 0; i < count; i++) {
1217 unsigned long ctx = tctx[i + 2];
1218
1219 if (ctx & 1)
1220 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
1221 else
1222 tasklet_wakeup((struct tasklet *)ctx);
1223 }
1224
1225 if (msg && *msg)
1226 return cli_dynmsg(appctx, LOG_INFO, msg);
1227 return 1;
1228
1229 fail:
1230 /* free partially allocated entries */
1231 for (i = 0; tctx && i < count; i++) {
1232 unsigned long ctx = tctx[i + 2];
1233
1234 if (!ctx)
1235 break;
1236
1237 if (ctx & 1)
1238 task_destroy((struct task *)(ctx - 1));
1239 else
1240 tasklet_free((struct tasklet *)ctx);
1241 }
1242
1243 free(tctx);
1244 return cli_err(appctx, "Not enough memory");
1245}
1246
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001247/* CLI state for "debug dev fd" */
1248struct dev_fd_ctx {
1249 int start_fd;
1250};
1251
Willy Tarreau5be7c192022-01-24 20:26:09 +01001252/* CLI parser for the "debug dev fd" command. The current FD to restart from is
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001253 * stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001254 */
1255static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
1256{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001257 struct dev_fd_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1258
Willy Tarreau5be7c192022-01-24 20:26:09 +01001259 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1260 return 1;
1261
1262 /* start at fd #0 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001263 ctx->start_fd = 0;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001264 return 0;
1265}
1266
1267/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1268 * accessible from the process but not known from fdtab. The FD number to
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001269 * restart from is stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001270 */
1271static int debug_iohandler_fd(struct appctx *appctx)
1272{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001273 struct dev_fd_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001274 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau5be7c192022-01-24 20:26:09 +01001275 struct sockaddr_storage sa;
1276 struct stat statbuf;
1277 socklen_t salen, vlen;
1278 int ret1, ret2, port;
1279 char *addrstr;
1280 int ret = 1;
1281 int i, fd;
1282
Christopher Faulet87633c32023-04-03 18:32:50 +02001283 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02001284 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau5be7c192022-01-24 20:26:09 +01001285 goto end;
1286
1287 chunk_reset(&trash);
1288
1289 thread_isolate();
1290
1291 /* we have two inner loops here, one for the proxy, the other one for
1292 * the buffer.
1293 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001294 for (fd = ctx->start_fd; fd < global.maxsock; fd++) {
Willy Tarreau5be7c192022-01-24 20:26:09 +01001295 /* check for FD's existence */
1296 ret1 = fcntl(fd, F_GETFD, 0);
1297 if (ret1 == -1)
1298 continue; // not known to the process
1299 if (fdtab[fd].owner)
1300 continue; // well-known
1301
1302 /* OK we're seeing an orphan let's try to retrieve as much
1303 * information as possible about it.
1304 */
1305 chunk_printf(&trash, "%5d", fd);
1306
1307 if (fstat(fd, &statbuf) != -1) {
1308 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1309 isatty(fd) ? "tty.":
1310 S_ISREG(statbuf.st_mode) ? "file":
1311 S_ISDIR(statbuf.st_mode) ? "dir.":
1312 S_ISCHR(statbuf.st_mode) ? "chr.":
1313 S_ISBLK(statbuf.st_mode) ? "blk.":
1314 S_ISFIFO(statbuf.st_mode) ? "pipe":
1315 S_ISLNK(statbuf.st_mode) ? "link":
1316 S_ISSOCK(statbuf.st_mode) ? "sock":
1317#ifdef USE_EPOLL
1318 epoll_wait(fd, NULL, 0, 0) != -1 || errno != EBADF ? "epol":
1319#endif
1320 "????",
1321 (uint)statbuf.st_mode & 07777,
1322
1323 (ullong)statbuf.st_rdev,
1324 (ullong)statbuf.st_size,
1325 (ullong)statbuf.st_uid,
1326 (ullong)statbuf.st_gid,
1327
1328 (ullong)statbuf.st_dev,
1329 (ullong)statbuf.st_ino);
1330 }
1331
1332 chunk_appendf(&trash, " getfd=%s+%#x",
1333 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1334 ret1 &~ FD_CLOEXEC);
1335
1336 /* FD options */
1337 ret2 = fcntl(fd, F_GETFL, 0);
1338 if (ret2) {
1339 chunk_appendf(&trash, " getfl=%s",
1340 (ret1 & 3) >= 2 ? "O_RDWR" :
1341 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1342
1343 for (i = 2; i < 32; i++) {
1344 if (!(ret2 & (1UL << i)))
1345 continue;
1346 switch (1UL << i) {
1347 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1348 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1349 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1350 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1351 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001352#ifdef O_ASYNC
Willy Tarreau5be7c192022-01-24 20:26:09 +01001353 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001354#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001355#ifdef O_DIRECT
1356 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1357#endif
1358#ifdef O_NOATIME
1359 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1360#endif
1361 }
1362 }
1363 }
1364
1365 vlen = sizeof(ret2);
1366 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1367 if (ret1 != -1)
1368 chunk_appendf(&trash, " so_type=%d", ret2);
1369
1370 vlen = sizeof(ret2);
1371 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1372 if (ret1 != -1)
1373 chunk_appendf(&trash, " so_accept=%d", ret2);
1374
1375 vlen = sizeof(ret2);
1376 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1377 if (ret1 != -1)
1378 chunk_appendf(&trash, " so_error=%d", ret2);
1379
1380 salen = sizeof(sa);
1381 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1382 if (sa.ss_family == AF_INET)
1383 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1384 else if (sa.ss_family == AF_INET6)
1385 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1386 else
1387 port = 0;
1388 addrstr = sa2str(&sa, port, 0);
1389 chunk_appendf(&trash, " laddr=%s", addrstr);
1390 free(addrstr);
1391 }
1392
1393 salen = sizeof(sa);
1394 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1395 if (sa.ss_family == AF_INET)
1396 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1397 else if (sa.ss_family == AF_INET6)
1398 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1399 else
1400 port = 0;
1401 addrstr = sa2str(&sa, port, 0);
1402 chunk_appendf(&trash, " raddr=%s", addrstr);
1403 free(addrstr);
1404 }
1405
1406 chunk_appendf(&trash, "\n");
1407
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001408 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001409 ctx->start_fd = fd;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001410 ret = 0;
1411 break;
1412 }
1413 }
1414
1415 thread_release();
1416 end:
1417 return ret;
1418}
1419
Willy Tarreaua6026a02020-07-02 09:14:48 +02001420#if defined(DEBUG_MEM_STATS)
Willy Tarreau40e952f2022-05-05 14:39:51 +02001421
1422/* CLI state for "debug dev memstats" */
1423struct dev_mem_ctx {
1424 struct mem_stats *start, *stop; /* begin/end of dump */
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001425 char *match; /* non-null if a name prefix is specified */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001426 int show_all; /* show all entries if non-null */
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001427 int width; /* 1st column width */
1428 long tot_size; /* sum of alloc-free */
1429 ulong tot_calls; /* sum of calls */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001430};
1431
1432/* CLI parser for the "debug dev memstats" command. Sets a dev_mem_ctx shown above. */
Willy Tarreaua6026a02020-07-02 09:14:48 +02001433static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1434{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001435 struct dev_mem_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau334d0912022-11-30 16:50:48 +01001436 int arg;
Willy Tarreau40e952f2022-05-05 14:39:51 +02001437
Willy Tarreaua6026a02020-07-02 09:14:48 +02001438 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1439 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1440
1441 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1442 return 1;
1443
Willy Tarreau334d0912022-11-30 16:50:48 +01001444 for (arg = 3; *args[arg]; arg++) {
1445 if (strcmp(args[arg], "reset") == 0) {
1446 struct mem_stats *ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001447
Willy Tarreau334d0912022-11-30 16:50:48 +01001448 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1449 return 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001450
Willy Tarreau334d0912022-11-30 16:50:48 +01001451 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1452 _HA_ATOMIC_STORE(&ptr->calls, 0);
1453 _HA_ATOMIC_STORE(&ptr->size, 0);
1454 }
1455 return 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001456 }
Willy Tarreau334d0912022-11-30 16:50:48 +01001457 else if (strcmp(args[arg], "all") == 0) {
1458 ctx->show_all = 1;
1459 continue;
1460 }
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001461 else if (strcmp(args[arg], "match") == 0 && *args[arg + 1]) {
1462 ha_free(&ctx->match);
1463 ctx->match = strdup(args[arg + 1]);
1464 arg++;
1465 continue;
1466 }
Willy Tarreau334d0912022-11-30 16:50:48 +01001467 else
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001468 return cli_err(appctx, "Expects either 'reset', 'all', or 'match <pfx>'.\n");
Willy Tarreaua6026a02020-07-02 09:14:48 +02001469 }
1470
Willy Tarreaua6026a02020-07-02 09:14:48 +02001471 /* otherwise proceed with the dump from p0 to p1 */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001472 ctx->start = &__start_mem_stats;
1473 ctx->stop = &__stop_mem_stats;
Willy Tarreau4a426e22022-08-09 08:51:08 +02001474 ctx->width = 0;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001475 return 0;
1476}
1477
Willy Tarreau40e952f2022-05-05 14:39:51 +02001478/* CLI I/O handler for the "debug dev memstats" command using a dev_mem_ctx
1479 * found in appctx->svcctx. Dumps all mem_stats structs referenced by pointers
1480 * located between ->start and ->stop. Dumps all entries if ->show_all != 0,
1481 * otherwise only non-zero calls.
Willy Tarreaua6026a02020-07-02 09:14:48 +02001482 */
1483static int debug_iohandler_memstats(struct appctx *appctx)
1484{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001485 struct dev_mem_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001486 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001487 struct mem_stats *ptr;
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001488 const char *pfx = ctx->match;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001489 int ret = 1;
1490
Christopher Faulet87633c32023-04-03 18:32:50 +02001491 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02001492 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreaua6026a02020-07-02 09:14:48 +02001493 goto end;
1494
Willy Tarreau4a426e22022-08-09 08:51:08 +02001495 if (!ctx->width) {
1496 /* we don't know the first column's width, let's compute it
1497 * now based on a first pass on printable entries and their
1498 * expected width (approximated).
1499 */
1500 for (ptr = ctx->start; ptr != ctx->stop; ptr++) {
1501 const char *p, *name;
1502 int w = 0;
1503 char tmp;
1504
1505 if (!ptr->size && !ptr->calls && !ctx->show_all)
1506 continue;
1507
Willy Tarreaud96d2142022-09-06 08:05:59 +02001508 for (p = name = ptr->caller.file; *p; p++) {
Willy Tarreau4a426e22022-08-09 08:51:08 +02001509 if (*p == '/')
1510 name = p + 1;
1511 }
1512
1513 if (ctx->show_all)
Willy Tarreaud96d2142022-09-06 08:05:59 +02001514 w = snprintf(&tmp, 0, "%s(%s:%d) ", ptr->caller.func, name, ptr->caller.line);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001515 else
Willy Tarreaud96d2142022-09-06 08:05:59 +02001516 w = snprintf(&tmp, 0, "%s:%d ", name, ptr->caller.line);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001517
1518 if (w > ctx->width)
1519 ctx->width = w;
1520 }
1521 }
1522
Willy Tarreaua6026a02020-07-02 09:14:48 +02001523 /* we have two inner loops here, one for the proxy, the other one for
1524 * the buffer.
1525 */
Willy Tarreau4a426e22022-08-09 08:51:08 +02001526 for (ptr = ctx->start; ptr != ctx->stop; ptr++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001527 const char *type;
1528 const char *name;
1529 const char *p;
Willy Tarreau55c950b2022-08-09 08:15:27 +02001530 const char *info = NULL;
Willy Tarreau17200dd2022-08-09 08:40:08 +02001531 const char *func = NULL;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001532 int direction = 0; // neither alloc nor free (e.g. realloc)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001533
Willy Tarreau40e952f2022-05-05 14:39:51 +02001534 if (!ptr->size && !ptr->calls && !ctx->show_all)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001535 continue;
1536
1537 /* basename only */
Willy Tarreaud96d2142022-09-06 08:05:59 +02001538 for (p = name = ptr->caller.file; *p; p++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001539 if (*p == '/')
1540 name = p + 1;
1541 }
1542
Willy Tarreaud96d2142022-09-06 08:05:59 +02001543 func = ptr->caller.func;
Willy Tarreau17200dd2022-08-09 08:40:08 +02001544
Willy Tarreaud96d2142022-09-06 08:05:59 +02001545 switch (ptr->caller.what) {
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001546 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; direction = 1; break;
1547 case MEM_STATS_TYPE_FREE: type = "FREE"; direction = -1; break;
1548 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; direction = 1; break;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001549 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001550 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; direction = 1; break;
1551 case MEM_STATS_TYPE_P_ALLOC: type = "P_ALLOC"; direction = 1; if (ptr->extra) info = ((const struct pool_head *)ptr->extra)->name; break;
1552 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 +02001553 default: type = "UNSET"; break;
1554 }
1555
1556 //chunk_printf(&trash,
1557 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1558 // name, ptr->line, type,
1559 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1560 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1561
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001562 /* only match requested prefixes */
1563 if (pfx && (!info || strncmp(info, pfx, strlen(pfx)) != 0))
1564 continue;
1565
Willy Tarreau17200dd2022-08-09 08:40:08 +02001566 chunk_reset(&trash);
1567 if (ctx->show_all)
1568 chunk_appendf(&trash, "%s(", func);
1569
Willy Tarreaud96d2142022-09-06 08:05:59 +02001570 chunk_appendf(&trash, "%s:%d", name, ptr->caller.line);
Willy Tarreau17200dd2022-08-09 08:40:08 +02001571
1572 if (ctx->show_all)
1573 chunk_appendf(&trash, ")");
1574
Willy Tarreau4a426e22022-08-09 08:51:08 +02001575 while (trash.data < ctx->width)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001576 trash.area[trash.data++] = ' ';
Willy Tarreau17200dd2022-08-09 08:40:08 +02001577
Willy Tarreau55c950b2022-08-09 08:15:27 +02001578 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu %s\n",
Willy Tarreaua6026a02020-07-02 09:14:48 +02001579 type,
1580 (unsigned long)ptr->size, (unsigned long)ptr->calls,
Willy Tarreau55c950b2022-08-09 08:15:27 +02001581 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0),
1582 info ? info : "");
Willy Tarreaua6026a02020-07-02 09:14:48 +02001583
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001584 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau40e952f2022-05-05 14:39:51 +02001585 ctx->start = ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001586 ret = 0;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001587 goto end;
1588 }
1589 if (direction > 0) {
1590 ctx->tot_size += (ulong)ptr->size;
1591 ctx->tot_calls += (ulong)ptr->calls;
1592 }
1593 else if (direction < 0) {
1594 ctx->tot_size -= (ulong)ptr->size;
1595 ctx->tot_calls += (ulong)ptr->calls;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001596 }
1597 }
1598
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001599 /* now dump a summary */
1600 chunk_reset(&trash);
1601 chunk_appendf(&trash, "Total");
1602 while (trash.data < ctx->width)
1603 trash.area[trash.data++] = ' ';
1604
1605 chunk_appendf(&trash, "%7s size: %12ld calls: %9lu size/call: %6ld %s\n",
1606 "BALANCE",
1607 ctx->tot_size, ctx->tot_calls,
1608 (long)(ctx->tot_calls ? (ctx->tot_size / ctx->tot_calls) : 0),
1609 "(excl. realloc)");
1610
1611 if (applet_putchk(appctx, &trash) == -1) {
1612 ctx->start = ptr;
1613 ret = 0;
1614 goto end;
1615 }
Willy Tarreaua6026a02020-07-02 09:14:48 +02001616 end:
1617 return ret;
1618}
1619
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001620/* release the "show pools" context */
1621static void debug_release_memstats(struct appctx *appctx)
1622{
1623 struct dev_mem_ctx *ctx = appctx->svcctx;
1624
1625 ha_free(&ctx->match);
1626}
Willy Tarreaua6026a02020-07-02 09:14:48 +02001627#endif
1628
Willy Tarreauc7091d82019-05-17 10:08:49 +02001629#ifndef USE_THREAD_DUMP
1630
1631/* This function dumps all threads' state to the trash. This version is the
1632 * most basic one, which doesn't inspect other threads.
1633 */
1634void ha_thread_dump_all_to_trash()
1635{
1636 unsigned int thr;
1637
1638 for (thr = 0; thr < global.nbthread; thr++)
1639 ha_thread_dump(&trash, thr, tid);
1640}
1641
1642#else /* below USE_THREAD_DUMP is set */
1643
Willy Tarreauc7091d82019-05-17 10:08:49 +02001644/* ID of the thread requesting the dump */
1645static unsigned int thread_dump_tid;
1646
1647/* points to the buffer where the dump functions should write. It must
1648 * have already been initialized by the requester. Nothing is done if
1649 * it's NULL.
1650 */
1651struct buffer *thread_dump_buffer = NULL;
1652
Willy Tarreau00c27b52022-07-01 19:08:56 +02001653/* initiates a thread dump */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001654void ha_thread_dump_all_to_trash()
1655{
Willy Tarreau039972b2022-07-01 19:37:42 +02001656 unsigned int old;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001657
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001658 /* initiate a dump starting from first thread. Use a CAS so that we do
1659 * not wait if we're not the first one, but we wait for a previous dump
1660 * to finish.
Willy Tarreau67297262022-07-13 08:59:39 +02001661 */
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001662 while (1) {
1663 old = 0;
1664 if (HA_ATOMIC_CAS(&thread_dump_state, &old, THREAD_DUMP_FSYNC))
1665 break;
1666 ha_thread_relax();
1667 }
Willy Tarreauc7091d82019-05-17 10:08:49 +02001668 thread_dump_buffer = &trash;
1669 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001670 ha_tkillall(DEBUGSIG);
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001671
1672 /* the call above contains a raise() so we're certain to return after
1673 * returning from the sighandler, hence when the dump is complete.
1674 */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001675}
1676
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001677/* handles DEBUGSIG to dump the state of the thread it's working on. This is
1678 * appended at the end of thread_dump_buffer which must be protected against
1679 * reentrance from different threads (a thread-local buffer works fine).
1680 */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001681void debug_handler(int sig, siginfo_t *si, void *arg)
1682{
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001683 int harmless = is_thread_harmless();
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001684 int running = 0;
1685 uint prev;
Willy Tarreau00c27b52022-07-01 19:08:56 +02001686 uint next;
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001687
Willy Tarreau82aafc42020-03-03 08:31:34 +01001688 /* first, let's check it's really for us and that we didn't just get
1689 * a spurious DEBUGSIG.
1690 */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001691 if (!_HA_ATOMIC_LOAD(&thread_dump_state))
Willy Tarreau82aafc42020-03-03 08:31:34 +01001692 return;
1693
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001694 /* There are 5 phases in the dump process:
1695 * 1- wait for all threads to sync or the first one to start
1696 * 2- wait for our turn, i.e. when tid appears in lower bits.
1697 * 3- perform the action if our tid is there
1698 * 4- pass tid to the number of the next thread to dump or
1699 * reset running counter if we're last one.
1700 * 5- wait for running to be zero and decrement the count
Willy Tarreauc7091d82019-05-17 10:08:49 +02001701 */
1702
1703 /* wait for all previous threads to finish first */
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001704 if (!harmless)
1705 thread_harmless_now();
1706
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001707 if (HA_ATOMIC_FETCH_ADD(&thread_dump_state, 1) == THREAD_DUMP_FSYNC) {
1708 /* the first one which lands here is responsible for constantly
1709 * recounting the number of active theads and switching from
1710 * SYNC to DUMP.
1711 */
1712 while (1) {
1713 int first = -1; // first tid to dump
1714 int thr;
1715
1716 running = 0;
1717 for (thr = 0; thr < global.nbthread; thr++) {
1718 if (ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit) {
1719 running++;
1720 if (first < 0)
1721 first = thr;
1722 }
1723 }
1724
1725 if ((HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_TMASK) == running) {
1726 /* all threads are there, let's try to start */
1727 prev = THREAD_DUMP_FSYNC | running;
1728 next = (running << 16) | first;
1729 if (HA_ATOMIC_CAS(&thread_dump_state, &prev, next))
1730 break;
1731 /* it failed! maybe a thread appeared late (e.g. during boot), let's
1732 * recount.
1733 */
1734 }
1735 ha_thread_relax();
1736 }
1737 }
1738
1739 /* all threads: let's wait for the SYNC flag to disappear; tid is reset at
1740 * the same time to the first valid tid to dump and pmask will reflect the
1741 * number of participants.
1742 */
1743 while (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_FSYNC)
1744 ha_thread_relax();
1745
1746 /* wait for our turn */
1747 while ((HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_TMASK) != tid)
Willy Tarreauc7091d82019-05-17 10:08:49 +02001748 ha_thread_relax();
1749
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001750 if (!harmless)
Willy Tarreau9debe0f2023-01-19 18:52:31 +01001751 thread_harmless_end_sig();
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001752
Willy Tarreauc7091d82019-05-17 10:08:49 +02001753 /* dump if needed */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001754 if (thread_dump_buffer)
1755 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1756
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001757 /* figure which is the next thread ID to dump among enabled ones. Note
1758 * that this relies on the fact that we're not creating new threads in
1759 * the middle of a dump, which is normally granted by the harmless bits
1760 * anyway.
1761 */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001762 for (next = tid + 1; next < global.nbthread; next++) {
1763 if (unlikely(next >= MAX_THREADS)) {
1764 /* just to please gcc 6.5 who guesses the ranges wrong. */
1765 continue;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001766 }
Willy Tarreau00c27b52022-07-01 19:08:56 +02001767
1768 if (ha_thread_info[next].tg &&
1769 ha_thread_info[next].tg->threads_enabled & ha_thread_info[next].ltid_bit)
1770 break;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001771 }
1772
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001773 /* if there are threads left to dump, we atomically set the next one,
1774 * otherwise we'll clear dump and set the thread part to the number of
1775 * threads that need to disappear.
1776 */
1777 if (next < global.nbthread) {
1778 next = (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_PMASK) | next;
1779 HA_ATOMIC_STORE(&thread_dump_state, next);
1780 } else {
Willy Tarreau00c27b52022-07-01 19:08:56 +02001781 thread_dump_buffer = NULL; // was the last one
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001782 running = (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_PMASK) >> 16;
1783 HA_ATOMIC_STORE(&thread_dump_state, running);
1784 }
Willy Tarreau00c27b52022-07-01 19:08:56 +02001785
1786 /* now wait for all others to finish dumping: the lowest part will turn
Willy Tarreau9debe0f2023-01-19 18:52:31 +01001787 * to zero. Then all others decrement the done part. We must not change
1788 * the harmless status anymore because one of the other threads might
1789 * have been interrupted in thread_isolate() waiting for all others to
1790 * become harmless, and changing the situation here would break that
1791 * promise.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001792 */
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001793
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001794 /* wait for everyone to finish*/
1795 while (HA_ATOMIC_LOAD(&thread_dump_state) & THREAD_DUMP_PMASK)
Willy Tarreauc0773622019-07-31 19:15:45 +02001796 ha_thread_relax();
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001797
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001798 /* we're gone. Past this point anything can happen including another
1799 * thread trying to re-trigger a dump, so thread_dump_buffer and
1800 * thread_dump_tid may become invalid immediately after this call.
1801 */
1802 HA_ATOMIC_SUB(&thread_dump_state, 1);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001803
1804 /* mark the current thread as stuck to detect it upon next invocation
1805 * if it didn't move.
1806 */
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001807 if (!harmless &&
Willy Tarreaue7475c82022-06-20 09:23:24 +02001808 !(_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_SLEEPING))
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001809 _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_STUCK);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001810}
1811
1812static int init_debug_per_thread()
1813{
1814 sigset_t set;
1815
1816 /* unblock the DEBUGSIG signal we intend to use */
1817 sigemptyset(&set);
1818 sigaddset(&set, DEBUGSIG);
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001819#if defined(DEBUG_DEV)
1820 sigaddset(&set, SIGRTMAX);
1821#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +02001822 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1823 return 1;
1824}
1825
1826static int init_debug()
1827{
1828 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001829 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001830
Willy Tarreau0214b452020-03-04 06:01:40 +01001831 /* calling backtrace() will access libgcc at runtime. We don't want to
1832 * do it after the chroot, so let's perform a first call to have it
1833 * ready in memory for later use.
1834 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001835 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001836 sa.sa_handler = NULL;
1837 sa.sa_sigaction = debug_handler;
1838 sigemptyset(&sa.sa_mask);
1839 sa.sa_flags = SA_SIGINFO;
1840 sigaction(DEBUGSIG, &sa, NULL);
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001841
1842#if defined(DEBUG_DEV)
1843 sa.sa_handler = NULL;
1844 sa.sa_sigaction = debug_delay_inj_sighandler;
1845 sigemptyset(&sa.sa_mask);
1846 sa.sa_flags = SA_SIGINFO;
1847 sigaction(SIGRTMAX, &sa, NULL);
1848#endif
Christopher Fauletfc633b62020-11-06 15:24:23 +01001849 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001850}
1851
1852REGISTER_POST_CHECK(init_debug);
1853REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1854
1855#endif /* USE_THREAD_DUMP */
1856
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001857/* register cli keywords */
1858static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001859 {{ "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 +01001860 {{ "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 +02001861 {{ "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 +02001862 {{ "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 +02001863 {{ "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 +02001864#if defined(DEBUG_DEV)
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001865 {{ "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 +02001866 {{ "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 +02001867#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001868 {{ "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 +02001869 {{ "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 +01001870 {{ "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 +02001871 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1872 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaue5e62232023-05-04 11:50:26 +02001873 {{ "debug", "dev", "loop", NULL }, "debug dev loop <ms> [isolated] : loop this long, possibly isolated", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001874#if defined(DEBUG_MEM_STATS)
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001875 {{ "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 +02001876#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001877 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1878 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1879 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1880 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau98679872023-05-03 11:22:45 +02001881 {{ "debug", "dev", "task", NULL }, "debug dev task <ptr> [wake|expire|kill] : show/wake/expire/kill task/tasklet", debug_parse_cli_task, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001882 {{ "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 +01001883 {{ "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 +02001884 {{ "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 +02001885
Willy Tarreau6ab7b212021-12-28 09:57:10 +01001886#if defined(HA_HAVE_DUMP_LIBS)
1887 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1888#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001889 {{ "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 +02001890 {{},}
1891}};
1892
1893INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);