blob: efbf19fe58e56b2f984f293e56575874864ae7be [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 Tarreau368bff42019-12-06 17:18:28 +010019#include <sys/types.h>
20#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020021
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020022#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020023#include <common/config.h>
24#include <common/debug.h>
25#include <common/hathreads.h>
26#include <common/initcall.h>
Willy Tarreau68680bb2019-10-23 17:23:25 +020027#include <common/ist.h>
28#include <common/net_helper.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020029#include <common/standard.h>
30
31#include <types/global.h>
Olivier Houchardde01ea92020-03-18 13:07:19 +010032#include <types/signal.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020033
34#include <proto/cli.h>
35#include <proto/fd.h>
Willy Tarreau78a7cb62019-08-21 14:16:02 +020036#include <proto/hlua.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020037#include <proto/stream_interface.h>
38#include <proto/task.h>
39
Willy Tarreaua37cb182019-07-31 19:20:39 +020040/* mask of threads still having to dump, used to respect ordering. Only used
41 * when USE_THREAD_DUMP is set.
42 */
43volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020044unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020045
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020046/* Dumps to the buffer some known information for the desired thread, and
47 * optionally extra info for the current thread. The dump will be appended to
48 * the buffer, so the caller is responsible for preliminary initializing it.
49 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020050 * in front of the calling thread's line (usually it's tid). Any stuck thread
51 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020052 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020053void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020054{
55 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010056 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
57 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
58 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020059
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020060 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +020061 "%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 +020062 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020063 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +020064 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020065 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020066 !!(global_tasks_mask & thr_bit),
67 !eb_is_empty(&task_per_thread[thr].timers),
68 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010069 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
70 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
71 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
72 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020073 task_per_thread[thr].task_list_size,
74 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020075 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020076 !!(task_profiling_mask & thr_bit));
77
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020078 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020079 " harmless=%d wantrdv=%d",
80 !!(threads_harmless_mask & thr_bit),
81 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020082
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020083 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020084 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020085
86 /* this is the end of what we can dump from outside the thread */
87
88 if (thr != tid)
89 return;
90
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020091 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020092 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +010093
94#ifdef USE_BACKTRACE
95 if (stuck) {
96 /* We only emit the backtrace for stuck threads in order not to
97 * waste precious output buffer space with non-interesting data.
98 */
99 struct buffer bak;
100 void *callers[100];
101 int j, nptrs;
102 void *addr;
103 int dump = 0;
104
Willy Tarreau13faf162020-03-04 07:44:06 +0100105 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100106
107 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO)
108 would produce similar output to the following: */
109
110 if (nptrs)
Willy Tarreaucdd80742020-03-04 07:38:23 +0100111 chunk_appendf(buf, " call trace(%d):\n", nptrs);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100112
Willy Tarreaua91b7942020-03-04 07:39:32 +0100113 for (j = 0; j < nptrs || dump < 2; j++) {
114 if (j == nptrs && !dump) {
115 /* we failed to spot the starting point of the
116 * dump, let's start over dumping everything we
117 * have.
118 */
119 dump = 2;
120 j = 0;
121 }
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100122 bak = *buf;
123 dump_addr_and_bytes(buf, " | ", callers[j], 8);
124 addr = resolve_sym_name(buf, ": ", callers[j]);
125 if (dump == 0) {
126 /* dump not started, will start *after*
127 * ha_thread_dump_all_to_trash and ha_panic
128 */
129 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic)
130 dump = 1;
131 *buf = bak;
132 continue;
133 }
134
135 if (dump == 1) {
136 /* starting */
137 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) {
138 *buf = bak;
139 continue;
140 }
141 dump = 2;
142 }
143
144 if (dump == 2) {
145 /* dumping */
146 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_list) {
147 dump = 3;
148 *buf = bak;
149 break;
150 }
151 }
152 /* OK, line dumped */
153 chunk_appendf(buf, "\n");
154 }
155 }
156#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200157}
158
159
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200160/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200161 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200162 * with <pfx>. The buffer is only appended and the first output starts by the
163 * pointer itself. The caller is responsible for making sure the task is not
164 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200165 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200166void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200167{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200168 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200169 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200170 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200171
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200172 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200173 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200174 return;
175 }
176
Willy Tarreau20db9112019-05-17 14:14:35 +0200177 if (TASK_IS_TASKLET(task))
178 chunk_appendf(buf,
179 "%p (tasklet) calls=%u\n",
180 task,
181 task->calls);
182 else
183 chunk_appendf(buf,
184 "%p (task) calls=%u last=%llu%s\n",
185 task,
186 task->calls,
187 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
188 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200189
Willy Tarreau2e89b092020-03-03 17:13:02 +0100190 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
191 resolve_sym_name(buf, NULL, task->process);
192 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200193
Willy Tarreaua512b022019-08-21 14:12:19 +0200194 if (task->process == task_run_applet && (appctx = task->context))
195 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
196 else
197 chunk_appendf(buf, "\n");
198
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200199 if (task->process == process_stream && task->context)
200 s = (struct stream *)task->context;
201 else if (task->process == task_run_applet && task->context)
202 s = si_strm(((struct appctx *)task->context)->owner);
203 else if (task->process == si_cs_io_cb && task->context)
204 s = si_strm((struct stream_interface *)task->context);
205
206 if (s)
207 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200208
209#ifdef USE_LUA
210 hlua = NULL;
211 if (s && (hlua = s->hlua)) {
212 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
213 }
214 else if (task->process == hlua_process_task && (hlua = task->context)) {
215 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
216 }
217 else if (task->process == task_run_applet && (appctx = task->context) &&
218 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
219 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
220 }
221 else if (task->process == task_run_applet && (appctx = task->context) &&
222 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
223 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
224 }
225
226 if (hlua) {
227 luaL_traceback(hlua->T, hlua->T, NULL, 0);
228 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
229 b_putchr(buf, '\n');
230 }
231#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200232}
233
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200234/* This function dumps all profiling settings. It returns 0 if the output
235 * buffer is full and it needs to be called again, otherwise non-zero.
236 */
237static int cli_io_handler_show_threads(struct appctx *appctx)
238{
239 struct stream_interface *si = appctx->owner;
240 int thr;
241
242 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
243 return 1;
244
245 if (appctx->st0)
246 thr = appctx->st1;
247 else
248 thr = 0;
249
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200250 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200251 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200252
253 if (ci_putchk(si_ic(si), &trash) == -1) {
254 /* failed, try again */
255 si_rx_room_blk(si);
256 appctx->st1 = thr;
257 return 0;
258 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200259 return 1;
260}
261
Willy Tarreau56131ca2019-05-20 13:48:29 +0200262/* dumps a state of all threads into the trash and on fd #2, then aborts. */
263void ha_panic()
264{
265 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200266 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200267 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100268 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200269 for (;;)
270 abort();
271}
272
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200273/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
274static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
275{
276 int code = atoi(args[3]);
277
278 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
279 return 1;
280
Willy Tarreau9b013702019-10-24 18:18:02 +0200281 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200282 exit(code);
283 return 1;
284}
285
286/* parse a "debug dev close" command. It always returns 1. */
287static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
288{
289 int fd;
290
291 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
292 return 1;
293
Willy Tarreau9d008692019-08-09 11:21:01 +0200294 if (!*args[3])
295 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200296
297 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200298 if (fd < 0 || fd >= global.maxsock)
299 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200300
Willy Tarreau9d008692019-08-09 11:21:01 +0200301 if (!fdtab[fd].owner)
302 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200303
Willy Tarreau9b013702019-10-24 18:18:02 +0200304 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200305 fd_delete(fd);
306 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200307}
308
309/* parse a "debug dev delay" command. It always returns 1. */
310static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
311{
312 int delay = atoi(args[3]);
313
314 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
315 return 1;
316
Willy Tarreau9b013702019-10-24 18:18:02 +0200317 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200318 usleep((long)delay * 1000);
319 return 1;
320}
321
322/* parse a "debug dev log" command. It always returns 1. */
323static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
324{
325 int arg;
326
327 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
328 return 1;
329
Willy Tarreau9b013702019-10-24 18:18:02 +0200330 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200331 chunk_reset(&trash);
332 for (arg = 3; *args[arg]; arg++) {
333 if (arg > 3)
334 chunk_strcat(&trash, " ");
335 chunk_strcat(&trash, args[arg]);
336 }
337
338 send_log(NULL, LOG_INFO, "%s\n", trash.area);
339 return 1;
340}
341
342/* parse a "debug dev loop" command. It always returns 1. */
343static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
344{
345 struct timeval deadline, curr;
346 int loop = atoi(args[3]);
347
348 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
349 return 1;
350
Willy Tarreau9b013702019-10-24 18:18:02 +0200351 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200352 gettimeofday(&curr, NULL);
353 tv_ms_add(&deadline, &curr, loop);
354
355 while (tv_ms_cmp(&curr, &deadline) < 0)
356 gettimeofday(&curr, NULL);
357
358 return 1;
359}
360
361/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
362static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
363{
364 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
365 return 1;
366
Willy Tarreau9b013702019-10-24 18:18:02 +0200367 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200368 ha_panic();
369 return 1;
370}
371
372/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200373#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200374static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
375{
Willy Tarreau368bff42019-12-06 17:18:28 +0100376 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100378 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379
380 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
381 return 1;
382
Willy Tarreau9b013702019-10-24 18:18:02 +0200383 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200384 chunk_reset(&trash);
385 for (arg = 3; *args[arg]; arg++) {
386 if (arg > 3)
387 chunk_strcat(&trash, " ");
388 chunk_strcat(&trash, args[arg]);
389 }
390
Willy Tarreau368bff42019-12-06 17:18:28 +0100391 thread_isolate();
392 if (pipe(pipefd) < 0)
393 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200394
Willy Tarreau368bff42019-12-06 17:18:28 +0100395 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
396 goto fail_fcntl;
397
398 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
399 goto fail_fcntl;
400
401 pid = fork();
402
403 if (pid < 0)
404 goto fail_fork;
405 else if (pid == 0) {
406 /* child */
407 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
408
409 close(0);
410 dup2(pipefd[1], 1);
411 dup2(pipefd[1], 2);
412
413 cmd[2] = trash.area;
414 execvp(cmd[0], cmd);
415 printf("execvp() failed\n");
416 exit(1);
417 }
418
419 /* parent */
420 thread_release();
421 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200422 chunk_reset(&trash);
423 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100424 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
425 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200426 break;
427 trash.data += ret;
428 if (trash.data + 20 == trash.size) {
429 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
430 break;
431 }
432 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100433 close(pipefd[0]);
434 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200435 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200436 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100437
438 fail_fork:
439 fail_fcntl:
440 close(pipefd[0]);
441 close(pipefd[1]);
442 fail_pipe:
443 thread_release();
444 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200445}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200446#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200447
448/* parse a "debug dev hex" command. It always returns 1. */
449static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
450{
451 unsigned long start, len;
452
453 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
454 return 1;
455
Willy Tarreau9d008692019-08-09 11:21:01 +0200456 if (!*args[3])
457 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200458
459 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200460 if (!start)
461 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200462
Willy Tarreau9b013702019-10-24 18:18:02 +0200463 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
464
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200465 /* by default, dump ~128 till next block of 16 */
466 len = strtoul(args[4], NULL, 0);
467 if (!len)
468 len = ((start + 128) & -16) - start;
469
470 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200471 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200472 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200473 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200474}
475
476/* parse a "debug dev tkill" command. It always returns 1. */
477static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
478{
479 int thr = 0;
480 int sig = SIGABRT;
481
482 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
483 return 1;
484
485 if (*args[3])
486 thr = atoi(args[3]);
487
Willy Tarreau9d008692019-08-09 11:21:01 +0200488 if (thr < 0 || thr > global.nbthread)
489 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200490
491 if (*args[4])
492 sig = atoi(args[4]);
493
Willy Tarreau9b013702019-10-24 18:18:02 +0200494 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200495 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200496 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200497 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200498 raise(sig);
499 return 1;
500}
501
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100502/* parse a "debug dev write" command. It always returns 1. */
503static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
504{
505 unsigned long len;
506
507 if (!*args[3])
508 return cli_err(appctx, "Missing output size.\n");
509
510 len = strtoul(args[3], NULL, 0);
511 if (len >= trash.size)
512 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
513
514 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
515
516 chunk_reset(&trash);
517 trash.data = len;
518 memset(trash.area, '.', trash.data);
519 trash.area[trash.data] = 0;
520 for (len = 64; len < trash.data; len += 64)
521 trash.area[len] = '\n';
522 return cli_msg(appctx, LOG_INFO, trash.area);
523}
524
Willy Tarreau68680bb2019-10-23 17:23:25 +0200525/* parse a "debug dev stream" command */
526/*
527 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
528 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
529 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
530 * [sif.s[=<state>]] [sib.s[=<state>]]
531 */
532static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
533{
534 struct stream *s = si_strm(appctx->owner);
535 int arg;
536 void *ptr;
537 int size;
538 const char *word, *end;
539 struct ist name;
540 char *msg = NULL;
541 char *endarg;
542 unsigned long long old, new;
543
544 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
545 return 1;
546
547 ptr = NULL; size = 0;
548
549 if (!*args[3]) {
550 return cli_err(appctx,
551 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
552 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
553 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
554 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
555 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
556 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
557 );
558 }
559
Willy Tarreau9b013702019-10-24 18:18:02 +0200560 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200561 for (arg = 3; *args[arg]; arg++) {
562 old = 0;
563 end = word = args[arg];
564 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
565 end++;
566 name = ist2(word, end - word);
567 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200568 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200569 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200570 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200571 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200572 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200573 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200574 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200575 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200576 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200577 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200578 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200579 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200580 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200581 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200582 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200583 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200584 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200585 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200586 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200587 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200588 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200589 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200590 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200591 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200592 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200593 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200594 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200595 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200596 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200597 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200598 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200599 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
600 continue;
601 } else
602 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
603
604 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200605 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200606 if (size == 8)
607 old = read_u64(ptr);
608 else if (size == 4)
609 old = read_u32(ptr);
610 else if (size == 2)
611 old = read_u16(ptr);
612 else
613 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200614 } else {
615 memprintf(&msg,
616 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
617 msg ? msg : "", ptr, (int)(end - word), word);
618 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200619 }
620
621 /* parse the new value . */
622 new = strtoll(end + 1, &endarg, 0);
623 if (end[1] && *endarg) {
624 if (strcmp(end + 1, "now") == 0)
625 new = now_ms;
626 else {
627 memprintf(&msg,
628 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
629 msg ? msg : "", end + 1, (int)(end - word), word);
630 continue;
631 }
632 }
633
634 switch (*end) {
635 case '\0': /* show */
636 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
637 new = old; // do not change the value
638 break;
639
640 case '=': /* set */
641 break;
642
643 case '^': /* XOR */
644 new = old ^ new;
645 break;
646
647 case '+': /* OR */
648 new = old | new;
649 break;
650
651 case '-': /* AND NOT */
652 new = old & ~new;
653 break;
654
655 default:
656 break;
657 }
658
659 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200660 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200661 if (size == 8)
662 write_u64(ptr, new);
663 else if (size == 4)
664 write_u32(ptr, new);
665 else if (size == 2)
666 write_u16(ptr, new);
667 else
668 *(uint8_t *)ptr = new;
669 }
670 }
671
672 if (msg && *msg)
673 return cli_dynmsg(appctx, LOG_INFO, msg);
674 return 1;
675}
676
Willy Tarreauc7091d82019-05-17 10:08:49 +0200677#ifndef USE_THREAD_DUMP
678
679/* This function dumps all threads' state to the trash. This version is the
680 * most basic one, which doesn't inspect other threads.
681 */
682void ha_thread_dump_all_to_trash()
683{
684 unsigned int thr;
685
686 for (thr = 0; thr < global.nbthread; thr++)
687 ha_thread_dump(&trash, thr, tid);
688}
689
690#else /* below USE_THREAD_DUMP is set */
691
Willy Tarreauc7091d82019-05-17 10:08:49 +0200692/* ID of the thread requesting the dump */
693static unsigned int thread_dump_tid;
694
695/* points to the buffer where the dump functions should write. It must
696 * have already been initialized by the requester. Nothing is done if
697 * it's NULL.
698 */
699struct buffer *thread_dump_buffer = NULL;
700
701void ha_thread_dump_all_to_trash()
702{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200703 unsigned long old;
704
705 while (1) {
706 old = 0;
707 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
708 break;
709 ha_thread_relax();
710 }
711
712 thread_dump_buffer = &trash;
713 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200714 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200715}
716
717/* handles DEBUGSIG to dump the state of the thread it's working on */
718void debug_handler(int sig, siginfo_t *si, void *arg)
719{
Willy Tarreau82aafc42020-03-03 08:31:34 +0100720 /* first, let's check it's really for us and that we didn't just get
721 * a spurious DEBUGSIG.
722 */
723 if (!(threads_to_dump & tid_bit))
724 return;
725
Willy Tarreauc7091d82019-05-17 10:08:49 +0200726 /* There are 4 phases in the dump process:
727 * 1- wait for our turn, i.e. when all lower bits are gone.
728 * 2- perform the action if our bit is set
729 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200730 * the last one and have to put them all as a signal
731 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200732 */
733
734 /* wait for all previous threads to finish first */
735 while (threads_to_dump & (tid_bit - 1))
736 ha_thread_relax();
737
738 /* dump if needed */
739 if (threads_to_dump & tid_bit) {
740 if (thread_dump_buffer)
741 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
742 if ((threads_to_dump & all_threads_mask) == tid_bit) {
743 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200744 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200745 thread_dump_buffer = NULL;
746 }
747 else
748 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
749 }
750
751 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200752 * bits again to broadcast the leaving condition so we'll see ourselves
753 * present again. This way the threads_to_dump variable never passes to
754 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200755 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200756 while (!(threads_to_dump & tid_bit))
757 ha_thread_relax();
758 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200759
760 /* mark the current thread as stuck to detect it upon next invocation
761 * if it didn't move.
762 */
763 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
764 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200765}
766
767static int init_debug_per_thread()
768{
769 sigset_t set;
770
771 /* unblock the DEBUGSIG signal we intend to use */
772 sigemptyset(&set);
773 sigaddset(&set, DEBUGSIG);
774 ha_sigmask(SIG_UNBLOCK, &set, NULL);
775 return 1;
776}
777
778static int init_debug()
779{
780 struct sigaction sa;
781
Willy Tarreau0214b452020-03-04 06:01:40 +0100782#ifdef USE_BACKTRACE
783 /* calling backtrace() will access libgcc at runtime. We don't want to
784 * do it after the chroot, so let's perform a first call to have it
785 * ready in memory for later use.
786 */
787 void *callers[1];
Willy Tarreau13faf162020-03-04 07:44:06 +0100788 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreau0214b452020-03-04 06:01:40 +0100789#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +0200790 sa.sa_handler = NULL;
791 sa.sa_sigaction = debug_handler;
792 sigemptyset(&sa.sa_mask);
793 sa.sa_flags = SA_SIGINFO;
794 sigaction(DEBUGSIG, &sa, NULL);
795 return 0;
796}
797
798REGISTER_POST_CHECK(init_debug);
799REGISTER_PER_THREAD_INIT(init_debug_per_thread);
800
801#endif /* USE_THREAD_DUMP */
802
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200803/* register cli keywords */
804static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200805 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
806 {{ "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 +0200807#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200808 {{ "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 +0200809#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200810 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
811 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
812 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
813 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
814 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
815 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
816 {{ "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 +0100817 {{ "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 +0200818 {{ "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 +0200819 {{},}
820}};
821
822INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);