blob: f83a7c018c74bbd465c40562cdc2499fe186e705 [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 Tarreauaeed4a82020-06-04 22:01:04 +020019#include <syslog.h>
Willy Tarreau368bff42019-12-06 17:18:28 +010020#include <sys/types.h>
21#include <sys/wait.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020022
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020024#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020026#include <haproxy/debug.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/fd.h>
28#include <haproxy/global.h>
Willy Tarreau86416052020-06-04 09:20:54 +020029#include <haproxy/hlua.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/net_helper.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020032#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020033#include <haproxy/task.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020034#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020035#include <haproxy/tools.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <import/ist.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020037
Willy Tarreau4e2b6462019-05-16 17:44:30 +020038
Willy Tarreaua37cb182019-07-31 19:20:39 +020039/* mask of threads still having to dump, used to respect ordering. Only used
40 * when USE_THREAD_DUMP is set.
41 */
42volatile unsigned long threads_to_dump = 0;
Willy Tarreau9b013702019-10-24 18:18:02 +020043unsigned int debug_commands_issued = 0;
Willy Tarreaua37cb182019-07-31 19:20:39 +020044
Willy Tarreau8a069eb2020-11-30 16:17:33 +010045/* Xorshift RNGs from http://www.jstatsoft.org/v08/i14/paper */
46static THREAD_LOCAL unsigned int y = 2463534242;
47static unsigned int debug_prng()
48{
49
50 y ^= y << 13;
51 y ^= y >> 17;
52 y ^= y << 5;
53 return y;
54}
55
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020056/* Dumps to the buffer some known information for the desired thread, and
57 * optionally extra info for the current thread. The dump will be appended to
58 * the buffer, so the caller is responsible for preliminary initializing it.
59 * The calling thread ID needs to be passed in <calling_tid> to display a star
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020060 * in front of the calling thread's line (usually it's tid). Any stuck thread
61 * is also prefixed with a '>'.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020062 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020063void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020064{
65 unsigned long thr_bit = 1UL << thr;
David Carliera92c5ce2019-09-13 05:03:12 +010066 unsigned long long p = ha_thread_info[thr].prev_cpu_time;
67 unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
68 int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020069
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020070 chunk_appendf(buf,
Willy Tarreauf0e5da22020-05-01 12:26:03 +020071 "%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 +020072 " stuck=%d prof=%d",
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020073 (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
Willy Tarreauff64d3b2020-05-01 11:28:49 +020074 ha_get_pthread_id(thr),
Olivier Houchardcfbb3e62019-05-29 19:22:43 +020075 thread_has_tasks(),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020076 !!(global_tasks_mask & thr_bit),
77 !eb_is_empty(&task_per_thread[thr].timers),
78 !eb_is_empty(&task_per_thread[thr].rqueue),
Willy Tarreaua62917b2020-01-30 18:37:28 +010079 !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
80 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
81 LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
82 MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
Willy Tarreau4e2b6462019-05-16 17:44:30 +020083 task_per_thread[thr].task_list_size,
84 task_per_thread[thr].rqueue_size,
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020085 stuck,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020086 !!(task_profiling_mask & thr_bit));
87
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020088 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020089 " harmless=%d wantrdv=%d",
90 !!(threads_harmless_mask & thr_bit),
91 !!(threads_want_rdv_mask & thr_bit));
Willy Tarreau4e2b6462019-05-16 17:44:30 +020092
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020093 chunk_appendf(buf, "\n");
Willy Tarreau9c8800a2019-05-20 20:52:20 +020094 chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020095
96 /* this is the end of what we can dump from outside the thread */
97
98 if (thr != tid)
99 return;
100
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200101 chunk_appendf(buf, " curr_task=");
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200102 ha_task_dump(buf, sched->current, " ");
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100103
104#ifdef USE_BACKTRACE
105 if (stuck) {
106 /* We only emit the backtrace for stuck threads in order not to
107 * waste precious output buffer space with non-interesting data.
108 */
109 struct buffer bak;
110 void *callers[100];
111 int j, nptrs;
Willy Tarreau0c439d82020-07-05 20:26:04 +0200112 const void *addr;
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100113 int dump = 0;
114
Willy Tarreau13faf162020-03-04 07:44:06 +0100115 nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100116
117 /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO)
118 would produce similar output to the following: */
119
120 if (nptrs)
Willy Tarreaucdd80742020-03-04 07:38:23 +0100121 chunk_appendf(buf, " call trace(%d):\n", nptrs);
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100122
Willy Tarreaua91b7942020-03-04 07:39:32 +0100123 for (j = 0; j < nptrs || dump < 2; j++) {
124 if (j == nptrs && !dump) {
125 /* we failed to spot the starting point of the
126 * dump, let's start over dumping everything we
127 * have.
128 */
129 dump = 2;
130 j = 0;
131 }
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100132 bak = *buf;
133 dump_addr_and_bytes(buf, " | ", callers[j], 8);
134 addr = resolve_sym_name(buf, ": ", callers[j]);
135 if (dump == 0) {
136 /* dump not started, will start *after*
137 * ha_thread_dump_all_to_trash and ha_panic
138 */
139 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic)
140 dump = 1;
141 *buf = bak;
142 continue;
143 }
144
145 if (dump == 1) {
146 /* starting */
147 if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) {
148 *buf = bak;
149 continue;
150 }
151 dump = 2;
152 }
153
154 if (dump == 2) {
155 /* dumping */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200156 if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
Willy Tarreauf5b4e062020-03-03 15:40:23 +0100157 dump = 3;
158 *buf = bak;
159 break;
160 }
161 }
162 /* OK, line dumped */
163 chunk_appendf(buf, "\n");
164 }
165 }
166#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200167}
168
169
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200170/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200171 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200172 * with <pfx>. The buffer is only appended and the first output starts by the
173 * pointer itself. The caller is responsible for making sure the task is not
174 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200175 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200176void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200177{
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200178 const struct stream *s = NULL;
Willy Tarreaua512b022019-08-21 14:12:19 +0200179 const struct appctx __maybe_unused *appctx = NULL;
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200180 struct hlua __maybe_unused *hlua = NULL;
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200181
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200182 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200183 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +0200184 return;
185 }
186
Willy Tarreau20db9112019-05-17 14:14:35 +0200187 if (TASK_IS_TASKLET(task))
188 chunk_appendf(buf,
189 "%p (tasklet) calls=%u\n",
190 task,
191 task->calls);
192 else
193 chunk_appendf(buf,
194 "%p (task) calls=%u last=%llu%s\n",
195 task,
196 task->calls,
197 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
198 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200199
Willy Tarreau2e89b092020-03-03 17:13:02 +0100200 chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
201 resolve_sym_name(buf, NULL, task->process);
202 chunk_appendf(buf,") ctx=%p", task->context);
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200203
Willy Tarreaua512b022019-08-21 14:12:19 +0200204 if (task->process == task_run_applet && (appctx = task->context))
205 chunk_appendf(buf, "(%s)\n", appctx->applet->name);
206 else
207 chunk_appendf(buf, "\n");
208
Willy Tarreau578ea8b2019-05-22 09:43:09 +0200209 if (task->process == process_stream && task->context)
210 s = (struct stream *)task->context;
211 else if (task->process == task_run_applet && task->context)
212 s = si_strm(((struct appctx *)task->context)->owner);
213 else if (task->process == si_cs_io_cb && task->context)
214 s = si_strm((struct stream_interface *)task->context);
215
216 if (s)
217 stream_dump(buf, s, pfx, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200218
219#ifdef USE_LUA
220 hlua = NULL;
221 if (s && (hlua = s->hlua)) {
222 chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
223 }
224 else if (task->process == hlua_process_task && (hlua = task->context)) {
225 chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
226 }
227 else if (task->process == task_run_applet && (appctx = task->context) &&
228 (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
229 chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
230 }
231 else if (task->process == task_run_applet && (appctx = task->context) &&
232 (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
233 chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
234 }
235
Christopher Faulet471425f2020-07-24 19:08:05 +0200236 if (hlua && hlua->T) {
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200237 luaL_traceback(hlua->T, hlua->T, NULL, 0);
238 if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
239 b_putchr(buf, '\n');
240 }
Christopher Faulet471425f2020-07-24 19:08:05 +0200241 else
242 b_putchr(buf, '\n');
Willy Tarreau78a7cb62019-08-21 14:16:02 +0200243#endif
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200244}
245
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200246/* This function dumps all profiling settings. It returns 0 if the output
247 * buffer is full and it needs to be called again, otherwise non-zero.
248 */
249static int cli_io_handler_show_threads(struct appctx *appctx)
250{
251 struct stream_interface *si = appctx->owner;
252 int thr;
253
254 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
255 return 1;
256
257 if (appctx->st0)
258 thr = appctx->st1;
259 else
260 thr = 0;
261
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200262 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200263 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200264
265 if (ci_putchk(si_ic(si), &trash) == -1) {
266 /* failed, try again */
267 si_rx_room_blk(si);
268 appctx->st1 = thr;
269 return 0;
270 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200271 return 1;
272}
273
Willy Tarreau56131ca2019-05-20 13:48:29 +0200274/* dumps a state of all threads into the trash and on fd #2, then aborts. */
275void ha_panic()
276{
277 chunk_reset(&trash);
Willy Tarreaua9f9fc92019-05-20 17:45:35 +0200278 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
Willy Tarreau56131ca2019-05-20 13:48:29 +0200279 ha_thread_dump_all_to_trash();
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100280 DISGUISE(write(2, trash.area, trash.data));
Willy Tarreau56131ca2019-05-20 13:48:29 +0200281 for (;;)
282 abort();
283}
284
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200285/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
286static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
287{
288 int code = atoi(args[3]);
289
290 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
291 return 1;
292
Willy Tarreau9b013702019-10-24 18:18:02 +0200293 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200294 exit(code);
295 return 1;
296}
297
298/* parse a "debug dev close" command. It always returns 1. */
299static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
300{
301 int fd;
302
303 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
304 return 1;
305
Willy Tarreau9d008692019-08-09 11:21:01 +0200306 if (!*args[3])
307 return cli_err(appctx, "Missing file descriptor number.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200308
309 fd = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200310 if (fd < 0 || fd >= global.maxsock)
311 return cli_err(appctx, "File descriptor out of range.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200312
Willy Tarreau9d008692019-08-09 11:21:01 +0200313 if (!fdtab[fd].owner)
314 return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200315
Willy Tarreau9b013702019-10-24 18:18:02 +0200316 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200317 fd_delete(fd);
318 return 1;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200319}
320
321/* parse a "debug dev delay" command. It always returns 1. */
322static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
323{
324 int delay = atoi(args[3]);
325
326 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
327 return 1;
328
Willy Tarreau9b013702019-10-24 18:18:02 +0200329 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200330 usleep((long)delay * 1000);
331 return 1;
332}
333
334/* parse a "debug dev log" command. It always returns 1. */
335static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
336{
337 int arg;
338
339 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
340 return 1;
341
Willy Tarreau9b013702019-10-24 18:18:02 +0200342 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200343 chunk_reset(&trash);
344 for (arg = 3; *args[arg]; arg++) {
345 if (arg > 3)
346 chunk_strcat(&trash, " ");
347 chunk_strcat(&trash, args[arg]);
348 }
349
350 send_log(NULL, LOG_INFO, "%s\n", trash.area);
351 return 1;
352}
353
354/* parse a "debug dev loop" command. It always returns 1. */
355static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
356{
357 struct timeval deadline, curr;
358 int loop = atoi(args[3]);
359
360 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
361 return 1;
362
Willy Tarreau9b013702019-10-24 18:18:02 +0200363 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200364 gettimeofday(&curr, NULL);
365 tv_ms_add(&deadline, &curr, loop);
366
367 while (tv_ms_cmp(&curr, &deadline) < 0)
368 gettimeofday(&curr, NULL);
369
370 return 1;
371}
372
373/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
374static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
375{
376 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
377 return 1;
378
Willy Tarreau9b013702019-10-24 18:18:02 +0200379 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200380 ha_panic();
381 return 1;
382}
383
384/* parse a "debug dev exec" command. It always returns 1. */
Willy Tarreaub24ab222019-10-24 18:03:39 +0200385#if defined(DEBUG_DEV)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200386static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
387{
Willy Tarreau368bff42019-12-06 17:18:28 +0100388 int pipefd[2];
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200389 int arg;
Willy Tarreau368bff42019-12-06 17:18:28 +0100390 int pid;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200391
392 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
393 return 1;
394
Willy Tarreau9b013702019-10-24 18:18:02 +0200395 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200396 chunk_reset(&trash);
397 for (arg = 3; *args[arg]; arg++) {
398 if (arg > 3)
399 chunk_strcat(&trash, " ");
400 chunk_strcat(&trash, args[arg]);
401 }
402
Willy Tarreau368bff42019-12-06 17:18:28 +0100403 thread_isolate();
404 if (pipe(pipefd) < 0)
405 goto fail_pipe;
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200406
Willy Tarreau368bff42019-12-06 17:18:28 +0100407 if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
408 goto fail_fcntl;
409
410 if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
411 goto fail_fcntl;
412
413 pid = fork();
414
415 if (pid < 0)
416 goto fail_fork;
417 else if (pid == 0) {
418 /* child */
419 char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
420
421 close(0);
422 dup2(pipefd[1], 1);
423 dup2(pipefd[1], 2);
424
425 cmd[2] = trash.area;
426 execvp(cmd[0], cmd);
427 printf("execvp() failed\n");
428 exit(1);
429 }
430
431 /* parent */
432 thread_release();
433 close(pipefd[1]);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200434 chunk_reset(&trash);
435 while (1) {
Willy Tarreau368bff42019-12-06 17:18:28 +0100436 size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
437 if (ret <= 0)
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200438 break;
439 trash.data += ret;
440 if (trash.data + 20 == trash.size) {
441 chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
442 break;
443 }
444 }
Willy Tarreau368bff42019-12-06 17:18:28 +0100445 close(pipefd[0]);
446 waitpid(pid, NULL, WNOHANG);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200447 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200448 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau368bff42019-12-06 17:18:28 +0100449
450 fail_fork:
451 fail_fcntl:
452 close(pipefd[0]);
453 close(pipefd[1]);
454 fail_pipe:
455 thread_release();
456 return cli_err(appctx, "Failed to execute command.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200457}
Willy Tarreaub24ab222019-10-24 18:03:39 +0200458#endif
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200459
460/* parse a "debug dev hex" command. It always returns 1. */
461static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
462{
463 unsigned long start, len;
464
465 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
466 return 1;
467
Willy Tarreau9d008692019-08-09 11:21:01 +0200468 if (!*args[3])
469 return cli_err(appctx, "Missing memory address to dump from.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200470
471 start = strtoul(args[3], NULL, 0);
Willy Tarreau9d008692019-08-09 11:21:01 +0200472 if (!start)
473 return cli_err(appctx, "Will not dump from NULL address.\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200474
Willy Tarreau9b013702019-10-24 18:18:02 +0200475 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
476
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200477 /* by default, dump ~128 till next block of 16 */
478 len = strtoul(args[4], NULL, 0);
479 if (!len)
480 len = ((start + 128) & -16) - start;
481
482 chunk_reset(&trash);
Willy Tarreau37101052019-05-20 16:48:20 +0200483 dump_hex(&trash, " ", (const void *)start, len, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200484 trash.area[trash.data] = 0;
Willy Tarreau9d008692019-08-09 11:21:01 +0200485 return cli_msg(appctx, LOG_INFO, trash.area);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200486}
487
488/* parse a "debug dev tkill" command. It always returns 1. */
489static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
490{
491 int thr = 0;
492 int sig = SIGABRT;
493
494 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
495 return 1;
496
497 if (*args[3])
498 thr = atoi(args[3]);
499
Willy Tarreau9d008692019-08-09 11:21:01 +0200500 if (thr < 0 || thr > global.nbthread)
501 return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200502
503 if (*args[4])
504 sig = atoi(args[4]);
505
Willy Tarreau9b013702019-10-24 18:18:02 +0200506 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200507 if (thr)
Willy Tarreaufade80d2019-05-22 08:46:59 +0200508 ha_tkill(thr - 1, sig);
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200509 else
Willy Tarreau6bdf3e92019-05-20 14:25:05 +0200510 raise(sig);
511 return 1;
512}
513
Willy Tarreau6cbe62b2020-03-05 17:16:24 +0100514/* parse a "debug dev write" command. It always returns 1. */
515static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
516{
517 unsigned long len;
518
519 if (!*args[3])
520 return cli_err(appctx, "Missing output size.\n");
521
522 len = strtoul(args[3], NULL, 0);
523 if (len >= trash.size)
524 return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
525
526 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
527
528 chunk_reset(&trash);
529 trash.data = len;
530 memset(trash.area, '.', trash.data);
531 trash.area[trash.data] = 0;
532 for (len = 64; len < trash.data; len += 64)
533 trash.area[len] = '\n';
534 return cli_msg(appctx, LOG_INFO, trash.area);
535}
536
Willy Tarreau68680bb2019-10-23 17:23:25 +0200537/* parse a "debug dev stream" command */
538/*
539 * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
540 * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
541 * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
542 * [sif.s[=<state>]] [sib.s[=<state>]]
543 */
544static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
545{
546 struct stream *s = si_strm(appctx->owner);
547 int arg;
548 void *ptr;
549 int size;
550 const char *word, *end;
551 struct ist name;
552 char *msg = NULL;
553 char *endarg;
554 unsigned long long old, new;
555
556 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
557 return 1;
558
559 ptr = NULL; size = 0;
560
561 if (!*args[3]) {
562 return cli_err(appctx,
563 "Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
564 " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
565 " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
566 " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
567 " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
568 " 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
569 );
570 }
571
Willy Tarreau9b013702019-10-24 18:18:02 +0200572 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200573 for (arg = 3; *args[arg]; arg++) {
574 old = 0;
575 end = word = args[arg];
576 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
577 end++;
578 name = ist2(word, end - word);
579 if (isteq(name, ist("strm"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200580 ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200581 } else if (isteq(name, ist("strm.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200582 ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200583 } else if (isteq(name, ist("txn.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200584 ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200585 } else if (isteq(name, ist("req.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200586 ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200587 } else if (isteq(name, ist("res.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200588 ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200589 } else if (isteq(name, ist("req.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200590 ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200591 } else if (isteq(name, ist("res.r"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200592 ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200593 } else if (isteq(name, ist("req.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200594 ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200595 } else if (isteq(name, ist("res.w"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200596 ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200597 } else if (isteq(name, ist("sif.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200598 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200599 } else if (isteq(name, ist("sib.f"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200600 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200601 } else if (isteq(name, ist("sif.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200602 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200603 } else if (isteq(name, ist("sib.x"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200604 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200605 } else if (isteq(name, ist("sif.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200606 ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200607 } else if (isteq(name, ist("sib.s"))) {
Willy Tarreaub2fee042019-10-25 10:06:55 +0200608 ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
Willy Tarreau68680bb2019-10-23 17:23:25 +0200609 } else if (isteq(name, ist("wake"))) {
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200610 if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
Willy Tarreau68680bb2019-10-23 17:23:25 +0200611 task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
612 continue;
613 } else
614 return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
615
616 /* read previous value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200617 if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200618 if (size == 8)
619 old = read_u64(ptr);
620 else if (size == 4)
621 old = read_u32(ptr);
622 else if (size == 2)
623 old = read_u16(ptr);
624 else
625 old = *(const uint8_t *)ptr;
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200626 } else {
627 memprintf(&msg,
628 "%sSkipping inaccessible pointer %p for field '%.*s'.\n",
629 msg ? msg : "", ptr, (int)(end - word), word);
630 continue;
Willy Tarreau68680bb2019-10-23 17:23:25 +0200631 }
632
633 /* parse the new value . */
634 new = strtoll(end + 1, &endarg, 0);
635 if (end[1] && *endarg) {
636 if (strcmp(end + 1, "now") == 0)
637 new = now_ms;
638 else {
639 memprintf(&msg,
640 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
641 msg ? msg : "", end + 1, (int)(end - word), word);
642 continue;
643 }
644 }
645
646 switch (*end) {
647 case '\0': /* show */
648 memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
649 new = old; // do not change the value
650 break;
651
652 case '=': /* set */
653 break;
654
655 case '^': /* XOR */
656 new = old ^ new;
657 break;
658
659 case '+': /* OR */
660 new = old | new;
661 break;
662
663 case '-': /* AND NOT */
664 new = old & ~new;
665 break;
666
667 default:
668 break;
669 }
670
671 /* write the new value */
Willy Tarreau2b5520d2019-10-24 18:28:23 +0200672 if (new != old) {
Willy Tarreau68680bb2019-10-23 17:23:25 +0200673 if (size == 8)
674 write_u64(ptr, new);
675 else if (size == 4)
676 write_u32(ptr, new);
677 else if (size == 2)
678 write_u16(ptr, new);
679 else
680 *(uint8_t *)ptr = new;
681 }
682 }
683
684 if (msg && *msg)
685 return cli_dynmsg(appctx, LOG_INFO, msg);
686 return 1;
687}
688
Willy Tarreaua5a44792020-11-29 17:12:15 +0100689static struct task *debug_task_handler(struct task *t, void *ctx, unsigned short state)
690{
691 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
692 unsigned long inter = tctx[1];
693 unsigned long rnd;
694
695 t->expire = tick_add(now_ms, inter);
696
697 /* half of the calls will wake up another entry */
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100698 rnd = debug_prng();
Willy Tarreaua5a44792020-11-29 17:12:15 +0100699 if (rnd & 1) {
700 rnd >>= 1;
701 rnd %= tctx[0];
702 rnd = tctx[rnd + 2];
703
704 if (rnd & 1)
705 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
706 else
707 tasklet_wakeup((struct tasklet *)rnd);
708 }
709 return t;
710}
711
712static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned short state)
713{
714 unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
715 unsigned long rnd;
716 int i;
717
718 /* wake up two random entries */
719 for (i = 0; i < 2; i++) {
Willy Tarreau8a069eb2020-11-30 16:17:33 +0100720 rnd = debug_prng() % tctx[0];
Willy Tarreaua5a44792020-11-29 17:12:15 +0100721 rnd = tctx[rnd + 2];
722
723 if (rnd & 1)
724 task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
725 else
726 tasklet_wakeup((struct tasklet *)rnd);
727 }
728 return t;
729}
730
731/* parse a "debug dev sched" command
732 * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
733 */
734static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
735{
736 int arg;
737 void *ptr;
738 int size;
739 const char *word, *end;
740 struct ist name;
741 char *msg = NULL;
742 char *endarg;
743 unsigned long long new;
744 unsigned long count = 0;
745 unsigned long thrid = 0;
746 unsigned int inter = 0;
747 unsigned long mask, tmask;
748 unsigned long i;
749 int mode = 0; // 0 = tasklet; 1 = task
750 int single = 0;
751 unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
752
753 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
754 return 1;
755
756 ptr = NULL; size = 0;
757 mask = all_threads_mask;
758
759 if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
760 return cli_err(appctx,
761 "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
762 " <obj> = {count | mask | inter | single }\n"
763 " <value> = 64-bit dec/hex integer (0x prefix supported)\n"
764 );
765 }
766
767 mode = strcmp(args[3], "task") == 0;
768
769 _HA_ATOMIC_ADD(&debug_commands_issued, 1);
770 for (arg = 4; *args[arg]; arg++) {
771 end = word = args[arg];
772 while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
773 end++;
774 name = ist2(word, end - word);
775 if (isteq(name, ist("count"))) {
776 ptr = &count; size = sizeof(count);
777 } else if (isteq(name, ist("mask"))) {
778 ptr = &mask; size = sizeof(mask);
779 } else if (isteq(name, ist("tid"))) {
780 ptr = &thrid; size = sizeof(thrid);
781 } else if (isteq(name, ist("inter"))) {
782 ptr = &inter; size = sizeof(inter);
783 } else if (isteq(name, ist("single"))) {
784 ptr = &single; size = sizeof(single);
785 } else
786 return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
787
788 /* parse the new value . */
789 new = strtoll(end + 1, &endarg, 0);
790 if (end[1] && *endarg) {
791 memprintf(&msg,
792 "%sIgnoring unparsable value '%s' for field '%.*s'.\n",
793 msg ? msg : "", end + 1, (int)(end - word), word);
794 continue;
795 }
796
797 /* write the new value */
798 if (size == 8)
799 write_u64(ptr, new);
800 else if (size == 4)
801 write_u32(ptr, new);
802 else if (size == 2)
803 write_u16(ptr, new);
804 else
805 *(uint8_t *)ptr = new;
806 }
807
808 tctx = calloc(sizeof(*tctx), count + 2);
809 if (!tctx)
810 goto fail;
811
812 tctx[0] = (unsigned long)count;
813 tctx[1] = (unsigned long)inter;
814
815 mask &= all_threads_mask;
816 if (!mask)
817 mask = tid_bit;
818
819 tmask = 0;
820 for (i = 0; i < count; i++) {
821 if (single || mode == 0) {
822 /* look for next bit matching a bit in mask or loop back to zero */
823 for (tmask <<= 1; !(mask & tmask); ) {
824 if (!(mask & -tmask))
825 tmask = 1;
826 else
827 tmask <<= 1;
828 }
829 } else {
830 /* multi-threaded task */
831 tmask = mask;
832 }
833
834 /* now, if poly or mask was set, tmask corresponds to the
835 * valid thread mask to use, otherwise it remains zero.
836 */
837 //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
838 if (mode == 0) {
839 struct tasklet *tl = tasklet_new();
840
841 if (!tl)
842 goto fail;
843
844 if (tmask)
845 tl->tid = my_ffsl(tmask) - 1;
846 tl->process = debug_tasklet_handler;
847 tl->context = tctx;
848 tctx[i + 2] = (unsigned long)tl;
849 } else {
850 struct task *task = task_new(tmask ? tmask : tid_bit);
851
852 if (!task)
853 goto fail;
854
855 task->process = debug_task_handler;
856 task->context = tctx;
857 tctx[i + 2] = (unsigned long)task + 1;
858 }
859 }
860
861 /* start the tasks and tasklets */
862 for (i = 0; i < count; i++) {
863 unsigned long ctx = tctx[i + 2];
864
865 if (ctx & 1)
866 task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
867 else
868 tasklet_wakeup((struct tasklet *)ctx);
869 }
870
871 if (msg && *msg)
872 return cli_dynmsg(appctx, LOG_INFO, msg);
873 return 1;
874
875 fail:
876 /* free partially allocated entries */
877 for (i = 0; tctx && i < count; i++) {
878 unsigned long ctx = tctx[i + 2];
879
880 if (!ctx)
881 break;
882
883 if (ctx & 1)
884 task_destroy((struct task *)(ctx - 1));
885 else
886 tasklet_free((struct tasklet *)ctx);
887 }
888
889 free(tctx);
890 return cli_err(appctx, "Not enough memory");
891}
892
Willy Tarreaua6026a02020-07-02 09:14:48 +0200893#if defined(DEBUG_MEM_STATS)
894/* CLI parser for the "debug dev memstats" command */
895static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
896{
897 extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
898 extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
899
900 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
901 return 1;
902
903 if (strcmp(args[3], "reset") == 0) {
904 struct mem_stats *ptr;
905
906 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
907 return 1;
908
909 for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
910 _HA_ATOMIC_STORE(&ptr->calls, 0);
911 _HA_ATOMIC_STORE(&ptr->size, 0);
912 }
913 return 1;
914 }
915
916 if (strcmp(args[3], "all") == 0)
917 appctx->ctx.cli.i0 = 1;
918
919 /* otherwise proceed with the dump from p0 to p1 */
920 appctx->ctx.cli.p0 = &__start_mem_stats;
921 appctx->ctx.cli.p1 = &__stop_mem_stats;
922 return 0;
923}
924
925/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
926 * structs referenced by pointers located between p0 and p1. Dumps all entries
927 * if i0 > 0, otherwise only non-zero calls.
928 */
929static int debug_iohandler_memstats(struct appctx *appctx)
930{
931 struct stream_interface *si = appctx->owner;
932 struct mem_stats *ptr = appctx->ctx.cli.p0;
933 int ret = 1;
934
935 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
936 goto end;
937
938 chunk_reset(&trash);
939
940 /* we have two inner loops here, one for the proxy, the other one for
941 * the buffer.
942 */
943 for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) {
944 const char *type;
945 const char *name;
946 const char *p;
947
948 if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0)
949 continue;
950
951 /* basename only */
952 for (p = name = ptr->file; *p; p++) {
953 if (*p == '/')
954 name = p + 1;
955 }
956
957 switch (ptr->type) {
958 case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break;
959 case MEM_STATS_TYPE_FREE: type = "FREE"; break;
960 case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break;
961 case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break;
962 case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break;
963 default: type = "UNSET"; break;
964 }
965
966 //chunk_printf(&trash,
967 // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n",
968 // name, ptr->line, type,
969 // (unsigned long)ptr->size, (unsigned long)ptr->calls,
970 // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
971
972 chunk_printf(&trash, "%s:%d", name, ptr->line);
973 while (trash.data < 25)
974 trash.area[trash.data++] = ' ';
975 chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n",
976 type,
977 (unsigned long)ptr->size, (unsigned long)ptr->calls,
978 (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0));
979
980 if (ci_putchk(si_ic(si), &trash) == -1) {
981 si_rx_room_blk(si);
982 appctx->ctx.cli.p0 = ptr;
983 ret = 0;
984 break;
985 }
986 }
987
988 end:
989 return ret;
990}
991
992#endif
993
Willy Tarreauc7091d82019-05-17 10:08:49 +0200994#ifndef USE_THREAD_DUMP
995
996/* This function dumps all threads' state to the trash. This version is the
997 * most basic one, which doesn't inspect other threads.
998 */
999void ha_thread_dump_all_to_trash()
1000{
1001 unsigned int thr;
1002
1003 for (thr = 0; thr < global.nbthread; thr++)
1004 ha_thread_dump(&trash, thr, tid);
1005}
1006
1007#else /* below USE_THREAD_DUMP is set */
1008
Willy Tarreauc7091d82019-05-17 10:08:49 +02001009/* ID of the thread requesting the dump */
1010static unsigned int thread_dump_tid;
1011
1012/* points to the buffer where the dump functions should write. It must
1013 * have already been initialized by the requester. Nothing is done if
1014 * it's NULL.
1015 */
1016struct buffer *thread_dump_buffer = NULL;
1017
1018void ha_thread_dump_all_to_trash()
1019{
Willy Tarreauc7091d82019-05-17 10:08:49 +02001020 unsigned long old;
1021
1022 while (1) {
1023 old = 0;
1024 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
1025 break;
1026 ha_thread_relax();
1027 }
1028
1029 thread_dump_buffer = &trash;
1030 thread_dump_tid = tid;
Willy Tarreaufade80d2019-05-22 08:46:59 +02001031 ha_tkillall(DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001032}
1033
1034/* handles DEBUGSIG to dump the state of the thread it's working on */
1035void debug_handler(int sig, siginfo_t *si, void *arg)
1036{
Willy Tarreau82aafc42020-03-03 08:31:34 +01001037 /* first, let's check it's really for us and that we didn't just get
1038 * a spurious DEBUGSIG.
1039 */
1040 if (!(threads_to_dump & tid_bit))
1041 return;
1042
Willy Tarreauc7091d82019-05-17 10:08:49 +02001043 /* There are 4 phases in the dump process:
1044 * 1- wait for our turn, i.e. when all lower bits are gone.
1045 * 2- perform the action if our bit is set
1046 * 3- remove our bit to let the next one go, unless we're
Willy Tarreauc0773622019-07-31 19:15:45 +02001047 * the last one and have to put them all as a signal
1048 * 4- wait out bit to re-appear, then clear it and quit.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001049 */
1050
1051 /* wait for all previous threads to finish first */
1052 while (threads_to_dump & (tid_bit - 1))
1053 ha_thread_relax();
1054
1055 /* dump if needed */
1056 if (threads_to_dump & tid_bit) {
1057 if (thread_dump_buffer)
1058 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
1059 if ((threads_to_dump & all_threads_mask) == tid_bit) {
1060 /* last one */
Willy Tarreauc0773622019-07-31 19:15:45 +02001061 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask);
Willy Tarreauc7091d82019-05-17 10:08:49 +02001062 thread_dump_buffer = NULL;
1063 }
1064 else
1065 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
1066 }
1067
1068 /* now wait for all others to finish dumping. The last one will set all
Willy Tarreauc0773622019-07-31 19:15:45 +02001069 * bits again to broadcast the leaving condition so we'll see ourselves
1070 * present again. This way the threads_to_dump variable never passes to
1071 * zero until all visitors have stopped waiting.
Willy Tarreauc7091d82019-05-17 10:08:49 +02001072 */
Willy Tarreauc0773622019-07-31 19:15:45 +02001073 while (!(threads_to_dump & tid_bit))
1074 ha_thread_relax();
1075 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
Willy Tarreaue6a02fa2019-05-22 07:06:44 +02001076
1077 /* mark the current thread as stuck to detect it upon next invocation
1078 * if it didn't move.
1079 */
1080 if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit))
1081 ti->flags |= TI_FL_STUCK;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001082}
1083
1084static int init_debug_per_thread()
1085{
1086 sigset_t set;
1087
1088 /* unblock the DEBUGSIG signal we intend to use */
1089 sigemptyset(&set);
1090 sigaddset(&set, DEBUGSIG);
1091 ha_sigmask(SIG_UNBLOCK, &set, NULL);
1092 return 1;
1093}
1094
1095static int init_debug()
1096{
1097 struct sigaction sa;
1098
Willy Tarreau0214b452020-03-04 06:01:40 +01001099#ifdef USE_BACKTRACE
1100 /* calling backtrace() will access libgcc at runtime. We don't want to
1101 * do it after the chroot, so let's perform a first call to have it
1102 * ready in memory for later use.
1103 */
1104 void *callers[1];
Willy Tarreau13faf162020-03-04 07:44:06 +01001105 my_backtrace(callers, sizeof(callers)/sizeof(*callers));
Willy Tarreau0214b452020-03-04 06:01:40 +01001106#endif
Willy Tarreauc7091d82019-05-17 10:08:49 +02001107 sa.sa_handler = NULL;
1108 sa.sa_sigaction = debug_handler;
1109 sigemptyset(&sa.sa_mask);
1110 sa.sa_flags = SA_SIGINFO;
1111 sigaction(DEBUGSIG, &sa, NULL);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001112 return ERR_NONE;
Willy Tarreauc7091d82019-05-17 10:08:49 +02001113}
1114
1115REGISTER_POST_CHECK(init_debug);
1116REGISTER_PER_THREAD_INIT(init_debug_per_thread);
1117
1118#endif /* USE_THREAD_DUMP */
1119
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001120/* register cli keywords */
1121static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub24ab222019-10-24 18:03:39 +02001122 {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT },
1123 {{ "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 +02001124#if defined(DEBUG_DEV)
Willy Tarreaub24ab222019-10-24 18:03:39 +02001125 {{ "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 +02001126#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001127 {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT },
1128 {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT },
1129 {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT },
1130 {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua6026a02020-07-02 09:14:48 +02001131#if defined(DEBUG_MEM_STATS)
1132 {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT },
1133#endif
Willy Tarreaub24ab222019-10-24 18:03:39 +02001134 {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaua5a44792020-11-29 17:12:15 +01001135 {{ "debug", "dev", "sched", NULL }, "debug dev sched ... : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT },
Willy Tarreaub24ab222019-10-24 18:03:39 +02001136 {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
1137 {{ "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 +01001138 {{ "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 +02001139 {{ "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 +02001140 {{},}
1141}};
1142
1143INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);