blob: 3077e97c20ecd9a4782a2dc0c2d0181c83c130b7 [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 Tarreau445b2b72019-07-31 19:20:39 +020032/* mask of threads still having to dump, used to respect ordering. Only used
33 * when USE_THREAD_DUMP is set.
34 */
35volatile unsigned long threads_to_dump = 0;
36
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020037/* Dumps to the buffer some known information for the desired thread, and
38 * optionally extra info for the current thread. The dump will be appended to
39 * the buffer, so the caller is responsible for preliminary initializing it.
40 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020041 * in front of the calling thread's line (usually it's tid). Any stuck thread
42 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020043 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020044void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020045{
46 unsigned long thr_bit = 1UL << thr;
Willy Tarreau9c8800a2019-05-20 20:52:20 +020047 unsigned long long p = thread_info[thr].prev_cpu_time;
48 unsigned long long n = now_cpu_time_thread(&thread_info[thr]);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020049 int stuck = !!(thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020050
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020051 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020052 "%c%cThread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
53 " stuck=%d fdcache=%d prof=%d",
54 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020055 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020056 !!(global_tasks_mask & thr_bit),
57 !eb_is_empty(&task_per_thread[thr].timers),
58 !eb_is_empty(&task_per_thread[thr].rqueue),
59 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
60 task_per_thread[thr].task_list_size,
61 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020062 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020063 !!(fd_cache_mask & thr_bit),
64 !!(task_profiling_mask & thr_bit));
65
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020066 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020067 " harmless=%d wantrdv=%d",
68 !!(threads_harmless_mask & thr_bit),
69 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020070
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020071 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020072 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020073
74 /* this is the end of what we can dump from outside the thread */
75
76 if (thr != tid)
77 return;
78
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020079 chunk_appendf(buf, " curr_task=");
80 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020081}
82
83
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020084/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020085 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020086 * with <pfx>. The buffer is only appended and the first output starts by the
87 * pointer itself. The caller is responsible for making sure the task is not
88 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020089 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020090void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020091{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020092 const struct stream *s = NULL;
93
Willy Tarreau14a1ab72019-05-17 10:34:25 +020094 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020095 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020096 return;
97 }
98
Willy Tarreau20db9112019-05-17 14:14:35 +020099 if (TASK_IS_TASKLET(task))
100 chunk_appendf(buf,
101 "%p (tasklet) calls=%u\n",
102 task,
103 task->calls);
104 else
105 chunk_appendf(buf,
106 "%p (task) calls=%u last=%llu%s\n",
107 task,
108 task->calls,
109 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
110 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200111
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200112 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200113 " fct=%p (%s) ctx=%p\n",
114 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200115 task->process,
116 task->process == process_stream ? "process_stream" :
117 task->process == task_run_applet ? "task_run_applet" :
118 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200119 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200120 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200121
122 if (task->process == process_stream && task->context)
123 s = (struct stream *)task->context;
124 else if (task->process == task_run_applet && task->context)
125 s = si_strm(((struct appctx *)task->context)->owner);
126 else if (task->process == si_cs_io_cb && task->context)
127 s = si_strm((struct stream_interface *)task->context);
128
129 if (s)
130 stream_dump(buf, s, pfx, '\n');
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200131}
132
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200133/* This function dumps all profiling settings. It returns 0 if the output
134 * buffer is full and it needs to be called again, otherwise non-zero.
135 */
136static int cli_io_handler_show_threads(struct appctx *appctx)
137{
138 struct stream_interface *si = appctx->owner;
139 int thr;
140
141 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
142 return 1;
143
144 if (appctx->st0)
145 thr = appctx->st1;
146 else
147 thr = 0;
148
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200149 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200150 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200151
152 if (ci_putchk(si_ic(si), &trash) == -1) {
153 /* failed, try again */
154 si_rx_room_blk(si);
155 appctx->st1 = thr;
156 return 0;
157 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200158 return 1;
159}
160
Willy Tarreau56131ca2019-05-20 13:48:29 +0200161/* dumps a state of all threads into the trash and on fd #2, then aborts. */
162void ha_panic()
163{
164 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200165 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200166 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200167 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200168 for (;;)
169 abort();
170}
171
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200172#if defined(DEBUG_DEV)
173/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
174static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
175{
176 int code = atoi(args[3]);
177
178 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
179 return 1;
180
181 exit(code);
182 return 1;
183}
184
185/* parse a "debug dev close" command. It always returns 1. */
186static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
187{
188 int fd;
189
190 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
191 return 1;
192
193 if (!*args[3]) {
194 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
195 goto reterr;
196 }
197
198 fd = atoi(args[3]);
199 if (fd < 0 || fd >= global.maxsock) {
200 appctx->ctx.cli.msg = "File descriptor out of range.\n";
201 goto reterr;
202 }
203
204 if (!fdtab[fd].owner) {
205 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
206 goto retinfo;
207 }
208
209 fd_delete(fd);
210 return 1;
211 retinfo:
212 appctx->ctx.cli.severity = LOG_INFO;
213 appctx->st0 = CLI_ST_PRINT;
214 return 1;
215 reterr:
216 appctx->ctx.cli.severity = LOG_ERR;
217 appctx->st0 = CLI_ST_PRINT;
218 return 1;
219}
220
221/* parse a "debug dev delay" command. It always returns 1. */
222static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
223{
224 int delay = atoi(args[3]);
225
226 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
227 return 1;
228
229 usleep((long)delay * 1000);
230 return 1;
231}
232
233/* parse a "debug dev log" command. It always returns 1. */
234static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
235{
236 int arg;
237
238 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
239 return 1;
240
241 chunk_reset(&trash);
242 for (arg = 3; *args[arg]; arg++) {
243 if (arg > 3)
244 chunk_strcat(&trash, " ");
245 chunk_strcat(&trash, args[arg]);
246 }
247
248 send_log(NULL, LOG_INFO, "%s\n", trash.area);
249 return 1;
250}
251
252/* parse a "debug dev loop" command. It always returns 1. */
253static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
254{
255 struct timeval deadline, curr;
256 int loop = atoi(args[3]);
257
258 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
259 return 1;
260
261 gettimeofday(&curr, NULL);
262 tv_ms_add(&deadline, &curr, loop);
263
264 while (tv_ms_cmp(&curr, &deadline) < 0)
265 gettimeofday(&curr, NULL);
266
267 return 1;
268}
269
270/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
271static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
272{
273 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
274 return 1;
275
276 ha_panic();
277 return 1;
278}
279
280/* parse a "debug dev exec" command. It always returns 1. */
281static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
282{
283 FILE *f;
284 int arg;
285
286 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
287 return 1;
288
289 chunk_reset(&trash);
290 for (arg = 3; *args[arg]; arg++) {
291 if (arg > 3)
292 chunk_strcat(&trash, " ");
293 chunk_strcat(&trash, args[arg]);
294 }
295
296 f = popen(trash.area, "re");
297 if (!f) {
298 appctx->ctx.cli.severity = LOG_ERR;
299 appctx->ctx.cli.msg = "Failed to execute command.\n";
300 appctx->st0 = CLI_ST_PRINT;
301 return 1;
302 }
303
304 chunk_reset(&trash);
305 while (1) {
306 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
307 if (!ret)
308 break;
309 trash.data += ret;
310 if (trash.data + 20 == trash.size) {
311 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
312 break;
313 }
314 }
315
316 fclose(f);
317 trash.area[trash.data] = 0;
318 appctx->ctx.cli.severity = LOG_INFO;
319 appctx->ctx.cli.msg = trash.area;
320 appctx->st0 = CLI_ST_PRINT;
321 return 1;
322}
323
324/* parse a "debug dev hex" command. It always returns 1. */
325static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
326{
327 unsigned long start, len;
328
329 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
330 return 1;
331
332 if (!*args[3]) {
333 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
334 goto reterr;
335 }
336
337 start = strtoul(args[3], NULL, 0);
338 if (!start) {
339 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
340 goto reterr;
341 }
342
343 /* by default, dump ~128 till next block of 16 */
344 len = strtoul(args[4], NULL, 0);
345 if (!len)
346 len = ((start + 128) & -16) - start;
347
348 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200349 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200350 trash.area[trash.data] = 0;
351 appctx->ctx.cli.severity = LOG_INFO;
352 appctx->ctx.cli.msg = trash.area;
353 appctx->st0 = CLI_ST_PRINT;
354 return 1;
355 reterr:
356 appctx->ctx.cli.severity = LOG_ERR;
357 appctx->st0 = CLI_ST_PRINT;
358 return 1;
359}
360
361/* parse a "debug dev tkill" command. It always returns 1. */
362static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
363{
364 int thr = 0;
365 int sig = SIGABRT;
366
367 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
368 return 1;
369
370 if (*args[3])
371 thr = atoi(args[3]);
372
373 if (thr < 0 || thr > global.nbthread) {
374 appctx->ctx.cli.severity = LOG_ERR;
375 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
376 appctx->st0 = CLI_ST_PRINT;
377 return 1;
378 }
379
380 if (*args[4])
381 sig = atoi(args[4]);
382
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200383 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200384 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200385 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200386 raise(sig);
387 return 1;
388}
389
390#endif
391
Willy Tarreauc7091d82019-05-17 10:08:49 +0200392#ifndef USE_THREAD_DUMP
393
394/* This function dumps all threads' state to the trash. This version is the
395 * most basic one, which doesn't inspect other threads.
396 */
397void ha_thread_dump_all_to_trash()
398{
399 unsigned int thr;
400
401 for (thr = 0; thr < global.nbthread; thr++)
402 ha_thread_dump(&trash, thr, tid);
403}
404
405#else /* below USE_THREAD_DUMP is set */
406
Willy Tarreauddd85332019-05-22 06:28:54 +0200407/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
408 * of not stopping gdb by default, so that issuing "show threads" in a process
409 * being debugged has no adverse effect.
410 */
411#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200412
Willy Tarreauc7091d82019-05-17 10:08:49 +0200413/* ID of the thread requesting the dump */
414static unsigned int thread_dump_tid;
415
416/* points to the buffer where the dump functions should write. It must
417 * have already been initialized by the requester. Nothing is done if
418 * it's NULL.
419 */
420struct buffer *thread_dump_buffer = NULL;
421
422void ha_thread_dump_all_to_trash()
423{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200424 unsigned long old;
425
426 while (1) {
427 old = 0;
428 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
429 break;
430 ha_thread_relax();
431 }
432
433 thread_dump_buffer = &trash;
434 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200435 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200436}
437
438/* handles DEBUGSIG to dump the state of the thread it's working on */
439void debug_handler(int sig, siginfo_t *si, void *arg)
440{
441 /* There are 4 phases in the dump process:
442 * 1- wait for our turn, i.e. when all lower bits are gone.
443 * 2- perform the action if our bit is set
444 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauda767ea2019-07-31 19:15:45 +0200445 * the last one and have to put them all as a signal
446 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200447 */
448
449 /* wait for all previous threads to finish first */
450 while (threads_to_dump & (tid_bit - 1))
451 ha_thread_relax();
452
453 /* dump if needed */
454 if (threads_to_dump & tid_bit) {
455 if (thread_dump_buffer)
456 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
457 if ((threads_to_dump & all_threads_mask) == tid_bit) {
458 /* last one */
Willy Tarreauda767ea2019-07-31 19:15:45 +0200459 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200460 thread_dump_buffer = NULL;
461 }
462 else
463 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
464 }
465
466 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauda767ea2019-07-31 19:15:45 +0200467 * bits again to broadcast the leaving condition so we'll see ourselves
468 * present again. This way the threads_to_dump variable never passes to
469 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200470 */
Willy Tarreauda767ea2019-07-31 19:15:45 +0200471 while (!(threads_to_dump & tid_bit))
472 ha_thread_relax();
473 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200474
475 /* mark the current thread as stuck to detect it upon next invocation
476 * if it didn't move.
477 */
478 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
479 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200480}
481
482static int init_debug_per_thread()
483{
484 sigset_t set;
485
486 /* unblock the DEBUGSIG signal we intend to use */
487 sigemptyset(&set);
488 sigaddset(&set, DEBUGSIG);
489 ha_sigmask(SIG_UNBLOCK, &set, NULL);
490 return 1;
491}
492
493static int init_debug()
494{
495 struct sigaction sa;
496
497 sa.sa_handler = NULL;
498 sa.sa_sigaction = debug_handler;
499 sigemptyset(&sa.sa_mask);
500 sa.sa_flags = SA_SIGINFO;
501 sigaction(DEBUGSIG, &sa, NULL);
502 return 0;
503}
504
505REGISTER_POST_CHECK(init_debug);
506REGISTER_PER_THREAD_INIT(init_debug_per_thread);
507
508#endif /* USE_THREAD_DUMP */
509
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200510/* register cli keywords */
511static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200512#if defined(DEBUG_DEV)
513 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
514 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
515 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
516 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
517 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
518 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
519 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
520 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
521 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
522#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200523 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
524 {{},}
525}};
526
527INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);