blob: 717719fe6215d9308c1f5d90f15914372f2ff3f1 [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 Tarreaua512b022019-08-21 14:12:19 +0200124 " fct=%p (%s) ctx=%p",
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200125 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200126 task->process,
127 task->process == process_stream ? "process_stream" :
128 task->process == task_run_applet ? "task_run_applet" :
129 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200130#ifdef USE_LUA
131 task->process == hlua_process_task ? "hlua_process_task" :
132#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200133 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200134 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200135
Willy Tarreaua512b022019-08-21 14:12:19 +0200136 if (task->process == task_run_applet && (appctx = task->context))
137 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
138 else
139 chunk_appendf(buf, "\n");
140
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200141 if (task->process == process_stream && task->context)
142 s = (struct stream *)task->context;
143 else if (task->process == task_run_applet && task->context)
144 s = si_strm(((struct appctx *)task->context)->owner);
145 else if (task->process == si_cs_io_cb && task->context)
146 s = si_strm((struct stream_interface *)task->context);
147
148 if (s)
149 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200150
151#ifdef USE_LUA
152 hlua = NULL;
153 if (s && (hlua = s->hlua)) {
154 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
155 }
156 else if (task->process == hlua_process_task && (hlua = task->context)) {
157 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
158 }
159 else if (task->process == task_run_applet && (appctx = task->context) &&
160 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
161 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
162 }
163 else if (task->process == task_run_applet && (appctx = task->context) &&
164 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
165 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
166 }
167
168 if (hlua) {
169 luaL_traceback(hlua->T, hlua->T, NULL, 0);
170 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
171 b_putchr(buf, '\n');
172 }
173#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200174}
175
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200176/* This function dumps all profiling settings. It returns 0 if the output
177 * buffer is full and it needs to be called again, otherwise non-zero.
178 */
179static int cli_io_handler_show_threads(struct appctx *appctx)
180{
181 struct stream_interface *si = appctx->owner;
182 int thr;
183
184 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
185 return 1;
186
187 if (appctx->st0)
188 thr = appctx->st1;
189 else
190 thr = 0;
191
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200192 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200193 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200194
195 if (ci_putchk(si_ic(si), &trash) == -1) {
196 /* failed, try again */
197 si_rx_room_blk(si);
198 appctx->st1 = thr;
199 return 0;
200 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200201 return 1;
202}
203
Willy Tarreau56131ca2019-05-20 13:48:29 +0200204/* dumps a state of all threads into the trash and on fd #2, then aborts. */
205void ha_panic()
206{
207 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200208 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200209 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200210 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200211 for (;;)
212 abort();
213}
214
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200215/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
216static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
217{
218 int code = atoi(args[3]);
219
220 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
221 return 1;
222
Willy Tarreau9b013702019-10-24 18:18:02 +0200223 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200224 exit(code);
225 return 1;
226}
227
228/* parse a "debug dev close" command. It always returns 1. */
229static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
230{
231 int fd;
232
233 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
234 return 1;
235
Willy Tarreau9d008692019-08-09 11:21:01 +0200236 if (!*args[3])
237 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200238
239 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200240 if (fd < 0 || fd >= global.maxsock)
241 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200242
Willy Tarreau9d008692019-08-09 11:21:01 +0200243 if (!fdtab[fd].owner)
244 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200245
Willy Tarreau9b013702019-10-24 18:18:02 +0200246 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200247 fd_delete(fd);
248 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200249}
250
251/* parse a "debug dev delay" command. It always returns 1. */
252static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
253{
254 int delay = atoi(args[3]);
255
256 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
257 return 1;
258
Willy Tarreau9b013702019-10-24 18:18:02 +0200259 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200260 usleep((long)delay * 1000);
261 return 1;
262}
263
264/* parse a "debug dev log" command. It always returns 1. */
265static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
266{
267 int arg;
268
269 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
270 return 1;
271
Willy Tarreau9b013702019-10-24 18:18:02 +0200272 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200273 chunk_reset(&trash);
274 for (arg = 3; *args[arg]; arg++) {
275 if (arg > 3)
276 chunk_strcat(&trash, " ");
277 chunk_strcat(&trash, args[arg]);
278 }
279
280 send_log(NULL, LOG_INFO, "%s\n", trash.area);
281 return 1;
282}
283
284/* parse a "debug dev loop" command. It always returns 1. */
285static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
286{
287 struct timeval deadline, curr;
288 int loop = atoi(args[3]);
289
290 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
291 return 1;
292
Willy Tarreau9b013702019-10-24 18:18:02 +0200293 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200294 gettimeofday(&curr, NULL);
295 tv_ms_add(&deadline, &curr, loop);
296
297 while (tv_ms_cmp(&curr, &deadline) < 0)
298 gettimeofday(&curr, NULL);
299
300 return 1;
301}
302
303/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
304static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
305{
306 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
307 return 1;
308
Willy Tarreau9b013702019-10-24 18:18:02 +0200309 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200310 ha_panic();
311 return 1;
312}
313
314/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200315#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200316static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
317{
Willy Tarreau368bff42019-12-06 17:18:28 +0100318 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200319 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100320 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200321
322 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
323 return 1;
324
Willy Tarreau9b013702019-10-24 18:18:02 +0200325 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200326 chunk_reset(&trash);
327 for (arg = 3; *args[arg]; arg++) {
328 if (arg > 3)
329 chunk_strcat(&trash, " ");
330 chunk_strcat(&trash, args[arg]);
331 }
332
Willy Tarreau368bff42019-12-06 17:18:28 +0100333 thread_isolate();
334 if (pipe(pipefd) < 0)
335 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200336
Willy Tarreau368bff42019-12-06 17:18:28 +0100337 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
338 goto fail_fcntl;
339
340 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
341 goto fail_fcntl;
342
343 pid = fork();
344
345 if (pid < 0)
346 goto fail_fork;
347 else if (pid == 0) {
348 /* child */
349 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
350
351 close(0);
352 dup2(pipefd[1], 1);
353 dup2(pipefd[1], 2);
354
355 cmd[2] = trash.area;
356 execvp(cmd[0], cmd);
357 printf("execvp() failed\n");
358 exit(1);
359 }
360
361 /* parent */
362 thread_release();
363 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200364 chunk_reset(&trash);
365 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100366 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
367 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200368 break;
369 trash.data += ret;
370 if (trash.data + 20 == trash.size) {
371 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
372 break;
373 }
374 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100375 close(pipefd[0]);
376 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200378 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100379
380 fail_fork:
381 fail_fcntl:
382 close(pipefd[0]);
383 close(pipefd[1]);
384 fail_pipe:
385 thread_release();
386 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200387}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200388#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200389
390/* parse a "debug dev hex" command. It always returns 1. */
391static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
392{
393 unsigned long start, len;
394
395 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
396 return 1;
397
Willy Tarreau9d008692019-08-09 11:21:01 +0200398 if (!*args[3])
399 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200400
401 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200402 if (!start)
403 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200404
Willy Tarreau9b013702019-10-24 18:18:02 +0200405 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
406
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200407 /* by default, dump ~128 till next block of 16 */
408 len = strtoul(args[4], NULL, 0);
409 if (!len)
410 len = ((start + 128) & -16) - start;
411
412 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200413 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200414 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200415 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200416}
417
418/* parse a "debug dev tkill" command. It always returns 1. */
419static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
420{
421 int thr = 0;
422 int sig = SIGABRT;
423
424 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
425 return 1;
426
427 if (*args[3])
428 thr = atoi(args[3]);
429
Willy Tarreau9d008692019-08-09 11:21:01 +0200430 if (thr < 0 || thr > global.nbthread)
431 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200432
433 if (*args[4])
434 sig = atoi(args[4]);
435
Willy Tarreau9b013702019-10-24 18:18:02 +0200436 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200437 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200438 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200439 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200440 raise(sig);
441 return 1;
442}
443
Willy Tarreau68680bb2019-10-23 17:23:25 +0200444/* parse a "debug dev stream" command */
445/*
446 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
447 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
448 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
449 * [sif.s[=<state>]] [sib.s[=<state>]]
450 */
451static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
452{
453 struct stream *s = si_strm(appctx->owner);
454 int arg;
455 void *ptr;
456 int size;
457 const char *word, *end;
458 struct ist name;
459 char *msg = NULL;
460 char *endarg;
461 unsigned long long old, new;
462
463 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
464 return 1;
465
466 ptr = NULL; size = 0;
467
468 if (!*args[3]) {
469 return cli_err(appctx,
470 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
471 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
472 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
473 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
474 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
475 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
476 );
477 }
478
Willy Tarreau9b013702019-10-24 18:18:02 +0200479 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200480 for (arg = 3; *args[arg]; arg++) {
481 old = 0;
482 end = word = args[arg];
483 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
484 end++;
485 name = ist2(word, end - word);
486 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200487 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200488 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200489 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200490 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200491 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200492 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200493 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200494 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200495 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200496 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200497 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200498 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200499 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200500 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200501 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200502 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200503 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200504 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200505 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200506 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200507 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200508 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200509 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200510 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200511 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200512 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200513 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200514 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200515 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200516 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200517 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200518 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
519 continue;
520 } else
521 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
522
523 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200524 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200525 if (size == 8)
526 old = read_u64(ptr);
527 else if (size == 4)
528 old = read_u32(ptr);
529 else if (size == 2)
530 old = read_u16(ptr);
531 else
532 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200533 } else {
534 memprintf(&msg,
535 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
536 msg ? msg : "", ptr, (int)(end - word), word);
537 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200538 }
539
540 /* parse the new value . */
541 new = strtoll(end + 1, &endarg, 0);
542 if (end[1] && *endarg) {
543 if (strcmp(end + 1, "now") == 0)
544 new = now_ms;
545 else {
546 memprintf(&msg,
547 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
548 msg ? msg : "", end + 1, (int)(end - word), word);
549 continue;
550 }
551 }
552
553 switch (*end) {
554 case '\0': /* show */
555 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
556 new = old; // do not change the value
557 break;
558
559 case '=': /* set */
560 break;
561
562 case '^': /* XOR */
563 new = old ^ new;
564 break;
565
566 case '+': /* OR */
567 new = old | new;
568 break;
569
570 case '-': /* AND NOT */
571 new = old & ~new;
572 break;
573
574 default:
575 break;
576 }
577
578 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200579 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200580 if (size == 8)
581 write_u64(ptr, new);
582 else if (size == 4)
583 write_u32(ptr, new);
584 else if (size == 2)
585 write_u16(ptr, new);
586 else
587 *(uint8_t *)ptr = new;
588 }
589 }
590
591 if (msg && *msg)
592 return cli_dynmsg(appctx, LOG_INFO, msg);
593 return 1;
594}
595
Willy Tarreauc7091d82019-05-17 10:08:49 +0200596#ifndef USE_THREAD_DUMP
597
598/* This function dumps all threads' state to the trash. This version is the
599 * most basic one, which doesn't inspect other threads.
600 */
601void ha_thread_dump_all_to_trash()
602{
603 unsigned int thr;
604
605 for (thr = 0; thr < global.nbthread; thr++)
606 ha_thread_dump(&trash, thr, tid);
607}
608
609#else /* below USE_THREAD_DUMP is set */
610
Willy Tarreauddd85332019-05-22 06:28:54 +0200611/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
612 * of not stopping gdb by default, so that issuing "show threads" in a process
613 * being debugged has no adverse effect.
614 */
615#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200616
Willy Tarreauc7091d82019-05-17 10:08:49 +0200617/* ID of the thread requesting the dump */
618static unsigned int thread_dump_tid;
619
620/* points to the buffer where the dump functions should write. It must
621 * have already been initialized by the requester. Nothing is done if
622 * it's NULL.
623 */
624struct buffer *thread_dump_buffer = NULL;
625
626void ha_thread_dump_all_to_trash()
627{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200628 unsigned long old;
629
630 while (1) {
631 old = 0;
632 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
633 break;
634 ha_thread_relax();
635 }
636
637 thread_dump_buffer = &trash;
638 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200639 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200640}
641
642/* handles DEBUGSIG to dump the state of the thread it's working on */
643void debug_handler(int sig, siginfo_t *si, void *arg)
644{
645 /* There are 4 phases in the dump process:
646 * 1- wait for our turn, i.e. when all lower bits are gone.
647 * 2- perform the action if our bit is set
648 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200649 * the last one and have to put them all as a signal
650 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200651 */
652
653 /* wait for all previous threads to finish first */
654 while (threads_to_dump & (tid_bit - 1))
655 ha_thread_relax();
656
657 /* dump if needed */
658 if (threads_to_dump & tid_bit) {
659 if (thread_dump_buffer)
660 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
661 if ((threads_to_dump & all_threads_mask) == tid_bit) {
662 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200663 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200664 thread_dump_buffer = NULL;
665 }
666 else
667 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
668 }
669
670 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200671 * bits again to broadcast the leaving condition so we'll see ourselves
672 * present again. This way the threads_to_dump variable never passes to
673 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200674 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200675 while (!(threads_to_dump & tid_bit))
676 ha_thread_relax();
677 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200678
679 /* mark the current thread as stuck to detect it upon next invocation
680 * if it didn't move.
681 */
682 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
683 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200684}
685
686static int init_debug_per_thread()
687{
688 sigset_t set;
689
690 /* unblock the DEBUGSIG signal we intend to use */
691 sigemptyset(&set);
692 sigaddset(&set, DEBUGSIG);
693 ha_sigmask(SIG_UNBLOCK, &set, NULL);
694 return 1;
695}
696
697static int init_debug()
698{
699 struct sigaction sa;
700
701 sa.sa_handler = NULL;
702 sa.sa_sigaction = debug_handler;
703 sigemptyset(&sa.sa_mask);
704 sa.sa_flags = SA_SIGINFO;
705 sigaction(DEBUGSIG, &sa, NULL);
706 return 0;
707}
708
709REGISTER_POST_CHECK(init_debug);
710REGISTER_PER_THREAD_INIT(init_debug_per_thread);
711
712#endif /* USE_THREAD_DUMP */
713
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200714/* register cli keywords */
715static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200716 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
717 {{ "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 +0200718#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200719 {{ "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 +0200720#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200721 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
722 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
723 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
724 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
725 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
726 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
727 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
728 {{ "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 +0200729 {{},}
730}};
731
732INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);