blob: bf64f19f05daa52023beb918de04acdf1628e1d1 [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 Tarreau8a069eb2020-11-30 16:17:33 +010045/* Xorshift RNGs from http://www.jstatsoft.org/v08/i14/paper */
Willy Tarreauc7ead072020-12-18 16:26:36 +010046static THREAD_LOCAL unsigned int y = 2463534242U;
Willy Tarreau8a069eb2020-11-30 16:17:33 +010047static unsigned int debug_prng()
48{
Willy Tarreau8a069eb2020-11-30 16:17:33 +010049 y ^= y << 13;
50 y ^= y >> 17;
51 y ^= y << 5;
52 return y;
53}
54
Willy Tarreau123fc972021-01-22 13:52:41 +010055/* dumps a backtrace of the current thread that is appended to buffer <buf>.
56 * Lines are prefixed with the string <prefix> which may be empty (used for
57 * indenting). It is recommended to use this at a function's tail so that
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010058 * the function does not appear in the call stack. The <dump> argument
59 * indicates what dump state to start from, and should usually be zero. It
60 * may be among the following values:
61 * - 0: search usual callers before step 1, or directly jump to 2
62 * - 1: skip usual callers before step 2
63 * - 2: dump until polling loop, scheduler, or main() (excluded)
64 * - 3: end
65 * - 4-7: like 0 but stops *after* main.
Willy Tarreau123fc972021-01-22 13:52:41 +010066 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010067void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
Willy Tarreau123fc972021-01-22 13:52:41 +010068{
69 struct buffer bak;
70 char pfx2[100];
71 void *callers[100];
72 int j, nptrs;
73 const void *addr;
Willy Tarreau123fc972021-01-22 13:52:41 +010074
75 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
76 if (!nptrs)
77 return;
78
79 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
80 pfx2[0] = 0;
81
82 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
83 * produce similar output to the following:
84 */
85 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010086 for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
87 if (j == nptrs && !(dump & 3)) {
Willy Tarreau123fc972021-01-22 13:52:41 +010088 /* we failed to spot the starting point of the
89 * dump, let's start over dumping everything we
90 * have.
91 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010092 dump += 2;
Willy Tarreau123fc972021-01-22 13:52:41 +010093 j = 0;
94 }
95 bak = *buf;
96 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
97 addr = resolve_sym_name(buf, ": ", callers[j]);
Willy Tarreau2bfce7e2021-01-22 14:48:34 +010098 if ((dump & 3) == 0) {
Willy Tarreau123fc972021-01-22 13:52:41 +010099 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +0100100 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +0100101 */
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 Tarreau2bfce7e2021-01-22 14:48:34 +0100104 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100105 *buf = bak;
106 continue;
107 }
108
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100109 if ((dump & 3) == 1) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100110 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100111 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
112 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100113 *buf = bak;
114 continue;
115 }
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100116 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100117 }
118
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100119 if ((dump & 3) == 2) {
120 /* still dumping */
121 if (dump == 6) {
122 /* we only stop *after* main and we must send the LF */
123 if (addr == main) {
124 j = nptrs;
125 dump++;
126 }
127 }
128 else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
129 dump++;
Willy Tarreau123fc972021-01-22 13:52:41 +0100130 *buf = bak;
131 break;
132 }
133 }
134 /* OK, line dumped */
135 chunk_appendf(buf, "\n");
136 }
137}
138
Willy Tarreaua8459b22021-01-22 14:12:27 +0100139/* dump a backtrace of current thread's stack to stderr. */
140void ha_backtrace_to_stderr()
141{
142 char area[2048];
143 struct buffer b = b_make(area, sizeof(area), 0, 0);
144
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100145 ha_dump_backtrace(&b, " ", 4);
Willy Tarreaua8459b22021-01-22 14:12:27 +0100146 if (b.data)
Willy Tarreau2cbe2e72021-01-22 15:58:26 +0100147 DISGUISE(write(2, b.area, b.data));
Willy Tarreaua8459b22021-01-22 14:12:27 +0100148}
149
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200150/* Dumps to the buffer some known information for the desired thread, and
151 * optionally extra info for the current thread. The dump will be appended to
152 * the buffer, so the caller is responsible for preliminary initializing it.
153 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200154 * in front of the calling thread's line (usually it's tid). Any stuck thread
155 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200156 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200157void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200158{
159 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +0100160 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
161 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
162 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200163
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200164 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200165 "%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 +0200166 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200167 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200168 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200169 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170 !!(global_tasks_mask & thr_bit),
171 !eb_is_empty(&task_per_thread[thr].timers),
172 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +0100173 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
174 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
175 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
176 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200177 task_per_thread[thr].task_list_size,
178 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200179 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200180 !!(task_profiling_mask & thr_bit));
181
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200182 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200183 " harmless=%d wantrdv=%d",
184 !!(threads_harmless_mask & thr_bit),
185 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200186
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200187 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200188 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200189
190 /* this is the end of what we can dump from outside the thread */
191
192 if (thr != tid)
193 return;
194
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200195 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200196 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100197
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100198 if (stuck) {
199 /* We only emit the backtrace for stuck threads in order not to
200 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100201 * Please leave this as the last instruction in this function
202 * so that the compiler uses tail merging and the current
203 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100204 */
Willy Tarreau2bfce7e2021-01-22 14:48:34 +0100205 ha_dump_backtrace(buf, " ", 0);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100206 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200207}
208
209
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200210/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200211 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200212 * with <pfx>. The buffer is only appended and the first output starts by the
213 * pointer itself. The caller is responsible for making sure the task is not
214 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200215 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200216void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200217{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200218 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200219 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200220 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200221
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200222 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200223 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200224 return;
225 }
226
Willy Tarreau20db9112019-05-17 14:14:35 +0200227 if (TASK_IS_TASKLET(task))
228 chunk_appendf(buf,
229 "%p (tasklet) calls=%u\n",
230 task,
231 task->calls);
232 else
233 chunk_appendf(buf,
234 "%p (task) calls=%u last=%llu%s\n",
235 task,
236 task->calls,
237 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
238 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200239
Willy Tarreau2e89b092020-03-03 17:13:02 +0100240 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
241 resolve_sym_name(buf, NULL, task->process);
242 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200243
Willy Tarreaua512b022019-08-21 14:12:19 +0200244 if (task->process == task_run_applet && (appctx = task->context))
245 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
246 else
247 chunk_appendf(buf, "\n");
248
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200249 if (task->process == process_stream && task->context)
250 s = (struct stream *)task->context;
251 else if (task->process == task_run_applet && task->context)
252 s = si_strm(((struct appctx *)task->context)->owner);
253 else if (task->process == si_cs_io_cb && task->context)
254 s = si_strm((struct stream_interface *)task->context);
255
256 if (s)
257 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200258
259#ifdef USE_LUA
260 hlua = NULL;
261 if (s && (hlua = s->hlua)) {
262 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
263 }
264 else if (task->process == hlua_process_task && (hlua = task->context)) {
265 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
266 }
267 else if (task->process == task_run_applet && (appctx = task->context) &&
268 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
269 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
270 }
271 else if (task->process == task_run_applet && (appctx = task->context) &&
272 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
273 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
274 }
275
Christopher Faulet471425f2020-07-24 19:08:05 +0200276 if (hlua && hlua->T) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200277 luaL_traceback(hlua->T, hlua->T, NULL, 0);
278 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
279 b_putchr(buf, '\n');
280 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200281 else
282 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200283#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200284}
285
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200286/* This function dumps all profiling settings. It returns 0 if the output
287 * buffer is full and it needs to be called again, otherwise non-zero.
288 */
289static int cli_io_handler_show_threads(struct appctx *appctx)
290{
291 struct stream_interface *si = appctx->owner;
292 int thr;
293
294 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
295 return 1;
296
297 if (appctx->st0)
298 thr = appctx->st1;
299 else
300 thr = 0;
301
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200302 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200303 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200304
305 if (ci_putchk(si_ic(si), &trash) == -1) {
306 /* failed, try again */
307 si_rx_room_blk(si);
308 appctx->st1 = thr;
309 return 0;
310 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200311 return 1;
312}
313
Willy Tarreau56131ca2019-05-20 13:48:29 +0200314/* dumps a state of all threads into the trash and on fd #2, then aborts. */
315void ha_panic()
316{
317 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200318 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200319 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100320 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200321 for (;;)
322 abort();
323}
324
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200325/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
326static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
327{
328 int code = atoi(args[3]);
329
330 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
331 return 1;
332
Willy Tarreau9b013702019-10-24 18:18:02 +0200333 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200334 exit(code);
335 return 1;
336}
337
Willy Tarreau5baf4fe2021-01-22 14:15:46 +0100338/* parse a "debug dev bug" command. It always returns 1, though it should never return.
339 * Note: we make sure not to make the function static so that it appears in the trace.
340 */
341int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
342{
343 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
344 return 1;
345
346 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
347 BUG_ON(one > zero);
348 return 1;
349}
350
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200351/* parse a "debug dev close" command. It always returns 1. */
352static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
353{
354 int fd;
355
356 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
357 return 1;
358
Willy Tarreau9d008692019-08-09 11:21:01 +0200359 if (!*args[3])
360 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200361
362 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200363 if (fd < 0 || fd >= global.maxsock)
364 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200365
Willy Tarreau9d008692019-08-09 11:21:01 +0200366 if (!fdtab[fd].owner)
367 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200368
Willy Tarreau9b013702019-10-24 18:18:02 +0200369 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200370 fd_delete(fd);
371 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200372}
373
374/* parse a "debug dev delay" command. It always returns 1. */
375static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
376{
377 int delay = atoi(args[3]);
378
379 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
380 return 1;
381
Willy Tarreau9b013702019-10-24 18:18:02 +0200382 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200383 usleep((long)delay * 1000);
384 return 1;
385}
386
387/* parse a "debug dev log" command. It always returns 1. */
388static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
389{
390 int arg;
391
392 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
393 return 1;
394
Willy Tarreau9b013702019-10-24 18:18:02 +0200395 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200396 chunk_reset(&trash);
397 for (arg = 3; *args[arg]; arg++) {
398 if (arg > 3)
399 chunk_strcat(&trash, " ");
400 chunk_strcat(&trash, args[arg]);
401 }
402
403 send_log(NULL, LOG_INFO, "%s\n", trash.area);
404 return 1;
405}
406
407/* parse a "debug dev loop" command. It always returns 1. */
408static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
409{
410 struct timeval deadline, curr;
411 int loop = atoi(args[3]);
412
413 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
414 return 1;
415
Willy Tarreau9b013702019-10-24 18:18:02 +0200416 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200417 gettimeofday(&curr, NULL);
418 tv_ms_add(&deadline, &curr, loop);
419
420 while (tv_ms_cmp(&curr, &deadline) < 0)
421 gettimeofday(&curr, NULL);
422
423 return 1;
424}
425
426/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
427static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
428{
429 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
430 return 1;
431
Willy Tarreau9b013702019-10-24 18:18:02 +0200432 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200433 ha_panic();
434 return 1;
435}
436
437/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200438#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200439static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
440{
Willy Tarreau368bff42019-12-06 17:18:28 +0100441 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200442 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100443 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200444
445 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
446 return 1;
447
Willy Tarreau9b013702019-10-24 18:18:02 +0200448 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200449 chunk_reset(&trash);
450 for (arg = 3; *args[arg]; arg++) {
451 if (arg > 3)
452 chunk_strcat(&trash, " ");
453 chunk_strcat(&trash, args[arg]);
454 }
455
Willy Tarreau368bff42019-12-06 17:18:28 +0100456 thread_isolate();
457 if (pipe(pipefd) < 0)
458 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200459
Willy Tarreau368bff42019-12-06 17:18:28 +0100460 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
461 goto fail_fcntl;
462
463 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
464 goto fail_fcntl;
465
466 pid = fork();
467
468 if (pid < 0)
469 goto fail_fork;
470 else if (pid == 0) {
471 /* child */
472 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
473
474 close(0);
475 dup2(pipefd[1], 1);
476 dup2(pipefd[1], 2);
477
478 cmd[2] = trash.area;
479 execvp(cmd[0], cmd);
480 printf("execvp() failed\n");
481 exit(1);
482 }
483
484 /* parent */
485 thread_release();
486 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200487 chunk_reset(&trash);
488 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100489 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
490 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200491 break;
492 trash.data += ret;
493 if (trash.data + 20 == trash.size) {
494 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
495 break;
496 }
497 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100498 close(pipefd[0]);
499 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200500 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200501 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100502
503 fail_fork:
504 fail_fcntl:
505 close(pipefd[0]);
506 close(pipefd[1]);
507 fail_pipe:
508 thread_release();
509 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200510}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200511#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200512
513/* parse a "debug dev hex" command. It always returns 1. */
514static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
515{
516 unsigned long start, len;
517
518 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
519 return 1;
520
Willy Tarreau9d008692019-08-09 11:21:01 +0200521 if (!*args[3])
522 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200523
524 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200525 if (!start)
526 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200527
Willy Tarreau9b013702019-10-24 18:18:02 +0200528 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
529
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200530 /* by default, dump ~128 till next block of 16 */
531 len = strtoul(args[4], NULL, 0);
532 if (!len)
533 len = ((start + 128) & -16) - start;
534
535 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200536 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200537 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200538 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200539}
540
541/* parse a "debug dev tkill" command. It always returns 1. */
542static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
543{
544 int thr = 0;
545 int sig = SIGABRT;
546
547 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
548 return 1;
549
550 if (*args[3])
551 thr = atoi(args[3]);
552
Willy Tarreau9d008692019-08-09 11:21:01 +0200553 if (thr < 0 || thr > global.nbthread)
554 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200555
556 if (*args[4])
557 sig = atoi(args[4]);
558
Willy Tarreau9b013702019-10-24 18:18:02 +0200559 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200560 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200561 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200562 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200563 raise(sig);
564 return 1;
565}
566
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100567/* parse a "debug dev write" command. It always returns 1. */
568static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
569{
570 unsigned long len;
571
572 if (!*args[3])
573 return cli_err(appctx, "Missing output size.\n");
574
575 len = strtoul(args[3], NULL, 0);
576 if (len >= trash.size)
577 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
578
579 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
580
581 chunk_reset(&trash);
582 trash.data = len;
583 memset(trash.area, '.', trash.data);
584 trash.area[trash.data] = 0;
585 for (len = 64; len < trash.data; len += 64)
586 trash.area[len] = '\n';
587 return cli_msg(appctx, LOG_INFO, trash.area);
588}
589
Willy Tarreau68680bb2019-10-23 17:23:25 +0200590/* parse a "debug dev stream" command */
591/*
592 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
593 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
594 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
595 * [sif.s[=<state>]] [sib.s[=<state>]]
596 */
597static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
598{
599 struct stream *s = si_strm(appctx->owner);
600 int arg;
601 void *ptr;
602 int size;
603 const char *word, *end;
604 struct ist name;
605 char *msg = NULL;
606 char *endarg;
607 unsigned long long old, new;
608
609 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
610 return 1;
611
612 ptr = NULL; size = 0;
613
614 if (!*args[3]) {
615 return cli_err(appctx,
616 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
617 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
618 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
619 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
620 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
621 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
622 );
623 }
624
Willy Tarreau9b013702019-10-24 18:18:02 +0200625 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200626 for (arg = 3; *args[arg]; arg++) {
627 old = 0;
628 end = word = args[arg];
629 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
630 end++;
631 name = ist2(word, end - word);
632 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200633 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200634 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200635 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200636 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200637 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200638 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200639 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200640 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200641 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200642 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200643 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200644 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200645 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200646 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200647 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200648 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200649 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200650 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200651 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200652 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200653 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200654 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200655 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200656 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200657 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200658 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200659 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200660 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200661 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200662 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200663 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200664 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
665 continue;
666 } else
667 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
668
669 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200670 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200671 if (size == 8)
672 old = read_u64(ptr);
673 else if (size == 4)
674 old = read_u32(ptr);
675 else if (size == 2)
676 old = read_u16(ptr);
677 else
678 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200679 } else {
680 memprintf(&msg,
681 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
682 msg ? msg : "", ptr, (int)(end - word), word);
683 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200684 }
685
686 /* parse the new value . */
687 new = strtoll(end + 1, &endarg, 0);
688 if (end[1] && *endarg) {
689 if (strcmp(end + 1, "now") == 0)
690 new = now_ms;
691 else {
692 memprintf(&msg,
693 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
694 msg ? msg : "", end + 1, (int)(end - word), word);
695 continue;
696 }
697 }
698
699 switch (*end) {
700 case '\0': /* show */
701 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
702 new = old; // do not change the value
703 break;
704
705 case '=': /* set */
706 break;
707
708 case '^': /* XOR */
709 new = old ^ new;
710 break;
711
712 case '+': /* OR */
713 new = old | new;
714 break;
715
716 case '-': /* AND NOT */
717 new = old & ~new;
718 break;
719
720 default:
721 break;
722 }
723
724 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200725 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200726 if (size == 8)
727 write_u64(ptr, new);
728 else if (size == 4)
729 write_u32(ptr, new);
730 else if (size == 2)
731 write_u16(ptr, new);
732 else
733 *(uint8_t *)ptr = new;
734 }
735 }
736
737 if (msg && *msg)
738 return cli_dynmsg(appctx, LOG_INFO, msg);
739 return 1;
740}
741
Willy Tarreaua5a44792020-11-29 17:12:15 +0100742static struct task *debug_task_handler(struct task *t, void *ctx, unsigned short state)
743{
744 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
745 unsigned long inter = tctx[1];
746 unsigned long rnd;
747
748 t->expire = tick_add(now_ms, inter);
749
750 /* half of the calls will wake up another entry */
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100751 rnd = debug_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100752 if (rnd & 1) {
753 rnd >>= 1;
754 rnd %= tctx[0];
755 rnd = tctx[rnd + 2];
756
757 if (rnd & 1)
758 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
759 else
760 tasklet_wakeup((struct tasklet *)rnd);
761 }
762 return t;
763}
764
765static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned short state)
766{
767 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
768 unsigned long rnd;
769 int i;
770
771 /* wake up two random entries */
772 for (i = 0; i < 2; i++) {
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100773 rnd = debug_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100774 rnd = tctx[rnd + 2];
775
776 if (rnd & 1)
777 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
778 else
779 tasklet_wakeup((struct tasklet *)rnd);
780 }
781 return t;
782}
783
784/* parse a "debug dev sched" command
785 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
786 */
787static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
788{
789 int arg;
790 void *ptr;
791 int size;
792 const char *word, *end;
793 struct ist name;
794 char *msg = NULL;
795 char *endarg;
796 unsigned long long new;
797 unsigned long count = 0;
798 unsigned long thrid = 0;
799 unsigned int inter = 0;
800 unsigned long mask, tmask;
801 unsigned long i;
802 int mode = 0; // 0 = tasklet; 1 = task
803 int single = 0;
804 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
805
806 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
807 return 1;
808
809 ptr = NULL; size = 0;
810 mask = all_threads_mask;
811
812 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
813 return cli_err(appctx,
814 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
815 " <obj> = {count | mask | inter | single }\n"
816 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
817 );
818 }
819
820 mode = strcmp(args[3], "task") == 0;
821
822 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
823 for (arg = 4; *args[arg]; arg++) {
824 end = word = args[arg];
825 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
826 end++;
827 name = ist2(word, end - word);
828 if (isteq(name, ist("count"))) {
829 ptr = &count; size = sizeof(count);
830 } else if (isteq(name, ist("mask"))) {
831 ptr = &mask; size = sizeof(mask);
832 } else if (isteq(name, ist("tid"))) {
833 ptr = &thrid; size = sizeof(thrid);
834 } else if (isteq(name, ist("inter"))) {
835 ptr = &inter; size = sizeof(inter);
836 } else if (isteq(name, ist("single"))) {
837 ptr = &single; size = sizeof(single);
838 } else
839 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
840
841 /* parse the new value . */
842 new = strtoll(end + 1, &endarg, 0);
843 if (end[1] && *endarg) {
844 memprintf(&msg,
845 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
846 msg ? msg : "", end + 1, (int)(end - word), word);
847 continue;
848 }
849
850 /* write the new value */
851 if (size == 8)
852 write_u64(ptr, new);
853 else if (size == 4)
854 write_u32(ptr, new);
855 else if (size == 2)
856 write_u16(ptr, new);
857 else
858 *(uint8_t *)ptr = new;
859 }
860
861 tctx = calloc(sizeof(*tctx), count + 2);
862 if (!tctx)
863 goto fail;
864
865 tctx[0] = (unsigned long)count;
866 tctx[1] = (unsigned long)inter;
867
868 mask &= all_threads_mask;
869 if (!mask)
870 mask = tid_bit;
871
872 tmask = 0;
873 for (i = 0; i < count; i++) {
874 if (single || mode == 0) {
875 /* look for next bit matching a bit in mask or loop back to zero */
876 for (tmask <<= 1; !(mask & tmask); ) {
877 if (!(mask & -tmask))
878 tmask = 1;
879 else
880 tmask <<= 1;
881 }
882 } else {
883 /* multi-threaded task */
884 tmask = mask;
885 }
886
887 /* now, if poly or mask was set, tmask corresponds to the
888 * valid thread mask to use, otherwise it remains zero.
889 */
890 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
891 if (mode == 0) {
892 struct tasklet *tl = tasklet_new();
893
894 if (!tl)
895 goto fail;
896
897 if (tmask)
898 tl->tid = my_ffsl(tmask) - 1;
899 tl->process = debug_tasklet_handler;
900 tl->context = tctx;
901 tctx[i + 2] = (unsigned long)tl;
902 } else {
903 struct task *task = task_new(tmask ? tmask : tid_bit);
904
905 if (!task)
906 goto fail;
907
908 task->process = debug_task_handler;
909 task->context = tctx;
910 tctx[i + 2] = (unsigned long)task + 1;
911 }
912 }
913
914 /* start the tasks and tasklets */
915 for (i = 0; i < count; i++) {
916 unsigned long ctx = tctx[i + 2];
917
918 if (ctx & 1)
919 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
920 else
921 tasklet_wakeup((struct tasklet *)ctx);
922 }
923
924 if (msg && *msg)
925 return cli_dynmsg(appctx, LOG_INFO, msg);
926 return 1;
927
928 fail:
929 /* free partially allocated entries */
930 for (i = 0; tctx && i < count; i++) {
931 unsigned long ctx = tctx[i + 2];
932
933 if (!ctx)
934 break;
935
936 if (ctx & 1)
937 task_destroy((struct task *)(ctx - 1));
938 else
939 tasklet_free((struct tasklet *)ctx);
940 }
941
942 free(tctx);
943 return cli_err(appctx, "Not enough memory");
944}
945
Willy Tarreaua6026a02020-07-02 09:14:48 +0200946#if defined(DEBUG_MEM_STATS)
947/* CLI parser for the "debug dev memstats" command */
948static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
949{
950 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
951 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
952
953 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
954 return 1;
955
956 if (strcmp(args[3], "reset") == 0) {
957 struct mem_stats *ptr;
958
959 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
960 return 1;
961
962 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
963 _HA_ATOMIC_STORE(&ptr->calls, 0);
964 _HA_ATOMIC_STORE(&ptr->size, 0);
965 }
966 return 1;
967 }
968
969 if (strcmp(args[3], "all") == 0)
970 appctx->ctx.cli.i0 = 1;
971
972 /* otherwise proceed with the dump from p0 to p1 */
973 appctx->ctx.cli.p0 = &__start_mem_stats;
974 appctx->ctx.cli.p1 = &__stop_mem_stats;
975 return 0;
976}
977
978/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
979 * structs referenced by pointers located between p0 and p1. Dumps all entries
980 * if i0 > 0, otherwise only non-zero calls.
981 */
982static int debug_iohandler_memstats(struct appctx *appctx)
983{
984 struct stream_interface *si = appctx->owner;
985 struct mem_stats *ptr = appctx->ctx.cli.p0;
986 int ret = 1;
987
988 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
989 goto end;
990
991 chunk_reset(&trash);
992
993 /* we have two inner loops here, one for the proxy, the other one for
994 * the buffer.
995 */
996 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
997 const char *type;
998 const char *name;
999 const char *p;
1000
1001 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
1002 continue;
1003
1004 /* basename only */
1005 for (p = name = ptr->file; *p; p++) {
1006 if (*p == '/')
1007 name = p + 1;
1008 }
1009
1010 switch (ptr->type) {
1011 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
1012 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
1013 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
1014 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
1015 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
1016 default: type = "UNSET"; break;
1017 }
1018
1019 //chunk_printf(&trash,
1020 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
1021 // name, ptr->line, type,
1022 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
1023 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1024
1025 chunk_printf(&trash, "%s:%d", name, ptr->line);
1026 while (trash.data < 25)
1027 trash.area[trash.data++] = ' ';
1028 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1029 type,
1030 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1031 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1032
1033 if (ci_putchk(si_ic(si), &trash) == -1) {
1034 si_rx_room_blk(si);
1035 appctx->ctx.cli.p0 = ptr;
1036 ret = 0;
1037 break;
1038 }
1039 }
1040
1041 end:
1042 return ret;
1043}
1044
1045#endif
1046
Willy Tarreauc7091d82019-05-17 10:08:49 +02001047#ifndef USE_THREAD_DUMP
1048
1049/* This function dumps all threads' state to the trash. This version is the
1050 * most basic one, which doesn't inspect other threads.
1051 */
1052void ha_thread_dump_all_to_trash()
1053{
1054 unsigned int thr;
1055
1056 for (thr = 0; thr < global.nbthread; thr++)
1057 ha_thread_dump(&trash, thr, tid);
1058}
1059
1060#else /* below USE_THREAD_DUMP is set */
1061
Willy Tarreauc7091d82019-05-17 10:08:49 +02001062/* ID of the thread requesting the dump */
1063static unsigned int thread_dump_tid;
1064
1065/* points to the buffer where the dump functions should write. It must
1066 * have already been initialized by the requester. Nothing is done if
1067 * it's NULL.
1068 */
1069struct buffer *thread_dump_buffer = NULL;
1070
1071void ha_thread_dump_all_to_trash()
1072{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001073 unsigned long old;
1074
1075 while (1) {
1076 old = 0;
1077 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1078 break;
1079 ha_thread_relax();
1080 }
1081
1082 thread_dump_buffer = &trash;
1083 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001084 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001085}
1086
1087/* handles DEBUGSIG to dump the state of the thread it's working on */
1088void debug_handler(int sig, siginfo_t *si, void *arg)
1089{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001090 /* first, let's check it's really for us and that we didn't just get
1091 * a spurious DEBUGSIG.
1092 */
1093 if (!(threads_to_dump & tid_bit))
1094 return;
1095
Willy Tarreauc7091d82019-05-17 10:08:49 +02001096 /* There are 4 phases in the dump process:
1097 * 1- wait for our turn, i.e. when all lower bits are gone.
1098 * 2- perform the action if our bit is set
1099 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001100 * the last one and have to put them all as a signal
1101 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001102 */
1103
1104 /* wait for all previous threads to finish first */
1105 while (threads_to_dump & (tid_bit - 1))
1106 ha_thread_relax();
1107
1108 /* dump if needed */
1109 if (threads_to_dump & tid_bit) {
1110 if (thread_dump_buffer)
1111 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1112 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1113 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001114 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001115 thread_dump_buffer = NULL;
1116 }
1117 else
1118 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1119 }
1120
1121 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001122 * bits again to broadcast the leaving condition so we'll see ourselves
1123 * present again. This way the threads_to_dump variable never passes to
1124 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001125 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001126 while (!(threads_to_dump & tid_bit))
1127 ha_thread_relax();
1128 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001129
1130 /* mark the current thread as stuck to detect it upon next invocation
1131 * if it didn't move.
1132 */
1133 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1134 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001135}
1136
1137static int init_debug_per_thread()
1138{
1139 sigset_t set;
1140
1141 /* unblock the DEBUGSIG signal we intend to use */
1142 sigemptyset(&set);
1143 sigaddset(&set, DEBUGSIG);
1144 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1145 return 1;
1146}
1147
1148static int init_debug()
1149{
1150 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001151 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001152
Willy Tarreau0214b452020-03-04 06:01:40 +01001153 /* calling backtrace() will access libgcc at runtime. We don't want to
1154 * do it after the chroot, so let's perform a first call to have it
1155 * ready in memory for later use.
1156 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001157 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001158 sa.sa_handler = NULL;
1159 sa.sa_sigaction = debug_handler;
1160 sigemptyset(&sa.sa_mask);
1161 sa.sa_flags = SA_SIGINFO;
1162 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001163 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001164}
1165
1166REGISTER_POST_CHECK(init_debug);
1167REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1168
1169#endif /* USE_THREAD_DUMP */
1170
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001171/* register cli keywords */
1172static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau5baf4fe2021-01-22 14:15:46 +01001173 {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON()", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub24ab222019-10-24 18:03:39 +02001174 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1175 {{ "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 +02001176#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +02001177 {{ "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 +02001178#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001179 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1180 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1181 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1182 {{ "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 +02001183#if defined(DEBUG_MEM_STATS)
1184 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT },
1185#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001186 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua5a44792020-11-29 17:12:15 +01001187 {{ "debug", "dev", "sched", NULL }, "debug dev sched ... : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub24ab222019-10-24 18:03:39 +02001188 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1189 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6cbe62b2020-03-05 17:16:24 +01001190 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub24ab222019-10-24 18:03:39 +02001191 {{ "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 +02001192 {{},}
1193}};
1194
1195INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);