blob: 41fa7ae4a16f65bbed237bd5767f3db58bb264a6 [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),
Olivier Houchard06910462019-10-11 16:35:01 +020066 !(LIST_ISEMPTY(&task_per_thread[thr].task_list) |
67 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020068 task_per_thread[thr].task_list_size,
69 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020070 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020071 !!(task_profiling_mask & thr_bit));
72
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020073 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020074 " harmless=%d wantrdv=%d",
75 !!(threads_harmless_mask & thr_bit),
76 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020077
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020078 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020079 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020080
81 /* this is the end of what we can dump from outside the thread */
82
83 if (thr != tid)
84 return;
85
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020086 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020087 ha_task_dump(buf, sched->current, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088}
89
90
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020091/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020092 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020093 * with <pfx>. The buffer is only appended and the first output starts by the
94 * pointer itself. The caller is responsible for making sure the task is not
95 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020096 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020097void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020098{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020099 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200100 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200101 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200102
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200103 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200104 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200105 return;
106 }
107
Willy Tarreau20db9112019-05-17 14:14:35 +0200108 if (TASK_IS_TASKLET(task))
109 chunk_appendf(buf,
110 "%p (tasklet) calls=%u\n",
111 task,
112 task->calls);
113 else
114 chunk_appendf(buf,
115 "%p (task) calls=%u last=%llu%s\n",
116 task,
117 task->calls,
118 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
119 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200120
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200121 chunk_appendf(buf, "%s"
Willy Tarreaua512b022019-08-21 14:12:19 +0200122 " fct=%p (%s) ctx=%p",
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200123 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200124 task->process,
125 task->process == process_stream ? "process_stream" :
126 task->process == task_run_applet ? "task_run_applet" :
127 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200128#ifdef USE_LUA
129 task->process == hlua_process_task ? "hlua_process_task" :
130#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200131 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200132 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200133
Willy Tarreaua512b022019-08-21 14:12:19 +0200134 if (task->process == task_run_applet && (appctx = task->context))
135 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
136 else
137 chunk_appendf(buf, "\n");
138
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200139 if (task->process == process_stream && task->context)
140 s = (struct stream *)task->context;
141 else if (task->process == task_run_applet && task->context)
142 s = si_strm(((struct appctx *)task->context)->owner);
143 else if (task->process == si_cs_io_cb && task->context)
144 s = si_strm((struct stream_interface *)task->context);
145
146 if (s)
147 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200148
149#ifdef USE_LUA
150 hlua = NULL;
151 if (s && (hlua = s->hlua)) {
152 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
153 }
154 else if (task->process == hlua_process_task && (hlua = task->context)) {
155 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
156 }
157 else if (task->process == task_run_applet && (appctx = task->context) &&
158 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
159 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
160 }
161 else if (task->process == task_run_applet && (appctx = task->context) &&
162 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
163 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
164 }
165
166 if (hlua) {
167 luaL_traceback(hlua->T, hlua->T, NULL, 0);
168 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
169 b_putchr(buf, '\n');
170 }
171#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/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
214static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
215{
216 int code = atoi(args[3]);
217
218 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
219 return 1;
220
Willy Tarreau9b013702019-10-24 18:18:02 +0200221 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200222 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
Willy Tarreau9d008692019-08-09 11:21:01 +0200234 if (!*args[3])
235 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200236
237 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200238 if (fd < 0 || fd >= global.maxsock)
239 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200240
Willy Tarreau9d008692019-08-09 11:21:01 +0200241 if (!fdtab[fd].owner)
242 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200243
Willy Tarreau9b013702019-10-24 18:18:02 +0200244 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200245 fd_delete(fd);
246 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200247}
248
249/* parse a "debug dev delay" command. It always returns 1. */
250static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
251{
252 int delay = atoi(args[3]);
253
254 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
255 return 1;
256
Willy Tarreau9b013702019-10-24 18:18:02 +0200257 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200258 usleep((long)delay * 1000);
259 return 1;
260}
261
262/* parse a "debug dev log" command. It always returns 1. */
263static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
264{
265 int arg;
266
267 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
268 return 1;
269
Willy Tarreau9b013702019-10-24 18:18:02 +0200270 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200271 chunk_reset(&trash);
272 for (arg = 3; *args[arg]; arg++) {
273 if (arg > 3)
274 chunk_strcat(&trash, " ");
275 chunk_strcat(&trash, args[arg]);
276 }
277
278 send_log(NULL, LOG_INFO, "%s\n", trash.area);
279 return 1;
280}
281
282/* parse a "debug dev loop" command. It always returns 1. */
283static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
284{
285 struct timeval deadline, curr;
286 int loop = atoi(args[3]);
287
288 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
289 return 1;
290
Willy Tarreau9b013702019-10-24 18:18:02 +0200291 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200292 gettimeofday(&curr, NULL);
293 tv_ms_add(&deadline, &curr, loop);
294
295 while (tv_ms_cmp(&curr, &deadline) < 0)
296 gettimeofday(&curr, NULL);
297
298 return 1;
299}
300
301/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
302static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
303{
304 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
305 return 1;
306
Willy Tarreau9b013702019-10-24 18:18:02 +0200307 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200308 ha_panic();
309 return 1;
310}
311
312/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200313#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200314static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
315{
Willy Tarreau368bff42019-12-06 17:18:28 +0100316 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200317 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100318 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200319
320 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
321 return 1;
322
Willy Tarreau9b013702019-10-24 18:18:02 +0200323 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200324 chunk_reset(&trash);
325 for (arg = 3; *args[arg]; arg++) {
326 if (arg > 3)
327 chunk_strcat(&trash, " ");
328 chunk_strcat(&trash, args[arg]);
329 }
330
Willy Tarreau368bff42019-12-06 17:18:28 +0100331 thread_isolate();
332 if (pipe(pipefd) < 0)
333 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200334
Willy Tarreau368bff42019-12-06 17:18:28 +0100335 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
336 goto fail_fcntl;
337
338 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
339 goto fail_fcntl;
340
341 pid = fork();
342
343 if (pid < 0)
344 goto fail_fork;
345 else if (pid == 0) {
346 /* child */
347 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
348
349 close(0);
350 dup2(pipefd[1], 1);
351 dup2(pipefd[1], 2);
352
353 cmd[2] = trash.area;
354 execvp(cmd[0], cmd);
355 printf("execvp() failed\n");
356 exit(1);
357 }
358
359 /* parent */
360 thread_release();
361 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200362 chunk_reset(&trash);
363 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100364 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
365 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200366 break;
367 trash.data += ret;
368 if (trash.data + 20 == trash.size) {
369 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
370 break;
371 }
372 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100373 close(pipefd[0]);
374 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200375 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200376 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100377
378 fail_fork:
379 fail_fcntl:
380 close(pipefd[0]);
381 close(pipefd[1]);
382 fail_pipe:
383 thread_release();
384 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200385}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200386#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200387
388/* parse a "debug dev hex" command. It always returns 1. */
389static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
390{
391 unsigned long start, len;
392
393 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
394 return 1;
395
Willy Tarreau9d008692019-08-09 11:21:01 +0200396 if (!*args[3])
397 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200398
399 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200400 if (!start)
401 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200402
Willy Tarreau9b013702019-10-24 18:18:02 +0200403 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
404
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200405 /* by default, dump ~128 till next block of 16 */
406 len = strtoul(args[4], NULL, 0);
407 if (!len)
408 len = ((start + 128) & -16) - start;
409
410 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200411 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200412 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200413 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200414}
415
416/* parse a "debug dev tkill" command. It always returns 1. */
417static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
418{
419 int thr = 0;
420 int sig = SIGABRT;
421
422 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
423 return 1;
424
425 if (*args[3])
426 thr = atoi(args[3]);
427
Willy Tarreau9d008692019-08-09 11:21:01 +0200428 if (thr < 0 || thr > global.nbthread)
429 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200430
431 if (*args[4])
432 sig = atoi(args[4]);
433
Willy Tarreau9b013702019-10-24 18:18:02 +0200434 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200435 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200436 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200437 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200438 raise(sig);
439 return 1;
440}
441
Willy Tarreau68680bb2019-10-23 17:23:25 +0200442/* parse a "debug dev stream" command */
443/*
444 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
445 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
446 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
447 * [sif.s[=<state>]] [sib.s[=<state>]]
448 */
449static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
450{
451 struct stream *s = si_strm(appctx->owner);
452 int arg;
453 void *ptr;
454 int size;
455 const char *word, *end;
456 struct ist name;
457 char *msg = NULL;
458 char *endarg;
459 unsigned long long old, new;
460
461 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
462 return 1;
463
464 ptr = NULL; size = 0;
465
466 if (!*args[3]) {
467 return cli_err(appctx,
468 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
469 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
470 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
471 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
472 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
473 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
474 );
475 }
476
Willy Tarreau9b013702019-10-24 18:18:02 +0200477 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200478 for (arg = 3; *args[arg]; arg++) {
479 old = 0;
480 end = word = args[arg];
481 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
482 end++;
483 name = ist2(word, end - word);
484 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200485 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200486 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200487 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200488 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200489 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200490 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200491 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200492 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200493 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200494 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200495 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200496 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200497 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200498 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200499 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200500 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200501 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200502 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200503 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200504 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200505 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200506 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200507 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200508 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200509 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200510 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200511 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200512 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200513 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200514 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200515 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200516 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
517 continue;
518 } else
519 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
520
521 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200522 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200523 if (size == 8)
524 old = read_u64(ptr);
525 else if (size == 4)
526 old = read_u32(ptr);
527 else if (size == 2)
528 old = read_u16(ptr);
529 else
530 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200531 } else {
532 memprintf(&msg,
533 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
534 msg ? msg : "", ptr, (int)(end - word), word);
535 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200536 }
537
538 /* parse the new value . */
539 new = strtoll(end + 1, &endarg, 0);
540 if (end[1] && *endarg) {
541 if (strcmp(end + 1, "now") == 0)
542 new = now_ms;
543 else {
544 memprintf(&msg,
545 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
546 msg ? msg : "", end + 1, (int)(end - word), word);
547 continue;
548 }
549 }
550
551 switch (*end) {
552 case '\0': /* show */
553 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
554 new = old; // do not change the value
555 break;
556
557 case '=': /* set */
558 break;
559
560 case '^': /* XOR */
561 new = old ^ new;
562 break;
563
564 case '+': /* OR */
565 new = old | new;
566 break;
567
568 case '-': /* AND NOT */
569 new = old & ~new;
570 break;
571
572 default:
573 break;
574 }
575
576 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200577 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200578 if (size == 8)
579 write_u64(ptr, new);
580 else if (size == 4)
581 write_u32(ptr, new);
582 else if (size == 2)
583 write_u16(ptr, new);
584 else
585 *(uint8_t *)ptr = new;
586 }
587 }
588
589 if (msg && *msg)
590 return cli_dynmsg(appctx, LOG_INFO, msg);
591 return 1;
592}
593
Willy Tarreauc7091d82019-05-17 10:08:49 +0200594#ifndef USE_THREAD_DUMP
595
596/* This function dumps all threads' state to the trash. This version is the
597 * most basic one, which doesn't inspect other threads.
598 */
599void ha_thread_dump_all_to_trash()
600{
601 unsigned int thr;
602
603 for (thr = 0; thr < global.nbthread; thr++)
604 ha_thread_dump(&trash, thr, tid);
605}
606
607#else /* below USE_THREAD_DUMP is set */
608
Willy Tarreauddd85332019-05-22 06:28:54 +0200609/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
610 * of not stopping gdb by default, so that issuing "show threads" in a process
611 * being debugged has no adverse effect.
612 */
613#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200614
Willy Tarreauc7091d82019-05-17 10:08:49 +0200615/* ID of the thread requesting the dump */
616static unsigned int thread_dump_tid;
617
618/* points to the buffer where the dump functions should write. It must
619 * have already been initialized by the requester. Nothing is done if
620 * it's NULL.
621 */
622struct buffer *thread_dump_buffer = NULL;
623
624void ha_thread_dump_all_to_trash()
625{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200626 unsigned long old;
627
628 while (1) {
629 old = 0;
630 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
631 break;
632 ha_thread_relax();
633 }
634
635 thread_dump_buffer = &trash;
636 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200637 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200638}
639
640/* handles DEBUGSIG to dump the state of the thread it's working on */
641void debug_handler(int sig, siginfo_t *si, void *arg)
642{
643 /* There are 4 phases in the dump process:
644 * 1- wait for our turn, i.e. when all lower bits are gone.
645 * 2- perform the action if our bit is set
646 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200647 * the last one and have to put them all as a signal
648 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200649 */
650
651 /* wait for all previous threads to finish first */
652 while (threads_to_dump & (tid_bit - 1))
653 ha_thread_relax();
654
655 /* dump if needed */
656 if (threads_to_dump & tid_bit) {
657 if (thread_dump_buffer)
658 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
659 if ((threads_to_dump & all_threads_mask) == tid_bit) {
660 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200661 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200662 thread_dump_buffer = NULL;
663 }
664 else
665 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
666 }
667
668 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200669 * bits again to broadcast the leaving condition so we'll see ourselves
670 * present again. This way the threads_to_dump variable never passes to
671 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200672 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200673 while (!(threads_to_dump & tid_bit))
674 ha_thread_relax();
675 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200676
677 /* mark the current thread as stuck to detect it upon next invocation
678 * if it didn't move.
679 */
680 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
681 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200682}
683
684static int init_debug_per_thread()
685{
686 sigset_t set;
687
688 /* unblock the DEBUGSIG signal we intend to use */
689 sigemptyset(&set);
690 sigaddset(&set, DEBUGSIG);
691 ha_sigmask(SIG_UNBLOCK, &set, NULL);
692 return 1;
693}
694
695static int init_debug()
696{
697 struct sigaction sa;
698
699 sa.sa_handler = NULL;
700 sa.sa_sigaction = debug_handler;
701 sigemptyset(&sa.sa_mask);
702 sa.sa_flags = SA_SIGINFO;
703 sigaction(DEBUGSIG, &sa, NULL);
704 return 0;
705}
706
707REGISTER_POST_CHECK(init_debug);
708REGISTER_PER_THREAD_INIT(init_debug_per_thread);
709
710#endif /* USE_THREAD_DUMP */
711
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200712/* register cli keywords */
713static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200714 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
715 {{ "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 +0200716#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200717 {{ "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 +0200718#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200719 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
720 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
721 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
722 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
723 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
724 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
725 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
726 {{ "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 +0200727 {{},}
728}};
729
730INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);