blob: fe2fe310c91639de099c6b09d321170847630d67 [file] [log] [blame]
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001/*
2 * Process debugging functions.
3 *
4 * Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <signal.h>
14#include <time.h>
15#include <stdio.h>
Willy Tarreau6bdf3e92019-05-20 14:25:05 +020016#include <stdlib.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020017
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020018#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020019#include <common/config.h>
20#include <common/debug.h>
21#include <common/hathreads.h>
22#include <common/initcall.h>
Willy Tarreau68680bb2019-10-23 17:23:25 +020023#include <common/ist.h>
24#include <common/net_helper.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020025#include <common/standard.h>
26
27#include <types/global.h>
28
29#include <proto/cli.h>
30#include <proto/fd.h>
Willy Tarreau78a7cb62019-08-21 14:16:02 +020031#include <proto/hlua.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020032#include <proto/stream_interface.h>
33#include <proto/task.h>
34
Willy Tarreaua37cb182019-07-31 19:20:39 +020035/* mask of threads still having to dump, used to respect ordering. Only used
36 * when USE_THREAD_DUMP is set.
37 */
38volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020039unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020040
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020041/* Dumps to the buffer some known information for the desired thread, and
42 * optionally extra info for the current thread. The dump will be appended to
43 * the buffer, so the caller is responsible for preliminary initializing it.
44 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020045 * in front of the calling thread's line (usually it's tid). Any stuck thread
46 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020047 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020048void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020049{
50 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010051 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
52 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
53 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020054
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020055 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020056 "%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 +020057 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020058 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020059 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020060 !!(global_tasks_mask & thr_bit),
61 !eb_is_empty(&task_per_thread[thr].timers),
62 !eb_is_empty(&task_per_thread[thr].rqueue),
Olivier Houchard06910462019-10-11 16:35:01 +020063 !(LIST_ISEMPTY(&task_per_thread[thr].task_list) |
64 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020065 task_per_thread[thr].task_list_size,
66 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020067 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020068 !!(task_profiling_mask & thr_bit));
69
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020070 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020071 " harmless=%d wantrdv=%d",
72 !!(threads_harmless_mask & thr_bit),
73 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020074
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020075 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020076 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020077
78 /* this is the end of what we can dump from outside the thread */
79
80 if (thr != tid)
81 return;
82
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020083 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020084 ha_task_dump(buf, sched->current, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020085}
86
87
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020088/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020089 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020090 * with <pfx>. The buffer is only appended and the first output starts by the
91 * pointer itself. The caller is responsible for making sure the task is not
92 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020093 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020094void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020095{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020096 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +020097 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +020098 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +020099
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200100 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200101 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200102 return;
103 }
104
Willy Tarreau20db9112019-05-17 14:14:35 +0200105 if (TASK_IS_TASKLET(task))
106 chunk_appendf(buf,
107 "%p (tasklet) calls=%u\n",
108 task,
109 task->calls);
110 else
111 chunk_appendf(buf,
112 "%p (task) calls=%u last=%llu%s\n",
113 task,
114 task->calls,
115 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
116 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200117
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200118 chunk_appendf(buf, "%s"
Willy Tarreaua512b022019-08-21 14:12:19 +0200119 " fct=%p (%s) ctx=%p",
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200120 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200121 task->process,
122 task->process == process_stream ? "process_stream" :
123 task->process == task_run_applet ? "task_run_applet" :
124 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200125#ifdef USE_LUA
126 task->process == hlua_process_task ? "hlua_process_task" :
127#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200128 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200129 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200130
Willy Tarreaua512b022019-08-21 14:12:19 +0200131 if (task->process == task_run_applet && (appctx = task->context))
132 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
133 else
134 chunk_appendf(buf, "\n");
135
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200136 if (task->process == process_stream && task->context)
137 s = (struct stream *)task->context;
138 else if (task->process == task_run_applet && task->context)
139 s = si_strm(((struct appctx *)task->context)->owner);
140 else if (task->process == si_cs_io_cb && task->context)
141 s = si_strm((struct stream_interface *)task->context);
142
143 if (s)
144 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200145
146#ifdef USE_LUA
147 hlua = NULL;
148 if (s && (hlua = s->hlua)) {
149 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
150 }
151 else if (task->process == hlua_process_task && (hlua = task->context)) {
152 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
153 }
154 else if (task->process == task_run_applet && (appctx = task->context) &&
155 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
156 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
157 }
158 else if (task->process == task_run_applet && (appctx = task->context) &&
159 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
160 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
161 }
162
163 if (hlua) {
164 luaL_traceback(hlua->T, hlua->T, NULL, 0);
165 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
166 b_putchr(buf, '\n');
167 }
168#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200169}
170
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200171/* This function dumps all profiling settings. It returns 0 if the output
172 * buffer is full and it needs to be called again, otherwise non-zero.
173 */
174static int cli_io_handler_show_threads(struct appctx *appctx)
175{
176 struct stream_interface *si = appctx->owner;
177 int thr;
178
179 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
180 return 1;
181
182 if (appctx->st0)
183 thr = appctx->st1;
184 else
185 thr = 0;
186
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200187 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200188 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200189
190 if (ci_putchk(si_ic(si), &trash) == -1) {
191 /* failed, try again */
192 si_rx_room_blk(si);
193 appctx->st1 = thr;
194 return 0;
195 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200196 return 1;
197}
198
Willy Tarreau56131ca2019-05-20 13:48:29 +0200199/* dumps a state of all threads into the trash and on fd #2, then aborts. */
200void ha_panic()
201{
202 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200203 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200204 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200205 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200206 for (;;)
207 abort();
208}
209
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200210/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
211static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
212{
213 int code = atoi(args[3]);
214
215 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
216 return 1;
217
Willy Tarreau9b013702019-10-24 18:18:02 +0200218 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200219 exit(code);
220 return 1;
221}
222
223/* parse a "debug dev close" command. It always returns 1. */
224static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
225{
226 int fd;
227
228 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
229 return 1;
230
Willy Tarreau9d008692019-08-09 11:21:01 +0200231 if (!*args[3])
232 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200233
234 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200235 if (fd < 0 || fd >= global.maxsock)
236 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200237
Willy Tarreau9d008692019-08-09 11:21:01 +0200238 if (!fdtab[fd].owner)
239 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200240
Willy Tarreau9b013702019-10-24 18:18:02 +0200241 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200242 fd_delete(fd);
243 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200244}
245
246/* parse a "debug dev delay" command. It always returns 1. */
247static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
248{
249 int delay = atoi(args[3]);
250
251 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
252 return 1;
253
Willy Tarreau9b013702019-10-24 18:18:02 +0200254 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200255 usleep((long)delay * 1000);
256 return 1;
257}
258
259/* parse a "debug dev log" command. It always returns 1. */
260static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
261{
262 int arg;
263
264 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
265 return 1;
266
Willy Tarreau9b013702019-10-24 18:18:02 +0200267 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200268 chunk_reset(&trash);
269 for (arg = 3; *args[arg]; arg++) {
270 if (arg > 3)
271 chunk_strcat(&trash, " ");
272 chunk_strcat(&trash, args[arg]);
273 }
274
275 send_log(NULL, LOG_INFO, "%s\n", trash.area);
276 return 1;
277}
278
279/* parse a "debug dev loop" command. It always returns 1. */
280static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
281{
282 struct timeval deadline, curr;
283 int loop = atoi(args[3]);
284
285 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
286 return 1;
287
Willy Tarreau9b013702019-10-24 18:18:02 +0200288 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200289 gettimeofday(&curr, NULL);
290 tv_ms_add(&deadline, &curr, loop);
291
292 while (tv_ms_cmp(&curr, &deadline) < 0)
293 gettimeofday(&curr, NULL);
294
295 return 1;
296}
297
298/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
299static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
300{
301 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
302 return 1;
303
Willy Tarreau9b013702019-10-24 18:18:02 +0200304 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200305 ha_panic();
306 return 1;
307}
308
309/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200310#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200311static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
312{
313 FILE *f;
314 int arg;
315
316 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
317 return 1;
318
Willy Tarreau9b013702019-10-24 18:18:02 +0200319 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200320 chunk_reset(&trash);
321 for (arg = 3; *args[arg]; arg++) {
322 if (arg > 3)
323 chunk_strcat(&trash, " ");
324 chunk_strcat(&trash, args[arg]);
325 }
326
327 f = popen(trash.area, "re");
Willy Tarreau9d008692019-08-09 11:21:01 +0200328 if (!f)
329 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200330
331 chunk_reset(&trash);
332 while (1) {
333 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
334 if (!ret)
335 break;
336 trash.data += ret;
337 if (trash.data + 20 == trash.size) {
338 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
339 break;
340 }
341 }
342
343 fclose(f);
344 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200345 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200346}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200347#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200348
349/* parse a "debug dev hex" command. It always returns 1. */
350static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
351{
352 unsigned long start, len;
353
354 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
355 return 1;
356
Willy Tarreau9d008692019-08-09 11:21:01 +0200357 if (!*args[3])
358 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200359
360 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200361 if (!start)
362 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200363
Willy Tarreau9b013702019-10-24 18:18:02 +0200364 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
365
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200366 /* by default, dump ~128 till next block of 16 */
367 len = strtoul(args[4], NULL, 0);
368 if (!len)
369 len = ((start + 128) & -16) - start;
370
371 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200372 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200373 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200374 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200375}
376
377/* parse a "debug dev tkill" command. It always returns 1. */
378static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
379{
380 int thr = 0;
381 int sig = SIGABRT;
382
383 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
384 return 1;
385
386 if (*args[3])
387 thr = atoi(args[3]);
388
Willy Tarreau9d008692019-08-09 11:21:01 +0200389 if (thr < 0 || thr > global.nbthread)
390 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391
392 if (*args[4])
393 sig = atoi(args[4]);
394
Willy Tarreau9b013702019-10-24 18:18:02 +0200395 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200396 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200397 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200398 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200399 raise(sig);
400 return 1;
401}
402
Willy Tarreau68680bb2019-10-23 17:23:25 +0200403/* parse a "debug dev stream" command */
404/*
405 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
406 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
407 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
408 * [sif.s[=<state>]] [sib.s[=<state>]]
409 */
410static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
411{
412 struct stream *s = si_strm(appctx->owner);
413 int arg;
414 void *ptr;
415 int size;
416 const char *word, *end;
417 struct ist name;
418 char *msg = NULL;
419 char *endarg;
420 unsigned long long old, new;
421
422 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
423 return 1;
424
425 ptr = NULL; size = 0;
426
427 if (!*args[3]) {
428 return cli_err(appctx,
429 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
430 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
431 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
432 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
433 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
434 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
435 );
436 }
437
Willy Tarreau9b013702019-10-24 18:18:02 +0200438 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200439 for (arg = 3; *args[arg]; arg++) {
440 old = 0;
441 end = word = args[arg];
442 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
443 end++;
444 name = ist2(word, end - word);
445 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200446 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200447 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200448 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200449 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200450 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200451 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200452 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200453 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200454 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200455 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200456 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200457 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200458 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200459 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200460 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200461 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200462 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200463 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200464 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200465 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200466 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200467 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200468 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200469 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200470 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200471 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200472 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200473 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200474 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200475 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200476 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200477 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
478 continue;
479 } else
480 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
481
482 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200483 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200484 if (size == 8)
485 old = read_u64(ptr);
486 else if (size == 4)
487 old = read_u32(ptr);
488 else if (size == 2)
489 old = read_u16(ptr);
490 else
491 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200492 } else {
493 memprintf(&msg,
494 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
495 msg ? msg : "", ptr, (int)(end - word), word);
496 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200497 }
498
499 /* parse the new value . */
500 new = strtoll(end + 1, &endarg, 0);
501 if (end[1] && *endarg) {
502 if (strcmp(end + 1, "now") == 0)
503 new = now_ms;
504 else {
505 memprintf(&msg,
506 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
507 msg ? msg : "", end + 1, (int)(end - word), word);
508 continue;
509 }
510 }
511
512 switch (*end) {
513 case '\0': /* show */
514 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
515 new = old; // do not change the value
516 break;
517
518 case '=': /* set */
519 break;
520
521 case '^': /* XOR */
522 new = old ^ new;
523 break;
524
525 case '+': /* OR */
526 new = old | new;
527 break;
528
529 case '-': /* AND NOT */
530 new = old & ~new;
531 break;
532
533 default:
534 break;
535 }
536
537 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200538 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200539 if (size == 8)
540 write_u64(ptr, new);
541 else if (size == 4)
542 write_u32(ptr, new);
543 else if (size == 2)
544 write_u16(ptr, new);
545 else
546 *(uint8_t *)ptr = new;
547 }
548 }
549
550 if (msg && *msg)
551 return cli_dynmsg(appctx, LOG_INFO, msg);
552 return 1;
553}
554
Willy Tarreauc7091d82019-05-17 10:08:49 +0200555#ifndef USE_THREAD_DUMP
556
557/* This function dumps all threads' state to the trash. This version is the
558 * most basic one, which doesn't inspect other threads.
559 */
560void ha_thread_dump_all_to_trash()
561{
562 unsigned int thr;
563
564 for (thr = 0; thr < global.nbthread; thr++)
565 ha_thread_dump(&trash, thr, tid);
566}
567
568#else /* below USE_THREAD_DUMP is set */
569
Willy Tarreauddd85332019-05-22 06:28:54 +0200570/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
571 * of not stopping gdb by default, so that issuing "show threads" in a process
572 * being debugged has no adverse effect.
573 */
574#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200575
Willy Tarreauc7091d82019-05-17 10:08:49 +0200576/* ID of the thread requesting the dump */
577static unsigned int thread_dump_tid;
578
579/* points to the buffer where the dump functions should write. It must
580 * have already been initialized by the requester. Nothing is done if
581 * it's NULL.
582 */
583struct buffer *thread_dump_buffer = NULL;
584
585void ha_thread_dump_all_to_trash()
586{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200587 unsigned long old;
588
589 while (1) {
590 old = 0;
591 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
592 break;
593 ha_thread_relax();
594 }
595
596 thread_dump_buffer = &trash;
597 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200598 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200599}
600
601/* handles DEBUGSIG to dump the state of the thread it's working on */
602void debug_handler(int sig, siginfo_t *si, void *arg)
603{
604 /* There are 4 phases in the dump process:
605 * 1- wait for our turn, i.e. when all lower bits are gone.
606 * 2- perform the action if our bit is set
607 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200608 * the last one and have to put them all as a signal
609 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200610 */
611
612 /* wait for all previous threads to finish first */
613 while (threads_to_dump & (tid_bit - 1))
614 ha_thread_relax();
615
616 /* dump if needed */
617 if (threads_to_dump & tid_bit) {
618 if (thread_dump_buffer)
619 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
620 if ((threads_to_dump & all_threads_mask) == tid_bit) {
621 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200622 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200623 thread_dump_buffer = NULL;
624 }
625 else
626 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
627 }
628
629 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200630 * bits again to broadcast the leaving condition so we'll see ourselves
631 * present again. This way the threads_to_dump variable never passes to
632 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200633 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200634 while (!(threads_to_dump & tid_bit))
635 ha_thread_relax();
636 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200637
638 /* mark the current thread as stuck to detect it upon next invocation
639 * if it didn't move.
640 */
641 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
642 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200643}
644
645static int init_debug_per_thread()
646{
647 sigset_t set;
648
649 /* unblock the DEBUGSIG signal we intend to use */
650 sigemptyset(&set);
651 sigaddset(&set, DEBUGSIG);
652 ha_sigmask(SIG_UNBLOCK, &set, NULL);
653 return 1;
654}
655
656static int init_debug()
657{
658 struct sigaction sa;
659
660 sa.sa_handler = NULL;
661 sa.sa_sigaction = debug_handler;
662 sigemptyset(&sa.sa_mask);
663 sa.sa_flags = SA_SIGINFO;
664 sigaction(DEBUGSIG, &sa, NULL);
665 return 0;
666}
667
668REGISTER_POST_CHECK(init_debug);
669REGISTER_PER_THREAD_INIT(init_debug_per_thread);
670
671#endif /* USE_THREAD_DUMP */
672
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200673/* register cli keywords */
674static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +0200675 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
676 {{ "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 +0200677#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +0200678 {{ "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 +0200679#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +0200680 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
681 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
682 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
683 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
684 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
685 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
686 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
687 {{ "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 +0200688 {{},}
689}};
690
691INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);