blob: 303e28269fba12462a86b08c76ec753075d81aac [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 Tarreau368bff42019-12-06 17:18:28 +010014#include <fcntl.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020015#include <signal.h>
16#include <time.h>
17#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020018#include <stdlib.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020019#include <syslog.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010020#include <sys/types.h>
21#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020022
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020024#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020026#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/fd.h>
28#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020029#include <haproxy/hlua.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020030#include <haproxy/http_ana.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/net_helper.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020034#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020035#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020036#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020038
Willy Tarreau4e2b6462019-05-16 17:44:30 +020039
Willy Tarreaua37cb182019-07-31 19:20:39 +020040/* mask of threads still having to dump, used to respect ordering. Only used
41 * when USE_THREAD_DUMP is set.
42 */
43volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020044unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020045
Willy Tarreau123fc972021-01-22 13:52:41 +010046/* dumps a backtrace of the current thread that is appended to buffer <buf>.
47 * Lines are prefixed with the string <prefix> which may be empty (used for
48 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010049 * the function does not appear in the call stack. The <dump> argument
50 * indicates what dump state to start from, and should usually be zero. It
51 * may be among the following values:
52 * - 0: search usual callers before step 1, or directly jump to 2
53 * - 1: skip usual callers before step 2
54 * - 2: dump until polling loop, scheduler, or main() (excluded)
55 * - 3: end
56 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010057 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010058void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010059{
60 struct buffer bak;
61 char pfx2[100];
62 void *callers[100];
63 int j, nptrs;
64 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010065
66 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
67 if (!nptrs)
68 return;
69
70 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
71 pfx2[0] = 0;
72
73 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
74 * produce similar output to the following:
75 */
76 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010077 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
78 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010079 /* we failed to spot the starting point of the
80 * dump, let's start over dumping everything we
81 * have.
82 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010083 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010084 j = 0;
85 }
86 bak = *buf;
87 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
88 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010089 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010090 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010091 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +010092 */
Willy Tarreaua8459b22021-01-22 14:12:27 +010093 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
94 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010095 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +010096 *buf = bak;
97 continue;
98 }
99
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100100 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100101 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100102 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
103 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100104 *buf = bak;
105 continue;
106 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100107 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100108 }
109
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100110 if ((dump & 3) == 2) {
111 /* still dumping */
112 if (dump == 6) {
113 /* we only stop *after* main and we must send the LF */
114 if (addr == main) {
115 j = nptrs;
116 dump++;
117 }
118 }
119 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
120 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100121 *buf = bak;
122 break;
123 }
124 }
125 /* OK, line dumped */
126 chunk_appendf(buf, "\n");
127 }
128}
129
Willy Tarreaua8459b22021-01-22 14:12:27 +0100130/* dump a backtrace of current thread's stack to stderr. */
131void ha_backtrace_to_stderr()
132{
133 char area[2048];
134 struct buffer b = b_make(area, sizeof(area), 0, 0);
135
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100136 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100137 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100138 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100139}
140
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200141/* Dumps to the buffer some known information for the desired thread, and
142 * optionally extra info for the current thread. The dump will be appended to
143 * the buffer, so the caller is responsible for preliminary initializing it.
144 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200145 * in front of the calling thread's line (usually it's tid). Any stuck thread
146 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200147 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200148void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200149{
150 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +0100151 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
152 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
153 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200154
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200155 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200156 "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200157 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200158 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200159 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200160 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200161 !!(global_tasks_mask & thr_bit),
162 !eb_is_empty(&task_per_thread[thr].timers),
163 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +0100164 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
165 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
166 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
167 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100168 task_per_thread[thr].tasks_in_list,
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100169 task_per_thread[thr].rq_total,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200170 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200171 !!(task_profiling_mask & thr_bit));
172
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200173 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200174 " harmless=%d wantrdv=%d",
175 !!(threads_harmless_mask & thr_bit),
176 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200177
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200178 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200179 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200180
181 /* this is the end of what we can dump from outside the thread */
182
183 if (thr != tid)
184 return;
185
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200186 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200187 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100188
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100189 if (stuck) {
190 /* We only emit the backtrace for stuck threads in order not to
191 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100192 * Please leave this as the last instruction in this function
193 * so that the compiler uses tail merging and the current
194 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100195 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100196 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100197 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200198}
199
200
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200201/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200202 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200203 * with <pfx>. The buffer is only appended and the first output starts by the
204 * pointer itself. The caller is responsible for making sure the task is not
205 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200206 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200207void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200208{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200209 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200210 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200211 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200212
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200213 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200214 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200215 return;
216 }
217
Willy Tarreau20db9112019-05-17 14:14:35 +0200218 if (TASK_IS_TASKLET(task))
219 chunk_appendf(buf,
220 "%p (tasklet) calls=%u\n",
221 task,
222 task->calls);
223 else
224 chunk_appendf(buf,
225 "%p (task) calls=%u last=%llu%s\n",
226 task,
227 task->calls,
228 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
229 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200230
Willy Tarreau2e89b092020-03-03 17:13:02 +0100231 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
232 resolve_sym_name(buf, NULL, task->process);
233 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200234
Willy Tarreaua512b022019-08-21 14:12:19 +0200235 if (task->process == task_run_applet && (appctx = task->context))
236 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
237 else
238 chunk_appendf(buf, "\n");
239
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200240 if (task->process == process_stream && task->context)
241 s = (struct stream *)task->context;
242 else if (task->process == task_run_applet && task->context)
243 s = si_strm(((struct appctx *)task->context)->owner);
244 else if (task->process == si_cs_io_cb && task->context)
245 s = si_strm((struct stream_interface *)task->context);
246
247 if (s)
248 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200249
250#ifdef USE_LUA
251 hlua = NULL;
252 if (s && (hlua = s->hlua)) {
253 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
254 }
255 else if (task->process == hlua_process_task && (hlua = task->context)) {
256 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
257 }
258 else if (task->process == task_run_applet && (appctx = task->context) &&
259 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
260 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
261 }
262 else if (task->process == task_run_applet && (appctx = task->context) &&
263 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
264 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
265 }
266
Christopher Faulet471425f2020-07-24 19:08:05 +0200267 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100268 chunk_appendf(buf, "stack traceback:\n ");
269 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
270 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200271 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200272 else
273 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200274#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200275}
276
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200277/* This function dumps all profiling settings. It returns 0 if the output
278 * buffer is full and it needs to be called again, otherwise non-zero.
279 */
280static int cli_io_handler_show_threads(struct appctx *appctx)
281{
282 struct stream_interface *si = appctx->owner;
283 int thr;
284
285 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
286 return 1;
287
288 if (appctx->st0)
289 thr = appctx->st1;
290 else
291 thr = 0;
292
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200293 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200294 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200295
296 if (ci_putchk(si_ic(si), &trash) == -1) {
297 /* failed, try again */
298 si_rx_room_blk(si);
299 appctx->st1 = thr;
300 return 0;
301 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200302 return 1;
303}
304
Willy Tarreau56131ca2019-05-20 13:48:29 +0200305/* dumps a state of all threads into the trash and on fd #2, then aborts. */
306void ha_panic()
307{
308 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200309 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200310 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100311 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200312 for (;;)
313 abort();
314}
315
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200316/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
317static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
318{
319 int code = atoi(args[3]);
320
321 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
322 return 1;
323
Willy Tarreau4781b152021-04-06 13:53:36 +0200324 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200325 exit(code);
326 return 1;
327}
328
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100329/* parse a "debug dev bug" command. It always returns 1, though it should never return.
330 * Note: we make sure not to make the function static so that it appears in the trace.
331 */
332int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
333{
334 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
335 return 1;
336
Willy Tarreau4781b152021-04-06 13:53:36 +0200337 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100338 BUG_ON(one > zero);
339 return 1;
340}
341
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200342/* parse a "debug dev close" command. It always returns 1. */
343static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
344{
345 int fd;
346
347 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
348 return 1;
349
Willy Tarreau9d008692019-08-09 11:21:01 +0200350 if (!*args[3])
351 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200352
353 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200354 if (fd < 0 || fd >= global.maxsock)
355 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200356
Willy Tarreau9d008692019-08-09 11:21:01 +0200357 if (!fdtab[fd].owner)
358 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200359
Willy Tarreau4781b152021-04-06 13:53:36 +0200360 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200361 fd_delete(fd);
362 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363}
364
365/* parse a "debug dev delay" command. It always returns 1. */
366static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
367{
368 int delay = atoi(args[3]);
369
370 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
371 return 1;
372
Willy Tarreau4781b152021-04-06 13:53:36 +0200373 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200374 usleep((long)delay * 1000);
375 return 1;
376}
377
378/* parse a "debug dev log" command. It always returns 1. */
379static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
380{
381 int arg;
382
383 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
384 return 1;
385
Willy Tarreau4781b152021-04-06 13:53:36 +0200386 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200387 chunk_reset(&trash);
388 for (arg = 3; *args[arg]; arg++) {
389 if (arg > 3)
390 chunk_strcat(&trash, " ");
391 chunk_strcat(&trash, args[arg]);
392 }
393
394 send_log(NULL, LOG_INFO, "%s\n", trash.area);
395 return 1;
396}
397
398/* parse a "debug dev loop" command. It always returns 1. */
399static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
400{
401 struct timeval deadline, curr;
402 int loop = atoi(args[3]);
403
404 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
405 return 1;
406
Willy Tarreau4781b152021-04-06 13:53:36 +0200407 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200408 gettimeofday(&curr, NULL);
409 tv_ms_add(&deadline, &curr, loop);
410
411 while (tv_ms_cmp(&curr, &deadline) < 0)
412 gettimeofday(&curr, NULL);
413
414 return 1;
415}
416
417/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
418static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
419{
420 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
421 return 1;
422
Willy Tarreau4781b152021-04-06 13:53:36 +0200423 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424 ha_panic();
425 return 1;
426}
427
428/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200429#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200430static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
431{
Willy Tarreau368bff42019-12-06 17:18:28 +0100432 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200433 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100434 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200435
436 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
437 return 1;
438
Willy Tarreau4781b152021-04-06 13:53:36 +0200439 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200440 chunk_reset(&trash);
441 for (arg = 3; *args[arg]; arg++) {
442 if (arg > 3)
443 chunk_strcat(&trash, " ");
444 chunk_strcat(&trash, args[arg]);
445 }
446
Willy Tarreau368bff42019-12-06 17:18:28 +0100447 thread_isolate();
448 if (pipe(pipefd) < 0)
449 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200450
Willy Tarreau368bff42019-12-06 17:18:28 +0100451 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
452 goto fail_fcntl;
453
454 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
455 goto fail_fcntl;
456
457 pid = fork();
458
459 if (pid < 0)
460 goto fail_fork;
461 else if (pid == 0) {
462 /* child */
463 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
464
465 close(0);
466 dup2(pipefd[1], 1);
467 dup2(pipefd[1], 2);
468
469 cmd[2] = trash.area;
470 execvp(cmd[0], cmd);
471 printf("execvp() failed\n");
472 exit(1);
473 }
474
475 /* parent */
476 thread_release();
477 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200478 chunk_reset(&trash);
479 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100480 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
481 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200482 break;
483 trash.data += ret;
484 if (trash.data + 20 == trash.size) {
485 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
486 break;
487 }
488 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100489 close(pipefd[0]);
490 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200491 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200492 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100493
494 fail_fork:
495 fail_fcntl:
496 close(pipefd[0]);
497 close(pipefd[1]);
498 fail_pipe:
499 thread_release();
500 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200501}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200502#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200503
504/* parse a "debug dev hex" command. It always returns 1. */
505static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
506{
507 unsigned long start, len;
508
509 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
510 return 1;
511
Willy Tarreau9d008692019-08-09 11:21:01 +0200512 if (!*args[3])
513 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200514
515 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200516 if (!start)
517 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200518
Willy Tarreau4781b152021-04-06 13:53:36 +0200519 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200520
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200521 /* by default, dump ~128 till next block of 16 */
522 len = strtoul(args[4], NULL, 0);
523 if (!len)
524 len = ((start + 128) & -16) - start;
525
526 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200527 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200528 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200529 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200530}
531
Willy Tarreau48129be2021-05-04 18:40:50 +0200532/* parse a "debug dev sym <addr>" command. It always returns 1. */
533static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
534{
535 unsigned long addr;
536
537 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
538 return 1;
539
540 if (!*args[3])
541 return cli_err(appctx, "Missing memory address to be resolved.\n");
542
543 _HA_ATOMIC_INC(&debug_commands_issued);
544
545 addr = strtoul(args[3], NULL, 0);
546 chunk_printf(&trash, "%#lx resolves to ", addr);
547 resolve_sym_name(&trash, NULL, (const void *)addr);
548 chunk_appendf(&trash, "\n");
549
550 return cli_msg(appctx, LOG_INFO, trash.area);
551}
552
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200553/* parse a "debug dev tkill" command. It always returns 1. */
554static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
555{
556 int thr = 0;
557 int sig = SIGABRT;
558
559 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
560 return 1;
561
562 if (*args[3])
563 thr = atoi(args[3]);
564
Willy Tarreau9d008692019-08-09 11:21:01 +0200565 if (thr < 0 || thr > global.nbthread)
566 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200567
568 if (*args[4])
569 sig = atoi(args[4]);
570
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200572 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200573 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200574 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200575 raise(sig);
576 return 1;
577}
578
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100579/* parse a "debug dev write" command. It always returns 1. */
580static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
581{
582 unsigned long len;
583
584 if (!*args[3])
585 return cli_err(appctx, "Missing output size.\n");
586
587 len = strtoul(args[3], NULL, 0);
588 if (len >= trash.size)
589 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
590
Willy Tarreau4781b152021-04-06 13:53:36 +0200591 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100592
593 chunk_reset(&trash);
594 trash.data = len;
595 memset(trash.area, '.', trash.data);
596 trash.area[trash.data] = 0;
597 for (len = 64; len < trash.data; len += 64)
598 trash.area[len] = '\n';
599 return cli_msg(appctx, LOG_INFO, trash.area);
600}
601
Willy Tarreau68680bb2019-10-23 17:23:25 +0200602/* parse a "debug dev stream" command */
603/*
604 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
605 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
606 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
607 * [sif.s[=<state>]] [sib.s[=<state>]]
608 */
609static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
610{
611 struct stream *s = si_strm(appctx->owner);
612 int arg;
613 void *ptr;
614 int size;
615 const char *word, *end;
616 struct ist name;
617 char *msg = NULL;
618 char *endarg;
619 unsigned long long old, new;
620
621 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
622 return 1;
623
624 ptr = NULL; size = 0;
625
626 if (!*args[3]) {
627 return cli_err(appctx,
628 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
629 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
630 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
631 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
632 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
633 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
634 );
635 }
636
Willy Tarreau4781b152021-04-06 13:53:36 +0200637 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200638 for (arg = 3; *args[arg]; arg++) {
639 old = 0;
640 end = word = args[arg];
641 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
642 end++;
643 name = ist2(word, end - word);
644 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200645 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200646 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200647 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200648 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200649 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200650 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200651 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200652 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200653 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200654 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200655 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200656 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200657 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200658 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200659 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200660 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200661 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200662 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200663 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200664 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200665 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200666 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200667 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200668 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200669 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200670 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200671 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200672 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200673 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200674 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200675 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200676 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
677 continue;
678 } else
679 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
680
681 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200682 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200683 if (size == 8)
684 old = read_u64(ptr);
685 else if (size == 4)
686 old = read_u32(ptr);
687 else if (size == 2)
688 old = read_u16(ptr);
689 else
690 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200691 } else {
692 memprintf(&msg,
693 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
694 msg ? msg : "", ptr, (int)(end - word), word);
695 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200696 }
697
698 /* parse the new value . */
699 new = strtoll(end + 1, &endarg, 0);
700 if (end[1] && *endarg) {
701 if (strcmp(end + 1, "now") == 0)
702 new = now_ms;
703 else {
704 memprintf(&msg,
705 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
706 msg ? msg : "", end + 1, (int)(end - word), word);
707 continue;
708 }
709 }
710
711 switch (*end) {
712 case '\0': /* show */
713 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
714 new = old; // do not change the value
715 break;
716
717 case '=': /* set */
718 break;
719
720 case '^': /* XOR */
721 new = old ^ new;
722 break;
723
724 case '+': /* OR */
725 new = old | new;
726 break;
727
728 case '-': /* AND NOT */
729 new = old & ~new;
730 break;
731
732 default:
733 break;
734 }
735
736 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200737 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200738 if (size == 8)
739 write_u64(ptr, new);
740 else if (size == 4)
741 write_u32(ptr, new);
742 else if (size == 2)
743 write_u16(ptr, new);
744 else
745 *(uint8_t *)ptr = new;
746 }
747 }
748
749 if (msg && *msg)
750 return cli_dynmsg(appctx, LOG_INFO, msg);
751 return 1;
752}
753
Willy Tarreau144f84a2021-03-02 16:09:26 +0100754static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100755{
756 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
757 unsigned long inter = tctx[1];
758 unsigned long rnd;
759
760 t->expire = tick_add(now_ms, inter);
761
762 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100763 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100764 if (rnd & 1) {
765 rnd >>= 1;
766 rnd %= tctx[0];
767 rnd = tctx[rnd + 2];
768
769 if (rnd & 1)
770 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
771 else
772 tasklet_wakeup((struct tasklet *)rnd);
773 }
774 return t;
775}
776
Willy Tarreau144f84a2021-03-02 16:09:26 +0100777static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100778{
779 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
780 unsigned long rnd;
781 int i;
782
783 /* wake up two random entries */
784 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100785 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100786 rnd = tctx[rnd + 2];
787
788 if (rnd & 1)
789 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
790 else
791 tasklet_wakeup((struct tasklet *)rnd);
792 }
793 return t;
794}
795
796/* parse a "debug dev sched" command
797 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
798 */
799static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
800{
801 int arg;
802 void *ptr;
803 int size;
804 const char *word, *end;
805 struct ist name;
806 char *msg = NULL;
807 char *endarg;
808 unsigned long long new;
809 unsigned long count = 0;
810 unsigned long thrid = 0;
811 unsigned int inter = 0;
812 unsigned long mask, tmask;
813 unsigned long i;
814 int mode = 0; // 0 = tasklet; 1 = task
815 int single = 0;
816 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
817
818 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
819 return 1;
820
821 ptr = NULL; size = 0;
822 mask = all_threads_mask;
823
824 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
825 return cli_err(appctx,
826 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
827 " <obj> = {count | mask | inter | single }\n"
828 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
829 );
830 }
831
832 mode = strcmp(args[3], "task") == 0;
833
Willy Tarreau4781b152021-04-06 13:53:36 +0200834 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100835 for (arg = 4; *args[arg]; arg++) {
836 end = word = args[arg];
837 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
838 end++;
839 name = ist2(word, end - word);
840 if (isteq(name, ist("count"))) {
841 ptr = &count; size = sizeof(count);
842 } else if (isteq(name, ist("mask"))) {
843 ptr = &mask; size = sizeof(mask);
844 } else if (isteq(name, ist("tid"))) {
845 ptr = &thrid; size = sizeof(thrid);
846 } else if (isteq(name, ist("inter"))) {
847 ptr = &inter; size = sizeof(inter);
848 } else if (isteq(name, ist("single"))) {
849 ptr = &single; size = sizeof(single);
850 } else
851 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
852
853 /* parse the new value . */
854 new = strtoll(end + 1, &endarg, 0);
855 if (end[1] && *endarg) {
856 memprintf(&msg,
857 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
858 msg ? msg : "", end + 1, (int)(end - word), word);
859 continue;
860 }
861
862 /* write the new value */
863 if (size == 8)
864 write_u64(ptr, new);
865 else if (size == 4)
866 write_u32(ptr, new);
867 else if (size == 2)
868 write_u16(ptr, new);
869 else
870 *(uint8_t *)ptr = new;
871 }
872
873 tctx = calloc(sizeof(*tctx), count + 2);
874 if (!tctx)
875 goto fail;
876
877 tctx[0] = (unsigned long)count;
878 tctx[1] = (unsigned long)inter;
879
880 mask &= all_threads_mask;
881 if (!mask)
882 mask = tid_bit;
883
884 tmask = 0;
885 for (i = 0; i < count; i++) {
886 if (single || mode == 0) {
887 /* look for next bit matching a bit in mask or loop back to zero */
888 for (tmask <<= 1; !(mask & tmask); ) {
889 if (!(mask & -tmask))
890 tmask = 1;
891 else
892 tmask <<= 1;
893 }
894 } else {
895 /* multi-threaded task */
896 tmask = mask;
897 }
898
899 /* now, if poly or mask was set, tmask corresponds to the
900 * valid thread mask to use, otherwise it remains zero.
901 */
902 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
903 if (mode == 0) {
904 struct tasklet *tl = tasklet_new();
905
906 if (!tl)
907 goto fail;
908
909 if (tmask)
910 tl->tid = my_ffsl(tmask) - 1;
911 tl->process = debug_tasklet_handler;
912 tl->context = tctx;
913 tctx[i + 2] = (unsigned long)tl;
914 } else {
915 struct task *task = task_new(tmask ? tmask : tid_bit);
916
917 if (!task)
918 goto fail;
919
920 task->process = debug_task_handler;
921 task->context = tctx;
922 tctx[i + 2] = (unsigned long)task + 1;
923 }
924 }
925
926 /* start the tasks and tasklets */
927 for (i = 0; i < count; i++) {
928 unsigned long ctx = tctx[i + 2];
929
930 if (ctx & 1)
931 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
932 else
933 tasklet_wakeup((struct tasklet *)ctx);
934 }
935
936 if (msg && *msg)
937 return cli_dynmsg(appctx, LOG_INFO, msg);
938 return 1;
939
940 fail:
941 /* free partially allocated entries */
942 for (i = 0; tctx && i < count; i++) {
943 unsigned long ctx = tctx[i + 2];
944
945 if (!ctx)
946 break;
947
948 if (ctx & 1)
949 task_destroy((struct task *)(ctx - 1));
950 else
951 tasklet_free((struct tasklet *)ctx);
952 }
953
954 free(tctx);
955 return cli_err(appctx, "Not enough memory");
956}
957
Willy Tarreaua6026a02020-07-02 09:14:48 +0200958#if defined(DEBUG_MEM_STATS)
959/* CLI parser for the "debug dev memstats" command */
960static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
961{
962 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
963 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
964
965 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
966 return 1;
967
968 if (strcmp(args[3], "reset") == 0) {
969 struct mem_stats *ptr;
970
971 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
972 return 1;
973
974 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
975 _HA_ATOMIC_STORE(&ptr->calls, 0);
976 _HA_ATOMIC_STORE(&ptr->size, 0);
977 }
978 return 1;
979 }
980
981 if (strcmp(args[3], "all") == 0)
982 appctx->ctx.cli.i0 = 1;
983
984 /* otherwise proceed with the dump from p0 to p1 */
985 appctx->ctx.cli.p0 = &__start_mem_stats;
986 appctx->ctx.cli.p1 = &__stop_mem_stats;
987 return 0;
988}
989
990/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
991 * structs referenced by pointers located between p0 and p1. Dumps all entries
992 * if i0 > 0, otherwise only non-zero calls.
993 */
994static int debug_iohandler_memstats(struct appctx *appctx)
995{
996 struct stream_interface *si = appctx->owner;
997 struct mem_stats *ptr = appctx->ctx.cli.p0;
998 int ret = 1;
999
1000 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1001 goto end;
1002
1003 chunk_reset(&trash);
1004
1005 /* we have two inner loops here, one for the proxy, the other one for
1006 * the buffer.
1007 */
1008 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1009 const char *type;
1010 const char *name;
1011 const char *p;
1012
1013 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1014 continue;
1015
1016 /* basename only */
1017 for (p = name = ptr->file; *p; p++) {
1018 if (*p == '/')
1019 name = p + 1;
1020 }
1021
1022 switch (ptr->type) {
1023 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1024 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1025 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1026 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1027 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1028 default: type = "UNSET"; break;
1029 }
1030
1031 //chunk_printf(&trash,
1032 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1033 // name, ptr->line, type,
1034 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1035 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1036
1037 chunk_printf(&trash, "%s:%d", name, ptr->line);
1038 while (trash.data < 25)
1039 trash.area[trash.data++] = ' ';
1040 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1041 type,
1042 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1043 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1044
1045 if (ci_putchk(si_ic(si), &trash) == -1) {
1046 si_rx_room_blk(si);
1047 appctx->ctx.cli.p0 = ptr;
1048 ret = 0;
1049 break;
1050 }
1051 }
1052
1053 end:
1054 return ret;
1055}
1056
1057#endif
1058
Willy Tarreauc7091d82019-05-17 10:08:49 +02001059#ifndef USE_THREAD_DUMP
1060
1061/* This function dumps all threads' state to the trash. This version is the
1062 * most basic one, which doesn't inspect other threads.
1063 */
1064void ha_thread_dump_all_to_trash()
1065{
1066 unsigned int thr;
1067
1068 for (thr = 0; thr < global.nbthread; thr++)
1069 ha_thread_dump(&trash, thr, tid);
1070}
1071
1072#else /* below USE_THREAD_DUMP is set */
1073
Willy Tarreauc7091d82019-05-17 10:08:49 +02001074/* ID of the thread requesting the dump */
1075static unsigned int thread_dump_tid;
1076
1077/* points to the buffer where the dump functions should write. It must
1078 * have already been initialized by the requester. Nothing is done if
1079 * it's NULL.
1080 */
1081struct buffer *thread_dump_buffer = NULL;
1082
1083void ha_thread_dump_all_to_trash()
1084{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001085 unsigned long old;
1086
1087 while (1) {
1088 old = 0;
1089 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1090 break;
1091 ha_thread_relax();
1092 }
1093
1094 thread_dump_buffer = &trash;
1095 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001096 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001097}
1098
1099/* handles DEBUGSIG to dump the state of the thread it's working on */
1100void debug_handler(int sig, siginfo_t *si, void *arg)
1101{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001102 /* first, let's check it's really for us and that we didn't just get
1103 * a spurious DEBUGSIG.
1104 */
1105 if (!(threads_to_dump & tid_bit))
1106 return;
1107
Willy Tarreauc7091d82019-05-17 10:08:49 +02001108 /* There are 4 phases in the dump process:
1109 * 1- wait for our turn, i.e. when all lower bits are gone.
1110 * 2- perform the action if our bit is set
1111 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001112 * the last one and have to put them all as a signal
1113 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001114 */
1115
1116 /* wait for all previous threads to finish first */
1117 while (threads_to_dump & (tid_bit - 1))
1118 ha_thread_relax();
1119
1120 /* dump if needed */
1121 if (threads_to_dump & tid_bit) {
1122 if (thread_dump_buffer)
1123 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1124 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1125 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001126 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001127 thread_dump_buffer = NULL;
1128 }
1129 else
1130 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1131 }
1132
1133 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001134 * bits again to broadcast the leaving condition so we'll see ourselves
1135 * present again. This way the threads_to_dump variable never passes to
1136 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001137 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001138 while (!(threads_to_dump & tid_bit))
1139 ha_thread_relax();
1140 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001141
1142 /* mark the current thread as stuck to detect it upon next invocation
1143 * if it didn't move.
1144 */
1145 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1146 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001147}
1148
1149static int init_debug_per_thread()
1150{
1151 sigset_t set;
1152
1153 /* unblock the DEBUGSIG signal we intend to use */
1154 sigemptyset(&set);
1155 sigaddset(&set, DEBUGSIG);
1156 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1157 return 1;
1158}
1159
1160static int init_debug()
1161{
1162 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001163 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001164
Willy Tarreau0214b452020-03-04 06:01:40 +01001165 /* calling backtrace() will access libgcc at runtime. We don't want to
1166 * do it after the chroot, so let's perform a first call to have it
1167 * ready in memory for later use.
1168 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001169 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001170 sa.sa_handler = NULL;
1171 sa.sa_sigaction = debug_handler;
1172 sigemptyset(&sa.sa_mask);
1173 sa.sa_flags = SA_SIGINFO;
1174 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001175 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001176}
1177
1178REGISTER_POST_CHECK(init_debug);
1179REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1180
1181#endif /* USE_THREAD_DUMP */
1182
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001183/* register cli keywords */
1184static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001185 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1186 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1187 {{ "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 +02001188#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001189 {{ "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 +02001190#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001191 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1192 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1193 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1194 {{ "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 +02001195#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001196 {{ "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 +02001197#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001198 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1199 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1200 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1201 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1202 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
1203 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
1204 {{ "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 +02001205 {{},}
1206}};
1207
1208INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);