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