blob: b35bff53b3b6dad2be2964e93f563065aa3fb8a8 [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
Christopher Faulet90bccc72020-07-24 19:08:05 +0200164 if (hlua && hlua->T) {
Christopher Faulet49de1f22021-03-24 14:52:24 +0100165 chunk_appendf(buf, "stack traceback:\n ");
166 append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
167 b_putchr(buf, '\n');
Willy Tarreau4856b362019-08-21 14:16:02 +0200168 }
Christopher Faulet90bccc72020-07-24 19:08:05 +0200169 else
170 b_putchr(buf, '\n');
Willy Tarreau4856b362019-08-21 14:16:02 +0200171#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200172}
173
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200174/* This function dumps all profiling settings. It returns 0 if the output
175 * buffer is full and it needs to be called again, otherwise non-zero.
176 */
177static int cli_io_handler_show_threads(struct appctx *appctx)
178{
179 struct stream_interface *si = appctx->owner;
180 int thr;
181
182 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
183 return 1;
184
185 if (appctx->st0)
186 thr = appctx->st1;
187 else
188 thr = 0;
189
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200190 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200191 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200192
193 if (ci_putchk(si_ic(si), &trash) == -1) {
194 /* failed, try again */
195 si_rx_room_blk(si);
196 appctx->st1 = thr;
197 return 0;
198 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200199 return 1;
200}
201
Willy Tarreau56131ca2019-05-20 13:48:29 +0200202/* dumps a state of all threads into the trash and on fd #2, then aborts. */
203void ha_panic()
204{
205 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200206 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200207 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200208 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200209 for (;;)
210 abort();
211}
212
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200213#if defined(DEBUG_DEV)
214/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
215static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
216{
217 int code = atoi(args[3]);
218
219 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
220 return 1;
221
222 exit(code);
223 return 1;
224}
225
226/* parse a "debug dev close" command. It always returns 1. */
227static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
228{
229 int fd;
230
231 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
232 return 1;
233
234 if (!*args[3]) {
235 appctx->ctx.cli.msg = "Missing file descriptor number.\n";
236 goto reterr;
237 }
238
239 fd = atoi(args[3]);
240 if (fd < 0 || fd >= global.maxsock) {
241 appctx->ctx.cli.msg = "File descriptor out of range.\n";
242 goto reterr;
243 }
244
245 if (!fdtab[fd].owner) {
246 appctx->ctx.cli.msg = "File descriptor was already closed.\n";
247 goto retinfo;
248 }
249
250 fd_delete(fd);
251 return 1;
252 retinfo:
253 appctx->ctx.cli.severity = LOG_INFO;
254 appctx->st0 = CLI_ST_PRINT;
255 return 1;
256 reterr:
257 appctx->ctx.cli.severity = LOG_ERR;
258 appctx->st0 = CLI_ST_PRINT;
259 return 1;
260}
261
262/* parse a "debug dev delay" command. It always returns 1. */
263static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
264{
265 int delay = atoi(args[3]);
266
267 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
268 return 1;
269
270 usleep((long)delay * 1000);
271 return 1;
272}
273
274/* parse a "debug dev log" command. It always returns 1. */
275static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
276{
277 int arg;
278
279 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
280 return 1;
281
282 chunk_reset(&trash);
283 for (arg = 3; *args[arg]; arg++) {
284 if (arg > 3)
285 chunk_strcat(&trash, " ");
286 chunk_strcat(&trash, args[arg]);
287 }
288
289 send_log(NULL, LOG_INFO, "%s\n", trash.area);
290 return 1;
291}
292
293/* parse a "debug dev loop" command. It always returns 1. */
294static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
295{
296 struct timeval deadline, curr;
297 int loop = atoi(args[3]);
298
299 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
300 return 1;
301
302 gettimeofday(&curr, NULL);
303 tv_ms_add(&deadline, &curr, loop);
304
305 while (tv_ms_cmp(&curr, &deadline) < 0)
306 gettimeofday(&curr, NULL);
307
308 return 1;
309}
310
311/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
312static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
313{
314 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
315 return 1;
316
317 ha_panic();
318 return 1;
319}
320
321/* parse a "debug dev exec" command. It always returns 1. */
322static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
323{
324 FILE *f;
325 int arg;
326
327 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
328 return 1;
329
330 chunk_reset(&trash);
331 for (arg = 3; *args[arg]; arg++) {
332 if (arg > 3)
333 chunk_strcat(&trash, " ");
334 chunk_strcat(&trash, args[arg]);
335 }
336
337 f = popen(trash.area, "re");
338 if (!f) {
339 appctx->ctx.cli.severity = LOG_ERR;
340 appctx->ctx.cli.msg = "Failed to execute command.\n";
341 appctx->st0 = CLI_ST_PRINT;
342 return 1;
343 }
344
345 chunk_reset(&trash);
346 while (1) {
347 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
348 if (!ret)
349 break;
350 trash.data += ret;
351 if (trash.data + 20 == trash.size) {
352 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
353 break;
354 }
355 }
356
357 fclose(f);
358 trash.area[trash.data] = 0;
359 appctx->ctx.cli.severity = LOG_INFO;
360 appctx->ctx.cli.msg = trash.area;
361 appctx->st0 = CLI_ST_PRINT;
362 return 1;
363}
364
365/* parse a "debug dev hex" command. It always returns 1. */
366static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
367{
368 unsigned long start, len;
369
370 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
371 return 1;
372
373 if (!*args[3]) {
374 appctx->ctx.cli.msg = "Missing memory address to dump from.\n";
375 goto reterr;
376 }
377
378 start = strtoul(args[3], NULL, 0);
379 if (!start) {
380 appctx->ctx.cli.msg = "Will not dump from NULL address.\n";
381 goto reterr;
382 }
383
384 /* by default, dump ~128 till next block of 16 */
385 len = strtoul(args[4], NULL, 0);
386 if (!len)
387 len = ((start + 128) & -16) - start;
388
389 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200390 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391 trash.area[trash.data] = 0;
392 appctx->ctx.cli.severity = LOG_INFO;
393 appctx->ctx.cli.msg = trash.area;
394 appctx->st0 = CLI_ST_PRINT;
395 return 1;
396 reterr:
397 appctx->ctx.cli.severity = LOG_ERR;
398 appctx->st0 = CLI_ST_PRINT;
399 return 1;
400}
401
402/* parse a "debug dev tkill" command. It always returns 1. */
403static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
404{
405 int thr = 0;
406 int sig = SIGABRT;
407
408 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
409 return 1;
410
411 if (*args[3])
412 thr = atoi(args[3]);
413
414 if (thr < 0 || thr > global.nbthread) {
415 appctx->ctx.cli.severity = LOG_ERR;
416 appctx->ctx.cli.msg = "Thread number out of range (use 0 for current).\n";
417 appctx->st0 = CLI_ST_PRINT;
418 return 1;
419 }
420
421 if (*args[4])
422 sig = atoi(args[4]);
423
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200425 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200426 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200427 raise(sig);
428 return 1;
429}
430
431#endif
432
Willy Tarreauc7091d82019-05-17 10:08:49 +0200433#ifndef USE_THREAD_DUMP
434
435/* This function dumps all threads' state to the trash. This version is the
436 * most basic one, which doesn't inspect other threads.
437 */
438void ha_thread_dump_all_to_trash()
439{
440 unsigned int thr;
441
442 for (thr = 0; thr < global.nbthread; thr++)
443 ha_thread_dump(&trash, thr, tid);
444}
445
446#else /* below USE_THREAD_DUMP is set */
447
Willy Tarreauc7091d82019-05-17 10:08:49 +0200448/* ID of the thread requesting the dump */
449static unsigned int thread_dump_tid;
450
451/* points to the buffer where the dump functions should write. It must
452 * have already been initialized by the requester. Nothing is done if
453 * it's NULL.
454 */
455struct buffer *thread_dump_buffer = NULL;
456
457void ha_thread_dump_all_to_trash()
458{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200459 unsigned long old;
460
461 while (1) {
462 old = 0;
463 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
464 break;
465 ha_thread_relax();
466 }
467
468 thread_dump_buffer = &trash;
469 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200470 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200471}
472
473/* handles DEBUGSIG to dump the state of the thread it's working on */
474void debug_handler(int sig, siginfo_t *si, void *arg)
475{
Willy Tarreaubce97b42020-03-03 08:31:34 +0100476 /* first, let's check it's really for us and that we didn't just get
477 * a spurious DEBUGSIG.
478 */
479 if (!(threads_to_dump & tid_bit))
480 return;
481
Willy Tarreauc7091d82019-05-17 10:08:49 +0200482 /* There are 4 phases in the dump process:
483 * 1- wait for our turn, i.e. when all lower bits are gone.
484 * 2- perform the action if our bit is set
485 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauda767ea2019-07-31 19:15:45 +0200486 * the last one and have to put them all as a signal
487 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200488 */
489
490 /* wait for all previous threads to finish first */
491 while (threads_to_dump & (tid_bit - 1))
492 ha_thread_relax();
493
494 /* dump if needed */
495 if (threads_to_dump & tid_bit) {
496 if (thread_dump_buffer)
497 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
498 if ((threads_to_dump & all_threads_mask) == tid_bit) {
499 /* last one */
Willy Tarreauda767ea2019-07-31 19:15:45 +0200500 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200501 thread_dump_buffer = NULL;
502 }
503 else
504 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
505 }
506
507 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauda767ea2019-07-31 19:15:45 +0200508 * bits again to broadcast the leaving condition so we'll see ourselves
509 * present again. This way the threads_to_dump variable never passes to
510 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200511 */
Willy Tarreauda767ea2019-07-31 19:15:45 +0200512 while (!(threads_to_dump & tid_bit))
513 ha_thread_relax();
514 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200515
516 /* mark the current thread as stuck to detect it upon next invocation
517 * if it didn't move.
518 */
519 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
520 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200521}
522
523static int init_debug_per_thread()
524{
525 sigset_t set;
526
527 /* unblock the DEBUGSIG signal we intend to use */
528 sigemptyset(&set);
529 sigaddset(&set, DEBUGSIG);
530 ha_sigmask(SIG_UNBLOCK, &set, NULL);
531 return 1;
532}
533
534static int init_debug()
535{
536 struct sigaction sa;
537
538 sa.sa_handler = NULL;
539 sa.sa_sigaction = debug_handler;
540 sigemptyset(&sa.sa_mask);
541 sa.sa_flags = SA_SIGINFO;
542 sigaction(DEBUGSIG, &sa, NULL);
543 return 0;
544}
545
546REGISTER_POST_CHECK(init_debug);
547REGISTER_PER_THREAD_INIT(init_debug_per_thread);
548
549#endif /* USE_THREAD_DUMP */
550
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200551/* register cli keywords */
552static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200553#if defined(DEBUG_DEV)
554 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
555 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
556 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
557 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
558 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
559 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
560 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
561 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
562 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
563#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200564 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
565 {{},}
566}};
567
568INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);