blob: 2a5e164c6dd6a8270ef587696d1b0e69fd484539 [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
58 * the function does not appear in the call stack.
59 */
60void ha_dump_backtrace(struct buffer *buf, const char *prefix)
61{
62 struct buffer bak;
63 char pfx2[100];
64 void *callers[100];
65 int j, nptrs;
66 const void *addr;
67 int dump = 0;
68
69 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
70 if (!nptrs)
71 return;
72
73 if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
74 pfx2[0] = 0;
75
76 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
77 * produce similar output to the following:
78 */
79 chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
80 for (j = 0; (j < nptrs || dump < 2); j++) {
81 if (j == nptrs && !dump) {
82 /* we failed to spot the starting point of the
83 * dump, let's start over dumping everything we
84 * have.
85 */
86 dump = 2;
87 j = 0;
88 }
89 bak = *buf;
90 dump_addr_and_bytes(buf, pfx2, callers[j], 8);
91 addr = resolve_sym_name(buf, ": ", callers[j]);
92 if (dump == 0) {
93 /* dump not started, will start *after*
Willy Tarreaua8459b22021-01-22 14:12:27 +010094 * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
Willy Tarreau123fc972021-01-22 13:52:41 +010095 */
Willy Tarreaua8459b22021-01-22 14:12:27 +010096 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
97 addr == ha_backtrace_to_stderr)
Willy Tarreau123fc972021-01-22 13:52:41 +010098 dump = 1;
99 *buf = bak;
100 continue;
101 }
102
103 if (dump == 1) {
104 /* starting */
Willy Tarreaua8459b22021-01-22 14:12:27 +0100105 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
106 addr == ha_backtrace_to_stderr) {
Willy Tarreau123fc972021-01-22 13:52:41 +0100107 *buf = bak;
108 continue;
109 }
110 dump = 2;
111 }
112
113 if (dump == 2) {
114 /* dumping */
115 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
116 dump = 3;
117 *buf = bak;
118 break;
119 }
120 }
121 /* OK, line dumped */
122 chunk_appendf(buf, "\n");
123 }
124}
125
Willy Tarreaua8459b22021-01-22 14:12:27 +0100126/* dump a backtrace of current thread's stack to stderr. */
127void ha_backtrace_to_stderr()
128{
129 char area[2048];
130 struct buffer b = b_make(area, sizeof(area), 0, 0);
131
132 ha_dump_backtrace(&b, " ");
133 if (b.data)
134 write(2, b.area, b.data);
135}
136
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200137/* Dumps to the buffer some known information for the desired thread, and
138 * optionally extra info for the current thread. The dump will be appended to
139 * the buffer, so the caller is responsible for preliminary initializing it.
140 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200141 * in front of the calling thread's line (usually it's tid). Any stuck thread
142 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200143 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200144void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200145{
146 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +0100147 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
148 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
149 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200150
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200151 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200152 "%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 +0200153 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200154 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200155 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +0200156 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200157 !!(global_tasks_mask & thr_bit),
158 !eb_is_empty(&task_per_thread[thr].timers),
159 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +0100160 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
161 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
162 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
163 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200164 task_per_thread[thr].task_list_size,
165 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200166 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200167 !!(task_profiling_mask & thr_bit));
168
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200169 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170 " harmless=%d wantrdv=%d",
171 !!(threads_harmless_mask & thr_bit),
172 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200173
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200174 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +0200175 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176
177 /* this is the end of what we can dump from outside the thread */
178
179 if (thr != tid)
180 return;
181
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200182 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200183 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100184
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100185 if (stuck) {
186 /* We only emit the backtrace for stuck threads in order not to
187 * waste precious output buffer space with non-interesting data.
Willy Tarreau123fc972021-01-22 13:52:41 +0100188 * Please leave this as the last instruction in this function
189 * so that the compiler uses tail merging and the current
190 * function does not appear in the stack.
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100191 */
Willy Tarreau123fc972021-01-22 13:52:41 +0100192 ha_dump_backtrace(buf, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100193 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200194}
195
196
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200197/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200198 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200199 * with <pfx>. The buffer is only appended and the first output starts by the
200 * pointer itself. The caller is responsible for making sure the task is not
201 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200202 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200203void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200204{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200205 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200206 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200207 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200208
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200209 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200210 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200211 return;
212 }
213
Willy Tarreau20db9112019-05-17 14:14:35 +0200214 if (TASK_IS_TASKLET(task))
215 chunk_appendf(buf,
216 "%p (tasklet) calls=%u\n",
217 task,
218 task->calls);
219 else
220 chunk_appendf(buf,
221 "%p (task) calls=%u last=%llu%s\n",
222 task,
223 task->calls,
224 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
225 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200226
Willy Tarreau2e89b092020-03-03 17:13:02 +0100227 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
228 resolve_sym_name(buf, NULL, task->process);
229 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200230
Willy Tarreaua512b022019-08-21 14:12:19 +0200231 if (task->process == task_run_applet && (appctx = task->context))
232 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
233 else
234 chunk_appendf(buf, "\n");
235
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200236 if (task->process == process_stream && task->context)
237 s = (struct stream *)task->context;
238 else if (task->process == task_run_applet && task->context)
239 s = si_strm(((struct appctx *)task->context)->owner);
240 else if (task->process == si_cs_io_cb && task->context)
241 s = si_strm((struct stream_interface *)task->context);
242
243 if (s)
244 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200245
246#ifdef USE_LUA
247 hlua = NULL;
248 if (s && (hlua = s->hlua)) {
249 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
250 }
251 else if (task->process == hlua_process_task && (hlua = task->context)) {
252 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
253 }
254 else if (task->process == task_run_applet && (appctx = task->context) &&
255 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
256 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
257 }
258 else if (task->process == task_run_applet && (appctx = task->context) &&
259 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
260 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
261 }
262
Christopher Faulet471425f2020-07-24 19:08:05 +0200263 if (hlua && hlua->T) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200264 luaL_traceback(hlua->T, hlua->T, NULL, 0);
265 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
266 b_putchr(buf, '\n');
267 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200268 else
269 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200270#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200271}
272
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200273/* This function dumps all profiling settings. It returns 0 if the output
274 * buffer is full and it needs to be called again, otherwise non-zero.
275 */
276static int cli_io_handler_show_threads(struct appctx *appctx)
277{
278 struct stream_interface *si = appctx->owner;
279 int thr;
280
281 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
282 return 1;
283
284 if (appctx->st0)
285 thr = appctx->st1;
286 else
287 thr = 0;
288
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200289 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200290 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200291
292 if (ci_putchk(si_ic(si), &trash) == -1) {
293 /* failed, try again */
294 si_rx_room_blk(si);
295 appctx->st1 = thr;
296 return 0;
297 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200298 return 1;
299}
300
Willy Tarreau56131ca2019-05-20 13:48:29 +0200301/* dumps a state of all threads into the trash and on fd #2, then aborts. */
302void ha_panic()
303{
304 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200305 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200306 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100307 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200308 for (;;)
309 abort();
310}
311
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200312/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
313static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
314{
315 int code = atoi(args[3]);
316
317 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
318 return 1;
319
Willy Tarreau9b013702019-10-24 18:18:02 +0200320 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200321 exit(code);
322 return 1;
323}
324
325/* parse a "debug dev close" command. It always returns 1. */
326static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
327{
328 int fd;
329
330 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
331 return 1;
332
Willy Tarreau9d008692019-08-09 11:21:01 +0200333 if (!*args[3])
334 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200335
336 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200337 if (fd < 0 || fd >= global.maxsock)
338 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200339
Willy Tarreau9d008692019-08-09 11:21:01 +0200340 if (!fdtab[fd].owner)
341 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200342
Willy Tarreau9b013702019-10-24 18:18:02 +0200343 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200344 fd_delete(fd);
345 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200346}
347
348/* parse a "debug dev delay" command. It always returns 1. */
349static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
350{
351 int delay = atoi(args[3]);
352
353 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
354 return 1;
355
Willy Tarreau9b013702019-10-24 18:18:02 +0200356 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200357 usleep((long)delay * 1000);
358 return 1;
359}
360
361/* parse a "debug dev log" command. It always returns 1. */
362static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
363{
364 int arg;
365
366 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
367 return 1;
368
Willy Tarreau9b013702019-10-24 18:18:02 +0200369 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200370 chunk_reset(&trash);
371 for (arg = 3; *args[arg]; arg++) {
372 if (arg > 3)
373 chunk_strcat(&trash, " ");
374 chunk_strcat(&trash, args[arg]);
375 }
376
377 send_log(NULL, LOG_INFO, "%s\n", trash.area);
378 return 1;
379}
380
381/* parse a "debug dev loop" command. It always returns 1. */
382static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
383{
384 struct timeval deadline, curr;
385 int loop = atoi(args[3]);
386
387 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
388 return 1;
389
Willy Tarreau9b013702019-10-24 18:18:02 +0200390 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391 gettimeofday(&curr, NULL);
392 tv_ms_add(&deadline, &curr, loop);
393
394 while (tv_ms_cmp(&curr, &deadline) < 0)
395 gettimeofday(&curr, NULL);
396
397 return 1;
398}
399
400/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
401static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
402{
403 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
404 return 1;
405
Willy Tarreau9b013702019-10-24 18:18:02 +0200406 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200407 ha_panic();
408 return 1;
409}
410
411/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200412#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200413static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
414{
Willy Tarreau368bff42019-12-06 17:18:28 +0100415 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200416 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100417 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200418
419 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
420 return 1;
421
Willy Tarreau9b013702019-10-24 18:18:02 +0200422 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200423 chunk_reset(&trash);
424 for (arg = 3; *args[arg]; arg++) {
425 if (arg > 3)
426 chunk_strcat(&trash, " ");
427 chunk_strcat(&trash, args[arg]);
428 }
429
Willy Tarreau368bff42019-12-06 17:18:28 +0100430 thread_isolate();
431 if (pipe(pipefd) < 0)
432 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200433
Willy Tarreau368bff42019-12-06 17:18:28 +0100434 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
435 goto fail_fcntl;
436
437 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
438 goto fail_fcntl;
439
440 pid = fork();
441
442 if (pid < 0)
443 goto fail_fork;
444 else if (pid == 0) {
445 /* child */
446 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
447
448 close(0);
449 dup2(pipefd[1], 1);
450 dup2(pipefd[1], 2);
451
452 cmd[2] = trash.area;
453 execvp(cmd[0], cmd);
454 printf("execvp() failed\n");
455 exit(1);
456 }
457
458 /* parent */
459 thread_release();
460 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200461 chunk_reset(&trash);
462 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100463 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
464 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200465 break;
466 trash.data += ret;
467 if (trash.data + 20 == trash.size) {
468 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
469 break;
470 }
471 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100472 close(pipefd[0]);
473 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200474 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200475 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100476
477 fail_fork:
478 fail_fcntl:
479 close(pipefd[0]);
480 close(pipefd[1]);
481 fail_pipe:
482 thread_release();
483 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200484}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200485#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200486
487/* parse a "debug dev hex" command. It always returns 1. */
488static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
489{
490 unsigned long start, len;
491
492 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
493 return 1;
494
Willy Tarreau9d008692019-08-09 11:21:01 +0200495 if (!*args[3])
496 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200497
498 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200499 if (!start)
500 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200501
Willy Tarreau9b013702019-10-24 18:18:02 +0200502 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
503
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200504 /* by default, dump ~128 till next block of 16 */
505 len = strtoul(args[4], NULL, 0);
506 if (!len)
507 len = ((start + 128) & -16) - start;
508
509 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200510 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200511 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200512 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200513}
514
515/* parse a "debug dev tkill" command. It always returns 1. */
516static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
517{
518 int thr = 0;
519 int sig = SIGABRT;
520
521 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
522 return 1;
523
524 if (*args[3])
525 thr = atoi(args[3]);
526
Willy Tarreau9d008692019-08-09 11:21:01 +0200527 if (thr < 0 || thr > global.nbthread)
528 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200529
530 if (*args[4])
531 sig = atoi(args[4]);
532
Willy Tarreau9b013702019-10-24 18:18:02 +0200533 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200534 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200535 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200536 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200537 raise(sig);
538 return 1;
539}
540
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100541/* parse a "debug dev write" command. It always returns 1. */
542static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
543{
544 unsigned long len;
545
546 if (!*args[3])
547 return cli_err(appctx, "Missing output size.\n");
548
549 len = strtoul(args[3], NULL, 0);
550 if (len >= trash.size)
551 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
552
553 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
554
555 chunk_reset(&trash);
556 trash.data = len;
557 memset(trash.area, '.', trash.data);
558 trash.area[trash.data] = 0;
559 for (len = 64; len < trash.data; len += 64)
560 trash.area[len] = '\n';
561 return cli_msg(appctx, LOG_INFO, trash.area);
562}
563
Willy Tarreau68680bb2019-10-23 17:23:25 +0200564/* parse a "debug dev stream" command */
565/*
566 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
567 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
568 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
569 * [sif.s[=<state>]] [sib.s[=<state>]]
570 */
571static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
572{
573 struct stream *s = si_strm(appctx->owner);
574 int arg;
575 void *ptr;
576 int size;
577 const char *word, *end;
578 struct ist name;
579 char *msg = NULL;
580 char *endarg;
581 unsigned long long old, new;
582
583 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
584 return 1;
585
586 ptr = NULL; size = 0;
587
588 if (!*args[3]) {
589 return cli_err(appctx,
590 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
591 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
592 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
593 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
594 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
595 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
596 );
597 }
598
Willy Tarreau9b013702019-10-24 18:18:02 +0200599 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200600 for (arg = 3; *args[arg]; arg++) {
601 old = 0;
602 end = word = args[arg];
603 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
604 end++;
605 name = ist2(word, end - word);
606 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200607 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200608 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200609 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200610 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200611 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200612 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200613 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200614 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200615 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200616 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200617 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200618 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200619 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200620 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200621 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200622 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200623 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200624 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200625 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200626 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200627 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200628 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200629 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200630 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200631 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200632 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200633 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200634 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200635 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200636 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200637 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200638 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
639 continue;
640 } else
641 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
642
643 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200644 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200645 if (size == 8)
646 old = read_u64(ptr);
647 else if (size == 4)
648 old = read_u32(ptr);
649 else if (size == 2)
650 old = read_u16(ptr);
651 else
652 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200653 } else {
654 memprintf(&msg,
655 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
656 msg ? msg : "", ptr, (int)(end - word), word);
657 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200658 }
659
660 /* parse the new value . */
661 new = strtoll(end + 1, &endarg, 0);
662 if (end[1] && *endarg) {
663 if (strcmp(end + 1, "now") == 0)
664 new = now_ms;
665 else {
666 memprintf(&msg,
667 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
668 msg ? msg : "", end + 1, (int)(end - word), word);
669 continue;
670 }
671 }
672
673 switch (*end) {
674 case '\0': /* show */
675 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
676 new = old; // do not change the value
677 break;
678
679 case '=': /* set */
680 break;
681
682 case '^': /* XOR */
683 new = old ^ new;
684 break;
685
686 case '+': /* OR */
687 new = old | new;
688 break;
689
690 case '-': /* AND NOT */
691 new = old & ~new;
692 break;
693
694 default:
695 break;
696 }
697
698 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200699 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200700 if (size == 8)
701 write_u64(ptr, new);
702 else if (size == 4)
703 write_u32(ptr, new);
704 else if (size == 2)
705 write_u16(ptr, new);
706 else
707 *(uint8_t *)ptr = new;
708 }
709 }
710
711 if (msg && *msg)
712 return cli_dynmsg(appctx, LOG_INFO, msg);
713 return 1;
714}
715
Willy Tarreaua5a44792020-11-29 17:12:15 +0100716static struct task *debug_task_handler(struct task *t, void *ctx, unsigned short state)
717{
718 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
719 unsigned long inter = tctx[1];
720 unsigned long rnd;
721
722 t->expire = tick_add(now_ms, inter);
723
724 /* half of the calls will wake up another entry */
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100725 rnd = debug_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100726 if (rnd & 1) {
727 rnd >>= 1;
728 rnd %= tctx[0];
729 rnd = tctx[rnd + 2];
730
731 if (rnd & 1)
732 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
733 else
734 tasklet_wakeup((struct tasklet *)rnd);
735 }
736 return t;
737}
738
739static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned short state)
740{
741 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
742 unsigned long rnd;
743 int i;
744
745 /* wake up two random entries */
746 for (i = 0; i < 2; i++) {
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100747 rnd = debug_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100748 rnd = tctx[rnd + 2];
749
750 if (rnd & 1)
751 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
752 else
753 tasklet_wakeup((struct tasklet *)rnd);
754 }
755 return t;
756}
757
758/* parse a "debug dev sched" command
759 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
760 */
761static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
762{
763 int arg;
764 void *ptr;
765 int size;
766 const char *word, *end;
767 struct ist name;
768 char *msg = NULL;
769 char *endarg;
770 unsigned long long new;
771 unsigned long count = 0;
772 unsigned long thrid = 0;
773 unsigned int inter = 0;
774 unsigned long mask, tmask;
775 unsigned long i;
776 int mode = 0; // 0 = tasklet; 1 = task
777 int single = 0;
778 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
779
780 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
781 return 1;
782
783 ptr = NULL; size = 0;
784 mask = all_threads_mask;
785
786 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
787 return cli_err(appctx,
788 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
789 " <obj> = {count | mask | inter | single }\n"
790 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
791 );
792 }
793
794 mode = strcmp(args[3], "task") == 0;
795
796 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
797 for (arg = 4; *args[arg]; arg++) {
798 end = word = args[arg];
799 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
800 end++;
801 name = ist2(word, end - word);
802 if (isteq(name, ist("count"))) {
803 ptr = &count; size = sizeof(count);
804 } else if (isteq(name, ist("mask"))) {
805 ptr = &mask; size = sizeof(mask);
806 } else if (isteq(name, ist("tid"))) {
807 ptr = &thrid; size = sizeof(thrid);
808 } else if (isteq(name, ist("inter"))) {
809 ptr = &inter; size = sizeof(inter);
810 } else if (isteq(name, ist("single"))) {
811 ptr = &single; size = sizeof(single);
812 } else
813 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
814
815 /* parse the new value . */
816 new = strtoll(end + 1, &endarg, 0);
817 if (end[1] && *endarg) {
818 memprintf(&msg,
819 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
820 msg ? msg : "", end + 1, (int)(end - word), word);
821 continue;
822 }
823
824 /* write the new value */
825 if (size == 8)
826 write_u64(ptr, new);
827 else if (size == 4)
828 write_u32(ptr, new);
829 else if (size == 2)
830 write_u16(ptr, new);
831 else
832 *(uint8_t *)ptr = new;
833 }
834
835 tctx = calloc(sizeof(*tctx), count + 2);
836 if (!tctx)
837 goto fail;
838
839 tctx[0] = (unsigned long)count;
840 tctx[1] = (unsigned long)inter;
841
842 mask &= all_threads_mask;
843 if (!mask)
844 mask = tid_bit;
845
846 tmask = 0;
847 for (i = 0; i < count; i++) {
848 if (single || mode == 0) {
849 /* look for next bit matching a bit in mask or loop back to zero */
850 for (tmask <<= 1; !(mask & tmask); ) {
851 if (!(mask & -tmask))
852 tmask = 1;
853 else
854 tmask <<= 1;
855 }
856 } else {
857 /* multi-threaded task */
858 tmask = mask;
859 }
860
861 /* now, if poly or mask was set, tmask corresponds to the
862 * valid thread mask to use, otherwise it remains zero.
863 */
864 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
865 if (mode == 0) {
866 struct tasklet *tl = tasklet_new();
867
868 if (!tl)
869 goto fail;
870
871 if (tmask)
872 tl->tid = my_ffsl(tmask) - 1;
873 tl->process = debug_tasklet_handler;
874 tl->context = tctx;
875 tctx[i + 2] = (unsigned long)tl;
876 } else {
877 struct task *task = task_new(tmask ? tmask : tid_bit);
878
879 if (!task)
880 goto fail;
881
882 task->process = debug_task_handler;
883 task->context = tctx;
884 tctx[i + 2] = (unsigned long)task + 1;
885 }
886 }
887
888 /* start the tasks and tasklets */
889 for (i = 0; i < count; i++) {
890 unsigned long ctx = tctx[i + 2];
891
892 if (ctx & 1)
893 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
894 else
895 tasklet_wakeup((struct tasklet *)ctx);
896 }
897
898 if (msg && *msg)
899 return cli_dynmsg(appctx, LOG_INFO, msg);
900 return 1;
901
902 fail:
903 /* free partially allocated entries */
904 for (i = 0; tctx && i < count; i++) {
905 unsigned long ctx = tctx[i + 2];
906
907 if (!ctx)
908 break;
909
910 if (ctx & 1)
911 task_destroy((struct task *)(ctx - 1));
912 else
913 tasklet_free((struct tasklet *)ctx);
914 }
915
916 free(tctx);
917 return cli_err(appctx, "Not enough memory");
918}
919
Willy Tarreaua6026a02020-07-02 09:14:48 +0200920#if defined(DEBUG_MEM_STATS)
921/* CLI parser for the "debug dev memstats" command */
922static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
923{
924 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
925 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
926
927 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
928 return 1;
929
930 if (strcmp(args[3], "reset") == 0) {
931 struct mem_stats *ptr;
932
933 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
934 return 1;
935
936 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
937 _HA_ATOMIC_STORE(&ptr->calls, 0);
938 _HA_ATOMIC_STORE(&ptr->size, 0);
939 }
940 return 1;
941 }
942
943 if (strcmp(args[3], "all") == 0)
944 appctx->ctx.cli.i0 = 1;
945
946 /* otherwise proceed with the dump from p0 to p1 */
947 appctx->ctx.cli.p0 = &__start_mem_stats;
948 appctx->ctx.cli.p1 = &__stop_mem_stats;
949 return 0;
950}
951
952/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
953 * structs referenced by pointers located between p0 and p1. Dumps all entries
954 * if i0 > 0, otherwise only non-zero calls.
955 */
956static int debug_iohandler_memstats(struct appctx *appctx)
957{
958 struct stream_interface *si = appctx->owner;
959 struct mem_stats *ptr = appctx->ctx.cli.p0;
960 int ret = 1;
961
962 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
963 goto end;
964
965 chunk_reset(&trash);
966
967 /* we have two inner loops here, one for the proxy, the other one for
968 * the buffer.
969 */
970 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
971 const char *type;
972 const char *name;
973 const char *p;
974
975 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
976 continue;
977
978 /* basename only */
979 for (p = name = ptr->file; *p; p++) {
980 if (*p == '/')
981 name = p + 1;
982 }
983
984 switch (ptr->type) {
985 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
986 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
987 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
988 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
989 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
990 default: type = "UNSET"; break;
991 }
992
993 //chunk_printf(&trash,
994 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
995 // name, ptr->line, type,
996 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
997 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
998
999 chunk_printf(&trash, "%s:%d", name, ptr->line);
1000 while (trash.data < 25)
1001 trash.area[trash.data++] = ' ';
1002 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
1003 type,
1004 (unsigned long)ptr->size, (unsigned long)ptr->calls,
1005 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
1006
1007 if (ci_putchk(si_ic(si), &trash) == -1) {
1008 si_rx_room_blk(si);
1009 appctx->ctx.cli.p0 = ptr;
1010 ret = 0;
1011 break;
1012 }
1013 }
1014
1015 end:
1016 return ret;
1017}
1018
1019#endif
1020
Willy Tarreauc7091d82019-05-17 10:08:49 +02001021#ifndef USE_THREAD_DUMP
1022
1023/* This function dumps all threads' state to the trash. This version is the
1024 * most basic one, which doesn't inspect other threads.
1025 */
1026void ha_thread_dump_all_to_trash()
1027{
1028 unsigned int thr;
1029
1030 for (thr = 0; thr < global.nbthread; thr++)
1031 ha_thread_dump(&trash, thr, tid);
1032}
1033
1034#else /* below USE_THREAD_DUMP is set */
1035
Willy Tarreauc7091d82019-05-17 10:08:49 +02001036/* ID of the thread requesting the dump */
1037static unsigned int thread_dump_tid;
1038
1039/* points to the buffer where the dump functions should write. It must
1040 * have already been initialized by the requester. Nothing is done if
1041 * it's NULL.
1042 */
1043struct buffer *thread_dump_buffer = NULL;
1044
1045void ha_thread_dump_all_to_trash()
1046{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001047 unsigned long old;
1048
1049 while (1) {
1050 old = 0;
1051 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1052 break;
1053 ha_thread_relax();
1054 }
1055
1056 thread_dump_buffer = &trash;
1057 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001058 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001059}
1060
1061/* handles DEBUGSIG to dump the state of the thread it's working on */
1062void debug_handler(int sig, siginfo_t *si, void *arg)
1063{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001064 /* first, let's check it's really for us and that we didn't just get
1065 * a spurious DEBUGSIG.
1066 */
1067 if (!(threads_to_dump & tid_bit))
1068 return;
1069
Willy Tarreauc7091d82019-05-17 10:08:49 +02001070 /* There are 4 phases in the dump process:
1071 * 1- wait for our turn, i.e. when all lower bits are gone.
1072 * 2- perform the action if our bit is set
1073 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001074 * the last one and have to put them all as a signal
1075 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001076 */
1077
1078 /* wait for all previous threads to finish first */
1079 while (threads_to_dump & (tid_bit - 1))
1080 ha_thread_relax();
1081
1082 /* dump if needed */
1083 if (threads_to_dump & tid_bit) {
1084 if (thread_dump_buffer)
1085 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1086 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1087 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001088 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001089 thread_dump_buffer = NULL;
1090 }
1091 else
1092 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1093 }
1094
1095 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001096 * bits again to broadcast the leaving condition so we'll see ourselves
1097 * present again. This way the threads_to_dump variable never passes to
1098 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001099 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001100 while (!(threads_to_dump & tid_bit))
1101 ha_thread_relax();
1102 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001103
1104 /* mark the current thread as stuck to detect it upon next invocation
1105 * if it didn't move.
1106 */
1107 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1108 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001109}
1110
1111static int init_debug_per_thread()
1112{
1113 sigset_t set;
1114
1115 /* unblock the DEBUGSIG signal we intend to use */
1116 sigemptyset(&set);
1117 sigaddset(&set, DEBUGSIG);
1118 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1119 return 1;
1120}
1121
1122static int init_debug()
1123{
1124 struct sigaction sa;
Willy Tarreau2f1227e2021-01-22 12:12:29 +01001125 void *callers[1];
Willy Tarreauc7091d82019-05-17 10:08:49 +02001126
Willy Tarreau0214b452020-03-04 06:01:40 +01001127 /* calling backtrace() will access libgcc at runtime. We don't want to
1128 * do it after the chroot, so let's perform a first call to have it
1129 * ready in memory for later use.
1130 */
Willy Tarreau13faf162020-03-04 07:44:06 +01001131 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauc7091d82019-05-17 10:08:49 +02001132 sa.sa_handler = NULL;
1133 sa.sa_sigaction = debug_handler;
1134 sigemptyset(&sa.sa_mask);
1135 sa.sa_flags = SA_SIGINFO;
1136 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001137 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001138}
1139
1140REGISTER_POST_CHECK(init_debug);
1141REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1142
1143#endif /* USE_THREAD_DUMP */
1144
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001145/* register cli keywords */
1146static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +02001147 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1148 {{ "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 +02001149#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +02001150 {{ "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 +02001151#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001152 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1153 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1154 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1155 {{ "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 +02001156#if defined(DEBUG_MEM_STATS)
1157 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT },
1158#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001159 {{ "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 +01001160 {{ "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 +02001161 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1162 {{ "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 +01001163 {{ "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 +02001164 {{ "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 +02001165 {{},}
1166}};
1167
1168INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);