blob: f378cd97f882bb2198d7792c6512fa87749f2339 [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 Tarreaua37cb182019-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"
Olivier Houchard305d5ab2019-07-24 18:07:06 +020053 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020054 (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 !!(task_profiling_mask & thr_bit));
64
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020065 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020066 " harmless=%d wantrdv=%d",
67 !!(threads_harmless_mask & thr_bit),
68 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020069
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020070 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020071 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020072
73 /* this is the end of what we can dump from outside the thread */
74
75 if (thr != tid)
76 return;
77
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020078 chunk_appendf(buf, " curr_task=");
79 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020080}
81
82
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020083/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020084 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020085 * with <pfx>. The buffer is only appended and the first output starts by the
86 * pointer itself. The caller is responsible for making sure the task is not
87 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020089void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020090{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020091 const struct stream *s = NULL;
92
Willy Tarreau14a1ab72019-05-17 10:34:25 +020093 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020094 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020095 return;
96 }
97
Willy Tarreau20db9112019-05-17 14:14:35 +020098 if (TASK_IS_TASKLET(task))
99 chunk_appendf(buf,
100 "%p (tasklet) calls=%u\n",
101 task,
102 task->calls);
103 else
104 chunk_appendf(buf,
105 "%p (task) calls=%u last=%llu%s\n",
106 task,
107 task->calls,
108 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
109 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200110
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200111 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200112 " fct=%p (%s) ctx=%p\n",
113 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200114 task->process,
115 task->process == process_stream ? "process_stream" :
116 task->process == task_run_applet ? "task_run_applet" :
117 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200118 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200119 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200120
121 if (task->process == process_stream && task->context)
122 s = (struct stream *)task->context;
123 else if (task->process == task_run_applet && task->context)
124 s = si_strm(((struct appctx *)task->context)->owner);
125 else if (task->process == si_cs_io_cb && task->context)
126 s = si_strm((struct stream_interface *)task->context);
127
128 if (s)
129 stream_dump(buf, s, pfx, '\n');
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200130}
131
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200132/* This function dumps all profiling settings. It returns 0 if the output
133 * buffer is full and it needs to be called again, otherwise non-zero.
134 */
135static int cli_io_handler_show_threads(struct appctx *appctx)
136{
137 struct stream_interface *si = appctx->owner;
138 int thr;
139
140 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
141 return 1;
142
143 if (appctx->st0)
144 thr = appctx->st1;
145 else
146 thr = 0;
147
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200148 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200149 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200150
151 if (ci_putchk(si_ic(si), &trash) == -1) {
152 /* failed, try again */
153 si_rx_room_blk(si);
154 appctx->st1 = thr;
155 return 0;
156 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200157 return 1;
158}
159
Willy Tarreau56131ca2019-05-20 13:48:29 +0200160/* dumps a state of all threads into the trash and on fd #2, then aborts. */
161void ha_panic()
162{
163 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200164 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200165 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200166 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200167 for (;;)
168 abort();
169}
170
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200171#if defined(DEBUG_DEV)
172/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
173static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
174{
175 int code = atoi(args[3]);
176
177 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
178 return 1;
179
180 exit(code);
181 return 1;
182}
183
184/* parse a "debug dev close" command. It always returns 1. */
185static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
186{
187 int fd;
188
189 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
190 return 1;
191
Willy Tarreau9d008692019-08-09 11:21:01 +0200192 if (!*args[3])
193 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200194
195 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200196 if (fd < 0 || fd >= global.maxsock)
197 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200198
Willy Tarreau9d008692019-08-09 11:21:01 +0200199 if (!fdtab[fd].owner)
200 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200201
202 fd_delete(fd);
203 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200204}
205
206/* parse a "debug dev delay" command. It always returns 1. */
207static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
208{
209 int delay = atoi(args[3]);
210
211 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
212 return 1;
213
214 usleep((long)delay * 1000);
215 return 1;
216}
217
218/* parse a "debug dev log" command. It always returns 1. */
219static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
220{
221 int arg;
222
223 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
224 return 1;
225
226 chunk_reset(&trash);
227 for (arg = 3; *args[arg]; arg++) {
228 if (arg > 3)
229 chunk_strcat(&trash, " ");
230 chunk_strcat(&trash, args[arg]);
231 }
232
233 send_log(NULL, LOG_INFO, "%s\n", trash.area);
234 return 1;
235}
236
237/* parse a "debug dev loop" command. It always returns 1. */
238static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
239{
240 struct timeval deadline, curr;
241 int loop = atoi(args[3]);
242
243 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
244 return 1;
245
246 gettimeofday(&curr, NULL);
247 tv_ms_add(&deadline, &curr, loop);
248
249 while (tv_ms_cmp(&curr, &deadline) < 0)
250 gettimeofday(&curr, NULL);
251
252 return 1;
253}
254
255/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
256static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
257{
258 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
259 return 1;
260
261 ha_panic();
262 return 1;
263}
264
265/* parse a "debug dev exec" command. It always returns 1. */
266static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
267{
268 FILE *f;
269 int arg;
270
271 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
272 return 1;
273
274 chunk_reset(&trash);
275 for (arg = 3; *args[arg]; arg++) {
276 if (arg > 3)
277 chunk_strcat(&trash, " ");
278 chunk_strcat(&trash, args[arg]);
279 }
280
281 f = popen(trash.area, "re");
Willy Tarreau9d008692019-08-09 11:21:01 +0200282 if (!f)
283 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200284
285 chunk_reset(&trash);
286 while (1) {
287 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
288 if (!ret)
289 break;
290 trash.data += ret;
291 if (trash.data + 20 == trash.size) {
292 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
293 break;
294 }
295 }
296
297 fclose(f);
298 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200299 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200300}
301
302/* parse a "debug dev hex" command. It always returns 1. */
303static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
304{
305 unsigned long start, len;
306
307 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
308 return 1;
309
Willy Tarreau9d008692019-08-09 11:21:01 +0200310 if (!*args[3])
311 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200312
313 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200314 if (!start)
315 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200316
317 /* by default, dump ~128 till next block of 16 */
318 len = strtoul(args[4], NULL, 0);
319 if (!len)
320 len = ((start + 128) & -16) - start;
321
322 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200323 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200324 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200325 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200326}
327
328/* parse a "debug dev tkill" command. It always returns 1. */
329static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
330{
331 int thr = 0;
332 int sig = SIGABRT;
333
334 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
335 return 1;
336
337 if (*args[3])
338 thr = atoi(args[3]);
339
Willy Tarreau9d008692019-08-09 11:21:01 +0200340 if (thr < 0 || thr > global.nbthread)
341 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200342
343 if (*args[4])
344 sig = atoi(args[4]);
345
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200346 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200347 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200348 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200349 raise(sig);
350 return 1;
351}
352
353#endif
354
Willy Tarreauc7091d82019-05-17 10:08:49 +0200355#ifndef USE_THREAD_DUMP
356
357/* This function dumps all threads' state to the trash. This version is the
358 * most basic one, which doesn't inspect other threads.
359 */
360void ha_thread_dump_all_to_trash()
361{
362 unsigned int thr;
363
364 for (thr = 0; thr < global.nbthread; thr++)
365 ha_thread_dump(&trash, thr, tid);
366}
367
368#else /* below USE_THREAD_DUMP is set */
369
Willy Tarreauddd85332019-05-22 06:28:54 +0200370/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
371 * of not stopping gdb by default, so that issuing "show threads" in a process
372 * being debugged has no adverse effect.
373 */
374#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200375
Willy Tarreauc7091d82019-05-17 10:08:49 +0200376/* ID of the thread requesting the dump */
377static unsigned int thread_dump_tid;
378
379/* points to the buffer where the dump functions should write. It must
380 * have already been initialized by the requester. Nothing is done if
381 * it's NULL.
382 */
383struct buffer *thread_dump_buffer = NULL;
384
385void ha_thread_dump_all_to_trash()
386{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200387 unsigned long old;
388
389 while (1) {
390 old = 0;
391 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
392 break;
393 ha_thread_relax();
394 }
395
396 thread_dump_buffer = &trash;
397 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200398 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200399}
400
401/* handles DEBUGSIG to dump the state of the thread it's working on */
402void debug_handler(int sig, siginfo_t *si, void *arg)
403{
404 /* There are 4 phases in the dump process:
405 * 1- wait for our turn, i.e. when all lower bits are gone.
406 * 2- perform the action if our bit is set
407 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200408 * the last one and have to put them all as a signal
409 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200410 */
411
412 /* wait for all previous threads to finish first */
413 while (threads_to_dump & (tid_bit - 1))
414 ha_thread_relax();
415
416 /* dump if needed */
417 if (threads_to_dump & tid_bit) {
418 if (thread_dump_buffer)
419 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
420 if ((threads_to_dump & all_threads_mask) == tid_bit) {
421 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200422 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200423 thread_dump_buffer = NULL;
424 }
425 else
426 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
427 }
428
429 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200430 * bits again to broadcast the leaving condition so we'll see ourselves
431 * present again. This way the threads_to_dump variable never passes to
432 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200433 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200434 while (!(threads_to_dump & tid_bit))
435 ha_thread_relax();
436 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200437
438 /* mark the current thread as stuck to detect it upon next invocation
439 * if it didn't move.
440 */
441 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
442 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200443}
444
445static int init_debug_per_thread()
446{
447 sigset_t set;
448
449 /* unblock the DEBUGSIG signal we intend to use */
450 sigemptyset(&set);
451 sigaddset(&set, DEBUGSIG);
452 ha_sigmask(SIG_UNBLOCK, &set, NULL);
453 return 1;
454}
455
456static int init_debug()
457{
458 struct sigaction sa;
459
460 sa.sa_handler = NULL;
461 sa.sa_sigaction = debug_handler;
462 sigemptyset(&sa.sa_mask);
463 sa.sa_flags = SA_SIGINFO;
464 sigaction(DEBUGSIG, &sa, NULL);
465 return 0;
466}
467
468REGISTER_POST_CHECK(init_debug);
469REGISTER_PER_THREAD_INIT(init_debug_per_thread);
470
471#endif /* USE_THREAD_DUMP */
472
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200473/* register cli keywords */
474static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200475#if defined(DEBUG_DEV)
476 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
477 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
478 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
479 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
480 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
481 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
482 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
483 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
484 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
485#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200486 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
487 {{},}
488}};
489
490INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);