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