blob: 0720e1f92c617d7e64f9ef99b5dd5e8754cc622a [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 Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/net_helper.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020032#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020033#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020034#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020035#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020037
Willy Tarreau4e2b6462019-05-16 17:44:30 +020038
Willy Tarreaua37cb182019-07-31 19:20:39 +020039/* mask of threads still having to dump, used to respect ordering. Only used
40 * when USE_THREAD_DUMP is set.
41 */
42volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020043unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020044
Willy Tarreau123fc972021-01-22 13:52:41 +010045/* dumps a backtrace of the current thread that is appended to buffer <buf>.
46 * Lines are prefixed with the string <prefix> which may be empty (used for
47 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010048 * the function does not appear in the call stack. The <dump> argument
49 * indicates what dump state to start from, and should usually be zero. It
50 * may be among the following values:
51 * - 0: search usual callers before step 1, or directly jump to 2
52 * - 1: skip usual callers before step 2
53 * - 2: dump until polling loop, scheduler, or main() (excluded)
54 * - 3: end
55 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010056 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010057void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010058{
59 struct buffer bak;
60 char pfx2[100];
61 void *callers[100];
62 int j, nptrs;
63 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010064
65 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
66 if (!nptrs)
67 return;
68
69 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
70 pfx2[0] = 0;
71
72 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
73 * produce similar output to the following:
74 */
75 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010076 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
77 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010078 /* we failed to spot the starting point of the
79 * dump, let's start over dumping everything we
80 * have.
81 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010082 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010083 j = 0;
84 }
85 bak = *buf;
86 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
87 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010088 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010089 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010090 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +010091 */
Willy Tarreaua8459b22021-01-22 14:12:27 +010092 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
93 addr == ha_backtrace_to_stderr)
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010094 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +010095 *buf = bak;
96 continue;
97 }
98
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010099 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100100 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100101 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
102 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100103 *buf = bak;
104 continue;
105 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100106 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100107 }
108
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100109 if ((dump & 3) == 2) {
110 /* still dumping */
111 if (dump == 6) {
112 /* we only stop *after* main and we must send the LF */
113 if (addr == main) {
114 j = nptrs;
115 dump++;
116 }
117 }
118 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
119 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100120 *buf = bak;
121 break;
122 }
123 }
124 /* OK, line dumped */
125 chunk_appendf(buf, "\n");
126 }
127}
128
Willy Tarreaua8459b22021-01-22 14:12:27 +0100129/* dump a backtrace of current thread's stack to stderr. */
130void ha_backtrace_to_stderr()
131{
132 char area[2048];
133 struct buffer b = b_make(area, sizeof(area), 0, 0);
134
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100135 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100136 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100137 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100138}
139
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200140/* Dumps to the buffer some known information for the desired thread, and
141 * optionally extra info for the current thread. The dump will be appended to
142 * the buffer, so the caller is responsible for preliminary initializing it.
143 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200144 * in front of the calling thread's line (usually it's tid). Any stuck thread
145 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200146 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200147void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200148{
149 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +0100150 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
151 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
152 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200153
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200154 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200155 "%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 +0200156 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200157 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200158 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200159 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200160 !!(global_tasks_mask & thr_bit),
161 !eb_is_empty(&task_per_thread[thr].timers),
162 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +0100163 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
164 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
165 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
166 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100167 task_per_thread[thr].tasks_in_list,
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100168 task_per_thread[thr].rq_total,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200169 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170 !!(task_profiling_mask & thr_bit));
171
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200172 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200173 " harmless=%d wantrdv=%d",
174 !!(threads_harmless_mask & thr_bit),
175 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200177 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200178 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200179
180 /* this is the end of what we can dump from outside the thread */
181
182 if (thr != tid)
183 return;
184
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200185 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200186 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100187
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100188 if (stuck) {
189 /* We only emit the backtrace for stuck threads in order not to
190 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100191 * Please leave this as the last instruction in this function
192 * so that the compiler uses tail merging and the current
193 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100194 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100195 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100196 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200197}
198
199
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200200/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200201 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200202 * with <pfx>. The buffer is only appended and the first output starts by the
203 * pointer itself. The caller is responsible for making sure the task is not
204 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200205 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200206void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200207{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200208 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200209 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200210 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200211
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200212 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200213 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200214 return;
215 }
216
Willy Tarreau20db9112019-05-17 14:14:35 +0200217 if (TASK_IS_TASKLET(task))
218 chunk_appendf(buf,
219 "%p (tasklet) calls=%u\n",
220 task,
221 task->calls);
222 else
223 chunk_appendf(buf,
224 "%p (task) calls=%u last=%llu%s\n",
225 task,
226 task->calls,
227 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
228 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200229
Willy Tarreau2e89b092020-03-03 17:13:02 +0100230 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
231 resolve_sym_name(buf, NULL, task->process);
232 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200233
Willy Tarreaua512b022019-08-21 14:12:19 +0200234 if (task->process == task_run_applet && (appctx = task->context))
235 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
236 else
237 chunk_appendf(buf, "\n");
238
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200239 if (task->process == process_stream && task->context)
240 s = (struct stream *)task->context;
241 else if (task->process == task_run_applet && task->context)
242 s = si_strm(((struct appctx *)task->context)->owner);
243 else if (task->process == si_cs_io_cb && task->context)
244 s = si_strm((struct stream_interface *)task->context);
245
246 if (s)
247 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200248
249#ifdef USE_LUA
250 hlua = NULL;
251 if (s && (hlua = s->hlua)) {
252 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
253 }
254 else if (task->process == hlua_process_task && (hlua = task->context)) {
255 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
256 }
257 else if (task->process == task_run_applet && (appctx = task->context) &&
258 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
259 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
260 }
261 else if (task->process == task_run_applet && (appctx = task->context) &&
262 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
263 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
264 }
265
Christopher Faulet471425f2020-07-24 19:08:05 +0200266 if (hlua && hlua->T) {
Christopher Fauletcc2c4f82021-03-24 14:52:24 +0100267 chunk_appendf(buf, "stack traceback:\n ");
268 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
269 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200270 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200271 else
272 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200273#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200274}
275
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200276/* This function dumps all profiling settings. It returns 0 if the output
277 * buffer is full and it needs to be called again, otherwise non-zero.
278 */
279static int cli_io_handler_show_threads(struct appctx *appctx)
280{
281 struct stream_interface *si = appctx->owner;
282 int thr;
283
284 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
285 return 1;
286
287 if (appctx->st0)
288 thr = appctx->st1;
289 else
290 thr = 0;
291
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200292 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200293 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200294
295 if (ci_putchk(si_ic(si), &trash) == -1) {
296 /* failed, try again */
297 si_rx_room_blk(si);
298 appctx->st1 = thr;
299 return 0;
300 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200301 return 1;
302}
303
Willy Tarreau56131ca2019-05-20 13:48:29 +0200304/* dumps a state of all threads into the trash and on fd #2, then aborts. */
305void ha_panic()
306{
307 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200308 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200309 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100310 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200311 for (;;)
312 abort();
313}
314
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200315/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
316static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
317{
318 int code = atoi(args[3]);
319
320 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
321 return 1;
322
Willy Tarreau4781b152021-04-06 13:53:36 +0200323 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200324 exit(code);
325 return 1;
326}
327
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100328/* parse a "debug dev bug" command. It always returns 1, though it should never return.
329 * Note: we make sure not to make the function static so that it appears in the trace.
330 */
331int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
332{
333 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
334 return 1;
335
Willy Tarreau4781b152021-04-06 13:53:36 +0200336 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100337 BUG_ON(one > zero);
338 return 1;
339}
340
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200341/* parse a "debug dev close" command. It always returns 1. */
342static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
343{
344 int fd;
345
346 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
347 return 1;
348
Willy Tarreau9d008692019-08-09 11:21:01 +0200349 if (!*args[3])
350 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200351
352 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200353 if (fd < 0 || fd >= global.maxsock)
354 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200355
Willy Tarreau9d008692019-08-09 11:21:01 +0200356 if (!fdtab[fd].owner)
357 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200358
Willy Tarreau4781b152021-04-06 13:53:36 +0200359 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200360 fd_delete(fd);
361 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200362}
363
364/* parse a "debug dev delay" command. It always returns 1. */
365static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
366{
367 int delay = atoi(args[3]);
368
369 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
370 return 1;
371
Willy Tarreau4781b152021-04-06 13:53:36 +0200372 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200373 usleep((long)delay * 1000);
374 return 1;
375}
376
377/* parse a "debug dev log" command. It always returns 1. */
378static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
379{
380 int arg;
381
382 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
383 return 1;
384
Willy Tarreau4781b152021-04-06 13:53:36 +0200385 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200386 chunk_reset(&trash);
387 for (arg = 3; *args[arg]; arg++) {
388 if (arg > 3)
389 chunk_strcat(&trash, " ");
390 chunk_strcat(&trash, args[arg]);
391 }
392
393 send_log(NULL, LOG_INFO, "%s\n", trash.area);
394 return 1;
395}
396
397/* parse a "debug dev loop" command. It always returns 1. */
398static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
399{
400 struct timeval deadline, curr;
401 int loop = atoi(args[3]);
402
403 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
404 return 1;
405
Willy Tarreau4781b152021-04-06 13:53:36 +0200406 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200407 gettimeofday(&curr, NULL);
408 tv_ms_add(&deadline, &curr, loop);
409
410 while (tv_ms_cmp(&curr, &deadline) < 0)
411 gettimeofday(&curr, NULL);
412
413 return 1;
414}
415
416/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
417static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
418{
419 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
420 return 1;
421
Willy Tarreau4781b152021-04-06 13:53:36 +0200422 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200423 ha_panic();
424 return 1;
425}
426
427/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200428#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200429static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
430{
Willy Tarreau368bff42019-12-06 17:18:28 +0100431 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200432 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100433 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200434
435 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
436 return 1;
437
Willy Tarreau4781b152021-04-06 13:53:36 +0200438 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200439 chunk_reset(&trash);
440 for (arg = 3; *args[arg]; arg++) {
441 if (arg > 3)
442 chunk_strcat(&trash, " ");
443 chunk_strcat(&trash, args[arg]);
444 }
445
Willy Tarreau368bff42019-12-06 17:18:28 +0100446 thread_isolate();
447 if (pipe(pipefd) < 0)
448 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200449
Willy Tarreau368bff42019-12-06 17:18:28 +0100450 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
451 goto fail_fcntl;
452
453 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
454 goto fail_fcntl;
455
456 pid = fork();
457
458 if (pid < 0)
459 goto fail_fork;
460 else if (pid == 0) {
461 /* child */
462 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
463
464 close(0);
465 dup2(pipefd[1], 1);
466 dup2(pipefd[1], 2);
467
468 cmd[2] = trash.area;
469 execvp(cmd[0], cmd);
470 printf("execvp() failed\n");
471 exit(1);
472 }
473
474 /* parent */
475 thread_release();
476 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200477 chunk_reset(&trash);
478 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100479 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
480 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200481 break;
482 trash.data += ret;
483 if (trash.data + 20 == trash.size) {
484 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
485 break;
486 }
487 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100488 close(pipefd[0]);
489 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200490 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200491 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100492
493 fail_fork:
494 fail_fcntl:
495 close(pipefd[0]);
496 close(pipefd[1]);
497 fail_pipe:
498 thread_release();
499 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200500}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200501#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200502
503/* parse a "debug dev hex" command. It always returns 1. */
504static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
505{
506 unsigned long start, len;
507
508 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
509 return 1;
510
Willy Tarreau9d008692019-08-09 11:21:01 +0200511 if (!*args[3])
512 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200513
514 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200515 if (!start)
516 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200517
Willy Tarreau4781b152021-04-06 13:53:36 +0200518 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau9b013702019-10-24 18:18:02 +0200519
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200520 /* by default, dump ~128 till next block of 16 */
521 len = strtoul(args[4], NULL, 0);
522 if (!len)
523 len = ((start + 128) & -16) - start;
524
525 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200526 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200527 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200528 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200529}
530
Willy Tarreau48129be2021-05-04 18:40:50 +0200531/* parse a "debug dev sym <addr>" command. It always returns 1. */
532static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
533{
534 unsigned long addr;
535
536 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
537 return 1;
538
539 if (!*args[3])
540 return cli_err(appctx, "Missing memory address to be resolved.\n");
541
542 _HA_ATOMIC_INC(&debug_commands_issued);
543
544 addr = strtoul(args[3], NULL, 0);
545 chunk_printf(&trash, "%#lx resolves to ", addr);
546 resolve_sym_name(&trash, NULL, (const void *)addr);
547 chunk_appendf(&trash, "\n");
548
549 return cli_msg(appctx, LOG_INFO, trash.area);
550}
551
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200552/* parse a "debug dev tkill" command. It always returns 1. */
553static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
554{
555 int thr = 0;
556 int sig = SIGABRT;
557
558 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
559 return 1;
560
561 if (*args[3])
562 thr = atoi(args[3]);
563
Willy Tarreau9d008692019-08-09 11:21:01 +0200564 if (thr < 0 || thr > global.nbthread)
565 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200566
567 if (*args[4])
568 sig = atoi(args[4]);
569
Willy Tarreau4781b152021-04-06 13:53:36 +0200570 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200571 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200572 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200573 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200574 raise(sig);
575 return 1;
576}
577
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100578/* parse a "debug dev write" command. It always returns 1. */
579static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
580{
581 unsigned long len;
582
583 if (!*args[3])
584 return cli_err(appctx, "Missing output size.\n");
585
586 len = strtoul(args[3], NULL, 0);
587 if (len >= trash.size)
588 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
589
Willy Tarreau4781b152021-04-06 13:53:36 +0200590 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100591
592 chunk_reset(&trash);
593 trash.data = len;
594 memset(trash.area, '.', trash.data);
595 trash.area[trash.data] = 0;
596 for (len = 64; len < trash.data; len += 64)
597 trash.area[len] = '\n';
598 return cli_msg(appctx, LOG_INFO, trash.area);
599}
600
Willy Tarreau68680bb2019-10-23 17:23:25 +0200601/* parse a "debug dev stream" command */
602/*
603 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
604 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
605 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
606 * [sif.s[=<state>]] [sib.s[=<state>]]
607 */
608static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
609{
610 struct stream *s = si_strm(appctx->owner);
611 int arg;
612 void *ptr;
613 int size;
614 const char *word, *end;
615 struct ist name;
616 char *msg = NULL;
617 char *endarg;
618 unsigned long long old, new;
619
620 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
621 return 1;
622
623 ptr = NULL; size = 0;
624
625 if (!*args[3]) {
626 return cli_err(appctx,
627 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
628 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
629 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
630 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
631 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
632 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
633 );
634 }
635
Willy Tarreau4781b152021-04-06 13:53:36 +0200636 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200637 for (arg = 3; *args[arg]; arg++) {
638 old = 0;
639 end = word = args[arg];
640 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
641 end++;
642 name = ist2(word, end - word);
643 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200644 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200645 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200646 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200647 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200648 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200649 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200650 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200651 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200652 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200653 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200654 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200655 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200656 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200657 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200658 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200659 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200660 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200661 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200662 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200663 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200664 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200665 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200666 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200667 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200668 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200669 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200670 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200671 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200672 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200673 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200674 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200675 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
676 continue;
677 } else
678 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
679
680 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200681 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200682 if (size == 8)
683 old = read_u64(ptr);
684 else if (size == 4)
685 old = read_u32(ptr);
686 else if (size == 2)
687 old = read_u16(ptr);
688 else
689 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200690 } else {
691 memprintf(&msg,
692 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
693 msg ? msg : "", ptr, (int)(end - word), word);
694 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200695 }
696
697 /* parse the new value . */
698 new = strtoll(end + 1, &endarg, 0);
699 if (end[1] && *endarg) {
700 if (strcmp(end + 1, "now") == 0)
701 new = now_ms;
702 else {
703 memprintf(&msg,
704 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
705 msg ? msg : "", end + 1, (int)(end - word), word);
706 continue;
707 }
708 }
709
710 switch (*end) {
711 case '\0': /* show */
712 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
713 new = old; // do not change the value
714 break;
715
716 case '=': /* set */
717 break;
718
719 case '^': /* XOR */
720 new = old ^ new;
721 break;
722
723 case '+': /* OR */
724 new = old | new;
725 break;
726
727 case '-': /* AND NOT */
728 new = old & ~new;
729 break;
730
731 default:
732 break;
733 }
734
735 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200736 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200737 if (size == 8)
738 write_u64(ptr, new);
739 else if (size == 4)
740 write_u32(ptr, new);
741 else if (size == 2)
742 write_u16(ptr, new);
743 else
744 *(uint8_t *)ptr = new;
745 }
746 }
747
748 if (msg && *msg)
749 return cli_dynmsg(appctx, LOG_INFO, msg);
750 return 1;
751}
752
Willy Tarreau144f84a2021-03-02 16:09:26 +0100753static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100754{
755 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
756 unsigned long inter = tctx[1];
757 unsigned long rnd;
758
759 t->expire = tick_add(now_ms, inter);
760
761 /* half of the calls will wake up another entry */
Willy Tarreau06e69b52021-03-02 14:01:35 +0100762 rnd = statistical_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100763 if (rnd & 1) {
764 rnd >>= 1;
765 rnd %= tctx[0];
766 rnd = tctx[rnd + 2];
767
768 if (rnd & 1)
769 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
770 else
771 tasklet_wakeup((struct tasklet *)rnd);
772 }
773 return t;
774}
775
Willy Tarreau144f84a2021-03-02 16:09:26 +0100776static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
Willy Tarreaua5a44792020-11-29 17:12:15 +0100777{
778 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
779 unsigned long rnd;
780 int i;
781
782 /* wake up two random entries */
783 for (i = 0; i < 2; i++) {
Willy Tarreau06e69b52021-03-02 14:01:35 +0100784 rnd = statistical_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100785 rnd = tctx[rnd + 2];
786
787 if (rnd & 1)
788 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
789 else
790 tasklet_wakeup((struct tasklet *)rnd);
791 }
792 return t;
793}
794
795/* parse a "debug dev sched" command
796 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
797 */
798static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
799{
800 int arg;
801 void *ptr;
802 int size;
803 const char *word, *end;
804 struct ist name;
805 char *msg = NULL;
806 char *endarg;
807 unsigned long long new;
808 unsigned long count = 0;
809 unsigned long thrid = 0;
810 unsigned int inter = 0;
811 unsigned long mask, tmask;
812 unsigned long i;
813 int mode = 0; // 0 = tasklet; 1 = task
814 int single = 0;
815 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
816
817 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
818 return 1;
819
820 ptr = NULL; size = 0;
821 mask = all_threads_mask;
822
823 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
824 return cli_err(appctx,
825 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
826 " <obj> = {count | mask | inter | single }\n"
827 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
828 );
829 }
830
831 mode = strcmp(args[3], "task") == 0;
832
Willy Tarreau4781b152021-04-06 13:53:36 +0200833 _HA_ATOMIC_INC(&debug_commands_issued);
Willy Tarreaua5a44792020-11-29 17:12:15 +0100834 for (arg = 4; *args[arg]; arg++) {
835 end = word = args[arg];
836 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
837 end++;
838 name = ist2(word, end - word);
839 if (isteq(name, ist("count"))) {
840 ptr = &count; size = sizeof(count);
841 } else if (isteq(name, ist("mask"))) {
842 ptr = &mask; size = sizeof(mask);
843 } else if (isteq(name, ist("tid"))) {
844 ptr = &thrid; size = sizeof(thrid);
845 } else if (isteq(name, ist("inter"))) {
846 ptr = &inter; size = sizeof(inter);
847 } else if (isteq(name, ist("single"))) {
848 ptr = &single; size = sizeof(single);
849 } else
850 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
851
852 /* parse the new value . */
853 new = strtoll(end + 1, &endarg, 0);
854 if (end[1] && *endarg) {
855 memprintf(&msg,
856 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
857 msg ? msg : "", end + 1, (int)(end - word), word);
858 continue;
859 }
860
861 /* write the new value */
862 if (size == 8)
863 write_u64(ptr, new);
864 else if (size == 4)
865 write_u32(ptr, new);
866 else if (size == 2)
867 write_u16(ptr, new);
868 else
869 *(uint8_t *)ptr = new;
870 }
871
872 tctx = calloc(sizeof(*tctx), count + 2);
873 if (!tctx)
874 goto fail;
875
876 tctx[0] = (unsigned long)count;
877 tctx[1] = (unsigned long)inter;
878
879 mask &= all_threads_mask;
880 if (!mask)
881 mask = tid_bit;
882
883 tmask = 0;
884 for (i = 0; i < count; i++) {
885 if (single || mode == 0) {
886 /* look for next bit matching a bit in mask or loop back to zero */
887 for (tmask <<= 1; !(mask & tmask); ) {
888 if (!(mask & -tmask))
889 tmask = 1;
890 else
891 tmask <<= 1;
892 }
893 } else {
894 /* multi-threaded task */
895 tmask = mask;
896 }
897
898 /* now, if poly or mask was set, tmask corresponds to the
899 * valid thread mask to use, otherwise it remains zero.
900 */
901 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
902 if (mode == 0) {
903 struct tasklet *tl = tasklet_new();
904
905 if (!tl)
906 goto fail;
907
908 if (tmask)
909 tl->tid = my_ffsl(tmask) - 1;
910 tl->process = debug_tasklet_handler;
911 tl->context = tctx;
912 tctx[i + 2] = (unsigned long)tl;
913 } else {
914 struct task *task = task_new(tmask ? tmask : tid_bit);
915
916 if (!task)
917 goto fail;
918
919 task->process = debug_task_handler;
920 task->context = tctx;
921 tctx[i + 2] = (unsigned long)task + 1;
922 }
923 }
924
925 /* start the tasks and tasklets */
926 for (i = 0; i < count; i++) {
927 unsigned long ctx = tctx[i + 2];
928
929 if (ctx & 1)
930 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
931 else
932 tasklet_wakeup((struct tasklet *)ctx);
933 }
934
935 if (msg && *msg)
936 return cli_dynmsg(appctx, LOG_INFO, msg);
937 return 1;
938
939 fail:
940 /* free partially allocated entries */
941 for (i = 0; tctx && i < count; i++) {
942 unsigned long ctx = tctx[i + 2];
943
944 if (!ctx)
945 break;
946
947 if (ctx & 1)
948 task_destroy((struct task *)(ctx - 1));
949 else
950 tasklet_free((struct tasklet *)ctx);
951 }
952
953 free(tctx);
954 return cli_err(appctx, "Not enough memory");
955}
956
Willy Tarreaua6026a02020-07-02 09:14:48 +0200957#if defined(DEBUG_MEM_STATS)
958/* CLI parser for the "debug dev memstats" command */
959static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
960{
961 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
962 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
963
964 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
965 return 1;
966
967 if (strcmp(args[3], "reset") == 0) {
968 struct mem_stats *ptr;
969
970 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
971 return 1;
972
973 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
974 _HA_ATOMIC_STORE(&ptr->calls, 0);
975 _HA_ATOMIC_STORE(&ptr->size, 0);
976 }
977 return 1;
978 }
979
980 if (strcmp(args[3], "all") == 0)
981 appctx->ctx.cli.i0 = 1;
982
983 /* otherwise proceed with the dump from p0 to p1 */
984 appctx->ctx.cli.p0 = &__start_mem_stats;
985 appctx->ctx.cli.p1 = &__stop_mem_stats;
986 return 0;
987}
988
989/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
990 * structs referenced by pointers located between p0 and p1. Dumps all entries
991 * if i0 > 0, otherwise only non-zero calls.
992 */
993static int debug_iohandler_memstats(struct appctx *appctx)
994{
995 struct stream_interface *si = appctx->owner;
996 struct mem_stats *ptr = appctx->ctx.cli.p0;
997 int ret = 1;
998
999 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1000 goto end;
1001
1002 chunk_reset(&trash);
1003
1004 /* we have two inner loops here, one for the proxy, the other one for
1005 * the buffer.
1006 */
1007 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
1008 const char *type;
1009 const char *name;
1010 const char *p;
1011
1012 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1013 continue;
1014
1015 /* basename only */
1016 for (p = name = ptr->file; *p; p++) {
1017 if (*p == '/')
1018 name = p + 1;
1019 }
1020
1021 switch (ptr->type) {
1022 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1023 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1024 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1025 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1026 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1027 default: type = "UNSET"; break;
1028 }
1029
1030 //chunk_printf(&trash,
1031 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1032 // name, ptr->line, type,
1033 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1034 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1035
1036 chunk_printf(&trash, "%s:%d", name, ptr->line);
1037 while (trash.data < 25)
1038 trash.area[trash.data++] = ' ';
1039 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1040 type,
1041 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1042 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1043
1044 if (ci_putchk(si_ic(si), &trash) == -1) {
1045 si_rx_room_blk(si);
1046 appctx->ctx.cli.p0 = ptr;
1047 ret = 0;
1048 break;
1049 }
1050 }
1051
1052 end:
1053 return ret;
1054}
1055
1056#endif
1057
Willy Tarreauc7091d82019-05-17 10:08:49 +02001058#ifndef USE_THREAD_DUMP
1059
1060/* This function dumps all threads' state to the trash. This version is the
1061 * most basic one, which doesn't inspect other threads.
1062 */
1063void ha_thread_dump_all_to_trash()
1064{
1065 unsigned int thr;
1066
1067 for (thr = 0; thr < global.nbthread; thr++)
1068 ha_thread_dump(&trash, thr, tid);
1069}
1070
1071#else /* below USE_THREAD_DUMP is set */
1072
Willy Tarreauc7091d82019-05-17 10:08:49 +02001073/* ID of the thread requesting the dump */
1074static unsigned int thread_dump_tid;
1075
1076/* points to the buffer where the dump functions should write. It must
1077 * have already been initialized by the requester. Nothing is done if
1078 * it's NULL.
1079 */
1080struct buffer *thread_dump_buffer = NULL;
1081
1082void ha_thread_dump_all_to_trash()
1083{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001084 unsigned long old;
1085
1086 while (1) {
1087 old = 0;
1088 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1089 break;
1090 ha_thread_relax();
1091 }
1092
1093 thread_dump_buffer = &trash;
1094 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001095 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001096}
1097
1098/* handles DEBUGSIG to dump the state of the thread it's working on */
1099void debug_handler(int sig, siginfo_t *si, void *arg)
1100{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001101 /* first, let's check it's really for us and that we didn't just get
1102 * a spurious DEBUGSIG.
1103 */
1104 if (!(threads_to_dump & tid_bit))
1105 return;
1106
Willy Tarreauc7091d82019-05-17 10:08:49 +02001107 /* There are 4 phases in the dump process:
1108 * 1- wait for our turn, i.e. when all lower bits are gone.
1109 * 2- perform the action if our bit is set
1110 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001111 * the last one and have to put them all as a signal
1112 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001113 */
1114
1115 /* wait for all previous threads to finish first */
1116 while (threads_to_dump & (tid_bit - 1))
1117 ha_thread_relax();
1118
1119 /* dump if needed */
1120 if (threads_to_dump & tid_bit) {
1121 if (thread_dump_buffer)
1122 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1123 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1124 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001125 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001126 thread_dump_buffer = NULL;
1127 }
1128 else
1129 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1130 }
1131
1132 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001133 * bits again to broadcast the leaving condition so we'll see ourselves
1134 * present again. This way the threads_to_dump variable never passes to
1135 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001136 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001137 while (!(threads_to_dump & tid_bit))
1138 ha_thread_relax();
1139 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001140
1141 /* mark the current thread as stuck to detect it upon next invocation
1142 * if it didn't move.
1143 */
1144 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1145 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001146}
1147
1148static int init_debug_per_thread()
1149{
1150 sigset_t set;
1151
1152 /* unblock the DEBUGSIG signal we intend to use */
1153 sigemptyset(&set);
1154 sigaddset(&set, DEBUGSIG);
1155 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1156 return 1;
1157}
1158
1159static int init_debug()
1160{
1161 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001162 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001163
Willy Tarreau0214b452020-03-04 06:01:40 +01001164 /* calling backtrace() will access libgcc at runtime. We don't want to
1165 * do it after the chroot, so let's perform a first call to have it
1166 * ready in memory for later use.
1167 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001168 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001169 sa.sa_handler = NULL;
1170 sa.sa_sigaction = debug_handler;
1171 sigemptyset(&sa.sa_mask);
1172 sa.sa_flags = SA_SIGINFO;
1173 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001174 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001175}
1176
1177REGISTER_POST_CHECK(init_debug);
1178REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1179
1180#endif /* USE_THREAD_DUMP */
1181
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001182/* register cli keywords */
1183static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001184 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
1185 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1186 {{ "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 +02001187#if defined(DEBUG_DEV)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001188 {{ "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 +02001189#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001190 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1191 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1192 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1193 {{ "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 +02001194#if defined(DEBUG_MEM_STATS)
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001195 {{ "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 +02001196#endif
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001197 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
1198 {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
1199 {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1200 {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT },
1201 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
1202 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
1203 {{ "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 +02001204 {{},}
1205}};
1206
1207INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);