blob: 84c50c7fa94ad8219376913dd4d7ea88fc1a8c00 [file] [log] [blame]
Willy Tarreau4151c752019-08-08 18:21:26 +02001/*
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 Tarreaueb6f7012020-05-27 16:21:26 +020021#include <import/ist.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020022#include <haproxy/api.h>
23#include <haproxy/buf.h>
Willy Tarreau9fd05422022-11-16 17:29:12 +010024#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020026#include <haproxy/errors.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020027#include <haproxy/istbuf.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020028#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreaub71c4f32024-08-06 11:07:13 +020030#include <haproxy/quic_conn-t.h>
Willy Tarreauba2f73d2020-06-03 20:02:28 +020031#include <haproxy/sink.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020032#include <haproxy/trace.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020033
34struct list trace_sources = LIST_HEAD_INIT(trace_sources);
Willy Tarreau88ebd402019-08-19 15:55:34 +020035THREAD_LOCAL struct buffer trace_buf = { };
36
37/* allocates the trace buffers. Returns 0 in case of failure. It is safe to
38 * call to call this function multiple times if the size changes.
39 */
40static int alloc_trace_buffers_per_thread()
41{
42 chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2e7fdfc2019-11-25 19:43:31 +010043 return !!trace_buf.area;
Willy Tarreau88ebd402019-08-19 15:55:34 +020044}
45
46static void free_trace_buffers_per_thread()
47{
48 chunk_destroy(&trace_buf);
49}
Willy Tarreau4151c752019-08-08 18:21:26 +020050
Willy Tarreau88ebd402019-08-19 15:55:34 +020051REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread);
52REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread);
Willy Tarreau864e8802019-08-08 18:48:12 +020053
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020054/* pick the lowest non-null argument with a non-null arg_def mask */
55static inline const void *trace_pick_arg(uint32_t arg_def, const void *a1, const void *a2, const void *a3, const void *a4)
56{
57 if (arg_def & 0x0000FFFF) {
58 if ((arg_def & 0x000000FF) && a1)
59 return a1;
60 if ((arg_def & 0x0000FF00) && a2)
61 return a2;
62 }
63
64 if (arg_def & 0xFFFF0000) {
65 if ((arg_def & 0x00FF0000) && a3)
66 return a3;
67 if ((arg_def & 0xFF000000) && a4)
68 return a4;
69 }
70
71 return NULL;
72}
73
Willy Tarreau8f9a9702023-01-24 17:48:53 +010074/* Reports whether the trace is enabled for the specified arguments, needs to enable
75 * or disable tracking. It gets the same API as __trace() except for <cb> and <msg>
76 * which are not used and were dropped, and plockptr which is an optional pointer to
77 * the lockptr to be updated (or NULL) for tracking. The function returns:
78 * 0 if the trace is not enabled for the module or these values
79 * <0 if the trace matches some locking criteria but don't have the proper level.
80 * In this case the interested caller might have to consider disabling tracking.
81 * >0 if the trace is enabled for the given criteria.
82 * In all cases, <plockptr> will only be set if non-null and if a locking criterion
83 * matched. It will be up to the caller to enable tracking if desired. A casual
84 * tester not interested in adjusting tracking (i.e. calling the function before
85 * deciding so prepare a buffer to be dumped) will only need to pass 0 for plockptr
86 * and check if the result is >0.
87 */
88int __trace_enabled(enum trace_level level, uint64_t mask, struct trace_source *src,
89 const struct ist where, const char *func,
90 const void *a1, const void *a2, const void *a3, const void *a4,
91 const void **plockptr)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020092{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020093 const struct listener *li = NULL;
94 const struct proxy *fe = NULL;
95 const struct proxy *be = NULL;
96 const struct server *srv = NULL;
97 const struct session *sess = NULL;
98 const struct stream *strm = NULL;
99 const struct connection *conn = NULL;
Christopher Faulet6d80b632021-04-12 11:53:16 +0200100 const struct check *check = NULL;
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100101 const struct quic_conn *qc = NULL;
Christopher Faulet26e09352023-03-29 17:42:28 +0200102 const struct appctx *appctx = NULL;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200103 const void *lockon_ptr = NULL;
Willy Tarreau419bd492019-08-12 17:27:09 +0200104
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200105 if (likely(src->state == TRACE_STATE_STOPPED))
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100106 return 0;
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200107
108 /* check that at least one action is interested by this event */
109 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100110 return 0;
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200111
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200112 /* retrieve available information from the caller's arguments */
113 if (src->arg_def & TRC_ARGS_CONN)
114 conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4);
115
116 if (src->arg_def & TRC_ARGS_SESS)
117 sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4);
118
119 if (src->arg_def & TRC_ARGS_STRM)
120 strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4);
121
Christopher Faulet6d80b632021-04-12 11:53:16 +0200122 if (src->arg_def & TRC_ARGS_CHK)
123 check = trace_pick_arg(src->arg_def & TRC_ARGS_CHK, a1, a2, a3, a4);
124
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100125 if (src->arg_def & TRC_ARGS_QCON)
126 qc = trace_pick_arg(src->arg_def & TRC_ARGS_QCON, a1, a2, a3, a4);
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100127
Christopher Faulet26e09352023-03-29 17:42:28 +0200128 if (src->arg_def & TRC_ARGS_APPCTX)
129 appctx = trace_pick_arg(src->arg_def & TRC_ARGS_APPCTX, a1, a2, a3, a4);
130
Willy Tarreaub71c4f32024-08-06 11:07:13 +0200131#ifdef USE_QUIC
132 if (qc && !conn)
133 conn = qc->conn;
134#endif
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200135 if (!sess && strm)
136 sess = strm->sess;
Amaury Denoyelle926a5612024-03-14 11:24:10 +0100137 else if (!sess && conn && LIST_INLIST(&conn->sess_el))
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200138 sess = conn->owner;
Christopher Faulet6d80b632021-04-12 11:53:16 +0200139 else if (!sess && check)
140 sess = check->sess;
Christopher Faulet26e09352023-03-29 17:42:28 +0200141 else if (!sess && appctx)
142 sess = appctx->sess;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200143
144 if (sess) {
145 fe = sess->fe;
146 li = sess->listener;
147 }
148
149 if (!li && conn)
150 li = objt_listener(conn->target);
151
152 if (li && !fe)
153 fe = li->bind_conf->frontend;
154
155 if (strm) {
156 be = strm->be;
157 srv = strm->srv_conn;
158 }
Christopher Faulet6d80b632021-04-12 11:53:16 +0200159 if (check) {
160 srv = check->server;
Christopher Faulete3b25742022-06-08 09:06:15 +0200161 be = (srv ? srv->proxy : NULL);
Christopher Faulet6d80b632021-04-12 11:53:16 +0200162 }
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200163
164 if (!srv && conn)
165 srv = objt_server(conn->target);
166
167 if (srv && !be)
168 be = srv->proxy;
169
170 if (!be && conn)
171 be = objt_proxy(conn->target);
172
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200173 /* TODO: add handling of filters here, return if no match (not even update states) */
174
175 /* check if we need to start the trace now */
176 if (src->state == TRACE_STATE_WAITING) {
177 if ((src->start_events & mask) == 0)
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100178 return 0;
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200179
180 /* TODO: add update of lockon+lockon_ptr here */
181 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
182 }
183
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200184 /* we may want to lock on a particular object */
185 if (src->lockon != TRACE_LOCKON_NOTHING) {
186 switch (src->lockon) {
Christopher Faulet26e09352023-03-29 17:42:28 +0200187 case TRACE_LOCKON_BACKEND: lockon_ptr = be; break;
188 case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break;
189 case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break;
190 case TRACE_LOCKON_LISTENER: lockon_ptr = li; break;
191 case TRACE_LOCKON_SERVER: lockon_ptr = srv; break;
192 case TRACE_LOCKON_SESSION: lockon_ptr = sess; break;
193 case TRACE_LOCKON_STREAM: lockon_ptr = strm; break;
194 case TRACE_LOCKON_CHECK: lockon_ptr = check; break;
195 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
196 case TRACE_LOCKON_QCON: lockon_ptr = qc; break;
197 case TRACE_LOCKON_APPCTX: lockon_ptr = appctx; break;
198 case TRACE_LOCKON_ARG1: lockon_ptr = a1; break;
199 case TRACE_LOCKON_ARG2: lockon_ptr = a2; break;
200 case TRACE_LOCKON_ARG3: lockon_ptr = a3; break;
201 case TRACE_LOCKON_ARG4: lockon_ptr = a4; break;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200202 default: break; // silence stupid gcc -Wswitch
203 }
204
205 if (src->lockon_ptr && src->lockon_ptr != lockon_ptr)
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100206 return 0;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200207
Willy Tarreau2edfb892024-08-06 11:32:10 +0200208 if (plockptr && !src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100209 *plockptr = lockon_ptr;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200210 }
211
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200212 /* here the trace is running and is tracking a desired item */
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100213 if ((src->report_events & mask) == 0 || level > src->level) {
214 /* tracking did match, and might have to be disabled */
215 return -1;
216 }
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200217
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100218 /* OK trace still enabled */
219 return 1;
220}
221
222/* write a message for the given trace source */
223void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
224 const struct ist where, const char *func,
225 const void *a1, const void *a2, const void *a3, const void *a4,
226 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
227 const struct ist where, const struct ist func,
228 const void *a1, const void *a2, const void *a3, const void *a4),
229 const struct ist msg)
230{
231 const void *lockon_ptr;
232 struct ist ist_func = ist(func);
233 char tnum[4];
234 struct ist line[12];
235 int words = 0;
236 int ret;
237
238 lockon_ptr = NULL;
239 ret = __trace_enabled(level, mask, src, where, func, a1, a2, a3, a4, &lockon_ptr);
240 if (lockon_ptr)
241 HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr);
242
243 if (ret <= 0) {
244 if (ret < 0) // may have to disable tracking
245 goto end;
246 return;
247 }
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200248
Willy Tarreau419bd492019-08-12 17:27:09 +0200249 /* log the logging location truncated to 10 chars from the right so that
250 * the line number and the end of the file name are there.
251 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200252 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200253 tnum[0] = '0' + tid / 10;
254 tnum[1] = '0' + tid % 10;
255 tnum[2] = '|';
256 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200257 line[words++] = ist(tnum);
258 line[words++] = src->name;
259 line[words++] = ist("|");
Frédéric Lécaillefd183142020-12-02 16:51:00 +0100260 line[words++] = ist2("012345" + level, 1); // "0" to "5"
Willy Tarreau052ad362019-08-29 09:08:36 +0200261 line[words++] = ist("|");
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200262 line[words] = where;
263 if (line[words].len > 13) {
264 line[words].ptr += (line[words].len - 13);
265 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200266 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200267 words++;
268 line[words++] = ist("] ");
269
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100270 if (isttest(ist_func)) {
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200271 line[words++] = ist_func;
272 line[words++] = ist("(): ");
273 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200274
Willy Tarreau3da00262019-08-28 07:03:58 +0200275 if (!cb)
276 cb = src->default_cb;
277
Willy Tarreau370a6942019-08-29 08:24:16 +0200278 if (cb && src->verbosity) {
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200279 /* decode function passed, we want to pre-fill the
280 * buffer with the message and let the decode function
281 * do its job, possibly even overwriting it.
282 */
283 b_reset(&trace_buf);
284 b_istput(&trace_buf, msg);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200285 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100286 line[words] = ist2(trace_buf.area, trace_buf.data);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200287 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200288 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200289 else {
290 /* Note that here we could decide to print some args whose type
291 * is known, when verbosity is above the quiet level, and even
292 * to print the name and values of those which are declared for
293 * lock-on.
294 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200295 line[words++] = msg;
Willy Tarreau370a6942019-08-29 08:24:16 +0200296 }
Willy Tarreau419bd492019-08-12 17:27:09 +0200297
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200298 if (src->sink)
Aurelien DARRAGON5bab37e2023-06-26 16:44:41 +0200299 sink_write(src->sink, 0, line, words, 0, 0, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200300
301 end:
302 /* check if we need to stop the trace now */
303 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200304 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200305 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
306 }
307 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200308 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200309 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
310 }
311}
312
Willy Tarreau4b36d5e2023-01-24 18:03:07 +0100313/* this callback may be used when no output modification is desired */
314void trace_no_cb(enum trace_level level, uint64_t mask, const struct trace_source *src,
315 const struct ist where, const struct ist func,
316 const void *a1, const void *a2, const void *a3, const void *a4)
317{
318 /* do nothing */
319}
320
Willy Tarreau4de2cda2022-03-02 14:53:00 +0100321/* registers trace source <source>. Modifies the list element!
322 * The {start,pause,stop,report} events are not changed so the source may
323 * preset them.
324 */
325void trace_register_source(struct trace_source *source)
326{
327 source->lockon = TRACE_LOCKON_NOTHING;
328 source->level = TRACE_LEVEL_USER;
329 source->verbosity = 1;
330 source->sink = NULL;
331 source->state = TRACE_STATE_STOPPED;
332 source->lockon_ptr = NULL;
333 LIST_APPEND(&trace_sources, &source->source_link);
334}
335
Willy Tarreau864e8802019-08-08 18:48:12 +0200336struct trace_source *trace_find_source(const char *name)
337{
338 struct trace_source *src;
339 const struct ist iname = ist(name);
340
341 list_for_each_entry(src, &trace_sources, source_link)
342 if (isteq(src->name, iname))
343 return src;
344 return NULL;
345}
346
347const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
348{
349 for (; ev && ev->mask; ev++)
350 if (strcmp(ev->name, name) == 0)
351 return ev;
352 return NULL;
353}
354
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100355/* Parse a "trace" statement. Returns a severity as a LOG_* level and a status
356 * message that may be delivered to the user, in <msg>. The message will be
Willy Tarreauf72eca22023-10-19 14:45:07 +0200357 * nulled first and msg must be an allocated pointer. A null status message output
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100358 * indicates no error. Be careful not to use the return value as a boolean, as
359 * LOG_* values are not ordered as one could imagine (LOG_EMERG is zero). The
360 * function may/will use the trash buffer as the storage for the response
361 * message so that the caller never needs to release anything.
362 */
Willy Tarreauf72eca22023-10-19 14:45:07 +0200363static int trace_parse_statement(char **args, char **msg)
Willy Tarreau864e8802019-08-08 18:48:12 +0200364{
365 struct trace_source *src;
366 uint64_t *ev_ptr = NULL;
367
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100368 /* no error by default */
369 *msg = NULL;
Willy Tarreau864e8802019-08-08 18:48:12 +0200370
371 if (!*args[1]) {
372 /* no arg => report the list of supported sources as a warning */
373 chunk_printf(&trash,
374 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
375 " [.] 0 : not a source, will immediately stop all traces\n"
376 );
377
378 list_for_each_entry(src, &trace_sources, source_link)
379 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
380
381 trash.area[trash.data] = 0;
Willy Tarreauf72eca22023-10-19 14:45:07 +0200382 *msg = strdup(trash.area);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100383 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200384 }
385
386 if (strcmp(args[1], "0") == 0) {
387 /* emergency stop of all traces */
388 list_for_each_entry(src, &trace_sources, source_link)
389 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreauf72eca22023-10-19 14:45:07 +0200390 *msg = strdup("All traces now stopped");
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100391 return LOG_NOTICE;
Willy Tarreau864e8802019-08-08 18:48:12 +0200392 }
393
394 src = trace_find_source(args[1]);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100395 if (!src) {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200396 memprintf(msg, "No such trace source '%s'", args[1]);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100397 return LOG_ERR;
398 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200399
400 if (!*args[2]) {
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100401 *msg = "Supported commands:\n"
402 " event : list/enable/disable source-specific event reporting\n"
403 //" filter : list/enable/disable generic filters\n"
404 " level : list/set trace reporting level\n"
405 " lock : automatic lock on thread/connection/stream/...\n"
406 " pause : pause and automatically restart after a specific event\n"
407 " sink : list/set event sinks\n"
408 " start : start immediately or after a specific event\n"
409 " stop : stop immediately or after a specific event\n"
410 " verbosity : list/set trace output verbosity\n";
Willy Tarreauf72eca22023-10-19 14:45:07 +0200411 *msg = strdup(*msg);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100412 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200413 }
414 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
415 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
416 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
417 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
418 const struct trace_event *ev;
419 const char *name = args[3];
420 int neg = 0;
421 int i;
422
423 /* skip prefix '!', '-', '+' and remind negation */
424 while (*name) {
425 if (*name == '!' || *name == '-')
426 neg = 1;
427 else if (*name == '+')
428 neg = 0;
429 else
430 break;
431 name++;
432 }
433
434 if (!*name) {
435 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
436 if (ev_ptr != &src->report_events)
Willy Tarreau99282dd2019-08-30 07:37:32 +0200437 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
438 chunk_appendf(&trash, " - none : disable all event types\n");
439 chunk_appendf(&trash, " - any : enable all event types\n");
Willy Tarreau864e8802019-08-08 18:48:12 +0200440 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200441 chunk_appendf(&trash, " %c %-12s : %s\n",
Willy Tarreau864e8802019-08-08 18:48:12 +0200442 trace_event_char(*ev_ptr, src->known_events[i].mask),
443 src->known_events[i].name, src->known_events[i].desc);
444 }
445 trash.area[trash.data] = 0;
Willy Tarreauf72eca22023-10-19 14:45:07 +0200446 *msg = strdup(trash.area);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100447 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200448 }
449
Willy Tarreau0a796402024-08-06 11:45:54 +0200450 /* state transitions:
451 * - "start now" => TRACE_STATE_RUNNING
452 * - "stop now" => TRACE_STATE_STOPPED
453 * - "pause now" => TRACE_STATE_WAITING
454 * - "start <evt>" && STATE_STOPPED => TRACE_STATE_WAITING
455 */
456
Willy Tarreau864e8802019-08-08 18:48:12 +0200457 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
458 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200459 if (ev_ptr == &src->pause_events) {
460 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200461 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200462 }
463 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200464 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200465 }
466 else if (ev_ptr == &src->stop_events) {
467 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200468 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200469 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200470 return 0;
471 }
472
473 if (strcmp(name, "none") == 0)
474 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreau0a796402024-08-06 11:45:54 +0200475 else if (strcmp(name, "any") == 0) {
476 enum trace_state old = TRACE_STATE_STOPPED;
477
Willy Tarreau864e8802019-08-08 18:48:12 +0200478 HA_ATOMIC_STORE(ev_ptr, ~0);
Willy Tarreau0a796402024-08-06 11:45:54 +0200479 if (ev_ptr == &src->start_events)
480 HA_ATOMIC_CAS(&src->state, &old, TRACE_STATE_WAITING);
481 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200482 else {
Willy Tarreau0a796402024-08-06 11:45:54 +0200483 enum trace_state old = TRACE_STATE_STOPPED;
484
Willy Tarreau864e8802019-08-08 18:48:12 +0200485 ev = trace_find_event(src->known_events, name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100486 if (!ev) {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200487 memprintf(msg, "No such trace event '%s'", name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100488 return LOG_ERR;
489 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200490
491 if (!neg)
492 HA_ATOMIC_OR(ev_ptr, ev->mask);
493 else
494 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
Willy Tarreau0a796402024-08-06 11:45:54 +0200495
496 if (ev_ptr == &src->start_events && HA_ATOMIC_LOAD(ev_ptr) != 0)
497 HA_ATOMIC_CAS(&src->state, &old, TRACE_STATE_WAITING);
Willy Tarreau864e8802019-08-08 18:48:12 +0200498 }
499 }
500 else if (strcmp(args[2], "sink") == 0) {
501 const char *name = args[3];
502 struct sink *sink;
503
504 if (!*name) {
505 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
506 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
507 list_for_each_entry(sink, &sink_list, sink_list) {
508 chunk_appendf(&trash, " %c %-10s : %s\n",
509 src->sink == sink ? '*' : ' ',
510 sink->name, sink->desc);
511 }
512 trash.area[trash.data] = 0;
Willy Tarreauf72eca22023-10-19 14:45:07 +0200513 *msg = strdup(trash.area);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100514 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200515 }
516
517 if (strcmp(name, "none") == 0)
518 sink = NULL;
519 else {
520 sink = sink_find(name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100521 if (!sink) {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200522 memprintf(msg, "No such trace sink '%s'", name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100523 return LOG_ERR;
524 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200525 }
526
527 HA_ATOMIC_STORE(&src->sink, sink);
528 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200529 else if (strcmp(args[2], "level") == 0) {
530 const char *name = args[3];
531
532 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200533 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreaua1f12742020-12-01 09:46:46 +0100534 chunk_appendf(&trash, " %c error : report errors\n",
535 src->level == TRACE_LEVEL_ERROR ? '*' : ' ');
536 chunk_appendf(&trash, " %c user : also information useful to the end user\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200537 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200538 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200539 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200540 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200541 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200542 chunk_appendf(&trash, " %c data : also report data transfers\n",
543 src->level == TRACE_LEVEL_DATA ? '*' : ' ');
544 chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200545 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
546 trash.area[trash.data] = 0;
Willy Tarreauf72eca22023-10-19 14:45:07 +0200547 *msg = strdup(trash.area);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100548 return LOG_WARNING;
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200549 }
550
Willy Tarreaua1f12742020-12-01 09:46:46 +0100551 if (strcmp(name, "error") == 0)
552 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_ERROR);
553 else if (strcmp(name, "user") == 0)
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200554 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200555 else if (strcmp(name, "proto") == 0)
556 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
557 else if (strcmp(name, "state") == 0)
558 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200559 else if (strcmp(name, "data") == 0)
560 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200561 else if (strcmp(name, "developer") == 0)
562 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100563 else {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200564 memprintf(msg, "No such trace level '%s'", name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100565 return LOG_ERR;
566 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200567 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200568 else if (strcmp(args[2], "lock") == 0) {
569 const char *name = args[3];
570
571 if (!*name) {
572 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
573 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
574 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
575 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
576
Christopher Faulet6d80b632021-04-12 11:53:16 +0200577 if (src->arg_def & TRC_ARGS_CHK)
578 chunk_appendf(&trash, " %c check : lock on the check that started the trace\n",
579 src->lockon == TRACE_LOCKON_CHECK ? '*' : ' ');
580
Willy Tarreau16f11132024-08-06 11:09:03 +0200581 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON))
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200582 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
583 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
584
Willy Tarreau16f11132024-08-06 11:09:03 +0200585 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON|TRC_ARGS_SESS|TRC_ARGS_STRM))
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200586 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
587 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
588
Willy Tarreau16f11132024-08-06 11:09:03 +0200589 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON|TRC_ARGS_SESS|TRC_ARGS_STRM))
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200590 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
591 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
592
593 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
594 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
595
596 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
597 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
598 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
Willy Tarreau037bc372024-08-06 15:21:00 +0200599#ifdef USE_QUIC
600 if (src->arg_def & TRC_ARGS_QCON)
601 chunk_appendf(&trash, " %c qconn : lock on the QUIC connection that started the trace\n",
602 src->lockon == TRACE_LOCKON_QCON ? '*' : ' ');
603#endif
Willy Tarreau16f11132024-08-06 11:09:03 +0200604 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON|TRC_ARGS_SESS|TRC_ARGS_STRM))
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200605 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
606 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
607
608 if (src->arg_def & TRC_ARGS_STRM)
609 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
610 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
611
Christopher Faulet26e09352023-03-29 17:42:28 +0200612 if (src->arg_def & TRC_ARGS_APPCTX)
613 chunk_appendf(&trash, " %c applet : lock on the applet that started the trace\n",
614 src->lockon == TRACE_LOCKON_APPCTX ? '*' : ' ');
615
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200616 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
617 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
618
619 if (src->lockon_args && src->lockon_args[0].name)
620 chunk_appendf(&trash, " %c %-10s : %s\n",
621 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
622 src->lockon_args[0].name, src->lockon_args[0].desc);
623
624 if (src->lockon_args && src->lockon_args[1].name)
625 chunk_appendf(&trash, " %c %-10s : %s\n",
626 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
627 src->lockon_args[1].name, src->lockon_args[1].desc);
628
629 if (src->lockon_args && src->lockon_args[2].name)
630 chunk_appendf(&trash, " %c %-10s : %s\n",
631 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
632 src->lockon_args[2].name, src->lockon_args[2].desc);
633
634 if (src->lockon_args && src->lockon_args[3].name)
635 chunk_appendf(&trash, " %c %-10s : %s\n",
636 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
637 src->lockon_args[3].name, src->lockon_args[3].desc);
638
639 trash.area[trash.data] = 0;
Willy Tarreauf72eca22023-10-19 14:45:07 +0200640 *msg = strdup(trash.area);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100641 return LOG_WARNING;
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200642 }
643 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
644 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
645 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
646 }
Christopher Faulet6d80b632021-04-12 11:53:16 +0200647 else if ((src->arg_def & TRC_ARGS_CHK) && strcmp(name, "check") == 0) {
648 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CHECK);
649 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
650 }
Willy Tarreau16f11132024-08-06 11:09:03 +0200651 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON)) && strcmp(name, "connection") == 0) {
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200652 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
653 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
654 }
Willy Tarreau16f11132024-08-06 11:09:03 +0200655 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200656 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
657 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
658 }
Willy Tarreau16f11132024-08-06 11:09:03 +0200659 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200660 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
661 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
662 }
663 else if (strcmp(name, "nothing") == 0) {
664 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
665 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
666 }
667 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
668 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
669 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
670 }
Willy Tarreau16f11132024-08-06 11:09:03 +0200671 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_QCON|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200672 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
673 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
674 }
Willy Tarreau037bc372024-08-06 15:21:00 +0200675 else if ((src->arg_def & TRC_ARGS_QCON) && strcmp(name, "qconn") == 0) {
676 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_QCON);
677 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
678 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200679 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
680 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
681 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
682 }
Christopher Faulet26e09352023-03-29 17:42:28 +0200683 else if ((src->arg_def & TRC_ARGS_APPCTX) && strcmp(name, "appctx") == 0) {
684 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_APPCTX);
685 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
686 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200687 else if (strcmp(name, "thread") == 0) {
688 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
689 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
690 }
691 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
692 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
693 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
694 }
695 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
696 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
697 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
698 }
699 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
700 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
701 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
702 }
703 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
704 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
705 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
706 }
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100707 else {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200708 memprintf(msg, "Unsupported lock-on criterion '%s'", name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100709 return LOG_ERR;
710 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200711 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200712 else if (strcmp(args[2], "verbosity") == 0) {
713 const char *name = args[3];
714 const struct name_desc *nd;
715
716 if (!*name) {
717 chunk_printf(&trash, "Supported trace verbosities for source %s:\n", src->name.ptr);
718 chunk_appendf(&trash, " %c quiet : only report basic information with no decoding\n",
719 src->verbosity == 0 ? '*' : ' ');
720 if (!src->decoding || !src->decoding[0].name) {
721 chunk_appendf(&trash, " %c default : report extra information when available\n",
722 src->verbosity > 0 ? '*' : ' ');
723 } else {
724 for (nd = src->decoding; nd->name && nd->desc; nd++)
725 chunk_appendf(&trash, " %c %-10s : %s\n",
726 nd == (src->decoding + src->verbosity - 1) ? '*' : ' ',
727 nd->name, nd->desc);
728 }
729 trash.area[trash.data] = 0;
Willy Tarreauf72eca22023-10-19 14:45:07 +0200730 *msg = strdup(trash.area);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100731 return LOG_WARNING;
Willy Tarreau370a6942019-08-29 08:24:16 +0200732 }
733
734 if (strcmp(name, "quiet") == 0)
735 HA_ATOMIC_STORE(&src->verbosity, 0);
736 else if (!src->decoding || !src->decoding[0].name) {
737 if (strcmp(name, "default") == 0)
738 HA_ATOMIC_STORE(&src->verbosity, 1);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100739 else {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200740 memprintf(msg, "No such verbosity level '%s'", name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100741 return LOG_ERR;
742 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200743 } else {
744 for (nd = src->decoding; nd->name && nd->desc; nd++)
745 if (strcmp(name, nd->name) == 0)
746 break;
747
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100748 if (!nd->name || !nd->desc) {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200749 memprintf(msg, "No such verbosity level '%s'", name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100750 return LOG_ERR;
751 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200752
753 HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1);
754 }
755 }
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100756 else {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200757 memprintf(msg, "Unknown trace keyword '%s'", args[2]);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100758 return LOG_ERR;
759 }
760 return 0;
761
762}
763
Willy Tarreau9fd05422022-11-16 17:29:12 +0100764/* parse a "trace" statement in the "global" section, returns 1 if a message is returned, otherwise zero */
765static int cfg_parse_trace(char **args, int section_type, struct proxy *curpx,
766 const struct proxy *defpx, const char *file, int line,
767 char **err)
768{
Willy Tarreauf72eca22023-10-19 14:45:07 +0200769 char *msg;
Willy Tarreau9fd05422022-11-16 17:29:12 +0100770 int severity;
771
772 severity = trace_parse_statement(args, &msg);
773 if (msg) {
774 if (severity >= LOG_NOTICE)
775 ha_notice("parsing [%s:%d] : '%s': %s\n", file, line, args[0], msg);
776 else if (severity >= LOG_WARNING)
777 ha_warning("parsing [%s:%d] : '%s': %s\n", file, line, args[0], msg);
778 else {
Willy Tarreauf72eca22023-10-19 14:45:07 +0200779 /* let the caller free the message */
780 *err = msg;
Willy Tarreau9fd05422022-11-16 17:29:12 +0100781 return -1;
782 }
Willy Tarreauf72eca22023-10-19 14:45:07 +0200783 ha_free(&msg);
Willy Tarreau9fd05422022-11-16 17:29:12 +0100784 }
785 return 0;
786}
787
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100788/* parse the command, returns 1 if a message is returned, otherwise zero */
789static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
790{
Willy Tarreauf72eca22023-10-19 14:45:07 +0200791 char *msg;
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100792 int severity;
793
794 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
795 return 1;
796
797 severity = trace_parse_statement(args, &msg);
798 if (msg)
Willy Tarreauf72eca22023-10-19 14:45:07 +0200799 return cli_dynmsg(appctx, severity, msg);
Willy Tarreau864e8802019-08-08 18:48:12 +0200800
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100801 /* total success */
Willy Tarreau864e8802019-08-08 18:48:12 +0200802 return 0;
803}
804
Willy Tarreau85b15752019-08-12 16:44:33 +0200805/* parse the command, returns 1 if a message is returned, otherwise zero */
806static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
807{
808 struct trace_source *src;
809 const struct sink *sink;
810 int i;
811
812 args++; // make args[1] the 1st arg
813
814 if (!*args[1]) {
815 /* no arg => report the list of supported sources */
816 chunk_printf(&trash,
817 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
818 );
819
820 list_for_each_entry(src, &trace_sources, source_link) {
821 sink = src->sink;
822 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
823 trace_state_char(src->state), src->name.ptr,
824 sink ? sink->name : "none",
825 sink ? sink->ctx.dropped : 0,
826 src->desc);
827 }
828
829 trash.area[trash.data] = 0;
830 return cli_msg(appctx, LOG_INFO, trash.area);
831 }
832
833 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
834 return 1;
835
836 src = trace_find_source(args[1]);
837 if (!src)
838 return cli_err(appctx, "No such trace source");
839
840 sink = src->sink;
841 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
842 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
843 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
844
Willy Tarreau99282dd2019-08-30 07:37:32 +0200845 chunk_appendf(&trash, " - event name : report start stop pause\n");
Willy Tarreau85b15752019-08-12 16:44:33 +0200846 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200847 chunk_appendf(&trash, " %-12s : %c %c %c %c\n",
Willy Tarreau85b15752019-08-12 16:44:33 +0200848 src->known_events[i].name,
849 trace_event_char(src->report_events, src->known_events[i].mask),
850 trace_event_char(src->start_events, src->known_events[i].mask),
851 trace_event_char(src->stop_events, src->known_events[i].mask),
852 trace_event_char(src->pause_events, src->known_events[i].mask));
853 }
854
855 trash.area[trash.data] = 0;
856 return cli_msg(appctx, LOG_WARNING, trash.area);
857}
858
Willy Tarreau864e8802019-08-08 18:48:12 +0200859static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200860 { { "trace", NULL }, "trace [<module>|0] [cmd [args...]] : manage live tracing (empty to list, 0 to stop all)", cli_parse_trace, NULL, NULL },
861 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200862 {{},}
863}};
864
865INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
866
Willy Tarreau9fd05422022-11-16 17:29:12 +0100867static struct cfg_kw_list cfg_kws = {ILH, {
868 { CFG_GLOBAL, "trace", cfg_parse_trace, KWF_EXPERIMENTAL },
869 { /* END */ },
870}};
871
872INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
873
Willy Tarreau4151c752019-08-08 18:21:26 +0200874/*
875 * Local variables:
876 * c-indent-level: 8
877 * c-basic-offset: 8
878 * End:
879 */