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 | |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 13 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 14 | #include <fcntl.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 15 | #include <signal.h> |
| 16 | #include <time.h> |
| 17 | #include <stdio.h> |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 19 | #include <syslog.h> |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 23 | #include <haproxy/api.h> |
Willy Tarreau | 8dabda7 | 2020-05-27 17:22:10 +0200 | [diff] [blame] | 24 | #include <haproxy/buf.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 26 | #include <haproxy/clock.h> |
Willy Tarreau | 2a83d60 | 2020-05-27 16:58:08 +0200 | [diff] [blame] | 27 | #include <haproxy/debug.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/fd.h> |
| 29 | #include <haproxy/global.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 30 | #include <haproxy/hlua.h> |
Willy Tarreau | b7fc4c4 | 2021-10-06 18:56:42 +0200 | [diff] [blame] | 31 | #include <haproxy/http_ana.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 32 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/net_helper.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 35 | #include <haproxy/task.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 36 | #include <haproxy/thread.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 37 | #include <haproxy/time.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 38 | #include <haproxy/tools.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <import/ist.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 42 | /* mask of threads still having to dump, used to respect ordering. Only used |
| 43 | * when USE_THREAD_DUMP is set. |
| 44 | */ |
| 45 | volatile unsigned long threads_to_dump = 0; |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 46 | unsigned int debug_commands_issued = 0; |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 48 | /* dumps a backtrace of the current thread that is appended to buffer <buf>. |
| 49 | * Lines are prefixed with the string <prefix> which may be empty (used for |
| 50 | * indenting). It is recommended to use this at a function's tail so that |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 51 | * the function does not appear in the call stack. The <dump> argument |
| 52 | * indicates what dump state to start from, and should usually be zero. It |
| 53 | * may be among the following values: |
| 54 | * - 0: search usual callers before step 1, or directly jump to 2 |
| 55 | * - 1: skip usual callers before step 2 |
| 56 | * - 2: dump until polling loop, scheduler, or main() (excluded) |
| 57 | * - 3: end |
| 58 | * - 4-7: like 0 but stops *after* main. |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 59 | */ |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 60 | void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump) |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 61 | { |
| 62 | struct buffer bak; |
| 63 | char pfx2[100]; |
| 64 | void *callers[100]; |
| 65 | int j, nptrs; |
| 66 | const void *addr; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 67 | |
| 68 | nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
| 69 | if (!nptrs) |
| 70 | return; |
| 71 | |
| 72 | if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2)) |
| 73 | pfx2[0] = 0; |
| 74 | |
| 75 | /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would |
| 76 | * produce similar output to the following: |
| 77 | */ |
| 78 | chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs); |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 79 | for (j = 0; (j < nptrs || (dump & 3) < 2); j++) { |
| 80 | if (j == nptrs && !(dump & 3)) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 81 | /* we failed to spot the starting point of the |
| 82 | * dump, let's start over dumping everything we |
| 83 | * have. |
| 84 | */ |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 85 | dump += 2; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 86 | j = 0; |
| 87 | } |
| 88 | bak = *buf; |
| 89 | dump_addr_and_bytes(buf, pfx2, callers[j], 8); |
| 90 | addr = resolve_sym_name(buf, ": ", callers[j]); |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 91 | if ((dump & 3) == 0) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 92 | /* dump not started, will start *after* |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 93 | * ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 94 | */ |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 95 | if (addr == ha_thread_dump_all_to_trash || addr == ha_panic || |
| 96 | addr == ha_backtrace_to_stderr) |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 97 | dump++; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 98 | *buf = bak; |
| 99 | continue; |
| 100 | } |
| 101 | |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 102 | if ((dump & 3) == 1) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 103 | /* starting */ |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 104 | if (addr == ha_thread_dump_all_to_trash || addr == ha_panic || |
| 105 | addr == ha_backtrace_to_stderr) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 106 | *buf = bak; |
| 107 | continue; |
| 108 | } |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 109 | dump++; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 112 | if ((dump & 3) == 2) { |
| 113 | /* still dumping */ |
| 114 | if (dump == 6) { |
| 115 | /* we only stop *after* main and we must send the LF */ |
| 116 | if (addr == main) { |
| 117 | j = nptrs; |
| 118 | dump++; |
| 119 | } |
| 120 | } |
| 121 | else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) { |
| 122 | dump++; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 123 | *buf = bak; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | /* OK, line dumped */ |
| 128 | chunk_appendf(buf, "\n"); |
| 129 | } |
| 130 | } |
| 131 | |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 132 | /* dump a backtrace of current thread's stack to stderr. */ |
| 133 | void ha_backtrace_to_stderr() |
| 134 | { |
| 135 | char area[2048]; |
| 136 | struct buffer b = b_make(area, sizeof(area), 0, 0); |
| 137 | |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 138 | ha_dump_backtrace(&b, " ", 4); |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 139 | if (b.data) |
Willy Tarreau | 2cbe2e7 | 2021-01-22 15:58:26 +0100 | [diff] [blame] | 140 | DISGUISE(write(2, b.area, b.data)); |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 143 | /* Dumps to the buffer some known information for the desired thread, and |
| 144 | * optionally extra info for the current thread. The dump will be appended to |
| 145 | * the buffer, so the caller is responsible for preliminary initializing it. |
| 146 | * 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] | 147 | * in front of the calling thread's line (usually it's tid). Any stuck thread |
| 148 | * is also prefixed with a '>'. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 149 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 150 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 151 | { |
| 152 | unsigned long thr_bit = 1UL << thr; |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 153 | unsigned long long p = ha_thread_ctx[thr].prev_cpu_time; |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 154 | unsigned long long n = now_cpu_time_thread(thr); |
Willy Tarreau | a0b9953 | 2021-09-30 18:48:37 +0200 | [diff] [blame] | 155 | int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 156 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 157 | chunk_appendf(buf, |
Willy Tarreau | f0e5da2 | 2020-05-01 12:26:03 +0200 | [diff] [blame] | 158 | "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n" |
Willy Tarreau | a3870b7 | 2021-09-13 19:24:47 +0200 | [diff] [blame] | 159 | " %2u/%-2u stuck=%d prof=%d", |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 160 | (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Willy Tarreau | ff64d3b | 2020-05-01 11:28:49 +0200 | [diff] [blame] | 161 | ha_get_pthread_id(thr), |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 162 | thread_has_tasks(), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 163 | !!(global_tasks_mask & thr_bit), |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 164 | !eb_is_empty(&ha_thread_ctx[thr].timers), |
| 165 | !eb_is_empty(&ha_thread_ctx[thr].rqueue), |
| 166 | !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) && |
| 167 | LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) && |
| 168 | LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) && |
| 169 | MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)), |
| 170 | ha_thread_ctx[thr].tasks_in_list, |
| 171 | ha_thread_ctx[thr].rq_total, |
Willy Tarreau | a3870b7 | 2021-09-13 19:24:47 +0200 | [diff] [blame] | 172 | ha_thread_info[thr].tg->tgid, ha_thread_info[thr].ltid + 1, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 173 | stuck, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 174 | !!(task_profiling_mask & thr_bit)); |
| 175 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 176 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 177 | " harmless=%d wantrdv=%d", |
| 178 | !!(threads_harmless_mask & thr_bit), |
| 179 | !!(threads_want_rdv_mask & thr_bit)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 181 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 182 | 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] | 183 | |
Willy Tarreau | a3870b7 | 2021-09-13 19:24:47 +0200 | [diff] [blame] | 184 | /* this is the end of what we can dump from outside the current thread */ |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 185 | |
| 186 | if (thr != tid) |
| 187 | return; |
| 188 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 189 | chunk_appendf(buf, " curr_task="); |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 190 | ha_task_dump(buf, th_ctx->current, " "); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 191 | |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 192 | if (stuck) { |
| 193 | /* We only emit the backtrace for stuck threads in order not to |
| 194 | * waste precious output buffer space with non-interesting data. |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 195 | * Please leave this as the last instruction in this function |
| 196 | * so that the compiler uses tail merging and the current |
| 197 | * function does not appear in the stack. |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 198 | */ |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 199 | ha_dump_backtrace(buf, " ", 0); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 200 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 204 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 205 | * 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] | 206 | * with <pfx>. The buffer is only appended and the first output starts by the |
| 207 | * pointer itself. The caller is responsible for making sure the task is not |
| 208 | * going to vanish during the dump. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 209 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 210 | 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] | 211 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 212 | const struct stream *s = NULL; |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 213 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 214 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 216 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 217 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 221 | if (TASK_IS_TASKLET(task)) |
| 222 | chunk_appendf(buf, |
| 223 | "%p (tasklet) calls=%u\n", |
| 224 | task, |
| 225 | task->calls); |
| 226 | else |
| 227 | chunk_appendf(buf, |
| 228 | "%p (task) calls=%u last=%llu%s\n", |
| 229 | task, |
| 230 | task->calls, |
| 231 | task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0, |
| 232 | task->call_date ? " ns ago" : ""); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 233 | |
Willy Tarreau | 2e89b09 | 2020-03-03 17:13:02 +0100 | [diff] [blame] | 234 | chunk_appendf(buf, "%s fct=%p(", pfx, task->process); |
| 235 | resolve_sym_name(buf, NULL, task->process); |
| 236 | chunk_appendf(buf,") ctx=%p", task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 237 | |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 238 | if (task->process == task_run_applet && (appctx = task->context)) |
| 239 | chunk_appendf(buf, "(%s)\n", appctx->applet->name); |
| 240 | else |
| 241 | chunk_appendf(buf, "\n"); |
| 242 | |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 243 | if (task->process == process_stream && task->context) |
| 244 | s = (struct stream *)task->context; |
| 245 | else if (task->process == task_run_applet && task->context) |
| 246 | s = si_strm(((struct appctx *)task->context)->owner); |
| 247 | else if (task->process == si_cs_io_cb && task->context) |
| 248 | s = si_strm((struct stream_interface *)task->context); |
| 249 | |
| 250 | if (s) |
| 251 | stream_dump(buf, s, pfx, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 252 | |
| 253 | #ifdef USE_LUA |
| 254 | hlua = NULL; |
| 255 | if (s && (hlua = s->hlua)) { |
| 256 | chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); |
| 257 | } |
| 258 | else if (task->process == hlua_process_task && (hlua = task->context)) { |
| 259 | chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx); |
| 260 | } |
| 261 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 262 | (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) { |
| 263 | chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx); |
| 264 | } |
| 265 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 266 | (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) { |
| 267 | chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx); |
| 268 | } |
| 269 | |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 270 | if (hlua && hlua->T) { |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 271 | chunk_appendf(buf, "stack traceback:\n "); |
| 272 | append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0); |
| 273 | b_putchr(buf, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 274 | } |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 275 | else |
| 276 | b_putchr(buf, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 277 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 280 | /* This function dumps all profiling settings. It returns 0 if the output |
| 281 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 282 | */ |
| 283 | static int cli_io_handler_show_threads(struct appctx *appctx) |
| 284 | { |
| 285 | struct stream_interface *si = appctx->owner; |
| 286 | int thr; |
| 287 | |
| 288 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 289 | return 1; |
| 290 | |
| 291 | if (appctx->st0) |
| 292 | thr = appctx->st1; |
| 293 | else |
| 294 | thr = 0; |
| 295 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 296 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 297 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 298 | |
| 299 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 300 | /* failed, try again */ |
| 301 | si_rx_room_blk(si); |
| 302 | appctx->st1 = thr; |
| 303 | return 0; |
| 304 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 305 | return 1; |
| 306 | } |
| 307 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 308 | /* dumps a state of all threads into the trash and on fd #2, then aborts. */ |
| 309 | void ha_panic() |
| 310 | { |
| 311 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 312 | 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] | 313 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 314 | DISGUISE(write(2, trash.area, trash.data)); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 315 | for (;;) |
| 316 | abort(); |
| 317 | } |
| 318 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 319 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 320 | static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private) |
| 321 | { |
| 322 | int code = atoi(args[3]); |
| 323 | |
| 324 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 325 | return 1; |
| 326 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 327 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 328 | exit(code); |
| 329 | return 1; |
| 330 | } |
| 331 | |
Willy Tarreau | 5baf4fe | 2021-01-22 14:15:46 +0100 | [diff] [blame] | 332 | /* parse a "debug dev bug" command. It always returns 1, though it should never return. |
| 333 | * Note: we make sure not to make the function static so that it appears in the trace. |
| 334 | */ |
| 335 | int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private) |
| 336 | { |
| 337 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 338 | return 1; |
| 339 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 340 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 5baf4fe | 2021-01-22 14:15:46 +0100 | [diff] [blame] | 341 | BUG_ON(one > zero); |
| 342 | return 1; |
| 343 | } |
| 344 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 345 | /* parse a "debug dev close" command. It always returns 1. */ |
| 346 | static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private) |
| 347 | { |
| 348 | int fd; |
| 349 | |
| 350 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 351 | return 1; |
| 352 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 353 | if (!*args[3]) |
| 354 | return cli_err(appctx, "Missing file descriptor number.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 355 | |
| 356 | fd = atoi(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 357 | if (fd < 0 || fd >= global.maxsock) |
| 358 | return cli_err(appctx, "File descriptor out of range.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 359 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 360 | if (!fdtab[fd].owner) |
| 361 | return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 362 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 363 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 364 | fd_delete(fd); |
| 365 | return 1; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 369 | static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private) |
| 370 | { |
| 371 | int delay = atoi(args[3]); |
| 372 | |
| 373 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 374 | return 1; |
| 375 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 376 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 377 | usleep((long)delay * 1000); |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | /* parse a "debug dev log" command. It always returns 1. */ |
| 382 | static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private) |
| 383 | { |
| 384 | int arg; |
| 385 | |
| 386 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 387 | return 1; |
| 388 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 389 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 390 | chunk_reset(&trash); |
| 391 | for (arg = 3; *args[arg]; arg++) { |
| 392 | if (arg > 3) |
| 393 | chunk_strcat(&trash, " "); |
| 394 | chunk_strcat(&trash, args[arg]); |
| 395 | } |
| 396 | |
| 397 | send_log(NULL, LOG_INFO, "%s\n", trash.area); |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | /* parse a "debug dev loop" command. It always returns 1. */ |
| 402 | static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private) |
| 403 | { |
| 404 | struct timeval deadline, curr; |
| 405 | int loop = atoi(args[3]); |
| 406 | |
| 407 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 408 | return 1; |
| 409 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 410 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 411 | gettimeofday(&curr, NULL); |
| 412 | tv_ms_add(&deadline, &curr, loop); |
| 413 | |
| 414 | while (tv_ms_cmp(&curr, &deadline) < 0) |
| 415 | gettimeofday(&curr, NULL); |
| 416 | |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | /* parse a "debug dev panic" command. It always returns 1, though it should never return. */ |
| 421 | static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private) |
| 422 | { |
| 423 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 424 | return 1; |
| 425 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 426 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 427 | ha_panic(); |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | /* parse a "debug dev exec" command. It always returns 1. */ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 432 | #if defined(DEBUG_DEV) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 433 | static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private) |
| 434 | { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 435 | int pipefd[2]; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 436 | int arg; |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 437 | int pid; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 438 | |
| 439 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 440 | return 1; |
| 441 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 442 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 443 | chunk_reset(&trash); |
| 444 | for (arg = 3; *args[arg]; arg++) { |
| 445 | if (arg > 3) |
| 446 | chunk_strcat(&trash, " "); |
| 447 | chunk_strcat(&trash, args[arg]); |
| 448 | } |
| 449 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 450 | thread_isolate(); |
| 451 | if (pipe(pipefd) < 0) |
| 452 | goto fail_pipe; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 453 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 454 | if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 455 | goto fail_fcntl; |
| 456 | |
| 457 | if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 458 | goto fail_fcntl; |
| 459 | |
| 460 | pid = fork(); |
| 461 | |
| 462 | if (pid < 0) |
| 463 | goto fail_fork; |
| 464 | else if (pid == 0) { |
| 465 | /* child */ |
| 466 | char *cmd[4] = { "/bin/sh", "-c", 0, 0 }; |
| 467 | |
| 468 | close(0); |
| 469 | dup2(pipefd[1], 1); |
| 470 | dup2(pipefd[1], 2); |
| 471 | |
| 472 | cmd[2] = trash.area; |
| 473 | execvp(cmd[0], cmd); |
| 474 | printf("execvp() failed\n"); |
| 475 | exit(1); |
| 476 | } |
| 477 | |
| 478 | /* parent */ |
| 479 | thread_release(); |
| 480 | close(pipefd[1]); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 481 | chunk_reset(&trash); |
| 482 | while (1) { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 483 | size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data); |
| 484 | if (ret <= 0) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 485 | break; |
| 486 | trash.data += ret; |
| 487 | if (trash.data + 20 == trash.size) { |
| 488 | chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n"); |
| 489 | break; |
| 490 | } |
| 491 | } |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 492 | close(pipefd[0]); |
| 493 | waitpid(pid, NULL, WNOHANG); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 494 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 495 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 496 | |
| 497 | fail_fork: |
| 498 | fail_fcntl: |
| 499 | close(pipefd[0]); |
| 500 | close(pipefd[1]); |
| 501 | fail_pipe: |
| 502 | thread_release(); |
| 503 | return cli_err(appctx, "Failed to execute command.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 504 | } |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 505 | #endif |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 506 | |
| 507 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 508 | static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private) |
| 509 | { |
| 510 | unsigned long start, len; |
| 511 | |
| 512 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 513 | return 1; |
| 514 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 515 | if (!*args[3]) |
| 516 | return cli_err(appctx, "Missing memory address to dump from.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 517 | |
| 518 | start = strtoul(args[3], NULL, 0); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 519 | if (!start) |
| 520 | return cli_err(appctx, "Will not dump from NULL address.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 521 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 522 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 523 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 524 | /* by default, dump ~128 till next block of 16 */ |
| 525 | len = strtoul(args[4], NULL, 0); |
| 526 | if (!len) |
| 527 | len = ((start + 128) & -16) - start; |
| 528 | |
| 529 | chunk_reset(&trash); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 530 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 531 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 532 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Willy Tarreau | 48129be | 2021-05-04 18:40:50 +0200 | [diff] [blame] | 535 | /* parse a "debug dev sym <addr>" command. It always returns 1. */ |
| 536 | static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private) |
| 537 | { |
| 538 | unsigned long addr; |
| 539 | |
| 540 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 541 | return 1; |
| 542 | |
| 543 | if (!*args[3]) |
| 544 | return cli_err(appctx, "Missing memory address to be resolved.\n"); |
| 545 | |
| 546 | _HA_ATOMIC_INC(&debug_commands_issued); |
| 547 | |
| 548 | addr = strtoul(args[3], NULL, 0); |
| 549 | chunk_printf(&trash, "%#lx resolves to ", addr); |
| 550 | resolve_sym_name(&trash, NULL, (const void *)addr); |
| 551 | chunk_appendf(&trash, "\n"); |
| 552 | |
| 553 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 554 | } |
| 555 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 556 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 557 | static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private) |
| 558 | { |
| 559 | int thr = 0; |
| 560 | int sig = SIGABRT; |
| 561 | |
| 562 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 563 | return 1; |
| 564 | |
| 565 | if (*args[3]) |
| 566 | thr = atoi(args[3]); |
| 567 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 568 | if (thr < 0 || thr > global.nbthread) |
| 569 | 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] | 570 | |
| 571 | if (*args[4]) |
| 572 | sig = atoi(args[4]); |
| 573 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 574 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 575 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 576 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 577 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 578 | raise(sig); |
| 579 | return 1; |
| 580 | } |
| 581 | |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 582 | /* parse a "debug dev write" command. It always returns 1. */ |
| 583 | static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private) |
| 584 | { |
| 585 | unsigned long len; |
| 586 | |
| 587 | if (!*args[3]) |
| 588 | return cli_err(appctx, "Missing output size.\n"); |
| 589 | |
| 590 | len = strtoul(args[3], NULL, 0); |
| 591 | if (len >= trash.size) |
| 592 | return cli_err(appctx, "Output too large, must be <tune.bufsize.\n"); |
| 593 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 594 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 595 | |
| 596 | chunk_reset(&trash); |
| 597 | trash.data = len; |
| 598 | memset(trash.area, '.', trash.data); |
| 599 | trash.area[trash.data] = 0; |
| 600 | for (len = 64; len < trash.data; len += 64) |
| 601 | trash.area[len] = '\n'; |
| 602 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 603 | } |
| 604 | |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 605 | /* parse a "debug dev stream" command */ |
| 606 | /* |
| 607 | * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \ |
| 608 | * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \ |
| 609 | * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \ |
| 610 | * [sif.s[=<state>]] [sib.s[=<state>]] |
| 611 | */ |
| 612 | static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private) |
| 613 | { |
| 614 | struct stream *s = si_strm(appctx->owner); |
| 615 | int arg; |
| 616 | void *ptr; |
| 617 | int size; |
| 618 | const char *word, *end; |
| 619 | struct ist name; |
| 620 | char *msg = NULL; |
| 621 | char *endarg; |
| 622 | unsigned long long old, new; |
| 623 | |
| 624 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 625 | return 1; |
| 626 | |
| 627 | ptr = NULL; size = 0; |
| 628 | |
| 629 | if (!*args[3]) { |
| 630 | return cli_err(appctx, |
| 631 | "Usage: debug dev stream { <obj> <op> <value> | wake }*\n" |
| 632 | " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n" |
| 633 | " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n" |
| 634 | " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n" |
| 635 | " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n" |
| 636 | " 'wake' wakes the stream asssigned to 'strm' (default: current)\n" |
| 637 | ); |
| 638 | } |
| 639 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 640 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 641 | for (arg = 3; *args[arg]; arg++) { |
| 642 | old = 0; |
| 643 | end = word = args[arg]; |
| 644 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 645 | end++; |
| 646 | name = ist2(word, end - word); |
| 647 | if (isteq(name, ist("strm"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 648 | ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 649 | } else if (isteq(name, ist("strm.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 650 | ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 651 | } else if (isteq(name, ist("txn.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 652 | ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 653 | } else if (isteq(name, ist("req.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 654 | ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 655 | } else if (isteq(name, ist("res.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 656 | ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 657 | } else if (isteq(name, ist("req.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 658 | ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 659 | } else if (isteq(name, ist("res.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 660 | ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 661 | } else if (isteq(name, ist("req.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 662 | ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 663 | } else if (isteq(name, ist("res.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 664 | ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 665 | } else if (isteq(name, ist("sif.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 666 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 667 | } else if (isteq(name, ist("sib.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 668 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 669 | } else if (isteq(name, ist("sif.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 670 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 671 | } else if (isteq(name, ist("sib.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 672 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 673 | } else if (isteq(name, ist("sif.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 674 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 675 | } else if (isteq(name, ist("sib.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 676 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 677 | } else if (isteq(name, ist("wake"))) { |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 678 | if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1)) |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 679 | task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG); |
| 680 | continue; |
| 681 | } else |
| 682 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word)); |
| 683 | |
| 684 | /* read previous value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 685 | 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] | 686 | if (size == 8) |
| 687 | old = read_u64(ptr); |
| 688 | else if (size == 4) |
| 689 | old = read_u32(ptr); |
| 690 | else if (size == 2) |
| 691 | old = read_u16(ptr); |
| 692 | else |
| 693 | old = *(const uint8_t *)ptr; |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 694 | } else { |
| 695 | memprintf(&msg, |
| 696 | "%sSkipping inaccessible pointer %p for field '%.*s'.\n", |
| 697 | msg ? msg : "", ptr, (int)(end - word), word); |
| 698 | continue; |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /* parse the new value . */ |
| 702 | new = strtoll(end + 1, &endarg, 0); |
| 703 | if (end[1] && *endarg) { |
| 704 | if (strcmp(end + 1, "now") == 0) |
| 705 | new = now_ms; |
| 706 | else { |
| 707 | memprintf(&msg, |
| 708 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 709 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 710 | continue; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | switch (*end) { |
| 715 | case '\0': /* show */ |
| 716 | memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old); |
| 717 | new = old; // do not change the value |
| 718 | break; |
| 719 | |
| 720 | case '=': /* set */ |
| 721 | break; |
| 722 | |
| 723 | case '^': /* XOR */ |
| 724 | new = old ^ new; |
| 725 | break; |
| 726 | |
| 727 | case '+': /* OR */ |
| 728 | new = old | new; |
| 729 | break; |
| 730 | |
| 731 | case '-': /* AND NOT */ |
| 732 | new = old & ~new; |
| 733 | break; |
| 734 | |
| 735 | default: |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | /* write the new value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 740 | if (new != old) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 741 | if (size == 8) |
| 742 | write_u64(ptr, new); |
| 743 | else if (size == 4) |
| 744 | write_u32(ptr, new); |
| 745 | else if (size == 2) |
| 746 | write_u16(ptr, new); |
| 747 | else |
| 748 | *(uint8_t *)ptr = new; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (msg && *msg) |
| 753 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 754 | return 1; |
| 755 | } |
| 756 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 757 | static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state) |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 758 | { |
| 759 | unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 760 | unsigned long inter = tctx[1]; |
| 761 | unsigned long rnd; |
| 762 | |
| 763 | t->expire = tick_add(now_ms, inter); |
| 764 | |
| 765 | /* half of the calls will wake up another entry */ |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 766 | rnd = statistical_prng(); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 767 | if (rnd & 1) { |
| 768 | rnd >>= 1; |
| 769 | rnd %= tctx[0]; |
| 770 | rnd = tctx[rnd + 2]; |
| 771 | |
| 772 | if (rnd & 1) |
| 773 | task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG); |
| 774 | else |
| 775 | tasklet_wakeup((struct tasklet *)rnd); |
| 776 | } |
| 777 | return t; |
| 778 | } |
| 779 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 780 | static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state) |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 781 | { |
| 782 | unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 783 | unsigned long rnd; |
| 784 | int i; |
| 785 | |
| 786 | /* wake up two random entries */ |
| 787 | for (i = 0; i < 2; i++) { |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 788 | rnd = statistical_prng() % tctx[0]; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 789 | rnd = tctx[rnd + 2]; |
| 790 | |
| 791 | if (rnd & 1) |
| 792 | task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG); |
| 793 | else |
| 794 | tasklet_wakeup((struct tasklet *)rnd); |
| 795 | } |
| 796 | return t; |
| 797 | } |
| 798 | |
| 799 | /* parse a "debug dev sched" command |
| 800 | * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>] |
| 801 | */ |
| 802 | static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private) |
| 803 | { |
| 804 | int arg; |
| 805 | void *ptr; |
| 806 | int size; |
| 807 | const char *word, *end; |
| 808 | struct ist name; |
| 809 | char *msg = NULL; |
| 810 | char *endarg; |
| 811 | unsigned long long new; |
| 812 | unsigned long count = 0; |
| 813 | unsigned long thrid = 0; |
| 814 | unsigned int inter = 0; |
| 815 | unsigned long mask, tmask; |
| 816 | unsigned long i; |
| 817 | int mode = 0; // 0 = tasklet; 1 = task |
| 818 | int single = 0; |
| 819 | unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 820 | |
| 821 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 822 | return 1; |
| 823 | |
| 824 | ptr = NULL; size = 0; |
| 825 | mask = all_threads_mask; |
| 826 | |
| 827 | if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) { |
| 828 | return cli_err(appctx, |
| 829 | "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n" |
| 830 | " <obj> = {count | mask | inter | single }\n" |
| 831 | " <value> = 64-bit dec/hex integer (0x prefix supported)\n" |
| 832 | ); |
| 833 | } |
| 834 | |
| 835 | mode = strcmp(args[3], "task") == 0; |
| 836 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 837 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 838 | for (arg = 4; *args[arg]; arg++) { |
| 839 | end = word = args[arg]; |
| 840 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 841 | end++; |
| 842 | name = ist2(word, end - word); |
| 843 | if (isteq(name, ist("count"))) { |
| 844 | ptr = &count; size = sizeof(count); |
| 845 | } else if (isteq(name, ist("mask"))) { |
| 846 | ptr = &mask; size = sizeof(mask); |
| 847 | } else if (isteq(name, ist("tid"))) { |
| 848 | ptr = &thrid; size = sizeof(thrid); |
| 849 | } else if (isteq(name, ist("inter"))) { |
| 850 | ptr = &inter; size = sizeof(inter); |
| 851 | } else if (isteq(name, ist("single"))) { |
| 852 | ptr = &single; size = sizeof(single); |
| 853 | } else |
| 854 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word)); |
| 855 | |
| 856 | /* parse the new value . */ |
| 857 | new = strtoll(end + 1, &endarg, 0); |
| 858 | if (end[1] && *endarg) { |
| 859 | memprintf(&msg, |
| 860 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 861 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 862 | continue; |
| 863 | } |
| 864 | |
| 865 | /* write the new value */ |
| 866 | if (size == 8) |
| 867 | write_u64(ptr, new); |
| 868 | else if (size == 4) |
| 869 | write_u32(ptr, new); |
| 870 | else if (size == 2) |
| 871 | write_u16(ptr, new); |
| 872 | else |
| 873 | *(uint8_t *)ptr = new; |
| 874 | } |
| 875 | |
| 876 | tctx = calloc(sizeof(*tctx), count + 2); |
| 877 | if (!tctx) |
| 878 | goto fail; |
| 879 | |
| 880 | tctx[0] = (unsigned long)count; |
| 881 | tctx[1] = (unsigned long)inter; |
| 882 | |
| 883 | mask &= all_threads_mask; |
| 884 | if (!mask) |
| 885 | mask = tid_bit; |
| 886 | |
| 887 | tmask = 0; |
| 888 | for (i = 0; i < count; i++) { |
| 889 | if (single || mode == 0) { |
| 890 | /* look for next bit matching a bit in mask or loop back to zero */ |
| 891 | for (tmask <<= 1; !(mask & tmask); ) { |
| 892 | if (!(mask & -tmask)) |
| 893 | tmask = 1; |
| 894 | else |
| 895 | tmask <<= 1; |
| 896 | } |
| 897 | } else { |
| 898 | /* multi-threaded task */ |
| 899 | tmask = mask; |
| 900 | } |
| 901 | |
| 902 | /* now, if poly or mask was set, tmask corresponds to the |
| 903 | * valid thread mask to use, otherwise it remains zero. |
| 904 | */ |
| 905 | //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask); |
| 906 | if (mode == 0) { |
| 907 | struct tasklet *tl = tasklet_new(); |
| 908 | |
| 909 | if (!tl) |
| 910 | goto fail; |
| 911 | |
| 912 | if (tmask) |
| 913 | tl->tid = my_ffsl(tmask) - 1; |
| 914 | tl->process = debug_tasklet_handler; |
| 915 | tl->context = tctx; |
| 916 | tctx[i + 2] = (unsigned long)tl; |
| 917 | } else { |
| 918 | struct task *task = task_new(tmask ? tmask : tid_bit); |
| 919 | |
| 920 | if (!task) |
| 921 | goto fail; |
| 922 | |
| 923 | task->process = debug_task_handler; |
| 924 | task->context = tctx; |
| 925 | tctx[i + 2] = (unsigned long)task + 1; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /* start the tasks and tasklets */ |
| 930 | for (i = 0; i < count; i++) { |
| 931 | unsigned long ctx = tctx[i + 2]; |
| 932 | |
| 933 | if (ctx & 1) |
| 934 | task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT); |
| 935 | else |
| 936 | tasklet_wakeup((struct tasklet *)ctx); |
| 937 | } |
| 938 | |
| 939 | if (msg && *msg) |
| 940 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 941 | return 1; |
| 942 | |
| 943 | fail: |
| 944 | /* free partially allocated entries */ |
| 945 | for (i = 0; tctx && i < count; i++) { |
| 946 | unsigned long ctx = tctx[i + 2]; |
| 947 | |
| 948 | if (!ctx) |
| 949 | break; |
| 950 | |
| 951 | if (ctx & 1) |
| 952 | task_destroy((struct task *)(ctx - 1)); |
| 953 | else |
| 954 | tasklet_free((struct tasklet *)ctx); |
| 955 | } |
| 956 | |
| 957 | free(tctx); |
| 958 | return cli_err(appctx, "Not enough memory"); |
| 959 | } |
| 960 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 961 | #if defined(DEBUG_MEM_STATS) |
| 962 | /* CLI parser for the "debug dev memstats" command */ |
| 963 | static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private) |
| 964 | { |
| 965 | extern __attribute__((__weak__)) struct mem_stats __start_mem_stats; |
| 966 | extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats; |
| 967 | |
| 968 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 969 | return 1; |
| 970 | |
| 971 | if (strcmp(args[3], "reset") == 0) { |
| 972 | struct mem_stats *ptr; |
| 973 | |
| 974 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 975 | return 1; |
| 976 | |
| 977 | for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) { |
| 978 | _HA_ATOMIC_STORE(&ptr->calls, 0); |
| 979 | _HA_ATOMIC_STORE(&ptr->size, 0); |
| 980 | } |
| 981 | return 1; |
| 982 | } |
| 983 | |
| 984 | if (strcmp(args[3], "all") == 0) |
| 985 | appctx->ctx.cli.i0 = 1; |
| 986 | |
| 987 | /* otherwise proceed with the dump from p0 to p1 */ |
| 988 | appctx->ctx.cli.p0 = &__start_mem_stats; |
| 989 | appctx->ctx.cli.p1 = &__stop_mem_stats; |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | /* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats |
| 994 | * structs referenced by pointers located between p0 and p1. Dumps all entries |
| 995 | * if i0 > 0, otherwise only non-zero calls. |
| 996 | */ |
| 997 | static int debug_iohandler_memstats(struct appctx *appctx) |
| 998 | { |
| 999 | struct stream_interface *si = appctx->owner; |
| 1000 | struct mem_stats *ptr = appctx->ctx.cli.p0; |
| 1001 | int ret = 1; |
| 1002 | |
| 1003 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 1004 | goto end; |
| 1005 | |
| 1006 | chunk_reset(&trash); |
| 1007 | |
| 1008 | /* we have two inner loops here, one for the proxy, the other one for |
| 1009 | * the buffer. |
| 1010 | */ |
| 1011 | for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) { |
| 1012 | const char *type; |
| 1013 | const char *name; |
| 1014 | const char *p; |
| 1015 | |
| 1016 | if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0) |
| 1017 | continue; |
| 1018 | |
| 1019 | /* basename only */ |
| 1020 | for (p = name = ptr->file; *p; p++) { |
| 1021 | if (*p == '/') |
| 1022 | name = p + 1; |
| 1023 | } |
| 1024 | |
| 1025 | switch (ptr->type) { |
| 1026 | case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break; |
| 1027 | case MEM_STATS_TYPE_FREE: type = "FREE"; break; |
| 1028 | case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break; |
| 1029 | case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break; |
| 1030 | case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break; |
| 1031 | default: type = "UNSET"; break; |
| 1032 | } |
| 1033 | |
| 1034 | //chunk_printf(&trash, |
| 1035 | // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 1036 | // name, ptr->line, type, |
| 1037 | // (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 1038 | // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 1039 | |
| 1040 | chunk_printf(&trash, "%s:%d", name, ptr->line); |
| 1041 | while (trash.data < 25) |
| 1042 | trash.area[trash.data++] = ' '; |
| 1043 | chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 1044 | type, |
| 1045 | (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 1046 | (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 1047 | |
| 1048 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 1049 | si_rx_room_blk(si); |
| 1050 | appctx->ctx.cli.p0 = ptr; |
| 1051 | ret = 0; |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | end: |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | #endif |
| 1061 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1062 | #ifndef USE_THREAD_DUMP |
| 1063 | |
| 1064 | /* This function dumps all threads' state to the trash. This version is the |
| 1065 | * most basic one, which doesn't inspect other threads. |
| 1066 | */ |
| 1067 | void ha_thread_dump_all_to_trash() |
| 1068 | { |
| 1069 | unsigned int thr; |
| 1070 | |
| 1071 | for (thr = 0; thr < global.nbthread; thr++) |
| 1072 | ha_thread_dump(&trash, thr, tid); |
| 1073 | } |
| 1074 | |
| 1075 | #else /* below USE_THREAD_DUMP is set */ |
| 1076 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1077 | /* ID of the thread requesting the dump */ |
| 1078 | static unsigned int thread_dump_tid; |
| 1079 | |
| 1080 | /* points to the buffer where the dump functions should write. It must |
| 1081 | * have already been initialized by the requester. Nothing is done if |
| 1082 | * it's NULL. |
| 1083 | */ |
| 1084 | struct buffer *thread_dump_buffer = NULL; |
| 1085 | |
| 1086 | void ha_thread_dump_all_to_trash() |
| 1087 | { |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1088 | unsigned long old; |
| 1089 | |
| 1090 | while (1) { |
| 1091 | old = 0; |
| 1092 | if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask)) |
| 1093 | break; |
| 1094 | ha_thread_relax(); |
| 1095 | } |
| 1096 | |
| 1097 | thread_dump_buffer = &trash; |
| 1098 | thread_dump_tid = tid; |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 1099 | ha_tkillall(DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* handles DEBUGSIG to dump the state of the thread it's working on */ |
| 1103 | void debug_handler(int sig, siginfo_t *si, void *arg) |
| 1104 | { |
Willy Tarreau | 82aafc4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 1105 | /* first, let's check it's really for us and that we didn't just get |
| 1106 | * a spurious DEBUGSIG. |
| 1107 | */ |
| 1108 | if (!(threads_to_dump & tid_bit)) |
| 1109 | return; |
| 1110 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1111 | /* There are 4 phases in the dump process: |
| 1112 | * 1- wait for our turn, i.e. when all lower bits are gone. |
| 1113 | * 2- perform the action if our bit is set |
| 1114 | * 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] | 1115 | * the last one and have to put them all as a signal |
| 1116 | * 4- wait out bit to re-appear, then clear it and quit. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1117 | */ |
| 1118 | |
| 1119 | /* wait for all previous threads to finish first */ |
| 1120 | while (threads_to_dump & (tid_bit - 1)) |
| 1121 | ha_thread_relax(); |
| 1122 | |
| 1123 | /* dump if needed */ |
| 1124 | if (threads_to_dump & tid_bit) { |
| 1125 | if (thread_dump_buffer) |
| 1126 | ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid); |
| 1127 | if ((threads_to_dump & all_threads_mask) == tid_bit) { |
| 1128 | /* last one */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 1129 | HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1130 | thread_dump_buffer = NULL; |
| 1131 | } |
| 1132 | else |
| 1133 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
| 1134 | } |
| 1135 | |
| 1136 | /* 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] | 1137 | * bits again to broadcast the leaving condition so we'll see ourselves |
| 1138 | * present again. This way the threads_to_dump variable never passes to |
| 1139 | * zero until all visitors have stopped waiting. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1140 | */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 1141 | while (!(threads_to_dump & tid_bit)) |
| 1142 | ha_thread_relax(); |
| 1143 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 1144 | |
| 1145 | /* mark the current thread as stuck to detect it upon next invocation |
| 1146 | * if it didn't move. |
| 1147 | */ |
| 1148 | if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit)) |
Willy Tarreau | a0b9953 | 2021-09-30 18:48:37 +0200 | [diff] [blame] | 1149 | th_ctx->flags |= TH_FL_STUCK; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static int init_debug_per_thread() |
| 1153 | { |
| 1154 | sigset_t set; |
| 1155 | |
| 1156 | /* unblock the DEBUGSIG signal we intend to use */ |
| 1157 | sigemptyset(&set); |
| 1158 | sigaddset(&set, DEBUGSIG); |
| 1159 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 1160 | return 1; |
| 1161 | } |
| 1162 | |
| 1163 | static int init_debug() |
| 1164 | { |
| 1165 | struct sigaction sa; |
Willy Tarreau | 2f1227e | 2021-01-22 12:12:29 +0100 | [diff] [blame] | 1166 | void *callers[1]; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1167 | |
Willy Tarreau | 0214b45 | 2020-03-04 06:01:40 +0100 | [diff] [blame] | 1168 | /* calling backtrace() will access libgcc at runtime. We don't want to |
| 1169 | * do it after the chroot, so let's perform a first call to have it |
| 1170 | * ready in memory for later use. |
| 1171 | */ |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 1172 | my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1173 | sa.sa_handler = NULL; |
| 1174 | sa.sa_sigaction = debug_handler; |
| 1175 | sigemptyset(&sa.sa_mask); |
| 1176 | sa.sa_flags = SA_SIGINFO; |
| 1177 | sigaction(DEBUGSIG, &sa, NULL); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1178 | return ERR_NONE; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | REGISTER_POST_CHECK(init_debug); |
| 1182 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 1183 | |
| 1184 | #endif /* USE_THREAD_DUMP */ |
| 1185 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1186 | /* register cli keywords */ |
| 1187 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1188 | {{ "debug", "dev", "bug", NULL }, "debug dev bug : call BUG_ON() and crash", debug_parse_cli_bug, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1189 | {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1190 | {{ "debug", "dev", "delay", NULL }, "debug dev delay [ms] : sleep this long", debug_parse_cli_delay, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 1191 | #if defined(DEBUG_DEV) |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1192 | {{ "debug", "dev", "exec", NULL }, "debug dev exec [cmd] ... : show this command's output", debug_parse_cli_exec, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 1193 | #endif |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1194 | {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1195 | {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1196 | {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1197 | {{ "debug", "dev", "loop", NULL }, "debug dev loop [ms] : loop this long", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1198 | #if defined(DEBUG_MEM_STATS) |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1199 | {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1200 | #endif |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1201 | {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1202 | {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1203 | {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1204 | {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1205 | {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1206 | {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1207 | {{ "show", "threads", NULL, NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL }, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1208 | {{},} |
| 1209 | }}; |
| 1210 | |
| 1211 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |