Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Runtime tracing API |
| 3 | * |
| 4 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Willy Tarreau | eb6f701 | 2020-05-27 16:21:26 +0200 | [diff] [blame] | 21 | #include <import/ist.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
| 23 | #include <haproxy/buf.h> |
Willy Tarreau | 9fd0542 | 2022-11-16 17:29:12 +0100 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 26 | #include <haproxy/errors.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 27 | #include <haproxy/istbuf.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 28 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 29 | #include <haproxy/log.h> |
Willy Tarreau | ba2f73d | 2020-06-03 20:02:28 +0200 | [diff] [blame] | 30 | #include <haproxy/sink.h> |
Willy Tarreau | c6d61d7 | 2020-06-04 19:02:42 +0200 | [diff] [blame] | 31 | #include <haproxy/trace.h> |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 32 | |
| 33 | struct list trace_sources = LIST_HEAD_INIT(trace_sources); |
Willy Tarreau | 88ebd40 | 2019-08-19 15:55:34 +0200 | [diff] [blame] | 34 | THREAD_LOCAL struct buffer trace_buf = { }; |
| 35 | |
| 36 | /* allocates the trace buffers. Returns 0 in case of failure. It is safe to |
| 37 | * call to call this function multiple times if the size changes. |
| 38 | */ |
| 39 | static int alloc_trace_buffers_per_thread() |
| 40 | { |
| 41 | chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize); |
Willy Tarreau | 2e7fdfc | 2019-11-25 19:43:31 +0100 | [diff] [blame] | 42 | return !!trace_buf.area; |
Willy Tarreau | 88ebd40 | 2019-08-19 15:55:34 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void free_trace_buffers_per_thread() |
| 46 | { |
| 47 | chunk_destroy(&trace_buf); |
| 48 | } |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 88ebd40 | 2019-08-19 15:55:34 +0200 | [diff] [blame] | 50 | REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread); |
| 51 | REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 53 | /* pick the lowest non-null argument with a non-null arg_def mask */ |
| 54 | static inline const void *trace_pick_arg(uint32_t arg_def, const void *a1, const void *a2, const void *a3, const void *a4) |
| 55 | { |
| 56 | if (arg_def & 0x0000FFFF) { |
| 57 | if ((arg_def & 0x000000FF) && a1) |
| 58 | return a1; |
| 59 | if ((arg_def & 0x0000FF00) && a2) |
| 60 | return a2; |
| 61 | } |
| 62 | |
| 63 | if (arg_def & 0xFFFF0000) { |
| 64 | if ((arg_def & 0x00FF0000) && a3) |
| 65 | return a3; |
| 66 | if ((arg_def & 0xFF000000) && a4) |
| 67 | return a4; |
| 68 | } |
| 69 | |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 73 | /* Reports whether the trace is enabled for the specified arguments, needs to enable |
| 74 | * or disable tracking. It gets the same API as __trace() except for <cb> and <msg> |
| 75 | * which are not used and were dropped, and plockptr which is an optional pointer to |
| 76 | * the lockptr to be updated (or NULL) for tracking. The function returns: |
| 77 | * 0 if the trace is not enabled for the module or these values |
| 78 | * <0 if the trace matches some locking criteria but don't have the proper level. |
| 79 | * In this case the interested caller might have to consider disabling tracking. |
| 80 | * >0 if the trace is enabled for the given criteria. |
| 81 | * In all cases, <plockptr> will only be set if non-null and if a locking criterion |
| 82 | * matched. It will be up to the caller to enable tracking if desired. A casual |
| 83 | * tester not interested in adjusting tracking (i.e. calling the function before |
| 84 | * deciding so prepare a buffer to be dumped) will only need to pass 0 for plockptr |
| 85 | * and check if the result is >0. |
| 86 | */ |
| 87 | int __trace_enabled(enum trace_level level, uint64_t mask, struct trace_source *src, |
| 88 | const struct ist where, const char *func, |
| 89 | const void *a1, const void *a2, const void *a3, const void *a4, |
| 90 | const void **plockptr) |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 91 | { |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 92 | const struct listener *li = NULL; |
| 93 | const struct proxy *fe = NULL; |
| 94 | const struct proxy *be = NULL; |
| 95 | const struct server *srv = NULL; |
| 96 | const struct session *sess = NULL; |
| 97 | const struct stream *strm = NULL; |
| 98 | const struct connection *conn = NULL; |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 99 | const struct check *check = NULL; |
Amaury Denoyelle | baea964 | 2021-12-21 11:26:02 +0100 | [diff] [blame] | 100 | const struct quic_conn *qc = NULL; |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 101 | const void *lockon_ptr = NULL; |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 103 | if (likely(src->state == TRACE_STATE_STOPPED)) |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 104 | return 0; |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 105 | |
| 106 | /* check that at least one action is interested by this event */ |
| 107 | if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0) |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 108 | return 0; |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 109 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 110 | /* retrieve available information from the caller's arguments */ |
| 111 | if (src->arg_def & TRC_ARGS_CONN) |
| 112 | conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4); |
| 113 | |
| 114 | if (src->arg_def & TRC_ARGS_SESS) |
| 115 | sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4); |
| 116 | |
| 117 | if (src->arg_def & TRC_ARGS_STRM) |
| 118 | strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4); |
| 119 | |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 120 | if (src->arg_def & TRC_ARGS_CHK) |
| 121 | check = trace_pick_arg(src->arg_def & TRC_ARGS_CHK, a1, a2, a3, a4); |
| 122 | |
Amaury Denoyelle | baea964 | 2021-12-21 11:26:02 +0100 | [diff] [blame] | 123 | if (src->arg_def & TRC_ARGS_QCON) |
| 124 | qc = trace_pick_arg(src->arg_def & TRC_ARGS_QCON, a1, a2, a3, a4); |
Amaury Denoyelle | baea964 | 2021-12-21 11:26:02 +0100 | [diff] [blame] | 125 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 126 | if (!sess && strm) |
| 127 | sess = strm->sess; |
Willy Tarreau | 6e805da | 2022-03-18 17:37:20 +0100 | [diff] [blame] | 128 | else if (!sess && conn && LIST_INLIST(&conn->session_list)) |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 129 | sess = conn->owner; |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 130 | else if (!sess && check) |
| 131 | sess = check->sess; |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 132 | |
| 133 | if (sess) { |
| 134 | fe = sess->fe; |
| 135 | li = sess->listener; |
| 136 | } |
| 137 | |
| 138 | if (!li && conn) |
| 139 | li = objt_listener(conn->target); |
| 140 | |
| 141 | if (li && !fe) |
| 142 | fe = li->bind_conf->frontend; |
| 143 | |
| 144 | if (strm) { |
| 145 | be = strm->be; |
| 146 | srv = strm->srv_conn; |
| 147 | } |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 148 | if (check) { |
| 149 | srv = check->server; |
Christopher Faulet | e3b2574 | 2022-06-08 09:06:15 +0200 | [diff] [blame] | 150 | be = (srv ? srv->proxy : NULL); |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 151 | } |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 152 | |
| 153 | if (!srv && conn) |
| 154 | srv = objt_server(conn->target); |
| 155 | |
| 156 | if (srv && !be) |
| 157 | be = srv->proxy; |
| 158 | |
| 159 | if (!be && conn) |
| 160 | be = objt_proxy(conn->target); |
| 161 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 162 | /* TODO: add handling of filters here, return if no match (not even update states) */ |
| 163 | |
| 164 | /* check if we need to start the trace now */ |
| 165 | if (src->state == TRACE_STATE_WAITING) { |
| 166 | if ((src->start_events & mask) == 0) |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 167 | return 0; |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 168 | |
| 169 | /* TODO: add update of lockon+lockon_ptr here */ |
| 170 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING); |
| 171 | } |
| 172 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 173 | /* we may want to lock on a particular object */ |
| 174 | if (src->lockon != TRACE_LOCKON_NOTHING) { |
| 175 | switch (src->lockon) { |
| 176 | case TRACE_LOCKON_BACKEND: lockon_ptr = be; break; |
| 177 | case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break; |
| 178 | case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break; |
| 179 | case TRACE_LOCKON_LISTENER: lockon_ptr = li; break; |
| 180 | case TRACE_LOCKON_SERVER: lockon_ptr = srv; break; |
| 181 | case TRACE_LOCKON_SESSION: lockon_ptr = sess; break; |
| 182 | case TRACE_LOCKON_STREAM: lockon_ptr = strm; break; |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 183 | case TRACE_LOCKON_CHECK: lockon_ptr = check; break; |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 184 | case TRACE_LOCKON_THREAD: lockon_ptr = ti; break; |
Amaury Denoyelle | baea964 | 2021-12-21 11:26:02 +0100 | [diff] [blame] | 185 | case TRACE_LOCKON_QCON: lockon_ptr = qc; break; |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 186 | case TRACE_LOCKON_ARG1: lockon_ptr = a1; break; |
| 187 | case TRACE_LOCKON_ARG2: lockon_ptr = a2; break; |
| 188 | case TRACE_LOCKON_ARG3: lockon_ptr = a3; break; |
| 189 | case TRACE_LOCKON_ARG4: lockon_ptr = a4; break; |
| 190 | default: break; // silence stupid gcc -Wswitch |
| 191 | } |
| 192 | |
| 193 | if (src->lockon_ptr && src->lockon_ptr != lockon_ptr) |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 194 | return 0; |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 195 | |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 196 | if (*plockptr && !src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING) |
| 197 | *plockptr = lockon_ptr; |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 200 | /* here the trace is running and is tracking a desired item */ |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 201 | if ((src->report_events & mask) == 0 || level > src->level) { |
| 202 | /* tracking did match, and might have to be disabled */ |
| 203 | return -1; |
| 204 | } |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 205 | |
Willy Tarreau | 8f9a970 | 2023-01-24 17:48:53 +0100 | [diff] [blame] | 206 | /* OK trace still enabled */ |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | /* write a message for the given trace source */ |
| 211 | void __trace(enum trace_level level, uint64_t mask, struct trace_source *src, |
| 212 | const struct ist where, const char *func, |
| 213 | const void *a1, const void *a2, const void *a3, const void *a4, |
| 214 | void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 215 | const struct ist where, const struct ist func, |
| 216 | const void *a1, const void *a2, const void *a3, const void *a4), |
| 217 | const struct ist msg) |
| 218 | { |
| 219 | const void *lockon_ptr; |
| 220 | struct ist ist_func = ist(func); |
| 221 | char tnum[4]; |
| 222 | struct ist line[12]; |
| 223 | int words = 0; |
| 224 | int ret; |
| 225 | |
| 226 | lockon_ptr = NULL; |
| 227 | ret = __trace_enabled(level, mask, src, where, func, a1, a2, a3, a4, &lockon_ptr); |
| 228 | if (lockon_ptr) |
| 229 | HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr); |
| 230 | |
| 231 | if (ret <= 0) { |
| 232 | if (ret < 0) // may have to disable tracking |
| 233 | goto end; |
| 234 | return; |
| 235 | } |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 237 | /* log the logging location truncated to 10 chars from the right so that |
| 238 | * the line number and the end of the file name are there. |
| 239 | */ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 240 | line[words++] = ist("["); |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 241 | tnum[0] = '0' + tid / 10; |
| 242 | tnum[1] = '0' + tid % 10; |
| 243 | tnum[2] = '|'; |
| 244 | tnum[3] = 0; |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 245 | line[words++] = ist(tnum); |
| 246 | line[words++] = src->name; |
| 247 | line[words++] = ist("|"); |
Frédéric Lécaille | fd18314 | 2020-12-02 16:51:00 +0100 | [diff] [blame] | 248 | line[words++] = ist2("012345" + level, 1); // "0" to "5" |
Willy Tarreau | 052ad36 | 2019-08-29 09:08:36 +0200 | [diff] [blame] | 249 | line[words++] = ist("|"); |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 250 | line[words] = where; |
| 251 | if (line[words].len > 13) { |
| 252 | line[words].ptr += (line[words].len - 13); |
| 253 | line[words].len = 13; |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 254 | } |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 255 | words++; |
| 256 | line[words++] = ist("] "); |
| 257 | |
Tim Duesterhus | 7b5777d | 2021-03-02 18:57:28 +0100 | [diff] [blame] | 258 | if (isttest(ist_func)) { |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 259 | line[words++] = ist_func; |
| 260 | line[words++] = ist("(): "); |
| 261 | } |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 3da0026 | 2019-08-28 07:03:58 +0200 | [diff] [blame] | 263 | if (!cb) |
| 264 | cb = src->default_cb; |
| 265 | |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 266 | if (cb && src->verbosity) { |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 267 | /* decode function passed, we want to pre-fill the |
| 268 | * buffer with the message and let the decode function |
| 269 | * do its job, possibly even overwriting it. |
| 270 | */ |
| 271 | b_reset(&trace_buf); |
| 272 | b_istput(&trace_buf, msg); |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 273 | cb(level, mask, src, where, ist_func, a1, a2, a3, a4); |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 274 | line[words] = ist2(trace_buf.area, trace_buf.data); |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 275 | words++; |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 276 | } |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 277 | else { |
| 278 | /* Note that here we could decide to print some args whose type |
| 279 | * is known, when verbosity is above the quiet level, and even |
| 280 | * to print the name and values of those which are declared for |
| 281 | * lock-on. |
| 282 | */ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 283 | line[words++] = msg; |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 284 | } |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 285 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 286 | if (src->sink) |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 287 | sink_write(src->sink, line, words, 0, 0, NULL); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 288 | |
| 289 | end: |
| 290 | /* check if we need to stop the trace now */ |
| 291 | if ((src->stop_events & mask) != 0) { |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 292 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 293 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED); |
| 294 | } |
| 295 | else if ((src->pause_events & mask) != 0) { |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 296 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 297 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING); |
| 298 | } |
| 299 | } |
| 300 | |
Willy Tarreau | 4b36d5e | 2023-01-24 18:03:07 +0100 | [diff] [blame] | 301 | /* this callback may be used when no output modification is desired */ |
| 302 | void trace_no_cb(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 303 | const struct ist where, const struct ist func, |
| 304 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 305 | { |
| 306 | /* do nothing */ |
| 307 | } |
| 308 | |
Willy Tarreau | 4de2cda | 2022-03-02 14:53:00 +0100 | [diff] [blame] | 309 | /* registers trace source <source>. Modifies the list element! |
| 310 | * The {start,pause,stop,report} events are not changed so the source may |
| 311 | * preset them. |
| 312 | */ |
| 313 | void trace_register_source(struct trace_source *source) |
| 314 | { |
| 315 | source->lockon = TRACE_LOCKON_NOTHING; |
| 316 | source->level = TRACE_LEVEL_USER; |
| 317 | source->verbosity = 1; |
| 318 | source->sink = NULL; |
| 319 | source->state = TRACE_STATE_STOPPED; |
| 320 | source->lockon_ptr = NULL; |
| 321 | LIST_APPEND(&trace_sources, &source->source_link); |
| 322 | } |
| 323 | |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 324 | struct trace_source *trace_find_source(const char *name) |
| 325 | { |
| 326 | struct trace_source *src; |
| 327 | const struct ist iname = ist(name); |
| 328 | |
| 329 | list_for_each_entry(src, &trace_sources, source_link) |
| 330 | if (isteq(src->name, iname)) |
| 331 | return src; |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name) |
| 336 | { |
| 337 | for (; ev && ev->mask; ev++) |
| 338 | if (strcmp(ev->name, name) == 0) |
| 339 | return ev; |
| 340 | return NULL; |
| 341 | } |
| 342 | |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 343 | /* Parse a "trace" statement. Returns a severity as a LOG_* level and a status |
| 344 | * message that may be delivered to the user, in <msg>. The message will be |
| 345 | * nulled first and msg must be a valid pointer. A null status message output |
| 346 | * indicates no error. Be careful not to use the return value as a boolean, as |
| 347 | * LOG_* values are not ordered as one could imagine (LOG_EMERG is zero). The |
| 348 | * function may/will use the trash buffer as the storage for the response |
| 349 | * message so that the caller never needs to release anything. |
| 350 | */ |
| 351 | static int trace_parse_statement(char **args, const char **msg) |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 352 | { |
| 353 | struct trace_source *src; |
| 354 | uint64_t *ev_ptr = NULL; |
| 355 | |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 356 | /* no error by default */ |
| 357 | *msg = NULL; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 358 | |
| 359 | if (!*args[1]) { |
| 360 | /* no arg => report the list of supported sources as a warning */ |
| 361 | chunk_printf(&trash, |
| 362 | "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n" |
| 363 | " [.] 0 : not a source, will immediately stop all traces\n" |
| 364 | ); |
| 365 | |
| 366 | list_for_each_entry(src, &trace_sources, source_link) |
| 367 | chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc); |
| 368 | |
| 369 | trash.area[trash.data] = 0; |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 370 | *msg = trash.area; |
| 371 | return LOG_WARNING; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (strcmp(args[1], "0") == 0) { |
| 375 | /* emergency stop of all traces */ |
| 376 | list_for_each_entry(src, &trace_sources, source_link) |
| 377 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED); |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 378 | *msg = "All traces now stopped"; |
| 379 | return LOG_NOTICE; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | src = trace_find_source(args[1]); |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 383 | if (!src) { |
| 384 | *msg = "No such trace source"; |
| 385 | return LOG_ERR; |
| 386 | } |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 387 | |
| 388 | if (!*args[2]) { |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 389 | *msg = "Supported commands:\n" |
| 390 | " event : list/enable/disable source-specific event reporting\n" |
| 391 | //" filter : list/enable/disable generic filters\n" |
| 392 | " level : list/set trace reporting level\n" |
| 393 | " lock : automatic lock on thread/connection/stream/...\n" |
| 394 | " pause : pause and automatically restart after a specific event\n" |
| 395 | " sink : list/set event sinks\n" |
| 396 | " start : start immediately or after a specific event\n" |
| 397 | " stop : stop immediately or after a specific event\n" |
| 398 | " verbosity : list/set trace output verbosity\n"; |
| 399 | return LOG_WARNING; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 400 | } |
| 401 | else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) || |
| 402 | (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) || |
| 403 | (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) || |
| 404 | (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) { |
| 405 | const struct trace_event *ev; |
| 406 | const char *name = args[3]; |
| 407 | int neg = 0; |
| 408 | int i; |
| 409 | |
| 410 | /* skip prefix '!', '-', '+' and remind negation */ |
| 411 | while (*name) { |
| 412 | if (*name == '!' || *name == '-') |
| 413 | neg = 1; |
| 414 | else if (*name == '+') |
| 415 | neg = 0; |
| 416 | else |
| 417 | break; |
| 418 | name++; |
| 419 | } |
| 420 | |
| 421 | if (!*name) { |
| 422 | chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr); |
| 423 | if (ev_ptr != &src->report_events) |
Willy Tarreau | 99282dd | 2019-08-30 07:37:32 +0200 | [diff] [blame] | 424 | chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n"); |
| 425 | chunk_appendf(&trash, " - none : disable all event types\n"); |
| 426 | chunk_appendf(&trash, " - any : enable all event types\n"); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 427 | for (i = 0; src->known_events && src->known_events[i].mask; i++) { |
Willy Tarreau | 99282dd | 2019-08-30 07:37:32 +0200 | [diff] [blame] | 428 | chunk_appendf(&trash, " %c %-12s : %s\n", |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 429 | trace_event_char(*ev_ptr, src->known_events[i].mask), |
| 430 | src->known_events[i].name, src->known_events[i].desc); |
| 431 | } |
| 432 | trash.area[trash.data] = 0; |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 433 | *msg = trash.area; |
| 434 | return LOG_WARNING; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) { |
| 438 | HA_ATOMIC_STORE(ev_ptr, 0); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 439 | if (ev_ptr == &src->pause_events) { |
| 440 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 441 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 442 | } |
| 443 | else if (ev_ptr == &src->start_events) { |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 444 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 445 | } |
| 446 | else if (ev_ptr == &src->stop_events) { |
| 447 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 448 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 449 | } |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 450 | *msg = NULL; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | if (strcmp(name, "none") == 0) |
| 455 | HA_ATOMIC_STORE(ev_ptr, 0); |
| 456 | else if (strcmp(name, "any") == 0) |
| 457 | HA_ATOMIC_STORE(ev_ptr, ~0); |
| 458 | else { |
| 459 | ev = trace_find_event(src->known_events, name); |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 460 | if (!ev) { |
| 461 | *msg = "No such trace event"; |
| 462 | return LOG_ERR; |
| 463 | } |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 464 | |
| 465 | if (!neg) |
| 466 | HA_ATOMIC_OR(ev_ptr, ev->mask); |
| 467 | else |
| 468 | HA_ATOMIC_AND(ev_ptr, ~ev->mask); |
| 469 | } |
| 470 | } |
| 471 | else if (strcmp(args[2], "sink") == 0) { |
| 472 | const char *name = args[3]; |
| 473 | struct sink *sink; |
| 474 | |
| 475 | if (!*name) { |
| 476 | chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr); |
| 477 | chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*'); |
| 478 | list_for_each_entry(sink, &sink_list, sink_list) { |
| 479 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 480 | src->sink == sink ? '*' : ' ', |
| 481 | sink->name, sink->desc); |
| 482 | } |
| 483 | trash.area[trash.data] = 0; |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 484 | *msg = trash.area; |
| 485 | return LOG_WARNING; |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | if (strcmp(name, "none") == 0) |
| 489 | sink = NULL; |
| 490 | else { |
| 491 | sink = sink_find(name); |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 492 | if (!sink) { |
| 493 | *msg = "No such sink"; |
| 494 | return LOG_ERR; |
| 495 | } |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | HA_ATOMIC_STORE(&src->sink, sink); |
| 499 | } |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 500 | else if (strcmp(args[2], "level") == 0) { |
| 501 | const char *name = args[3]; |
| 502 | |
| 503 | if (!*name) { |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame] | 504 | chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr); |
Willy Tarreau | a1f1274 | 2020-12-01 09:46:46 +0100 | [diff] [blame] | 505 | chunk_appendf(&trash, " %c error : report errors\n", |
| 506 | src->level == TRACE_LEVEL_ERROR ? '*' : ' '); |
| 507 | chunk_appendf(&trash, " %c user : also information useful to the end user\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 508 | src->level == TRACE_LEVEL_USER ? '*' : ' '); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame] | 509 | chunk_appendf(&trash, " %c proto : also protocol-level updates\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 510 | src->level == TRACE_LEVEL_PROTO ? '*' : ' '); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame] | 511 | chunk_appendf(&trash, " %c state : also report internal state changes\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 512 | src->level == TRACE_LEVEL_STATE ? '*' : ' '); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame] | 513 | chunk_appendf(&trash, " %c data : also report data transfers\n", |
| 514 | src->level == TRACE_LEVEL_DATA ? '*' : ' '); |
| 515 | chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 516 | src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' '); |
| 517 | trash.area[trash.data] = 0; |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 518 | *msg = trash.area; |
| 519 | return LOG_WARNING; |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Willy Tarreau | a1f1274 | 2020-12-01 09:46:46 +0100 | [diff] [blame] | 522 | if (strcmp(name, "error") == 0) |
| 523 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_ERROR); |
| 524 | else if (strcmp(name, "user") == 0) |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 525 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER); |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 526 | else if (strcmp(name, "proto") == 0) |
| 527 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO); |
| 528 | else if (strcmp(name, "state") == 0) |
| 529 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame] | 530 | else if (strcmp(name, "data") == 0) |
| 531 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA); |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 532 | else if (strcmp(name, "developer") == 0) |
| 533 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER); |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 534 | else { |
| 535 | *msg = "No such trace level"; |
| 536 | return LOG_ERR; |
| 537 | } |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 538 | } |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 539 | else if (strcmp(args[2], "lock") == 0) { |
| 540 | const char *name = args[3]; |
| 541 | |
| 542 | if (!*name) { |
| 543 | chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr); |
| 544 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) |
| 545 | chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n", |
| 546 | src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' '); |
| 547 | |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 548 | if (src->arg_def & TRC_ARGS_CHK) |
| 549 | chunk_appendf(&trash, " %c check : lock on the check that started the trace\n", |
| 550 | src->lockon == TRACE_LOCKON_CHECK ? '*' : ' '); |
| 551 | |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 552 | if (src->arg_def & TRC_ARGS_CONN) |
| 553 | chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n", |
| 554 | src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' '); |
| 555 | |
| 556 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) |
| 557 | chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n", |
| 558 | src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' '); |
| 559 | |
| 560 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) |
| 561 | chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n", |
| 562 | src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' '); |
| 563 | |
| 564 | chunk_appendf(&trash, " %c nothing : do not lock on anything\n", |
| 565 | src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' '); |
| 566 | |
| 567 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) |
| 568 | chunk_appendf(&trash, " %c server : lock on the server that started the trace\n", |
| 569 | src->lockon == TRACE_LOCKON_SERVER ? '*' : ' '); |
| 570 | |
| 571 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) |
| 572 | chunk_appendf(&trash, " %c session : lock on the session that started the trace\n", |
| 573 | src->lockon == TRACE_LOCKON_SESSION ? '*' : ' '); |
| 574 | |
| 575 | if (src->arg_def & TRC_ARGS_STRM) |
| 576 | chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n", |
| 577 | src->lockon == TRACE_LOCKON_STREAM ? '*' : ' '); |
| 578 | |
| 579 | chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n", |
| 580 | src->lockon == TRACE_LOCKON_THREAD ? '*' : ' '); |
| 581 | |
| 582 | if (src->lockon_args && src->lockon_args[0].name) |
| 583 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 584 | src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ', |
| 585 | src->lockon_args[0].name, src->lockon_args[0].desc); |
| 586 | |
| 587 | if (src->lockon_args && src->lockon_args[1].name) |
| 588 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 589 | src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ', |
| 590 | src->lockon_args[1].name, src->lockon_args[1].desc); |
| 591 | |
| 592 | if (src->lockon_args && src->lockon_args[2].name) |
| 593 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 594 | src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ', |
| 595 | src->lockon_args[2].name, src->lockon_args[2].desc); |
| 596 | |
| 597 | if (src->lockon_args && src->lockon_args[3].name) |
| 598 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 599 | src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ', |
| 600 | src->lockon_args[3].name, src->lockon_args[3].desc); |
| 601 | |
| 602 | trash.area[trash.data] = 0; |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 603 | *msg = trash.area; |
| 604 | return LOG_WARNING; |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 605 | } |
| 606 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) { |
| 607 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND); |
| 608 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 609 | } |
Christopher Faulet | 6d80b63 | 2021-04-12 11:53:16 +0200 | [diff] [blame] | 610 | else if ((src->arg_def & TRC_ARGS_CHK) && strcmp(name, "check") == 0) { |
| 611 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CHECK); |
| 612 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 613 | } |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 614 | else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) { |
| 615 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION); |
| 616 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 617 | } |
| 618 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) { |
| 619 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND); |
| 620 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 621 | } |
| 622 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) { |
| 623 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER); |
| 624 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 625 | } |
| 626 | else if (strcmp(name, "nothing") == 0) { |
| 627 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING); |
| 628 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 629 | } |
| 630 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) { |
| 631 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER); |
| 632 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 633 | } |
| 634 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) { |
| 635 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION); |
| 636 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 637 | } |
| 638 | else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) { |
| 639 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM); |
| 640 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 641 | } |
| 642 | else if (strcmp(name, "thread") == 0) { |
| 643 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD); |
| 644 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 645 | } |
| 646 | else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) { |
| 647 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1); |
| 648 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 649 | } |
| 650 | else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) { |
| 651 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2); |
| 652 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 653 | } |
| 654 | else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) { |
| 655 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3); |
| 656 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 657 | } |
| 658 | else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) { |
| 659 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4); |
| 660 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 661 | } |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 662 | else { |
| 663 | *msg = "Unsupported lock-on criterion"; |
| 664 | return LOG_ERR; |
| 665 | } |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 666 | } |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 667 | else if (strcmp(args[2], "verbosity") == 0) { |
| 668 | const char *name = args[3]; |
| 669 | const struct name_desc *nd; |
| 670 | |
| 671 | if (!*name) { |
| 672 | chunk_printf(&trash, "Supported trace verbosities for source %s:\n", src->name.ptr); |
| 673 | chunk_appendf(&trash, " %c quiet : only report basic information with no decoding\n", |
| 674 | src->verbosity == 0 ? '*' : ' '); |
| 675 | if (!src->decoding || !src->decoding[0].name) { |
| 676 | chunk_appendf(&trash, " %c default : report extra information when available\n", |
| 677 | src->verbosity > 0 ? '*' : ' '); |
| 678 | } else { |
| 679 | for (nd = src->decoding; nd->name && nd->desc; nd++) |
| 680 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 681 | nd == (src->decoding + src->verbosity - 1) ? '*' : ' ', |
| 682 | nd->name, nd->desc); |
| 683 | } |
| 684 | trash.area[trash.data] = 0; |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 685 | *msg = trash.area; |
| 686 | return LOG_WARNING; |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (strcmp(name, "quiet") == 0) |
| 690 | HA_ATOMIC_STORE(&src->verbosity, 0); |
| 691 | else if (!src->decoding || !src->decoding[0].name) { |
| 692 | if (strcmp(name, "default") == 0) |
| 693 | HA_ATOMIC_STORE(&src->verbosity, 1); |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 694 | else { |
| 695 | *msg = "No such verbosity level"; |
| 696 | return LOG_ERR; |
| 697 | } |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 698 | } else { |
| 699 | for (nd = src->decoding; nd->name && nd->desc; nd++) |
| 700 | if (strcmp(name, nd->name) == 0) |
| 701 | break; |
| 702 | |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 703 | if (!nd->name || !nd->desc) { |
| 704 | *msg = "No such verbosiry level"; |
| 705 | return LOG_ERR; |
| 706 | } |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 707 | |
| 708 | HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1); |
| 709 | } |
| 710 | } |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 711 | else { |
| 712 | *msg = "Unknown trace keyword"; |
| 713 | return LOG_ERR; |
| 714 | } |
| 715 | return 0; |
| 716 | |
| 717 | } |
| 718 | |
Willy Tarreau | 9fd0542 | 2022-11-16 17:29:12 +0100 | [diff] [blame] | 719 | /* parse a "trace" statement in the "global" section, returns 1 if a message is returned, otherwise zero */ |
| 720 | static int cfg_parse_trace(char **args, int section_type, struct proxy *curpx, |
| 721 | const struct proxy *defpx, const char *file, int line, |
| 722 | char **err) |
| 723 | { |
| 724 | const char *msg; |
| 725 | int severity; |
| 726 | |
| 727 | severity = trace_parse_statement(args, &msg); |
| 728 | if (msg) { |
| 729 | if (severity >= LOG_NOTICE) |
| 730 | ha_notice("parsing [%s:%d] : '%s': %s\n", file, line, args[0], msg); |
| 731 | else if (severity >= LOG_WARNING) |
| 732 | ha_warning("parsing [%s:%d] : '%s': %s\n", file, line, args[0], msg); |
| 733 | else { |
| 734 | ha_alert("parsing [%s:%d] : '%s': %s\n", file, line, args[0], msg); |
| 735 | return -1; |
| 736 | } |
| 737 | } |
| 738 | return 0; |
| 739 | } |
| 740 | |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 741 | /* parse the command, returns 1 if a message is returned, otherwise zero */ |
| 742 | static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private) |
| 743 | { |
| 744 | const char *msg; |
| 745 | int severity; |
| 746 | |
| 747 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 748 | return 1; |
| 749 | |
| 750 | severity = trace_parse_statement(args, &msg); |
| 751 | if (msg) |
| 752 | return cli_msg(appctx, severity, msg); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 753 | |
Willy Tarreau | c11f1cd | 2022-11-16 17:18:04 +0100 | [diff] [blame] | 754 | /* total success */ |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 755 | return 0; |
| 756 | } |
| 757 | |
Willy Tarreau | 85b1575 | 2019-08-12 16:44:33 +0200 | [diff] [blame] | 758 | /* parse the command, returns 1 if a message is returned, otherwise zero */ |
| 759 | static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private) |
| 760 | { |
| 761 | struct trace_source *src; |
| 762 | const struct sink *sink; |
| 763 | int i; |
| 764 | |
| 765 | args++; // make args[1] the 1st arg |
| 766 | |
| 767 | if (!*args[1]) { |
| 768 | /* no arg => report the list of supported sources */ |
| 769 | chunk_printf(&trash, |
| 770 | "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n" |
| 771 | ); |
| 772 | |
| 773 | list_for_each_entry(src, &trace_sources, source_link) { |
| 774 | sink = src->sink; |
| 775 | chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n", |
| 776 | trace_state_char(src->state), src->name.ptr, |
| 777 | sink ? sink->name : "none", |
| 778 | sink ? sink->ctx.dropped : 0, |
| 779 | src->desc); |
| 780 | } |
| 781 | |
| 782 | trash.area[trash.data] = 0; |
| 783 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 784 | } |
| 785 | |
| 786 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 787 | return 1; |
| 788 | |
| 789 | src = trace_find_source(args[1]); |
| 790 | if (!src) |
| 791 | return cli_err(appctx, "No such trace source"); |
| 792 | |
| 793 | sink = src->sink; |
| 794 | chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr); |
| 795 | chunk_appendf(&trash, " - sink: %s [%u dropped]\n", |
| 796 | sink ? sink->name : "none", sink ? sink->ctx.dropped : 0); |
| 797 | |
Willy Tarreau | 99282dd | 2019-08-30 07:37:32 +0200 | [diff] [blame] | 798 | chunk_appendf(&trash, " - event name : report start stop pause\n"); |
Willy Tarreau | 85b1575 | 2019-08-12 16:44:33 +0200 | [diff] [blame] | 799 | for (i = 0; src->known_events && src->known_events[i].mask; i++) { |
Willy Tarreau | 99282dd | 2019-08-30 07:37:32 +0200 | [diff] [blame] | 800 | chunk_appendf(&trash, " %-12s : %c %c %c %c\n", |
Willy Tarreau | 85b1575 | 2019-08-12 16:44:33 +0200 | [diff] [blame] | 801 | src->known_events[i].name, |
| 802 | trace_event_char(src->report_events, src->known_events[i].mask), |
| 803 | trace_event_char(src->start_events, src->known_events[i].mask), |
| 804 | trace_event_char(src->stop_events, src->known_events[i].mask), |
| 805 | trace_event_char(src->pause_events, src->known_events[i].mask)); |
| 806 | } |
| 807 | |
| 808 | trash.area[trash.data] = 0; |
| 809 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 810 | } |
| 811 | |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 812 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 813 | { { "trace", NULL }, "trace [<module>|0] [cmd [args...]] : manage live tracing (empty to list, 0 to stop all)", cli_parse_trace, NULL, NULL }, |
| 814 | { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL }, |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 815 | {{},} |
| 816 | }}; |
| 817 | |
| 818 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 819 | |
Willy Tarreau | 9fd0542 | 2022-11-16 17:29:12 +0100 | [diff] [blame] | 820 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 821 | { CFG_GLOBAL, "trace", cfg_parse_trace, KWF_EXPERIMENTAL }, |
| 822 | { /* END */ }, |
| 823 | }}; |
| 824 | |
| 825 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 826 | |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 827 | /* |
| 828 | * Local variables: |
| 829 | * c-indent-level: 8 |
| 830 | * c-basic-offset: 8 |
| 831 | * End: |
| 832 | */ |