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