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 | f0c86dd | 2022-07-15 13:14:03 +0200 | [diff] [blame] | 50 | /* The dump state is made of: |
| 51 | * - num_thread on the lowest 15 bits |
| 52 | * - a SYNC flag on bit 15 (waiting for sync start) |
| 53 | * - number of participating threads on bits 16-30 |
| 54 | * Initiating a dump consists in setting it to SYNC and incrementing the |
| 55 | * num_thread part when entering the function. The first thread periodically |
| 56 | * recounts active threads and compares it to the ready ones, and clears SYNC |
| 57 | * and sets the number of participants to the value found, which serves as a |
| 58 | * start signal. A thread finished dumping looks up the TID of the next active |
| 59 | * thread after it and writes it in the lowest part. If there's none, it sets |
| 60 | * the thread counter to the number of participants and resets that part, |
| 61 | * which serves as an end-of-dump signal. All threads decrement the num_thread |
| 62 | * part. Then all threads wait for the value to reach zero. Only used when |
| 63 | * USE_THREAD_DUMP is set. |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 64 | */ |
Willy Tarreau | f0c86dd | 2022-07-15 13:14:03 +0200 | [diff] [blame] | 65 | #define THREAD_DUMP_TMASK 0x00007FFFU |
| 66 | #define THREAD_DUMP_FSYNC 0x00008000U |
| 67 | #define THREAD_DUMP_PMASK 0x7FFF0000U |
| 68 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 69 | /* Points to a copy of the buffer where the dump functions should write, when |
| 70 | * non-null. It's only used by debuggers for core dump analysis. |
| 71 | */ |
| 72 | struct buffer *thread_dump_buffer = NULL; |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 73 | unsigned int debug_commands_issued = 0; |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 75 | /* dumps a backtrace of the current thread that is appended to buffer <buf>. |
| 76 | * Lines are prefixed with the string <prefix> which may be empty (used for |
| 77 | * 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] | 78 | * the function does not appear in the call stack. The <dump> argument |
| 79 | * indicates what dump state to start from, and should usually be zero. It |
| 80 | * may be among the following values: |
| 81 | * - 0: search usual callers before step 1, or directly jump to 2 |
| 82 | * - 1: skip usual callers before step 2 |
| 83 | * - 2: dump until polling loop, scheduler, or main() (excluded) |
| 84 | * - 3: end |
| 85 | * - 4-7: like 0 but stops *after* main. |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 86 | */ |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 87 | void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump) |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 88 | { |
| 89 | struct buffer bak; |
| 90 | char pfx2[100]; |
| 91 | void *callers[100]; |
| 92 | int j, nptrs; |
| 93 | const void *addr; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 94 | |
| 95 | nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
| 96 | if (!nptrs) |
| 97 | return; |
| 98 | |
| 99 | if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2)) |
| 100 | pfx2[0] = 0; |
| 101 | |
| 102 | /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would |
| 103 | * produce similar output to the following: |
| 104 | */ |
| 105 | chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs); |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 106 | for (j = 0; (j < nptrs || (dump & 3) < 2); j++) { |
| 107 | if (j == nptrs && !(dump & 3)) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 108 | /* we failed to spot the starting point of the |
| 109 | * dump, let's start over dumping everything we |
| 110 | * have. |
| 111 | */ |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 112 | dump += 2; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 113 | j = 0; |
| 114 | } |
| 115 | bak = *buf; |
| 116 | dump_addr_and_bytes(buf, pfx2, callers[j], 8); |
| 117 | addr = resolve_sym_name(buf, ": ", callers[j]); |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 118 | if ((dump & 3) == 0) { |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 119 | /* dump not started, will start *after* ha_thread_dump_one(), |
Willy Tarreau | e69919d | 2023-05-04 19:19:04 +0200 | [diff] [blame] | 120 | * ha_panic and ha_backtrace_to_stderr |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 121 | */ |
Willy Tarreau | e69919d | 2023-05-04 19:19:04 +0200 | [diff] [blame] | 122 | if (addr == ha_panic || |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 123 | addr == ha_backtrace_to_stderr || addr == ha_thread_dump_one) |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 124 | dump++; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 125 | *buf = bak; |
| 126 | continue; |
| 127 | } |
| 128 | |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 129 | if ((dump & 3) == 1) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 130 | /* starting */ |
Willy Tarreau | e69919d | 2023-05-04 19:19:04 +0200 | [diff] [blame] | 131 | if (addr == ha_panic || |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 132 | addr == ha_backtrace_to_stderr || addr == ha_thread_dump_one) { |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 133 | *buf = bak; |
| 134 | continue; |
| 135 | } |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 136 | dump++; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 139 | if ((dump & 3) == 2) { |
| 140 | /* still dumping */ |
| 141 | if (dump == 6) { |
| 142 | /* we only stop *after* main and we must send the LF */ |
| 143 | if (addr == main) { |
| 144 | j = nptrs; |
| 145 | dump++; |
| 146 | } |
| 147 | } |
| 148 | else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) { |
| 149 | dump++; |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 150 | *buf = bak; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | /* OK, line dumped */ |
| 155 | chunk_appendf(buf, "\n"); |
| 156 | } |
| 157 | } |
| 158 | |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 159 | /* dump a backtrace of current thread's stack to stderr. */ |
Willy Tarreau | 7831e02 | 2022-05-06 15:16:19 +0200 | [diff] [blame] | 160 | void ha_backtrace_to_stderr(void) |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 161 | { |
| 162 | char area[2048]; |
| 163 | struct buffer b = b_make(area, sizeof(area), 0, 0); |
| 164 | |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 165 | ha_dump_backtrace(&b, " ", 4); |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 166 | if (b.data) |
Willy Tarreau | 2cbe2e7 | 2021-01-22 15:58:26 +0100 | [diff] [blame] | 167 | DISGUISE(write(2, b.area, b.data)); |
Willy Tarreau | a8459b2 | 2021-01-22 14:12:27 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 170 | /* Dumps to the thread's buffer some known information for the desired thread, |
| 171 | * and optionally extra info when it's safe to do so (current thread or |
| 172 | * isolated). The dump will be appended to the buffer, so the caller is |
| 173 | * responsible for preliminary initializing it. The <from_signal> argument will |
| 174 | * indicate if the function is called from the debug signal handler, indicating |
| 175 | * the thread was dumped upon request from another one, otherwise if the thread |
| 176 | * it the current one, a star ('*') will be displayed in front of the thread to |
| 177 | * indicate the requesting one. Any stuck thread is also prefixed with a '>'. |
| 178 | * The caller is responsible for atomically setting up the thread's dump buffer |
| 179 | * to point to a valid buffer with enough room. Output will be truncated if it |
| 180 | * does not fit. When the dump is complete, the dump buffer will be switched to |
| 181 | * (void*)0x1 that the caller must turn to 0x0 once the contents are collected. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 182 | */ |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 183 | void ha_thread_dump_one(int thr, int from_signal) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 184 | { |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 185 | struct buffer *buf = HA_ATOMIC_LOAD(&ha_thread_ctx[thr].thread_dump_buffer); |
Willy Tarreau | 95e6c99 | 2023-05-07 15:02:30 +0200 | [diff] [blame] | 186 | unsigned long __maybe_unused thr_bit = ha_thread_info[thr].ltid_bit; |
| 187 | int __maybe_unused tgrp = ha_thread_info[thr].tgid; |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 188 | unsigned long long p = ha_thread_ctx[thr].prev_cpu_time; |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 189 | unsigned long long n = now_cpu_time_thread(thr); |
Willy Tarreau | a0b9953 | 2021-09-30 18:48:37 +0200 | [diff] [blame] | 190 | int stuck = !!(ha_thread_ctx[thr].flags & TH_FL_STUCK); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 192 | chunk_appendf(buf, |
Willy Tarreau | f0e5da2 | 2020-05-01 12:26:03 +0200 | [diff] [blame] | 193 | "%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] | 194 | " %2u/%-2u stuck=%d prof=%d", |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 195 | (thr == tid && !from_signal) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Willy Tarreau | ff64d3b | 2020-05-01 11:28:49 +0200 | [diff] [blame] | 196 | ha_get_pthread_id(thr), |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 197 | thread_has_tasks(), |
Willy Tarreau | c958c70 | 2022-06-16 15:59:36 +0200 | [diff] [blame] | 198 | !eb_is_empty(&ha_thread_ctx[thr].rqueue_shared), |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 199 | !eb_is_empty(&ha_thread_ctx[thr].timers), |
| 200 | !eb_is_empty(&ha_thread_ctx[thr].rqueue), |
| 201 | !(LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_URGENT]) && |
| 202 | LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_NORMAL]) && |
| 203 | LIST_ISEMPTY(&ha_thread_ctx[thr].tasklets[TL_BULK]) && |
| 204 | MT_LIST_ISEMPTY(&ha_thread_ctx[thr].shared_tasklet_list)), |
| 205 | ha_thread_ctx[thr].tasks_in_list, |
| 206 | ha_thread_ctx[thr].rq_total, |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 207 | ha_thread_info[thr].tgid, ha_thread_info[thr].ltid + 1, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 208 | stuck, |
Willy Tarreau | b30ced3 | 2023-05-04 11:30:55 +0200 | [diff] [blame] | 209 | !!(ha_thread_ctx[thr].flags & TH_FL_TASK_PROFILING)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 95e6c99 | 2023-05-07 15:02:30 +0200 | [diff] [blame] | 211 | #if defined(USE_THREAD) |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 212 | chunk_appendf(buf, |
Willy Tarreau | b30ced3 | 2023-05-04 11:30:55 +0200 | [diff] [blame] | 213 | " harmless=%d isolated=%d", |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 214 | !!(_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp-1].threads_harmless) & thr_bit), |
Willy Tarreau | b30ced3 | 2023-05-04 11:30:55 +0200 | [diff] [blame] | 215 | isolated_thread == thr); |
Willy Tarreau | 95e6c99 | 2023-05-07 15:02:30 +0200 | [diff] [blame] | 216 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 217 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 218 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 219 | 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] | 220 | |
Willy Tarreau | a3870b7 | 2021-09-13 19:24:47 +0200 | [diff] [blame] | 221 | /* 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] | 222 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 223 | if (thr != tid && !thread_isolated()) |
| 224 | goto leave; |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 225 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 226 | chunk_appendf(buf, " curr_task="); |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 227 | ha_task_dump(buf, th_ctx->current, " "); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 228 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 229 | if (stuck && thr == tid) { |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 230 | /* We only emit the backtrace for stuck threads in order not to |
| 231 | * waste precious output buffer space with non-interesting data. |
Willy Tarreau | 123fc97 | 2021-01-22 13:52:41 +0100 | [diff] [blame] | 232 | * Please leave this as the last instruction in this function |
| 233 | * so that the compiler uses tail merging and the current |
| 234 | * function does not appear in the stack. |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 235 | */ |
Willy Tarreau | 2bfce7e | 2021-01-22 14:48:34 +0100 | [diff] [blame] | 236 | ha_dump_backtrace(buf, " ", 0); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 237 | } |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 238 | leave: |
| 239 | /* end of dump, setting the buffer to 0x1 will tell the caller we're done */ |
| 240 | HA_ATOMIC_STORE(&ha_thread_ctx[thr].thread_dump_buffer, (void*)0x1UL); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 243 | /* Triggers a thread dump from thread <thr>, either directly if it's the |
| 244 | * current thread or if thread dump signals are not implemented, or by sending |
| 245 | * a signal if it's a remote one and the feature is supported. The buffer <buf> |
| 246 | * will get the dump appended, and the caller is responsible for making sure |
| 247 | * there is enough room otherwise some contents will be truncated. |
| 248 | */ |
| 249 | void ha_thread_dump(struct buffer *buf, int thr) |
| 250 | { |
| 251 | struct buffer *old = NULL; |
| 252 | |
| 253 | /* try to impose our dump buffer and to reserve the target thread's |
| 254 | * next dump for us. |
| 255 | */ |
| 256 | do { |
| 257 | if (old) |
| 258 | ha_thread_relax(); |
| 259 | old = NULL; |
| 260 | } while (!HA_ATOMIC_CAS(&ha_thread_ctx[thr].thread_dump_buffer, &old, buf)); |
| 261 | |
| 262 | #ifdef USE_THREAD_DUMP |
| 263 | /* asking the remote thread to dump itself allows to get more details |
| 264 | * including a backtrace. |
| 265 | */ |
| 266 | if (thr != tid) |
| 267 | ha_tkill(thr, DEBUGSIG); |
| 268 | else |
| 269 | #endif |
| 270 | ha_thread_dump_one(thr, thr != tid); |
| 271 | |
| 272 | /* now wait for the dump to be done, and release it */ |
| 273 | do { |
| 274 | if (old) |
| 275 | ha_thread_relax(); |
| 276 | old = (void*)0x01; |
| 277 | } while (!HA_ATOMIC_CAS(&ha_thread_ctx[thr].thread_dump_buffer, &old, 0)); |
| 278 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 279 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 280 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 281 | * 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] | 282 | * with <pfx>. The buffer is only appended and the first output starts by the |
| 283 | * pointer itself. The caller is responsible for making sure the task is not |
| 284 | * going to vanish during the dump. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 285 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 286 | 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] | 287 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 288 | const struct stream *s = NULL; |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 289 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 290 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 52f238d | 2022-07-15 12:08:40 +0200 | [diff] [blame] | 291 | const struct stconn *sc; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 292 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 293 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 294 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 298 | if (TASK_IS_TASKLET(task)) |
| 299 | chunk_appendf(buf, |
| 300 | "%p (tasklet) calls=%u\n", |
| 301 | task, |
| 302 | task->calls); |
| 303 | else |
| 304 | chunk_appendf(buf, |
| 305 | "%p (task) calls=%u last=%llu%s\n", |
| 306 | task, |
| 307 | task->calls, |
Willy Tarreau | 04e50b3 | 2022-09-07 14:49:50 +0200 | [diff] [blame] | 308 | task->wake_date ? (unsigned long long)(now_mono_time() - task->wake_date) : 0, |
| 309 | task->wake_date ? " ns ago" : ""); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 310 | |
Willy Tarreau | 2e89b09 | 2020-03-03 17:13:02 +0100 | [diff] [blame] | 311 | chunk_appendf(buf, "%s fct=%p(", pfx, task->process); |
| 312 | resolve_sym_name(buf, NULL, task->process); |
| 313 | chunk_appendf(buf,") ctx=%p", task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 314 | |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 315 | if (task->process == task_run_applet && (appctx = task->context)) |
| 316 | chunk_appendf(buf, "(%s)\n", appctx->applet->name); |
| 317 | else |
| 318 | chunk_appendf(buf, "\n"); |
| 319 | |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 320 | if (task->process == process_stream && task->context) |
| 321 | s = (struct stream *)task->context; |
Willy Tarreau | 52f238d | 2022-07-15 12:08:40 +0200 | [diff] [blame] | 322 | else if (task->process == task_run_applet && task->context && (sc = appctx_sc((struct appctx *)task->context))) |
| 323 | s = sc_strm(sc); |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 324 | else if (task->process == sc_conn_io_cb && task->context) |
Willy Tarreau | ea27f48 | 2022-05-18 16:10:52 +0200 | [diff] [blame] | 325 | s = sc_strm(((struct stconn *)task->context)); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 326 | |
| 327 | if (s) |
| 328 | stream_dump(buf, s, pfx, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 329 | |
| 330 | #ifdef USE_LUA |
| 331 | hlua = NULL; |
Aurelien DARRAGON | 1a3ac40 | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 332 | if (s && ((s->hlua[0] && HLUA_IS_BUSY(s->hlua[0])) || |
| 333 | (s->hlua[1] && HLUA_IS_BUSY(s->hlua[1])))) { |
| 334 | hlua = (s->hlua[0] && HLUA_IS_BUSY(s->hlua[0])) ? s->hlua[0] : s->hlua[1]; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 335 | chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); |
| 336 | } |
| 337 | else if (task->process == hlua_process_task && (hlua = task->context)) { |
| 338 | chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx); |
| 339 | } |
| 340 | else if (task->process == task_run_applet && (appctx = task->context) && |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 341 | (appctx->applet->fct == hlua_applet_tcp_fct)) { |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 342 | chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx); |
| 343 | } |
| 344 | else if (task->process == task_run_applet && (appctx = task->context) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 345 | (appctx->applet->fct == hlua_applet_http_fct)) { |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 346 | chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx); |
| 347 | } |
| 348 | |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 349 | if (hlua && hlua->T) { |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 350 | chunk_appendf(buf, "stack traceback:\n "); |
| 351 | append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 352 | } |
Willy Tarreau | cb01f5d | 2023-05-04 16:28:30 +0200 | [diff] [blame] | 353 | |
| 354 | /* we may need to terminate the current line */ |
| 355 | if (*b_peek(buf, b_data(buf)-1) != '\n') |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 356 | b_putchr(buf, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 357 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 360 | /* This function dumps all profiling settings. It returns 0 if the output |
| 361 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 362 | */ |
| 363 | static int cli_io_handler_show_threads(struct appctx *appctx) |
| 364 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 365 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | dee5920 | 2024-07-16 11:14:49 +0200 | [diff] [blame] | 366 | int *thr = appctx->svcctx; |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 367 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 368 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 369 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 370 | return 1; |
| 371 | |
Willy Tarreau | dee5920 | 2024-07-16 11:14:49 +0200 | [diff] [blame] | 372 | if (!thr) |
| 373 | thr = applet_reserve_svcctx(appctx, sizeof(*thr)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 374 | |
Willy Tarreau | 009b551 | 2023-05-04 19:07:56 +0200 | [diff] [blame] | 375 | do { |
| 376 | chunk_reset(&trash); |
Willy Tarreau | dee5920 | 2024-07-16 11:14:49 +0200 | [diff] [blame] | 377 | ha_thread_dump(&trash, *thr); |
Willy Tarreau | 009b551 | 2023-05-04 19:07:56 +0200 | [diff] [blame] | 378 | |
| 379 | if (applet_putchk(appctx, &trash) == -1) { |
| 380 | /* failed, try again */ |
Willy Tarreau | 009b551 | 2023-05-04 19:07:56 +0200 | [diff] [blame] | 381 | return 0; |
| 382 | } |
Willy Tarreau | dee5920 | 2024-07-16 11:14:49 +0200 | [diff] [blame] | 383 | (*thr)++; |
| 384 | } while (*thr < global.nbthread); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 385 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 386 | return 1; |
| 387 | } |
| 388 | |
Willy Tarreau | 6ab7b21 | 2021-12-28 09:57:10 +0100 | [diff] [blame] | 389 | #if defined(HA_HAVE_DUMP_LIBS) |
| 390 | /* parse a "show libs" command. It returns 1 if it emits anything otherwise zero. */ |
| 391 | static int debug_parse_cli_show_libs(char **args, char *payload, struct appctx *appctx, void *private) |
| 392 | { |
| 393 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 394 | return 1; |
| 395 | |
| 396 | chunk_reset(&trash); |
| 397 | if (dump_libs(&trash, 1)) |
| 398 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 399 | else |
| 400 | return 0; |
| 401 | } |
| 402 | #endif |
| 403 | |
Willy Tarreau | 880d168 | 2023-05-04 18:52:51 +0200 | [diff] [blame] | 404 | /* Dumps a state of all threads into the trash and on fd #2, then aborts. |
| 405 | * A copy will be put into a trash chunk that's assigned to thread_dump_buffer |
| 406 | * so that the debugger can easily find it. This buffer might be truncated if |
| 407 | * too many threads are being dumped, but at least we'll dump them all on stderr. |
| 408 | * If thread_dump_buffer is set, it means that a panic has already begun. |
| 409 | */ |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 410 | void ha_panic() |
| 411 | { |
Willy Tarreau | 880d168 | 2023-05-04 18:52:51 +0200 | [diff] [blame] | 412 | struct buffer *old; |
| 413 | unsigned int thr; |
| 414 | |
| 415 | old = NULL; |
| 416 | if (!HA_ATOMIC_CAS(&thread_dump_buffer, &old, get_trash_chunk())) { |
Willy Tarreau | 55433f9 | 2022-07-15 12:48:58 +0200 | [diff] [blame] | 417 | /* a panic dump is already in progress, let's not disturb it, |
| 418 | * we'll be called via signal DEBUGSIG. By returning we may be |
| 419 | * able to leave a current signal handler (e.g. WDT) so that |
| 420 | * this will ensure more reliable signal delivery. |
| 421 | */ |
| 422 | return; |
| 423 | } |
Willy Tarreau | 880d168 | 2023-05-04 18:52:51 +0200 | [diff] [blame] | 424 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 425 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 426 | chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1); |
Willy Tarreau | 880d168 | 2023-05-04 18:52:51 +0200 | [diff] [blame] | 427 | |
| 428 | for (thr = 0; thr < global.nbthread; thr++) { |
| 429 | ha_thread_dump(&trash, thr); |
| 430 | DISGUISE(write(2, trash.area, trash.data)); |
| 431 | b_force_xfer(thread_dump_buffer, &trash, b_room(thread_dump_buffer)); |
| 432 | chunk_reset(&trash); |
| 433 | } |
| 434 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 435 | for (;;) |
| 436 | abort(); |
| 437 | } |
| 438 | |
Willy Tarreau | 06e66c8 | 2022-03-02 15:52:03 +0100 | [diff] [blame] | 439 | /* Complain with message <msg> on stderr. If <counter> is not NULL, it is |
| 440 | * atomically incremented, and the message is only printed when the counter |
| 441 | * was zero, so that the message is only printed once. <taint> is only checked |
| 442 | * on bit 1, and will taint the process either for a bug (2) or warn (0). |
| 443 | */ |
| 444 | void complain(int *counter, const char *msg, int taint) |
| 445 | { |
| 446 | if (counter && _HA_ATOMIC_FETCH_ADD(counter, 1)) |
| 447 | return; |
| 448 | DISGUISE(write(2, msg, strlen(msg))); |
| 449 | if (taint & 2) |
| 450 | mark_tainted(TAINTED_BUG); |
| 451 | else |
| 452 | mark_tainted(TAINTED_WARN); |
| 453 | } |
| 454 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 455 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 456 | static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private) |
| 457 | { |
| 458 | int code = atoi(args[3]); |
| 459 | |
| 460 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 461 | return 1; |
| 462 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 463 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 464 | exit(code); |
| 465 | return 1; |
| 466 | } |
| 467 | |
Willy Tarreau | 5baf4fe | 2021-01-22 14:15:46 +0100 | [diff] [blame] | 468 | /* parse a "debug dev bug" command. It always returns 1, though it should never return. |
| 469 | * Note: we make sure not to make the function static so that it appears in the trace. |
| 470 | */ |
| 471 | int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private) |
| 472 | { |
| 473 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 474 | return 1; |
| 475 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 476 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 5baf4fe | 2021-01-22 14:15:46 +0100 | [diff] [blame] | 477 | BUG_ON(one > zero); |
| 478 | return 1; |
| 479 | } |
| 480 | |
Willy Tarreau | 305cfbd | 2022-02-25 08:52:39 +0100 | [diff] [blame] | 481 | /* parse a "debug dev warn" command. It always returns 1. |
| 482 | * Note: we make sure not to make the function static so that it appears in the trace. |
| 483 | */ |
| 484 | int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private) |
| 485 | { |
| 486 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 487 | return 1; |
| 488 | |
| 489 | _HA_ATOMIC_INC(&debug_commands_issued); |
| 490 | WARN_ON(one > zero); |
| 491 | return 1; |
| 492 | } |
| 493 | |
Willy Tarreau | 6d3f1e3 | 2022-02-28 11:51:23 +0100 | [diff] [blame] | 494 | /* parse a "debug dev check" command. It always returns 1. |
Willy Tarreau | 4e0a8b1 | 2022-02-25 08:55:11 +0100 | [diff] [blame] | 495 | * Note: we make sure not to make the function static so that it appears in the trace. |
| 496 | */ |
Willy Tarreau | 6d3f1e3 | 2022-02-28 11:51:23 +0100 | [diff] [blame] | 497 | 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] | 498 | { |
| 499 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 500 | return 1; |
| 501 | |
| 502 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6d3f1e3 | 2022-02-28 11:51:23 +0100 | [diff] [blame] | 503 | CHECK_IF(one > zero); |
Willy Tarreau | 4e0a8b1 | 2022-02-25 08:55:11 +0100 | [diff] [blame] | 504 | return 1; |
| 505 | } |
| 506 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 507 | /* parse a "debug dev close" command. It always returns 1. */ |
| 508 | static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private) |
| 509 | { |
| 510 | int fd; |
| 511 | |
| 512 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 513 | return 1; |
| 514 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 515 | if (!*args[3]) |
| 516 | return cli_err(appctx, "Missing file descriptor number.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 517 | |
| 518 | fd = atoi(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 519 | if (fd < 0 || fd >= global.maxsock) |
| 520 | return cli_err(appctx, "File descriptor out of range.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 521 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 522 | if (!fdtab[fd].owner) |
| 523 | return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 524 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 525 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 526 | fd_delete(fd); |
| 527 | return 1; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Willy Tarreau | dadf00e | 2022-07-15 08:25:03 +0200 | [diff] [blame] | 530 | /* this is meant to cause a deadlock when more than one task is running it or when run twice */ |
| 531 | static struct task *debug_run_cli_deadlock(struct task *task, void *ctx, unsigned int state) |
| 532 | { |
| 533 | static HA_SPINLOCK_T lock __maybe_unused; |
| 534 | |
| 535 | HA_SPIN_LOCK(OTHER_LOCK, &lock); |
| 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | /* parse a "debug dev deadlock" command. It always returns 1. */ |
| 540 | static int debug_parse_cli_deadlock(char **args, char *payload, struct appctx *appctx, void *private) |
| 541 | { |
| 542 | int tasks; |
| 543 | |
| 544 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 545 | return 1; |
| 546 | |
| 547 | _HA_ATOMIC_INC(&debug_commands_issued); |
| 548 | for (tasks = atoi(args[3]); tasks > 0; tasks--) { |
| 549 | struct task *t = task_new_on(tasks % global.nbthread); |
| 550 | if (!t) |
| 551 | continue; |
| 552 | t->process = debug_run_cli_deadlock; |
| 553 | t->context = NULL; |
| 554 | task_wakeup(t, TASK_WOKEN_INIT); |
| 555 | } |
| 556 | |
| 557 | return 1; |
| 558 | } |
| 559 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 560 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 561 | static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private) |
| 562 | { |
| 563 | int delay = atoi(args[3]); |
| 564 | |
| 565 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 566 | return 1; |
| 567 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 568 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 569 | usleep((long)delay * 1000); |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | /* parse a "debug dev log" command. It always returns 1. */ |
| 574 | static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private) |
| 575 | { |
| 576 | int arg; |
| 577 | |
| 578 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 579 | return 1; |
| 580 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 581 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 582 | chunk_reset(&trash); |
| 583 | for (arg = 3; *args[arg]; arg++) { |
| 584 | if (arg > 3) |
| 585 | chunk_strcat(&trash, " "); |
| 586 | chunk_strcat(&trash, args[arg]); |
| 587 | } |
| 588 | |
| 589 | send_log(NULL, LOG_INFO, "%s\n", trash.area); |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | /* parse a "debug dev loop" command. It always returns 1. */ |
| 594 | static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private) |
| 595 | { |
| 596 | struct timeval deadline, curr; |
| 597 | int loop = atoi(args[3]); |
Willy Tarreau | e5e6223 | 2023-05-04 11:50:26 +0200 | [diff] [blame] | 598 | int isolate; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 599 | |
| 600 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 601 | return 1; |
| 602 | |
Willy Tarreau | e5e6223 | 2023-05-04 11:50:26 +0200 | [diff] [blame] | 603 | isolate = strcmp(args[4], "isolated") == 0; |
| 604 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 605 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 606 | gettimeofday(&curr, NULL); |
| 607 | tv_ms_add(&deadline, &curr, loop); |
| 608 | |
Willy Tarreau | e5e6223 | 2023-05-04 11:50:26 +0200 | [diff] [blame] | 609 | if (isolate) |
| 610 | thread_isolate(); |
| 611 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 612 | while (tv_ms_cmp(&curr, &deadline) < 0) |
| 613 | gettimeofday(&curr, NULL); |
| 614 | |
Willy Tarreau | e5e6223 | 2023-05-04 11:50:26 +0200 | [diff] [blame] | 615 | if (isolate) |
| 616 | thread_release(); |
| 617 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | /* parse a "debug dev panic" command. It always returns 1, though it should never return. */ |
| 622 | static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private) |
| 623 | { |
| 624 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 625 | return 1; |
| 626 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 627 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 628 | ha_panic(); |
| 629 | return 1; |
| 630 | } |
| 631 | |
| 632 | /* parse a "debug dev exec" command. It always returns 1. */ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 633 | #if defined(DEBUG_DEV) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 634 | static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private) |
| 635 | { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 636 | int pipefd[2]; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 637 | int arg; |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 638 | int pid; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 639 | |
| 640 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 641 | return 1; |
| 642 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 643 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 644 | chunk_reset(&trash); |
| 645 | for (arg = 3; *args[arg]; arg++) { |
| 646 | if (arg > 3) |
| 647 | chunk_strcat(&trash, " "); |
| 648 | chunk_strcat(&trash, args[arg]); |
| 649 | } |
| 650 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 651 | thread_isolate(); |
| 652 | if (pipe(pipefd) < 0) |
| 653 | goto fail_pipe; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 654 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 655 | if (fd_set_cloexec(pipefd[0]) == -1) |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 656 | goto fail_fcntl; |
| 657 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 658 | if (fd_set_cloexec(pipefd[1]) == -1) |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 659 | goto fail_fcntl; |
| 660 | |
| 661 | pid = fork(); |
| 662 | |
| 663 | if (pid < 0) |
| 664 | goto fail_fork; |
| 665 | else if (pid == 0) { |
| 666 | /* child */ |
| 667 | char *cmd[4] = { "/bin/sh", "-c", 0, 0 }; |
| 668 | |
| 669 | close(0); |
| 670 | dup2(pipefd[1], 1); |
| 671 | dup2(pipefd[1], 2); |
| 672 | |
| 673 | cmd[2] = trash.area; |
| 674 | execvp(cmd[0], cmd); |
| 675 | printf("execvp() failed\n"); |
| 676 | exit(1); |
| 677 | } |
| 678 | |
| 679 | /* parent */ |
| 680 | thread_release(); |
| 681 | close(pipefd[1]); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 682 | chunk_reset(&trash); |
| 683 | while (1) { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 684 | size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data); |
| 685 | if (ret <= 0) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 686 | break; |
| 687 | trash.data += ret; |
| 688 | if (trash.data + 20 == trash.size) { |
| 689 | chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n"); |
| 690 | break; |
| 691 | } |
| 692 | } |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 693 | close(pipefd[0]); |
| 694 | waitpid(pid, NULL, WNOHANG); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 695 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 696 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 697 | |
| 698 | fail_fork: |
| 699 | fail_fcntl: |
| 700 | close(pipefd[0]); |
| 701 | close(pipefd[1]); |
| 702 | fail_pipe: |
| 703 | thread_release(); |
| 704 | return cli_err(appctx, "Failed to execute command.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 705 | } |
Willy Tarreau | bd3b44e | 2023-03-09 08:25:01 +0100 | [diff] [blame] | 706 | |
| 707 | /* handles SIGRTMAX to inject random delays on the receiving thread in order |
| 708 | * to try to increase the likelihood to reproduce inter-thread races. The |
| 709 | * signal is periodically sent by a task initiated by "debug dev delay-inj". |
| 710 | */ |
| 711 | void debug_delay_inj_sighandler(int sig, siginfo_t *si, void *arg) |
| 712 | { |
| 713 | volatile int i = statistical_prng_range(10000); |
| 714 | |
| 715 | while (i--) |
| 716 | __ha_cpu_relax(); |
| 717 | } |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 718 | #endif |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 719 | |
| 720 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 721 | static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private) |
| 722 | { |
| 723 | unsigned long start, len; |
| 724 | |
| 725 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 726 | return 1; |
| 727 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 728 | if (!*args[3]) |
| 729 | return cli_err(appctx, "Missing memory address to dump from.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 730 | |
| 731 | start = strtoul(args[3], NULL, 0); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 732 | if (!start) |
| 733 | return cli_err(appctx, "Will not dump from NULL address.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 734 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 735 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 736 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 737 | /* by default, dump ~128 till next block of 16 */ |
| 738 | len = strtoul(args[4], NULL, 0); |
| 739 | if (!len) |
| 740 | len = ((start + 128) & -16) - start; |
| 741 | |
| 742 | chunk_reset(&trash); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 743 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 744 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 745 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Willy Tarreau | 48129be | 2021-05-04 18:40:50 +0200 | [diff] [blame] | 748 | /* parse a "debug dev sym <addr>" command. It always returns 1. */ |
| 749 | static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private) |
| 750 | { |
| 751 | unsigned long addr; |
| 752 | |
| 753 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 754 | return 1; |
| 755 | |
| 756 | if (!*args[3]) |
| 757 | return cli_err(appctx, "Missing memory address to be resolved.\n"); |
| 758 | |
| 759 | _HA_ATOMIC_INC(&debug_commands_issued); |
| 760 | |
| 761 | addr = strtoul(args[3], NULL, 0); |
| 762 | chunk_printf(&trash, "%#lx resolves to ", addr); |
| 763 | resolve_sym_name(&trash, NULL, (const void *)addr); |
| 764 | chunk_appendf(&trash, "\n"); |
| 765 | |
| 766 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 767 | } |
| 768 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 769 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 770 | static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private) |
| 771 | { |
| 772 | int thr = 0; |
| 773 | int sig = SIGABRT; |
| 774 | |
| 775 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 776 | return 1; |
| 777 | |
| 778 | if (*args[3]) |
| 779 | thr = atoi(args[3]); |
| 780 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 781 | if (thr < 0 || thr > global.nbthread) |
| 782 | 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] | 783 | |
| 784 | if (*args[4]) |
| 785 | sig = atoi(args[4]); |
| 786 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 787 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 788 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 789 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 790 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 791 | raise(sig); |
| 792 | return 1; |
| 793 | } |
| 794 | |
Erwan Le Goas | 54966df | 2022-09-14 17:24:22 +0200 | [diff] [blame] | 795 | /* hashes 'word' in "debug dev hash 'word' ". */ |
| 796 | static int debug_parse_cli_hash(char **args, char *payload, struct appctx *appctx, void *private) |
| 797 | { |
| 798 | char *msg = NULL; |
| 799 | |
| 800 | cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", HA_ANON_CLI(args[3]))); |
| 801 | return 1; |
| 802 | } |
| 803 | |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 804 | /* parse a "debug dev write" command. It always returns 1. */ |
| 805 | static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private) |
| 806 | { |
| 807 | unsigned long len; |
| 808 | |
| 809 | if (!*args[3]) |
| 810 | return cli_err(appctx, "Missing output size.\n"); |
| 811 | |
| 812 | len = strtoul(args[3], NULL, 0); |
| 813 | if (len >= trash.size) |
| 814 | return cli_err(appctx, "Output too large, must be <tune.bufsize.\n"); |
| 815 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 816 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 817 | |
| 818 | chunk_reset(&trash); |
| 819 | trash.data = len; |
| 820 | memset(trash.area, '.', trash.data); |
| 821 | trash.area[trash.data] = 0; |
| 822 | for (len = 64; len < trash.data; len += 64) |
| 823 | trash.area[len] = '\n'; |
| 824 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 825 | } |
| 826 | |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 827 | /* parse a "debug dev stream" command */ |
| 828 | /* |
| 829 | * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \ |
| 830 | * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \ |
| 831 | * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \ |
| 832 | * [sif.s[=<state>]] [sib.s[=<state>]] |
| 833 | */ |
| 834 | static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private) |
| 835 | { |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 836 | struct stream *s = appctx_strm(appctx); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 837 | int arg; |
| 838 | void *ptr; |
| 839 | int size; |
| 840 | const char *word, *end; |
| 841 | struct ist name; |
| 842 | char *msg = NULL; |
| 843 | char *endarg; |
| 844 | unsigned long long old, new; |
| 845 | |
| 846 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 847 | return 1; |
| 848 | |
| 849 | ptr = NULL; size = 0; |
| 850 | |
| 851 | if (!*args[3]) { |
| 852 | return cli_err(appctx, |
Willy Tarreau | dd01448 | 2023-05-02 16:37:13 +0200 | [diff] [blame] | 853 | "Usage: debug dev stream [ strm=<ptr> ] { <obj> <op> <value> | wake }*\n" |
| 854 | " <obj> = { strm.f | strm.x | scf.s | scb.s | txn.f | req.f | res.f }\n" |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 855 | " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n" |
| 856 | " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n" |
| 857 | " 'wake' wakes the stream asssigned to 'strm' (default: current)\n" |
| 858 | ); |
| 859 | } |
| 860 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 861 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 862 | for (arg = 3; *args[arg]; arg++) { |
| 863 | old = 0; |
| 864 | end = word = args[arg]; |
| 865 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 866 | end++; |
| 867 | name = ist2(word, end - word); |
| 868 | if (isteq(name, ist("strm"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 869 | ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 870 | } else if (isteq(name, ist("strm.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 871 | ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags); |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 872 | } else if (isteq(name, ist("strm.x"))) { |
| 873 | 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] | 874 | } else if (isteq(name, ist("txn.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 875 | 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] | 876 | } else if (isteq(name, ist("req.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 877 | 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] | 878 | } else if (isteq(name, ist("res.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 879 | ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags); |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 880 | } else if (isteq(name, ist("scf.s"))) { |
| 881 | ptr = (!s || !may_access(s)) ? NULL : &s->scf->state; size = sizeof(s->scf->state); |
| 882 | } else if (isteq(name, ist("scb.s"))) { |
| 883 | 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] | 884 | } else if (isteq(name, ist("wake"))) { |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 885 | if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1)) |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 886 | task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG); |
| 887 | continue; |
| 888 | } else |
| 889 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word)); |
| 890 | |
| 891 | /* read previous value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 892 | 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] | 893 | if (size == 8) |
| 894 | old = read_u64(ptr); |
| 895 | else if (size == 4) |
| 896 | old = read_u32(ptr); |
| 897 | else if (size == 2) |
| 898 | old = read_u16(ptr); |
| 899 | else |
| 900 | old = *(const uint8_t *)ptr; |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 901 | } else { |
| 902 | memprintf(&msg, |
| 903 | "%sSkipping inaccessible pointer %p for field '%.*s'.\n", |
| 904 | msg ? msg : "", ptr, (int)(end - word), word); |
| 905 | continue; |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* parse the new value . */ |
| 909 | new = strtoll(end + 1, &endarg, 0); |
| 910 | if (end[1] && *endarg) { |
| 911 | if (strcmp(end + 1, "now") == 0) |
| 912 | new = now_ms; |
| 913 | else { |
| 914 | memprintf(&msg, |
| 915 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 916 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 917 | continue; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | switch (*end) { |
| 922 | case '\0': /* show */ |
| 923 | memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old); |
| 924 | new = old; // do not change the value |
| 925 | break; |
| 926 | |
| 927 | case '=': /* set */ |
| 928 | break; |
| 929 | |
| 930 | case '^': /* XOR */ |
| 931 | new = old ^ new; |
| 932 | break; |
| 933 | |
| 934 | case '+': /* OR */ |
| 935 | new = old | new; |
| 936 | break; |
| 937 | |
| 938 | case '-': /* AND NOT */ |
| 939 | new = old & ~new; |
| 940 | break; |
| 941 | |
| 942 | default: |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | /* write the new value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 947 | if (new != old) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 948 | if (size == 8) |
| 949 | write_u64(ptr, new); |
| 950 | else if (size == 4) |
| 951 | write_u32(ptr, new); |
| 952 | else if (size == 2) |
| 953 | write_u16(ptr, new); |
| 954 | else |
| 955 | *(uint8_t *)ptr = new; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (msg && *msg) |
| 960 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 961 | return 1; |
| 962 | } |
| 963 | |
Willy Tarreau | 9867987 | 2023-05-03 11:22:45 +0200 | [diff] [blame] | 964 | /* parse a "debug dev stream" command */ |
| 965 | /* |
| 966 | * debug dev task <ptr> [ "wake" | "expire" | "kill" ] |
| 967 | * Show/change status of a task/tasklet |
| 968 | */ |
| 969 | static int debug_parse_cli_task(char **args, char *payload, struct appctx *appctx, void *private) |
| 970 | { |
| 971 | const struct ha_caller *caller; |
| 972 | struct task *t; |
| 973 | char *endarg; |
| 974 | char *msg; |
| 975 | void *ptr; |
| 976 | int ret = 1; |
| 977 | int task_ok; |
| 978 | int arg; |
| 979 | |
| 980 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 981 | return 1; |
| 982 | |
| 983 | /* parse the pointer value */ |
Willy Tarreau | 94df1b5 | 2023-05-12 04:40:06 +0200 | [diff] [blame] | 984 | ptr = (void *)strtoul(args[3], &endarg, 0); |
Willy Tarreau | 9867987 | 2023-05-03 11:22:45 +0200 | [diff] [blame] | 985 | if (!*args[3] || *endarg) |
| 986 | goto usage; |
| 987 | |
| 988 | _HA_ATOMIC_INC(&debug_commands_issued); |
| 989 | |
| 990 | /* everything below must run under thread isolation till reaching label "leave" */ |
| 991 | thread_isolate(); |
| 992 | |
| 993 | /* struct tasklet is smaller than struct task and is sufficient to check |
| 994 | * the TASK_COMMON part. |
| 995 | */ |
| 996 | if (!may_access(ptr) || !may_access(ptr + sizeof(struct tasklet) - 1) || |
| 997 | ((const struct tasklet *)ptr)->tid < -1 || |
| 998 | ((const struct tasklet *)ptr)->tid >= (int)MAX_THREADS) { |
| 999 | ret = cli_err(appctx, "The designated memory area doesn't look like a valid task/tasklet\n"); |
| 1000 | goto leave; |
| 1001 | } |
| 1002 | |
| 1003 | t = ptr; |
| 1004 | caller = t->caller; |
| 1005 | msg = NULL; |
Aurelien DARRAGON | b6a24a5 | 2023-05-15 11:59:08 +0200 | [diff] [blame] | 1006 | task_ok = may_access(ptr + sizeof(*t) - 1); |
Willy Tarreau | 9867987 | 2023-05-03 11:22:45 +0200 | [diff] [blame] | 1007 | |
| 1008 | chunk_reset(&trash); |
| 1009 | resolve_sym_name(&trash, NULL, (const void *)t->process); |
| 1010 | |
| 1011 | /* we need to be careful here because we may dump a freed task that's |
| 1012 | * still in the pool cache, containing garbage in pointers. |
| 1013 | */ |
| 1014 | if (!*args[4]) { |
| 1015 | memprintf(&msg, "%s%p: %s state=%#x tid=%d process=%s ctx=%p calls=%d last=%s:%d intl=%d", |
| 1016 | msg ? msg : "", t, (t->state & TASK_F_TASKLET) ? "tasklet" : "task", |
| 1017 | t->state, t->tid, trash.area, t->context, t->calls, |
Willy Tarreau | ff508f1 | 2023-05-03 16:28:54 +0200 | [diff] [blame] | 1018 | caller && may_access(caller) && may_access(caller->func) && isalnum((uchar)*caller->func) ? caller->func : "0", |
Willy Tarreau | 9867987 | 2023-05-03 11:22:45 +0200 | [diff] [blame] | 1019 | caller ? t->caller->line : 0, |
| 1020 | (t->state & TASK_F_TASKLET) ? LIST_INLIST(&((const struct tasklet *)t)->list) : 0); |
| 1021 | |
| 1022 | if (task_ok && !(t->state & TASK_F_TASKLET)) |
| 1023 | memprintf(&msg, "%s inrq=%d inwq=%d exp=%d nice=%d", |
| 1024 | msg ? msg : "", task_in_rq(t), task_in_wq(t), t->expire, t->nice); |
| 1025 | |
| 1026 | memprintf(&msg, "%s\n", msg ? msg : ""); |
| 1027 | } |
| 1028 | |
| 1029 | for (arg = 4; *args[arg]; arg++) { |
| 1030 | if (strcmp(args[arg], "expire") == 0) { |
| 1031 | if (t->state & TASK_F_TASKLET) { |
| 1032 | /* do nothing for tasklets */ |
| 1033 | } |
| 1034 | else if (task_ok) { |
| 1035 | /* unlink task and wake with timer flag */ |
| 1036 | __task_unlink_wq(t); |
| 1037 | t->expire = now_ms; |
| 1038 | task_wakeup(t, TASK_WOKEN_TIMER); |
| 1039 | } |
| 1040 | } else if (strcmp(args[arg], "wake") == 0) { |
| 1041 | /* wake with all flags but init / timer */ |
| 1042 | if (t->state & TASK_F_TASKLET) |
| 1043 | tasklet_wakeup((struct tasklet *)t); |
| 1044 | else if (task_ok) |
| 1045 | task_wakeup(t, TASK_WOKEN_ANY & ~(TASK_WOKEN_INIT|TASK_WOKEN_TIMER)); |
| 1046 | } else if (strcmp(args[arg], "kill") == 0) { |
| 1047 | /* Kill the task. This is not idempotent! */ |
| 1048 | if (!(t->state & TASK_KILLED)) { |
| 1049 | if (t->state & TASK_F_TASKLET) |
| 1050 | tasklet_kill((struct tasklet *)t); |
| 1051 | else if (task_ok) |
| 1052 | task_kill(t); |
| 1053 | } |
| 1054 | } else { |
| 1055 | thread_release(); |
| 1056 | goto usage; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | if (msg && *msg) |
| 1061 | ret = cli_dynmsg(appctx, LOG_INFO, msg); |
| 1062 | leave: |
| 1063 | thread_release(); |
| 1064 | return ret; |
| 1065 | usage: |
| 1066 | return cli_err(appctx, |
| 1067 | "Usage: debug dev task <ptr> [ wake | expire | kill ]\n" |
| 1068 | " By default, dumps some info on task/tasklet <ptr>. 'wake' will wake it up\n" |
| 1069 | " with all conditions flags but init/exp. 'expire' will expire the entry, and\n" |
| 1070 | " 'kill' will kill it (warning: may crash since later not idempotent!). All\n" |
| 1071 | " changes may crash the process if performed on a wrong object!\n" |
| 1072 | ); |
| 1073 | } |
| 1074 | |
Willy Tarreau | bd3b44e | 2023-03-09 08:25:01 +0100 | [diff] [blame] | 1075 | #if defined(DEBUG_DEV) |
| 1076 | static struct task *debug_delay_inj_task(struct task *t, void *ctx, unsigned int state) |
| 1077 | { |
| 1078 | unsigned long *tctx = ctx; // [0] = interval, [1] = nbwakeups |
| 1079 | unsigned long inter = tctx[0]; |
| 1080 | unsigned long count = tctx[1]; |
| 1081 | unsigned long rnd; |
| 1082 | |
| 1083 | if (inter) |
| 1084 | t->expire = tick_add(now_ms, inter); |
| 1085 | else |
| 1086 | task_wakeup(t, TASK_WOKEN_MSG); |
| 1087 | |
| 1088 | /* wake a random thread */ |
| 1089 | while (count--) { |
| 1090 | rnd = statistical_prng_range(global.nbthread); |
| 1091 | ha_tkill(rnd, SIGRTMAX); |
| 1092 | } |
| 1093 | return t; |
| 1094 | } |
| 1095 | |
| 1096 | /* parse a "debug dev delay-inj" command |
| 1097 | * debug dev delay-inj <inter> <count> |
| 1098 | */ |
| 1099 | static int debug_parse_delay_inj(char **args, char *payload, struct appctx *appctx, void *private) |
| 1100 | { |
| 1101 | unsigned long *tctx; // [0] = inter, [2] = count |
| 1102 | struct task *task; |
| 1103 | |
| 1104 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1105 | return 1; |
| 1106 | |
| 1107 | if (!*args[4]) |
| 1108 | return cli_err(appctx, "Usage: debug dev delay-inj <inter_ms> <count>*\n"); |
| 1109 | |
| 1110 | _HA_ATOMIC_INC(&debug_commands_issued); |
| 1111 | |
Willy Tarreau | 8330d9f | 2024-02-10 11:35:07 +0100 | [diff] [blame] | 1112 | tctx = calloc(2, sizeof(*tctx)); |
Willy Tarreau | bd3b44e | 2023-03-09 08:25:01 +0100 | [diff] [blame] | 1113 | if (!tctx) |
| 1114 | goto fail; |
| 1115 | |
| 1116 | tctx[0] = atoi(args[3]); |
| 1117 | tctx[1] = atoi(args[4]); |
| 1118 | |
| 1119 | task = task_new_here/*anywhere*/(); |
| 1120 | if (!task) |
| 1121 | goto fail; |
| 1122 | |
| 1123 | task->process = debug_delay_inj_task; |
| 1124 | task->context = tctx; |
| 1125 | task_wakeup(task, TASK_WOKEN_INIT); |
| 1126 | return 1; |
| 1127 | |
| 1128 | fail: |
| 1129 | free(tctx); |
| 1130 | return cli_err(appctx, "Not enough memory"); |
| 1131 | } |
| 1132 | #endif // DEBUG_DEV |
| 1133 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1134 | 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] | 1135 | { |
| 1136 | unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 1137 | unsigned long inter = tctx[1]; |
| 1138 | unsigned long rnd; |
| 1139 | |
| 1140 | t->expire = tick_add(now_ms, inter); |
| 1141 | |
| 1142 | /* half of the calls will wake up another entry */ |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 1143 | rnd = statistical_prng(); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1144 | if (rnd & 1) { |
| 1145 | rnd >>= 1; |
| 1146 | rnd %= tctx[0]; |
| 1147 | rnd = tctx[rnd + 2]; |
| 1148 | |
| 1149 | if (rnd & 1) |
| 1150 | task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG); |
| 1151 | else |
| 1152 | tasklet_wakeup((struct tasklet *)rnd); |
| 1153 | } |
| 1154 | return t; |
| 1155 | } |
| 1156 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1157 | 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] | 1158 | { |
| 1159 | unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 1160 | unsigned long rnd; |
| 1161 | int i; |
| 1162 | |
| 1163 | /* wake up two random entries */ |
| 1164 | for (i = 0; i < 2; i++) { |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 1165 | rnd = statistical_prng() % tctx[0]; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1166 | rnd = tctx[rnd + 2]; |
| 1167 | |
| 1168 | if (rnd & 1) |
| 1169 | task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG); |
| 1170 | else |
| 1171 | tasklet_wakeup((struct tasklet *)rnd); |
| 1172 | } |
| 1173 | return t; |
| 1174 | } |
| 1175 | |
| 1176 | /* parse a "debug dev sched" command |
| 1177 | * debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>] |
| 1178 | */ |
| 1179 | static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private) |
| 1180 | { |
| 1181 | int arg; |
| 1182 | void *ptr; |
| 1183 | int size; |
| 1184 | const char *word, *end; |
| 1185 | struct ist name; |
| 1186 | char *msg = NULL; |
| 1187 | char *endarg; |
| 1188 | unsigned long long new; |
| 1189 | unsigned long count = 0; |
Willy Tarreau | 8e5c53a | 2022-06-15 16:32:41 +0200 | [diff] [blame] | 1190 | unsigned long thrid = tid; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1191 | unsigned int inter = 0; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1192 | unsigned long i; |
| 1193 | int mode = 0; // 0 = tasklet; 1 = task |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1194 | unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) } |
| 1195 | |
| 1196 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1197 | return 1; |
| 1198 | |
| 1199 | ptr = NULL; size = 0; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1200 | |
| 1201 | if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) { |
| 1202 | return cli_err(appctx, |
| 1203 | "Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n" |
Willy Tarreau | 8e5c53a | 2022-06-15 16:32:41 +0200 | [diff] [blame] | 1204 | " <obj> = {count | tid | inter }\n" |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1205 | " <value> = 64-bit dec/hex integer (0x prefix supported)\n" |
| 1206 | ); |
| 1207 | } |
| 1208 | |
| 1209 | mode = strcmp(args[3], "task") == 0; |
| 1210 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1211 | _HA_ATOMIC_INC(&debug_commands_issued); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1212 | for (arg = 4; *args[arg]; arg++) { |
| 1213 | end = word = args[arg]; |
| 1214 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 1215 | end++; |
| 1216 | name = ist2(word, end - word); |
| 1217 | if (isteq(name, ist("count"))) { |
| 1218 | ptr = &count; size = sizeof(count); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1219 | } else if (isteq(name, ist("tid"))) { |
| 1220 | ptr = &thrid; size = sizeof(thrid); |
| 1221 | } else if (isteq(name, ist("inter"))) { |
| 1222 | ptr = &inter; size = sizeof(inter); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1223 | } else |
| 1224 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word)); |
| 1225 | |
| 1226 | /* parse the new value . */ |
| 1227 | new = strtoll(end + 1, &endarg, 0); |
| 1228 | if (end[1] && *endarg) { |
| 1229 | memprintf(&msg, |
| 1230 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 1231 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
| 1235 | /* write the new value */ |
| 1236 | if (size == 8) |
| 1237 | write_u64(ptr, new); |
| 1238 | else if (size == 4) |
| 1239 | write_u32(ptr, new); |
| 1240 | else if (size == 2) |
| 1241 | write_u16(ptr, new); |
| 1242 | else |
| 1243 | *(uint8_t *)ptr = new; |
| 1244 | } |
| 1245 | |
Willy Tarreau | 8330d9f | 2024-02-10 11:35:07 +0100 | [diff] [blame] | 1246 | tctx = calloc(count + 2, sizeof(*tctx)); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1247 | if (!tctx) |
| 1248 | goto fail; |
| 1249 | |
| 1250 | tctx[0] = (unsigned long)count; |
| 1251 | tctx[1] = (unsigned long)inter; |
| 1252 | |
Willy Tarreau | 8e5c53a | 2022-06-15 16:32:41 +0200 | [diff] [blame] | 1253 | if (thrid >= global.nbthread) |
| 1254 | thrid = tid; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1255 | |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1256 | for (i = 0; i < count; i++) { |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1257 | /* now, if poly or mask was set, tmask corresponds to the |
| 1258 | * valid thread mask to use, otherwise it remains zero. |
| 1259 | */ |
| 1260 | //printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask); |
| 1261 | if (mode == 0) { |
| 1262 | struct tasklet *tl = tasklet_new(); |
| 1263 | |
| 1264 | if (!tl) |
| 1265 | goto fail; |
| 1266 | |
Willy Tarreau | 8e5c53a | 2022-06-15 16:32:41 +0200 | [diff] [blame] | 1267 | tl->tid = thrid; |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1268 | tl->process = debug_tasklet_handler; |
| 1269 | tl->context = tctx; |
| 1270 | tctx[i + 2] = (unsigned long)tl; |
| 1271 | } else { |
Willy Tarreau | 8e5c53a | 2022-06-15 16:32:41 +0200 | [diff] [blame] | 1272 | struct task *task = task_new_on(thrid); |
Willy Tarreau | a5a4479 | 2020-11-29 17:12:15 +0100 | [diff] [blame] | 1273 | |
| 1274 | if (!task) |
| 1275 | goto fail; |
| 1276 | |
| 1277 | task->process = debug_task_handler; |
| 1278 | task->context = tctx; |
| 1279 | tctx[i + 2] = (unsigned long)task + 1; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | /* start the tasks and tasklets */ |
| 1284 | for (i = 0; i < count; i++) { |
| 1285 | unsigned long ctx = tctx[i + 2]; |
| 1286 | |
| 1287 | if (ctx & 1) |
| 1288 | task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT); |
| 1289 | else |
| 1290 | tasklet_wakeup((struct tasklet *)ctx); |
| 1291 | } |
| 1292 | |
| 1293 | if (msg && *msg) |
| 1294 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 1295 | return 1; |
| 1296 | |
| 1297 | fail: |
| 1298 | /* free partially allocated entries */ |
| 1299 | for (i = 0; tctx && i < count; i++) { |
| 1300 | unsigned long ctx = tctx[i + 2]; |
| 1301 | |
| 1302 | if (!ctx) |
| 1303 | break; |
| 1304 | |
| 1305 | if (ctx & 1) |
| 1306 | task_destroy((struct task *)(ctx - 1)); |
| 1307 | else |
| 1308 | tasklet_free((struct tasklet *)ctx); |
| 1309 | } |
| 1310 | |
| 1311 | free(tctx); |
| 1312 | return cli_err(appctx, "Not enough memory"); |
| 1313 | } |
| 1314 | |
Willy Tarreau | e06bbf3 | 2022-05-05 14:26:28 +0200 | [diff] [blame] | 1315 | /* CLI state for "debug dev fd" */ |
| 1316 | struct dev_fd_ctx { |
| 1317 | int start_fd; |
| 1318 | }; |
| 1319 | |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1320 | /* 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] | 1321 | * stored in a struct dev_fd_ctx pointed to by svcctx. |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1322 | */ |
| 1323 | static int debug_parse_cli_fd(char **args, char *payload, struct appctx *appctx, void *private) |
| 1324 | { |
Willy Tarreau | e06bbf3 | 2022-05-05 14:26:28 +0200 | [diff] [blame] | 1325 | struct dev_fd_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 1326 | |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1327 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1328 | return 1; |
| 1329 | |
| 1330 | /* start at fd #0 */ |
Willy Tarreau | e06bbf3 | 2022-05-05 14:26:28 +0200 | [diff] [blame] | 1331 | ctx->start_fd = 0; |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | /* CLI I/O handler for the "debug dev fd" command. Dumps all FDs that are |
| 1336 | * 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] | 1337 | * 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] | 1338 | */ |
| 1339 | static int debug_iohandler_fd(struct appctx *appctx) |
| 1340 | { |
Willy Tarreau | e06bbf3 | 2022-05-05 14:26:28 +0200 | [diff] [blame] | 1341 | struct dev_fd_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1342 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1343 | struct sockaddr_storage sa; |
| 1344 | struct stat statbuf; |
| 1345 | socklen_t salen, vlen; |
| 1346 | int ret1, ret2, port; |
| 1347 | char *addrstr; |
| 1348 | int ret = 1; |
| 1349 | int i, fd; |
| 1350 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1351 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1352 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1353 | goto end; |
| 1354 | |
| 1355 | chunk_reset(&trash); |
| 1356 | |
| 1357 | thread_isolate(); |
| 1358 | |
| 1359 | /* we have two inner loops here, one for the proxy, the other one for |
| 1360 | * the buffer. |
| 1361 | */ |
Willy Tarreau | e06bbf3 | 2022-05-05 14:26:28 +0200 | [diff] [blame] | 1362 | for (fd = ctx->start_fd; fd < global.maxsock; fd++) { |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1363 | /* check for FD's existence */ |
| 1364 | ret1 = fcntl(fd, F_GETFD, 0); |
| 1365 | if (ret1 == -1) |
| 1366 | continue; // not known to the process |
| 1367 | if (fdtab[fd].owner) |
| 1368 | continue; // well-known |
| 1369 | |
| 1370 | /* OK we're seeing an orphan let's try to retrieve as much |
| 1371 | * information as possible about it. |
| 1372 | */ |
| 1373 | chunk_printf(&trash, "%5d", fd); |
| 1374 | |
| 1375 | if (fstat(fd, &statbuf) != -1) { |
| 1376 | chunk_appendf(&trash, " type=%s mod=%04o dev=%#llx siz=%#llx uid=%lld gid=%lld fs=%#llx ino=%#llx", |
| 1377 | isatty(fd) ? "tty.": |
| 1378 | S_ISREG(statbuf.st_mode) ? "file": |
| 1379 | S_ISDIR(statbuf.st_mode) ? "dir.": |
| 1380 | S_ISCHR(statbuf.st_mode) ? "chr.": |
| 1381 | S_ISBLK(statbuf.st_mode) ? "blk.": |
| 1382 | S_ISFIFO(statbuf.st_mode) ? "pipe": |
| 1383 | S_ISLNK(statbuf.st_mode) ? "link": |
| 1384 | S_ISSOCK(statbuf.st_mode) ? "sock": |
| 1385 | #ifdef USE_EPOLL |
Willy Tarreau | 2058f53 | 2023-07-02 10:49:49 +0200 | [diff] [blame] | 1386 | /* trick: epoll_ctl() will return -ENOENT when trying |
| 1387 | * to remove from a valid epoll FD an FD that was not |
| 1388 | * registered against it. But we don't want to risk |
| 1389 | * disabling a random FD. Instead we'll create a new |
| 1390 | * one by duplicating 0 (it should be valid since |
| 1391 | * pointing to a terminal or /dev/null), and try to |
| 1392 | * remove it. |
| 1393 | */ |
| 1394 | ({ |
| 1395 | int fd2 = dup(0); |
| 1396 | int ret = fd2; |
| 1397 | if (ret >= 0) { |
| 1398 | ret = epoll_ctl(fd, EPOLL_CTL_DEL, fd2, NULL); |
| 1399 | if (ret == -1 && errno == ENOENT) |
| 1400 | ret = 0; // that's a real epoll |
| 1401 | else |
| 1402 | ret = -1; // it's something else |
| 1403 | close(fd2); |
| 1404 | } |
| 1405 | ret; |
| 1406 | }) == 0 ? "epol" : |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1407 | #endif |
| 1408 | "????", |
| 1409 | (uint)statbuf.st_mode & 07777, |
| 1410 | |
| 1411 | (ullong)statbuf.st_rdev, |
| 1412 | (ullong)statbuf.st_size, |
| 1413 | (ullong)statbuf.st_uid, |
| 1414 | (ullong)statbuf.st_gid, |
| 1415 | |
| 1416 | (ullong)statbuf.st_dev, |
| 1417 | (ullong)statbuf.st_ino); |
| 1418 | } |
| 1419 | |
| 1420 | chunk_appendf(&trash, " getfd=%s+%#x", |
| 1421 | (ret1 & FD_CLOEXEC) ? "cloex" : "", |
| 1422 | ret1 &~ FD_CLOEXEC); |
| 1423 | |
| 1424 | /* FD options */ |
| 1425 | ret2 = fcntl(fd, F_GETFL, 0); |
| 1426 | if (ret2) { |
| 1427 | chunk_appendf(&trash, " getfl=%s", |
| 1428 | (ret1 & 3) >= 2 ? "O_RDWR" : |
| 1429 | (ret1 & 1) ? "O_WRONLY" : "O_RDONLY"); |
| 1430 | |
| 1431 | for (i = 2; i < 32; i++) { |
| 1432 | if (!(ret2 & (1UL << i))) |
| 1433 | continue; |
| 1434 | switch (1UL << i) { |
| 1435 | case O_CREAT: chunk_appendf(&trash, ",O_CREAT"); break; |
| 1436 | case O_EXCL: chunk_appendf(&trash, ",O_EXCL"); break; |
| 1437 | case O_NOCTTY: chunk_appendf(&trash, ",O_NOCTTY"); break; |
| 1438 | case O_TRUNC: chunk_appendf(&trash, ",O_TRUNC"); break; |
| 1439 | case O_APPEND: chunk_appendf(&trash, ",O_APPEND"); break; |
Willy Tarreau | 410942b | 2022-01-25 14:51:53 +0100 | [diff] [blame] | 1440 | #ifdef O_ASYNC |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1441 | case O_ASYNC: chunk_appendf(&trash, ",O_ASYNC"); break; |
Willy Tarreau | 410942b | 2022-01-25 14:51:53 +0100 | [diff] [blame] | 1442 | #endif |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1443 | #ifdef O_DIRECT |
| 1444 | case O_DIRECT: chunk_appendf(&trash, ",O_DIRECT"); break; |
| 1445 | #endif |
| 1446 | #ifdef O_NOATIME |
| 1447 | case O_NOATIME: chunk_appendf(&trash, ",O_NOATIME"); break; |
| 1448 | #endif |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | vlen = sizeof(ret2); |
| 1454 | ret1 = getsockopt(fd, SOL_SOCKET, SO_TYPE, &ret2, &vlen); |
| 1455 | if (ret1 != -1) |
| 1456 | chunk_appendf(&trash, " so_type=%d", ret2); |
| 1457 | |
| 1458 | vlen = sizeof(ret2); |
| 1459 | ret1 = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ret2, &vlen); |
| 1460 | if (ret1 != -1) |
| 1461 | chunk_appendf(&trash, " so_accept=%d", ret2); |
| 1462 | |
| 1463 | vlen = sizeof(ret2); |
| 1464 | ret1 = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret2, &vlen); |
| 1465 | if (ret1 != -1) |
| 1466 | chunk_appendf(&trash, " so_error=%d", ret2); |
| 1467 | |
| 1468 | salen = sizeof(sa); |
| 1469 | if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) { |
| 1470 | if (sa.ss_family == AF_INET) |
| 1471 | port = ntohs(((const struct sockaddr_in *)&sa)->sin_port); |
| 1472 | else if (sa.ss_family == AF_INET6) |
| 1473 | port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port); |
| 1474 | else |
| 1475 | port = 0; |
| 1476 | addrstr = sa2str(&sa, port, 0); |
| 1477 | chunk_appendf(&trash, " laddr=%s", addrstr); |
| 1478 | free(addrstr); |
| 1479 | } |
| 1480 | |
| 1481 | salen = sizeof(sa); |
| 1482 | if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) { |
| 1483 | if (sa.ss_family == AF_INET) |
| 1484 | port = ntohs(((const struct sockaddr_in *)&sa)->sin_port); |
| 1485 | else if (sa.ss_family == AF_INET6) |
| 1486 | port = ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port); |
| 1487 | else |
| 1488 | port = 0; |
| 1489 | addrstr = sa2str(&sa, port, 0); |
| 1490 | chunk_appendf(&trash, " raddr=%s", addrstr); |
| 1491 | free(addrstr); |
| 1492 | } |
| 1493 | |
| 1494 | chunk_appendf(&trash, "\n"); |
| 1495 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1496 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | e06bbf3 | 2022-05-05 14:26:28 +0200 | [diff] [blame] | 1497 | ctx->start_fd = fd; |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1498 | ret = 0; |
| 1499 | break; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | thread_release(); |
| 1504 | end: |
| 1505 | return ret; |
| 1506 | } |
| 1507 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1508 | #if defined(DEBUG_MEM_STATS) |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1509 | |
| 1510 | /* CLI state for "debug dev memstats" */ |
| 1511 | struct dev_mem_ctx { |
| 1512 | struct mem_stats *start, *stop; /* begin/end of dump */ |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1513 | char *match; /* non-null if a name prefix is specified */ |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1514 | int show_all; /* show all entries if non-null */ |
Willy Tarreau | b59e3f6 | 2022-11-30 17:16:51 +0100 | [diff] [blame] | 1515 | int width; /* 1st column width */ |
| 1516 | long tot_size; /* sum of alloc-free */ |
| 1517 | ulong tot_calls; /* sum of calls */ |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1518 | }; |
| 1519 | |
| 1520 | /* 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] | 1521 | static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private) |
| 1522 | { |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1523 | struct dev_mem_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Willy Tarreau | 334d091 | 2022-11-30 16:50:48 +0100 | [diff] [blame] | 1524 | int arg; |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1525 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1526 | extern __attribute__((__weak__)) struct mem_stats __start_mem_stats; |
| 1527 | extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats; |
| 1528 | |
| 1529 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1530 | return 1; |
| 1531 | |
Willy Tarreau | 334d091 | 2022-11-30 16:50:48 +0100 | [diff] [blame] | 1532 | for (arg = 3; *args[arg]; arg++) { |
| 1533 | if (strcmp(args[arg], "reset") == 0) { |
| 1534 | struct mem_stats *ptr; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1535 | |
Willy Tarreau | 334d091 | 2022-11-30 16:50:48 +0100 | [diff] [blame] | 1536 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1537 | return 1; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1538 | |
Willy Tarreau | 334d091 | 2022-11-30 16:50:48 +0100 | [diff] [blame] | 1539 | for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) { |
| 1540 | _HA_ATOMIC_STORE(&ptr->calls, 0); |
| 1541 | _HA_ATOMIC_STORE(&ptr->size, 0); |
| 1542 | } |
| 1543 | return 1; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1544 | } |
Willy Tarreau | 334d091 | 2022-11-30 16:50:48 +0100 | [diff] [blame] | 1545 | else if (strcmp(args[arg], "all") == 0) { |
| 1546 | ctx->show_all = 1; |
| 1547 | continue; |
| 1548 | } |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1549 | else if (strcmp(args[arg], "match") == 0 && *args[arg + 1]) { |
| 1550 | ha_free(&ctx->match); |
| 1551 | ctx->match = strdup(args[arg + 1]); |
| 1552 | arg++; |
| 1553 | continue; |
| 1554 | } |
Willy Tarreau | 334d091 | 2022-11-30 16:50:48 +0100 | [diff] [blame] | 1555 | else |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1556 | return cli_err(appctx, "Expects either 'reset', 'all', or 'match <pfx>'.\n"); |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1557 | } |
| 1558 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1559 | /* otherwise proceed with the dump from p0 to p1 */ |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1560 | ctx->start = &__start_mem_stats; |
| 1561 | ctx->stop = &__stop_mem_stats; |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1562 | ctx->width = 0; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
| 1565 | |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1566 | /* CLI I/O handler for the "debug dev memstats" command using a dev_mem_ctx |
| 1567 | * found in appctx->svcctx. Dumps all mem_stats structs referenced by pointers |
| 1568 | * located between ->start and ->stop. Dumps all entries if ->show_all != 0, |
| 1569 | * otherwise only non-zero calls. |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1570 | */ |
| 1571 | static int debug_iohandler_memstats(struct appctx *appctx) |
| 1572 | { |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1573 | struct dev_mem_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1574 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1575 | struct mem_stats *ptr; |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1576 | const char *pfx = ctx->match; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1577 | int ret = 1; |
| 1578 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1579 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1580 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1581 | goto end; |
| 1582 | |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1583 | if (!ctx->width) { |
| 1584 | /* we don't know the first column's width, let's compute it |
| 1585 | * now based on a first pass on printable entries and their |
| 1586 | * expected width (approximated). |
| 1587 | */ |
| 1588 | for (ptr = ctx->start; ptr != ctx->stop; ptr++) { |
| 1589 | const char *p, *name; |
| 1590 | int w = 0; |
| 1591 | char tmp; |
| 1592 | |
| 1593 | if (!ptr->size && !ptr->calls && !ctx->show_all) |
| 1594 | continue; |
| 1595 | |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1596 | for (p = name = ptr->caller.file; *p; p++) { |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1597 | if (*p == '/') |
| 1598 | name = p + 1; |
| 1599 | } |
| 1600 | |
| 1601 | if (ctx->show_all) |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1602 | w = snprintf(&tmp, 0, "%s(%s:%d) ", ptr->caller.func, name, ptr->caller.line); |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1603 | else |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1604 | w = snprintf(&tmp, 0, "%s:%d ", name, ptr->caller.line); |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1605 | |
| 1606 | if (w > ctx->width) |
| 1607 | ctx->width = w; |
| 1608 | } |
| 1609 | } |
| 1610 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1611 | /* we have two inner loops here, one for the proxy, the other one for |
| 1612 | * the buffer. |
| 1613 | */ |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1614 | for (ptr = ctx->start; ptr != ctx->stop; ptr++) { |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1615 | const char *type; |
| 1616 | const char *name; |
| 1617 | const char *p; |
Willy Tarreau | 55c950b | 2022-08-09 08:15:27 +0200 | [diff] [blame] | 1618 | const char *info = NULL; |
Willy Tarreau | 17200dd | 2022-08-09 08:40:08 +0200 | [diff] [blame] | 1619 | const char *func = NULL; |
Willy Tarreau | b59e3f6 | 2022-11-30 17:16:51 +0100 | [diff] [blame] | 1620 | int direction = 0; // neither alloc nor free (e.g. realloc) |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1621 | |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1622 | if (!ptr->size && !ptr->calls && !ctx->show_all) |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1623 | continue; |
| 1624 | |
| 1625 | /* basename only */ |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1626 | for (p = name = ptr->caller.file; *p; p++) { |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1627 | if (*p == '/') |
| 1628 | name = p + 1; |
| 1629 | } |
| 1630 | |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1631 | func = ptr->caller.func; |
Willy Tarreau | 17200dd | 2022-08-09 08:40:08 +0200 | [diff] [blame] | 1632 | |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1633 | switch (ptr->caller.what) { |
Willy Tarreau | b59e3f6 | 2022-11-30 17:16:51 +0100 | [diff] [blame] | 1634 | case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; direction = 1; break; |
| 1635 | case MEM_STATS_TYPE_FREE: type = "FREE"; direction = -1; break; |
| 1636 | case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; direction = 1; break; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1637 | case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break; |
Willy Tarreau | b59e3f6 | 2022-11-30 17:16:51 +0100 | [diff] [blame] | 1638 | case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; direction = 1; break; |
| 1639 | case MEM_STATS_TYPE_P_ALLOC: type = "P_ALLOC"; direction = 1; if (ptr->extra) info = ((const struct pool_head *)ptr->extra)->name; break; |
| 1640 | case MEM_STATS_TYPE_P_FREE: type = "P_FREE"; direction = -1; if (ptr->extra) info = ((const struct pool_head *)ptr->extra)->name; break; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1641 | default: type = "UNSET"; break; |
| 1642 | } |
| 1643 | |
| 1644 | //chunk_printf(&trash, |
| 1645 | // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 1646 | // name, ptr->line, type, |
| 1647 | // (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 1648 | // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 1649 | |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1650 | /* only match requested prefixes */ |
| 1651 | if (pfx && (!info || strncmp(info, pfx, strlen(pfx)) != 0)) |
| 1652 | continue; |
| 1653 | |
Willy Tarreau | 17200dd | 2022-08-09 08:40:08 +0200 | [diff] [blame] | 1654 | chunk_reset(&trash); |
| 1655 | if (ctx->show_all) |
| 1656 | chunk_appendf(&trash, "%s(", func); |
| 1657 | |
Willy Tarreau | d96d214 | 2022-09-06 08:05:59 +0200 | [diff] [blame] | 1658 | chunk_appendf(&trash, "%s:%d", name, ptr->caller.line); |
Willy Tarreau | 17200dd | 2022-08-09 08:40:08 +0200 | [diff] [blame] | 1659 | |
| 1660 | if (ctx->show_all) |
| 1661 | chunk_appendf(&trash, ")"); |
| 1662 | |
Willy Tarreau | 4a426e2 | 2022-08-09 08:51:08 +0200 | [diff] [blame] | 1663 | while (trash.data < ctx->width) |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1664 | trash.area[trash.data++] = ' '; |
Willy Tarreau | 17200dd | 2022-08-09 08:40:08 +0200 | [diff] [blame] | 1665 | |
Willy Tarreau | 55c950b | 2022-08-09 08:15:27 +0200 | [diff] [blame] | 1666 | chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu %s\n", |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1667 | type, |
| 1668 | (unsigned long)ptr->size, (unsigned long)ptr->calls, |
Willy Tarreau | 55c950b | 2022-08-09 08:15:27 +0200 | [diff] [blame] | 1669 | (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0), |
| 1670 | info ? info : ""); |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1671 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1672 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | 40e952f | 2022-05-05 14:39:51 +0200 | [diff] [blame] | 1673 | ctx->start = ptr; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1674 | ret = 0; |
Willy Tarreau | b59e3f6 | 2022-11-30 17:16:51 +0100 | [diff] [blame] | 1675 | goto end; |
| 1676 | } |
| 1677 | if (direction > 0) { |
| 1678 | ctx->tot_size += (ulong)ptr->size; |
| 1679 | ctx->tot_calls += (ulong)ptr->calls; |
| 1680 | } |
| 1681 | else if (direction < 0) { |
| 1682 | ctx->tot_size -= (ulong)ptr->size; |
| 1683 | ctx->tot_calls += (ulong)ptr->calls; |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | |
Willy Tarreau | b59e3f6 | 2022-11-30 17:16:51 +0100 | [diff] [blame] | 1687 | /* now dump a summary */ |
| 1688 | chunk_reset(&trash); |
| 1689 | chunk_appendf(&trash, "Total"); |
| 1690 | while (trash.data < ctx->width) |
| 1691 | trash.area[trash.data++] = ' '; |
| 1692 | |
| 1693 | chunk_appendf(&trash, "%7s size: %12ld calls: %9lu size/call: %6ld %s\n", |
| 1694 | "BALANCE", |
| 1695 | ctx->tot_size, ctx->tot_calls, |
| 1696 | (long)(ctx->tot_calls ? (ctx->tot_size / ctx->tot_calls) : 0), |
| 1697 | "(excl. realloc)"); |
| 1698 | |
| 1699 | if (applet_putchk(appctx, &trash) == -1) { |
| 1700 | ctx->start = ptr; |
| 1701 | ret = 0; |
| 1702 | goto end; |
| 1703 | } |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1704 | end: |
| 1705 | return ret; |
| 1706 | } |
| 1707 | |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1708 | /* release the "show pools" context */ |
| 1709 | static void debug_release_memstats(struct appctx *appctx) |
| 1710 | { |
| 1711 | struct dev_mem_ctx *ctx = appctx->svcctx; |
| 1712 | |
| 1713 | ha_free(&ctx->match); |
| 1714 | } |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1715 | #endif |
| 1716 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 1717 | #ifdef USE_THREAD_DUMP |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1718 | |
Willy Tarreau | f0c86dd | 2022-07-15 13:14:03 +0200 | [diff] [blame] | 1719 | /* handles DEBUGSIG to dump the state of the thread it's working on. This is |
| 1720 | * appended at the end of thread_dump_buffer which must be protected against |
| 1721 | * reentrance from different threads (a thread-local buffer works fine). |
| 1722 | */ |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1723 | void debug_handler(int sig, siginfo_t *si, void *arg) |
| 1724 | { |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 1725 | struct buffer *buf = HA_ATOMIC_LOAD(&th_ctx->thread_dump_buffer); |
Willy Tarreau | a2b8ed4 | 2022-07-01 17:11:03 +0200 | [diff] [blame] | 1726 | int harmless = is_thread_harmless(); |
| 1727 | |
Willy Tarreau | 82aafc4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 1728 | /* first, let's check it's really for us and that we didn't just get |
| 1729 | * a spurious DEBUGSIG. |
| 1730 | */ |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 1731 | if (!buf || buf == (void*)(0x1UL)) |
Willy Tarreau | 82aafc4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 1732 | return; |
| 1733 | |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 1734 | /* now dump the current state into the designated buffer, and indicate |
| 1735 | * we come from a sig handler. |
Willy Tarreau | f0c86dd | 2022-07-15 13:14:03 +0200 | [diff] [blame] | 1736 | */ |
Willy Tarreau | 9a6ecbd | 2023-05-04 16:23:51 +0200 | [diff] [blame] | 1737 | ha_thread_dump_one(tid, 1); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 1738 | |
| 1739 | /* mark the current thread as stuck to detect it upon next invocation |
| 1740 | * if it didn't move. |
| 1741 | */ |
Willy Tarreau | a2b8ed4 | 2022-07-01 17:11:03 +0200 | [diff] [blame] | 1742 | if (!harmless && |
Willy Tarreau | e7475c8 | 2022-06-20 09:23:24 +0200 | [diff] [blame] | 1743 | !(_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_SLEEPING)) |
Willy Tarreau | bdcd325 | 2022-06-22 09:19:46 +0200 | [diff] [blame] | 1744 | _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_STUCK); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static int init_debug_per_thread() |
| 1748 | { |
| 1749 | sigset_t set; |
| 1750 | |
| 1751 | /* unblock the DEBUGSIG signal we intend to use */ |
| 1752 | sigemptyset(&set); |
| 1753 | sigaddset(&set, DEBUGSIG); |
Willy Tarreau | bd3b44e | 2023-03-09 08:25:01 +0100 | [diff] [blame] | 1754 | #if defined(DEBUG_DEV) |
| 1755 | sigaddset(&set, SIGRTMAX); |
| 1756 | #endif |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1757 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
| 1761 | static int init_debug() |
| 1762 | { |
| 1763 | struct sigaction sa; |
Willy Tarreau | 2f1227e | 2021-01-22 12:12:29 +0100 | [diff] [blame] | 1764 | void *callers[1]; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | 0214b45 | 2020-03-04 06:01:40 +0100 | [diff] [blame] | 1766 | /* calling backtrace() will access libgcc at runtime. We don't want to |
| 1767 | * do it after the chroot, so let's perform a first call to have it |
| 1768 | * ready in memory for later use. |
| 1769 | */ |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 1770 | my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1771 | sa.sa_handler = NULL; |
| 1772 | sa.sa_sigaction = debug_handler; |
| 1773 | sigemptyset(&sa.sa_mask); |
| 1774 | sa.sa_flags = SA_SIGINFO; |
| 1775 | sigaction(DEBUGSIG, &sa, NULL); |
Willy Tarreau | bd3b44e | 2023-03-09 08:25:01 +0100 | [diff] [blame] | 1776 | |
| 1777 | #if defined(DEBUG_DEV) |
| 1778 | sa.sa_handler = NULL; |
| 1779 | sa.sa_sigaction = debug_delay_inj_sighandler; |
| 1780 | sigemptyset(&sa.sa_mask); |
| 1781 | sa.sa_flags = SA_SIGINFO; |
| 1782 | sigaction(SIGRTMAX, &sa, NULL); |
| 1783 | #endif |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1784 | return ERR_NONE; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | REGISTER_POST_CHECK(init_debug); |
| 1788 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 1789 | |
| 1790 | #endif /* USE_THREAD_DUMP */ |
| 1791 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1792 | /* register cli keywords */ |
| 1793 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1794 | {{ "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] | 1795 | {{ "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] | 1796 | {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | dadf00e | 2022-07-15 08:25:03 +0200 | [diff] [blame] | 1797 | {{ "debug", "dev", "deadlock", NULL }, "debug dev deadlock [nbtask] : deadlock between this number of tasks", debug_parse_cli_deadlock, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1798 | {{ "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] | 1799 | #if defined(DEBUG_DEV) |
Willy Tarreau | bd3b44e | 2023-03-09 08:25:01 +0100 | [diff] [blame] | 1800 | {{ "debug", "dev", "delay-inj", NULL },"debug dev delay-inj <inter> <count> : inject random delays into threads", debug_parse_delay_inj, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1801 | {{ "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] | 1802 | #endif |
Willy Tarreau | 5be7c19 | 2022-01-24 20:26:09 +0100 | [diff] [blame] | 1803 | {{ "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] | 1804 | {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 111c783 | 2022-11-30 17:51:47 +0100 | [diff] [blame] | 1805 | {{ "debug", "dev", "hash", NULL }, "debug dev hash [msg] : return msg hashed if anon is set", debug_parse_cli_hash, NULL, NULL, NULL, 0 }, |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1806 | {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len] : dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1807 | {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | e5e6223 | 2023-05-04 11:50:26 +0200 | [diff] [blame] | 1808 | {{ "debug", "dev", "loop", NULL }, "debug dev loop <ms> [isolated] : loop this long, possibly isolated", debug_parse_cli_loop, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1809 | #if defined(DEBUG_MEM_STATS) |
Willy Tarreau | e57fbed | 2022-11-30 16:42:54 +0100 | [diff] [blame] | 1810 | {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all|match ...]: dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, debug_release_memstats, NULL, 0 }, |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 1811 | #endif |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1812 | {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1813 | {{ "debug", "dev", "sched", NULL }, "debug dev sched {task|tasklet} [k=v]* : stress the scheduler", debug_parse_cli_sched, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1814 | {{ "debug", "dev", "stream",NULL }, "debug dev stream [k=v]* : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT }, |
| 1815 | {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 9867987 | 2023-05-03 11:22:45 +0200 | [diff] [blame] | 1816 | {{ "debug", "dev", "task", NULL }, "debug dev task <ptr> [wake|expire|kill] : show/wake/expire/kill task/tasklet", debug_parse_cli_task, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1817 | {{ "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] | 1818 | {{ "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] | 1819 | {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes in return", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT }, |
Erwan Le Goas | 54966df | 2022-09-14 17:24:22 +0200 | [diff] [blame] | 1820 | |
Willy Tarreau | 6ab7b21 | 2021-12-28 09:57:10 +0100 | [diff] [blame] | 1821 | #if defined(HA_HAVE_DUMP_LIBS) |
| 1822 | {{ "show", "libs", NULL, NULL }, "show libs : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL }, |
| 1823 | #endif |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1824 | {{ "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] | 1825 | {{},} |
| 1826 | }}; |
| 1827 | |
| 1828 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |