blob: ba668781e9b5bfa9c4d57040756577e7aa233b24 [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
58#ifdef USE_THREAD
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020059 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020060 " harmless=%d wantrdv=%d",
61 !!(threads_harmless_mask & thr_bit),
62 !!(threads_want_rdv_mask & thr_bit));
63#endif
64
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 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 Tarreau4e2b6462019-05-16 17:44:30 +0200113}
114
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200115/* This function dumps all profiling settings. It returns 0 if the output
116 * buffer is full and it needs to be called again, otherwise non-zero.
117 */
118static int cli_io_handler_show_threads(struct appctx *appctx)
119{
120 struct stream_interface *si = appctx->owner;
121 int thr;
122
123 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
124 return 1;
125
126 if (appctx->st0)
127 thr = appctx->st1;
128 else
129 thr = 0;
130
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200131 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200132 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200133
134 if (ci_putchk(si_ic(si), &trash) == -1) {
135 /* failed, try again */
136 si_rx_room_blk(si);
137 appctx->st1 = thr;
138 return 0;
139 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200140 return 1;
141}
142
Willy Tarreau56131ca2019-05-20 13:48:29 +0200143/* dumps a state of all threads into the trash and on fd #2, then aborts. */
144void ha_panic()
145{
146 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200147 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200148 ha_thread_dump_all_to_trash();
149 write(2, trash.area, trash.data);
150 for (;;)
151 abort();
152}
153
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200154#if defined(DEBUG_DEV)
155/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
156static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
157{
158 int code = atoi(args[3]);
159
160 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
161 return 1;
162
163 exit(code);
164 return 1;
165}
166
167/* parse a "debug dev close" command. It always returns 1. */
168static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
169{
170 int fd;
171
172 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
173 return 1;
174
175 if (!*args[3]) {
176 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
177 goto reterr;
178 }
179
180 fd = atoi(args[3]);
181 if (fd < 0 || fd >= global.maxsock) {
182 appctx->ctx.cli.msg = "File descriptor out of range.\n";
183 goto reterr;
184 }
185
186 if (!fdtab[fd].owner) {
187 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
188 goto retinfo;
189 }
190
191 fd_delete(fd);
192 return 1;
193 retinfo:
194 appctx->ctx.cli.severity = LOG_INFO;
195 appctx->st0 = CLI_ST_PRINT;
196 return 1;
197 reterr:
198 appctx->ctx.cli.severity = LOG_ERR;
199 appctx->st0 = CLI_ST_PRINT;
200 return 1;
201}
202
203/* parse a "debug dev delay" command. It always returns 1. */
204static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
205{
206 int delay = atoi(args[3]);
207
208 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
209 return 1;
210
211 usleep((long)delay * 1000);
212 return 1;
213}
214
215/* parse a "debug dev log" command. It always returns 1. */
216static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
217{
218 int arg;
219
220 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
221 return 1;
222
223 chunk_reset(&trash);
224 for (arg = 3; *args[arg]; arg++) {
225 if (arg > 3)
226 chunk_strcat(&trash, " ");
227 chunk_strcat(&trash, args[arg]);
228 }
229
230 send_log(NULL, LOG_INFO, "%s\n", trash.area);
231 return 1;
232}
233
234/* parse a "debug dev loop" command. It always returns 1. */
235static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
236{
237 struct timeval deadline, curr;
238 int loop = atoi(args[3]);
239
240 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
241 return 1;
242
243 gettimeofday(&curr, NULL);
244 tv_ms_add(&deadline, &curr, loop);
245
246 while (tv_ms_cmp(&curr, &deadline) < 0)
247 gettimeofday(&curr, NULL);
248
249 return 1;
250}
251
252/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
253static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
254{
255 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
256 return 1;
257
258 ha_panic();
259 return 1;
260}
261
262/* parse a "debug dev exec" command. It always returns 1. */
263static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
264{
265 FILE *f;
266 int arg;
267
268 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
269 return 1;
270
271 chunk_reset(&trash);
272 for (arg = 3; *args[arg]; arg++) {
273 if (arg > 3)
274 chunk_strcat(&trash, " ");
275 chunk_strcat(&trash, args[arg]);
276 }
277
278 f = popen(trash.area, "re");
279 if (!f) {
280 appctx->ctx.cli.severity = LOG_ERR;
281 appctx->ctx.cli.msg = "Failed to execute command.\n";
282 appctx->st0 = CLI_ST_PRINT;
283 return 1;
284 }
285
286 chunk_reset(&trash);
287 while (1) {
288 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
289 if (!ret)
290 break;
291 trash.data += ret;
292 if (trash.data + 20 == trash.size) {
293 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
294 break;
295 }
296 }
297
298 fclose(f);
299 trash.area[trash.data] = 0;
300 appctx->ctx.cli.severity = LOG_INFO;
301 appctx->ctx.cli.msg = trash.area;
302 appctx->st0 = CLI_ST_PRINT;
303 return 1;
304}
305
306/* parse a "debug dev hex" command. It always returns 1. */
307static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
308{
309 unsigned long start, len;
310
311 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
312 return 1;
313
314 if (!*args[3]) {
315 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
316 goto reterr;
317 }
318
319 start = strtoul(args[3], NULL, 0);
320 if (!start) {
321 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
322 goto reterr;
323 }
324
325 /* by default, dump ~128 till next block of 16 */
326 len = strtoul(args[4], NULL, 0);
327 if (!len)
328 len = ((start + 128) & -16) - start;
329
330 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200331 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200332 trash.area[trash.data] = 0;
333 appctx->ctx.cli.severity = LOG_INFO;
334 appctx->ctx.cli.msg = trash.area;
335 appctx->st0 = CLI_ST_PRINT;
336 return 1;
337 reterr:
338 appctx->ctx.cli.severity = LOG_ERR;
339 appctx->st0 = CLI_ST_PRINT;
340 return 1;
341}
342
343/* parse a "debug dev tkill" command. It always returns 1. */
344static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
345{
346 int thr = 0;
347 int sig = SIGABRT;
348
349 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
350 return 1;
351
352 if (*args[3])
353 thr = atoi(args[3]);
354
355 if (thr < 0 || thr > global.nbthread) {
356 appctx->ctx.cli.severity = LOG_ERR;
357 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
358 appctx->st0 = CLI_ST_PRINT;
359 return 1;
360 }
361
362 if (*args[4])
363 sig = atoi(args[4]);
364
365#if defined(USE_THREAD)
366 if (thr)
367 pthread_kill(thread_info[thr-1].pthread, sig);
368 else
369#endif
370 raise(sig);
371 return 1;
372}
373
374#endif
375
Willy Tarreauc7091d82019-05-17 10:08:49 +0200376#ifndef USE_THREAD_DUMP
377
378/* This function dumps all threads' state to the trash. This version is the
379 * most basic one, which doesn't inspect other threads.
380 */
381void ha_thread_dump_all_to_trash()
382{
383 unsigned int thr;
384
385 for (thr = 0; thr < global.nbthread; thr++)
386 ha_thread_dump(&trash, thr, tid);
387}
388
389#else /* below USE_THREAD_DUMP is set */
390
391/* The signal to trigger a debug dump on a thread is SIGPWR */
392#define DEBUGSIG SIGPWR
393
394/* mask of threads still having to dump, used to respect ordering */
395static volatile unsigned long threads_to_dump;
396
397/* ID of the thread requesting the dump */
398static unsigned int thread_dump_tid;
399
400/* points to the buffer where the dump functions should write. It must
401 * have already been initialized by the requester. Nothing is done if
402 * it's NULL.
403 */
404struct buffer *thread_dump_buffer = NULL;
405
406void ha_thread_dump_all_to_trash()
407{
408 __maybe_unused unsigned int thr;
409 unsigned long old;
410
411 while (1) {
412 old = 0;
413 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
414 break;
415 ha_thread_relax();
416 }
417
418 thread_dump_buffer = &trash;
419 thread_dump_tid = tid;
420
421#ifdef USE_THREAD
422 for (thr = 0; thr < global.nbthread; thr++) {
423 if (thr != tid)
Willy Tarreau522cfbc2019-05-03 10:16:39 +0200424 pthread_kill(thread_info[thr].pthread, DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200425 }
426#endif
427 /* dump ourselves last */
428 raise(DEBUGSIG);
429}
430
431/* handles DEBUGSIG to dump the state of the thread it's working on */
432void debug_handler(int sig, siginfo_t *si, void *arg)
433{
434 /* There are 4 phases in the dump process:
435 * 1- wait for our turn, i.e. when all lower bits are gone.
436 * 2- perform the action if our bit is set
437 * 3- remove our bit to let the next one go, unless we're
438 * the last one and have to put them all but ours
439 * 4- wait for zero and clear our bit if it's set
440 */
441
442 /* wait for all previous threads to finish first */
443 while (threads_to_dump & (tid_bit - 1))
444 ha_thread_relax();
445
446 /* dump if needed */
447 if (threads_to_dump & tid_bit) {
448 if (thread_dump_buffer)
449 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
450 if ((threads_to_dump & all_threads_mask) == tid_bit) {
451 /* last one */
452 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask & ~tid_bit);
453 thread_dump_buffer = NULL;
454 }
455 else
456 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
457 }
458
459 /* now wait for all others to finish dumping. The last one will set all
460 * bits again to broadcast the leaving condition.
461 */
462 while (threads_to_dump & all_threads_mask) {
463 if (threads_to_dump & tid_bit)
464 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
465 else
466 ha_thread_relax();
467 }
468}
469
470static int init_debug_per_thread()
471{
472 sigset_t set;
473
474 /* unblock the DEBUGSIG signal we intend to use */
475 sigemptyset(&set);
476 sigaddset(&set, DEBUGSIG);
477 ha_sigmask(SIG_UNBLOCK, &set, NULL);
478 return 1;
479}
480
481static int init_debug()
482{
483 struct sigaction sa;
484
485 sa.sa_handler = NULL;
486 sa.sa_sigaction = debug_handler;
487 sigemptyset(&sa.sa_mask);
488 sa.sa_flags = SA_SIGINFO;
489 sigaction(DEBUGSIG, &sa, NULL);
490 return 0;
491}
492
493REGISTER_POST_CHECK(init_debug);
494REGISTER_PER_THREAD_INIT(init_debug_per_thread);
495
496#endif /* USE_THREAD_DUMP */
497
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200498/* register cli keywords */
499static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200500#if defined(DEBUG_DEV)
501 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
502 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
503 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
504 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
505 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
506 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
507 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
508 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
509 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
510#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200511 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
512 {{},}
513}};
514
515INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);