blob: e47b85c83a65aedb8794d53700708056523e5843 [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
36 * in front of the calling thread's line (usually it's tid).
Willy Tarreau4e2b6462019-05-16 17:44:30 +020037 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020038void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020039{
40 unsigned long thr_bit = 1UL << thr;
Willy Tarreau9c8800a2019-05-20 20:52:20 +020041 unsigned long long p = thread_info[thr].prev_cpu_time;
42 unsigned long long n = now_cpu_time_thread(&thread_info[thr]);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020043
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020044 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020045 "%c Thread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
46 " fdcache=%d prof=%d",
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020047 (thr == calling_tid) ? '*' : ' ', thr + 1,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020048 !!(active_tasks_mask & thr_bit),
49 !!(global_tasks_mask & thr_bit),
50 !eb_is_empty(&task_per_thread[thr].timers),
51 !eb_is_empty(&task_per_thread[thr].rqueue),
52 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
53 task_per_thread[thr].task_list_size,
54 task_per_thread[thr].rqueue_size,
55 !!(fd_cache_mask & thr_bit),
56 !!(task_profiling_mask & thr_bit));
57
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020058 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020059 " harmless=%d wantrdv=%d",
60 !!(threads_harmless_mask & thr_bit),
61 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020062
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020063 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020064 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020065
66 /* this is the end of what we can dump from outside the thread */
67
68 if (thr != tid)
69 return;
70
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020071 chunk_appendf(buf, " curr_task=");
72 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020073}
74
75
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020076/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020077 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020078 * with <pfx>. The buffer is only appended and the first output starts by the
79 * pointer itself. The caller is responsible for making sure the task is not
80 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020081 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020082void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020083{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020084 const struct stream *s = NULL;
85
Willy Tarreau14a1ab72019-05-17 10:34:25 +020086 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020087 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020088 return;
89 }
90
Willy Tarreau20db9112019-05-17 14:14:35 +020091 if (TASK_IS_TASKLET(task))
92 chunk_appendf(buf,
93 "%p (tasklet) calls=%u\n",
94 task,
95 task->calls);
96 else
97 chunk_appendf(buf,
98 "%p (task) calls=%u last=%llu%s\n",
99 task,
100 task->calls,
101 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
102 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200103
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200104 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200105 " fct=%p (%s) ctx=%p\n",
106 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200107 task->process,
108 task->process == process_stream ? "process_stream" :
109 task->process == task_run_applet ? "task_run_applet" :
110 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200111 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200112 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200113
114 if (task->process == process_stream && task->context)
115 s = (struct stream *)task->context;
116 else if (task->process == task_run_applet && task->context)
117 s = si_strm(((struct appctx *)task->context)->owner);
118 else if (task->process == si_cs_io_cb && task->context)
119 s = si_strm((struct stream_interface *)task->context);
120
121 if (s)
122 stream_dump(buf, s, pfx, '\n');
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200123}
124
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200125/* This function dumps all profiling settings. It returns 0 if the output
126 * buffer is full and it needs to be called again, otherwise non-zero.
127 */
128static int cli_io_handler_show_threads(struct appctx *appctx)
129{
130 struct stream_interface *si = appctx->owner;
131 int thr;
132
133 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
134 return 1;
135
136 if (appctx->st0)
137 thr = appctx->st1;
138 else
139 thr = 0;
140
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200141 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200142 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200143
144 if (ci_putchk(si_ic(si), &trash) == -1) {
145 /* failed, try again */
146 si_rx_room_blk(si);
147 appctx->st1 = thr;
148 return 0;
149 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200150 return 1;
151}
152
Willy Tarreau56131ca2019-05-20 13:48:29 +0200153/* dumps a state of all threads into the trash and on fd #2, then aborts. */
154void ha_panic()
155{
156 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200157 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200158 ha_thread_dump_all_to_trash();
159 write(2, trash.area, trash.data);
160 for (;;)
161 abort();
162}
163
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200164#if defined(DEBUG_DEV)
165/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
166static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
167{
168 int code = atoi(args[3]);
169
170 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
171 return 1;
172
173 exit(code);
174 return 1;
175}
176
177/* parse a "debug dev close" command. It always returns 1. */
178static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
179{
180 int fd;
181
182 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
183 return 1;
184
185 if (!*args[3]) {
186 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
187 goto reterr;
188 }
189
190 fd = atoi(args[3]);
191 if (fd < 0 || fd >= global.maxsock) {
192 appctx->ctx.cli.msg = "File descriptor out of range.\n";
193 goto reterr;
194 }
195
196 if (!fdtab[fd].owner) {
197 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
198 goto retinfo;
199 }
200
201 fd_delete(fd);
202 return 1;
203 retinfo:
204 appctx->ctx.cli.severity = LOG_INFO;
205 appctx->st0 = CLI_ST_PRINT;
206 return 1;
207 reterr:
208 appctx->ctx.cli.severity = LOG_ERR;
209 appctx->st0 = CLI_ST_PRINT;
210 return 1;
211}
212
213/* parse a "debug dev delay" command. It always returns 1. */
214static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
215{
216 int delay = atoi(args[3]);
217
218 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
219 return 1;
220
221 usleep((long)delay * 1000);
222 return 1;
223}
224
225/* parse a "debug dev log" command. It always returns 1. */
226static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
227{
228 int arg;
229
230 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
231 return 1;
232
233 chunk_reset(&trash);
234 for (arg = 3; *args[arg]; arg++) {
235 if (arg > 3)
236 chunk_strcat(&trash, " ");
237 chunk_strcat(&trash, args[arg]);
238 }
239
240 send_log(NULL, LOG_INFO, "%s\n", trash.area);
241 return 1;
242}
243
244/* parse a "debug dev loop" command. It always returns 1. */
245static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
246{
247 struct timeval deadline, curr;
248 int loop = atoi(args[3]);
249
250 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
251 return 1;
252
253 gettimeofday(&curr, NULL);
254 tv_ms_add(&deadline, &curr, loop);
255
256 while (tv_ms_cmp(&curr, &deadline) < 0)
257 gettimeofday(&curr, NULL);
258
259 return 1;
260}
261
262/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
263static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
264{
265 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
266 return 1;
267
268 ha_panic();
269 return 1;
270}
271
272/* parse a "debug dev exec" command. It always returns 1. */
273static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
274{
275 FILE *f;
276 int arg;
277
278 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
279 return 1;
280
281 chunk_reset(&trash);
282 for (arg = 3; *args[arg]; arg++) {
283 if (arg > 3)
284 chunk_strcat(&trash, " ");
285 chunk_strcat(&trash, args[arg]);
286 }
287
288 f = popen(trash.area, "re");
289 if (!f) {
290 appctx->ctx.cli.severity = LOG_ERR;
291 appctx->ctx.cli.msg = "Failed to execute command.\n";
292 appctx->st0 = CLI_ST_PRINT;
293 return 1;
294 }
295
296 chunk_reset(&trash);
297 while (1) {
298 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
299 if (!ret)
300 break;
301 trash.data += ret;
302 if (trash.data + 20 == trash.size) {
303 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
304 break;
305 }
306 }
307
308 fclose(f);
309 trash.area[trash.data] = 0;
310 appctx->ctx.cli.severity = LOG_INFO;
311 appctx->ctx.cli.msg = trash.area;
312 appctx->st0 = CLI_ST_PRINT;
313 return 1;
314}
315
316/* parse a "debug dev hex" command. It always returns 1. */
317static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
318{
319 unsigned long start, len;
320
321 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
322 return 1;
323
324 if (!*args[3]) {
325 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
326 goto reterr;
327 }
328
329 start = strtoul(args[3], NULL, 0);
330 if (!start) {
331 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
332 goto reterr;
333 }
334
335 /* by default, dump ~128 till next block of 16 */
336 len = strtoul(args[4], NULL, 0);
337 if (!len)
338 len = ((start + 128) & -16) - start;
339
340 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200341 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200342 trash.area[trash.data] = 0;
343 appctx->ctx.cli.severity = LOG_INFO;
344 appctx->ctx.cli.msg = trash.area;
345 appctx->st0 = CLI_ST_PRINT;
346 return 1;
347 reterr:
348 appctx->ctx.cli.severity = LOG_ERR;
349 appctx->st0 = CLI_ST_PRINT;
350 return 1;
351}
352
353/* parse a "debug dev tkill" command. It always returns 1. */
354static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
355{
356 int thr = 0;
357 int sig = SIGABRT;
358
359 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
360 return 1;
361
362 if (*args[3])
363 thr = atoi(args[3]);
364
365 if (thr < 0 || thr > global.nbthread) {
366 appctx->ctx.cli.severity = LOG_ERR;
367 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
368 appctx->st0 = CLI_ST_PRINT;
369 return 1;
370 }
371
372 if (*args[4])
373 sig = atoi(args[4]);
374
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200375 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200376 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200378 raise(sig);
379 return 1;
380}
381
382#endif
383
Willy Tarreauc7091d82019-05-17 10:08:49 +0200384#ifndef USE_THREAD_DUMP
385
386/* This function dumps all threads' state to the trash. This version is the
387 * most basic one, which doesn't inspect other threads.
388 */
389void ha_thread_dump_all_to_trash()
390{
391 unsigned int thr;
392
393 for (thr = 0; thr < global.nbthread; thr++)
394 ha_thread_dump(&trash, thr, tid);
395}
396
397#else /* below USE_THREAD_DUMP is set */
398
Willy Tarreauddd85332019-05-22 06:28:54 +0200399/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
400 * of not stopping gdb by default, so that issuing "show threads" in a process
401 * being debugged has no adverse effect.
402 */
403#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200404
405/* mask of threads still having to dump, used to respect ordering */
406static volatile unsigned long threads_to_dump;
407
408/* ID of the thread requesting the dump */
409static unsigned int thread_dump_tid;
410
411/* points to the buffer where the dump functions should write. It must
412 * have already been initialized by the requester. Nothing is done if
413 * it's NULL.
414 */
415struct buffer *thread_dump_buffer = NULL;
416
417void ha_thread_dump_all_to_trash()
418{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200419 unsigned long old;
420
421 while (1) {
422 old = 0;
423 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
424 break;
425 ha_thread_relax();
426 }
427
428 thread_dump_buffer = &trash;
429 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200430 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200431}
432
433/* handles DEBUGSIG to dump the state of the thread it's working on */
434void debug_handler(int sig, siginfo_t *si, void *arg)
435{
436 /* There are 4 phases in the dump process:
437 * 1- wait for our turn, i.e. when all lower bits are gone.
438 * 2- perform the action if our bit is set
439 * 3- remove our bit to let the next one go, unless we're
440 * the last one and have to put them all but ours
441 * 4- wait for zero and clear our bit if it's set
442 */
443
444 /* wait for all previous threads to finish first */
445 while (threads_to_dump & (tid_bit - 1))
446 ha_thread_relax();
447
448 /* dump if needed */
449 if (threads_to_dump & tid_bit) {
450 if (thread_dump_buffer)
451 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
452 if ((threads_to_dump & all_threads_mask) == tid_bit) {
453 /* last one */
454 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask & ~tid_bit);
455 thread_dump_buffer = NULL;
456 }
457 else
458 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
459 }
460
461 /* now wait for all others to finish dumping. The last one will set all
462 * bits again to broadcast the leaving condition.
463 */
464 while (threads_to_dump & all_threads_mask) {
465 if (threads_to_dump & tid_bit)
466 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
467 else
468 ha_thread_relax();
469 }
470}
471
472static int init_debug_per_thread()
473{
474 sigset_t set;
475
476 /* unblock the DEBUGSIG signal we intend to use */
477 sigemptyset(&set);
478 sigaddset(&set, DEBUGSIG);
479 ha_sigmask(SIG_UNBLOCK, &set, NULL);
480 return 1;
481}
482
483static int init_debug()
484{
485 struct sigaction sa;
486
487 sa.sa_handler = NULL;
488 sa.sa_sigaction = debug_handler;
489 sigemptyset(&sa.sa_mask);
490 sa.sa_flags = SA_SIGINFO;
491 sigaction(DEBUGSIG, &sa, NULL);
492 return 0;
493}
494
495REGISTER_POST_CHECK(init_debug);
496REGISTER_PER_THREAD_INIT(init_debug_per_thread);
497
498#endif /* USE_THREAD_DUMP */
499
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200500/* register cli keywords */
501static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200502#if defined(DEBUG_DEV)
503 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
504 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
505 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
506 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
507 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
508 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
509 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
510 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
511 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
512#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200513 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
514 {{},}
515}};
516
517INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);