blob: ebb95c7e064470baf19c9d045e3086fa4b6f3a6c [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;
39
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020040/* Dumps to the buffer some known information for the desired thread, and
41 * optionally extra info for the current thread. The dump will be appended to
42 * the buffer, so the caller is responsible for preliminary initializing it.
43 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020044 * in front of the calling thread's line (usually it's tid). Any stuck thread
45 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020046 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020047void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020048{
49 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010050 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
51 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
52 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020053
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020054 chunk_appendf(buf,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020055 "%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 +020056 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020057 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020058 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020059 !!(global_tasks_mask & thr_bit),
60 !eb_is_empty(&task_per_thread[thr].timers),
61 !eb_is_empty(&task_per_thread[thr].rqueue),
Olivier Houchard06910462019-10-11 16:35:01 +020062 !(LIST_ISEMPTY(&task_per_thread[thr].task_list) |
63 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020064 task_per_thread[thr].task_list_size,
65 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020066 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020067 !!(task_profiling_mask & thr_bit));
68
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020069 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020070 " harmless=%d wantrdv=%d",
71 !!(threads_harmless_mask & thr_bit),
72 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020073
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020074 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020075 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020076
77 /* this is the end of what we can dump from outside the thread */
78
79 if (thr != tid)
80 return;
81
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020082 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +020083 ha_task_dump(buf, sched->current, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020084}
85
86
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020087/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020089 * with <pfx>. The buffer is only appended and the first output starts by the
90 * pointer itself. The caller is responsible for making sure the task is not
91 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020092 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020093void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020094{
Willy Tarreau578ea8b2019-05-22 09:43:09 +020095 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +020096 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +020097 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +020098
Willy Tarreau14a1ab72019-05-17 10:34:25 +020099 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200100 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200101 return;
102 }
103
Willy Tarreau20db9112019-05-17 14:14:35 +0200104 if (TASK_IS_TASKLET(task))
105 chunk_appendf(buf,
106 "%p (tasklet) calls=%u\n",
107 task,
108 task->calls);
109 else
110 chunk_appendf(buf,
111 "%p (task) calls=%u last=%llu%s\n",
112 task,
113 task->calls,
114 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
115 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200116
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200117 chunk_appendf(buf, "%s"
Willy Tarreaua512b022019-08-21 14:12:19 +0200118 " fct=%p (%s) ctx=%p",
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200119 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200120 task->process,
121 task->process == process_stream ? "process_stream" :
122 task->process == task_run_applet ? "task_run_applet" :
123 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200124#ifdef USE_LUA
125 task->process == hlua_process_task ? "hlua_process_task" :
126#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200127 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200128 task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200129
Willy Tarreaua512b022019-08-21 14:12:19 +0200130 if (task->process == task_run_applet && (appctx = task->context))
131 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
132 else
133 chunk_appendf(buf, "\n");
134
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200135 if (task->process == process_stream && task->context)
136 s = (struct stream *)task->context;
137 else if (task->process == task_run_applet && task->context)
138 s = si_strm(((struct appctx *)task->context)->owner);
139 else if (task->process == si_cs_io_cb && task->context)
140 s = si_strm((struct stream_interface *)task->context);
141
142 if (s)
143 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200144
145#ifdef USE_LUA
146 hlua = NULL;
147 if (s && (hlua = s->hlua)) {
148 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
149 }
150 else if (task->process == hlua_process_task && (hlua = task->context)) {
151 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
152 }
153 else if (task->process == task_run_applet && (appctx = task->context) &&
154 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
155 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
156 }
157 else if (task->process == task_run_applet && (appctx = task->context) &&
158 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
159 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
160 }
161
162 if (hlua) {
163 luaL_traceback(hlua->T, hlua->T, NULL, 0);
164 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
165 b_putchr(buf, '\n');
166 }
167#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200168}
169
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200170/* This function dumps all profiling settings. It returns 0 if the output
171 * buffer is full and it needs to be called again, otherwise non-zero.
172 */
173static int cli_io_handler_show_threads(struct appctx *appctx)
174{
175 struct stream_interface *si = appctx->owner;
176 int thr;
177
178 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
179 return 1;
180
181 if (appctx->st0)
182 thr = appctx->st1;
183 else
184 thr = 0;
185
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200186 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200187 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200188
189 if (ci_putchk(si_ic(si), &trash) == -1) {
190 /* failed, try again */
191 si_rx_room_blk(si);
192 appctx->st1 = thr;
193 return 0;
194 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200195 return 1;
196}
197
Willy Tarreau56131ca2019-05-20 13:48:29 +0200198/* dumps a state of all threads into the trash and on fd #2, then aborts. */
199void ha_panic()
200{
201 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200202 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200203 ha_thread_dump_all_to_trash();
Tim Duesterhusdda11552019-06-12 20:47:30 +0200204 shut_your_big_mouth_gcc(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200205 for (;;)
206 abort();
207}
208
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200209#if defined(DEBUG_DEV)
210/* 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
218 exit(code);
219 return 1;
220}
221
222/* parse a "debug dev close" command. It always returns 1. */
223static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
224{
225 int fd;
226
227 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
228 return 1;
229
Willy Tarreau9d008692019-08-09 11:21:01 +0200230 if (!*args[3])
231 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200232
233 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200234 if (fd < 0 || fd >= global.maxsock)
235 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200236
Willy Tarreau9d008692019-08-09 11:21:01 +0200237 if (!fdtab[fd].owner)
238 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200239
240 fd_delete(fd);
241 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200242}
243
244/* parse a "debug dev delay" command. It always returns 1. */
245static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
246{
247 int delay = atoi(args[3]);
248
249 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
250 return 1;
251
252 usleep((long)delay * 1000);
253 return 1;
254}
255
256/* parse a "debug dev log" command. It always returns 1. */
257static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
258{
259 int arg;
260
261 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
262 return 1;
263
264 chunk_reset(&trash);
265 for (arg = 3; *args[arg]; arg++) {
266 if (arg > 3)
267 chunk_strcat(&trash, " ");
268 chunk_strcat(&trash, args[arg]);
269 }
270
271 send_log(NULL, LOG_INFO, "%s\n", trash.area);
272 return 1;
273}
274
275/* parse a "debug dev loop" command. It always returns 1. */
276static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
277{
278 struct timeval deadline, curr;
279 int loop = atoi(args[3]);
280
281 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
282 return 1;
283
284 gettimeofday(&curr, NULL);
285 tv_ms_add(&deadline, &curr, loop);
286
287 while (tv_ms_cmp(&curr, &deadline) < 0)
288 gettimeofday(&curr, NULL);
289
290 return 1;
291}
292
293/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
294static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
295{
296 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
297 return 1;
298
299 ha_panic();
300 return 1;
301}
302
303/* parse a "debug dev exec" command. It always returns 1. */
304static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
305{
306 FILE *f;
307 int arg;
308
309 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
310 return 1;
311
312 chunk_reset(&trash);
313 for (arg = 3; *args[arg]; arg++) {
314 if (arg > 3)
315 chunk_strcat(&trash, " ");
316 chunk_strcat(&trash, args[arg]);
317 }
318
319 f = popen(trash.area, "re");
Willy Tarreau9d008692019-08-09 11:21:01 +0200320 if (!f)
321 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200322
323 chunk_reset(&trash);
324 while (1) {
325 size_t ret = fread(trash.area + trash.data, 1, trash.size - 20 - trash.data, f);
326 if (!ret)
327 break;
328 trash.data += ret;
329 if (trash.data + 20 == trash.size) {
330 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
331 break;
332 }
333 }
334
335 fclose(f);
336 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200337 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200338}
339
340/* parse a "debug dev hex" command. It always returns 1. */
341static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
342{
343 unsigned long start, len;
344
345 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
346 return 1;
347
Willy Tarreau9d008692019-08-09 11:21:01 +0200348 if (!*args[3])
349 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200350
351 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200352 if (!start)
353 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200354
355 /* by default, dump ~128 till next block of 16 */
356 len = strtoul(args[4], NULL, 0);
357 if (!len)
358 len = ((start + 128) & -16) - start;
359
360 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200361 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200362 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200363 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200364}
365
366/* parse a "debug dev tkill" command. It always returns 1. */
367static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
368{
369 int thr = 0;
370 int sig = SIGABRT;
371
372 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
373 return 1;
374
375 if (*args[3])
376 thr = atoi(args[3]);
377
Willy Tarreau9d008692019-08-09 11:21:01 +0200378 if (thr < 0 || thr > global.nbthread)
379 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200380
381 if (*args[4])
382 sig = atoi(args[4]);
383
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200384 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200385 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200386 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200387 raise(sig);
388 return 1;
389}
390
Willy Tarreau68680bb2019-10-23 17:23:25 +0200391/* parse a "debug dev stream" command */
392/*
393 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
394 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
395 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
396 * [sif.s[=<state>]] [sib.s[=<state>]]
397 */
398static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
399{
400 struct stream *s = si_strm(appctx->owner);
401 int arg;
402 void *ptr;
403 int size;
404 const char *word, *end;
405 struct ist name;
406 char *msg = NULL;
407 char *endarg;
408 unsigned long long old, new;
409
410 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
411 return 1;
412
413 ptr = NULL; size = 0;
414
415 if (!*args[3]) {
416 return cli_err(appctx,
417 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
418 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
419 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
420 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
421 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
422 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
423 );
424 }
425
426 for (arg = 3; *args[arg]; arg++) {
427 old = 0;
428 end = word = args[arg];
429 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
430 end++;
431 name = ist2(word, end - word);
432 if (isteq(name, ist("strm"))) {
433 ptr = &s; size = sizeof(s);
434 } else if (isteq(name, ist("strm.f"))) {
435 ptr = &s->flags; size = sizeof(s->flags);
436 } else if (isteq(name, ist("txn.f"))) {
437 ptr = &s->txn->flags; size = sizeof(s->txn->flags);
438 } else if (isteq(name, ist("req.f"))) {
439 ptr = &s->req.flags; size = sizeof(s->req.flags);
440 } else if (isteq(name, ist("res.f"))) {
441 ptr = &s->res.flags; size = sizeof(s->res.flags);
442 } else if (isteq(name, ist("req.r"))) {
443 ptr = &s->req.rex; size = sizeof(s->req.rex);
444 } else if (isteq(name, ist("res.r"))) {
445 ptr = &s->res.rex; size = sizeof(s->res.rex);
446 } else if (isteq(name, ist("req.w"))) {
447 ptr = &s->req.wex; size = sizeof(s->req.wex);
448 } else if (isteq(name, ist("res.w"))) {
449 ptr = &s->res.wex; size = sizeof(s->res.wex);
450 } else if (isteq(name, ist("sif.f"))) {
451 ptr = &s->si[0].flags; size = sizeof(s->si[0].flags);
452 } else if (isteq(name, ist("sib.f"))) {
453 ptr = &s->si[1].flags; size = sizeof(s->si[1].flags);
454 } else if (isteq(name, ist("sif.x"))) {
455 ptr = &s->si[0].exp; size = sizeof(s->si[0].exp);
456 } else if (isteq(name, ist("sib.x"))) {
457 ptr = &s->si[1].exp; size = sizeof(s->si[1].exp);
458 } else if (isteq(name, ist("sif.s"))) {
459 ptr = &s->si[0].state; size = sizeof(s->si[0].state);
460 } else if (isteq(name, ist("sib.s"))) {
461 ptr = &s->si[1].state; size = sizeof(s->si[1].state);
462 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200463 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200464 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
465 continue;
466 } else
467 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
468
469 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200470 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200471 if (size == 8)
472 old = read_u64(ptr);
473 else if (size == 4)
474 old = read_u32(ptr);
475 else if (size == 2)
476 old = read_u16(ptr);
477 else
478 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200479 } else {
480 memprintf(&msg,
481 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
482 msg ? msg : "", ptr, (int)(end - word), word);
483 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200484 }
485
486 /* parse the new value . */
487 new = strtoll(end + 1, &endarg, 0);
488 if (end[1] && *endarg) {
489 if (strcmp(end + 1, "now") == 0)
490 new = now_ms;
491 else {
492 memprintf(&msg,
493 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
494 msg ? msg : "", end + 1, (int)(end - word), word);
495 continue;
496 }
497 }
498
499 switch (*end) {
500 case '\0': /* show */
501 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
502 new = old; // do not change the value
503 break;
504
505 case '=': /* set */
506 break;
507
508 case '^': /* XOR */
509 new = old ^ new;
510 break;
511
512 case '+': /* OR */
513 new = old | new;
514 break;
515
516 case '-': /* AND NOT */
517 new = old & ~new;
518 break;
519
520 default:
521 break;
522 }
523
524 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200525 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200526 if (size == 8)
527 write_u64(ptr, new);
528 else if (size == 4)
529 write_u32(ptr, new);
530 else if (size == 2)
531 write_u16(ptr, new);
532 else
533 *(uint8_t *)ptr = new;
534 }
535 }
536
537 if (msg && *msg)
538 return cli_dynmsg(appctx, LOG_INFO, msg);
539 return 1;
540}
541
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200542#endif
543
Willy Tarreauc7091d82019-05-17 10:08:49 +0200544#ifndef USE_THREAD_DUMP
545
546/* This function dumps all threads' state to the trash. This version is the
547 * most basic one, which doesn't inspect other threads.
548 */
549void ha_thread_dump_all_to_trash()
550{
551 unsigned int thr;
552
553 for (thr = 0; thr < global.nbthread; thr++)
554 ha_thread_dump(&trash, thr, tid);
555}
556
557#else /* below USE_THREAD_DUMP is set */
558
Willy Tarreauddd85332019-05-22 06:28:54 +0200559/* The signal to trigger a debug dump on a thread is SIGURG. It has the benefit
560 * of not stopping gdb by default, so that issuing "show threads" in a process
561 * being debugged has no adverse effect.
562 */
563#define DEBUGSIG SIGURG
Willy Tarreauc7091d82019-05-17 10:08:49 +0200564
Willy Tarreauc7091d82019-05-17 10:08:49 +0200565/* ID of the thread requesting the dump */
566static unsigned int thread_dump_tid;
567
568/* points to the buffer where the dump functions should write. It must
569 * have already been initialized by the requester. Nothing is done if
570 * it's NULL.
571 */
572struct buffer *thread_dump_buffer = NULL;
573
574void ha_thread_dump_all_to_trash()
575{
Willy Tarreauc7091d82019-05-17 10:08:49 +0200576 unsigned long old;
577
578 while (1) {
579 old = 0;
580 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
581 break;
582 ha_thread_relax();
583 }
584
585 thread_dump_buffer = &trash;
586 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +0200587 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200588}
589
590/* handles DEBUGSIG to dump the state of the thread it's working on */
591void debug_handler(int sig, siginfo_t *si, void *arg)
592{
593 /* There are 4 phases in the dump process:
594 * 1- wait for our turn, i.e. when all lower bits are gone.
595 * 2- perform the action if our bit is set
596 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +0200597 * the last one and have to put them all as a signal
598 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200599 */
600
601 /* wait for all previous threads to finish first */
602 while (threads_to_dump & (tid_bit - 1))
603 ha_thread_relax();
604
605 /* dump if needed */
606 if (threads_to_dump & tid_bit) {
607 if (thread_dump_buffer)
608 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
609 if ((threads_to_dump & all_threads_mask) == tid_bit) {
610 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +0200611 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200612 thread_dump_buffer = NULL;
613 }
614 else
615 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
616 }
617
618 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +0200619 * bits again to broadcast the leaving condition so we'll see ourselves
620 * present again. This way the threads_to_dump variable never passes to
621 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +0200622 */
Willy Tarreauc0773622019-07-31 19:15:45 +0200623 while (!(threads_to_dump & tid_bit))
624 ha_thread_relax();
625 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200626
627 /* mark the current thread as stuck to detect it upon next invocation
628 * if it didn't move.
629 */
630 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
631 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +0200632}
633
634static int init_debug_per_thread()
635{
636 sigset_t set;
637
638 /* unblock the DEBUGSIG signal we intend to use */
639 sigemptyset(&set);
640 sigaddset(&set, DEBUGSIG);
641 ha_sigmask(SIG_UNBLOCK, &set, NULL);
642 return 1;
643}
644
645static int init_debug()
646{
647 struct sigaction sa;
648
649 sa.sa_handler = NULL;
650 sa.sa_sigaction = debug_handler;
651 sigemptyset(&sa.sa_mask);
652 sa.sa_flags = SA_SIGINFO;
653 sigaction(DEBUGSIG, &sa, NULL);
654 return 0;
655}
656
657REGISTER_POST_CHECK(init_debug);
658REGISTER_PER_THREAD_INIT(init_debug_per_thread);
659
660#endif /* USE_THREAD_DUMP */
661
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200662/* register cli keywords */
663static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200664#if defined(DEBUG_DEV)
665 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL },
666 {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL },
667 {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL },
668 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL },
669 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL },
670 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL },
671 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL },
672 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL },
Willy Tarreau68680bb2019-10-23 17:23:25 +0200673 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL },
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200674 {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL },
675#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200676 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
677 {{},}
678}};
679
680INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);