blob: 8d6210828ff7009da4319f2b615c2b31b7931396 [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 Tarreau00c27b52022-07-01 19:08:56 +020050/* The dump state is madee of:
51 * - num_thread+1 on the lowest 16 bits
52 * - a counter of done on the 16 high bits
53 * Initiating a dump consists in setting it to 1. A thread finished dumping
54 * adds 1<<16 and sets the lowest 15 bits to the ID of the next thread to
55 * dump + 1. Only used when USE_THREAD_DUMP is set.
Willy Tarreaua37cb182019-07-31 19:20:39 +020056 */
Willy Tarreau89ed89e2022-07-01 21:18:03 +020057volatile unsigned int thread_dump_state = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020058unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020059
Willy Tarreau123fc972021-01-22 13:52:41 +010060/* dumps a backtrace of the current thread that is appended to buffer <buf>.
61 * Lines are prefixed with the string <prefix> which may be empty (used for
62 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010063 * the function does not appear in the call stack. The <dump> argument
64 * indicates what dump state to start from, and should usually be zero. It
65 * may be among the following values:
66 * - 0: search usual callers before step 1, or directly jump to 2
67 * - 1: skip usual callers before step 2
68 * - 2: dump until polling loop, scheduler, or main() (excluded)
69 * - 3: end
70 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010071 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010072void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010073{
74 struct buffer bak;
75 char pfx2[100];
76 void *callers[100];
77 int j, nptrs;
78 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010079
80 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
81 if (!nptrs)
82 return;
83
84 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
85 pfx2[0] = 0;
86
87 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
88 * produce similar output to the following:
89 */
90 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010091 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
92 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010093 /* we failed to spot the starting point of the
94 * dump, let's start over dumping everything we
95 * have.
96 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010097 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010098 j = 0;
99 }
100 bak = *buf;
101 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
102 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100103 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100104 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +0100105 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +0100106 */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100107 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
108 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100109 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100110 *buf = bak;
111 continue;
112 }
113
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100114 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100115 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100116 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
117 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100118 *buf = bak;
119 continue;
120 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100121 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100122 }
123
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100124 if ((dump & 3) == 2) {
125 /* still dumping */
126 if (dump == 6) {
127 /* we only stop *after* main and we must send the LF */
128 if (addr == main) {
129 j = nptrs;
130 dump++;
131 }
132 }
133 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
134 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100135 *buf = bak;
136 break;
137 }
138 }
139 /* OK, line dumped */
140 chunk_appendf(buf, "\n");
141 }
142}
143
Willy Tarreaua8459b22021-01-22 14:12:27 +0100144/* dump a backtrace of current thread's stack to stderr. */
Willy Tarreau7831e022022-05-06 15:16:19 +0200145void ha_backtrace_to_stderr(void)
Willy Tarreaua8459b22021-01-22 14:12:27 +0100146{
147 char area[2048];
148 struct buffer b = b_make(area, sizeof(area), 0, 0);
149
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100150 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100151 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100152 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100153}
154
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200155/* Dumps to the buffer some known information for the desired thread, and
156 * optionally extra info for the current thread. The dump will be appended to
157 * the buffer, so the caller is responsible for preliminary initializing it.
158 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200159 * in front of the calling thread's line (usually it's tid). Any stuck thread
160 * is also prefixed with a '>'.
Willy Tarreaubdcd3252022-06-22 09:19:46 +0200161 * It must be called under thread isolation.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200162 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200163void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200164{
Willy Tarreau38d07122022-06-27 16:13:50 +0200165 unsigned long thr_bit = ha_thread_info[thr].ltid_bit;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200166 unsigned long long p = ha_thread_ctx[thr].prev_cpu_time;
Willy Tarreau21694982021-10-08 15:09:17 +0200167 unsigned long long n = now_cpu_time_thread(thr);
Willy Tarreaua0b99532021-09-30 18:48:37 +0200168 int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK);
Willy Tarreau03f9b352022-06-27 16:02:24 +0200169 int tgrp = ha_thread_info[thr].tgid;
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200171 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200172 "%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 +0200173 " %2u/%-2u stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200174 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200175 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200176 thread_has_tasks(),
Willy Tarreauc958c702022-06-16 15:59:36 +0200177 !eb_is_empty(&ha_thread_ctx[thr].rqueue_shared),
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200178 !eb_is_empty(&ha_thread_ctx[thr].timers),
179 !eb_is_empty(&ha_thread_ctx[thr].rqueue),
180 !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) &&
181 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) &&
182 LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) &&
183 MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)),
184 ha_thread_ctx[thr].tasks_in_list,
185 ha_thread_ctx[thr].rq_total,
Willy Tarreau66ad98a2022-06-28 10:49:57 +0200186 ha_thread_info[thr].tgid, ha_thread_info[thr].ltid + 1,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200187 stuck,
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200188 !!(th_ctx->flags & TH_FL_TASK_PROFILING));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200189
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200190 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200191 " harmless=%d wantrdv=%d",
Willy Tarreau03f9b352022-06-27 16:02:24 +0200192 !!(_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp-1].threads_harmless) & thr_bit),
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200193 !!(th_ctx->flags & TH_FL_TASK_PROFILING));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200194
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200195 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200196 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200197
Willy Tarreaua3870b72021-09-13 19:24:47 +0200198 /* this is the end of what we can dump from outside the current thread */
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200199
200 if (thr != tid)
201 return;
202
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200203 chunk_appendf(buf, " curr_task=");
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200204 ha_task_dump(buf, th_ctx->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100205
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100206 if (stuck) {
207 /* We only emit the backtrace for stuck threads in order not to
208 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100209 * Please leave this as the last instruction in this function
210 * so that the compiler uses tail merging and the current
211 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100212 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100213 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100214 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200215}
216
217
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200218/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200219 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200220 * with <pfx>. The buffer is only appended and the first output starts by the
221 * pointer itself. The caller is responsible for making sure the task is not
222 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200223 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200224void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200225{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200226 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200227 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200228 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200229
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200230 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200231 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200232 return;
233 }
234
Willy Tarreau20db9112019-05-17 14:14:35 +0200235 if (TASK_IS_TASKLET(task))
236 chunk_appendf(buf,
237 "%p (tasklet) calls=%u\n",
238 task,
239 task->calls);
240 else
241 chunk_appendf(buf,
242 "%p (task) calls=%u last=%llu%s\n",
243 task,
244 task->calls,
245 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
246 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200247
Willy Tarreau2e89b092020-03-03 17:13:02 +0100248 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
249 resolve_sym_name(buf, NULL, task->process);
250 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200251
Willy Tarreaua512b022019-08-21 14:12:19 +0200252 if (task->process == task_run_applet && (appctx = task->context))
253 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
254 else
255 chunk_appendf(buf, "\n");
256
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200257 if (task->process == process_stream && task->context)
258 s = (struct stream *)task->context;
259 else if (task->process == task_run_applet && task->context)
Willy Tarreauc12b3212022-05-27 11:08:15 +0200260 s = sc_strm(appctx_sc((struct appctx *)task->context));
Willy Tarreau462b9892022-05-18 18:06:53 +0200261 else if (task->process == sc_conn_io_cb && task->context)
Willy Tarreauea27f482022-05-18 16:10:52 +0200262 s = sc_strm(((struct stconn *)task->context));
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200263
264 if (s)
265 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200266
267#ifdef USE_LUA
268 hlua = NULL;
269 if (s && (hlua = s->hlua)) {
270 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
271 }
272 else if (task->process == hlua_process_task && (hlua = task->context)) {
273 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
274 }
275 else if (task->process == task_run_applet && (appctx = task->context) &&
Willy Tarreaue23f33b2022-05-06 14:07:13 +0200276 (appctx->applet->fct == hlua_applet_tcp_fct)) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200277 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
278 }
279 else if (task->process == task_run_applet && (appctx = task->context) &&
Willy Tarreauaa229cc2022-05-06 14:26:10 +0200280 (appctx->applet->fct == hlua_applet_http_fct)) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200281 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
282 }
283
Christopher Faulet471425f2020-07-24 19:08:05 +0200284 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100285 chunk_appendf(buf, "stack traceback:\n ");
286 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
287 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200288 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200289 else
290 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200291#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200292}
293
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200294/* This function dumps all profiling settings. It returns 0 if the output
295 * buffer is full and it needs to be called again, otherwise non-zero.
296 */
297static int cli_io_handler_show_threads(struct appctx *appctx)
298{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200299 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200300 int thr;
301
Willy Tarreau475e4632022-05-27 10:26:46 +0200302 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200303 return 1;
304
305 if (appctx->st0)
306 thr = appctx->st1;
307 else
308 thr = 0;
309
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200310 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200311 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200312
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200313 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200314 /* failed, try again */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200315 appctx->st1 = thr;
316 return 0;
317 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200318 return 1;
319}
320
Willy Tarreau6ab7b212021-12-28 09:57:10 +0100321#if defined(HA_HAVE_DUMP_LIBS)
322/* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */
323static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private)
324{
325 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
326 return 1;
327
328 chunk_reset(&trash);
329 if (dump_libs(&trash, 1))
330 return cli_msg(appctx, LOG_INFO, trash.area);
331 else
332 return 0;
333}
334#endif
335
Willy Tarreau56131ca2019-05-20 13:48:29 +0200336/* dumps a state of all threads into the trash and on fd #2, then aborts. */
337void ha_panic()
338{
339 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200340 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200341 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100342 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200343 for (;;)
344 abort();
345}
346
Willy Tarreau06e66c82022-03-02 15:52:03 +0100347/* Complain with message <msg> on stderr. If <counter> is not NULL, it is
348 * atomically incremented, and the message is only printed when the counter
349 * was zero, so that the message is only printed once. <taint> is only checked
350 * on bit 1, and will taint the process either for a bug (2) or warn (0).
351 */
352void complain(int *counter, const char *msg, int taint)
353{
354 if (counter && _HA_ATOMIC_FETCH_ADD(counter, 1))
355 return;
356 DISGUISE(write(2, msg, strlen(msg)));
357 if (taint & 2)
358 mark_tainted(TAINTED_BUG);
359 else
360 mark_tainted(TAINTED_WARN);
361}
362
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
364static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
365{
366 int code = atoi(args[3]);
367
368 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
369 return 1;
370
Willy Tarreau4781b152021-04-06 13:53:36 +0200371 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200372 exit(code);
373 return 1;
374}
375
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100376/* parse a "debug dev bug" command. It always returns 1, though it should never return.
377 * Note: we make sure not to make the function static so that it appears in the trace.
378 */
379int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
380{
381 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
382 return 1;
383
Willy Tarreau4781b152021-04-06 13:53:36 +0200384 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100385 BUG_ON(one > zero);
386 return 1;
387}
388
Willy Tarreau305cfbd2022-02-25 08:52:39 +0100389/* parse a "debug dev warn" command. It always returns 1.
390 * Note: we make sure not to make the function static so that it appears in the trace.
391 */
392int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
393{
394 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
395 return 1;
396
397 _HA_ATOMIC_INC(&debug_commands_issued);
398 WARN_ON(one > zero);
399 return 1;
400}
401
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100402/* parse a "debug dev check" command. It always returns 1.
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100403 * Note: we make sure not to make the function static so that it appears in the trace.
404 */
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100405int debug_parse_cli_check(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100406{
407 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
408 return 1;
409
410 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6d3f1e32022-02-28 11:51:23 +0100411 CHECK_IF(one > zero);
Willy Tarreau4e0a8b12022-02-25 08:55:11 +0100412 return 1;
413}
414
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200415/* parse a "debug dev close" command. It always returns 1. */
416static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
417{
418 int fd;
419
420 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
421 return 1;
422
Willy Tarreau9d008692019-08-09 11:21:01 +0200423 if (!*args[3])
424 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200425
426 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200427 if (fd < 0 || fd >= global.maxsock)
428 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200429
Willy Tarreau9d008692019-08-09 11:21:01 +0200430 if (!fdtab[fd].owner)
431 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200432
Willy Tarreau4781b152021-04-06 13:53:36 +0200433 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200434 fd_delete(fd);
435 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200436}
437
438/* parse a "debug dev delay" command. It always returns 1. */
439static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
440{
441 int delay = atoi(args[3]);
442
443 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
444 return 1;
445
Willy Tarreau4781b152021-04-06 13:53:36 +0200446 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200447 usleep((long)delay * 1000);
448 return 1;
449}
450
451/* parse a "debug dev log" command. It always returns 1. */
452static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
453{
454 int arg;
455
456 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
457 return 1;
458
Willy Tarreau4781b152021-04-06 13:53:36 +0200459 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200460 chunk_reset(&trash);
461 for (arg = 3; *args[arg]; arg++) {
462 if (arg > 3)
463 chunk_strcat(&trash, " ");
464 chunk_strcat(&trash, args[arg]);
465 }
466
467 send_log(NULL, LOG_INFO, "%s\n", trash.area);
468 return 1;
469}
470
471/* parse a "debug dev loop" command. It always returns 1. */
472static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
473{
474 struct timeval deadline, curr;
475 int loop = atoi(args[3]);
476
477 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
478 return 1;
479
Willy Tarreau4781b152021-04-06 13:53:36 +0200480 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200481 gettimeofday(&curr, NULL);
482 tv_ms_add(&deadline, &curr, loop);
483
484 while (tv_ms_cmp(&curr, &deadline) < 0)
485 gettimeofday(&curr, NULL);
486
487 return 1;
488}
489
490/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
491static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
492{
493 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
494 return 1;
495
Willy Tarreau4781b152021-04-06 13:53:36 +0200496 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200497 ha_panic();
498 return 1;
499}
500
501/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200502#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200503static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
504{
Willy Tarreau368bff42019-12-06 17:18:28 +0100505 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200506 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100507 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200508
509 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
510 return 1;
511
Willy Tarreau4781b152021-04-06 13:53:36 +0200512 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200513 chunk_reset(&trash);
514 for (arg = 3; *args[arg]; arg++) {
515 if (arg > 3)
516 chunk_strcat(&trash, " ");
517 chunk_strcat(&trash, args[arg]);
518 }
519
Willy Tarreau368bff42019-12-06 17:18:28 +0100520 thread_isolate();
521 if (pipe(pipefd) < 0)
522 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200523
Willy Tarreau38247432022-04-26 10:24:14 +0200524 if (fd_set_cloexec(pipefd[0]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100525 goto fail_fcntl;
526
Willy Tarreau38247432022-04-26 10:24:14 +0200527 if (fd_set_cloexec(pipefd[1]) == -1)
Willy Tarreau368bff42019-12-06 17:18:28 +0100528 goto fail_fcntl;
529
530 pid = fork();
531
532 if (pid < 0)
533 goto fail_fork;
534 else if (pid == 0) {
535 /* child */
536 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
537
538 close(0);
539 dup2(pipefd[1], 1);
540 dup2(pipefd[1], 2);
541
542 cmd[2] = trash.area;
543 execvp(cmd[0], cmd);
544 printf("execvp() failed\n");
545 exit(1);
546 }
547
548 /* parent */
549 thread_release();
550 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200551 chunk_reset(&trash);
552 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100553 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
554 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200555 break;
556 trash.data += ret;
557 if (trash.data + 20 == trash.size) {
558 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
559 break;
560 }
561 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100562 close(pipefd[0]);
563 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200564 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200565 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100566
567 fail_fork:
568 fail_fcntl:
569 close(pipefd[0]);
570 close(pipefd[1]);
571 fail_pipe:
572 thread_release();
573 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200574}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200575#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200576
577/* parse a "debug dev hex" command. It always returns 1. */
578static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
579{
580 unsigned long start, len;
581
582 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
583 return 1;
584
Willy Tarreau9d008692019-08-09 11:21:01 +0200585 if (!*args[3])
586 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200587
588 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200589 if (!start)
590 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200591
Willy Tarreau4781b152021-04-06 13:53:36 +0200592 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200593
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200594 /* by default, dump ~128 till next block of 16 */
595 len = strtoul(args[4], NULL, 0);
596 if (!len)
597 len = ((start + 128) & -16) - start;
598
599 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200600 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200601 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200602 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200603}
604
Willy Tarreau48129be2021-05-04 18:40:50 +0200605/* parse a "debug dev sym <addr>" command. It always returns 1. */
606static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
607{
608 unsigned long addr;
609
610 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
611 return 1;
612
613 if (!*args[3])
614 return cli_err(appctx, "Missing memory address to be resolved.\n");
615
616 _HA_ATOMIC_INC(&debug_commands_issued);
617
618 addr = strtoul(args[3], NULL, 0);
619 chunk_printf(&trash, "%#lx resolves to ", addr);
620 resolve_sym_name(&trash, NULL, (const void *)addr);
621 chunk_appendf(&trash, "\n");
622
623 return cli_msg(appctx, LOG_INFO, trash.area);
624}
625
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200626/* parse a "debug dev tkill" command. It always returns 1. */
627static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
628{
629 int thr = 0;
630 int sig = SIGABRT;
631
632 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
633 return 1;
634
635 if (*args[3])
636 thr = atoi(args[3]);
637
Willy Tarreau9d008692019-08-09 11:21:01 +0200638 if (thr < 0 || thr > global.nbthread)
639 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200640
641 if (*args[4])
642 sig = atoi(args[4]);
643
Willy Tarreau4781b152021-04-06 13:53:36 +0200644 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200645 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200646 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200647 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200648 raise(sig);
649 return 1;
650}
651
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100652/* parse a "debug dev write" command. It always returns 1. */
653static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
654{
655 unsigned long len;
656
657 if (!*args[3])
658 return cli_err(appctx, "Missing output size.\n");
659
660 len = strtoul(args[3], NULL, 0);
661 if (len >= trash.size)
662 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
663
Willy Tarreau4781b152021-04-06 13:53:36 +0200664 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100665
666 chunk_reset(&trash);
667 trash.data = len;
668 memset(trash.area, '.', trash.data);
669 trash.area[trash.data] = 0;
670 for (len = 64; len < trash.data; len += 64)
671 trash.area[len] = '\n';
672 return cli_msg(appctx, LOG_INFO, trash.area);
673}
674
Willy Tarreau68680bb2019-10-23 17:23:25 +0200675/* parse a "debug dev stream" command */
676/*
677 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
678 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
679 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
680 * [sif.s[=<state>]] [sib.s[=<state>]]
681 */
682static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
683{
Willy Tarreau0698c802022-05-11 14:09:57 +0200684 struct stream *s = appctx_strm(appctx);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200685 int arg;
686 void *ptr;
687 int size;
688 const char *word, *end;
689 struct ist name;
690 char *msg = NULL;
691 char *endarg;
692 unsigned long long old, new;
693
694 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
695 return 1;
696
697 ptr = NULL; size = 0;
698
699 if (!*args[3]) {
700 return cli_err(appctx,
701 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200702 " <obj> = {strm | strm.f | strm.x | scf.s | scb.s |\n"
Willy Tarreau68680bb2019-10-23 17:23:25 +0200703 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
704 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
705 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
706 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
707 );
708 }
709
Willy Tarreau4781b152021-04-06 13:53:36 +0200710 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200711 for (arg = 3; *args[arg]; arg++) {
712 old = 0;
713 end = word = args[arg];
714 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
715 end++;
716 name = ist2(word, end - word);
717 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200718 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200719 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200720 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200721 } else if (isteq(name, ist("strm.x"))) {
722 ptr = (!s || !may_access(s)) ? NULL : &s->conn_exp; size = sizeof(s->conn_exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200723 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200724 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200725 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200726 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200727 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200728 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200729 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200730 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200731 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200732 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200733 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200734 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200735 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200736 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200737 } else if (isteq(name, ist("scf.s"))) {
738 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scf->state);
739 } else if (isteq(name, ist("scb.s"))) {
740 ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scb->state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200741 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200742 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200743 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
744 continue;
745 } else
746 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
747
748 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200749 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200750 if (size == 8)
751 old = read_u64(ptr);
752 else if (size == 4)
753 old = read_u32(ptr);
754 else if (size == 2)
755 old = read_u16(ptr);
756 else
757 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200758 } else {
759 memprintf(&msg,
760 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
761 msg ? msg : "", ptr, (int)(end - word), word);
762 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200763 }
764
765 /* parse the new value . */
766 new = strtoll(end + 1, &endarg, 0);
767 if (end[1] && *endarg) {
768 if (strcmp(end + 1, "now") == 0)
769 new = now_ms;
770 else {
771 memprintf(&msg,
772 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
773 msg ? msg : "", end + 1, (int)(end - word), word);
774 continue;
775 }
776 }
777
778 switch (*end) {
779 case '\0': /* show */
780 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
781 new = old; // do not change the value
782 break;
783
784 case '=': /* set */
785 break;
786
787 case '^': /* XOR */
788 new = old ^ new;
789 break;
790
791 case '+': /* OR */
792 new = old | new;
793 break;
794
795 case '-': /* AND NOT */
796 new = old & ~new;
797 break;
798
799 default:
800 break;
801 }
802
803 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200804 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200805 if (size == 8)
806 write_u64(ptr, new);
807 else if (size == 4)
808 write_u32(ptr, new);
809 else if (size == 2)
810 write_u16(ptr, new);
811 else
812 *(uint8_t *)ptr = new;
813 }
814 }
815
816 if (msg && *msg)
817 return cli_dynmsg(appctx, LOG_INFO, msg);
818 return 1;
819}
820
Willy Tarreau144f84a2021-03-02 16:09:26 +0100821static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100822{
823 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
824 unsigned long inter = tctx[1];
825 unsigned long rnd;
826
827 t->expire = tick_add(now_ms, inter);
828
829 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100830 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100831 if (rnd & 1) {
832 rnd >>= 1;
833 rnd %= tctx[0];
834 rnd = tctx[rnd + 2];
835
836 if (rnd & 1)
837 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
838 else
839 tasklet_wakeup((struct tasklet *)rnd);
840 }
841 return t;
842}
843
Willy Tarreau144f84a2021-03-02 16:09:26 +0100844static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100845{
846 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
847 unsigned long rnd;
848 int i;
849
850 /* wake up two random entries */
851 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100852 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100853 rnd = tctx[rnd + 2];
854
855 if (rnd & 1)
856 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
857 else
858 tasklet_wakeup((struct tasklet *)rnd);
859 }
860 return t;
861}
862
863/* parse a "debug dev sched" command
864 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
865 */
866static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
867{
868 int arg;
869 void *ptr;
870 int size;
871 const char *word, *end;
872 struct ist name;
873 char *msg = NULL;
874 char *endarg;
875 unsigned long long new;
876 unsigned long count = 0;
Willy Tarreau8e5c53a2022-06-15 16:32:41 +0200877 unsigned long thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +0100878 unsigned int inter = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +0100879 unsigned long i;
880 int mode = 0; // 0 = tasklet; 1 = task
Willy Tarreaua5a44792020-11-29 17:12:15 +0100881 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
882
883 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
884 return 1;
885
886 ptr = NULL; size = 0;
Willy Tarreaua5a44792020-11-29 17:12:15 +0100887
888 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
889 return cli_err(appctx,
890 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
Willy Tarreau8e5c53a2022-06-15 16:32:41 +0200891 " <obj> = {count | tid | inter }\n"
Willy Tarreaua5a44792020-11-29 17:12:15 +0100892 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
893 );
894 }
895
896 mode = strcmp(args[3], "task") == 0;
897
Willy Tarreau4781b152021-04-06 13:53:36 +0200898 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100899 for (arg = 4; *args[arg]; arg++) {
900 end = word = args[arg];
901 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
902 end++;
903 name = ist2(word, end - word);
904 if (isteq(name, ist("count"))) {
905 ptr = &count; size = sizeof(count);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100906 } else if (isteq(name, ist("tid"))) {
907 ptr = &thrid; size = sizeof(thrid);
908 } else if (isteq(name, ist("inter"))) {
909 ptr = &inter; size = sizeof(inter);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100910 } else
911 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
912
913 /* parse the new value . */
914 new = strtoll(end + 1, &endarg, 0);
915 if (end[1] && *endarg) {
916 memprintf(&msg,
917 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
918 msg ? msg : "", end + 1, (int)(end - word), word);
919 continue;
920 }
921
922 /* write the new value */
923 if (size == 8)
924 write_u64(ptr, new);
925 else if (size == 4)
926 write_u32(ptr, new);
927 else if (size == 2)
928 write_u16(ptr, new);
929 else
930 *(uint8_t *)ptr = new;
931 }
932
933 tctx = calloc(sizeof(*tctx), count + 2);
934 if (!tctx)
935 goto fail;
936
937 tctx[0] = (unsigned long)count;
938 tctx[1] = (unsigned long)inter;
939
Willy Tarreau8e5c53a2022-06-15 16:32:41 +0200940 if (thrid >= global.nbthread)
941 thrid = tid;
Willy Tarreaua5a44792020-11-29 17:12:15 +0100942
Willy Tarreaua5a44792020-11-29 17:12:15 +0100943 for (i = 0; i < count; i++) {
Willy Tarreaua5a44792020-11-29 17:12:15 +0100944 /* now, if poly or mask was set, tmask corresponds to the
945 * valid thread mask to use, otherwise it remains zero.
946 */
947 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
948 if (mode == 0) {
949 struct tasklet *tl = tasklet_new();
950
951 if (!tl)
952 goto fail;
953
Willy Tarreau8e5c53a2022-06-15 16:32:41 +0200954 tl->tid = thrid;
Willy Tarreaua5a44792020-11-29 17:12:15 +0100955 tl->process = debug_tasklet_handler;
956 tl->context = tctx;
957 tctx[i + 2] = (unsigned long)tl;
958 } else {
Willy Tarreau8e5c53a2022-06-15 16:32:41 +0200959 struct task *task = task_new_on(thrid);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100960
961 if (!task)
962 goto fail;
963
964 task->process = debug_task_handler;
965 task->context = tctx;
966 tctx[i + 2] = (unsigned long)task + 1;
967 }
968 }
969
970 /* start the tasks and tasklets */
971 for (i = 0; i < count; i++) {
972 unsigned long ctx = tctx[i + 2];
973
974 if (ctx & 1)
975 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
976 else
977 tasklet_wakeup((struct tasklet *)ctx);
978 }
979
980 if (msg && *msg)
981 return cli_dynmsg(appctx, LOG_INFO, msg);
982 return 1;
983
984 fail:
985 /* free partially allocated entries */
986 for (i = 0; tctx && i < count; i++) {
987 unsigned long ctx = tctx[i + 2];
988
989 if (!ctx)
990 break;
991
992 if (ctx & 1)
993 task_destroy((struct task *)(ctx - 1));
994 else
995 tasklet_free((struct tasklet *)ctx);
996 }
997
998 free(tctx);
999 return cli_err(appctx, "Not enough memory");
1000}
1001
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001002/* CLI state for "debug dev fd" */
1003struct dev_fd_ctx {
1004 int start_fd;
1005};
1006
Willy Tarreau5be7c192022-01-24 20:26:09 +01001007/* CLI parser for the "debug dev fd" command. The current FD to restart from is
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001008 * stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001009 */
1010static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private)
1011{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001012 struct dev_fd_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1013
Willy Tarreau5be7c192022-01-24 20:26:09 +01001014 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1015 return 1;
1016
1017 /* start at fd #0 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001018 ctx->start_fd = 0;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001019 return 0;
1020}
1021
1022/* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are
1023 * accessible from the process but not known from fdtab. The FD number to
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001024 * restart from is stored in a struct dev_fd_ctx pointed to by svcctx.
Willy Tarreau5be7c192022-01-24 20:26:09 +01001025 */
1026static int debug_iohandler_fd(struct appctx *appctx)
1027{
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001028 struct dev_fd_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001029 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau5be7c192022-01-24 20:26:09 +01001030 struct sockaddr_storage sa;
1031 struct stat statbuf;
1032 socklen_t salen, vlen;
1033 int ret1, ret2, port;
1034 char *addrstr;
1035 int ret = 1;
1036 int i, fd;
1037
Willy Tarreau475e4632022-05-27 10:26:46 +02001038 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau5be7c192022-01-24 20:26:09 +01001039 goto end;
1040
1041 chunk_reset(&trash);
1042
1043 thread_isolate();
1044
1045 /* we have two inner loops here, one for the proxy, the other one for
1046 * the buffer.
1047 */
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001048 for (fd = ctx->start_fd; fd < global.maxsock; fd++) {
Willy Tarreau5be7c192022-01-24 20:26:09 +01001049 /* check for FD's existence */
1050 ret1 = fcntl(fd, F_GETFD, 0);
1051 if (ret1 == -1)
1052 continue; // not known to the process
1053 if (fdtab[fd].owner)
1054 continue; // well-known
1055
1056 /* OK we're seeing an orphan let's try to retrieve as much
1057 * information as possible about it.
1058 */
1059 chunk_printf(&trash, "%5d", fd);
1060
1061 if (fstat(fd, &statbuf) != -1) {
1062 chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx",
1063 isatty(fd) ? "tty.":
1064 S_ISREG(statbuf.st_mode) ? "file":
1065 S_ISDIR(statbuf.st_mode) ? "dir.":
1066 S_ISCHR(statbuf.st_mode) ? "chr.":
1067 S_ISBLK(statbuf.st_mode) ? "blk.":
1068 S_ISFIFO(statbuf.st_mode) ? "pipe":
1069 S_ISLNK(statbuf.st_mode) ? "link":
1070 S_ISSOCK(statbuf.st_mode) ? "sock":
1071#ifdef USE_EPOLL
1072 epoll_wait(fd, NULL, 0, 0) != -1 || errno != EBADF ? "epol":
1073#endif
1074 "????",
1075 (uint)statbuf.st_mode & 07777,
1076
1077 (ullong)statbuf.st_rdev,
1078 (ullong)statbuf.st_size,
1079 (ullong)statbuf.st_uid,
1080 (ullong)statbuf.st_gid,
1081
1082 (ullong)statbuf.st_dev,
1083 (ullong)statbuf.st_ino);
1084 }
1085
1086 chunk_appendf(&trash, " getfd=%s+%#x",
1087 (ret1 & FD_CLOEXEC) ? "cloex" : "",
1088 ret1 &~ FD_CLOEXEC);
1089
1090 /* FD options */
1091 ret2 = fcntl(fd, F_GETFL, 0);
1092 if (ret2) {
1093 chunk_appendf(&trash, " getfl=%s",
1094 (ret1 & 3) >= 2 ? "O_RDWR" :
1095 (ret1 & 1) ? "O_WRONLY" : "O_RDONLY");
1096
1097 for (i = 2; i < 32; i++) {
1098 if (!(ret2 & (1UL << i)))
1099 continue;
1100 switch (1UL << i) {
1101 case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break;
1102 case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break;
1103 case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break;
1104 case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break;
1105 case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001106#ifdef O_ASYNC
Willy Tarreau5be7c192022-01-24 20:26:09 +01001107 case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break;
Willy Tarreau410942b2022-01-25 14:51:53 +01001108#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001109#ifdef O_DIRECT
1110 case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break;
1111#endif
1112#ifdef O_NOATIME
1113 case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break;
1114#endif
1115 }
1116 }
1117 }
1118
1119 vlen = sizeof(ret2);
1120 ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen);
1121 if (ret1 != -1)
1122 chunk_appendf(&trash, " so_type=%d", ret2);
1123
1124 vlen = sizeof(ret2);
1125 ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen);
1126 if (ret1 != -1)
1127 chunk_appendf(&trash, " so_accept=%d", ret2);
1128
1129 vlen = sizeof(ret2);
1130 ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen);
1131 if (ret1 != -1)
1132 chunk_appendf(&trash, " so_error=%d", ret2);
1133
1134 salen = sizeof(sa);
1135 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1136 if (sa.ss_family == AF_INET)
1137 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1138 else if (sa.ss_family == AF_INET6)
1139 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1140 else
1141 port = 0;
1142 addrstr = sa2str(&sa, port, 0);
1143 chunk_appendf(&trash, " laddr=%s", addrstr);
1144 free(addrstr);
1145 }
1146
1147 salen = sizeof(sa);
1148 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1149 if (sa.ss_family == AF_INET)
1150 port = ntohs(((const struct sockaddr_in *)&sa)->sin_port);
1151 else if (sa.ss_family == AF_INET6)
1152 port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port);
1153 else
1154 port = 0;
1155 addrstr = sa2str(&sa, port, 0);
1156 chunk_appendf(&trash, " raddr=%s", addrstr);
1157 free(addrstr);
1158 }
1159
1160 chunk_appendf(&trash, "\n");
1161
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001162 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreaue06bbf32022-05-05 14:26:28 +02001163 ctx->start_fd = fd;
Willy Tarreau5be7c192022-01-24 20:26:09 +01001164 ret = 0;
1165 break;
1166 }
1167 }
1168
1169 thread_release();
1170 end:
1171 return ret;
1172}
1173
Willy Tarreaua6026a02020-07-02 09:14:48 +02001174#if defined(DEBUG_MEM_STATS)
Willy Tarreau40e952f2022-05-05 14:39:51 +02001175
1176/* CLI state for "debug dev memstats" */
1177struct dev_mem_ctx {
1178 struct mem_stats *start, *stop; /* begin/end of dump */
1179 int show_all; /* show all entries if non-null */
1180};
1181
1182/* CLI parser for the "debug dev memstats" command. Sets a dev_mem_ctx shown above. */
Willy Tarreaua6026a02020-07-02 09:14:48 +02001183static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
1184{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001185 struct dev_mem_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1186
Willy Tarreaua6026a02020-07-02 09:14:48 +02001187 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
1188 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
1189
1190 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1191 return 1;
1192
1193 if (strcmp(args[3], "reset") == 0) {
1194 struct mem_stats *ptr;
1195
1196 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1197 return 1;
1198
1199 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
1200 _HA_ATOMIC_STORE(&ptr->calls, 0);
1201 _HA_ATOMIC_STORE(&ptr->size, 0);
1202 }
1203 return 1;
1204 }
1205
1206 if (strcmp(args[3], "all") == 0)
Willy Tarreau40e952f2022-05-05 14:39:51 +02001207 ctx->show_all = 1;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001208
1209 /* otherwise proceed with the dump from p0 to p1 */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001210 ctx->start = &__start_mem_stats;
1211 ctx->stop = &__stop_mem_stats;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001212 return 0;
1213}
1214
Willy Tarreau40e952f2022-05-05 14:39:51 +02001215/* CLI I/O handler for the "debug dev memstats" command using a dev_mem_ctx
1216 * found in appctx->svcctx. Dumps all mem_stats structs referenced by pointers
1217 * located between ->start and ->stop. Dumps all entries if ->show_all != 0,
1218 * otherwise only non-zero calls.
Willy Tarreaua6026a02020-07-02 09:14:48 +02001219 */
1220static int debug_iohandler_memstats(struct appctx *appctx)
1221{
Willy Tarreau40e952f2022-05-05 14:39:51 +02001222 struct dev_mem_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001223 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau40e952f2022-05-05 14:39:51 +02001224 struct mem_stats *ptr = ctx->start;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001225 int ret = 1;
1226
Willy Tarreau475e4632022-05-27 10:26:46 +02001227 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreaua6026a02020-07-02 09:14:48 +02001228 goto end;
1229
1230 chunk_reset(&trash);
1231
1232 /* we have two inner loops here, one for the proxy, the other one for
1233 * the buffer.
1234 */
Willy Tarreau40e952f2022-05-05 14:39:51 +02001235 for (; ptr != ctx->stop; ptr++) {
Willy Tarreaua6026a02020-07-02 09:14:48 +02001236 const char *type;
1237 const char *name;
1238 const char *p;
1239
Willy Tarreau40e952f2022-05-05 14:39:51 +02001240 if (!ptr->size && !ptr->calls && !ctx->show_all)
Willy Tarreaua6026a02020-07-02 09:14:48 +02001241 continue;
1242
1243 /* basename only */
1244 for (p = name = ptr->file; *p; p++) {
1245 if (*p == '/')
1246 name = p + 1;
1247 }
1248
1249 switch (ptr->type) {
1250 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1251 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1252 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1253 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1254 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
Willy Tarreau27061cd2022-06-23 10:54:17 +02001255 case MEM_STATS_TYPE_P_ALLOC: type = "P_ALLOC"; break;
1256 case MEM_STATS_TYPE_P_FREE: type = "P_FREE"; break;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001257 default: type = "UNSET"; break;
1258 }
1259
1260 //chunk_printf(&trash,
1261 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1262 // name, ptr->line, type,
1263 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1264 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1265
1266 chunk_printf(&trash, "%s:%d", name, ptr->line);
1267 while (trash.data < 25)
1268 trash.area[trash.data++] = ' ';
1269 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1270 type,
1271 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1272 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1273
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001274 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau40e952f2022-05-05 14:39:51 +02001275 ctx->start = ptr;
Willy Tarreaua6026a02020-07-02 09:14:48 +02001276 ret = 0;
1277 break;
1278 }
1279 }
1280
1281 end:
1282 return ret;
1283}
1284
1285#endif
1286
Willy Tarreauc7091d82019-05-17 10:08:49 +02001287#ifndef USE_THREAD_DUMP
1288
1289/* This function dumps all threads' state to the trash. This version is the
1290 * most basic one, which doesn't inspect other threads.
1291 */
1292void ha_thread_dump_all_to_trash()
1293{
1294 unsigned int thr;
1295
1296 for (thr = 0; thr < global.nbthread; thr++)
1297 ha_thread_dump(&trash, thr, tid);
1298}
1299
1300#else /* below USE_THREAD_DUMP is set */
1301
Willy Tarreauc7091d82019-05-17 10:08:49 +02001302/* ID of the thread requesting the dump */
1303static unsigned int thread_dump_tid;
1304
1305/* points to the buffer where the dump functions should write. It must
1306 * have already been initialized by the requester. Nothing is done if
1307 * it's NULL.
1308 */
1309struct buffer *thread_dump_buffer = NULL;
1310
Willy Tarreau00c27b52022-07-01 19:08:56 +02001311/* initiates a thread dump */
Willy Tarreauc7091d82019-05-17 10:08:49 +02001312void ha_thread_dump_all_to_trash()
1313{
Willy Tarreau039972b2022-07-01 19:37:42 +02001314 unsigned int old;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001315
1316 while (1) {
1317 old = 0;
Willy Tarreau00c27b52022-07-01 19:08:56 +02001318 if (HA_ATOMIC_CAS(&thread_dump_state, &old, 1))
Willy Tarreauc7091d82019-05-17 10:08:49 +02001319 break;
1320 ha_thread_relax();
1321 }
1322
1323 thread_dump_buffer = &trash;
1324 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001325 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001326}
1327
1328/* handles DEBUGSIG to dump the state of the thread it's working on */
1329void debug_handler(int sig, siginfo_t *si, void *arg)
1330{
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001331 int harmless = is_thread_harmless();
Willy Tarreau00c27b52022-07-01 19:08:56 +02001332 uint next;
1333 uint done;
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001334
Willy Tarreau82aafc42020-03-03 08:31:34 +01001335 /* first, let's check it's really for us and that we didn't just get
1336 * a spurious DEBUGSIG.
1337 */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001338 if (!_HA_ATOMIC_LOAD(&thread_dump_state))
Willy Tarreau82aafc42020-03-03 08:31:34 +01001339 return;
1340
Willy Tarreauc7091d82019-05-17 10:08:49 +02001341 /* There are 4 phases in the dump process:
1342 * 1- wait for our turn, i.e. when all lower bits are gone.
1343 * 2- perform the action if our bit is set
1344 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001345 * the last one and have to put them all as a signal
1346 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001347 */
1348
1349 /* wait for all previous threads to finish first */
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001350 if (!harmless)
1351 thread_harmless_now();
1352
Willy Tarreau00c27b52022-07-01 19:08:56 +02001353 while ((_HA_ATOMIC_LOAD(&thread_dump_state) & 0xFFFF) < (tid + 1))
Willy Tarreauc7091d82019-05-17 10:08:49 +02001354 ha_thread_relax();
1355
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001356 if (!harmless)
1357 thread_harmless_end();
1358
Willy Tarreauc7091d82019-05-17 10:08:49 +02001359 /* dump if needed */
Willy Tarreau00c27b52022-07-01 19:08:56 +02001360 if (thread_dump_buffer)
1361 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1362
1363 /* figure which is the next thread ID to dump among enabled ones */
1364 for (next = tid + 1; next < global.nbthread; next++) {
1365 if (unlikely(next >= MAX_THREADS)) {
1366 /* just to please gcc 6.5 who guesses the ranges wrong. */
1367 continue;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001368 }
Willy Tarreau00c27b52022-07-01 19:08:56 +02001369
1370 if (ha_thread_info[next].tg &&
1371 ha_thread_info[next].tg->threads_enabled & ha_thread_info[next].ltid_bit)
1372 break;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001373 }
1374
Willy Tarreau00c27b52022-07-01 19:08:56 +02001375 /* atomically set the next to next+1 or 0, and increment the done counter */
1376 done = (HA_ATOMIC_LOAD(&thread_dump_state) >> 16) + 1;
1377 done <<= 16;
1378 if (next < global.nbthread)
1379 done += next + 1;
1380 else
1381 thread_dump_buffer = NULL; // was the last one
1382
1383 HA_ATOMIC_STORE(&thread_dump_state, done);
1384
1385 /* now wait for all others to finish dumping: the lowest part will turn
1386 * to zero. Then all others decrement the done part.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001387 */
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001388 if (!harmless)
1389 thread_harmless_now();
1390
Willy Tarreau00c27b52022-07-01 19:08:56 +02001391 while ((_HA_ATOMIC_LOAD(&thread_dump_state) & 0xFFFF) != 0)
Willy Tarreauc0773622019-07-31 19:15:45 +02001392 ha_thread_relax();
Willy Tarreauf7afdd92022-07-01 17:19:14 +02001393
1394 if (!harmless)
1395 thread_harmless_end();
1396
Willy Tarreau00c27b52022-07-01 19:08:56 +02001397 HA_ATOMIC_SUB(&thread_dump_state, 0x10000);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001398
1399 /* mark the current thread as stuck to detect it upon next invocation
1400 * if it didn't move.
1401 */
Willy Tarreaua2b8ed42022-07-01 17:11:03 +02001402 if (!harmless &&
Willy Tarreaue7475c82022-06-20 09:23:24 +02001403 !(_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_SLEEPING))
Willy Tarreaubdcd3252022-06-22 09:19:46 +02001404 _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_STUCK);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001405}
1406
1407static int init_debug_per_thread()
1408{
1409 sigset_t set;
1410
1411 /* unblock the DEBUGSIG signal we intend to use */
1412 sigemptyset(&set);
1413 sigaddset(&set, DEBUGSIG);
1414 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1415 return 1;
1416}
1417
1418static int init_debug()
1419{
1420 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001421 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001422
Willy Tarreau0214b452020-03-04 06:01:40 +01001423 /* calling backtrace() will access libgcc at runtime. We don't want to
1424 * do it after the chroot, so let's perform a first call to have it
1425 * ready in memory for later use.
1426 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001427 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001428 sa.sa_handler = NULL;
1429 sa.sa_sigaction = debug_handler;
1430 sigemptyset(&sa.sa_mask);
1431 sa.sa_flags = SA_SIGINFO;
1432 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001433 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001434}
1435
1436REGISTER_POST_CHECK(init_debug);
1437REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1438
1439#endif /* USE_THREAD_DUMP */
1440
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001441/* register cli keywords */
1442static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001443 {{ "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 +01001444 {{ "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 +02001445 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1446 {{ "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 +02001447#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001448 {{ "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 +02001449#endif
Willy Tarreau5be7c192022-01-24 20:26:09 +01001450 {{ "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 +02001451 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1452 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1453 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1454 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001455#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001456 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001457#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001458 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1459 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1460 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1461 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1462 {{ "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 +01001463 {{ "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 +02001464 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6ab7b212021-12-28 09:57:10 +01001465#if defined(HA_HAVE_DUMP_LIBS)
1466 {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },
1467#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001468 {{ "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 +02001469 {{},}
1470}};
1471
1472INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);