blob: 4f7e6cea74fe8b6e204911f25345d0e21386f481 [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 Tarreauf5b4e062020-03-03 15:40:23 +010013
Willy Tarreau368bff42019-12-06 17:18:28 +010014#include <fcntl.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020015#include <signal.h>
16#include <time.h>
17#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020018#include <stdlib.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010019#include <sys/types.h>
20#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020021
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020022#include <haproxy/api.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020023#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020024#include <haproxy/cli.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020025#include <haproxy/debug.h>
Willy Tarreau86416052020-06-04 09:20:54 +020026#include <haproxy/hlua.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020027#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020028#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020029#include <haproxy/thread.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020030#include <import/ist.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020032#include <haproxy/tools.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020033
Willy Tarreauf268ee82020-06-04 17:05:57 +020034#include <haproxy/global.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020035
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020036#include <haproxy/fd.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020037
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 Tarreauf0e5da22020-05-01 12:26:03 +020059 "%c%cThread %-2u: id=0x%llx 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,
Willy Tarreauff64d3b2020-05-01 11:28:49 +020062 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020063 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020064 !!(global_tasks_mask & thr_bit),
65 !eb_is_empty(&task_per_thread[thr].timers),
66 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010067 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
68 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
69 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
70 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020071 task_per_thread[thr].task_list_size,
72 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020073 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020074 !!(task_profiling_mask & thr_bit));
75
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020076 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020077 " harmless=%d wantrdv=%d",
78 !!(threads_harmless_mask & thr_bit),
79 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020080
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020081 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020082 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020083
84 /* this is the end of what we can dump from outside the thread */
85
86 if (thr != tid)
87 return;
88
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020089 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020090 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +010091
92#ifdef USE_BACKTRACE
93 if (stuck) {
94 /* We only emit the backtrace for stuck threads in order not to
95 * waste precious output buffer space with non-interesting data.
96 */
97 struct buffer bak;
98 void *callers[100];
99 int j, nptrs;
100 void *addr;
101 int dump = 0;
102
Willy Tarreau13faf162020-03-04 07:44:06 +0100103 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100104
105 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO)
106 would produce similar output to the following: */
107
108 if (nptrs)
Willy Tarreaucdd80742020-03-04 07:38:23 +0100109 chunk_appendf(buf, " call trace(%d):\n", nptrs);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100110
Willy Tarreaua91b7942020-03-04 07:39:32 +0100111 for (j = 0; j < nptrs || dump < 2; j++) {
112 if (j == nptrs && !dump) {
113 /* we failed to spot the starting point of the
114 * dump, let's start over dumping everything we
115 * have.
116 */
117 dump = 2;
118 j = 0;
119 }
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100120 bak = *buf;
121 dump_addr_and_bytes(buf, " | ", callers[j], 8);
122 addr = resolve_sym_name(buf, ": ", callers[j]);
123 if (dump == 0) {
124 /* dump not started, will start *after*
125 * ha_thread_dump_all_to_trash and ha_panic
126 */
127 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic)
128 dump = 1;
129 *buf = bak;
130 continue;
131 }
132
133 if (dump == 1) {
134 /* starting */
135 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) {
136 *buf = bak;
137 continue;
138 }
139 dump = 2;
140 }
141
142 if (dump == 2) {
143 /* dumping */
144 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_list) {
145 dump = 3;
146 *buf = bak;
147 break;
148 }
149 }
150 /* OK, line dumped */
151 chunk_appendf(buf, "\n");
152 }
153 }
154#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200155}
156
157
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200158/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200159 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200160 * with <pfx>. The buffer is only appended and the first output starts by the
161 * pointer itself. The caller is responsible for making sure the task is not
162 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200163 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200164void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200165{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200166 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200167 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200168 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200169
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200170 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200171 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200172 return;
173 }
174
Willy Tarreau20db9112019-05-17 14:14:35 +0200175 if (TASK_IS_TASKLET(task))
176 chunk_appendf(buf,
177 "%p (tasklet) calls=%u\n",
178 task,
179 task->calls);
180 else
181 chunk_appendf(buf,
182 "%p (task) calls=%u last=%llu%s\n",
183 task,
184 task->calls,
185 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
186 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200187
Willy Tarreau2e89b092020-03-03 17:13:02 +0100188 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
189 resolve_sym_name(buf, NULL, task->process);
190 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200191
Willy Tarreaua512b022019-08-21 14:12:19 +0200192 if (task->process == task_run_applet && (appctx = task->context))
193 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
194 else
195 chunk_appendf(buf, "\n");
196
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200197 if (task->process == process_stream && task->context)
198 s = (struct stream *)task->context;
199 else if (task->process == task_run_applet && task->context)
200 s = si_strm(((struct appctx *)task->context)->owner);
201 else if (task->process == si_cs_io_cb && task->context)
202 s = si_strm((struct stream_interface *)task->context);
203
204 if (s)
205 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200206
207#ifdef USE_LUA
208 hlua = NULL;
209 if (s && (hlua = s->hlua)) {
210 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
211 }
212 else if (task->process == hlua_process_task && (hlua = task->context)) {
213 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
214 }
215 else if (task->process == task_run_applet && (appctx = task->context) &&
216 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
217 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
218 }
219 else if (task->process == task_run_applet && (appctx = task->context) &&
220 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
221 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
222 }
223
224 if (hlua) {
225 luaL_traceback(hlua->T, hlua->T, NULL, 0);
226 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
227 b_putchr(buf, '\n');
228 }
229#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200230}
231
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200232/* This function dumps all profiling settings. It returns 0 if the output
233 * buffer is full and it needs to be called again, otherwise non-zero.
234 */
235static int cli_io_handler_show_threads(struct appctx *appctx)
236{
237 struct stream_interface *si = appctx->owner;
238 int thr;
239
240 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
241 return 1;
242
243 if (appctx->st0)
244 thr = appctx->st1;
245 else
246 thr = 0;
247
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200248 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200249 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200250
251 if (ci_putchk(si_ic(si), &trash) == -1) {
252 /* failed, try again */
253 si_rx_room_blk(si);
254 appctx->st1 = thr;
255 return 0;
256 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200257 return 1;
258}
259
Willy Tarreau56131ca2019-05-20 13:48:29 +0200260/* dumps a state of all threads into the trash and on fd #2, then aborts. */
261void ha_panic()
262{
263 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200264 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200265 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100266 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200267 for (;;)
268 abort();
269}
270
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200271/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
272static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
273{
274 int code = atoi(args[3]);
275
276 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
277 return 1;
278
Willy Tarreau9b013702019-10-24 18:18:02 +0200279 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200280 exit(code);
281 return 1;
282}
283
284/* parse a "debug dev close" command. It always returns 1. */
285static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
286{
287 int fd;
288
289 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
290 return 1;
291
Willy Tarreau9d008692019-08-09 11:21:01 +0200292 if (!*args[3])
293 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200294
295 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200296 if (fd < 0 || fd >= global.maxsock)
297 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200298
Willy Tarreau9d008692019-08-09 11:21:01 +0200299 if (!fdtab[fd].owner)
300 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200301
Willy Tarreau9b013702019-10-24 18:18:02 +0200302 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200303 fd_delete(fd);
304 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200305}
306
307/* parse a "debug dev delay" command. It always returns 1. */
308static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
309{
310 int delay = atoi(args[3]);
311
312 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
313 return 1;
314
Willy Tarreau9b013702019-10-24 18:18:02 +0200315 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200316 usleep((long)delay * 1000);
317 return 1;
318}
319
320/* parse a "debug dev log" command. It always returns 1. */
321static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
322{
323 int arg;
324
325 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
326 return 1;
327
Willy Tarreau9b013702019-10-24 18:18:02 +0200328 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200329 chunk_reset(&trash);
330 for (arg = 3; *args[arg]; arg++) {
331 if (arg > 3)
332 chunk_strcat(&trash, " ");
333 chunk_strcat(&trash, args[arg]);
334 }
335
336 send_log(NULL, LOG_INFO, "%s\n", trash.area);
337 return 1;
338}
339
340/* parse a "debug dev loop" command. It always returns 1. */
341static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
342{
343 struct timeval deadline, curr;
344 int loop = atoi(args[3]);
345
346 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
347 return 1;
348
Willy Tarreau9b013702019-10-24 18:18:02 +0200349 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200350 gettimeofday(&curr, NULL);
351 tv_ms_add(&deadline, &curr, loop);
352
353 while (tv_ms_cmp(&curr, &deadline) < 0)
354 gettimeofday(&curr, NULL);
355
356 return 1;
357}
358
359/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
360static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
361{
362 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
363 return 1;
364
Willy Tarreau9b013702019-10-24 18:18:02 +0200365 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200366 ha_panic();
367 return 1;
368}
369
370/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200371#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200372static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
373{
Willy Tarreau368bff42019-12-06 17:18:28 +0100374 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200375 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100376 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200377
378 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
379 return 1;
380
Willy Tarreau9b013702019-10-24 18:18:02 +0200381 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200382 chunk_reset(&trash);
383 for (arg = 3; *args[arg]; arg++) {
384 if (arg > 3)
385 chunk_strcat(&trash, " ");
386 chunk_strcat(&trash, args[arg]);
387 }
388
Willy Tarreau368bff42019-12-06 17:18:28 +0100389 thread_isolate();
390 if (pipe(pipefd) < 0)
391 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200392
Willy Tarreau368bff42019-12-06 17:18:28 +0100393 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
394 goto fail_fcntl;
395
396 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
397 goto fail_fcntl;
398
399 pid = fork();
400
401 if (pid < 0)
402 goto fail_fork;
403 else if (pid == 0) {
404 /* child */
405 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
406
407 close(0);
408 dup2(pipefd[1], 1);
409 dup2(pipefd[1], 2);
410
411 cmd[2] = trash.area;
412 execvp(cmd[0], cmd);
413 printf("execvp() failed\n");
414 exit(1);
415 }
416
417 /* parent */
418 thread_release();
419 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200420 chunk_reset(&trash);
421 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100422 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
423 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200424 break;
425 trash.data += ret;
426 if (trash.data + 20 == trash.size) {
427 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
428 break;
429 }
430 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100431 close(pipefd[0]);
432 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200433 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200434 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100435
436 fail_fork:
437 fail_fcntl:
438 close(pipefd[0]);
439 close(pipefd[1]);
440 fail_pipe:
441 thread_release();
442 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200443}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200444#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200445
446/* parse a "debug dev hex" command. It always returns 1. */
447static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
448{
449 unsigned long start, len;
450
451 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
452 return 1;
453
Willy Tarreau9d008692019-08-09 11:21:01 +0200454 if (!*args[3])
455 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200456
457 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200458 if (!start)
459 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200460
Willy Tarreau9b013702019-10-24 18:18:02 +0200461 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
462
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200463 /* by default, dump ~128 till next block of 16 */
464 len = strtoul(args[4], NULL, 0);
465 if (!len)
466 len = ((start + 128) & -16) - start;
467
468 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200469 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200470 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200471 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200472}
473
474/* parse a "debug dev tkill" command. It always returns 1. */
475static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
476{
477 int thr = 0;
478 int sig = SIGABRT;
479
480 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
481 return 1;
482
483 if (*args[3])
484 thr = atoi(args[3]);
485
Willy Tarreau9d008692019-08-09 11:21:01 +0200486 if (thr < 0 || thr > global.nbthread)
487 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200488
489 if (*args[4])
490 sig = atoi(args[4]);
491
Willy Tarreau9b013702019-10-24 18:18:02 +0200492 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200493 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200494 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200495 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200496 raise(sig);
497 return 1;
498}
499
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100500/* parse a "debug dev write" command. It always returns 1. */
501static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
502{
503 unsigned long len;
504
505 if (!*args[3])
506 return cli_err(appctx, "Missing output size.\n");
507
508 len = strtoul(args[3], NULL, 0);
509 if (len >= trash.size)
510 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
511
512 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
513
514 chunk_reset(&trash);
515 trash.data = len;
516 memset(trash.area, '.', trash.data);
517 trash.area[trash.data] = 0;
518 for (len = 64; len < trash.data; len += 64)
519 trash.area[len] = '\n';
520 return cli_msg(appctx, LOG_INFO, trash.area);
521}
522
Willy Tarreau68680bb2019-10-23 17:23:25 +0200523/* parse a "debug dev stream" command */
524/*
525 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
526 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
527 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
528 * [sif.s[=<state>]] [sib.s[=<state>]]
529 */
530static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
531{
532 struct stream *s = si_strm(appctx->owner);
533 int arg;
534 void *ptr;
535 int size;
536 const char *word, *end;
537 struct ist name;
538 char *msg = NULL;
539 char *endarg;
540 unsigned long long old, new;
541
542 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
543 return 1;
544
545 ptr = NULL; size = 0;
546
547 if (!*args[3]) {
548 return cli_err(appctx,
549 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
550 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
551 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
552 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
553 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
554 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
555 );
556 }
557
Willy Tarreau9b013702019-10-24 18:18:02 +0200558 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200559 for (arg = 3; *args[arg]; arg++) {
560 old = 0;
561 end = word = args[arg];
562 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
563 end++;
564 name = ist2(word, end - word);
565 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200566 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200567 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200568 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200569 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200570 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200571 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200572 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200573 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200574 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200575 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200576 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200577 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200578 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200579 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200580 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200581 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200582 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200583 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200584 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200585 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200586 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200587 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200588 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200589 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200590 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200591 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200592 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200593 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200594 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200595 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200596 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200597 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
598 continue;
599 } else
600 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
601
602 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200603 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200604 if (size == 8)
605 old = read_u64(ptr);
606 else if (size == 4)
607 old = read_u32(ptr);
608 else if (size == 2)
609 old = read_u16(ptr);
610 else
611 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200612 } else {
613 memprintf(&msg,
614 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
615 msg ? msg : "", ptr, (int)(end - word), word);
616 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200617 }
618
619 /* parse the new value . */
620 new = strtoll(end + 1, &endarg, 0);
621 if (end[1] && *endarg) {
622 if (strcmp(end + 1, "now") == 0)
623 new = now_ms;
624 else {
625 memprintf(&msg,
626 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
627 msg ? msg : "", end + 1, (int)(end - word), word);
628 continue;
629 }
630 }
631
632 switch (*end) {
633 case '\0': /* show */
634 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
635 new = old; // do not change the value
636 break;
637
638 case '=': /* set */
639 break;
640
641 case '^': /* XOR */
642 new = old ^ new;
643 break;
644
645 case '+': /* OR */
646 new = old | new;
647 break;
648
649 case '-': /* AND NOT */
650 new = old & ~new;
651 break;
652
653 default:
654 break;
655 }
656
657 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200658 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200659 if (size == 8)
660 write_u64(ptr, new);
661 else if (size == 4)
662 write_u32(ptr, new);
663 else if (size == 2)
664 write_u16(ptr, new);
665 else
666 *(uint8_t *)ptr = new;
667 }
668 }
669
670 if (msg && *msg)
671 return cli_dynmsg(appctx, LOG_INFO, msg);
672 return 1;
673}
674
Willy Tarreauc7091d82019-05-17 10:08:49 +0200675#ifndef USE_THREAD_DUMP
676
677/* This function dumps all threads' state to the trash. This version is the
678 * most basic one, which doesn't inspect other threads.
679 */
680void ha_thread_dump_all_to_trash()
681{
682 unsigned int thr;
683
684 for (thr = 0; thr < global.nbthread; thr++)
685 ha_thread_dump(&trash, thr, tid);
686}
687
688#else /* below USE_THREAD_DUMP is set */
689
Willy Tarreauc7091d82019-05-17 10:08:49 +0200690/* ID of the thread requesting the dump */
691static unsigned int thread_dump_tid;
692
693/* points to the buffer where the dump functions should write. It must
694 * have already been initialized by the requester. Nothing is done if
695 * it's NULL.
696 */
697struct buffer *thread_dump_buffer = NULL;
698
699void ha_thread_dump_all_to_trash()
700{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200701 unsigned long old;
702
703 while (1) {
704 old = 0;
705 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
706 break;
707 ha_thread_relax();
708 }
709
710 thread_dump_buffer = &trash;
711 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200712 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200713}
714
715/* handles DEBUGSIG to dump the state of the thread it's working on */
716void debug_handler(int sig, siginfo_t *si, void *arg)
717{
Willy Tarreau82aafc42020-03-03 08:31:34 +0100718 /* first, let's check it's really for us and that we didn't just get
719 * a spurious DEBUGSIG.
720 */
721 if (!(threads_to_dump & tid_bit))
722 return;
723
Willy Tarreauc7091d82019-05-17 10:08:49 +0200724 /* There are 4 phases in the dump process:
725 * 1- wait for our turn, i.e. when all lower bits are gone.
726 * 2- perform the action if our bit is set
727 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200728 * the last one and have to put them all as a signal
729 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200730 */
731
732 /* wait for all previous threads to finish first */
733 while (threads_to_dump & (tid_bit - 1))
734 ha_thread_relax();
735
736 /* dump if needed */
737 if (threads_to_dump & tid_bit) {
738 if (thread_dump_buffer)
739 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
740 if ((threads_to_dump & all_threads_mask) == tid_bit) {
741 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200742 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200743 thread_dump_buffer = NULL;
744 }
745 else
746 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
747 }
748
749 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200750 * bits again to broadcast the leaving condition so we'll see ourselves
751 * present again. This way the threads_to_dump variable never passes to
752 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200753 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200754 while (!(threads_to_dump & tid_bit))
755 ha_thread_relax();
756 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200757
758 /* mark the current thread as stuck to detect it upon next invocation
759 * if it didn't move.
760 */
761 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
762 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200763}
764
765static int init_debug_per_thread()
766{
767 sigset_t set;
768
769 /* unblock the DEBUGSIG signal we intend to use */
770 sigemptyset(&set);
771 sigaddset(&set, DEBUGSIG);
772 ha_sigmask(SIG_UNBLOCK, &set, NULL);
773 return 1;
774}
775
776static int init_debug()
777{
778 struct sigaction sa;
779
Willy Tarreau0214b452020-03-04 06:01:40 +0100780#ifdef USE_BACKTRACE
781 /* calling backtrace() will access libgcc at runtime. We don't want to
782 * do it after the chroot, so let's perform a first call to have it
783 * ready in memory for later use.
784 */
785 void *callers[1];
Willy Tarreau13faf162020-03-04 07:44:06 +0100786 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreau0214b452020-03-04 06:01:40 +0100787#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +0200788 sa.sa_handler = NULL;
789 sa.sa_sigaction = debug_handler;
790 sigemptyset(&sa.sa_mask);
791 sa.sa_flags = SA_SIGINFO;
792 sigaction(DEBUGSIG, &sa, NULL);
793 return 0;
794}
795
796REGISTER_POST_CHECK(init_debug);
797REGISTER_PER_THREAD_INIT(init_debug_per_thread);
798
799#endif /* USE_THREAD_DUMP */
800
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200801/* register cli keywords */
802static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200803 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
804 {{ "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 +0200805#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200806 {{ "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 +0200807#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200808 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
809 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
810 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
811 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
812 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
813 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
814 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100815 {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub24ab222019-10-24 18:03:39 +0200816 {{ "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 +0200817 {{},}
818}};
819
820INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);