blob: 1fb186435422c29f389a402f4c4b3f34efd1ff2f [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>
Olivier Houchard75893092020-03-18 13:07:19 +010026#include <types/signal.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020027
28#include <proto/cli.h>
29#include <proto/fd.h>
Willy Tarreau4856b362019-08-21 14:16:02 +020030#include <proto/hlua.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020031#include <proto/stream_interface.h>
32#include <proto/task.h>
33
Willy Tarreau445b2b72019-07-31 19:20:39 +020034/* mask of threads still having to dump, used to respect ordering. Only used
35 * when USE_THREAD_DUMP is set.
36 */
37volatile unsigned long threads_to_dump = 0;
38
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020039/* Dumps to the buffer some known information for the desired thread, and
40 * optionally extra info for the current thread. The dump will be appended to
41 * the buffer, so the caller is responsible for preliminary initializing it.
42 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020043 * in front of the calling thread's line (usually it's tid). Any stuck thread
44 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020045 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020046void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020047{
48 unsigned long thr_bit = 1UL << thr;
Willy Tarreau9c8800a2019-05-20 20:52:20 +020049 unsigned long long p = thread_info[thr].prev_cpu_time;
50 unsigned long long n = now_cpu_time_thread(&thread_info[thr]);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020051 int stuck = !!(thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020052
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020053 chunk_appendf(buf,
Willy Tarreau4a3768b2020-05-01 12:26:03 +020054 "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020055 " stuck=%d fdcache=%d prof=%d",
56 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauab12d192020-05-01 11:28:49 +020057 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020058 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020059 !!(global_tasks_mask & thr_bit),
60 !eb_is_empty(&task_per_thread[thr].timers),
61 !eb_is_empty(&task_per_thread[thr].rqueue),
62 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
63 task_per_thread[thr].task_list_size,
64 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020065 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020066 !!(fd_cache_mask & thr_bit),
67 !!(task_profiling_mask & thr_bit));
68
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020069 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020070 " harmless=%d wantrdv=%d",
71 !!(threads_harmless_mask & thr_bit),
72 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020073
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020074 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020075 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020076
77 /* this is the end of what we can dump from outside the thread */
78
79 if (thr != tid)
80 return;
81
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020082 chunk_appendf(buf, " curr_task=");
83 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020084}
85
86
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020087/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020089 * with <pfx>. The buffer is only appended and the first output starts by the
90 * pointer itself. The caller is responsible for making sure the task is not
91 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020092 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020093void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020094{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020095 const struct stream *s = NULL;
Willy Tarreau3a761682019-08-21 14:12:19 +020096 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau4856b362019-08-21 14:16:02 +020097 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +020098
Willy Tarreau14a1ab72019-05-17 10:34:25 +020099 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200100 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200101 return;
102 }
103
Willy Tarreau20db9112019-05-17 14:14:35 +0200104 if (TASK_IS_TASKLET(task))
105 chunk_appendf(buf,
106 "%p (tasklet) calls=%u\n",
107 task,
108 task->calls);
109 else
110 chunk_appendf(buf,
111 "%p (task) calls=%u last=%llu%s\n",
112 task,
113 task->calls,
114 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
115 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200116
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200117 chunk_appendf(buf, "%s"
Willy Tarreaud4f00f72020-03-03 07:04:42 +0100118 " fct=%p=main%s%ld (%s) ctx=%p",
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200119 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200120 task->process,
Willy Tarreaud4f00f72020-03-03 07:04:42 +0100121 ((void *)task->process - (void *)main) < 0 ? "" : "+",
122 (long)((void *)task->process - (void *)main),
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200123 task->process == process_stream ? "process_stream" :
124 task->process == task_run_applet ? "task_run_applet" :
125 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4856b362019-08-21 14:16:02 +0200126#ifdef USE_LUA
127 task->process == hlua_process_task ? "hlua_process_task" :
128#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200129 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200130 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200131
Willy Tarreau3a761682019-08-21 14:12:19 +0200132 if (task->process == task_run_applet && (appctx = task->context))
133 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
134 else
135 chunk_appendf(buf, "\n");
136
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200137 if (task->process == process_stream && task->context)
138 s = (struct stream *)task->context;
139 else if (task->process == task_run_applet && task->context)
140 s = si_strm(((struct appctx *)task->context)->owner);
141 else if (task->process == si_cs_io_cb && task->context)
142 s = si_strm((struct stream_interface *)task->context);
143
144 if (s)
145 stream_dump(buf, s, pfx, '\n');
Willy Tarreau4856b362019-08-21 14:16:02 +0200146
147#ifdef USE_LUA
148 hlua = NULL;
149 if (s && (hlua = s->hlua)) {
150 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
151 }
152 else if (task->process == hlua_process_task && (hlua = task->context)) {
153 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
154 }
155 else if (task->process == task_run_applet && (appctx = task->context) &&
156 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
157 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
158 }
159 else if (task->process == task_run_applet && (appctx = task->context) &&
160 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
161 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
162 }
163
164 if (hlua) {
165 luaL_traceback(hlua->T, hlua->T, NULL, 0);
166 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
167 b_putchr(buf, '\n');
168 }
169#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170}
171
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200172/* This function dumps all profiling settings. It returns 0 if the output
173 * buffer is full and it needs to be called again, otherwise non-zero.
174 */
175static int cli_io_handler_show_threads(struct appctx *appctx)
176{
177 struct stream_interface *si = appctx->owner;
178 int thr;
179
180 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
181 return 1;
182
183 if (appctx->st0)
184 thr = appctx->st1;
185 else
186 thr = 0;
187
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200188 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200189 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200190
191 if (ci_putchk(si_ic(si), &trash) == -1) {
192 /* failed, try again */
193 si_rx_room_blk(si);
194 appctx->st1 = thr;
195 return 0;
196 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200197 return 1;
198}
199
Willy Tarreau56131ca2019-05-20 13:48:29 +0200200/* dumps a state of all threads into the trash and on fd #2, then aborts. */
201void ha_panic()
202{
203 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200204 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200205 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200206 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200207 for (;;)
208 abort();
209}
210
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200211#if defined(DEBUG_DEV)
212/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
213static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
214{
215 int code = atoi(args[3]);
216
217 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
218 return 1;
219
220 exit(code);
221 return 1;
222}
223
224/* parse a "debug dev close" command. It always returns 1. */
225static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
226{
227 int fd;
228
229 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
230 return 1;
231
232 if (!*args[3]) {
233 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
234 goto reterr;
235 }
236
237 fd = atoi(args[3]);
238 if (fd < 0 || fd >= global.maxsock) {
239 appctx->ctx.cli.msg = "File descriptor out of range.\n";
240 goto reterr;
241 }
242
243 if (!fdtab[fd].owner) {
244 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
245 goto retinfo;
246 }
247
248 fd_delete(fd);
249 return 1;
250 retinfo:
251 appctx->ctx.cli.severity = LOG_INFO;
252 appctx->st0 = CLI_ST_PRINT;
253 return 1;
254 reterr:
255 appctx->ctx.cli.severity = LOG_ERR;
256 appctx->st0 = CLI_ST_PRINT;
257 return 1;
258}
259
260/* parse a "debug dev delay" command. It always returns 1. */
261static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
262{
263 int delay = atoi(args[3]);
264
265 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
266 return 1;
267
268 usleep((long)delay * 1000);
269 return 1;
270}
271
272/* parse a "debug dev log" command. It always returns 1. */
273static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
274{
275 int arg;
276
277 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
278 return 1;
279
280 chunk_reset(&trash);
281 for (arg = 3; *args[arg]; arg++) {
282 if (arg > 3)
283 chunk_strcat(&trash, " ");
284 chunk_strcat(&trash, args[arg]);
285 }
286
287 send_log(NULL, LOG_INFO, "%s\n", trash.area);
288 return 1;
289}
290
291/* parse a "debug dev loop" command. It always returns 1. */
292static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
293{
294 struct timeval deadline, curr;
295 int loop = atoi(args[3]);
296
297 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
298 return 1;
299
300 gettimeofday(&curr, NULL);
301 tv_ms_add(&deadline, &curr, loop);
302
303 while (tv_ms_cmp(&curr, &deadline) < 0)
304 gettimeofday(&curr, NULL);
305
306 return 1;
307}
308
309/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
310static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
311{
312 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
313 return 1;
314
315 ha_panic();
316 return 1;
317}
318
319/* parse a "debug dev exec" command. It always returns 1. */
320static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
321{
322 FILE *f;
323 int arg;
324
325 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
326 return 1;
327
328 chunk_reset(&trash);
329 for (arg = 3; *args[arg]; arg++) {
330 if (arg > 3)
331 chunk_strcat(&trash, " ");
332 chunk_strcat(&trash, args[arg]);
333 }
334
335 f = popen(trash.area, "re");
336 if (!f) {
337 appctx->ctx.cli.severity = LOG_ERR;
338 appctx->ctx.cli.msg = "Failed to execute command.\n";
339 appctx->st0 = CLI_ST_PRINT;
340 return 1;
341 }
342
343 chunk_reset(&trash);
344 while (1) {
345 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
346 if (!ret)
347 break;
348 trash.data += ret;
349 if (trash.data + 20 == trash.size) {
350 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
351 break;
352 }
353 }
354
355 fclose(f);
356 trash.area[trash.data] = 0;
357 appctx->ctx.cli.severity = LOG_INFO;
358 appctx->ctx.cli.msg = trash.area;
359 appctx->st0 = CLI_ST_PRINT;
360 return 1;
361}
362
363/* parse a "debug dev hex" command. It always returns 1. */
364static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
365{
366 unsigned long start, len;
367
368 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
369 return 1;
370
371 if (!*args[3]) {
372 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
373 goto reterr;
374 }
375
376 start = strtoul(args[3], NULL, 0);
377 if (!start) {
378 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
379 goto reterr;
380 }
381
382 /* by default, dump ~128 till next block of 16 */
383 len = strtoul(args[4], NULL, 0);
384 if (!len)
385 len = ((start + 128) & -16) - start;
386
387 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200388 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200389 trash.area[trash.data] = 0;
390 appctx->ctx.cli.severity = LOG_INFO;
391 appctx->ctx.cli.msg = trash.area;
392 appctx->st0 = CLI_ST_PRINT;
393 return 1;
394 reterr:
395 appctx->ctx.cli.severity = LOG_ERR;
396 appctx->st0 = CLI_ST_PRINT;
397 return 1;
398}
399
400/* parse a "debug dev tkill" command. It always returns 1. */
401static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
402{
403 int thr = 0;
404 int sig = SIGABRT;
405
406 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
407 return 1;
408
409 if (*args[3])
410 thr = atoi(args[3]);
411
412 if (thr < 0 || thr > global.nbthread) {
413 appctx->ctx.cli.severity = LOG_ERR;
414 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
415 appctx->st0 = CLI_ST_PRINT;
416 return 1;
417 }
418
419 if (*args[4])
420 sig = atoi(args[4]);
421
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200422 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200423 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200425 raise(sig);
426 return 1;
427}
428
429#endif
430
Willy Tarreauc7091d82019-05-17 10:08:49 +0200431#ifndef USE_THREAD_DUMP
432
433/* This function dumps all threads' state to the trash. This version is the
434 * most basic one, which doesn't inspect other threads.
435 */
436void ha_thread_dump_all_to_trash()
437{
438 unsigned int thr;
439
440 for (thr = 0; thr < global.nbthread; thr++)
441 ha_thread_dump(&trash, thr, tid);
442}
443
444#else /* below USE_THREAD_DUMP is set */
445
Willy Tarreauc7091d82019-05-17 10:08:49 +0200446/* ID of the thread requesting the dump */
447static unsigned int thread_dump_tid;
448
449/* points to the buffer where the dump functions should write. It must
450 * have already been initialized by the requester. Nothing is done if
451 * it's NULL.
452 */
453struct buffer *thread_dump_buffer = NULL;
454
455void ha_thread_dump_all_to_trash()
456{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200457 unsigned long old;
458
459 while (1) {
460 old = 0;
461 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
462 break;
463 ha_thread_relax();
464 }
465
466 thread_dump_buffer = &trash;
467 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200468 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200469}
470
471/* handles DEBUGSIG to dump the state of the thread it's working on */
472void debug_handler(int sig, siginfo_t *si, void *arg)
473{
Willy Tarreaubce97b42020-03-03 08:31:34 +0100474 /* first, let's check it's really for us and that we didn't just get
475 * a spurious DEBUGSIG.
476 */
477 if (!(threads_to_dump & tid_bit))
478 return;
479
Willy Tarreauc7091d82019-05-17 10:08:49 +0200480 /* There are 4 phases in the dump process:
481 * 1- wait for our turn, i.e. when all lower bits are gone.
482 * 2- perform the action if our bit is set
483 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauda767ea2019-07-31 19:15:45 +0200484 * the last one and have to put them all as a signal
485 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200486 */
487
488 /* wait for all previous threads to finish first */
489 while (threads_to_dump & (tid_bit - 1))
490 ha_thread_relax();
491
492 /* dump if needed */
493 if (threads_to_dump & tid_bit) {
494 if (thread_dump_buffer)
495 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
496 if ((threads_to_dump & all_threads_mask) == tid_bit) {
497 /* last one */
Willy Tarreauda767ea2019-07-31 19:15:45 +0200498 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200499 thread_dump_buffer = NULL;
500 }
501 else
502 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
503 }
504
505 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauda767ea2019-07-31 19:15:45 +0200506 * bits again to broadcast the leaving condition so we'll see ourselves
507 * present again. This way the threads_to_dump variable never passes to
508 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200509 */
Willy Tarreauda767ea2019-07-31 19:15:45 +0200510 while (!(threads_to_dump & tid_bit))
511 ha_thread_relax();
512 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200513
514 /* mark the current thread as stuck to detect it upon next invocation
515 * if it didn't move.
516 */
517 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
518 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200519}
520
521static int init_debug_per_thread()
522{
523 sigset_t set;
524
525 /* unblock the DEBUGSIG signal we intend to use */
526 sigemptyset(&set);
527 sigaddset(&set, DEBUGSIG);
528 ha_sigmask(SIG_UNBLOCK, &set, NULL);
529 return 1;
530}
531
532static int init_debug()
533{
534 struct sigaction sa;
535
536 sa.sa_handler = NULL;
537 sa.sa_sigaction = debug_handler;
538 sigemptyset(&sa.sa_mask);
539 sa.sa_flags = SA_SIGINFO;
540 sigaction(DEBUGSIG, &sa, NULL);
541 return 0;
542}
543
544REGISTER_POST_CHECK(init_debug);
545REGISTER_PER_THREAD_INIT(init_debug_per_thread);
546
547#endif /* USE_THREAD_DUMP */
548
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200549/* register cli keywords */
550static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200551#if defined(DEBUG_DEV)
552 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
553 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
554 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
555 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
556 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
557 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
558 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
559 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
560 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
561#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200562 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
563 {{},}
564}};
565
566INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);