blob: 1fbcde7c43e3581d02fe17fb1e1a7cef2b566608 [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>
32
33#include <proto/cli.h>
34#include <proto/fd.h>
Willy Tarreau78a7cb62019-08-21 14:16:02 +020035#include <proto/hlua.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020036#include <proto/stream_interface.h>
37#include <proto/task.h>
38
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 Tarreau5cf64dd2019-05-17 10:36:08 +020045/* Dumps to the buffer some known information for the desired thread, and
46 * optionally extra info for the current thread. The dump will be appended to
47 * the buffer, so the caller is responsible for preliminary initializing it.
48 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020049 * in front of the calling thread's line (usually it's tid). Any stuck thread
50 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020051 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020052void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020053{
54 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010055 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
56 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
57 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020058
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020059 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020060 "%c%cThread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
Olivier Houchard305d5ab2019-07-24 18:07:06 +020061 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020062 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020063 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020064 !!(global_tasks_mask & thr_bit),
65 !eb_is_empty(&task_per_thread[thr].timers),
66 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010067 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
68 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
69 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
70 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020071 task_per_thread[thr].task_list_size,
72 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020073 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020074 !!(task_profiling_mask & thr_bit));
75
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020076 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020077 " harmless=%d wantrdv=%d",
78 !!(threads_harmless_mask & thr_bit),
79 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020080
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020081 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020082 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020083
84 /* this is the end of what we can dump from outside the thread */
85
86 if (thr != tid)
87 return;
88
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020089 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020090 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +010091
92#ifdef USE_BACKTRACE
93 if (stuck) {
94 /* We only emit the backtrace for stuck threads in order not to
95 * waste precious output buffer space with non-interesting data.
96 */
97 struct buffer bak;
98 void *callers[100];
99 int j, nptrs;
100 void *addr;
101 int dump = 0;
102
Willy Tarreau13faf162020-03-04 07:44:06 +0100103 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100104
105 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO)
106 would produce similar output to the following: */
107
108 if (nptrs)
Willy Tarreaucdd80742020-03-04 07:38:23 +0100109 chunk_appendf(buf, " call trace(%d):\n", nptrs);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100110
111#ifndef USE_DL
112 /* if we can't rely on dladdr1() we won't figure what level is
113 * in ha_panic() or ha_thread_dump_all_to_trash(), so we want
114 * to immediately start the dump.
115 */
116 dump = 2;
117#endif
118 for (j = 0; j < nptrs; j++) {
119 bak = *buf;
120 dump_addr_and_bytes(buf, " | ", callers[j], 8);
121 addr = resolve_sym_name(buf, ": ", callers[j]);
122 if (dump == 0) {
123 /* dump not started, will start *after*
124 * ha_thread_dump_all_to_trash and ha_panic
125 */
126 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic)
127 dump = 1;
128 *buf = bak;
129 continue;
130 }
131
132 if (dump == 1) {
133 /* starting */
134 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) {
135 *buf = bak;
136 continue;
137 }
138 dump = 2;
139 }
140
141 if (dump == 2) {
142 /* dumping */
143 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_list) {
144 dump = 3;
145 *buf = bak;
146 break;
147 }
148 }
149 /* OK, line dumped */
150 chunk_appendf(buf, "\n");
151 }
152 }
153#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200154}
155
156
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200157/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200158 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200159 * with <pfx>. The buffer is only appended and the first output starts by the
160 * pointer itself. The caller is responsible for making sure the task is not
161 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200162 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200163void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200164{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200165 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200166 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200167 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200168
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200169 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200170 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200171 return;
172 }
173
Willy Tarreau20db9112019-05-17 14:14:35 +0200174 if (TASK_IS_TASKLET(task))
175 chunk_appendf(buf,
176 "%p (tasklet) calls=%u\n",
177 task,
178 task->calls);
179 else
180 chunk_appendf(buf,
181 "%p (task) calls=%u last=%llu%s\n",
182 task,
183 task->calls,
184 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
185 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200186
Willy Tarreau2e89b092020-03-03 17:13:02 +0100187 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
188 resolve_sym_name(buf, NULL, task->process);
189 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200190
Willy Tarreaua512b022019-08-21 14:12:19 +0200191 if (task->process == task_run_applet && (appctx = task->context))
192 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
193 else
194 chunk_appendf(buf, "\n");
195
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200196 if (task->process == process_stream && task->context)
197 s = (struct stream *)task->context;
198 else if (task->process == task_run_applet && task->context)
199 s = si_strm(((struct appctx *)task->context)->owner);
200 else if (task->process == si_cs_io_cb && task->context)
201 s = si_strm((struct stream_interface *)task->context);
202
203 if (s)
204 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200205
206#ifdef USE_LUA
207 hlua = NULL;
208 if (s && (hlua = s->hlua)) {
209 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
210 }
211 else if (task->process == hlua_process_task && (hlua = task->context)) {
212 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
213 }
214 else if (task->process == task_run_applet && (appctx = task->context) &&
215 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
216 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
217 }
218 else if (task->process == task_run_applet && (appctx = task->context) &&
219 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
220 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
221 }
222
223 if (hlua) {
224 luaL_traceback(hlua->T, hlua->T, NULL, 0);
225 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
226 b_putchr(buf, '\n');
227 }
228#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200229}
230
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200231/* This function dumps all profiling settings. It returns 0 if the output
232 * buffer is full and it needs to be called again, otherwise non-zero.
233 */
234static int cli_io_handler_show_threads(struct appctx *appctx)
235{
236 struct stream_interface *si = appctx->owner;
237 int thr;
238
239 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
240 return 1;
241
242 if (appctx->st0)
243 thr = appctx->st1;
244 else
245 thr = 0;
246
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200247 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200248 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200249
250 if (ci_putchk(si_ic(si), &trash) == -1) {
251 /* failed, try again */
252 si_rx_room_blk(si);
253 appctx->st1 = thr;
254 return 0;
255 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200256 return 1;
257}
258
Willy Tarreau56131ca2019-05-20 13:48:29 +0200259/* dumps a state of all threads into the trash and on fd #2, then aborts. */
260void ha_panic()
261{
262 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200263 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200264 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200265 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200266 for (;;)
267 abort();
268}
269
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200270/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
271static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
272{
273 int code = atoi(args[3]);
274
275 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
276 return 1;
277
Willy Tarreau9b013702019-10-24 18:18:02 +0200278 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200279 exit(code);
280 return 1;
281}
282
283/* parse a "debug dev close" command. It always returns 1. */
284static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
285{
286 int fd;
287
288 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
289 return 1;
290
Willy Tarreau9d008692019-08-09 11:21:01 +0200291 if (!*args[3])
292 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200293
294 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200295 if (fd < 0 || fd >= global.maxsock)
296 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200297
Willy Tarreau9d008692019-08-09 11:21:01 +0200298 if (!fdtab[fd].owner)
299 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200300
Willy Tarreau9b013702019-10-24 18:18:02 +0200301 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200302 fd_delete(fd);
303 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200304}
305
306/* parse a "debug dev delay" command. It always returns 1. */
307static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
308{
309 int delay = atoi(args[3]);
310
311 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
312 return 1;
313
Willy Tarreau9b013702019-10-24 18:18:02 +0200314 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200315 usleep((long)delay * 1000);
316 return 1;
317}
318
319/* parse a "debug dev log" command. It always returns 1. */
320static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
321{
322 int arg;
323
324 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
325 return 1;
326
Willy Tarreau9b013702019-10-24 18:18:02 +0200327 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200328 chunk_reset(&trash);
329 for (arg = 3; *args[arg]; arg++) {
330 if (arg > 3)
331 chunk_strcat(&trash, " ");
332 chunk_strcat(&trash, args[arg]);
333 }
334
335 send_log(NULL, LOG_INFO, "%s\n", trash.area);
336 return 1;
337}
338
339/* parse a "debug dev loop" command. It always returns 1. */
340static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
341{
342 struct timeval deadline, curr;
343 int loop = atoi(args[3]);
344
345 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
346 return 1;
347
Willy Tarreau9b013702019-10-24 18:18:02 +0200348 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200349 gettimeofday(&curr, NULL);
350 tv_ms_add(&deadline, &curr, loop);
351
352 while (tv_ms_cmp(&curr, &deadline) < 0)
353 gettimeofday(&curr, NULL);
354
355 return 1;
356}
357
358/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
359static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
360{
361 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
362 return 1;
363
Willy Tarreau9b013702019-10-24 18:18:02 +0200364 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200365 ha_panic();
366 return 1;
367}
368
369/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200370#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200371static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
372{
Willy Tarreau368bff42019-12-06 17:18:28 +0100373 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200374 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100375 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200376
377 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
378 return 1;
379
Willy Tarreau9b013702019-10-24 18:18:02 +0200380 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200381 chunk_reset(&trash);
382 for (arg = 3; *args[arg]; arg++) {
383 if (arg > 3)
384 chunk_strcat(&trash, " ");
385 chunk_strcat(&trash, args[arg]);
386 }
387
Willy Tarreau368bff42019-12-06 17:18:28 +0100388 thread_isolate();
389 if (pipe(pipefd) < 0)
390 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391
Willy Tarreau368bff42019-12-06 17:18:28 +0100392 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
393 goto fail_fcntl;
394
395 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
396 goto fail_fcntl;
397
398 pid = fork();
399
400 if (pid < 0)
401 goto fail_fork;
402 else if (pid == 0) {
403 /* child */
404 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
405
406 close(0);
407 dup2(pipefd[1], 1);
408 dup2(pipefd[1], 2);
409
410 cmd[2] = trash.area;
411 execvp(cmd[0], cmd);
412 printf("execvp() failed\n");
413 exit(1);
414 }
415
416 /* parent */
417 thread_release();
418 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200419 chunk_reset(&trash);
420 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100421 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
422 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200423 break;
424 trash.data += ret;
425 if (trash.data + 20 == trash.size) {
426 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
427 break;
428 }
429 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100430 close(pipefd[0]);
431 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200432 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200433 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100434
435 fail_fork:
436 fail_fcntl:
437 close(pipefd[0]);
438 close(pipefd[1]);
439 fail_pipe:
440 thread_release();
441 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200442}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200443#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200444
445/* parse a "debug dev hex" command. It always returns 1. */
446static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
447{
448 unsigned long start, len;
449
450 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
451 return 1;
452
Willy Tarreau9d008692019-08-09 11:21:01 +0200453 if (!*args[3])
454 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200455
456 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200457 if (!start)
458 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200459
Willy Tarreau9b013702019-10-24 18:18:02 +0200460 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
461
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200462 /* by default, dump ~128 till next block of 16 */
463 len = strtoul(args[4], NULL, 0);
464 if (!len)
465 len = ((start + 128) & -16) - start;
466
467 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200468 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200469 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200470 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200471}
472
473/* parse a "debug dev tkill" command. It always returns 1. */
474static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
475{
476 int thr = 0;
477 int sig = SIGABRT;
478
479 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
480 return 1;
481
482 if (*args[3])
483 thr = atoi(args[3]);
484
Willy Tarreau9d008692019-08-09 11:21:01 +0200485 if (thr < 0 || thr > global.nbthread)
486 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200487
488 if (*args[4])
489 sig = atoi(args[4]);
490
Willy Tarreau9b013702019-10-24 18:18:02 +0200491 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200492 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200493 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200494 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200495 raise(sig);
496 return 1;
497}
498
Willy Tarreau68680bb2019-10-23 17:23:25 +0200499/* parse a "debug dev stream" command */
500/*
501 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
502 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
503 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
504 * [sif.s[=<state>]] [sib.s[=<state>]]
505 */
506static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
507{
508 struct stream *s = si_strm(appctx->owner);
509 int arg;
510 void *ptr;
511 int size;
512 const char *word, *end;
513 struct ist name;
514 char *msg = NULL;
515 char *endarg;
516 unsigned long long old, new;
517
518 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
519 return 1;
520
521 ptr = NULL; size = 0;
522
523 if (!*args[3]) {
524 return cli_err(appctx,
525 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
526 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
527 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
528 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
529 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
530 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
531 );
532 }
533
Willy Tarreau9b013702019-10-24 18:18:02 +0200534 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200535 for (arg = 3; *args[arg]; arg++) {
536 old = 0;
537 end = word = args[arg];
538 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
539 end++;
540 name = ist2(word, end - word);
541 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200542 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200543 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200544 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200545 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200546 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200547 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200548 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200549 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200550 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200551 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200552 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200553 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200554 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200555 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200556 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200557 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200558 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200559 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200560 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200561 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200562 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200563 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200564 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200565 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200566 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200567 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200568 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200569 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200570 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200571 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200572 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200573 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
574 continue;
575 } else
576 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
577
578 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200579 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200580 if (size == 8)
581 old = read_u64(ptr);
582 else if (size == 4)
583 old = read_u32(ptr);
584 else if (size == 2)
585 old = read_u16(ptr);
586 else
587 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200588 } else {
589 memprintf(&msg,
590 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
591 msg ? msg : "", ptr, (int)(end - word), word);
592 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200593 }
594
595 /* parse the new value . */
596 new = strtoll(end + 1, &endarg, 0);
597 if (end[1] && *endarg) {
598 if (strcmp(end + 1, "now") == 0)
599 new = now_ms;
600 else {
601 memprintf(&msg,
602 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
603 msg ? msg : "", end + 1, (int)(end - word), word);
604 continue;
605 }
606 }
607
608 switch (*end) {
609 case '\0': /* show */
610 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
611 new = old; // do not change the value
612 break;
613
614 case '=': /* set */
615 break;
616
617 case '^': /* XOR */
618 new = old ^ new;
619 break;
620
621 case '+': /* OR */
622 new = old | new;
623 break;
624
625 case '-': /* AND NOT */
626 new = old & ~new;
627 break;
628
629 default:
630 break;
631 }
632
633 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200634 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200635 if (size == 8)
636 write_u64(ptr, new);
637 else if (size == 4)
638 write_u32(ptr, new);
639 else if (size == 2)
640 write_u16(ptr, new);
641 else
642 *(uint8_t *)ptr = new;
643 }
644 }
645
646 if (msg && *msg)
647 return cli_dynmsg(appctx, LOG_INFO, msg);
648 return 1;
649}
650
Willy Tarreauc7091d82019-05-17 10:08:49 +0200651#ifndef USE_THREAD_DUMP
652
653/* This function dumps all threads' state to the trash. This version is the
654 * most basic one, which doesn't inspect other threads.
655 */
656void ha_thread_dump_all_to_trash()
657{
658 unsigned int thr;
659
660 for (thr = 0; thr < global.nbthread; thr++)
661 ha_thread_dump(&trash, thr, tid);
662}
663
664#else /* below USE_THREAD_DUMP is set */
665
Willy Tarreauddd85332019-05-22 06:28:54 +0200666/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
667 * of not stopping gdb by default, so that issuing "show threads" in a process
668 * being debugged has no adverse effect.
669 */
670#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200671
Willy Tarreauc7091d82019-05-17 10:08:49 +0200672/* ID of the thread requesting the dump */
673static unsigned int thread_dump_tid;
674
675/* points to the buffer where the dump functions should write. It must
676 * have already been initialized by the requester. Nothing is done if
677 * it's NULL.
678 */
679struct buffer *thread_dump_buffer = NULL;
680
681void ha_thread_dump_all_to_trash()
682{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200683 unsigned long old;
684
685 while (1) {
686 old = 0;
687 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
688 break;
689 ha_thread_relax();
690 }
691
692 thread_dump_buffer = &trash;
693 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200694 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200695}
696
697/* handles DEBUGSIG to dump the state of the thread it's working on */
698void debug_handler(int sig, siginfo_t *si, void *arg)
699{
Willy Tarreau82aafc42020-03-03 08:31:34 +0100700 /* first, let's check it's really for us and that we didn't just get
701 * a spurious DEBUGSIG.
702 */
703 if (!(threads_to_dump & tid_bit))
704 return;
705
Willy Tarreauc7091d82019-05-17 10:08:49 +0200706 /* There are 4 phases in the dump process:
707 * 1- wait for our turn, i.e. when all lower bits are gone.
708 * 2- perform the action if our bit is set
709 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200710 * the last one and have to put them all as a signal
711 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200712 */
713
714 /* wait for all previous threads to finish first */
715 while (threads_to_dump & (tid_bit - 1))
716 ha_thread_relax();
717
718 /* dump if needed */
719 if (threads_to_dump & tid_bit) {
720 if (thread_dump_buffer)
721 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
722 if ((threads_to_dump & all_threads_mask) == tid_bit) {
723 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200724 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200725 thread_dump_buffer = NULL;
726 }
727 else
728 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
729 }
730
731 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200732 * bits again to broadcast the leaving condition so we'll see ourselves
733 * present again. This way the threads_to_dump variable never passes to
734 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200735 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200736 while (!(threads_to_dump & tid_bit))
737 ha_thread_relax();
738 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200739
740 /* mark the current thread as stuck to detect it upon next invocation
741 * if it didn't move.
742 */
743 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
744 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200745}
746
747static int init_debug_per_thread()
748{
749 sigset_t set;
750
751 /* unblock the DEBUGSIG signal we intend to use */
752 sigemptyset(&set);
753 sigaddset(&set, DEBUGSIG);
754 ha_sigmask(SIG_UNBLOCK, &set, NULL);
755 return 1;
756}
757
758static int init_debug()
759{
760 struct sigaction sa;
761
Willy Tarreau0214b452020-03-04 06:01:40 +0100762#ifdef USE_BACKTRACE
763 /* calling backtrace() will access libgcc at runtime. We don't want to
764 * do it after the chroot, so let's perform a first call to have it
765 * ready in memory for later use.
766 */
767 void *callers[1];
Willy Tarreau13faf162020-03-04 07:44:06 +0100768 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreau0214b452020-03-04 06:01:40 +0100769#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +0200770 sa.sa_handler = NULL;
771 sa.sa_sigaction = debug_handler;
772 sigemptyset(&sa.sa_mask);
773 sa.sa_flags = SA_SIGINFO;
774 sigaction(DEBUGSIG, &sa, NULL);
775 return 0;
776}
777
778REGISTER_POST_CHECK(init_debug);
779REGISTER_PER_THREAD_INIT(init_debug_per_thread);
780
781#endif /* USE_THREAD_DUMP */
782
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200783/* register cli keywords */
784static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200785 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
786 {{ "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 +0200787#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200788 {{ "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 +0200789#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200790 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
791 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
792 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
793 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
794 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
795 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
796 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
797 {{ "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 +0200798 {{},}
799}};
800
801INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);