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