blob: e6e3047e719a243185555721efc59b0e94167f82 [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
Willy Tarreau368bff42019-12-06 17:18:28 +010013#include <fcntl.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020014#include <signal.h>
15#include <time.h>
16#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020017#include <stdlib.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010018#include <sys/types.h>
19#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020020
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020021#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020022#include <common/config.h>
23#include <common/debug.h>
24#include <common/hathreads.h>
25#include <common/initcall.h>
Willy Tarreau68680bb2019-10-23 17:23:25 +020026#include <common/ist.h>
27#include <common/net_helper.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020028#include <common/standard.h>
29
30#include <types/global.h>
31
32#include <proto/cli.h>
33#include <proto/fd.h>
Willy Tarreau78a7cb62019-08-21 14:16:02 +020034#include <proto/hlua.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020035#include <proto/stream_interface.h>
36#include <proto/task.h>
37
Willy Tarreaua37cb182019-07-31 19:20:39 +020038/* mask of threads still having to dump, used to respect ordering. Only used
39 * when USE_THREAD_DUMP is set.
40 */
41volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020042unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020043
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020044/* Dumps to the buffer some known information for the desired thread, and
45 * optionally extra info for the current thread. The dump will be appended to
46 * the buffer, so the caller is responsible for preliminary initializing it.
47 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020048 * in front of the calling thread's line (usually it's tid). Any stuck thread
49 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020050 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020051void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020052{
53 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010054 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
55 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
56 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020057
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020058 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020059 "%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 +020060 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020061 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020062 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020063 !!(global_tasks_mask & thr_bit),
64 !eb_is_empty(&task_per_thread[thr].timers),
65 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010066 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
67 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
68 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
69 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020070 task_per_thread[thr].task_list_size,
71 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020072 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020073 !!(task_profiling_mask & thr_bit));
74
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020075 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020076 " harmless=%d wantrdv=%d",
77 !!(threads_harmless_mask & thr_bit),
78 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020079
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020080 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020081 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020082
83 /* this is the end of what we can dump from outside the thread */
84
85 if (thr != tid)
86 return;
87
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020088 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020089 ha_task_dump(buf, sched->current, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020090}
91
92
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020093/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020094 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020095 * with <pfx>. The buffer is only appended and the first output starts by the
96 * pointer itself. The caller is responsible for making sure the task is not
97 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020098 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020099void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200100{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200101 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200102 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200103 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200104
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200105 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200106 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200107 return;
108 }
109
Willy Tarreau20db9112019-05-17 14:14:35 +0200110 if (TASK_IS_TASKLET(task))
111 chunk_appendf(buf,
112 "%p (tasklet) calls=%u\n",
113 task,
114 task->calls);
115 else
116 chunk_appendf(buf,
117 "%p (task) calls=%u last=%llu%s\n",
118 task,
119 task->calls,
120 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
121 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200122
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200123 chunk_appendf(buf, "%s"
Willy Tarreau516853f2020-03-03 07:04:42 +0100124 " fct=%p=main%s%ld (%s) ctx=%p",
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200125 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200126 task->process,
Willy Tarreau516853f2020-03-03 07:04:42 +0100127 ((void *)task->process - (void *)main) < 0 ? "" : "+",
128 (long)((void *)task->process - (void *)main),
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200129 task->process == process_stream ? "process_stream" :
130 task->process == task_run_applet ? "task_run_applet" :
131 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200132#ifdef USE_LUA
133 task->process == hlua_process_task ? "hlua_process_task" :
134#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200135 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200136 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200137
Willy Tarreaua512b022019-08-21 14:12:19 +0200138 if (task->process == task_run_applet && (appctx = task->context))
139 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
140 else
141 chunk_appendf(buf, "\n");
142
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200143 if (task->process == process_stream && task->context)
144 s = (struct stream *)task->context;
145 else if (task->process == task_run_applet && task->context)
146 s = si_strm(((struct appctx *)task->context)->owner);
147 else if (task->process == si_cs_io_cb && task->context)
148 s = si_strm((struct stream_interface *)task->context);
149
150 if (s)
151 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200152
153#ifdef USE_LUA
154 hlua = NULL;
155 if (s && (hlua = s->hlua)) {
156 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
157 }
158 else if (task->process == hlua_process_task && (hlua = task->context)) {
159 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
160 }
161 else if (task->process == task_run_applet && (appctx = task->context) &&
162 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
163 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
164 }
165 else if (task->process == task_run_applet && (appctx = task->context) &&
166 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
167 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
168 }
169
170 if (hlua) {
171 luaL_traceback(hlua->T, hlua->T, NULL, 0);
172 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
173 b_putchr(buf, '\n');
174 }
175#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176}
177
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200178/* This function dumps all profiling settings. It returns 0 if the output
179 * buffer is full and it needs to be called again, otherwise non-zero.
180 */
181static int cli_io_handler_show_threads(struct appctx *appctx)
182{
183 struct stream_interface *si = appctx->owner;
184 int thr;
185
186 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
187 return 1;
188
189 if (appctx->st0)
190 thr = appctx->st1;
191 else
192 thr = 0;
193
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200194 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200195 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200196
197 if (ci_putchk(si_ic(si), &trash) == -1) {
198 /* failed, try again */
199 si_rx_room_blk(si);
200 appctx->st1 = thr;
201 return 0;
202 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200203 return 1;
204}
205
Willy Tarreau56131ca2019-05-20 13:48:29 +0200206/* dumps a state of all threads into the trash and on fd #2, then aborts. */
207void ha_panic()
208{
209 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200210 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200211 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200212 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200213 for (;;)
214 abort();
215}
216
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200217/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
218static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
219{
220 int code = atoi(args[3]);
221
222 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
223 return 1;
224
Willy Tarreau9b013702019-10-24 18:18:02 +0200225 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200226 exit(code);
227 return 1;
228}
229
230/* parse a "debug dev close" command. It always returns 1. */
231static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
232{
233 int fd;
234
235 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
236 return 1;
237
Willy Tarreau9d008692019-08-09 11:21:01 +0200238 if (!*args[3])
239 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200240
241 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200242 if (fd < 0 || fd >= global.maxsock)
243 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200244
Willy Tarreau9d008692019-08-09 11:21:01 +0200245 if (!fdtab[fd].owner)
246 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200247
Willy Tarreau9b013702019-10-24 18:18:02 +0200248 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200249 fd_delete(fd);
250 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200251}
252
253/* parse a "debug dev delay" command. It always returns 1. */
254static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
255{
256 int delay = atoi(args[3]);
257
258 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
259 return 1;
260
Willy Tarreau9b013702019-10-24 18:18:02 +0200261 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200262 usleep((long)delay * 1000);
263 return 1;
264}
265
266/* parse a "debug dev log" command. It always returns 1. */
267static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
268{
269 int arg;
270
271 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
272 return 1;
273
Willy Tarreau9b013702019-10-24 18:18:02 +0200274 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200275 chunk_reset(&trash);
276 for (arg = 3; *args[arg]; arg++) {
277 if (arg > 3)
278 chunk_strcat(&trash, " ");
279 chunk_strcat(&trash, args[arg]);
280 }
281
282 send_log(NULL, LOG_INFO, "%s\n", trash.area);
283 return 1;
284}
285
286/* parse a "debug dev loop" command. It always returns 1. */
287static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
288{
289 struct timeval deadline, curr;
290 int loop = atoi(args[3]);
291
292 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
293 return 1;
294
Willy Tarreau9b013702019-10-24 18:18:02 +0200295 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200296 gettimeofday(&curr, NULL);
297 tv_ms_add(&deadline, &curr, loop);
298
299 while (tv_ms_cmp(&curr, &deadline) < 0)
300 gettimeofday(&curr, NULL);
301
302 return 1;
303}
304
305/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
306static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
307{
308 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
309 return 1;
310
Willy Tarreau9b013702019-10-24 18:18:02 +0200311 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200312 ha_panic();
313 return 1;
314}
315
316/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200317#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200318static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
319{
Willy Tarreau368bff42019-12-06 17:18:28 +0100320 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200321 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100322 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200323
324 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
325 return 1;
326
Willy Tarreau9b013702019-10-24 18:18:02 +0200327 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200328 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
Willy Tarreau368bff42019-12-06 17:18:28 +0100335 thread_isolate();
336 if (pipe(pipefd) < 0)
337 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200338
Willy Tarreau368bff42019-12-06 17:18:28 +0100339 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
340 goto fail_fcntl;
341
342 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
343 goto fail_fcntl;
344
345 pid = fork();
346
347 if (pid < 0)
348 goto fail_fork;
349 else if (pid == 0) {
350 /* child */
351 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
352
353 close(0);
354 dup2(pipefd[1], 1);
355 dup2(pipefd[1], 2);
356
357 cmd[2] = trash.area;
358 execvp(cmd[0], cmd);
359 printf("execvp() failed\n");
360 exit(1);
361 }
362
363 /* parent */
364 thread_release();
365 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200366 chunk_reset(&trash);
367 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100368 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
369 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200370 break;
371 trash.data += ret;
372 if (trash.data + 20 == trash.size) {
373 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
374 break;
375 }
376 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100377 close(pipefd[0]);
378 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200379 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200380 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100381
382 fail_fork:
383 fail_fcntl:
384 close(pipefd[0]);
385 close(pipefd[1]);
386 fail_pipe:
387 thread_release();
388 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200389}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200390#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391
392/* parse a "debug dev hex" command. It always returns 1. */
393static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
394{
395 unsigned long start, len;
396
397 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
398 return 1;
399
Willy Tarreau9d008692019-08-09 11:21:01 +0200400 if (!*args[3])
401 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200402
403 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200404 if (!start)
405 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200406
Willy Tarreau9b013702019-10-24 18:18:02 +0200407 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
408
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200409 /* by default, dump ~128 till next block of 16 */
410 len = strtoul(args[4], NULL, 0);
411 if (!len)
412 len = ((start + 128) & -16) - start;
413
414 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200415 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200416 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200417 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200418}
419
420/* parse a "debug dev tkill" command. It always returns 1. */
421static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
422{
423 int thr = 0;
424 int sig = SIGABRT;
425
426 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
427 return 1;
428
429 if (*args[3])
430 thr = atoi(args[3]);
431
Willy Tarreau9d008692019-08-09 11:21:01 +0200432 if (thr < 0 || thr > global.nbthread)
433 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200434
435 if (*args[4])
436 sig = atoi(args[4]);
437
Willy Tarreau9b013702019-10-24 18:18:02 +0200438 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200439 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200440 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200441 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200442 raise(sig);
443 return 1;
444}
445
Willy Tarreau68680bb2019-10-23 17:23:25 +0200446/* parse a "debug dev stream" command */
447/*
448 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
449 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
450 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
451 * [sif.s[=<state>]] [sib.s[=<state>]]
452 */
453static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
454{
455 struct stream *s = si_strm(appctx->owner);
456 int arg;
457 void *ptr;
458 int size;
459 const char *word, *end;
460 struct ist name;
461 char *msg = NULL;
462 char *endarg;
463 unsigned long long old, new;
464
465 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
466 return 1;
467
468 ptr = NULL; size = 0;
469
470 if (!*args[3]) {
471 return cli_err(appctx,
472 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
473 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
474 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
475 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
476 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
477 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
478 );
479 }
480
Willy Tarreau9b013702019-10-24 18:18:02 +0200481 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200482 for (arg = 3; *args[arg]; arg++) {
483 old = 0;
484 end = word = args[arg];
485 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
486 end++;
487 name = ist2(word, end - word);
488 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200489 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200490 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200491 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200492 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200493 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200494 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200495 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200496 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200497 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200498 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200499 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200500 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200501 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200502 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200503 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200504 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200505 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200506 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200507 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200508 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200509 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200510 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200511 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200512 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200513 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200514 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200515 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200516 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200517 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200518 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200519 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200520 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
521 continue;
522 } else
523 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
524
525 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200526 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200527 if (size == 8)
528 old = read_u64(ptr);
529 else if (size == 4)
530 old = read_u32(ptr);
531 else if (size == 2)
532 old = read_u16(ptr);
533 else
534 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200535 } else {
536 memprintf(&msg,
537 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
538 msg ? msg : "", ptr, (int)(end - word), word);
539 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200540 }
541
542 /* parse the new value . */
543 new = strtoll(end + 1, &endarg, 0);
544 if (end[1] && *endarg) {
545 if (strcmp(end + 1, "now") == 0)
546 new = now_ms;
547 else {
548 memprintf(&msg,
549 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
550 msg ? msg : "", end + 1, (int)(end - word), word);
551 continue;
552 }
553 }
554
555 switch (*end) {
556 case '\0': /* show */
557 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
558 new = old; // do not change the value
559 break;
560
561 case '=': /* set */
562 break;
563
564 case '^': /* XOR */
565 new = old ^ new;
566 break;
567
568 case '+': /* OR */
569 new = old | new;
570 break;
571
572 case '-': /* AND NOT */
573 new = old & ~new;
574 break;
575
576 default:
577 break;
578 }
579
580 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200581 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200582 if (size == 8)
583 write_u64(ptr, new);
584 else if (size == 4)
585 write_u32(ptr, new);
586 else if (size == 2)
587 write_u16(ptr, new);
588 else
589 *(uint8_t *)ptr = new;
590 }
591 }
592
593 if (msg && *msg)
594 return cli_dynmsg(appctx, LOG_INFO, msg);
595 return 1;
596}
597
Willy Tarreauc7091d82019-05-17 10:08:49 +0200598#ifndef USE_THREAD_DUMP
599
600/* This function dumps all threads' state to the trash. This version is the
601 * most basic one, which doesn't inspect other threads.
602 */
603void ha_thread_dump_all_to_trash()
604{
605 unsigned int thr;
606
607 for (thr = 0; thr < global.nbthread; thr++)
608 ha_thread_dump(&trash, thr, tid);
609}
610
611#else /* below USE_THREAD_DUMP is set */
612
Willy Tarreauddd85332019-05-22 06:28:54 +0200613/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
614 * of not stopping gdb by default, so that issuing "show threads" in a process
615 * being debugged has no adverse effect.
616 */
617#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200618
Willy Tarreauc7091d82019-05-17 10:08:49 +0200619/* ID of the thread requesting the dump */
620static unsigned int thread_dump_tid;
621
622/* points to the buffer where the dump functions should write. It must
623 * have already been initialized by the requester. Nothing is done if
624 * it's NULL.
625 */
626struct buffer *thread_dump_buffer = NULL;
627
628void ha_thread_dump_all_to_trash()
629{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200630 unsigned long old;
631
632 while (1) {
633 old = 0;
634 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
635 break;
636 ha_thread_relax();
637 }
638
639 thread_dump_buffer = &trash;
640 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200641 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200642}
643
644/* handles DEBUGSIG to dump the state of the thread it's working on */
645void debug_handler(int sig, siginfo_t *si, void *arg)
646{
Willy Tarreau82aafc42020-03-03 08:31:34 +0100647 /* first, let's check it's really for us and that we didn't just get
648 * a spurious DEBUGSIG.
649 */
650 if (!(threads_to_dump & tid_bit))
651 return;
652
Willy Tarreauc7091d82019-05-17 10:08:49 +0200653 /* There are 4 phases in the dump process:
654 * 1- wait for our turn, i.e. when all lower bits are gone.
655 * 2- perform the action if our bit is set
656 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200657 * the last one and have to put them all as a signal
658 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200659 */
660
661 /* wait for all previous threads to finish first */
662 while (threads_to_dump & (tid_bit - 1))
663 ha_thread_relax();
664
665 /* dump if needed */
666 if (threads_to_dump & tid_bit) {
667 if (thread_dump_buffer)
668 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
669 if ((threads_to_dump & all_threads_mask) == tid_bit) {
670 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200671 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200672 thread_dump_buffer = NULL;
673 }
674 else
675 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
676 }
677
678 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200679 * bits again to broadcast the leaving condition so we'll see ourselves
680 * present again. This way the threads_to_dump variable never passes to
681 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200682 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200683 while (!(threads_to_dump & tid_bit))
684 ha_thread_relax();
685 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200686
687 /* mark the current thread as stuck to detect it upon next invocation
688 * if it didn't move.
689 */
690 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
691 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200692}
693
694static int init_debug_per_thread()
695{
696 sigset_t set;
697
698 /* unblock the DEBUGSIG signal we intend to use */
699 sigemptyset(&set);
700 sigaddset(&set, DEBUGSIG);
701 ha_sigmask(SIG_UNBLOCK, &set, NULL);
702 return 1;
703}
704
705static int init_debug()
706{
707 struct sigaction sa;
708
709 sa.sa_handler = NULL;
710 sa.sa_sigaction = debug_handler;
711 sigemptyset(&sa.sa_mask);
712 sa.sa_flags = SA_SIGINFO;
713 sigaction(DEBUGSIG, &sa, NULL);
714 return 0;
715}
716
717REGISTER_POST_CHECK(init_debug);
718REGISTER_PER_THREAD_INIT(init_debug_per_thread);
719
720#endif /* USE_THREAD_DUMP */
721
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200722/* register cli keywords */
723static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200724 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
725 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200726#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200727 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200728#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200729 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
730 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
731 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
732 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
733 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
734 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
735 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
736 {{ "show", "threads", NULL, NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200737 {{},}
738}};
739
740INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);