blob: 460ee4d3c50f4a87498e0a2f7bcc688e34718884 [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 Tarreau5cf64dd2019-05-17 10:36:08 +020055/* Dumps to the buffer some known information for the desired thread, and
56 * optionally extra info for the current thread. The dump will be appended to
57 * the buffer, so the caller is responsible for preliminary initializing it.
58 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020059 * in front of the calling thread's line (usually it's tid). Any stuck thread
60 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020061 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020062void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020063{
64 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010065 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
66 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
67 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020068
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020069 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +020070 "%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 +020071 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020072 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +020073 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020074 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020075 !!(global_tasks_mask & thr_bit),
76 !eb_is_empty(&task_per_thread[thr].timers),
77 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010078 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
79 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
80 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
81 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020082 task_per_thread[thr].task_list_size,
83 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020084 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020085 !!(task_profiling_mask & thr_bit));
86
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020087 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088 " harmless=%d wantrdv=%d",
89 !!(threads_harmless_mask & thr_bit),
90 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020091
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020092 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020093 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020094
95 /* this is the end of what we can dump from outside the thread */
96
97 if (thr != tid)
98 return;
99
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200100 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200101 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100102
103#ifdef USE_BACKTRACE
104 if (stuck) {
105 /* We only emit the backtrace for stuck threads in order not to
106 * waste precious output buffer space with non-interesting data.
107 */
108 struct buffer bak;
109 void *callers[100];
110 int j, nptrs;
Willy Tarreau0c439d82020-07-05 20:26:04 +0200111 const void *addr;
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100112 int dump = 0;
113
Willy Tarreau13faf162020-03-04 07:44:06 +0100114 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100115
116 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO)
117 would produce similar output to the following: */
118
119 if (nptrs)
Willy Tarreaucdd80742020-03-04 07:38:23 +0100120 chunk_appendf(buf, " call trace(%d):\n", nptrs);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100121
Willy Tarreaua91b7942020-03-04 07:39:32 +0100122 for (j = 0; j < nptrs || dump < 2; j++) {
123 if (j == nptrs && !dump) {
124 /* we failed to spot the starting point of the
125 * dump, let's start over dumping everything we
126 * have.
127 */
128 dump = 2;
129 j = 0;
130 }
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100131 bak = *buf;
132 dump_addr_and_bytes(buf, " | ", callers[j], 8);
133 addr = resolve_sym_name(buf, ": ", callers[j]);
134 if (dump == 0) {
135 /* dump not started, will start *after*
136 * ha_thread_dump_all_to_trash and ha_panic
137 */
138 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic)
139 dump = 1;
140 *buf = bak;
141 continue;
142 }
143
144 if (dump == 1) {
145 /* starting */
146 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) {
147 *buf = bak;
148 continue;
149 }
150 dump = 2;
151 }
152
153 if (dump == 2) {
154 /* dumping */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200155 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100156 dump = 3;
157 *buf = bak;
158 break;
159 }
160 }
161 /* OK, line dumped */
162 chunk_appendf(buf, "\n");
163 }
164 }
165#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200166}
167
168
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200169/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200171 * with <pfx>. The buffer is only appended and the first output starts by the
172 * pointer itself. The caller is responsible for making sure the task is not
173 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200174 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200175void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200177 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200178 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200179 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200180
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200181 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200182 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200183 return;
184 }
185
Willy Tarreau20db9112019-05-17 14:14:35 +0200186 if (TASK_IS_TASKLET(task))
187 chunk_appendf(buf,
188 "%p (tasklet) calls=%u\n",
189 task,
190 task->calls);
191 else
192 chunk_appendf(buf,
193 "%p (task) calls=%u last=%llu%s\n",
194 task,
195 task->calls,
196 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
197 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200198
Willy Tarreau2e89b092020-03-03 17:13:02 +0100199 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
200 resolve_sym_name(buf, NULL, task->process);
201 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200202
Willy Tarreaua512b022019-08-21 14:12:19 +0200203 if (task->process == task_run_applet && (appctx = task->context))
204 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
205 else
206 chunk_appendf(buf, "\n");
207
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200208 if (task->process == process_stream && task->context)
209 s = (struct stream *)task->context;
210 else if (task->process == task_run_applet && task->context)
211 s = si_strm(((struct appctx *)task->context)->owner);
212 else if (task->process == si_cs_io_cb && task->context)
213 s = si_strm((struct stream_interface *)task->context);
214
215 if (s)
216 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200217
218#ifdef USE_LUA
219 hlua = NULL;
220 if (s && (hlua = s->hlua)) {
221 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
222 }
223 else if (task->process == hlua_process_task && (hlua = task->context)) {
224 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
225 }
226 else if (task->process == task_run_applet && (appctx = task->context) &&
227 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
228 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
229 }
230 else if (task->process == task_run_applet && (appctx = task->context) &&
231 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
232 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
233 }
234
Christopher Faulet471425f2020-07-24 19:08:05 +0200235 if (hlua && hlua->T) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200236 luaL_traceback(hlua->T, hlua->T, NULL, 0);
237 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
238 b_putchr(buf, '\n');
239 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200240 else
241 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200242#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200243}
244
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200245/* This function dumps all profiling settings. It returns 0 if the output
246 * buffer is full and it needs to be called again, otherwise non-zero.
247 */
248static int cli_io_handler_show_threads(struct appctx *appctx)
249{
250 struct stream_interface *si = appctx->owner;
251 int thr;
252
253 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
254 return 1;
255
256 if (appctx->st0)
257 thr = appctx->st1;
258 else
259 thr = 0;
260
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200261 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200262 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200263
264 if (ci_putchk(si_ic(si), &trash) == -1) {
265 /* failed, try again */
266 si_rx_room_blk(si);
267 appctx->st1 = thr;
268 return 0;
269 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200270 return 1;
271}
272
Willy Tarreau56131ca2019-05-20 13:48:29 +0200273/* dumps a state of all threads into the trash and on fd #2, then aborts. */
274void ha_panic()
275{
276 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200277 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200278 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100279 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200280 for (;;)
281 abort();
282}
283
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200284/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
285static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
286{
287 int code = atoi(args[3]);
288
289 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
290 return 1;
291
Willy Tarreau9b013702019-10-24 18:18:02 +0200292 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200293 exit(code);
294 return 1;
295}
296
297/* parse a "debug dev close" command. It always returns 1. */
298static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
299{
300 int fd;
301
302 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
303 return 1;
304
Willy Tarreau9d008692019-08-09 11:21:01 +0200305 if (!*args[3])
306 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200307
308 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200309 if (fd < 0 || fd >= global.maxsock)
310 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200311
Willy Tarreau9d008692019-08-09 11:21:01 +0200312 if (!fdtab[fd].owner)
313 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200314
Willy Tarreau9b013702019-10-24 18:18:02 +0200315 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200316 fd_delete(fd);
317 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200318}
319
320/* parse a "debug dev delay" command. It always returns 1. */
321static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
322{
323 int delay = atoi(args[3]);
324
325 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
326 return 1;
327
Willy Tarreau9b013702019-10-24 18:18:02 +0200328 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200329 usleep((long)delay * 1000);
330 return 1;
331}
332
333/* parse a "debug dev log" command. It always returns 1. */
334static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
335{
336 int arg;
337
338 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
339 return 1;
340
Willy Tarreau9b013702019-10-24 18:18:02 +0200341 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200342 chunk_reset(&trash);
343 for (arg = 3; *args[arg]; arg++) {
344 if (arg > 3)
345 chunk_strcat(&trash, " ");
346 chunk_strcat(&trash, args[arg]);
347 }
348
349 send_log(NULL, LOG_INFO, "%s\n", trash.area);
350 return 1;
351}
352
353/* parse a "debug dev loop" command. It always returns 1. */
354static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
355{
356 struct timeval deadline, curr;
357 int loop = atoi(args[3]);
358
359 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
360 return 1;
361
Willy Tarreau9b013702019-10-24 18:18:02 +0200362 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363 gettimeofday(&curr, NULL);
364 tv_ms_add(&deadline, &curr, loop);
365
366 while (tv_ms_cmp(&curr, &deadline) < 0)
367 gettimeofday(&curr, NULL);
368
369 return 1;
370}
371
372/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
373static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
374{
375 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
376 return 1;
377
Willy Tarreau9b013702019-10-24 18:18:02 +0200378 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379 ha_panic();
380 return 1;
381}
382
383/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200384#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200385static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
386{
Willy Tarreau368bff42019-12-06 17:18:28 +0100387 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200388 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100389 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200390
391 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
392 return 1;
393
Willy Tarreau9b013702019-10-24 18:18:02 +0200394 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200395 chunk_reset(&trash);
396 for (arg = 3; *args[arg]; arg++) {
397 if (arg > 3)
398 chunk_strcat(&trash, " ");
399 chunk_strcat(&trash, args[arg]);
400 }
401
Willy Tarreau368bff42019-12-06 17:18:28 +0100402 thread_isolate();
403 if (pipe(pipefd) < 0)
404 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200405
Willy Tarreau368bff42019-12-06 17:18:28 +0100406 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
407 goto fail_fcntl;
408
409 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
410 goto fail_fcntl;
411
412 pid = fork();
413
414 if (pid < 0)
415 goto fail_fork;
416 else if (pid == 0) {
417 /* child */
418 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
419
420 close(0);
421 dup2(pipefd[1], 1);
422 dup2(pipefd[1], 2);
423
424 cmd[2] = trash.area;
425 execvp(cmd[0], cmd);
426 printf("execvp() failed\n");
427 exit(1);
428 }
429
430 /* parent */
431 thread_release();
432 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200433 chunk_reset(&trash);
434 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100435 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
436 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200437 break;
438 trash.data += ret;
439 if (trash.data + 20 == trash.size) {
440 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
441 break;
442 }
443 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100444 close(pipefd[0]);
445 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200446 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200447 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100448
449 fail_fork:
450 fail_fcntl:
451 close(pipefd[0]);
452 close(pipefd[1]);
453 fail_pipe:
454 thread_release();
455 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200456}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200457#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200458
459/* parse a "debug dev hex" command. It always returns 1. */
460static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
461{
462 unsigned long start, len;
463
464 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
465 return 1;
466
Willy Tarreau9d008692019-08-09 11:21:01 +0200467 if (!*args[3])
468 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200469
470 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200471 if (!start)
472 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200473
Willy Tarreau9b013702019-10-24 18:18:02 +0200474 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
475
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200476 /* by default, dump ~128 till next block of 16 */
477 len = strtoul(args[4], NULL, 0);
478 if (!len)
479 len = ((start + 128) & -16) - start;
480
481 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200482 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200483 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200484 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200485}
486
487/* parse a "debug dev tkill" command. It always returns 1. */
488static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
489{
490 int thr = 0;
491 int sig = SIGABRT;
492
493 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
494 return 1;
495
496 if (*args[3])
497 thr = atoi(args[3]);
498
Willy Tarreau9d008692019-08-09 11:21:01 +0200499 if (thr < 0 || thr > global.nbthread)
500 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200501
502 if (*args[4])
503 sig = atoi(args[4]);
504
Willy Tarreau9b013702019-10-24 18:18:02 +0200505 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200506 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200507 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200508 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200509 raise(sig);
510 return 1;
511}
512
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100513/* parse a "debug dev write" command. It always returns 1. */
514static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
515{
516 unsigned long len;
517
518 if (!*args[3])
519 return cli_err(appctx, "Missing output size.\n");
520
521 len = strtoul(args[3], NULL, 0);
522 if (len >= trash.size)
523 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
524
525 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
526
527 chunk_reset(&trash);
528 trash.data = len;
529 memset(trash.area, '.', trash.data);
530 trash.area[trash.data] = 0;
531 for (len = 64; len < trash.data; len += 64)
532 trash.area[len] = '\n';
533 return cli_msg(appctx, LOG_INFO, trash.area);
534}
535
Willy Tarreau68680bb2019-10-23 17:23:25 +0200536/* parse a "debug dev stream" command */
537/*
538 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
539 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
540 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
541 * [sif.s[=<state>]] [sib.s[=<state>]]
542 */
543static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
544{
545 struct stream *s = si_strm(appctx->owner);
546 int arg;
547 void *ptr;
548 int size;
549 const char *word, *end;
550 struct ist name;
551 char *msg = NULL;
552 char *endarg;
553 unsigned long long old, new;
554
555 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
556 return 1;
557
558 ptr = NULL; size = 0;
559
560 if (!*args[3]) {
561 return cli_err(appctx,
562 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
563 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
564 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
565 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
566 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
567 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
568 );
569 }
570
Willy Tarreau9b013702019-10-24 18:18:02 +0200571 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200572 for (arg = 3; *args[arg]; arg++) {
573 old = 0;
574 end = word = args[arg];
575 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
576 end++;
577 name = ist2(word, end - word);
578 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200579 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200580 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200581 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200582 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200583 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200584 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200585 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200586 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200587 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200588 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200589 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200590 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200591 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200592 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200593 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200594 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200595 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200596 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200597 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200598 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200599 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200600 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200601 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200602 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200603 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200604 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200605 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200606 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200607 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200608 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200609 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200610 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
611 continue;
612 } else
613 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
614
615 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200616 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200617 if (size == 8)
618 old = read_u64(ptr);
619 else if (size == 4)
620 old = read_u32(ptr);
621 else if (size == 2)
622 old = read_u16(ptr);
623 else
624 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200625 } else {
626 memprintf(&msg,
627 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
628 msg ? msg : "", ptr, (int)(end - word), word);
629 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200630 }
631
632 /* parse the new value . */
633 new = strtoll(end + 1, &endarg, 0);
634 if (end[1] && *endarg) {
635 if (strcmp(end + 1, "now") == 0)
636 new = now_ms;
637 else {
638 memprintf(&msg,
639 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
640 msg ? msg : "", end + 1, (int)(end - word), word);
641 continue;
642 }
643 }
644
645 switch (*end) {
646 case '\0': /* show */
647 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
648 new = old; // do not change the value
649 break;
650
651 case '=': /* set */
652 break;
653
654 case '^': /* XOR */
655 new = old ^ new;
656 break;
657
658 case '+': /* OR */
659 new = old | new;
660 break;
661
662 case '-': /* AND NOT */
663 new = old & ~new;
664 break;
665
666 default:
667 break;
668 }
669
670 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200671 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200672 if (size == 8)
673 write_u64(ptr, new);
674 else if (size == 4)
675 write_u32(ptr, new);
676 else if (size == 2)
677 write_u16(ptr, new);
678 else
679 *(uint8_t *)ptr = new;
680 }
681 }
682
683 if (msg && *msg)
684 return cli_dynmsg(appctx, LOG_INFO, msg);
685 return 1;
686}
687
Willy Tarreaua5a44792020-11-29 17:12:15 +0100688static struct task *debug_task_handler(struct task *t, void *ctx, unsigned short state)
689{
690 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
691 unsigned long inter = tctx[1];
692 unsigned long rnd;
693
694 t->expire = tick_add(now_ms, inter);
695
696 /* half of the calls will wake up another entry */
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100697 rnd = debug_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100698 if (rnd & 1) {
699 rnd >>= 1;
700 rnd %= tctx[0];
701 rnd = tctx[rnd + 2];
702
703 if (rnd & 1)
704 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
705 else
706 tasklet_wakeup((struct tasklet *)rnd);
707 }
708 return t;
709}
710
711static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned short state)
712{
713 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
714 unsigned long rnd;
715 int i;
716
717 /* wake up two random entries */
718 for (i = 0; i < 2; i++) {
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100719 rnd = debug_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100720 rnd = tctx[rnd + 2];
721
722 if (rnd & 1)
723 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
724 else
725 tasklet_wakeup((struct tasklet *)rnd);
726 }
727 return t;
728}
729
730/* parse a "debug dev sched" command
731 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
732 */
733static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
734{
735 int arg;
736 void *ptr;
737 int size;
738 const char *word, *end;
739 struct ist name;
740 char *msg = NULL;
741 char *endarg;
742 unsigned long long new;
743 unsigned long count = 0;
744 unsigned long thrid = 0;
745 unsigned int inter = 0;
746 unsigned long mask, tmask;
747 unsigned long i;
748 int mode = 0; // 0 = tasklet; 1 = task
749 int single = 0;
750 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
751
752 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
753 return 1;
754
755 ptr = NULL; size = 0;
756 mask = all_threads_mask;
757
758 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
759 return cli_err(appctx,
760 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
761 " <obj> = {count | mask | inter | single }\n"
762 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
763 );
764 }
765
766 mode = strcmp(args[3], "task") == 0;
767
768 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
769 for (arg = 4; *args[arg]; arg++) {
770 end = word = args[arg];
771 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
772 end++;
773 name = ist2(word, end - word);
774 if (isteq(name, ist("count"))) {
775 ptr = &count; size = sizeof(count);
776 } else if (isteq(name, ist("mask"))) {
777 ptr = &mask; size = sizeof(mask);
778 } else if (isteq(name, ist("tid"))) {
779 ptr = &thrid; size = sizeof(thrid);
780 } else if (isteq(name, ist("inter"))) {
781 ptr = &inter; size = sizeof(inter);
782 } else if (isteq(name, ist("single"))) {
783 ptr = &single; size = sizeof(single);
784 } else
785 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
786
787 /* parse the new value . */
788 new = strtoll(end + 1, &endarg, 0);
789 if (end[1] && *endarg) {
790 memprintf(&msg,
791 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
792 msg ? msg : "", end + 1, (int)(end - word), word);
793 continue;
794 }
795
796 /* write the new value */
797 if (size == 8)
798 write_u64(ptr, new);
799 else if (size == 4)
800 write_u32(ptr, new);
801 else if (size == 2)
802 write_u16(ptr, new);
803 else
804 *(uint8_t *)ptr = new;
805 }
806
807 tctx = calloc(sizeof(*tctx), count + 2);
808 if (!tctx)
809 goto fail;
810
811 tctx[0] = (unsigned long)count;
812 tctx[1] = (unsigned long)inter;
813
814 mask &= all_threads_mask;
815 if (!mask)
816 mask = tid_bit;
817
818 tmask = 0;
819 for (i = 0; i < count; i++) {
820 if (single || mode == 0) {
821 /* look for next bit matching a bit in mask or loop back to zero */
822 for (tmask <<= 1; !(mask & tmask); ) {
823 if (!(mask & -tmask))
824 tmask = 1;
825 else
826 tmask <<= 1;
827 }
828 } else {
829 /* multi-threaded task */
830 tmask = mask;
831 }
832
833 /* now, if poly or mask was set, tmask corresponds to the
834 * valid thread mask to use, otherwise it remains zero.
835 */
836 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
837 if (mode == 0) {
838 struct tasklet *tl = tasklet_new();
839
840 if (!tl)
841 goto fail;
842
843 if (tmask)
844 tl->tid = my_ffsl(tmask) - 1;
845 tl->process = debug_tasklet_handler;
846 tl->context = tctx;
847 tctx[i + 2] = (unsigned long)tl;
848 } else {
849 struct task *task = task_new(tmask ? tmask : tid_bit);
850
851 if (!task)
852 goto fail;
853
854 task->process = debug_task_handler;
855 task->context = tctx;
856 tctx[i + 2] = (unsigned long)task + 1;
857 }
858 }
859
860 /* start the tasks and tasklets */
861 for (i = 0; i < count; i++) {
862 unsigned long ctx = tctx[i + 2];
863
864 if (ctx & 1)
865 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
866 else
867 tasklet_wakeup((struct tasklet *)ctx);
868 }
869
870 if (msg && *msg)
871 return cli_dynmsg(appctx, LOG_INFO, msg);
872 return 1;
873
874 fail:
875 /* free partially allocated entries */
876 for (i = 0; tctx && i < count; i++) {
877 unsigned long ctx = tctx[i + 2];
878
879 if (!ctx)
880 break;
881
882 if (ctx & 1)
883 task_destroy((struct task *)(ctx - 1));
884 else
885 tasklet_free((struct tasklet *)ctx);
886 }
887
888 free(tctx);
889 return cli_err(appctx, "Not enough memory");
890}
891
Willy Tarreaua6026a02020-07-02 09:14:48 +0200892#if defined(DEBUG_MEM_STATS)
893/* CLI parser for the "debug dev memstats" command */
894static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
895{
896 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
897 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
898
899 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
900 return 1;
901
902 if (strcmp(args[3], "reset") == 0) {
903 struct mem_stats *ptr;
904
905 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
906 return 1;
907
908 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
909 _HA_ATOMIC_STORE(&ptr->calls, 0);
910 _HA_ATOMIC_STORE(&ptr->size, 0);
911 }
912 return 1;
913 }
914
915 if (strcmp(args[3], "all") == 0)
916 appctx->ctx.cli.i0 = 1;
917
918 /* otherwise proceed with the dump from p0 to p1 */
919 appctx->ctx.cli.p0 = &__start_mem_stats;
920 appctx->ctx.cli.p1 = &__stop_mem_stats;
921 return 0;
922}
923
924/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
925 * structs referenced by pointers located between p0 and p1. Dumps all entries
926 * if i0 > 0, otherwise only non-zero calls.
927 */
928static int debug_iohandler_memstats(struct appctx *appctx)
929{
930 struct stream_interface *si = appctx->owner;
931 struct mem_stats *ptr = appctx->ctx.cli.p0;
932 int ret = 1;
933
934 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
935 goto end;
936
937 chunk_reset(&trash);
938
939 /* we have two inner loops here, one for the proxy, the other one for
940 * the buffer.
941 */
942 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
943 const char *type;
944 const char *name;
945 const char *p;
946
947 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
948 continue;
949
950 /* basename only */
951 for (p = name = ptr->file; *p; p++) {
952 if (*p == '/')
953 name = p + 1;
954 }
955
956 switch (ptr->type) {
957 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
958 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
959 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
960 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
961 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
962 default: type = "UNSET"; break;
963 }
964
965 //chunk_printf(&trash,
966 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
967 // name, ptr->line, type,
968 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
969 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
970
971 chunk_printf(&trash, "%s:%d", name, ptr->line);
972 while (trash.data < 25)
973 trash.area[trash.data++] = ' ';
974 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
975 type,
976 (unsigned long)ptr->size, (unsigned long)ptr->calls,
977 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
978
979 if (ci_putchk(si_ic(si), &trash) == -1) {
980 si_rx_room_blk(si);
981 appctx->ctx.cli.p0 = ptr;
982 ret = 0;
983 break;
984 }
985 }
986
987 end:
988 return ret;
989}
990
991#endif
992
Willy Tarreauc7091d82019-05-17 10:08:49 +0200993#ifndef USE_THREAD_DUMP
994
995/* This function dumps all threads' state to the trash. This version is the
996 * most basic one, which doesn't inspect other threads.
997 */
998void ha_thread_dump_all_to_trash()
999{
1000 unsigned int thr;
1001
1002 for (thr = 0; thr < global.nbthread; thr++)
1003 ha_thread_dump(&trash, thr, tid);
1004}
1005
1006#else /* below USE_THREAD_DUMP is set */
1007
Willy Tarreauc7091d82019-05-17 10:08:49 +02001008/* ID of the thread requesting the dump */
1009static unsigned int thread_dump_tid;
1010
1011/* points to the buffer where the dump functions should write. It must
1012 * have already been initialized by the requester. Nothing is done if
1013 * it's NULL.
1014 */
1015struct buffer *thread_dump_buffer = NULL;
1016
1017void ha_thread_dump_all_to_trash()
1018{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001019 unsigned long old;
1020
1021 while (1) {
1022 old = 0;
1023 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1024 break;
1025 ha_thread_relax();
1026 }
1027
1028 thread_dump_buffer = &trash;
1029 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001030 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001031}
1032
1033/* handles DEBUGSIG to dump the state of the thread it's working on */
1034void debug_handler(int sig, siginfo_t *si, void *arg)
1035{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001036 /* first, let's check it's really for us and that we didn't just get
1037 * a spurious DEBUGSIG.
1038 */
1039 if (!(threads_to_dump & tid_bit))
1040 return;
1041
Willy Tarreauc7091d82019-05-17 10:08:49 +02001042 /* There are 4 phases in the dump process:
1043 * 1- wait for our turn, i.e. when all lower bits are gone.
1044 * 2- perform the action if our bit is set
1045 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001046 * the last one and have to put them all as a signal
1047 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001048 */
1049
1050 /* wait for all previous threads to finish first */
1051 while (threads_to_dump & (tid_bit - 1))
1052 ha_thread_relax();
1053
1054 /* dump if needed */
1055 if (threads_to_dump & tid_bit) {
1056 if (thread_dump_buffer)
1057 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1058 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1059 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001060 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001061 thread_dump_buffer = NULL;
1062 }
1063 else
1064 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1065 }
1066
1067 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001068 * bits again to broadcast the leaving condition so we'll see ourselves
1069 * present again. This way the threads_to_dump variable never passes to
1070 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001071 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001072 while (!(threads_to_dump & tid_bit))
1073 ha_thread_relax();
1074 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001075
1076 /* mark the current thread as stuck to detect it upon next invocation
1077 * if it didn't move.
1078 */
1079 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1080 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001081}
1082
1083static int init_debug_per_thread()
1084{
1085 sigset_t set;
1086
1087 /* unblock the DEBUGSIG signal we intend to use */
1088 sigemptyset(&set);
1089 sigaddset(&set, DEBUGSIG);
1090 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1091 return 1;
1092}
1093
1094static int init_debug()
1095{
1096 struct sigaction sa;
1097
Willy Tarreau0214b452020-03-04 06:01:40 +01001098#ifdef USE_BACKTRACE
1099 /* calling backtrace() will access libgcc at runtime. We don't want to
1100 * do it after the chroot, so let's perform a first call to have it
1101 * ready in memory for later use.
1102 */
1103 void *callers[1];
Willy Tarreau13faf162020-03-04 07:44:06 +01001104 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreau0214b452020-03-04 06:01:40 +01001105#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +02001106 sa.sa_handler = NULL;
1107 sa.sa_sigaction = debug_handler;
1108 sigemptyset(&sa.sa_mask);
1109 sa.sa_flags = SA_SIGINFO;
1110 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001111 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001112}
1113
1114REGISTER_POST_CHECK(init_debug);
1115REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1116
1117#endif /* USE_THREAD_DUMP */
1118
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001119/* register cli keywords */
1120static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +02001121 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1122 {{ "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 +02001123#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +02001124 {{ "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 +02001125#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001126 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1127 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1128 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1129 {{ "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 +02001130#if defined(DEBUG_MEM_STATS)
1131 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT },
1132#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001133 {{ "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 +01001134 {{ "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 +02001135 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1136 {{ "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 +01001137 {{ "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 +02001138 {{ "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 +02001139 {{},}
1140}};
1141
1142INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);