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