blob: b19a85b760695b89bc61a67885f84e388a937940 [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 Tarreau9a6ecbd2023-05-04 16:23:51 +020069/* Points to a copy of the buffer where the dump functions should write, when
70 * non-null. It's only used by debuggers for core dump analysis.
71 */
72struct buffer *thread_dump_buffer = NULL;
Willy Tarreau9b013702019-10-24 18:18:02 +020073unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020074
Willy Tarreau123fc972021-01-22 13:52:41 +010075/* dumps a backtrace of the current thread that is appended to buffer <buf>.
76 * Lines are prefixed with the string <prefix> which may be empty (used for
77 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010078 * the function does not appear in the call stack. The <dump> argument
79 * indicates what dump state to start from, and should usually be zero. It
80 * may be among the following values:
81 * - 0: search usual callers before step 1, or directly jump to 2
82 * - 1: skip usual callers before step 2
83 * - 2: dump until polling loop, scheduler, or main() (excluded)
84 * - 3: end
85 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010086 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010087void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010088{
89 struct buffer bak;
90 char pfx2[100];
91 void *callers[100];
92 int j, nptrs;
93 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010094
95 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
96 if (!nptrs)
97 return;
98
99 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
100 pfx2[0] = 0;
101
102 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
103 * produce similar output to the following:
104 */
105 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100106 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
107 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100108 /* we failed to spot the starting point of the
109 * dump, let's start over dumping everything we
110 * have.
111 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100112 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +0100113 j = 0;
114 }
115 bak = *buf;
116 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
117 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100118 if ((dump & 3) == 0) {
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200119 /* dump not started, will start *after* ha_thread_dump_one(),
Willy Tarreaue69919d2023-05-04 19:19:04 +0200120 * ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +0100121 */
Willy Tarreaue69919d2023-05-04 19:19:04 +0200122 if (addr == ha_panic ||
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200123 addr == ha_backtrace_to_stderr || addr == ha_thread_dump_one)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100124 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100125 *buf = bak;
126 continue;
127 }
128
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100129 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100130 /* starting */
Willy Tarreaue69919d2023-05-04 19:19:04 +0200131 if (addr == ha_panic ||
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200132 addr == ha_backtrace_to_stderr || addr == ha_thread_dump_one) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100133 *buf = bak;
134 continue;
135 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100136 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100137 }
138
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100139 if ((dump & 3) == 2) {
140 /* still dumping */
141 if (dump == 6) {
142 /* we only stop *after* main and we must send the LF */
143 if (addr == main) {
144 j = nptrs;
145 dump++;
146 }
147 }
148 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
149 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100150 *buf = bak;
151 break;
152 }
153 }
154 /* OK, line dumped */
155 chunk_appendf(buf, "\n");
156 }
157}
158
Willy Tarreaua8459b22021-01-22 14:12:27 +0100159/* dump a backtrace of current thread's stack to stderr. */
Willy Tarreau7831e022022-05-06 15:16:19 +0200160void ha_backtrace_to_stderr(void)
Willy Tarreaua8459b22021-01-22 14:12:27 +0100161{
162 char area[2048];
163 struct buffer b = b_make(area, sizeof(area), 0, 0);
164
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100165 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100166 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100167 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100168}
169
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200170/* Dumps to the thread's buffer some known information for the desired thread,
171 * and optionally extra info when it's safe to do so (current thread or
172 * isolated). The dump will be appended to the buffer, so the caller is
173 * responsible for preliminary initializing it. The <from_signal> argument will
174 * indicate if the function is called from the debug signal handler, indicating
175 * the thread was dumped upon request from another one, otherwise if the thread
176 * it the current one, a star ('*') will be displayed in front of the thread to
177 * indicate the requesting one. Any stuck thread is also prefixed with a '>'.
178 * The caller is responsible for atomically setting up the thread's dump buffer
179 * to point to a valid buffer with enough room. Output will be truncated if it
180 * does not fit. When the dump is complete, the dump buffer will be switched to
181 * (void*)0x1 that the caller must turn to 0x0 once the contents are collected.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200182 */
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200183void ha_thread_dump_one(int thr, int from_signal)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200184{
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200185 struct buffer *buf = HA_ATOMIC_LOAD(&ha_thread_ctx[thr].thread_dump_buffer);
Willy Tarreau95e6c992023-05-07 15:02:30 +0200186 unsigned long __maybe_unused thr_bit = ha_thread_info[thr].ltid_bit;
187 int __maybe_unused tgrp = ha_thread_info[thr].tgid;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200188 unsigned long long p = ha_thread_ctx[thr].prev_cpu_time;
Willy Tarreau21694982021-10-08 15:09:17 +0200189 unsigned long long n = now_cpu_time_thread(thr);
Willy Tarreaua0b99532021-09-30 18:48:37 +0200190 int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200191
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200192 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200193 "%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 +0200194 " %2u/%-2u stuck=%d prof=%d",
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200195 (thr == tid && !from_signal) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200196 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200197 thread_has_tasks(),
Willy Tarreauc958c702022-06-16 15:59:36 +0200198 !eb_is_empty(&ha_thread_ctx[thr].rqueue_shared),
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200199 !eb_is_empty(&ha_thread_ctx[thr].timers),
200 !eb_is_empty(&ha_thread_ctx[thr].rqueue),
201 !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) &&
202 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) &&
203 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) &&
204 MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)),
205 ha_thread_ctx[thr].tasks_in_list,
206 ha_thread_ctx[thr].rq_total,
Willy Tarreau66ad98a2022-06-28 10:49:57 +0200207 ha_thread_info[thr].tgid, ha_thread_info[thr].ltid + 1,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200208 stuck,
Willy Tarreaub30ced32023-05-04 11:30:55 +0200209 !!(ha_thread_ctx[thr].flags & TH_FL_TASK_PROFILING));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200210
Willy Tarreau95e6c992023-05-07 15:02:30 +0200211#if defined(USE_THREAD)
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200212 chunk_appendf(buf,
Willy Tarreaub30ced32023-05-04 11:30:55 +0200213 " harmless=%d isolated=%d",
Willy Tarreau03f9b352022-06-27 16:02:24 +0200214 !!(_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp-1].threads_harmless) & thr_bit),
Willy Tarreaub30ced32023-05-04 11:30:55 +0200215 isolated_thread == thr);
Willy Tarreau95e6c992023-05-07 15:02:30 +0200216#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200217
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200218 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200219 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200220
Willy Tarreaua3870b72021-09-13 19:24:47 +0200221 /* this is the end of what we can dump from outside the current thread */
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200222
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200223 if (thr != tid && !thread_isolated())
224 goto leave;
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200225
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200226 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200227 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100228
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200229 if (stuck && thr == tid) {
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100230 /* We only emit the backtrace for stuck threads in order not to
231 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100232 * Please leave this as the last instruction in this function
233 * so that the compiler uses tail merging and the current
234 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100235 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100236 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100237 }
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200238 leave:
239 /* end of dump, setting the buffer to 0x1 will tell the caller we're done */
240 HA_ATOMIC_STORE(&ha_thread_ctx[thr].thread_dump_buffer, (void*)0x1UL);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200241}
242
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +0200243/* Triggers a thread dump from thread <thr>, either directly if it's the
244 * current thread or if thread dump signals are not implemented, or by sending
245 * a signal if it's a remote one and the feature is supported. The buffer <buf>
246 * will get the dump appended, and the caller is responsible for making sure
247 * there is enough room otherwise some contents will be truncated.
248 */
249void ha_thread_dump(struct buffer *buf, int thr)
250{
251 struct buffer *old = NULL;
252
253 /* try to impose our dump buffer and to reserve the target thread's
254 * next dump for us.
255 */
256 do {
257 if (old)
258 ha_thread_relax();
259 old = NULL;
260 } while (!HA_ATOMIC_CAS(&ha_thread_ctx[thr].thread_dump_buffer, &old, buf));
261
262#ifdef USE_THREAD_DUMP
263 /* asking the remote thread to dump itself allows to get more details
264 * including a backtrace.
265 */
266 if (thr != tid)
267 ha_tkill(thr, DEBUGSIG);
268 else
269#endif
270 ha_thread_dump_one(thr, thr != tid);
271
272 /* now wait for the dump to be done, and release it */
273 do {
274 if (old)
275 ha_thread_relax();
276 old = (void*)0x01;
277 } while (!HA_ATOMIC_CAS(&ha_thread_ctx[thr].thread_dump_buffer, &old, 0));
278}
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200279
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200280/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200281 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200282 * with <pfx>. The buffer is only appended and the first output starts by the
283 * pointer itself. The caller is responsible for making sure the task is not
284 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200285 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200286void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200287{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200288 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200289 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200290 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau52f238d2022-07-15 12:08:40 +0200291 const struct stconn *sc;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200292
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200293 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200294 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200295 return;
296 }
297
Willy Tarreau20db9112019-05-17 14:14:35 +0200298 if (TASK_IS_TASKLET(task))
299 chunk_appendf(buf,
300 "%p (tasklet) calls=%u\n",
301 task,
302 task->calls);
303 else
304 chunk_appendf(buf,
305 "%p (task) calls=%u last=%llu%s\n",
306 task,
307 task->calls,
Willy Tarreau04e50b32022-09-07 14:49:50 +0200308 task->wake_date ? (unsigned long long)(now_mono_time() - task->wake_date) : 0,
309 task->wake_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200310
Willy Tarreau2e89b092020-03-03 17:13:02 +0100311 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
312 resolve_sym_name(buf, NULL, task->process);
313 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200314
Willy Tarreaua512b022019-08-21 14:12:19 +0200315 if (task->process == task_run_applet && (appctx = task->context))
316 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
317 else
318 chunk_appendf(buf, "\n");
319
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200320 if (task->process == process_stream && task->context)
321 s = (struct stream *)task->context;
Willy Tarreau52f238d2022-07-15 12:08:40 +0200322 else if (task->process == task_run_applet && task->context && (sc = appctx_sc((struct appctx *)task->context)))
323 s = sc_strm(sc);
Willy Tarreau462b9892022-05-18 18:06:53 +0200324 else if (task->process == sc_conn_io_cb && task->context)
Willy Tarreauea27f482022-05-18 16:10:52 +0200325 s = sc_strm(((struct stconn *)task->context));
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200326
327 if (s)
328 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200329
330#ifdef USE_LUA
331 hlua = NULL;
Aurelien DARRAGON1a3ac402024-03-12 17:05:54 +0100332 if (s && ((s->hlua[0] && HLUA_IS_BUSY(s->hlua[0])) ||
333 (s->hlua[1] && HLUA_IS_BUSY(s->hlua[1])))) {
334 hlua = (s->hlua[0] && HLUA_IS_BUSY(s->hlua[0])) ? s->hlua[0] : s->hlua[1];
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200335 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
336 }
337 else if (task->process == hlua_process_task && (hlua = task->context)) {
338 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
339 }
340 else if (task->process == task_run_applet && (appctx = task->context) &&
Willy Tarreaue23f33b2022-05-06 14:07:13 +0200341 (appctx->applet->fct == hlua_applet_tcp_fct)) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200342 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
343 }
344 else if (task->process == task_run_applet && (appctx = task->context) &&
Willy Tarreauaa229cc2022-05-06 14:26:10 +0200345 (appctx->applet->fct == hlua_applet_http_fct)) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200346 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
347 }
348
Christopher Faulet471425f2020-07-24 19:08:05 +0200349 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100350 chunk_appendf(buf, "stack traceback:\n ");
351 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200352 }
Willy Tarreaucb01f5d2023-05-04 16:28:30 +0200353
354 /* we may need to terminate the current line */
355 if (*b_peek(buf, b_data(buf)-1) != '\n')
Christopher Faulet471425f2020-07-24 19:08:05 +0200356 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200357#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200358}
359
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200360/* This function dumps all profiling settings. It returns 0 if the output
361 * buffer is full and it needs to be called again, otherwise non-zero.
362 */
363static int cli_io_handler_show_threads(struct appctx *appctx)
364{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200365 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200366 int thr;
367
Christopher Faulet87633c32023-04-03 18:32:50 +0200368 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +0200369 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200370 return 1;
371
372 if (appctx->st0)
373 thr = appctx->st1;
374 else
375 thr = 0;
376
Willy Tarreau009b5512023-05-04 19:07:56 +0200377 do {
378 chunk_reset(&trash);
379 ha_thread_dump(&trash, thr);
380
381 if (applet_putchk(appctx, &trash) == -1) {
382 /* failed, try again */
383 appctx->st1 = thr;
384 return 0;
385 }
386 thr++;
387 } while (thr < global.nbthread);
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200388
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200389 return 1;
390}
391
Willy Tarreau6ab7b212021-12-28 09:57:10 +0100392#if defined(HA_HAVE_DUMP_LIBS)
393/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
394static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
395{
396 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
397 return 1;
398
399 chunk_reset(&trash);
400 if (dump_libs(&trash, 1))
401 return cli_msg(appctx, LOG_INFO, trash.area);
402 else
403 return 0;
404}
405#endif
406
Willy Tarreau880d1682023-05-04 18:52:51 +0200407/* Dumps a state of all threads into the trash and on fd #2, then aborts.
408 * A copy will be put into a trash chunk that's assigned to thread_dump_buffer
409 * so that the debugger can easily find it. This buffer might be truncated if
410 * too many threads are being dumped, but at least we'll dump them all on stderr.
411 * If thread_dump_buffer is set, it means that a panic has already begun.
412 */
Willy Tarreau56131ca2019-05-20 13:48:29 +0200413void ha_panic()
414{
Willy Tarreau880d1682023-05-04 18:52:51 +0200415 struct buffer *old;
416 unsigned int thr;
417
418 old = NULL;
419 if (!HA_ATOMIC_CAS(&thread_dump_buffer, &old, get_trash_chunk())) {
Willy Tarreau55433f92022-07-15 12:48:58 +0200420 /* a panic dump is already in progress, let's not disturb it,
421 * we'll be called via signal DEBUGSIG. By returning we may be
422 * able to leave a current signal handler (e.g. WDT) so that
423 * this will ensure more reliable signal delivery.
424 */
425 return;
426 }
Willy Tarreau880d1682023-05-04 18:52:51 +0200427
Willy Tarreau56131ca2019-05-20 13:48:29 +0200428 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200429 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau880d1682023-05-04 18:52:51 +0200430
431 for (thr = 0; thr < global.nbthread; thr++) {
432 ha_thread_dump(&trash, thr);
433 DISGUISE(write(2, trash.area, trash.data));
434 b_force_xfer(thread_dump_buffer, &trash, b_room(thread_dump_buffer));
435 chunk_reset(&trash);
436 }
437
Willy Tarreau56131ca2019-05-20 13:48:29 +0200438 for (;;)
439 abort();
440}
441
Willy Tarreau06e66c82022-03-02 15:52:03 +0100442/* Complain with message <msg> on stderr. If <counter> is not NULL, it is
443 * atomically incremented, and the message is only printed when the counter
444 * was zero, so that the message is only printed once. <taint> is only checked
445 * on bit 1, and will taint the process either for a bug (2) or warn (0).
446 */
447void complain(int *counter, const char *msg, int taint)
448{
449 if (counter && _HA_ATOMIC_FETCH_ADD(counter, 1))
450 return;
451 DISGUISE(write(2, msg, strlen(msg)));
452 if (taint & 2)
453 mark_tainted(TAINTED_BUG);
454 else
455 mark_tainted(TAINTED_WARN);
456}
457
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200458/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
459static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
460{
461 int code = atoi(args[3]);
462
463 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
464 return 1;
465
Willy Tarreau4781b152021-04-06 13:53:36 +0200466 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200467 exit(code);
468 return 1;
469}
470
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100471/* parse a "debug dev bug" command. It always returns 1, though it should never return.
472 * Note: we make sure not to make the function static so that it appears in the trace.
473 */
474int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
475{
476 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
477 return 1;
478
Willy Tarreau4781b152021-04-06 13:53:36 +0200479 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100480 BUG_ON(one > zero);
481 return 1;
482}
483
Willy Tarreau305cfbd2022-02-25 08:52:39 +0100484/* parse a "debug dev warn" command. It always returns 1.
485 * Note: we make sure not to make the function static so that it appears in the trace.
486 */
487int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
488{
489 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
490 return 1;
491
492 _HA_ATOMIC_INC(&debug_commands_issued);
493 WARN_ON(one > zero);
494 return 1;
495}
496
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100497/* parse a "debug dev check" command. It always returns 1.
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100498 * Note: we make sure not to make the function static so that it appears in the trace.
499 */
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100500int debug_parse_cli_check(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100501{
502 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
503 return 1;
504
505 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100506 CHECK_IF(one > zero);
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100507 return 1;
508}
509
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200510/* parse a "debug dev close" command. It always returns 1. */
511static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
512{
513 int fd;
514
515 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
516 return 1;
517
Willy Tarreau9d008692019-08-09 11:21:01 +0200518 if (!*args[3])
519 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200520
521 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200522 if (fd < 0 || fd >= global.maxsock)
523 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200524
Willy Tarreau9d008692019-08-09 11:21:01 +0200525 if (!fdtab[fd].owner)
526 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200527
Willy Tarreau4781b152021-04-06 13:53:36 +0200528 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200529 fd_delete(fd);
530 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200531}
532
Willy Tarreaudadf00e2022-07-15 08:25:03 +0200533/* this is meant to cause a deadlock when more than one task is running it or when run twice */
534static struct task *debug_run_cli_deadlock(struct task *task, void *ctx, unsigned int state)
535{
536 static HA_SPINLOCK_T lock __maybe_unused;
537
538 HA_SPIN_LOCK(OTHER_LOCK, &lock);
539 return NULL;
540}
541
542/* parse a "debug dev deadlock" command. It always returns 1. */
543static int debug_parse_cli_deadlock(char **args, char *payload, struct appctx *appctx, void *private)
544{
545 int tasks;
546
547 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
548 return 1;
549
550 _HA_ATOMIC_INC(&debug_commands_issued);
551 for (tasks = atoi(args[3]); tasks > 0; tasks--) {
552 struct task *t = task_new_on(tasks % global.nbthread);
553 if (!t)
554 continue;
555 t->process = debug_run_cli_deadlock;
556 t->context = NULL;
557 task_wakeup(t, TASK_WOKEN_INIT);
558 }
559
560 return 1;
561}
562
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200563/* parse a "debug dev delay" command. It always returns 1. */
564static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
565{
566 int delay = atoi(args[3]);
567
568 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
569 return 1;
570
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200572 usleep((long)delay * 1000);
573 return 1;
574}
575
576/* parse a "debug dev log" command. It always returns 1. */
577static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
578{
579 int arg;
580
581 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
582 return 1;
583
Willy Tarreau4781b152021-04-06 13:53:36 +0200584 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200585 chunk_reset(&trash);
586 for (arg = 3; *args[arg]; arg++) {
587 if (arg > 3)
588 chunk_strcat(&trash, " ");
589 chunk_strcat(&trash, args[arg]);
590 }
591
592 send_log(NULL, LOG_INFO, "%s\n", trash.area);
593 return 1;
594}
595
596/* parse a "debug dev loop" command. It always returns 1. */
597static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
598{
599 struct timeval deadline, curr;
600 int loop = atoi(args[3]);
Willy Tarreaue5e62232023-05-04 11:50:26 +0200601 int isolate;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200602
603 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
604 return 1;
605
Willy Tarreaue5e62232023-05-04 11:50:26 +0200606 isolate = strcmp(args[4], "isolated") == 0;
607
Willy Tarreau4781b152021-04-06 13:53:36 +0200608 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200609 gettimeofday(&curr, NULL);
610 tv_ms_add(&deadline, &curr, loop);
611
Willy Tarreaue5e62232023-05-04 11:50:26 +0200612 if (isolate)
613 thread_isolate();
614
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200615 while (tv_ms_cmp(&curr, &deadline) < 0)
616 gettimeofday(&curr, NULL);
617
Willy Tarreaue5e62232023-05-04 11:50:26 +0200618 if (isolate)
619 thread_release();
620
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200621 return 1;
622}
623
624/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
625static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
626{
627 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
628 return 1;
629
Willy Tarreau4781b152021-04-06 13:53:36 +0200630 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200631 ha_panic();
632 return 1;
633}
634
635/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200636#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200637static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
638{
Willy Tarreau368bff42019-12-06 17:18:28 +0100639 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200640 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100641 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200642
643 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
644 return 1;
645
Willy Tarreau4781b152021-04-06 13:53:36 +0200646 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200647 chunk_reset(&trash);
648 for (arg = 3; *args[arg]; arg++) {
649 if (arg > 3)
650 chunk_strcat(&trash, " ");
651 chunk_strcat(&trash, args[arg]);
652 }
653
Willy Tarreau368bff42019-12-06 17:18:28 +0100654 thread_isolate();
655 if (pipe(pipefd) < 0)
656 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200657
Willy Tarreau38247432022-04-26 10:24:14 +0200658 if (fd_set_cloexec(pipefd[0]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100659 goto fail_fcntl;
660
Willy Tarreau38247432022-04-26 10:24:14 +0200661 if (fd_set_cloexec(pipefd[1]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100662 goto fail_fcntl;
663
664 pid = fork();
665
666 if (pid < 0)
667 goto fail_fork;
668 else if (pid == 0) {
669 /* child */
670 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
671
672 close(0);
673 dup2(pipefd[1], 1);
674 dup2(pipefd[1], 2);
675
676 cmd[2] = trash.area;
677 execvp(cmd[0], cmd);
678 printf("execvp() failed\n");
679 exit(1);
680 }
681
682 /* parent */
683 thread_release();
684 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200685 chunk_reset(&trash);
686 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100687 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
688 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200689 break;
690 trash.data += ret;
691 if (trash.data + 20 == trash.size) {
692 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
693 break;
694 }
695 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100696 close(pipefd[0]);
697 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200698 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200699 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100700
701 fail_fork:
702 fail_fcntl:
703 close(pipefd[0]);
704 close(pipefd[1]);
705 fail_pipe:
706 thread_release();
707 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200708}
Willy Tarreaubd3b44e2023-03-09 08:25:01 +0100709
710/* handles SIGRTMAX to inject random delays on the receiving thread in order
711 * to try to increase the likelihood to reproduce inter-thread races. The
712 * signal is periodically sent by a task initiated by "debug dev delay-inj".
713 */
714void debug_delay_inj_sighandler(int sig, siginfo_t *si, void *arg)
715{
716 volatile int i = statistical_prng_range(10000);
717
718 while (i--)
719 __ha_cpu_relax();
720}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200721#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200722
723/* parse a "debug dev hex" command. It always returns 1. */
724static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
725{
726 unsigned long start, len;
727
728 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
729 return 1;
730
Willy Tarreau9d008692019-08-09 11:21:01 +0200731 if (!*args[3])
732 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200733
734 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200735 if (!start)
736 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200737
Willy Tarreau4781b152021-04-06 13:53:36 +0200738 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200739
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200740 /* by default, dump ~128 till next block of 16 */
741 len = strtoul(args[4], NULL, 0);
742 if (!len)
743 len = ((start + 128) & -16) - start;
744
745 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200746 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200747 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200748 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200749}
750
Willy Tarreau48129be2021-05-04 18:40:50 +0200751/* parse a "debug dev sym <addr>" command. It always returns 1. */
752static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
753{
754 unsigned long addr;
755
756 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
757 return 1;
758
759 if (!*args[3])
760 return cli_err(appctx, "Missing memory address to be resolved.\n");
761
762 _HA_ATOMIC_INC(&debug_commands_issued);
763
764 addr = strtoul(args[3], NULL, 0);
765 chunk_printf(&trash, "%#lx resolves to ", addr);
766 resolve_sym_name(&trash, NULL, (const void *)addr);
767 chunk_appendf(&trash, "\n");
768
769 return cli_msg(appctx, LOG_INFO, trash.area);
770}
771
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200772/* parse a "debug dev tkill" command. It always returns 1. */
773static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
774{
775 int thr = 0;
776 int sig = SIGABRT;
777
778 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
779 return 1;
780
781 if (*args[3])
782 thr = atoi(args[3]);
783
Willy Tarreau9d008692019-08-09 11:21:01 +0200784 if (thr < 0 || thr > global.nbthread)
785 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200786
787 if (*args[4])
788 sig = atoi(args[4]);
789
Willy Tarreau4781b152021-04-06 13:53:36 +0200790 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200791 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200792 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200793 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200794 raise(sig);
795 return 1;
796}
797
Erwan Le Goas54966df2022-09-14 17:24:22 +0200798/* hashes 'word' in "debug dev hash 'word' ". */
799static int debug_parse_cli_hash(char **args, char *payload, struct appctx *appctx, void *private)
800{
801 char *msg = NULL;
802
803 cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", HA_ANON_CLI(args[3])));
804 return 1;
805}
806
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100807/* parse a "debug dev write" command. It always returns 1. */
808static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
809{
810 unsigned long len;
811
812 if (!*args[3])
813 return cli_err(appctx, "Missing output size.\n");
814
815 len = strtoul(args[3], NULL, 0);
816 if (len >= trash.size)
817 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
818
Willy Tarreau4781b152021-04-06 13:53:36 +0200819 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100820
821 chunk_reset(&trash);
822 trash.data = len;
823 memset(trash.area, '.', trash.data);
824 trash.area[trash.data] = 0;
825 for (len = 64; len < trash.data; len += 64)
826 trash.area[len] = '\n';
827 return cli_msg(appctx, LOG_INFO, trash.area);
828}
829
Willy Tarreau68680bb2019-10-23 17:23:25 +0200830/* parse a "debug dev stream" command */
831/*
832 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
833 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
834 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
835 * [sif.s[=<state>]] [sib.s[=<state>]]
836 */
837static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
838{
Willy Tarreau0698c802022-05-11 14:09:57 +0200839 struct stream *s = appctx_strm(appctx);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200840 int arg;
841 void *ptr;
842 int size;
843 const char *word, *end;
844 struct ist name;
845 char *msg = NULL;
846 char *endarg;
847 unsigned long long old, new;
848
849 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
850 return 1;
851
852 ptr = NULL; size = 0;
853
854 if (!*args[3]) {
855 return cli_err(appctx,
Willy Tarreaudd014482023-05-02 16:37:13 +0200856 "Usage: debug dev stream [ strm=<ptr> ] { <obj> <op> <value> | wake }*\n"
857 " <obj> = { strm.f | strm.x | scf.s | scb.s | txn.f | req.f | res.f }\n"
Willy Tarreau68680bb2019-10-23 17:23:25 +0200858 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
859 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
860 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
861 );
862 }
863
Willy Tarreau4781b152021-04-06 13:53:36 +0200864 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200865 for (arg = 3; *args[arg]; arg++) {
866 old = 0;
867 end = word = args[arg];
868 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
869 end++;
870 name = ist2(word, end - word);
871 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200872 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200873 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200874 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200875 } else if (isteq(name, ist("strm.x"))) {
876 ptr = (!s || !may_access(s)) ? NULL : &s->conn_exp; size = sizeof(s->conn_exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200877 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200878 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200879 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200880 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200881 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200882 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200883 } else if (isteq(name, ist("scf.s"))) {
884 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scf->state);
885 } else if (isteq(name, ist("scb.s"))) {
886 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scb->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200887 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200888 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200889 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
890 continue;
891 } else
892 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
893
894 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200895 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200896 if (size == 8)
897 old = read_u64(ptr);
898 else if (size == 4)
899 old = read_u32(ptr);
900 else if (size == 2)
901 old = read_u16(ptr);
902 else
903 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200904 } else {
905 memprintf(&msg,
906 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
907 msg ? msg : "", ptr, (int)(end - word), word);
908 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200909 }
910
911 /* parse the new value . */
912 new = strtoll(end + 1, &endarg, 0);
913 if (end[1] && *endarg) {
914 if (strcmp(end + 1, "now") == 0)
915 new = now_ms;
916 else {
917 memprintf(&msg,
918 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
919 msg ? msg : "", end + 1, (int)(end - word), word);
920 continue;
921 }
922 }
923
924 switch (*end) {
925 case '\0': /* show */
926 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
927 new = old; // do not change the value
928 break;
929
930 case '=': /* set */
931 break;
932
933 case '^': /* XOR */
934 new = old ^ new;
935 break;
936
937 case '+': /* OR */
938 new = old | new;
939 break;
940
941 case '-': /* AND NOT */
942 new = old & ~new;
943 break;
944
945 default:
946 break;
947 }
948
949 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200950 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200951 if (size == 8)
952 write_u64(ptr, new);
953 else if (size == 4)
954 write_u32(ptr, new);
955 else if (size == 2)
956 write_u16(ptr, new);
957 else
958 *(uint8_t *)ptr = new;
959 }
960 }
961
962 if (msg && *msg)
963 return cli_dynmsg(appctx, LOG_INFO, msg);
964 return 1;
965}
966
Willy Tarreau98679872023-05-03 11:22:45 +0200967/* parse a "debug dev stream" command */
968/*
969 * debug dev task <ptr> [ "wake" | "expire" | "kill" ]
970 * Show/change status of a task/tasklet
971 */
972static int debug_parse_cli_task(char **args, char *payload, struct appctx *appctx, void *private)
973{
974 const struct ha_caller *caller;
975 struct task *t;
976 char *endarg;
977 char *msg;
978 void *ptr;
979 int ret = 1;
980 int task_ok;
981 int arg;
982
983 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
984 return 1;
985
986 /* parse the pointer value */
Willy Tarreau94df1b52023-05-12 04:40:06 +0200987 ptr = (void *)strtoul(args[3], &endarg, 0);
Willy Tarreau98679872023-05-03 11:22:45 +0200988 if (!*args[3] || *endarg)
989 goto usage;
990
991 _HA_ATOMIC_INC(&debug_commands_issued);
992
993 /* everything below must run under thread isolation till reaching label "leave" */
994 thread_isolate();
995
996 /* struct tasklet is smaller than struct task and is sufficient to check
997 * the TASK_COMMON part.
998 */
999 if (!may_access(ptr) || !may_access(ptr + sizeof(struct tasklet) - 1) ||
1000 ((const struct tasklet *)ptr)->tid < -1 ||
1001 ((const struct tasklet *)ptr)->tid >= (int)MAX_THREADS) {
1002 ret = cli_err(appctx, "The designated memory area doesn't look like a valid task/tasklet\n");
1003 goto leave;
1004 }
1005
1006 t = ptr;
1007 caller = t->caller;
1008 msg = NULL;
Aurelien DARRAGONb6a24a52023-05-15 11:59:08 +02001009 task_ok = may_access(ptr + sizeof(*t) - 1);
Willy Tarreau98679872023-05-03 11:22:45 +02001010
1011 chunk_reset(&trash);
1012 resolve_sym_name(&trash, NULL, (const void *)t->process);
1013
1014 /* we need to be careful here because we may dump a freed task that's
1015 * still in the pool cache, containing garbage in pointers.
1016 */
1017 if (!*args[4]) {
1018 memprintf(&msg, "%s%p: %s state=%#x tid=%d process=%s ctx=%p calls=%d last=%s:%d intl=%d",
1019 msg ? msg : "", t, (t->state & TASK_F_TASKLET) ? "tasklet" : "task",
1020 t->state, t->tid, trash.area, t->context, t->calls,
Willy Tarreauff508f12023-05-03 16:28:54 +02001021 caller && may_access(caller) && may_access(caller->func) && isalnum((uchar)*caller->func) ? caller->func : "0",
Willy Tarreau98679872023-05-03 11:22:45 +02001022 caller ? t->caller->line : 0,
1023 (t->state & TASK_F_TASKLET) ? LIST_INLIST(&((const struct tasklet *)t)->list) : 0);
1024
1025 if (task_ok && !(t->state & TASK_F_TASKLET))
1026 memprintf(&msg, "%s inrq=%d inwq=%d exp=%d nice=%d",
1027 msg ? msg : "", task_in_rq(t), task_in_wq(t), t->expire, t->nice);
1028
1029 memprintf(&msg, "%s\n", msg ? msg : "");
1030 }
1031
1032 for (arg = 4; *args[arg]; arg++) {
1033 if (strcmp(args[arg], "expire") == 0) {
1034 if (t->state & TASK_F_TASKLET) {
1035 /* do nothing for tasklets */
1036 }
1037 else if (task_ok) {
1038 /* unlink task and wake with timer flag */
1039 __task_unlink_wq(t);
1040 t->expire = now_ms;
1041 task_wakeup(t, TASK_WOKEN_TIMER);
1042 }
1043 } else if (strcmp(args[arg], "wake") == 0) {
1044 /* wake with all flags but init / timer */
1045 if (t->state & TASK_F_TASKLET)
1046 tasklet_wakeup((struct tasklet *)t);
1047 else if (task_ok)
1048 task_wakeup(t, TASK_WOKEN_ANY & ~(TASK_WOKEN_INIT|TASK_WOKEN_TIMER));
1049 } else if (strcmp(args[arg], "kill") == 0) {
1050 /* Kill the task. This is not idempotent! */
1051 if (!(t->state & TASK_KILLED)) {
1052 if (t->state & TASK_F_TASKLET)
1053 tasklet_kill((struct tasklet *)t);
1054 else if (task_ok)
1055 task_kill(t);
1056 }
1057 } else {
1058 thread_release();
1059 goto usage;
1060 }
1061 }
1062
1063 if (msg && *msg)
1064 ret = cli_dynmsg(appctx, LOG_INFO, msg);
1065 leave:
1066 thread_release();
1067 return ret;
1068 usage:
1069 return cli_err(appctx,
1070 "Usage: debug dev task <ptr> [ wake | expire | kill ]\n"
1071 " By default, dumps some info on task/tasklet <ptr>. 'wake' will wake it up\n"
1072 " with all conditions flags but init/exp. 'expire' will expire the entry, and\n"
1073 " 'kill' will kill it (warning: may crash since later not idempotent!). All\n"
1074 " changes may crash the process if performed on a wrong object!\n"
1075 );
1076}
1077
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001078#if defined(DEBUG_DEV)
1079static struct task *debug_delay_inj_task(struct task *t, void *ctx, unsigned int state)
1080{
1081 unsigned long *tctx = ctx; // [0] = interval, [1] = nbwakeups
1082 unsigned long inter = tctx[0];
1083 unsigned long count = tctx[1];
1084 unsigned long rnd;
1085
1086 if (inter)
1087 t->expire = tick_add(now_ms, inter);
1088 else
1089 task_wakeup(t, TASK_WOKEN_MSG);
1090
1091 /* wake a random thread */
1092 while (count--) {
1093 rnd = statistical_prng_range(global.nbthread);
1094 ha_tkill(rnd, SIGRTMAX);
1095 }
1096 return t;
1097}
1098
1099/* parse a "debug dev delay-inj" command
1100 * debug dev delay-inj <inter> <count>
1101 */
1102static int debug_parse_delay_inj(char **args, char *payload, struct appctx *appctx, void *private)
1103{
1104 unsigned long *tctx; // [0] = inter, [2] = count
1105 struct task *task;
1106
1107 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1108 return 1;
1109
1110 if (!*args[4])
1111 return cli_err(appctx, "Usage: debug dev delay-inj <inter_ms> <count>*\n");
1112
1113 _HA_ATOMIC_INC(&debug_commands_issued);
1114
Willy Tarreau8330d9f2024-02-10 11:35:07 +01001115 tctx = calloc(2, sizeof(*tctx));
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001116 if (!tctx)
1117 goto fail;
1118
1119 tctx[0] = atoi(args[3]);
1120 tctx[1] = atoi(args[4]);
1121
1122 task = task_new_here/*anywhere*/();
1123 if (!task)
1124 goto fail;
1125
1126 task->process = debug_delay_inj_task;
1127 task->context = tctx;
1128 task_wakeup(task, TASK_WOKEN_INIT);
1129 return 1;
1130
1131 fail:
1132 free(tctx);
1133 return cli_err(appctx, "Not enough memory");
1134}
1135#endif // DEBUG_DEV
1136
Willy Tarreau144f84a2021-03-02 16:09:26 +01001137static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +01001138{
1139 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1140 unsigned long inter = tctx[1];
1141 unsigned long rnd;
1142
1143 t->expire = tick_add(now_ms, inter);
1144
1145 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +01001146 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +01001147 if (rnd & 1) {
1148 rnd >>= 1;
1149 rnd %= tctx[0];
1150 rnd = tctx[rnd + 2];
1151
1152 if (rnd & 1)
1153 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
1154 else
1155 tasklet_wakeup((struct tasklet *)rnd);
1156 }
1157 return t;
1158}
1159
Willy Tarreau144f84a2021-03-02 16:09:26 +01001160static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +01001161{
1162 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1163 unsigned long rnd;
1164 int i;
1165
1166 /* wake up two random entries */
1167 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +01001168 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +01001169 rnd = tctx[rnd + 2];
1170
1171 if (rnd & 1)
1172 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
1173 else
1174 tasklet_wakeup((struct tasklet *)rnd);
1175 }
1176 return t;
1177}
1178
1179/* parse a "debug dev sched" command
1180 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
1181 */
1182static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
1183{
1184 int arg;
1185 void *ptr;
1186 int size;
1187 const char *word, *end;
1188 struct ist name;
1189 char *msg = NULL;
1190 char *endarg;
1191 unsigned long long new;
1192 unsigned long count = 0;
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001193 unsigned long thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001194 unsigned int inter = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001195 unsigned long i;
1196 int mode = 0; // 0 = tasklet; 1 = task
Willy Tarreaua5a44792020-11-29 17:12:15 +01001197 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
1198
1199 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1200 return 1;
1201
1202 ptr = NULL; size = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001203
1204 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
1205 return cli_err(appctx,
1206 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001207 " <obj> = {count | tid | inter }\n"
Willy Tarreaua5a44792020-11-29 17:12:15 +01001208 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
1209 );
1210 }
1211
1212 mode = strcmp(args[3], "task") == 0;
1213
Willy Tarreau4781b152021-04-06 13:53:36 +02001214 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001215 for (arg = 4; *args[arg]; arg++) {
1216 end = word = args[arg];
1217 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
1218 end++;
1219 name = ist2(word, end - word);
1220 if (isteq(name, ist("count"))) {
1221 ptr = &count; size = sizeof(count);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001222 } else if (isteq(name, ist("tid"))) {
1223 ptr = &thrid; size = sizeof(thrid);
1224 } else if (isteq(name, ist("inter"))) {
1225 ptr = &inter; size = sizeof(inter);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001226 } else
1227 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
1228
1229 /* parse the new value . */
1230 new = strtoll(end + 1, &endarg, 0);
1231 if (end[1] && *endarg) {
1232 memprintf(&msg,
1233 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
1234 msg ? msg : "", end + 1, (int)(end - word), word);
1235 continue;
1236 }
1237
1238 /* write the new value */
1239 if (size == 8)
1240 write_u64(ptr, new);
1241 else if (size == 4)
1242 write_u32(ptr, new);
1243 else if (size == 2)
1244 write_u16(ptr, new);
1245 else
1246 *(uint8_t *)ptr = new;
1247 }
1248
Willy Tarreau8330d9f2024-02-10 11:35:07 +01001249 tctx = calloc(count + 2, sizeof(*tctx));
Willy Tarreaua5a44792020-11-29 17:12:15 +01001250 if (!tctx)
1251 goto fail;
1252
1253 tctx[0] = (unsigned long)count;
1254 tctx[1] = (unsigned long)inter;
1255
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001256 if (thrid >= global.nbthread)
1257 thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001258
Willy Tarreaua5a44792020-11-29 17:12:15 +01001259 for (i = 0; i < count; i++) {
Willy Tarreaua5a44792020-11-29 17:12:15 +01001260 /* now, if poly or mask was set, tmask corresponds to the
1261 * valid thread mask to use, otherwise it remains zero.
1262 */
1263 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
1264 if (mode == 0) {
1265 struct tasklet *tl = tasklet_new();
1266
1267 if (!tl)
1268 goto fail;
1269
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001270 tl->tid = thrid;
Willy Tarreaua5a44792020-11-29 17:12:15 +01001271 tl->process = debug_tasklet_handler;
1272 tl->context = tctx;
1273 tctx[i + 2] = (unsigned long)tl;
1274 } else {
Willy Tarreau8e5c53a2022-06-15 16:32:41 +02001275 struct task *task = task_new_on(thrid);
Willy Tarreaua5a44792020-11-29 17:12:15 +01001276
1277 if (!task)
1278 goto fail;
1279
1280 task->process = debug_task_handler;
1281 task->context = tctx;
1282 tctx[i + 2] = (unsigned long)task + 1;
1283 }
1284 }
1285
1286 /* start the tasks and tasklets */
1287 for (i = 0; i < count; i++) {
1288 unsigned long ctx = tctx[i + 2];
1289
1290 if (ctx & 1)
1291 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
1292 else
1293 tasklet_wakeup((struct tasklet *)ctx);
1294 }
1295
1296 if (msg && *msg)
1297 return cli_dynmsg(appctx, LOG_INFO, msg);
1298 return 1;
1299
1300 fail:
1301 /* free partially allocated entries */
1302 for (i = 0; tctx && i < count; i++) {
1303 unsigned long ctx = tctx[i + 2];
1304
1305 if (!ctx)
1306 break;
1307
1308 if (ctx & 1)
1309 task_destroy((struct task *)(ctx - 1));
1310 else
1311 tasklet_free((struct tasklet *)ctx);
1312 }
1313
1314 free(tctx);
1315 return cli_err(appctx, "Not enough memory");
1316}
1317
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001318/* CLI state for "debug dev fd" */
1319struct dev_fd_ctx {
1320 int start_fd;
1321};
1322
Willy Tarreau5be7c192022-01-24 20:26:09 +01001323/* CLI parser for the "debug dev fd" command. The current FD to restart from is
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001324 * stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001325 */
1326static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
1327{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001328 struct dev_fd_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1329
Willy Tarreau5be7c192022-01-24 20:26:09 +01001330 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1331 return 1;
1332
1333 /* start at fd #0 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001334 ctx->start_fd = 0;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001335 return 0;
1336}
1337
1338/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1339 * accessible from the process but not known from fdtab. The FD number to
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001340 * restart from is stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001341 */
1342static int debug_iohandler_fd(struct appctx *appctx)
1343{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001344 struct dev_fd_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001345 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau5be7c192022-01-24 20:26:09 +01001346 struct sockaddr_storage sa;
1347 struct stat statbuf;
1348 socklen_t salen, vlen;
1349 int ret1, ret2, port;
1350 char *addrstr;
1351 int ret = 1;
1352 int i, fd;
1353
Christopher Faulet87633c32023-04-03 18:32:50 +02001354 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02001355 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau5be7c192022-01-24 20:26:09 +01001356 goto end;
1357
1358 chunk_reset(&trash);
1359
1360 thread_isolate();
1361
1362 /* we have two inner loops here, one for the proxy, the other one for
1363 * the buffer.
1364 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001365 for (fd = ctx->start_fd; fd < global.maxsock; fd++) {
Willy Tarreau5be7c192022-01-24 20:26:09 +01001366 /* check for FD's existence */
1367 ret1 = fcntl(fd, F_GETFD, 0);
1368 if (ret1 == -1)
1369 continue; // not known to the process
1370 if (fdtab[fd].owner)
1371 continue; // well-known
1372
1373 /* OK we're seeing an orphan let's try to retrieve as much
1374 * information as possible about it.
1375 */
1376 chunk_printf(&trash, "%5d", fd);
1377
1378 if (fstat(fd, &statbuf) != -1) {
1379 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1380 isatty(fd) ? "tty.":
1381 S_ISREG(statbuf.st_mode) ? "file":
1382 S_ISDIR(statbuf.st_mode) ? "dir.":
1383 S_ISCHR(statbuf.st_mode) ? "chr.":
1384 S_ISBLK(statbuf.st_mode) ? "blk.":
1385 S_ISFIFO(statbuf.st_mode) ? "pipe":
1386 S_ISLNK(statbuf.st_mode) ? "link":
1387 S_ISSOCK(statbuf.st_mode) ? "sock":
1388#ifdef USE_EPOLL
Willy Tarreau2058f532023-07-02 10:49:49 +02001389 /* trick: epoll_ctl() will return -ENOENT when trying
1390 * to remove from a valid epoll FD an FD that was not
1391 * registered against it. But we don't want to risk
1392 * disabling a random FD. Instead we'll create a new
1393 * one by duplicating 0 (it should be valid since
1394 * pointing to a terminal or /dev/null), and try to
1395 * remove it.
1396 */
1397 ({
1398 int fd2 = dup(0);
1399 int ret = fd2;
1400 if (ret >= 0) {
1401 ret = epoll_ctl(fd, EPOLL_CTL_DEL, fd2, NULL);
1402 if (ret == -1 && errno == ENOENT)
1403 ret = 0; // that's a real epoll
1404 else
1405 ret = -1; // it's something else
1406 close(fd2);
1407 }
1408 ret;
1409 }) == 0 ? "epol" :
Willy Tarreau5be7c192022-01-24 20:26:09 +01001410#endif
1411 "????",
1412 (uint)statbuf.st_mode & 07777,
1413
1414 (ullong)statbuf.st_rdev,
1415 (ullong)statbuf.st_size,
1416 (ullong)statbuf.st_uid,
1417 (ullong)statbuf.st_gid,
1418
1419 (ullong)statbuf.st_dev,
1420 (ullong)statbuf.st_ino);
1421 }
1422
1423 chunk_appendf(&trash, " getfd=%s+%#x",
1424 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1425 ret1 &~ FD_CLOEXEC);
1426
1427 /* FD options */
1428 ret2 = fcntl(fd, F_GETFL, 0);
1429 if (ret2) {
1430 chunk_appendf(&trash, " getfl=%s",
1431 (ret1 & 3) >= 2 ? "O_RDWR" :
1432 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1433
1434 for (i = 2; i < 32; i++) {
1435 if (!(ret2 & (1UL << i)))
1436 continue;
1437 switch (1UL << i) {
1438 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1439 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1440 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1441 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1442 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001443#ifdef O_ASYNC
Willy Tarreau5be7c192022-01-24 20:26:09 +01001444 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001445#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001446#ifdef O_DIRECT
1447 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1448#endif
1449#ifdef O_NOATIME
1450 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1451#endif
1452 }
1453 }
1454 }
1455
1456 vlen = sizeof(ret2);
1457 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1458 if (ret1 != -1)
1459 chunk_appendf(&trash, " so_type=%d", ret2);
1460
1461 vlen = sizeof(ret2);
1462 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1463 if (ret1 != -1)
1464 chunk_appendf(&trash, " so_accept=%d", ret2);
1465
1466 vlen = sizeof(ret2);
1467 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1468 if (ret1 != -1)
1469 chunk_appendf(&trash, " so_error=%d", ret2);
1470
1471 salen = sizeof(sa);
1472 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1473 if (sa.ss_family == AF_INET)
1474 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1475 else if (sa.ss_family == AF_INET6)
1476 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1477 else
1478 port = 0;
1479 addrstr = sa2str(&sa, port, 0);
1480 chunk_appendf(&trash, " laddr=%s", addrstr);
1481 free(addrstr);
1482 }
1483
1484 salen = sizeof(sa);
1485 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1486 if (sa.ss_family == AF_INET)
1487 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1488 else if (sa.ss_family == AF_INET6)
1489 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1490 else
1491 port = 0;
1492 addrstr = sa2str(&sa, port, 0);
1493 chunk_appendf(&trash, " raddr=%s", addrstr);
1494 free(addrstr);
1495 }
1496
1497 chunk_appendf(&trash, "\n");
1498
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001499 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001500 ctx->start_fd = fd;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001501 ret = 0;
1502 break;
1503 }
1504 }
1505
1506 thread_release();
1507 end:
1508 return ret;
1509}
1510
Willy Tarreaua6026a02020-07-02 09:14:48 +02001511#if defined(DEBUG_MEM_STATS)
Willy Tarreau40e952f2022-05-05 14:39:51 +02001512
1513/* CLI state for "debug dev memstats" */
1514struct dev_mem_ctx {
1515 struct mem_stats *start, *stop; /* begin/end of dump */
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001516 char *match; /* non-null if a name prefix is specified */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001517 int show_all; /* show all entries if non-null */
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001518 int width; /* 1st column width */
1519 long tot_size; /* sum of alloc-free */
1520 ulong tot_calls; /* sum of calls */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001521};
1522
1523/* CLI parser for the "debug dev memstats" command. Sets a dev_mem_ctx shown above. */
Willy Tarreaua6026a02020-07-02 09:14:48 +02001524static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1525{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001526 struct dev_mem_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau334d0912022-11-30 16:50:48 +01001527 int arg;
Willy Tarreau40e952f2022-05-05 14:39:51 +02001528
Willy Tarreaua6026a02020-07-02 09:14:48 +02001529 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1530 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1531
1532 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1533 return 1;
1534
Willy Tarreau334d0912022-11-30 16:50:48 +01001535 for (arg = 3; *args[arg]; arg++) {
1536 if (strcmp(args[arg], "reset") == 0) {
1537 struct mem_stats *ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001538
Willy Tarreau334d0912022-11-30 16:50:48 +01001539 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1540 return 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001541
Willy Tarreau334d0912022-11-30 16:50:48 +01001542 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1543 _HA_ATOMIC_STORE(&ptr->calls, 0);
1544 _HA_ATOMIC_STORE(&ptr->size, 0);
1545 }
1546 return 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001547 }
Willy Tarreau334d0912022-11-30 16:50:48 +01001548 else if (strcmp(args[arg], "all") == 0) {
1549 ctx->show_all = 1;
1550 continue;
1551 }
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001552 else if (strcmp(args[arg], "match") == 0 && *args[arg + 1]) {
1553 ha_free(&ctx->match);
1554 ctx->match = strdup(args[arg + 1]);
1555 arg++;
1556 continue;
1557 }
Willy Tarreau334d0912022-11-30 16:50:48 +01001558 else
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001559 return cli_err(appctx, "Expects either 'reset', 'all', or 'match <pfx>'.\n");
Willy Tarreaua6026a02020-07-02 09:14:48 +02001560 }
1561
Willy Tarreaua6026a02020-07-02 09:14:48 +02001562 /* otherwise proceed with the dump from p0 to p1 */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001563 ctx->start = &__start_mem_stats;
1564 ctx->stop = &__stop_mem_stats;
Willy Tarreau4a426e22022-08-09 08:51:08 +02001565 ctx->width = 0;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001566 return 0;
1567}
1568
Willy Tarreau40e952f2022-05-05 14:39:51 +02001569/* CLI I/O handler for the "debug dev memstats" command using a dev_mem_ctx
1570 * found in appctx->svcctx. Dumps all mem_stats structs referenced by pointers
1571 * located between ->start and ->stop. Dumps all entries if ->show_all != 0,
1572 * otherwise only non-zero calls.
Willy Tarreaua6026a02020-07-02 09:14:48 +02001573 */
1574static int debug_iohandler_memstats(struct appctx *appctx)
1575{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001576 struct dev_mem_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001577 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001578 struct mem_stats *ptr;
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001579 const char *pfx = ctx->match;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001580 int ret = 1;
1581
Christopher Faulet87633c32023-04-03 18:32:50 +02001582 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02001583 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreaua6026a02020-07-02 09:14:48 +02001584 goto end;
1585
Willy Tarreau4a426e22022-08-09 08:51:08 +02001586 if (!ctx->width) {
1587 /* we don't know the first column's width, let's compute it
1588 * now based on a first pass on printable entries and their
1589 * expected width (approximated).
1590 */
1591 for (ptr = ctx->start; ptr != ctx->stop; ptr++) {
1592 const char *p, *name;
1593 int w = 0;
1594 char tmp;
1595
1596 if (!ptr->size && !ptr->calls && !ctx->show_all)
1597 continue;
1598
Willy Tarreaud96d2142022-09-06 08:05:59 +02001599 for (p = name = ptr->caller.file; *p; p++) {
Willy Tarreau4a426e22022-08-09 08:51:08 +02001600 if (*p == '/')
1601 name = p + 1;
1602 }
1603
1604 if (ctx->show_all)
Willy Tarreaud96d2142022-09-06 08:05:59 +02001605 w = snprintf(&tmp, 0, "%s(%s:%d) ", ptr->caller.func, name, ptr->caller.line);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001606 else
Willy Tarreaud96d2142022-09-06 08:05:59 +02001607 w = snprintf(&tmp, 0, "%s:%d ", name, ptr->caller.line);
Willy Tarreau4a426e22022-08-09 08:51:08 +02001608
1609 if (w > ctx->width)
1610 ctx->width = w;
1611 }
1612 }
1613
Willy Tarreaua6026a02020-07-02 09:14:48 +02001614 /* we have two inner loops here, one for the proxy, the other one for
1615 * the buffer.
1616 */
Willy Tarreau4a426e22022-08-09 08:51:08 +02001617 for (ptr = ctx->start; ptr != ctx->stop; ptr++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001618 const char *type;
1619 const char *name;
1620 const char *p;
Willy Tarreau55c950b2022-08-09 08:15:27 +02001621 const char *info = NULL;
Willy Tarreau17200dd2022-08-09 08:40:08 +02001622 const char *func = NULL;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001623 int direction = 0; // neither alloc nor free (e.g. realloc)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001624
Willy Tarreau40e952f2022-05-05 14:39:51 +02001625 if (!ptr->size && !ptr->calls && !ctx->show_all)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001626 continue;
1627
1628 /* basename only */
Willy Tarreaud96d2142022-09-06 08:05:59 +02001629 for (p = name = ptr->caller.file; *p; p++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001630 if (*p == '/')
1631 name = p + 1;
1632 }
1633
Willy Tarreaud96d2142022-09-06 08:05:59 +02001634 func = ptr->caller.func;
Willy Tarreau17200dd2022-08-09 08:40:08 +02001635
Willy Tarreaud96d2142022-09-06 08:05:59 +02001636 switch (ptr->caller.what) {
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001637 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; direction = 1; break;
1638 case MEM_STATS_TYPE_FREE: type = "FREE"; direction = -1; break;
1639 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; direction = 1; break;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001640 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001641 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; direction = 1; break;
1642 case MEM_STATS_TYPE_P_ALLOC: type = "P_ALLOC"; direction = 1; if (ptr->extra) info = ((const struct pool_head *)ptr->extra)->name; break;
1643 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 +02001644 default: type = "UNSET"; break;
1645 }
1646
1647 //chunk_printf(&trash,
1648 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1649 // name, ptr->line, type,
1650 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1651 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1652
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001653 /* only match requested prefixes */
1654 if (pfx && (!info || strncmp(info, pfx, strlen(pfx)) != 0))
1655 continue;
1656
Willy Tarreau17200dd2022-08-09 08:40:08 +02001657 chunk_reset(&trash);
1658 if (ctx->show_all)
1659 chunk_appendf(&trash, "%s(", func);
1660
Willy Tarreaud96d2142022-09-06 08:05:59 +02001661 chunk_appendf(&trash, "%s:%d", name, ptr->caller.line);
Willy Tarreau17200dd2022-08-09 08:40:08 +02001662
1663 if (ctx->show_all)
1664 chunk_appendf(&trash, ")");
1665
Willy Tarreau4a426e22022-08-09 08:51:08 +02001666 while (trash.data < ctx->width)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001667 trash.area[trash.data++] = ' ';
Willy Tarreau17200dd2022-08-09 08:40:08 +02001668
Willy Tarreau55c950b2022-08-09 08:15:27 +02001669 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu %s\n",
Willy Tarreaua6026a02020-07-02 09:14:48 +02001670 type,
1671 (unsigned long)ptr->size, (unsigned long)ptr->calls,
Willy Tarreau55c950b2022-08-09 08:15:27 +02001672 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0),
1673 info ? info : "");
Willy Tarreaua6026a02020-07-02 09:14:48 +02001674
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001675 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau40e952f2022-05-05 14:39:51 +02001676 ctx->start = ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001677 ret = 0;
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001678 goto end;
1679 }
1680 if (direction > 0) {
1681 ctx->tot_size += (ulong)ptr->size;
1682 ctx->tot_calls += (ulong)ptr->calls;
1683 }
1684 else if (direction < 0) {
1685 ctx->tot_size -= (ulong)ptr->size;
1686 ctx->tot_calls += (ulong)ptr->calls;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001687 }
1688 }
1689
Willy Tarreaub59e3f62022-11-30 17:16:51 +01001690 /* now dump a summary */
1691 chunk_reset(&trash);
1692 chunk_appendf(&trash, "Total");
1693 while (trash.data < ctx->width)
1694 trash.area[trash.data++] = ' ';
1695
1696 chunk_appendf(&trash, "%7s size: %12ld calls: %9lu size/call: %6ld %s\n",
1697 "BALANCE",
1698 ctx->tot_size, ctx->tot_calls,
1699 (long)(ctx->tot_calls ? (ctx->tot_size / ctx->tot_calls) : 0),
1700 "(excl. realloc)");
1701
1702 if (applet_putchk(appctx, &trash) == -1) {
1703 ctx->start = ptr;
1704 ret = 0;
1705 goto end;
1706 }
Willy Tarreaua6026a02020-07-02 09:14:48 +02001707 end:
1708 return ret;
1709}
1710
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001711/* release the "show pools" context */
1712static void debug_release_memstats(struct appctx *appctx)
1713{
1714 struct dev_mem_ctx *ctx = appctx->svcctx;
1715
1716 ha_free(&ctx->match);
1717}
Willy Tarreaua6026a02020-07-02 09:14:48 +02001718#endif
1719
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +02001720#ifdef USE_THREAD_DUMP
Willy Tarreauc7091d82019-05-17 10:08:49 +02001721
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001722/* handles DEBUGSIG to dump the state of the thread it's working on. This is
1723 * appended at the end of thread_dump_buffer which must be protected against
1724 * reentrance from different threads (a thread-local buffer works fine).
1725 */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001726void debug_handler(int sig, siginfo_t *si, void *arg)
1727{
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +02001728 struct buffer *buf = HA_ATOMIC_LOAD(&th_ctx->thread_dump_buffer);
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001729 int harmless = is_thread_harmless();
1730
Willy Tarreau82aafc42020-03-03 08:31:34 +01001731 /* first, let's check it's really for us and that we didn't just get
1732 * a spurious DEBUGSIG.
1733 */
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +02001734 if (!buf || buf == (void*)(0x1UL))
Willy Tarreau82aafc42020-03-03 08:31:34 +01001735 return;
1736
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +02001737 /* now dump the current state into the designated buffer, and indicate
1738 * we come from a sig handler.
Willy Tarreauf0c86dd2022-07-15 13:14:03 +02001739 */
Willy Tarreau9a6ecbd2023-05-04 16:23:51 +02001740 ha_thread_dump_one(tid, 1);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001741
1742 /* mark the current thread as stuck to detect it upon next invocation
1743 * if it didn't move.
1744 */
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001745 if (!harmless &&
Willy Tarreaue7475c82022-06-20 09:23:24 +02001746 !(_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_SLEEPING))
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001747 _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_STUCK);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001748}
1749
1750static int init_debug_per_thread()
1751{
1752 sigset_t set;
1753
1754 /* unblock the DEBUGSIG signal we intend to use */
1755 sigemptyset(&set);
1756 sigaddset(&set, DEBUGSIG);
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001757#if defined(DEBUG_DEV)
1758 sigaddset(&set, SIGRTMAX);
1759#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +02001760 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1761 return 1;
1762}
1763
1764static int init_debug()
1765{
1766 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001767 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001768
Willy Tarreau0214b452020-03-04 06:01:40 +01001769 /* calling backtrace() will access libgcc at runtime. We don't want to
1770 * do it after the chroot, so let's perform a first call to have it
1771 * ready in memory for later use.
1772 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001773 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001774 sa.sa_handler = NULL;
1775 sa.sa_sigaction = debug_handler;
1776 sigemptyset(&sa.sa_mask);
1777 sa.sa_flags = SA_SIGINFO;
1778 sigaction(DEBUGSIG, &sa, NULL);
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001779
1780#if defined(DEBUG_DEV)
1781 sa.sa_handler = NULL;
1782 sa.sa_sigaction = debug_delay_inj_sighandler;
1783 sigemptyset(&sa.sa_mask);
1784 sa.sa_flags = SA_SIGINFO;
1785 sigaction(SIGRTMAX, &sa, NULL);
1786#endif
Christopher Fauletfc633b62020-11-06 15:24:23 +01001787 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001788}
1789
1790REGISTER_POST_CHECK(init_debug);
1791REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1792
1793#endif /* USE_THREAD_DUMP */
1794
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001795/* register cli keywords */
1796static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001797 {{ "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 +01001798 {{ "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 +02001799 {{ "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 +02001800 {{ "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 +02001801 {{ "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 +02001802#if defined(DEBUG_DEV)
Willy Tarreaubd3b44e2023-03-09 08:25:01 +01001803 {{ "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 +02001804 {{ "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 +02001805#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001806 {{ "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 +02001807 {{ "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 +01001808 {{ "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 +02001809 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1810 {{ "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 +02001811 {{ "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 +02001812#if defined(DEBUG_MEM_STATS)
Willy Tarreaue57fbed2022-11-30 16:42:54 +01001813 {{ "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 +02001814#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001815 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1816 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1817 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1818 {{ "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 +02001819 {{ "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 +02001820 {{ "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 +01001821 {{ "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 +02001822 {{ "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 +02001823
Willy Tarreau6ab7b212021-12-28 09:57:10 +01001824#if defined(HA_HAVE_DUMP_LIBS)
1825 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1826#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001827 {{ "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 +02001828 {{},}
1829}};
1830
1831INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);