blob: 3e38bfa6c225aad79000b1220b79b994e18a2671 [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>
16
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020017#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020018#include <common/config.h>
19#include <common/debug.h>
20#include <common/hathreads.h>
21#include <common/initcall.h>
22#include <common/standard.h>
23
24#include <types/global.h>
25
26#include <proto/cli.h>
27#include <proto/fd.h>
28#include <proto/stream_interface.h>
29#include <proto/task.h>
30
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020031/* Dumps to the buffer some known information for the desired thread, and
32 * optionally extra info for the current thread. The dump will be appended to
33 * the buffer, so the caller is responsible for preliminary initializing it.
34 * The calling thread ID needs to be passed in <calling_tid> to display a star
35 * in front of the calling thread's line (usually it's tid).
Willy Tarreau4e2b6462019-05-16 17:44:30 +020036 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020037void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020038{
39 unsigned long thr_bit = 1UL << thr;
40
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020041 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020042 "%c Thread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
43 " fdcache=%d prof=%d",
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020044 (thr == calling_tid) ? '*' : ' ', thr + 1,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020045 !!(active_tasks_mask & thr_bit),
46 !!(global_tasks_mask & thr_bit),
47 !eb_is_empty(&task_per_thread[thr].timers),
48 !eb_is_empty(&task_per_thread[thr].rqueue),
49 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
50 task_per_thread[thr].task_list_size,
51 task_per_thread[thr].rqueue_size,
52 !!(fd_cache_mask & thr_bit),
53 !!(task_profiling_mask & thr_bit));
54
55#ifdef USE_THREAD
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020056 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020057 " harmless=%d wantrdv=%d",
58 !!(threads_harmless_mask & thr_bit),
59 !!(threads_want_rdv_mask & thr_bit));
60#endif
61
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020062 chunk_appendf(buf, "\n");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020063
64 /* this is the end of what we can dump from outside the thread */
65
66 if (thr != tid)
67 return;
68
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020069 chunk_appendf(buf, " curr_task=");
70 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020071}
72
73
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020074/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020075 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020076 * with <pfx>. The buffer is only appended and the first output starts by the
77 * pointer itself. The caller is responsible for making sure the task is not
78 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020079 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020080void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020081{
Willy Tarreau14a1ab72019-05-17 10:34:25 +020082 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020083 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020084 return;
85 }
86
Willy Tarreau20db9112019-05-17 14:14:35 +020087 if (TASK_IS_TASKLET(task))
88 chunk_appendf(buf,
89 "%p (tasklet) calls=%u\n",
90 task,
91 task->calls);
92 else
93 chunk_appendf(buf,
94 "%p (task) calls=%u last=%llu%s\n",
95 task,
96 task->calls,
97 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
98 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020099
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200100 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200101 " fct=%p (%s) ctx=%p\n",
102 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200103 task->process,
104 task->process == process_stream ? "process_stream" :
105 task->process == task_run_applet ? "task_run_applet" :
106 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200107 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200108 task->context);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200109}
110
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200111/* This function dumps all profiling settings. It returns 0 if the output
112 * buffer is full and it needs to be called again, otherwise non-zero.
113 */
114static int cli_io_handler_show_threads(struct appctx *appctx)
115{
116 struct stream_interface *si = appctx->owner;
117 int thr;
118
119 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
120 return 1;
121
122 if (appctx->st0)
123 thr = appctx->st1;
124 else
125 thr = 0;
126
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200127 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200128 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200129
130 if (ci_putchk(si_ic(si), &trash) == -1) {
131 /* failed, try again */
132 si_rx_room_blk(si);
133 appctx->st1 = thr;
134 return 0;
135 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200136 return 1;
137}
138
Willy Tarreau56131ca2019-05-20 13:48:29 +0200139/* dumps a state of all threads into the trash and on fd #2, then aborts. */
140void ha_panic()
141{
142 chunk_reset(&trash);
143 chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid);
144 ha_thread_dump_all_to_trash();
145 write(2, trash.area, trash.data);
146 for (;;)
147 abort();
148}
149
Willy Tarreauc7091d82019-05-17 10:08:49 +0200150#ifndef USE_THREAD_DUMP
151
152/* This function dumps all threads' state to the trash. This version is the
153 * most basic one, which doesn't inspect other threads.
154 */
155void ha_thread_dump_all_to_trash()
156{
157 unsigned int thr;
158
159 for (thr = 0; thr < global.nbthread; thr++)
160 ha_thread_dump(&trash, thr, tid);
161}
162
163#else /* below USE_THREAD_DUMP is set */
164
165/* The signal to trigger a debug dump on a thread is SIGPWR */
166#define DEBUGSIG SIGPWR
167
168/* mask of threads still having to dump, used to respect ordering */
169static volatile unsigned long threads_to_dump;
170
171/* ID of the thread requesting the dump */
172static unsigned int thread_dump_tid;
173
174/* points to the buffer where the dump functions should write. It must
175 * have already been initialized by the requester. Nothing is done if
176 * it's NULL.
177 */
178struct buffer *thread_dump_buffer = NULL;
179
180void ha_thread_dump_all_to_trash()
181{
182 __maybe_unused unsigned int thr;
183 unsigned long old;
184
185 while (1) {
186 old = 0;
187 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
188 break;
189 ha_thread_relax();
190 }
191
192 thread_dump_buffer = &trash;
193 thread_dump_tid = tid;
194
195#ifdef USE_THREAD
196 for (thr = 0; thr < global.nbthread; thr++) {
197 if (thr != tid)
Willy Tarreau522cfbc2019-05-03 10:16:39 +0200198 pthread_kill(thread_info[thr].pthread, DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200199 }
200#endif
201 /* dump ourselves last */
202 raise(DEBUGSIG);
203}
204
205/* handles DEBUGSIG to dump the state of the thread it's working on */
206void debug_handler(int sig, siginfo_t *si, void *arg)
207{
208 /* There are 4 phases in the dump process:
209 * 1- wait for our turn, i.e. when all lower bits are gone.
210 * 2- perform the action if our bit is set
211 * 3- remove our bit to let the next one go, unless we're
212 * the last one and have to put them all but ours
213 * 4- wait for zero and clear our bit if it's set
214 */
215
216 /* wait for all previous threads to finish first */
217 while (threads_to_dump & (tid_bit - 1))
218 ha_thread_relax();
219
220 /* dump if needed */
221 if (threads_to_dump & tid_bit) {
222 if (thread_dump_buffer)
223 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
224 if ((threads_to_dump & all_threads_mask) == tid_bit) {
225 /* last one */
226 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask & ~tid_bit);
227 thread_dump_buffer = NULL;
228 }
229 else
230 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
231 }
232
233 /* now wait for all others to finish dumping. The last one will set all
234 * bits again to broadcast the leaving condition.
235 */
236 while (threads_to_dump & all_threads_mask) {
237 if (threads_to_dump & tid_bit)
238 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
239 else
240 ha_thread_relax();
241 }
242}
243
244static int init_debug_per_thread()
245{
246 sigset_t set;
247
248 /* unblock the DEBUGSIG signal we intend to use */
249 sigemptyset(&set);
250 sigaddset(&set, DEBUGSIG);
251 ha_sigmask(SIG_UNBLOCK, &set, NULL);
252 return 1;
253}
254
255static int init_debug()
256{
257 struct sigaction sa;
258
259 sa.sa_handler = NULL;
260 sa.sa_sigaction = debug_handler;
261 sigemptyset(&sa.sa_mask);
262 sa.sa_flags = SA_SIGINFO;
263 sigaction(DEBUGSIG, &sa, NULL);
264 return 0;
265}
266
267REGISTER_POST_CHECK(init_debug);
268REGISTER_PER_THREAD_INIT(init_debug_per_thread);
269
270#endif /* USE_THREAD_DUMP */
271
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200272/* register cli keywords */
273static struct cli_kw_list cli_kws = {{ },{
274 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
275 {{},}
276}};
277
278INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);