blob: 751d26be66fa3fdc1ba7316742a5c1363e41b56f [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 Tarreau14a1ab72019-05-17 10:34:25 +020084 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020085 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020086 return;
87 }
88
Willy Tarreau20db9112019-05-17 14:14:35 +020089 if (TASK_IS_TASKLET(task))
90 chunk_appendf(buf,
91 "%p (tasklet) calls=%u\n",
92 task,
93 task->calls);
94 else
95 chunk_appendf(buf,
96 "%p (task) calls=%u last=%llu%s\n",
97 task,
98 task->calls,
99 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
100 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200101
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200102 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200103 " fct=%p (%s) ctx=%p\n",
104 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200105 task->process,
106 task->process == process_stream ? "process_stream" :
107 task->process == task_run_applet ? "task_run_applet" :
108 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200109 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200110 task->context);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200111}
112
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200113/* This function dumps all profiling settings. It returns 0 if the output
114 * buffer is full and it needs to be called again, otherwise non-zero.
115 */
116static int cli_io_handler_show_threads(struct appctx *appctx)
117{
118 struct stream_interface *si = appctx->owner;
119 int thr;
120
121 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
122 return 1;
123
124 if (appctx->st0)
125 thr = appctx->st1;
126 else
127 thr = 0;
128
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200129 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200130 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200131
132 if (ci_putchk(si_ic(si), &trash) == -1) {
133 /* failed, try again */
134 si_rx_room_blk(si);
135 appctx->st1 = thr;
136 return 0;
137 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200138 return 1;
139}
140
Willy Tarreau56131ca2019-05-20 13:48:29 +0200141/* dumps a state of all threads into the trash and on fd #2, then aborts. */
142void ha_panic()
143{
144 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200145 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200146 ha_thread_dump_all_to_trash();
147 write(2, trash.area, trash.data);
148 for (;;)
149 abort();
150}
151
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200152#if defined(DEBUG_DEV)
153/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
154static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
155{
156 int code = atoi(args[3]);
157
158 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
159 return 1;
160
161 exit(code);
162 return 1;
163}
164
165/* parse a "debug dev close" command. It always returns 1. */
166static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
167{
168 int fd;
169
170 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
171 return 1;
172
173 if (!*args[3]) {
174 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
175 goto reterr;
176 }
177
178 fd = atoi(args[3]);
179 if (fd < 0 || fd >= global.maxsock) {
180 appctx->ctx.cli.msg = "File descriptor out of range.\n";
181 goto reterr;
182 }
183
184 if (!fdtab[fd].owner) {
185 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
186 goto retinfo;
187 }
188
189 fd_delete(fd);
190 return 1;
191 retinfo:
192 appctx->ctx.cli.severity = LOG_INFO;
193 appctx->st0 = CLI_ST_PRINT;
194 return 1;
195 reterr:
196 appctx->ctx.cli.severity = LOG_ERR;
197 appctx->st0 = CLI_ST_PRINT;
198 return 1;
199}
200
201/* parse a "debug dev delay" command. It always returns 1. */
202static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
203{
204 int delay = atoi(args[3]);
205
206 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
207 return 1;
208
209 usleep((long)delay * 1000);
210 return 1;
211}
212
213/* parse a "debug dev log" command. It always returns 1. */
214static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
215{
216 int arg;
217
218 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
219 return 1;
220
221 chunk_reset(&trash);
222 for (arg = 3; *args[arg]; arg++) {
223 if (arg > 3)
224 chunk_strcat(&trash, " ");
225 chunk_strcat(&trash, args[arg]);
226 }
227
228 send_log(NULL, LOG_INFO, "%s\n", trash.area);
229 return 1;
230}
231
232/* parse a "debug dev loop" command. It always returns 1. */
233static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
234{
235 struct timeval deadline, curr;
236 int loop = atoi(args[3]);
237
238 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
239 return 1;
240
241 gettimeofday(&curr, NULL);
242 tv_ms_add(&deadline, &curr, loop);
243
244 while (tv_ms_cmp(&curr, &deadline) < 0)
245 gettimeofday(&curr, NULL);
246
247 return 1;
248}
249
250/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
251static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
252{
253 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
254 return 1;
255
256 ha_panic();
257 return 1;
258}
259
260/* parse a "debug dev exec" command. It always returns 1. */
261static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
262{
263 FILE *f;
264 int arg;
265
266 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
267 return 1;
268
269 chunk_reset(&trash);
270 for (arg = 3; *args[arg]; arg++) {
271 if (arg > 3)
272 chunk_strcat(&trash, " ");
273 chunk_strcat(&trash, args[arg]);
274 }
275
276 f = popen(trash.area, "re");
277 if (!f) {
278 appctx->ctx.cli.severity = LOG_ERR;
279 appctx->ctx.cli.msg = "Failed to execute command.\n";
280 appctx->st0 = CLI_ST_PRINT;
281 return 1;
282 }
283
284 chunk_reset(&trash);
285 while (1) {
286 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
287 if (!ret)
288 break;
289 trash.data += ret;
290 if (trash.data + 20 == trash.size) {
291 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
292 break;
293 }
294 }
295
296 fclose(f);
297 trash.area[trash.data] = 0;
298 appctx->ctx.cli.severity = LOG_INFO;
299 appctx->ctx.cli.msg = trash.area;
300 appctx->st0 = CLI_ST_PRINT;
301 return 1;
302}
303
304/* parse a "debug dev hex" command. It always returns 1. */
305static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
306{
307 unsigned long start, len;
308
309 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
310 return 1;
311
312 if (!*args[3]) {
313 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
314 goto reterr;
315 }
316
317 start = strtoul(args[3], NULL, 0);
318 if (!start) {
319 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
320 goto reterr;
321 }
322
323 /* by default, dump ~128 till next block of 16 */
324 len = strtoul(args[4], NULL, 0);
325 if (!len)
326 len = ((start + 128) & -16) - start;
327
328 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200329 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200330 trash.area[trash.data] = 0;
331 appctx->ctx.cli.severity = LOG_INFO;
332 appctx->ctx.cli.msg = trash.area;
333 appctx->st0 = CLI_ST_PRINT;
334 return 1;
335 reterr:
336 appctx->ctx.cli.severity = LOG_ERR;
337 appctx->st0 = CLI_ST_PRINT;
338 return 1;
339}
340
341/* parse a "debug dev tkill" command. It always returns 1. */
342static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
343{
344 int thr = 0;
345 int sig = SIGABRT;
346
347 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
348 return 1;
349
350 if (*args[3])
351 thr = atoi(args[3]);
352
353 if (thr < 0 || thr > global.nbthread) {
354 appctx->ctx.cli.severity = LOG_ERR;
355 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
356 appctx->st0 = CLI_ST_PRINT;
357 return 1;
358 }
359
360 if (*args[4])
361 sig = atoi(args[4]);
362
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200364 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200365 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200366 raise(sig);
367 return 1;
368}
369
370#endif
371
Willy Tarreauc7091d82019-05-17 10:08:49 +0200372#ifndef USE_THREAD_DUMP
373
374/* This function dumps all threads' state to the trash. This version is the
375 * most basic one, which doesn't inspect other threads.
376 */
377void ha_thread_dump_all_to_trash()
378{
379 unsigned int thr;
380
381 for (thr = 0; thr < global.nbthread; thr++)
382 ha_thread_dump(&trash, thr, tid);
383}
384
385#else /* below USE_THREAD_DUMP is set */
386
Willy Tarreauddd85332019-05-22 06:28:54 +0200387/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
388 * of not stopping gdb by default, so that issuing "show threads" in a process
389 * being debugged has no adverse effect.
390 */
391#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200392
393/* mask of threads still having to dump, used to respect ordering */
394static volatile unsigned long threads_to_dump;
395
396/* ID of the thread requesting the dump */
397static unsigned int thread_dump_tid;
398
399/* points to the buffer where the dump functions should write. It must
400 * have already been initialized by the requester. Nothing is done if
401 * it's NULL.
402 */
403struct buffer *thread_dump_buffer = NULL;
404
405void ha_thread_dump_all_to_trash()
406{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200407 unsigned long old;
408
409 while (1) {
410 old = 0;
411 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
412 break;
413 ha_thread_relax();
414 }
415
416 thread_dump_buffer = &trash;
417 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200418 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200419}
420
421/* handles DEBUGSIG to dump the state of the thread it's working on */
422void debug_handler(int sig, siginfo_t *si, void *arg)
423{
424 /* There are 4 phases in the dump process:
425 * 1- wait for our turn, i.e. when all lower bits are gone.
426 * 2- perform the action if our bit is set
427 * 3- remove our bit to let the next one go, unless we're
428 * the last one and have to put them all but ours
429 * 4- wait for zero and clear our bit if it's set
430 */
431
432 /* wait for all previous threads to finish first */
433 while (threads_to_dump & (tid_bit - 1))
434 ha_thread_relax();
435
436 /* dump if needed */
437 if (threads_to_dump & tid_bit) {
438 if (thread_dump_buffer)
439 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
440 if ((threads_to_dump & all_threads_mask) == tid_bit) {
441 /* last one */
442 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask & ~tid_bit);
443 thread_dump_buffer = NULL;
444 }
445 else
446 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
447 }
448
449 /* now wait for all others to finish dumping. The last one will set all
450 * bits again to broadcast the leaving condition.
451 */
452 while (threads_to_dump & all_threads_mask) {
453 if (threads_to_dump & tid_bit)
454 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
455 else
456 ha_thread_relax();
457 }
458}
459
460static int init_debug_per_thread()
461{
462 sigset_t set;
463
464 /* unblock the DEBUGSIG signal we intend to use */
465 sigemptyset(&set);
466 sigaddset(&set, DEBUGSIG);
467 ha_sigmask(SIG_UNBLOCK, &set, NULL);
468 return 1;
469}
470
471static int init_debug()
472{
473 struct sigaction sa;
474
475 sa.sa_handler = NULL;
476 sa.sa_sigaction = debug_handler;
477 sigemptyset(&sa.sa_mask);
478 sa.sa_flags = SA_SIGINFO;
479 sigaction(DEBUGSIG, &sa, NULL);
480 return 0;
481}
482
483REGISTER_POST_CHECK(init_debug);
484REGISTER_PER_THREAD_INIT(init_debug_per_thread);
485
486#endif /* USE_THREAD_DUMP */
487
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200488/* register cli keywords */
489static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200490#if defined(DEBUG_DEV)
491 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
492 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
493 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
494 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
495 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
496 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
497 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
498 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
499 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
500#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200501 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
502 {{},}
503}};
504
505INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);