blob: 32fac61d394191fddf69d8d56838bfaf75880615 [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 Tarreauba2f73d2020-06-03 20:02:28 +020030#include <haproxy/sink.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020031#include <haproxy/trace.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020032
33struct list trace_sources = LIST_HEAD_INIT(trace_sources);
Willy Tarreau88ebd402019-08-19 15:55:34 +020034THREAD_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 */
39static int alloc_trace_buffers_per_thread()
40{
41 chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2e7fdfc2019-11-25 19:43:31 +010042 return !!trace_buf.area;
Willy Tarreau88ebd402019-08-19 15:55:34 +020043}
44
45static void free_trace_buffers_per_thread()
46{
47 chunk_destroy(&trace_buf);
48}
Willy Tarreau4151c752019-08-08 18:21:26 +020049
Willy Tarreau88ebd402019-08-19 15:55:34 +020050REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread);
51REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread);
Willy Tarreau864e8802019-08-08 18:48:12 +020052
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020053/* pick the lowest non-null argument with a non-null arg_def mask */
54static 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 Tarreau8f9a9702023-01-24 17:48:53 +010073/* 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 */
87int __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 Tarreau4c2ae482019-08-12 15:51:58 +020091{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020092 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 Faulet6d80b632021-04-12 11:53:16 +020099 const struct check *check = NULL;
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100100 const struct quic_conn *qc = NULL;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200101 const void *lockon_ptr = NULL;
Willy Tarreau419bd492019-08-12 17:27:09 +0200102
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200103 if (likely(src->state == TRACE_STATE_STOPPED))
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100104 return 0;
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200105
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 Tarreau8f9a9702023-01-24 17:48:53 +0100108 return 0;
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200109
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200110 /* 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 Faulet6d80b632021-04-12 11:53:16 +0200120 if (src->arg_def & TRC_ARGS_CHK)
121 check = trace_pick_arg(src->arg_def & TRC_ARGS_CHK, a1, a2, a3, a4);
122
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100123 if (src->arg_def & TRC_ARGS_QCON)
124 qc = trace_pick_arg(src->arg_def & TRC_ARGS_QCON, a1, a2, a3, a4);
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100125
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200126 if (!sess && strm)
127 sess = strm->sess;
Willy Tarreau6e805da2022-03-18 17:37:20 +0100128 else if (!sess && conn && LIST_INLIST(&conn->session_list))
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200129 sess = conn->owner;
Christopher Faulet6d80b632021-04-12 11:53:16 +0200130 else if (!sess && check)
131 sess = check->sess;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200132
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 Faulet6d80b632021-04-12 11:53:16 +0200148 if (check) {
149 srv = check->server;
Christopher Faulete3b25742022-06-08 09:06:15 +0200150 be = (srv ? srv->proxy : NULL);
Christopher Faulet6d80b632021-04-12 11:53:16 +0200151 }
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200152
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 Tarreau4c2ae482019-08-12 15:51:58 +0200162 /* 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 Tarreau8f9a9702023-01-24 17:48:53 +0100167 return 0;
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200168
169 /* TODO: add update of lockon+lockon_ptr here */
170 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
171 }
172
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200173 /* 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 Faulet6d80b632021-04-12 11:53:16 +0200183 case TRACE_LOCKON_CHECK: lockon_ptr = check; break;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200184 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100185 case TRACE_LOCKON_QCON: lockon_ptr = qc; break;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200186 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 Tarreau8f9a9702023-01-24 17:48:53 +0100194 return 0;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200195
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100196 if (*plockptr && !src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
197 *plockptr = lockon_ptr;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200198 }
199
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200200 /* here the trace is running and is tracking a desired item */
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100201 if ((src->report_events & mask) == 0 || level > src->level) {
202 /* tracking did match, and might have to be disabled */
203 return -1;
204 }
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200205
Willy Tarreau8f9a9702023-01-24 17:48:53 +0100206 /* OK trace still enabled */
207 return 1;
208}
209
210/* write a message for the given trace source */
211void __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 Tarreau4c2ae482019-08-12 15:51:58 +0200236
Willy Tarreau419bd492019-08-12 17:27:09 +0200237 /* 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 Tarreau09fb0df2019-08-29 08:40:59 +0200240 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200241 tnum[0] = '0' + tid / 10;
242 tnum[1] = '0' + tid % 10;
243 tnum[2] = '|';
244 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200245 line[words++] = ist(tnum);
246 line[words++] = src->name;
247 line[words++] = ist("|");
Frédéric Lécaillefd183142020-12-02 16:51:00 +0100248 line[words++] = ist2("012345" + level, 1); // "0" to "5"
Willy Tarreau052ad362019-08-29 09:08:36 +0200249 line[words++] = ist("|");
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200250 line[words] = where;
251 if (line[words].len > 13) {
252 line[words].ptr += (line[words].len - 13);
253 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200254 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200255 words++;
256 line[words++] = ist("] ");
257
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100258 if (isttest(ist_func)) {
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200259 line[words++] = ist_func;
260 line[words++] = ist("(): ");
261 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200262
Willy Tarreau3da00262019-08-28 07:03:58 +0200263 if (!cb)
264 cb = src->default_cb;
265
Willy Tarreau370a6942019-08-29 08:24:16 +0200266 if (cb && src->verbosity) {
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200267 /* 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 Tarreau09fb0df2019-08-29 08:40:59 +0200273 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100274 line[words] = ist2(trace_buf.area, trace_buf.data);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200275 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200276 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200277 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 Tarreau09fb0df2019-08-29 08:40:59 +0200283 line[words++] = msg;
Willy Tarreau370a6942019-08-29 08:24:16 +0200284 }
Willy Tarreau419bd492019-08-12 17:27:09 +0200285
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200286 if (src->sink)
Emeric Brun54648852020-07-06 15:54:06 +0200287 sink_write(src->sink, line, words, 0, 0, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200288
289 end:
290 /* check if we need to stop the trace now */
291 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200292 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200293 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
294 }
295 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200296 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200297 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
298 }
299}
300
Willy Tarreau4b36d5e2023-01-24 18:03:07 +0100301/* this callback may be used when no output modification is desired */
302void 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 Tarreau4de2cda2022-03-02 14:53:00 +0100309/* 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 */
313void 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 Tarreau864e8802019-08-08 18:48:12 +0200324struct 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
335const 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100343/* 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 */
351static int trace_parse_statement(char **args, const char **msg)
Willy Tarreau864e8802019-08-08 18:48:12 +0200352{
353 struct trace_source *src;
354 uint64_t *ev_ptr = NULL;
355
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100356 /* no error by default */
357 *msg = NULL;
Willy Tarreau864e8802019-08-08 18:48:12 +0200358
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100370 *msg = trash.area;
371 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200372 }
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100378 *msg = "All traces now stopped";
379 return LOG_NOTICE;
Willy Tarreau864e8802019-08-08 18:48:12 +0200380 }
381
382 src = trace_find_source(args[1]);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100383 if (!src) {
384 *msg = "No such trace source";
385 return LOG_ERR;
386 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200387
388 if (!*args[2]) {
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100389 *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 Tarreau864e8802019-08-08 18:48:12 +0200400 }
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 Tarreau99282dd2019-08-30 07:37:32 +0200424 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 Tarreau864e8802019-08-08 18:48:12 +0200427 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200428 chunk_appendf(&trash, " %c %-12s : %s\n",
Willy Tarreau864e8802019-08-08 18:48:12 +0200429 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100433 *msg = trash.area;
434 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200435 }
436
437 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
438 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200439 if (ev_ptr == &src->pause_events) {
440 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200441 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200442 }
443 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200444 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200445 }
446 else if (ev_ptr == &src->stop_events) {
447 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200448 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200449 }
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100450 *msg = NULL;
Willy Tarreau864e8802019-08-08 18:48:12 +0200451 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100460 if (!ev) {
461 *msg = "No such trace event";
462 return LOG_ERR;
463 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200464
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100484 *msg = trash.area;
485 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200486 }
487
488 if (strcmp(name, "none") == 0)
489 sink = NULL;
490 else {
491 sink = sink_find(name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100492 if (!sink) {
493 *msg = "No such sink";
494 return LOG_ERR;
495 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200496 }
497
498 HA_ATOMIC_STORE(&src->sink, sink);
499 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200500 else if (strcmp(args[2], "level") == 0) {
501 const char *name = args[3];
502
503 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200504 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreaua1f12742020-12-01 09:46:46 +0100505 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 Tarreauaaaf4112019-08-12 17:57:57 +0200508 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200509 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200510 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200511 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200512 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200513 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 Tarreauaaaf4112019-08-12 17:57:57 +0200516 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
517 trash.area[trash.data] = 0;
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100518 *msg = trash.area;
519 return LOG_WARNING;
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200520 }
521
Willy Tarreaua1f12742020-12-01 09:46:46 +0100522 if (strcmp(name, "error") == 0)
523 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_ERROR);
524 else if (strcmp(name, "user") == 0)
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200525 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200526 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 Tarreau2ea549b2019-08-29 08:01:48 +0200530 else if (strcmp(name, "data") == 0)
531 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200532 else if (strcmp(name, "developer") == 0)
533 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100534 else {
535 *msg = "No such trace level";
536 return LOG_ERR;
537 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200538 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200539 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 Faulet6d80b632021-04-12 11:53:16 +0200548 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 Tarreau60e4c9f2019-08-20 19:24:10 +0200552 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100603 *msg = trash.area;
604 return LOG_WARNING;
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200605 }
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 Faulet6d80b632021-04-12 11:53:16 +0200610 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 Tarreau60e4c9f2019-08-20 19:24:10 +0200614 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100662 else {
663 *msg = "Unsupported lock-on criterion";
664 return LOG_ERR;
665 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200666 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200667 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100685 *msg = trash.area;
686 return LOG_WARNING;
Willy Tarreau370a6942019-08-29 08:24:16 +0200687 }
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100694 else {
695 *msg = "No such verbosity level";
696 return LOG_ERR;
697 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200698 } else {
699 for (nd = src->decoding; nd->name && nd->desc; nd++)
700 if (strcmp(name, nd->name) == 0)
701 break;
702
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100703 if (!nd->name || !nd->desc) {
704 *msg = "No such verbosiry level";
705 return LOG_ERR;
706 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200707
708 HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1);
709 }
710 }
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100711 else {
712 *msg = "Unknown trace keyword";
713 return LOG_ERR;
714 }
715 return 0;
716
717}
718
Willy Tarreau9fd05422022-11-16 17:29:12 +0100719/* parse a "trace" statement in the "global" section, returns 1 if a message is returned, otherwise zero */
720static 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100741/* parse the command, returns 1 if a message is returned, otherwise zero */
742static 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 Tarreau864e8802019-08-08 18:48:12 +0200753
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100754 /* total success */
Willy Tarreau864e8802019-08-08 18:48:12 +0200755 return 0;
756}
757
Willy Tarreau85b15752019-08-12 16:44:33 +0200758/* parse the command, returns 1 if a message is returned, otherwise zero */
759static 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 Tarreau99282dd2019-08-30 07:37:32 +0200798 chunk_appendf(&trash, " - event name : report start stop pause\n");
Willy Tarreau85b15752019-08-12 16:44:33 +0200799 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200800 chunk_appendf(&trash, " %-12s : %c %c %c %c\n",
Willy Tarreau85b15752019-08-12 16:44:33 +0200801 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 Tarreau864e8802019-08-08 18:48:12 +0200812static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200813 { { "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 Tarreau864e8802019-08-08 18:48:12 +0200815 {{},}
816}};
817
818INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
819
Willy Tarreau9fd05422022-11-16 17:29:12 +0100820static struct cfg_kw_list cfg_kws = {ILH, {
821 { CFG_GLOBAL, "trace", cfg_parse_trace, KWF_EXPERIMENTAL },
822 { /* END */ },
823}};
824
825INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
826
Willy Tarreau4151c752019-08-08 18:21:26 +0200827/*
828 * Local variables:
829 * c-indent-level: 8
830 * c-basic-offset: 8
831 * End:
832 */