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