blob: ec905377bde540d393f71634fcf78f06d86417e6 [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
14#ifdef USE_BACKTRACE
15#define _GNU_SOURCE
16#include <execinfo.h>
17#endif
18
Willy Tarreau368bff42019-12-06 17:18:28 +010019#include <fcntl.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020020#include <signal.h>
21#include <time.h>
22#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020023#include <stdlib.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010024#include <sys/types.h>
25#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020026
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020027#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020028#include <common/config.h>
29#include <common/debug.h>
30#include <common/hathreads.h>
31#include <common/initcall.h>
Willy Tarreau68680bb2019-10-23 17:23:25 +020032#include <common/ist.h>
33#include <common/net_helper.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020034#include <common/standard.h>
35
36#include <types/global.h>
37
38#include <proto/cli.h>
39#include <proto/fd.h>
Willy Tarreau78a7cb62019-08-21 14:16:02 +020040#include <proto/hlua.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020041#include <proto/stream_interface.h>
42#include <proto/task.h>
43
Willy Tarreaua37cb182019-07-31 19:20:39 +020044/* mask of threads still having to dump, used to respect ordering. Only used
45 * when USE_THREAD_DUMP is set.
46 */
47volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020048unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020049
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020050/* Dumps to the buffer some known information for the desired thread, and
51 * optionally extra info for the current thread. The dump will be appended to
52 * the buffer, so the caller is responsible for preliminary initializing it.
53 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020054 * in front of the calling thread's line (usually it's tid). Any stuck thread
55 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020056 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020057void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020058{
59 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010060 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
61 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
62 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020063
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020064 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020065 "%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 +020066 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020067 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020068 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020069 !!(global_tasks_mask & thr_bit),
70 !eb_is_empty(&task_per_thread[thr].timers),
71 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010072 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
73 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
74 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
75 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020076 task_per_thread[thr].task_list_size,
77 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020078 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020079 !!(task_profiling_mask & thr_bit));
80
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020081 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020082 " harmless=%d wantrdv=%d",
83 !!(threads_harmless_mask & thr_bit),
84 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020085
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020086 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020087 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088
89 /* this is the end of what we can dump from outside the thread */
90
91 if (thr != tid)
92 return;
93
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020094 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020095 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +010096
97#ifdef USE_BACKTRACE
98 if (stuck) {
99 /* We only emit the backtrace for stuck threads in order not to
100 * waste precious output buffer space with non-interesting data.
101 */
102 struct buffer bak;
103 void *callers[100];
104 int j, nptrs;
105 void *addr;
106 int dump = 0;
107
108 nptrs = backtrace(callers, sizeof(callers)/sizeof(*callers));
109
110 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO)
111 would produce similar output to the following: */
112
113 if (nptrs)
114 chunk_appendf(buf, " call trace:\n");
115
116#ifndef USE_DL
117 /* if we can't rely on dladdr1() we won't figure what level is
118 * in ha_panic() or ha_thread_dump_all_to_trash(), so we want
119 * to immediately start the dump.
120 */
121 dump = 2;
122#endif
123 for (j = 0; j < nptrs; j++) {
124 bak = *buf;
125 dump_addr_and_bytes(buf, " | ", callers[j], 8);
126 addr = resolve_sym_name(buf, ": ", callers[j]);
127 if (dump == 0) {
128 /* dump not started, will start *after*
129 * ha_thread_dump_all_to_trash and ha_panic
130 */
131 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic)
132 dump = 1;
133 *buf = bak;
134 continue;
135 }
136
137 if (dump == 1) {
138 /* starting */
139 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) {
140 *buf = bak;
141 continue;
142 }
143 dump = 2;
144 }
145
146 if (dump == 2) {
147 /* dumping */
148 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_list) {
149 dump = 3;
150 *buf = bak;
151 break;
152 }
153 }
154 /* OK, line dumped */
155 chunk_appendf(buf, "\n");
156 }
157 }
158#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200159}
160
161
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200162/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200163 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200164 * with <pfx>. The buffer is only appended and the first output starts by the
165 * pointer itself. The caller is responsible for making sure the task is not
166 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200167 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200168void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200169{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200170 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200171 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200172 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200173
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200174 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200175 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200176 return;
177 }
178
Willy Tarreau20db9112019-05-17 14:14:35 +0200179 if (TASK_IS_TASKLET(task))
180 chunk_appendf(buf,
181 "%p (tasklet) calls=%u\n",
182 task,
183 task->calls);
184 else
185 chunk_appendf(buf,
186 "%p (task) calls=%u last=%llu%s\n",
187 task,
188 task->calls,
189 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
190 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200191
Willy Tarreau2e89b092020-03-03 17:13:02 +0100192 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
193 resolve_sym_name(buf, NULL, task->process);
194 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200195
Willy Tarreaua512b022019-08-21 14:12:19 +0200196 if (task->process == task_run_applet && (appctx = task->context))
197 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
198 else
199 chunk_appendf(buf, "\n");
200
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200201 if (task->process == process_stream && task->context)
202 s = (struct stream *)task->context;
203 else if (task->process == task_run_applet && task->context)
204 s = si_strm(((struct appctx *)task->context)->owner);
205 else if (task->process == si_cs_io_cb && task->context)
206 s = si_strm((struct stream_interface *)task->context);
207
208 if (s)
209 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200210
211#ifdef USE_LUA
212 hlua = NULL;
213 if (s && (hlua = s->hlua)) {
214 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
215 }
216 else if (task->process == hlua_process_task && (hlua = task->context)) {
217 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
218 }
219 else if (task->process == task_run_applet && (appctx = task->context) &&
220 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
221 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
222 }
223 else if (task->process == task_run_applet && (appctx = task->context) &&
224 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
225 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
226 }
227
228 if (hlua) {
229 luaL_traceback(hlua->T, hlua->T, NULL, 0);
230 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
231 b_putchr(buf, '\n');
232 }
233#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200234}
235
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200236/* This function dumps all profiling settings. It returns 0 if the output
237 * buffer is full and it needs to be called again, otherwise non-zero.
238 */
239static int cli_io_handler_show_threads(struct appctx *appctx)
240{
241 struct stream_interface *si = appctx->owner;
242 int thr;
243
244 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
245 return 1;
246
247 if (appctx->st0)
248 thr = appctx->st1;
249 else
250 thr = 0;
251
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200252 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200253 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200254
255 if (ci_putchk(si_ic(si), &trash) == -1) {
256 /* failed, try again */
257 si_rx_room_blk(si);
258 appctx->st1 = thr;
259 return 0;
260 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200261 return 1;
262}
263
Willy Tarreau56131ca2019-05-20 13:48:29 +0200264/* dumps a state of all threads into the trash and on fd #2, then aborts. */
265void ha_panic()
266{
267 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200268 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200269 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200270 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200271 for (;;)
272 abort();
273}
274
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200275/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
276static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
277{
278 int code = atoi(args[3]);
279
280 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
281 return 1;
282
Willy Tarreau9b013702019-10-24 18:18:02 +0200283 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200284 exit(code);
285 return 1;
286}
287
288/* parse a "debug dev close" command. It always returns 1. */
289static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
290{
291 int fd;
292
293 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
294 return 1;
295
Willy Tarreau9d008692019-08-09 11:21:01 +0200296 if (!*args[3])
297 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200298
299 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200300 if (fd < 0 || fd >= global.maxsock)
301 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200302
Willy Tarreau9d008692019-08-09 11:21:01 +0200303 if (!fdtab[fd].owner)
304 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200305
Willy Tarreau9b013702019-10-24 18:18:02 +0200306 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200307 fd_delete(fd);
308 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200309}
310
311/* parse a "debug dev delay" command. It always returns 1. */
312static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
313{
314 int delay = atoi(args[3]);
315
316 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
317 return 1;
318
Willy Tarreau9b013702019-10-24 18:18:02 +0200319 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200320 usleep((long)delay * 1000);
321 return 1;
322}
323
324/* parse a "debug dev log" command. It always returns 1. */
325static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
326{
327 int arg;
328
329 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
330 return 1;
331
Willy Tarreau9b013702019-10-24 18:18:02 +0200332 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200333 chunk_reset(&trash);
334 for (arg = 3; *args[arg]; arg++) {
335 if (arg > 3)
336 chunk_strcat(&trash, " ");
337 chunk_strcat(&trash, args[arg]);
338 }
339
340 send_log(NULL, LOG_INFO, "%s\n", trash.area);
341 return 1;
342}
343
344/* parse a "debug dev loop" command. It always returns 1. */
345static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
346{
347 struct timeval deadline, curr;
348 int loop = atoi(args[3]);
349
350 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
351 return 1;
352
Willy Tarreau9b013702019-10-24 18:18:02 +0200353 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200354 gettimeofday(&curr, NULL);
355 tv_ms_add(&deadline, &curr, loop);
356
357 while (tv_ms_cmp(&curr, &deadline) < 0)
358 gettimeofday(&curr, NULL);
359
360 return 1;
361}
362
363/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
364static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
365{
366 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
367 return 1;
368
Willy Tarreau9b013702019-10-24 18:18:02 +0200369 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200370 ha_panic();
371 return 1;
372}
373
374/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200375#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200376static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
377{
Willy Tarreau368bff42019-12-06 17:18:28 +0100378 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100380 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200381
382 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
383 return 1;
384
Willy Tarreau9b013702019-10-24 18:18:02 +0200385 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200386 chunk_reset(&trash);
387 for (arg = 3; *args[arg]; arg++) {
388 if (arg > 3)
389 chunk_strcat(&trash, " ");
390 chunk_strcat(&trash, args[arg]);
391 }
392
Willy Tarreau368bff42019-12-06 17:18:28 +0100393 thread_isolate();
394 if (pipe(pipefd) < 0)
395 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200396
Willy Tarreau368bff42019-12-06 17:18:28 +0100397 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
398 goto fail_fcntl;
399
400 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
401 goto fail_fcntl;
402
403 pid = fork();
404
405 if (pid < 0)
406 goto fail_fork;
407 else if (pid == 0) {
408 /* child */
409 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
410
411 close(0);
412 dup2(pipefd[1], 1);
413 dup2(pipefd[1], 2);
414
415 cmd[2] = trash.area;
416 execvp(cmd[0], cmd);
417 printf("execvp() failed\n");
418 exit(1);
419 }
420
421 /* parent */
422 thread_release();
423 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424 chunk_reset(&trash);
425 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100426 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
427 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200428 break;
429 trash.data += ret;
430 if (trash.data + 20 == trash.size) {
431 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
432 break;
433 }
434 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100435 close(pipefd[0]);
436 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200437 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200438 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100439
440 fail_fork:
441 fail_fcntl:
442 close(pipefd[0]);
443 close(pipefd[1]);
444 fail_pipe:
445 thread_release();
446 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200447}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200448#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200449
450/* parse a "debug dev hex" command. It always returns 1. */
451static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
452{
453 unsigned long start, len;
454
455 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
456 return 1;
457
Willy Tarreau9d008692019-08-09 11:21:01 +0200458 if (!*args[3])
459 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200460
461 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200462 if (!start)
463 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200464
Willy Tarreau9b013702019-10-24 18:18:02 +0200465 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
466
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200467 /* by default, dump ~128 till next block of 16 */
468 len = strtoul(args[4], NULL, 0);
469 if (!len)
470 len = ((start + 128) & -16) - start;
471
472 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200473 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200474 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200475 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200476}
477
478/* parse a "debug dev tkill" command. It always returns 1. */
479static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
480{
481 int thr = 0;
482 int sig = SIGABRT;
483
484 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
485 return 1;
486
487 if (*args[3])
488 thr = atoi(args[3]);
489
Willy Tarreau9d008692019-08-09 11:21:01 +0200490 if (thr < 0 || thr > global.nbthread)
491 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200492
493 if (*args[4])
494 sig = atoi(args[4]);
495
Willy Tarreau9b013702019-10-24 18:18:02 +0200496 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200497 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200498 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200499 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200500 raise(sig);
501 return 1;
502}
503
Willy Tarreau68680bb2019-10-23 17:23:25 +0200504/* parse a "debug dev stream" command */
505/*
506 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
507 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
508 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
509 * [sif.s[=<state>]] [sib.s[=<state>]]
510 */
511static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
512{
513 struct stream *s = si_strm(appctx->owner);
514 int arg;
515 void *ptr;
516 int size;
517 const char *word, *end;
518 struct ist name;
519 char *msg = NULL;
520 char *endarg;
521 unsigned long long old, new;
522
523 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
524 return 1;
525
526 ptr = NULL; size = 0;
527
528 if (!*args[3]) {
529 return cli_err(appctx,
530 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
531 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
532 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
533 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
534 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
535 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
536 );
537 }
538
Willy Tarreau9b013702019-10-24 18:18:02 +0200539 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200540 for (arg = 3; *args[arg]; arg++) {
541 old = 0;
542 end = word = args[arg];
543 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
544 end++;
545 name = ist2(word, end - word);
546 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200547 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200548 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200549 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200550 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200551 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200552 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200553 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200554 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200555 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200556 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200557 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200558 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200559 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200560 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200561 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200562 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200563 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200564 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200565 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200566 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200567 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200568 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200569 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200570 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200571 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200572 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200573 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200574 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200575 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200576 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200577 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200578 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
579 continue;
580 } else
581 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
582
583 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200584 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200585 if (size == 8)
586 old = read_u64(ptr);
587 else if (size == 4)
588 old = read_u32(ptr);
589 else if (size == 2)
590 old = read_u16(ptr);
591 else
592 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200593 } else {
594 memprintf(&msg,
595 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
596 msg ? msg : "", ptr, (int)(end - word), word);
597 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200598 }
599
600 /* parse the new value . */
601 new = strtoll(end + 1, &endarg, 0);
602 if (end[1] && *endarg) {
603 if (strcmp(end + 1, "now") == 0)
604 new = now_ms;
605 else {
606 memprintf(&msg,
607 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
608 msg ? msg : "", end + 1, (int)(end - word), word);
609 continue;
610 }
611 }
612
613 switch (*end) {
614 case '\0': /* show */
615 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
616 new = old; // do not change the value
617 break;
618
619 case '=': /* set */
620 break;
621
622 case '^': /* XOR */
623 new = old ^ new;
624 break;
625
626 case '+': /* OR */
627 new = old | new;
628 break;
629
630 case '-': /* AND NOT */
631 new = old & ~new;
632 break;
633
634 default:
635 break;
636 }
637
638 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200639 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200640 if (size == 8)
641 write_u64(ptr, new);
642 else if (size == 4)
643 write_u32(ptr, new);
644 else if (size == 2)
645 write_u16(ptr, new);
646 else
647 *(uint8_t *)ptr = new;
648 }
649 }
650
651 if (msg && *msg)
652 return cli_dynmsg(appctx, LOG_INFO, msg);
653 return 1;
654}
655
Willy Tarreauc7091d82019-05-17 10:08:49 +0200656#ifndef USE_THREAD_DUMP
657
658/* This function dumps all threads' state to the trash. This version is the
659 * most basic one, which doesn't inspect other threads.
660 */
661void ha_thread_dump_all_to_trash()
662{
663 unsigned int thr;
664
665 for (thr = 0; thr < global.nbthread; thr++)
666 ha_thread_dump(&trash, thr, tid);
667}
668
669#else /* below USE_THREAD_DUMP is set */
670
Willy Tarreauddd85332019-05-22 06:28:54 +0200671/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
672 * of not stopping gdb by default, so that issuing "show threads" in a process
673 * being debugged has no adverse effect.
674 */
675#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200676
Willy Tarreauc7091d82019-05-17 10:08:49 +0200677/* ID of the thread requesting the dump */
678static unsigned int thread_dump_tid;
679
680/* points to the buffer where the dump functions should write. It must
681 * have already been initialized by the requester. Nothing is done if
682 * it's NULL.
683 */
684struct buffer *thread_dump_buffer = NULL;
685
686void ha_thread_dump_all_to_trash()
687{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200688 unsigned long old;
689
690 while (1) {
691 old = 0;
692 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
693 break;
694 ha_thread_relax();
695 }
696
697 thread_dump_buffer = &trash;
698 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200699 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200700}
701
702/* handles DEBUGSIG to dump the state of the thread it's working on */
703void debug_handler(int sig, siginfo_t *si, void *arg)
704{
Willy Tarreau82aafc42020-03-03 08:31:34 +0100705 /* first, let's check it's really for us and that we didn't just get
706 * a spurious DEBUGSIG.
707 */
708 if (!(threads_to_dump & tid_bit))
709 return;
710
Willy Tarreauc7091d82019-05-17 10:08:49 +0200711 /* There are 4 phases in the dump process:
712 * 1- wait for our turn, i.e. when all lower bits are gone.
713 * 2- perform the action if our bit is set
714 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200715 * the last one and have to put them all as a signal
716 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200717 */
718
719 /* wait for all previous threads to finish first */
720 while (threads_to_dump & (tid_bit - 1))
721 ha_thread_relax();
722
723 /* dump if needed */
724 if (threads_to_dump & tid_bit) {
725 if (thread_dump_buffer)
726 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
727 if ((threads_to_dump & all_threads_mask) == tid_bit) {
728 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200729 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200730 thread_dump_buffer = NULL;
731 }
732 else
733 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
734 }
735
736 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200737 * bits again to broadcast the leaving condition so we'll see ourselves
738 * present again. This way the threads_to_dump variable never passes to
739 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200740 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200741 while (!(threads_to_dump & tid_bit))
742 ha_thread_relax();
743 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200744
745 /* mark the current thread as stuck to detect it upon next invocation
746 * if it didn't move.
747 */
748 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
749 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200750}
751
752static int init_debug_per_thread()
753{
754 sigset_t set;
755
756 /* unblock the DEBUGSIG signal we intend to use */
757 sigemptyset(&set);
758 sigaddset(&set, DEBUGSIG);
759 ha_sigmask(SIG_UNBLOCK, &set, NULL);
760 return 1;
761}
762
763static int init_debug()
764{
765 struct sigaction sa;
766
Willy Tarreau0214b452020-03-04 06:01:40 +0100767#ifdef USE_BACKTRACE
768 /* calling backtrace() will access libgcc at runtime. We don't want to
769 * do it after the chroot, so let's perform a first call to have it
770 * ready in memory for later use.
771 */
772 void *callers[1];
773 backtrace(callers, sizeof(callers)/sizeof(*callers));
774#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +0200775 sa.sa_handler = NULL;
776 sa.sa_sigaction = debug_handler;
777 sigemptyset(&sa.sa_mask);
778 sa.sa_flags = SA_SIGINFO;
779 sigaction(DEBUGSIG, &sa, NULL);
780 return 0;
781}
782
783REGISTER_POST_CHECK(init_debug);
784REGISTER_PER_THREAD_INIT(init_debug_per_thread);
785
786#endif /* USE_THREAD_DUMP */
787
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200788/* register cli keywords */
789static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200790 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
791 {{ "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 +0200792#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200793 {{ "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 +0200794#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200795 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
796 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
797 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
798 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
799 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
800 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
801 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
802 {{ "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 +0200803 {{},}
804}};
805
806INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);