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