Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 16 | #include <stdlib.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 17 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 18 | #include <common/buf.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 19 | #include <common/config.h> |
| 20 | #include <common/debug.h> |
| 21 | #include <common/hathreads.h> |
| 22 | #include <common/initcall.h> |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 23 | #include <common/ist.h> |
| 24 | #include <common/net_helper.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 25 | #include <common/standard.h> |
| 26 | |
| 27 | #include <types/global.h> |
| 28 | |
| 29 | #include <proto/cli.h> |
| 30 | #include <proto/fd.h> |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 31 | #include <proto/hlua.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 32 | #include <proto/stream_interface.h> |
| 33 | #include <proto/task.h> |
| 34 | |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 35 | /* mask of threads still having to dump, used to respect ordering. Only used |
| 36 | * when USE_THREAD_DUMP is set. |
| 37 | */ |
| 38 | volatile unsigned long threads_to_dump = 0; |
| 39 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 40 | /* 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 Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 44 | * in front of the calling thread's line (usually it's tid). Any stuck thread |
| 45 | * is also prefixed with a '>'. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 46 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 47 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 48 | { |
| 49 | unsigned long thr_bit = 1UL << thr; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 50 | 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 54 | chunk_appendf(buf, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 55 | "%c%cThread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n" |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 56 | " stuck=%d prof=%d", |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 57 | (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 58 | thread_has_tasks(), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 59 | !!(global_tasks_mask & thr_bit), |
| 60 | !eb_is_empty(&task_per_thread[thr].timers), |
| 61 | !eb_is_empty(&task_per_thread[thr].rqueue), |
Olivier Houchard | 0691046 | 2019-10-11 16:35:01 +0200 | [diff] [blame] | 62 | !(LIST_ISEMPTY(&task_per_thread[thr].task_list) | |
| 63 | MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 64 | task_per_thread[thr].task_list_size, |
| 65 | task_per_thread[thr].rqueue_size, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 66 | stuck, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 67 | !!(task_profiling_mask & thr_bit)); |
| 68 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 69 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 70 | " harmless=%d wantrdv=%d", |
| 71 | !!(threads_harmless_mask & thr_bit), |
| 72 | !!(threads_want_rdv_mask & thr_bit)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 74 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 75 | chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 76 | |
| 77 | /* this is the end of what we can dump from outside the thread */ |
| 78 | |
| 79 | if (thr != tid) |
| 80 | return; |
| 81 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 82 | chunk_appendf(buf, " curr_task="); |
Willy Tarreau | d022e9c | 2019-09-24 08:25:15 +0200 | [diff] [blame] | 83 | ha_task_dump(buf, sched->current, " "); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 87 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 88 | * either be a task or a tasklet, and prepend each line except the first one |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 89 | * 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 92 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 93 | void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 94 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 95 | const struct stream *s = NULL; |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 96 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 97 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 99 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 100 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 104 | 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 117 | chunk_appendf(buf, "%s" |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 118 | " fct=%p (%s) ctx=%p", |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 119 | pfx, |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 120 | 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 Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 124 | #ifdef USE_LUA |
| 125 | task->process == hlua_process_task ? "hlua_process_task" : |
| 126 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 127 | "?", |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 128 | task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 130 | 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 Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 135 | 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 Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 144 | |
| 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 170 | /* 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 | */ |
| 173 | static 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 Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 186 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 187 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 188 | |
| 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 195 | return 1; |
| 196 | } |
| 197 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 198 | /* dumps a state of all threads into the trash and on fd #2, then aborts. */ |
| 199 | void ha_panic() |
| 200 | { |
| 201 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 202 | chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 203 | ha_thread_dump_all_to_trash(); |
Tim Duesterhus | dda1155 | 2019-06-12 20:47:30 +0200 | [diff] [blame] | 204 | shut_your_big_mouth_gcc(write(2, trash.area, trash.data)); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 205 | for (;;) |
| 206 | abort(); |
| 207 | } |
| 208 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 209 | #if defined(DEBUG_DEV) |
| 210 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 211 | static 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. */ |
| 223 | static 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 Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 230 | if (!*args[3]) |
| 231 | return cli_err(appctx, "Missing file descriptor number.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 232 | |
| 233 | fd = atoi(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 234 | if (fd < 0 || fd >= global.maxsock) |
| 235 | return cli_err(appctx, "File descriptor out of range.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 237 | if (!fdtab[fd].owner) |
| 238 | return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 239 | |
| 240 | fd_delete(fd); |
| 241 | return 1; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 245 | static 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. */ |
| 257 | static 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. */ |
| 276 | static 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. */ |
| 294 | static 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. */ |
| 304 | static 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 Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 320 | if (!f) |
| 321 | return cli_err(appctx, "Failed to execute command.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 322 | |
| 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 Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 337 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 341 | static 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 Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 348 | if (!*args[3]) |
| 349 | return cli_err(appctx, "Missing memory address to dump from.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 350 | |
| 351 | start = strtoul(args[3], NULL, 0); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 352 | if (!start) |
| 353 | return cli_err(appctx, "Will not dump from NULL address.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 354 | |
| 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 Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 361 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 362 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 363 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 367 | static 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 Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 378 | if (thr < 0 || thr > global.nbthread) |
| 379 | return cli_err(appctx, "Thread number out of range (use 0 for current).\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 380 | |
| 381 | if (*args[4]) |
| 382 | sig = atoi(args[4]); |
| 383 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 384 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 385 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 386 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 387 | raise(sig); |
| 388 | return 1; |
| 389 | } |
| 390 | |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 391 | /* 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 | */ |
| 398 | static 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 Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 463 | if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1)) |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 464 | 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 Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 470 | if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 471 | 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 Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 479 | } else { |
| 480 | memprintf(&msg, |
| 481 | "%sSkipping inaccessible pointer %p for field '%.*s'.\n", |
| 482 | msg ? msg : "", ptr, (int)(end - word), word); |
| 483 | continue; |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 484 | } |
| 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 Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 525 | if (new != old) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 526 | 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 Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 542 | #endif |
| 543 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 544 | #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 | */ |
| 549 | void 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 Tarreau | ddd8533 | 2019-05-22 06:28:54 +0200 | [diff] [blame] | 559 | /* 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 Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 564 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 565 | /* ID of the thread requesting the dump */ |
| 566 | static 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 | */ |
| 572 | struct buffer *thread_dump_buffer = NULL; |
| 573 | |
| 574 | void ha_thread_dump_all_to_trash() |
| 575 | { |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 576 | 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 Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 587 | ha_tkillall(DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* handles DEBUGSIG to dump the state of the thread it's working on */ |
| 591 | void 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 Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 597 | * 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 Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 599 | */ |
| 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 Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 611 | HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 612 | 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 Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 619 | * 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 Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 622 | */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 623 | while (!(threads_to_dump & tid_bit)) |
| 624 | ha_thread_relax(); |
| 625 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 626 | |
| 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 Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static 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 | |
| 645 | static 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 | |
| 657 | REGISTER_POST_CHECK(init_debug); |
| 658 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 659 | |
| 660 | #endif /* USE_THREAD_DUMP */ |
| 661 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 662 | /* register cli keywords */ |
| 663 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 664 | #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 Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 673 | {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL }, |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 674 | {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL }, |
| 675 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 676 | { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL }, |
| 677 | {{},} |
| 678 | }}; |
| 679 | |
| 680 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |