blob: 9f538b6606dac9fae7f23126b04055a620d96da3 [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
13#include <signal.h>
14#include <time.h>
15#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020016#include <stdlib.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020017
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020018#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020019#include <common/config.h>
20#include <common/debug.h>
21#include <common/hathreads.h>
22#include <common/initcall.h>
23#include <common/standard.h>
24
25#include <types/global.h>
26
27#include <proto/cli.h>
28#include <proto/fd.h>
29#include <proto/stream_interface.h>
30#include <proto/task.h>
31
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020032/* Dumps to the buffer some known information for the desired thread, and
33 * optionally extra info for the current thread. The dump will be appended to
34 * the buffer, so the caller is responsible for preliminary initializing it.
35 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020036 * in front of the calling thread's line (usually it's tid). Any stuck thread
37 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020038 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020039void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020040{
41 unsigned long thr_bit = 1UL << thr;
Willy Tarreau9c8800a2019-05-20 20:52:20 +020042 unsigned long long p = thread_info[thr].prev_cpu_time;
43 unsigned long long n = now_cpu_time_thread(&thread_info[thr]);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020044 int stuck = !!(thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020045
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020046 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020047 "%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 +020048 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020049 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020050 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020051 !!(global_tasks_mask & thr_bit),
52 !eb_is_empty(&task_per_thread[thr].timers),
53 !eb_is_empty(&task_per_thread[thr].rqueue),
54 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
55 task_per_thread[thr].task_list_size,
56 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020057 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020058 !!(task_profiling_mask & thr_bit));
59
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020060 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020061 " harmless=%d wantrdv=%d",
62 !!(threads_harmless_mask & thr_bit),
63 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020064
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020065 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020066 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020067
68 /* this is the end of what we can dump from outside the thread */
69
70 if (thr != tid)
71 return;
72
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020073 chunk_appendf(buf, " curr_task=");
74 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020075}
76
77
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020078/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020079 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020080 * with <pfx>. The buffer is only appended and the first output starts by the
81 * pointer itself. The caller is responsible for making sure the task is not
82 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020083 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020084void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020085{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020086 const struct stream *s = NULL;
87
Willy Tarreau14a1ab72019-05-17 10:34:25 +020088 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020089 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020090 return;
91 }
92
Willy Tarreau20db9112019-05-17 14:14:35 +020093 if (TASK_IS_TASKLET(task))
94 chunk_appendf(buf,
95 "%p (tasklet) calls=%u\n",
96 task,
97 task->calls);
98 else
99 chunk_appendf(buf,
100 "%p (task) calls=%u last=%llu%s\n",
101 task,
102 task->calls,
103 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
104 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200105
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200106 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200107 " fct=%p (%s) ctx=%p\n",
108 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200109 task->process,
110 task->process == process_stream ? "process_stream" :
111 task->process == task_run_applet ? "task_run_applet" :
112 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200113 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200114 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200115
116 if (task->process == process_stream && task->context)
117 s = (struct stream *)task->context;
118 else if (task->process == task_run_applet && task->context)
119 s = si_strm(((struct appctx *)task->context)->owner);
120 else if (task->process == si_cs_io_cb && task->context)
121 s = si_strm((struct stream_interface *)task->context);
122
123 if (s)
124 stream_dump(buf, s, pfx, '\n');
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200125}
126
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200127/* This function dumps all profiling settings. It returns 0 if the output
128 * buffer is full and it needs to be called again, otherwise non-zero.
129 */
130static int cli_io_handler_show_threads(struct appctx *appctx)
131{
132 struct stream_interface *si = appctx->owner;
133 int thr;
134
135 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
136 return 1;
137
138 if (appctx->st0)
139 thr = appctx->st1;
140 else
141 thr = 0;
142
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200143 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200144 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200145
146 if (ci_putchk(si_ic(si), &trash) == -1) {
147 /* failed, try again */
148 si_rx_room_blk(si);
149 appctx->st1 = thr;
150 return 0;
151 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200152 return 1;
153}
154
Willy Tarreau56131ca2019-05-20 13:48:29 +0200155/* dumps a state of all threads into the trash and on fd #2, then aborts. */
156void ha_panic()
157{
158 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200159 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200160 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200161 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200162 for (;;)
163 abort();
164}
165
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200166#if defined(DEBUG_DEV)
167/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
168static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
169{
170 int code = atoi(args[3]);
171
172 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
173 return 1;
174
175 exit(code);
176 return 1;
177}
178
179/* parse a "debug dev close" command. It always returns 1. */
180static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
181{
182 int fd;
183
184 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
185 return 1;
186
187 if (!*args[3]) {
188 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
189 goto reterr;
190 }
191
192 fd = atoi(args[3]);
193 if (fd < 0 || fd >= global.maxsock) {
194 appctx->ctx.cli.msg = "File descriptor out of range.\n";
195 goto reterr;
196 }
197
198 if (!fdtab[fd].owner) {
199 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
200 goto retinfo;
201 }
202
203 fd_delete(fd);
204 return 1;
205 retinfo:
206 appctx->ctx.cli.severity = LOG_INFO;
207 appctx->st0 = CLI_ST_PRINT;
208 return 1;
209 reterr:
210 appctx->ctx.cli.severity = LOG_ERR;
211 appctx->st0 = CLI_ST_PRINT;
212 return 1;
213}
214
215/* parse a "debug dev delay" command. It always returns 1. */
216static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
217{
218 int delay = atoi(args[3]);
219
220 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
221 return 1;
222
223 usleep((long)delay * 1000);
224 return 1;
225}
226
227/* parse a "debug dev log" command. It always returns 1. */
228static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
229{
230 int arg;
231
232 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
233 return 1;
234
235 chunk_reset(&trash);
236 for (arg = 3; *args[arg]; arg++) {
237 if (arg > 3)
238 chunk_strcat(&trash, " ");
239 chunk_strcat(&trash, args[arg]);
240 }
241
242 send_log(NULL, LOG_INFO, "%s\n", trash.area);
243 return 1;
244}
245
246/* parse a "debug dev loop" command. It always returns 1. */
247static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
248{
249 struct timeval deadline, curr;
250 int loop = atoi(args[3]);
251
252 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
253 return 1;
254
255 gettimeofday(&curr, NULL);
256 tv_ms_add(&deadline, &curr, loop);
257
258 while (tv_ms_cmp(&curr, &deadline) < 0)
259 gettimeofday(&curr, NULL);
260
261 return 1;
262}
263
264/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
265static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
266{
267 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
268 return 1;
269
270 ha_panic();
271 return 1;
272}
273
274/* parse a "debug dev exec" command. It always returns 1. */
275static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
276{
277 FILE *f;
278 int arg;
279
280 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
281 return 1;
282
283 chunk_reset(&trash);
284 for (arg = 3; *args[arg]; arg++) {
285 if (arg > 3)
286 chunk_strcat(&trash, " ");
287 chunk_strcat(&trash, args[arg]);
288 }
289
290 f = popen(trash.area, "re");
291 if (!f) {
292 appctx->ctx.cli.severity = LOG_ERR;
293 appctx->ctx.cli.msg = "Failed to execute command.\n";
294 appctx->st0 = CLI_ST_PRINT;
295 return 1;
296 }
297
298 chunk_reset(&trash);
299 while (1) {
300 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
301 if (!ret)
302 break;
303 trash.data += ret;
304 if (trash.data + 20 == trash.size) {
305 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
306 break;
307 }
308 }
309
310 fclose(f);
311 trash.area[trash.data] = 0;
312 appctx->ctx.cli.severity = LOG_INFO;
313 appctx->ctx.cli.msg = trash.area;
314 appctx->st0 = CLI_ST_PRINT;
315 return 1;
316}
317
318/* parse a "debug dev hex" command. It always returns 1. */
319static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
320{
321 unsigned long start, len;
322
323 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
324 return 1;
325
326 if (!*args[3]) {
327 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
328 goto reterr;
329 }
330
331 start = strtoul(args[3], NULL, 0);
332 if (!start) {
333 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
334 goto reterr;
335 }
336
337 /* by default, dump ~128 till next block of 16 */
338 len = strtoul(args[4], NULL, 0);
339 if (!len)
340 len = ((start + 128) & -16) - start;
341
342 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200343 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200344 trash.area[trash.data] = 0;
345 appctx->ctx.cli.severity = LOG_INFO;
346 appctx->ctx.cli.msg = trash.area;
347 appctx->st0 = CLI_ST_PRINT;
348 return 1;
349 reterr:
350 appctx->ctx.cli.severity = LOG_ERR;
351 appctx->st0 = CLI_ST_PRINT;
352 return 1;
353}
354
355/* parse a "debug dev tkill" command. It always returns 1. */
356static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
357{
358 int thr = 0;
359 int sig = SIGABRT;
360
361 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
362 return 1;
363
364 if (*args[3])
365 thr = atoi(args[3]);
366
367 if (thr < 0 || thr > global.nbthread) {
368 appctx->ctx.cli.severity = LOG_ERR;
369 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
370 appctx->st0 = CLI_ST_PRINT;
371 return 1;
372 }
373
374 if (*args[4])
375 sig = atoi(args[4]);
376
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200378 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200380 raise(sig);
381 return 1;
382}
383
384#endif
385
Willy Tarreauc7091d82019-05-17 10:08:49 +0200386#ifndef USE_THREAD_DUMP
387
388/* This function dumps all threads' state to the trash. This version is the
389 * most basic one, which doesn't inspect other threads.
390 */
391void ha_thread_dump_all_to_trash()
392{
393 unsigned int thr;
394
395 for (thr = 0; thr < global.nbthread; thr++)
396 ha_thread_dump(&trash, thr, tid);
397}
398
399#else /* below USE_THREAD_DUMP is set */
400
Willy Tarreauddd85332019-05-22 06:28:54 +0200401/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
402 * of not stopping gdb by default, so that issuing "show threads" in a process
403 * being debugged has no adverse effect.
404 */
405#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200406
407/* mask of threads still having to dump, used to respect ordering */
408static volatile unsigned long threads_to_dump;
409
410/* ID of the thread requesting the dump */
411static unsigned int thread_dump_tid;
412
413/* points to the buffer where the dump functions should write. It must
414 * have already been initialized by the requester. Nothing is done if
415 * it's NULL.
416 */
417struct buffer *thread_dump_buffer = NULL;
418
419void ha_thread_dump_all_to_trash()
420{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200421 unsigned long old;
422
423 while (1) {
424 old = 0;
425 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
426 break;
427 ha_thread_relax();
428 }
429
430 thread_dump_buffer = &trash;
431 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200432 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200433}
434
435/* handles DEBUGSIG to dump the state of the thread it's working on */
436void debug_handler(int sig, siginfo_t *si, void *arg)
437{
438 /* There are 4 phases in the dump process:
439 * 1- wait for our turn, i.e. when all lower bits are gone.
440 * 2- perform the action if our bit is set
441 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200442 * the last one and have to put them all as a signal
443 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200444 */
445
446 /* wait for all previous threads to finish first */
447 while (threads_to_dump & (tid_bit - 1))
448 ha_thread_relax();
449
450 /* dump if needed */
451 if (threads_to_dump & tid_bit) {
452 if (thread_dump_buffer)
453 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
454 if ((threads_to_dump & all_threads_mask) == tid_bit) {
455 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200456 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200457 thread_dump_buffer = NULL;
458 }
459 else
460 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
461 }
462
463 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200464 * bits again to broadcast the leaving condition so we'll see ourselves
465 * present again. This way the threads_to_dump variable never passes to
466 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200467 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200468 while (!(threads_to_dump & tid_bit))
469 ha_thread_relax();
470 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200471
472 /* mark the current thread as stuck to detect it upon next invocation
473 * if it didn't move.
474 */
475 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
476 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200477}
478
479static int init_debug_per_thread()
480{
481 sigset_t set;
482
483 /* unblock the DEBUGSIG signal we intend to use */
484 sigemptyset(&set);
485 sigaddset(&set, DEBUGSIG);
486 ha_sigmask(SIG_UNBLOCK, &set, NULL);
487 return 1;
488}
489
490static int init_debug()
491{
492 struct sigaction sa;
493
494 sa.sa_handler = NULL;
495 sa.sa_sigaction = debug_handler;
496 sigemptyset(&sa.sa_mask);
497 sa.sa_flags = SA_SIGINFO;
498 sigaction(DEBUGSIG, &sa, NULL);
499 return 0;
500}
501
502REGISTER_POST_CHECK(init_debug);
503REGISTER_PER_THREAD_INIT(init_debug_per_thread);
504
505#endif /* USE_THREAD_DUMP */
506
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200507/* register cli keywords */
508static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200509#if defined(DEBUG_DEV)
510 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
511 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
512 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
513 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
514 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
515 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
516 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
517 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
518 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
519#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200520 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
521 {{},}
522}};
523
524INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);