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 | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 14 | #include <fcntl.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 15 | #include <signal.h> |
| 16 | #include <time.h> |
| 17 | #include <stdio.h> |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 19 | #include <syslog.h> |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 23 | #include <haproxy/api.h> |
Willy Tarreau | 8dabda7 | 2020-05-27 17:22:10 +0200 | [diff] [blame] | 24 | #include <haproxy/buf.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Willy Tarreau | 2a83d60 | 2020-05-27 16:58:08 +0200 | [diff] [blame] | 26 | #include <haproxy/debug.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/fd.h> |
| 28 | #include <haproxy/global.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 29 | #include <haproxy/hlua.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/net_helper.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 32 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 33 | #include <haproxy/task.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 34 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 35 | #include <haproxy/tools.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 36 | #include <import/ist.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 39 | /* mask of threads still having to dump, used to respect ordering. Only used |
| 40 | * when USE_THREAD_DUMP is set. |
| 41 | */ |
| 42 | volatile unsigned long threads_to_dump = 0; |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 43 | unsigned int debug_commands_issued = 0; |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 45 | /* Dumps to the buffer some known information for the desired thread, and |
| 46 | * optionally extra info for the current thread. The dump will be appended to |
| 47 | * the buffer, so the caller is responsible for preliminary initializing it. |
| 48 | * 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] | 49 | * in front of the calling thread's line (usually it's tid). Any stuck thread |
| 50 | * is also prefixed with a '>'. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 51 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 52 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 53 | { |
| 54 | unsigned long thr_bit = 1UL << thr; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 55 | unsigned long long p = ha_thread_info[thr].prev_cpu_time; |
| 56 | unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]); |
| 57 | int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 59 | chunk_appendf(buf, |
Willy Tarreau | f0e5da2 | 2020-05-01 12:26:03 +0200 | [diff] [blame] | 60 | "%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n" |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 61 | " stuck=%d prof=%d", |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 62 | (thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1, |
Willy Tarreau | ff64d3b | 2020-05-01 11:28:49 +0200 | [diff] [blame] | 63 | ha_get_pthread_id(thr), |
Olivier Houchard | cfbb3e6 | 2019-05-29 19:22:43 +0200 | [diff] [blame] | 64 | thread_has_tasks(), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 65 | !!(global_tasks_mask & thr_bit), |
| 66 | !eb_is_empty(&task_per_thread[thr].timers), |
| 67 | !eb_is_empty(&task_per_thread[thr].rqueue), |
Willy Tarreau | a62917b | 2020-01-30 18:37:28 +0100 | [diff] [blame] | 68 | !(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) && |
| 69 | LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) && |
| 70 | LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) && |
| 71 | MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)), |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 72 | task_per_thread[thr].task_list_size, |
Willy Tarreau | eb163b2 | 2021-02-24 15:10:07 +0100 | [diff] [blame] | 73 | task_per_thread[thr].rq_total, |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 74 | stuck, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 75 | !!(task_profiling_mask & thr_bit)); |
| 76 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 77 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 78 | " harmless=%d wantrdv=%d", |
| 79 | !!(threads_harmless_mask & thr_bit), |
| 80 | !!(threads_want_rdv_mask & thr_bit)); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 82 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 9c8800a | 2019-05-20 20:52:20 +0200 | [diff] [blame] | 83 | 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] | 84 | |
| 85 | /* this is the end of what we can dump from outside the thread */ |
| 86 | |
| 87 | if (thr != tid) |
| 88 | return; |
| 89 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 90 | chunk_appendf(buf, " curr_task="); |
Willy Tarreau | d022e9c | 2019-09-24 08:25:15 +0200 | [diff] [blame] | 91 | ha_task_dump(buf, sched->current, " "); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 92 | |
| 93 | #ifdef USE_BACKTRACE |
| 94 | if (stuck) { |
| 95 | /* We only emit the backtrace for stuck threads in order not to |
| 96 | * waste precious output buffer space with non-interesting data. |
| 97 | */ |
| 98 | struct buffer bak; |
| 99 | void *callers[100]; |
| 100 | int j, nptrs; |
Willy Tarreau | 0c439d8 | 2020-07-05 20:26:04 +0200 | [diff] [blame] | 101 | const void *addr; |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 102 | int dump = 0; |
| 103 | |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 104 | nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 105 | |
| 106 | /* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO) |
| 107 | would produce similar output to the following: */ |
| 108 | |
| 109 | if (nptrs) |
Willy Tarreau | cdd8074 | 2020-03-04 07:38:23 +0100 | [diff] [blame] | 110 | chunk_appendf(buf, " call trace(%d):\n", nptrs); |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 111 | |
Willy Tarreau | a91b794 | 2020-03-04 07:39:32 +0100 | [diff] [blame] | 112 | for (j = 0; j < nptrs || dump < 2; j++) { |
| 113 | if (j == nptrs && !dump) { |
| 114 | /* we failed to spot the starting point of the |
| 115 | * dump, let's start over dumping everything we |
| 116 | * have. |
| 117 | */ |
| 118 | dump = 2; |
| 119 | j = 0; |
| 120 | } |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 121 | bak = *buf; |
| 122 | dump_addr_and_bytes(buf, " | ", callers[j], 8); |
| 123 | addr = resolve_sym_name(buf, ": ", callers[j]); |
| 124 | if (dump == 0) { |
| 125 | /* dump not started, will start *after* |
| 126 | * ha_thread_dump_all_to_trash and ha_panic |
| 127 | */ |
| 128 | if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) |
| 129 | dump = 1; |
| 130 | *buf = bak; |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | if (dump == 1) { |
| 135 | /* starting */ |
| 136 | if (addr == ha_thread_dump_all_to_trash || addr == ha_panic) { |
| 137 | *buf = bak; |
| 138 | continue; |
| 139 | } |
| 140 | dump = 2; |
| 141 | } |
| 142 | |
| 143 | if (dump == 2) { |
| 144 | /* dumping */ |
Willy Tarreau | 59153fe | 2020-06-24 10:17:29 +0200 | [diff] [blame] | 145 | if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) { |
Willy Tarreau | f5b4e06 | 2020-03-03 15:40:23 +0100 | [diff] [blame] | 146 | dump = 3; |
| 147 | *buf = bak; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | /* OK, line dumped */ |
| 152 | chunk_appendf(buf, "\n"); |
| 153 | } |
| 154 | } |
| 155 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 159 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 160 | * 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] | 161 | * with <pfx>. The buffer is only appended and the first output starts by the |
| 162 | * pointer itself. The caller is responsible for making sure the task is not |
| 163 | * going to vanish during the dump. |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 164 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 165 | 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] | 166 | { |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 167 | const struct stream *s = NULL; |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 168 | const struct appctx __maybe_unused *appctx = NULL; |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 169 | struct hlua __maybe_unused *hlua = NULL; |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 171 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 172 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
Willy Tarreau | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 176 | if (TASK_IS_TASKLET(task)) |
| 177 | chunk_appendf(buf, |
| 178 | "%p (tasklet) calls=%u\n", |
| 179 | task, |
| 180 | task->calls); |
| 181 | else |
| 182 | chunk_appendf(buf, |
| 183 | "%p (task) calls=%u last=%llu%s\n", |
| 184 | task, |
| 185 | task->calls, |
| 186 | task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0, |
| 187 | task->call_date ? " ns ago" : ""); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 188 | |
Willy Tarreau | 2e89b09 | 2020-03-03 17:13:02 +0100 | [diff] [blame] | 189 | chunk_appendf(buf, "%s fct=%p(", pfx, task->process); |
| 190 | resolve_sym_name(buf, NULL, task->process); |
| 191 | chunk_appendf(buf,") ctx=%p", task->context); |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 192 | |
Willy Tarreau | a512b02 | 2019-08-21 14:12:19 +0200 | [diff] [blame] | 193 | if (task->process == task_run_applet && (appctx = task->context)) |
| 194 | chunk_appendf(buf, "(%s)\n", appctx->applet->name); |
| 195 | else |
| 196 | chunk_appendf(buf, "\n"); |
| 197 | |
Willy Tarreau | 578ea8b | 2019-05-22 09:43:09 +0200 | [diff] [blame] | 198 | if (task->process == process_stream && task->context) |
| 199 | s = (struct stream *)task->context; |
| 200 | else if (task->process == task_run_applet && task->context) |
| 201 | s = si_strm(((struct appctx *)task->context)->owner); |
| 202 | else if (task->process == si_cs_io_cb && task->context) |
| 203 | s = si_strm((struct stream_interface *)task->context); |
| 204 | |
| 205 | if (s) |
| 206 | stream_dump(buf, s, pfx, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 207 | |
| 208 | #ifdef USE_LUA |
| 209 | hlua = NULL; |
| 210 | if (s && (hlua = s->hlua)) { |
| 211 | chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); |
| 212 | } |
| 213 | else if (task->process == hlua_process_task && (hlua = task->context)) { |
| 214 | chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx); |
| 215 | } |
| 216 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 217 | (appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) { |
| 218 | chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx); |
| 219 | } |
| 220 | else if (task->process == task_run_applet && (appctx = task->context) && |
| 221 | (appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) { |
| 222 | chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx); |
| 223 | } |
| 224 | |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 225 | if (hlua && hlua->T) { |
Christopher Faulet | 9fb7eb9 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 226 | chunk_appendf(buf, "stack traceback:\n "); |
| 227 | append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0); |
| 228 | b_putchr(buf, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 229 | } |
Christopher Faulet | 471425f | 2020-07-24 19:08:05 +0200 | [diff] [blame] | 230 | else |
| 231 | b_putchr(buf, '\n'); |
Willy Tarreau | 78a7cb6 | 2019-08-21 14:16:02 +0200 | [diff] [blame] | 232 | #endif |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 235 | /* This function dumps all profiling settings. It returns 0 if the output |
| 236 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 237 | */ |
| 238 | static int cli_io_handler_show_threads(struct appctx *appctx) |
| 239 | { |
| 240 | struct stream_interface *si = appctx->owner; |
| 241 | int thr; |
| 242 | |
| 243 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 244 | return 1; |
| 245 | |
| 246 | if (appctx->st0) |
| 247 | thr = appctx->st1; |
| 248 | else |
| 249 | thr = 0; |
| 250 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 251 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 252 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 253 | |
| 254 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 255 | /* failed, try again */ |
| 256 | si_rx_room_blk(si); |
| 257 | appctx->st1 = thr; |
| 258 | return 0; |
| 259 | } |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 260 | return 1; |
| 261 | } |
| 262 | |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 263 | /* dumps a state of all threads into the trash and on fd #2, then aborts. */ |
| 264 | void ha_panic() |
| 265 | { |
| 266 | chunk_reset(&trash); |
Willy Tarreau | a9f9fc9 | 2019-05-20 17:45:35 +0200 | [diff] [blame] | 267 | 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] | 268 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 269 | DISGUISE(write(2, trash.area, trash.data)); |
Willy Tarreau | 56131ca | 2019-05-20 13:48:29 +0200 | [diff] [blame] | 270 | for (;;) |
| 271 | abort(); |
| 272 | } |
| 273 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 274 | /* parse a "debug dev exit" command. It always returns 1, though it should never return. */ |
| 275 | static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private) |
| 276 | { |
| 277 | int code = atoi(args[3]); |
| 278 | |
| 279 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 280 | return 1; |
| 281 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 282 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 283 | exit(code); |
| 284 | return 1; |
| 285 | } |
| 286 | |
| 287 | /* parse a "debug dev close" command. It always returns 1. */ |
| 288 | static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private) |
| 289 | { |
| 290 | int fd; |
| 291 | |
| 292 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 293 | return 1; |
| 294 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 295 | if (!*args[3]) |
| 296 | return cli_err(appctx, "Missing file descriptor number.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 297 | |
| 298 | fd = atoi(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 299 | if (fd < 0 || fd >= global.maxsock) |
| 300 | return cli_err(appctx, "File descriptor out of range.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 301 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 302 | if (!fdtab[fd].owner) |
| 303 | return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 304 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 305 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 306 | fd_delete(fd); |
| 307 | return 1; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* parse a "debug dev delay" command. It always returns 1. */ |
| 311 | static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private) |
| 312 | { |
| 313 | int delay = atoi(args[3]); |
| 314 | |
| 315 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 316 | return 1; |
| 317 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 318 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 319 | usleep((long)delay * 1000); |
| 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | /* parse a "debug dev log" command. It always returns 1. */ |
| 324 | static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private) |
| 325 | { |
| 326 | int arg; |
| 327 | |
| 328 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 329 | return 1; |
| 330 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 331 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 332 | chunk_reset(&trash); |
| 333 | for (arg = 3; *args[arg]; arg++) { |
| 334 | if (arg > 3) |
| 335 | chunk_strcat(&trash, " "); |
| 336 | chunk_strcat(&trash, args[arg]); |
| 337 | } |
| 338 | |
| 339 | send_log(NULL, LOG_INFO, "%s\n", trash.area); |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* parse a "debug dev loop" command. It always returns 1. */ |
| 344 | static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private) |
| 345 | { |
| 346 | struct timeval deadline, curr; |
| 347 | int loop = atoi(args[3]); |
| 348 | |
| 349 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 350 | return 1; |
| 351 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 352 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 353 | gettimeofday(&curr, NULL); |
| 354 | tv_ms_add(&deadline, &curr, loop); |
| 355 | |
| 356 | while (tv_ms_cmp(&curr, &deadline) < 0) |
| 357 | gettimeofday(&curr, NULL); |
| 358 | |
| 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | /* parse a "debug dev panic" command. It always returns 1, though it should never return. */ |
| 363 | static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private) |
| 364 | { |
| 365 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 366 | return 1; |
| 367 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 368 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 369 | ha_panic(); |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | /* parse a "debug dev exec" command. It always returns 1. */ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 374 | #if defined(DEBUG_DEV) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 375 | static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private) |
| 376 | { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 377 | int pipefd[2]; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 378 | int arg; |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 379 | int pid; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 380 | |
| 381 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 382 | return 1; |
| 383 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 384 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 385 | chunk_reset(&trash); |
| 386 | for (arg = 3; *args[arg]; arg++) { |
| 387 | if (arg > 3) |
| 388 | chunk_strcat(&trash, " "); |
| 389 | chunk_strcat(&trash, args[arg]); |
| 390 | } |
| 391 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 392 | thread_isolate(); |
| 393 | if (pipe(pipefd) < 0) |
| 394 | goto fail_pipe; |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 395 | |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 396 | if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 397 | goto fail_fcntl; |
| 398 | |
| 399 | if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1) |
| 400 | goto fail_fcntl; |
| 401 | |
| 402 | pid = fork(); |
| 403 | |
| 404 | if (pid < 0) |
| 405 | goto fail_fork; |
| 406 | else if (pid == 0) { |
| 407 | /* child */ |
| 408 | char *cmd[4] = { "/bin/sh", "-c", 0, 0 }; |
| 409 | |
| 410 | close(0); |
| 411 | dup2(pipefd[1], 1); |
| 412 | dup2(pipefd[1], 2); |
| 413 | |
| 414 | cmd[2] = trash.area; |
| 415 | execvp(cmd[0], cmd); |
| 416 | printf("execvp() failed\n"); |
| 417 | exit(1); |
| 418 | } |
| 419 | |
| 420 | /* parent */ |
| 421 | thread_release(); |
| 422 | close(pipefd[1]); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 423 | chunk_reset(&trash); |
| 424 | while (1) { |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 425 | size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data); |
| 426 | if (ret <= 0) |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 427 | break; |
| 428 | trash.data += ret; |
| 429 | if (trash.data + 20 == trash.size) { |
| 430 | chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n"); |
| 431 | break; |
| 432 | } |
| 433 | } |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 434 | close(pipefd[0]); |
| 435 | waitpid(pid, NULL, WNOHANG); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 436 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 437 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 368bff4 | 2019-12-06 17:18:28 +0100 | [diff] [blame] | 438 | |
| 439 | fail_fork: |
| 440 | fail_fcntl: |
| 441 | close(pipefd[0]); |
| 442 | close(pipefd[1]); |
| 443 | fail_pipe: |
| 444 | thread_release(); |
| 445 | return cli_err(appctx, "Failed to execute command.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 446 | } |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 447 | #endif |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 448 | |
| 449 | /* parse a "debug dev hex" command. It always returns 1. */ |
| 450 | static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private) |
| 451 | { |
| 452 | unsigned long start, len; |
| 453 | |
| 454 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 455 | return 1; |
| 456 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 457 | if (!*args[3]) |
| 458 | return cli_err(appctx, "Missing memory address to dump from.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 459 | |
| 460 | start = strtoul(args[3], NULL, 0); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 461 | if (!start) |
| 462 | return cli_err(appctx, "Will not dump from NULL address.\n"); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 463 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 464 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 465 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 466 | /* by default, dump ~128 till next block of 16 */ |
| 467 | len = strtoul(args[4], NULL, 0); |
| 468 | if (!len) |
| 469 | len = ((start + 128) & -16) - start; |
| 470 | |
| 471 | chunk_reset(&trash); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 472 | dump_hex(&trash, " ", (const void *)start, len, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 473 | trash.area[trash.data] = 0; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 474 | return cli_msg(appctx, LOG_INFO, trash.area); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Willy Tarreau | bf5c748 | 2021-05-04 18:40:50 +0200 | [diff] [blame] | 477 | /* parse a "debug dev sym <addr>" command. It always returns 1. */ |
| 478 | static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private) |
| 479 | { |
| 480 | unsigned long addr; |
| 481 | |
| 482 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 483 | return 1; |
| 484 | |
| 485 | if (!*args[3]) |
| 486 | return cli_err(appctx, "Missing memory address to be resolved.\n"); |
| 487 | |
| 488 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 489 | |
| 490 | addr = strtoul(args[3], NULL, 0); |
| 491 | chunk_printf(&trash, "%#lx resolves to ", addr); |
| 492 | resolve_sym_name(&trash, NULL, (const void *)addr); |
| 493 | chunk_appendf(&trash, "\n"); |
| 494 | |
| 495 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 496 | } |
| 497 | |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 498 | /* parse a "debug dev tkill" command. It always returns 1. */ |
| 499 | static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private) |
| 500 | { |
| 501 | int thr = 0; |
| 502 | int sig = SIGABRT; |
| 503 | |
| 504 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 505 | return 1; |
| 506 | |
| 507 | if (*args[3]) |
| 508 | thr = atoi(args[3]); |
| 509 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 510 | if (thr < 0 || thr > global.nbthread) |
| 511 | 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] | 512 | |
| 513 | if (*args[4]) |
| 514 | sig = atoi(args[4]); |
| 515 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 516 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 517 | if (thr) |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 518 | ha_tkill(thr - 1, sig); |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 519 | else |
Willy Tarreau | 6bdf3e9 | 2019-05-20 14:25:05 +0200 | [diff] [blame] | 520 | raise(sig); |
| 521 | return 1; |
| 522 | } |
| 523 | |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 524 | /* parse a "debug dev write" command. It always returns 1. */ |
| 525 | static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private) |
| 526 | { |
| 527 | unsigned long len; |
| 528 | |
| 529 | if (!*args[3]) |
| 530 | return cli_err(appctx, "Missing output size.\n"); |
| 531 | |
| 532 | len = strtoul(args[3], NULL, 0); |
| 533 | if (len >= trash.size) |
| 534 | return cli_err(appctx, "Output too large, must be <tune.bufsize.\n"); |
| 535 | |
| 536 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
| 537 | |
| 538 | chunk_reset(&trash); |
| 539 | trash.data = len; |
| 540 | memset(trash.area, '.', trash.data); |
| 541 | trash.area[trash.data] = 0; |
| 542 | for (len = 64; len < trash.data; len += 64) |
| 543 | trash.area[len] = '\n'; |
| 544 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 545 | } |
| 546 | |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 547 | /* parse a "debug dev stream" command */ |
| 548 | /* |
| 549 | * debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \ |
| 550 | * [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \ |
| 551 | * [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \ |
| 552 | * [sif.s[=<state>]] [sib.s[=<state>]] |
| 553 | */ |
| 554 | static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private) |
| 555 | { |
| 556 | struct stream *s = si_strm(appctx->owner); |
| 557 | int arg; |
| 558 | void *ptr; |
| 559 | int size; |
| 560 | const char *word, *end; |
| 561 | struct ist name; |
| 562 | char *msg = NULL; |
| 563 | char *endarg; |
| 564 | unsigned long long old, new; |
| 565 | |
| 566 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 567 | return 1; |
| 568 | |
| 569 | ptr = NULL; size = 0; |
| 570 | |
| 571 | if (!*args[3]) { |
| 572 | return cli_err(appctx, |
| 573 | "Usage: debug dev stream { <obj> <op> <value> | wake }*\n" |
| 574 | " <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n" |
| 575 | " txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n" |
| 576 | " <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n" |
| 577 | " <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n" |
| 578 | " 'wake' wakes the stream asssigned to 'strm' (default: current)\n" |
| 579 | ); |
| 580 | } |
| 581 | |
Willy Tarreau | 9b01370 | 2019-10-24 18:18:02 +0200 | [diff] [blame] | 582 | _HA_ATOMIC_ADD(&debug_commands_issued, 1); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 583 | for (arg = 3; *args[arg]; arg++) { |
| 584 | old = 0; |
| 585 | end = word = args[arg]; |
| 586 | while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-') |
| 587 | end++; |
| 588 | name = ist2(word, end - word); |
| 589 | if (isteq(name, ist("strm"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 590 | ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 591 | } else if (isteq(name, ist("strm.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 592 | ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 593 | } else if (isteq(name, ist("txn.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 594 | 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] | 595 | } else if (isteq(name, ist("req.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 596 | 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] | 597 | } else if (isteq(name, ist("res.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 598 | 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] | 599 | } else if (isteq(name, ist("req.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 600 | 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] | 601 | } else if (isteq(name, ist("res.r"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 602 | 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] | 603 | } else if (isteq(name, ist("req.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 604 | 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] | 605 | } else if (isteq(name, ist("res.w"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 606 | ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 607 | } else if (isteq(name, ist("sif.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 608 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 609 | } else if (isteq(name, ist("sib.f"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 610 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 611 | } else if (isteq(name, ist("sif.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 612 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 613 | } else if (isteq(name, ist("sib.x"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 614 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 615 | } else if (isteq(name, ist("sif.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 616 | ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 617 | } else if (isteq(name, ist("sib.s"))) { |
Willy Tarreau | b2fee04 | 2019-10-25 10:06:55 +0200 | [diff] [blame] | 618 | ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state); |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 619 | } else if (isteq(name, ist("wake"))) { |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 620 | if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1)) |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 621 | task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG); |
| 622 | continue; |
| 623 | } else |
| 624 | return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word)); |
| 625 | |
| 626 | /* read previous value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 627 | 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] | 628 | if (size == 8) |
| 629 | old = read_u64(ptr); |
| 630 | else if (size == 4) |
| 631 | old = read_u32(ptr); |
| 632 | else if (size == 2) |
| 633 | old = read_u16(ptr); |
| 634 | else |
| 635 | old = *(const uint8_t *)ptr; |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 636 | } else { |
| 637 | memprintf(&msg, |
| 638 | "%sSkipping inaccessible pointer %p for field '%.*s'.\n", |
| 639 | msg ? msg : "", ptr, (int)(end - word), word); |
| 640 | continue; |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* parse the new value . */ |
| 644 | new = strtoll(end + 1, &endarg, 0); |
| 645 | if (end[1] && *endarg) { |
| 646 | if (strcmp(end + 1, "now") == 0) |
| 647 | new = now_ms; |
| 648 | else { |
| 649 | memprintf(&msg, |
| 650 | "%sIgnoring unparsable value '%s' for field '%.*s'.\n", |
| 651 | msg ? msg : "", end + 1, (int)(end - word), word); |
| 652 | continue; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | switch (*end) { |
| 657 | case '\0': /* show */ |
| 658 | memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old); |
| 659 | new = old; // do not change the value |
| 660 | break; |
| 661 | |
| 662 | case '=': /* set */ |
| 663 | break; |
| 664 | |
| 665 | case '^': /* XOR */ |
| 666 | new = old ^ new; |
| 667 | break; |
| 668 | |
| 669 | case '+': /* OR */ |
| 670 | new = old | new; |
| 671 | break; |
| 672 | |
| 673 | case '-': /* AND NOT */ |
| 674 | new = old & ~new; |
| 675 | break; |
| 676 | |
| 677 | default: |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | /* write the new value */ |
Willy Tarreau | 2b5520d | 2019-10-24 18:28:23 +0200 | [diff] [blame] | 682 | if (new != old) { |
Willy Tarreau | 68680bb | 2019-10-23 17:23:25 +0200 | [diff] [blame] | 683 | if (size == 8) |
| 684 | write_u64(ptr, new); |
| 685 | else if (size == 4) |
| 686 | write_u32(ptr, new); |
| 687 | else if (size == 2) |
| 688 | write_u16(ptr, new); |
| 689 | else |
| 690 | *(uint8_t *)ptr = new; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if (msg && *msg) |
| 695 | return cli_dynmsg(appctx, LOG_INFO, msg); |
| 696 | return 1; |
| 697 | } |
| 698 | |
Willy Tarreau | a6026a0 | 2020-07-02 09:14:48 +0200 | [diff] [blame] | 699 | #if defined(DEBUG_MEM_STATS) |
| 700 | /* CLI parser for the "debug dev memstats" command */ |
| 701 | static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private) |
| 702 | { |
| 703 | extern __attribute__((__weak__)) struct mem_stats __start_mem_stats; |
| 704 | extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats; |
| 705 | |
| 706 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 707 | return 1; |
| 708 | |
| 709 | if (strcmp(args[3], "reset") == 0) { |
| 710 | struct mem_stats *ptr; |
| 711 | |
| 712 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 713 | return 1; |
| 714 | |
| 715 | for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) { |
| 716 | _HA_ATOMIC_STORE(&ptr->calls, 0); |
| 717 | _HA_ATOMIC_STORE(&ptr->size, 0); |
| 718 | } |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | if (strcmp(args[3], "all") == 0) |
| 723 | appctx->ctx.cli.i0 = 1; |
| 724 | |
| 725 | /* otherwise proceed with the dump from p0 to p1 */ |
| 726 | appctx->ctx.cli.p0 = &__start_mem_stats; |
| 727 | appctx->ctx.cli.p1 = &__stop_mem_stats; |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats |
| 732 | * structs referenced by pointers located between p0 and p1. Dumps all entries |
| 733 | * if i0 > 0, otherwise only non-zero calls. |
| 734 | */ |
| 735 | static int debug_iohandler_memstats(struct appctx *appctx) |
| 736 | { |
| 737 | struct stream_interface *si = appctx->owner; |
| 738 | struct mem_stats *ptr = appctx->ctx.cli.p0; |
| 739 | int ret = 1; |
| 740 | |
| 741 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 742 | goto end; |
| 743 | |
| 744 | chunk_reset(&trash); |
| 745 | |
| 746 | /* we have two inner loops here, one for the proxy, the other one for |
| 747 | * the buffer. |
| 748 | */ |
| 749 | for (ptr = appctx->ctx.cli.p0; ptr != appctx->ctx.cli.p1; ptr++) { |
| 750 | const char *type; |
| 751 | const char *name; |
| 752 | const char *p; |
| 753 | |
| 754 | if (!ptr->size && !ptr->calls && !appctx->ctx.cli.i0) |
| 755 | continue; |
| 756 | |
| 757 | /* basename only */ |
| 758 | for (p = name = ptr->file; *p; p++) { |
| 759 | if (*p == '/') |
| 760 | name = p + 1; |
| 761 | } |
| 762 | |
| 763 | switch (ptr->type) { |
| 764 | case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break; |
| 765 | case MEM_STATS_TYPE_FREE: type = "FREE"; break; |
| 766 | case MEM_STATS_TYPE_MALLOC: type = "MALLOC"; break; |
| 767 | case MEM_STATS_TYPE_REALLOC: type = "REALLOC"; break; |
| 768 | case MEM_STATS_TYPE_STRDUP: type = "STRDUP"; break; |
| 769 | default: type = "UNSET"; break; |
| 770 | } |
| 771 | |
| 772 | //chunk_printf(&trash, |
| 773 | // "%20s:%-5d %7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 774 | // name, ptr->line, type, |
| 775 | // (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 776 | // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 777 | |
| 778 | chunk_printf(&trash, "%s:%d", name, ptr->line); |
| 779 | while (trash.data < 25) |
| 780 | trash.area[trash.data++] = ' '; |
| 781 | chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu\n", |
| 782 | type, |
| 783 | (unsigned long)ptr->size, (unsigned long)ptr->calls, |
| 784 | (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); |
| 785 | |
| 786 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 787 | si_rx_room_blk(si); |
| 788 | appctx->ctx.cli.p0 = ptr; |
| 789 | ret = 0; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | end: |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | #endif |
| 799 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 800 | #ifndef USE_THREAD_DUMP |
| 801 | |
| 802 | /* This function dumps all threads' state to the trash. This version is the |
| 803 | * most basic one, which doesn't inspect other threads. |
| 804 | */ |
| 805 | void ha_thread_dump_all_to_trash() |
| 806 | { |
| 807 | unsigned int thr; |
| 808 | |
| 809 | for (thr = 0; thr < global.nbthread; thr++) |
| 810 | ha_thread_dump(&trash, thr, tid); |
| 811 | } |
| 812 | |
| 813 | #else /* below USE_THREAD_DUMP is set */ |
| 814 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 815 | /* ID of the thread requesting the dump */ |
| 816 | static unsigned int thread_dump_tid; |
| 817 | |
| 818 | /* points to the buffer where the dump functions should write. It must |
| 819 | * have already been initialized by the requester. Nothing is done if |
| 820 | * it's NULL. |
| 821 | */ |
| 822 | struct buffer *thread_dump_buffer = NULL; |
| 823 | |
| 824 | void ha_thread_dump_all_to_trash() |
| 825 | { |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 826 | unsigned long old; |
| 827 | |
| 828 | while (1) { |
| 829 | old = 0; |
| 830 | if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask)) |
| 831 | break; |
| 832 | ha_thread_relax(); |
| 833 | } |
| 834 | |
| 835 | thread_dump_buffer = &trash; |
| 836 | thread_dump_tid = tid; |
Willy Tarreau | fade80d | 2019-05-22 08:46:59 +0200 | [diff] [blame] | 837 | ha_tkillall(DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* handles DEBUGSIG to dump the state of the thread it's working on */ |
| 841 | void debug_handler(int sig, siginfo_t *si, void *arg) |
| 842 | { |
Willy Tarreau | 82aafc4 | 2020-03-03 08:31:34 +0100 | [diff] [blame] | 843 | /* first, let's check it's really for us and that we didn't just get |
| 844 | * a spurious DEBUGSIG. |
| 845 | */ |
| 846 | if (!(threads_to_dump & tid_bit)) |
| 847 | return; |
| 848 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 849 | /* There are 4 phases in the dump process: |
| 850 | * 1- wait for our turn, i.e. when all lower bits are gone. |
| 851 | * 2- perform the action if our bit is set |
| 852 | * 3- remove our bit to let the next one go, unless we're |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 853 | * the last one and have to put them all as a signal |
| 854 | * 4- wait out bit to re-appear, then clear it and quit. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 855 | */ |
| 856 | |
| 857 | /* wait for all previous threads to finish first */ |
| 858 | while (threads_to_dump & (tid_bit - 1)) |
| 859 | ha_thread_relax(); |
| 860 | |
| 861 | /* dump if needed */ |
| 862 | if (threads_to_dump & tid_bit) { |
| 863 | if (thread_dump_buffer) |
| 864 | ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid); |
| 865 | if ((threads_to_dump & all_threads_mask) == tid_bit) { |
| 866 | /* last one */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 867 | HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 868 | thread_dump_buffer = NULL; |
| 869 | } |
| 870 | else |
| 871 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
| 872 | } |
| 873 | |
| 874 | /* now wait for all others to finish dumping. The last one will set all |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 875 | * bits again to broadcast the leaving condition so we'll see ourselves |
| 876 | * present again. This way the threads_to_dump variable never passes to |
| 877 | * zero until all visitors have stopped waiting. |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 878 | */ |
Willy Tarreau | c077362 | 2019-07-31 19:15:45 +0200 | [diff] [blame] | 879 | while (!(threads_to_dump & tid_bit)) |
| 880 | ha_thread_relax(); |
| 881 | HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); |
Willy Tarreau | e6a02fa | 2019-05-22 07:06:44 +0200 | [diff] [blame] | 882 | |
| 883 | /* mark the current thread as stuck to detect it upon next invocation |
| 884 | * if it didn't move. |
| 885 | */ |
| 886 | if (!((threads_harmless_mask|sleeping_thread_mask) & tid_bit)) |
| 887 | ti->flags |= TI_FL_STUCK; |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static int init_debug_per_thread() |
| 891 | { |
| 892 | sigset_t set; |
| 893 | |
| 894 | /* unblock the DEBUGSIG signal we intend to use */ |
| 895 | sigemptyset(&set); |
| 896 | sigaddset(&set, DEBUGSIG); |
| 897 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | static int init_debug() |
| 902 | { |
| 903 | struct sigaction sa; |
| 904 | |
Willy Tarreau | 0214b45 | 2020-03-04 06:01:40 +0100 | [diff] [blame] | 905 | #ifdef USE_BACKTRACE |
| 906 | /* calling backtrace() will access libgcc at runtime. We don't want to |
| 907 | * do it after the chroot, so let's perform a first call to have it |
| 908 | * ready in memory for later use. |
| 909 | */ |
| 910 | void *callers[1]; |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 911 | my_backtrace(callers, sizeof(callers)/sizeof(*callers)); |
Willy Tarreau | 0214b45 | 2020-03-04 06:01:40 +0100 | [diff] [blame] | 912 | #endif |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 913 | sa.sa_handler = NULL; |
| 914 | sa.sa_sigaction = debug_handler; |
| 915 | sigemptyset(&sa.sa_mask); |
| 916 | sa.sa_flags = SA_SIGINFO; |
| 917 | sigaction(DEBUGSIG, &sa, NULL); |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | REGISTER_POST_CHECK(init_debug); |
| 922 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 923 | |
| 924 | #endif /* USE_THREAD_DUMP */ |
| 925 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 926 | /* register cli keywords */ |
| 927 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 928 | {{ "debug", "dev", "close", NULL }, "debug dev close <fd> : close this file descriptor", debug_parse_cli_close, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 929 | {{ "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] | 930 | #if defined(DEBUG_DEV) |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 931 | {{ "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] | 932 | #endif |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 933 | {{ "debug", "dev", "exit", NULL }, "debug dev exit [code] : immediately exit the process", debug_parse_cli_exit, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 934 | {{ "debug", "dev", "hex", NULL }, "debug dev hex <addr> [len]: dump a memory area", debug_parse_cli_hex, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 935 | {{ "debug", "dev", "log", NULL }, "debug dev log [msg] ... : send this msg to global logs", debug_parse_cli_log, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 936 | {{ "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] | 937 | #if defined(DEBUG_MEM_STATS) |
| 938 | {{ "debug", "dev", "memstats", NULL }, "debug dev memstats [reset|all] : dump/reset memory statistics", debug_parse_cli_memstats, debug_iohandler_memstats, NULL, NULL, ACCESS_EXPERT }, |
| 939 | #endif |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 940 | {{ "debug", "dev", "panic", NULL }, "debug dev panic : immediately trigger a panic", debug_parse_cli_panic, NULL, NULL, NULL, ACCESS_EXPERT }, |
| 941 | {{ "debug", "dev", "stream",NULL }, "debug dev stream ... : show/manipulate stream flags", debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | bf5c748 | 2021-05-04 18:40:50 +0200 | [diff] [blame] | 942 | {{ "debug", "dev", "sym", NULL }, "debug dev sym <addr> : resolve symbol address", debug_parse_cli_sym, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 943 | {{ "debug", "dev", "tkill", NULL }, "debug dev tkill [thr] [sig] : send signal to thread", debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | 6cbe62b | 2020-03-05 17:16:24 +0100 | [diff] [blame] | 944 | {{ "debug", "dev", "write", NULL }, "debug dev write [size] : write that many bytes", debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT }, |
Willy Tarreau | b24ab22 | 2019-10-24 18:03:39 +0200 | [diff] [blame] | 945 | {{ "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] | 946 | {{},} |
| 947 | }}; |
| 948 | |
| 949 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |