blob: 9c0b3f2057311002dd76fcab004e84dabd233dfc [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 Tarreau4c2ae482019-08-12 15:51:58 +020073/* write a message for the given trace source */
Willy Tarreau09fb0df2019-08-29 08:40:59 +020074void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
75 const struct ist where, const char *func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020076 const void *a1, const void *a2, const void *a3, const void *a4,
Willy Tarreau09fb0df2019-08-29 08:40:59 +020077 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
78 const struct ist where, const struct ist func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020079 const void *a1, const void *a2, const void *a3, const void *a4),
80 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020081{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020082 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 Faulet6d80b632021-04-12 11:53:16 +020089 const struct check *check = NULL;
Amaury Denoyellebaea9642021-12-21 11:26:02 +010090#ifdef USE_QUIC
91 const struct quic_conn *qc = NULL;
92#endif
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020093 const void *lockon_ptr = NULL;
Willy Tarreau09fb0df2019-08-29 08:40:59 +020094 struct ist ist_func = ist(func);
Willy Tarreaua9f5b962019-08-28 10:08:58 +020095 char tnum[4];
Willy Tarreau052ad362019-08-29 09:08:36 +020096 struct ist line[12];
Willy Tarreau09fb0df2019-08-29 08:40:59 +020097 int words = 0;
Willy Tarreau419bd492019-08-12 17:27:09 +020098
Willy Tarreau4c2ae482019-08-12 15:51:58 +020099 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 Tarreaud8b99ed2019-08-21 17:05:46 +0200106 /* 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 Faulet6d80b632021-04-12 11:53:16 +0200116 if (src->arg_def & TRC_ARGS_CHK)
117 check = trace_pick_arg(src->arg_def & TRC_ARGS_CHK, a1, a2, a3, a4);
118
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100119#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 Tarreaud8b99ed2019-08-21 17:05:46 +0200124 if (!sess && strm)
125 sess = strm->sess;
Willy Tarreau6e805da2022-03-18 17:37:20 +0100126 else if (!sess && conn && LIST_INLIST(&conn->session_list))
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200127 sess = conn->owner;
Christopher Faulet6d80b632021-04-12 11:53:16 +0200128 else if (!sess && check)
129 sess = check->sess;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200130
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 Faulet6d80b632021-04-12 11:53:16 +0200146 if (check) {
147 srv = check->server;
Christopher Faulete3b25742022-06-08 09:06:15 +0200148 be = (srv ? srv->proxy : NULL);
Christopher Faulet6d80b632021-04-12 11:53:16 +0200149 }
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200150
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 Tarreau4c2ae482019-08-12 15:51:58 +0200160 /* 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 Tarreaud8b99ed2019-08-21 17:05:46 +0200171 /* 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 Faulet6d80b632021-04-12 11:53:16 +0200181 case TRACE_LOCKON_CHECK: lockon_ptr = check; break;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200182 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100183#ifdef USE_QUIC
184 case TRACE_LOCKON_QCON: lockon_ptr = qc; break;
185#endif
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)
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 Tarreau4c2ae482019-08-12 15:51:58 +0200200 /* here the trace is running and is tracking a desired item */
201
Willy Tarreau5da40882019-08-12 17:57:57 +0200202 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200203 goto end;
204
Willy Tarreau419bd492019-08-12 17:27:09 +0200205 /* 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 Tarreau09fb0df2019-08-29 08:40:59 +0200208 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200209 tnum[0] = '0' + tid / 10;
210 tnum[1] = '0' + tid % 10;
211 tnum[2] = '|';
212 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200213 line[words++] = ist(tnum);
214 line[words++] = src->name;
215 line[words++] = ist("|");
Frédéric Lécaillefd183142020-12-02 16:51:00 +0100216 line[words++] = ist2("012345" + level, 1); // "0" to "5"
Willy Tarreau052ad362019-08-29 09:08:36 +0200217 line[words++] = ist("|");
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200218 line[words] = where;
219 if (line[words].len > 13) {
220 line[words].ptr += (line[words].len - 13);
221 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200222 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200223 words++;
224 line[words++] = ist("] ");
225
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100226 if (isttest(ist_func)) {
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200227 line[words++] = ist_func;
228 line[words++] = ist("(): ");
229 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200230
Willy Tarreau3da00262019-08-28 07:03:58 +0200231 if (!cb)
232 cb = src->default_cb;
233
Willy Tarreau370a6942019-08-29 08:24:16 +0200234 if (cb && src->verbosity) {
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200235 /* 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 Tarreau09fb0df2019-08-29 08:40:59 +0200241 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100242 line[words] = ist2(trace_buf.area, trace_buf.data);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200243 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200244 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200245 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 Tarreau09fb0df2019-08-29 08:40:59 +0200251 line[words++] = msg;
Willy Tarreau370a6942019-08-29 08:24:16 +0200252 }
Willy Tarreau419bd492019-08-12 17:27:09 +0200253
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200254 if (src->sink)
Emeric Brun54648852020-07-06 15:54:06 +0200255 sink_write(src->sink, line, words, 0, 0, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200256
257 end:
258 /* check if we need to stop the trace now */
259 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200260 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200261 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
262 }
263 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200264 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200265 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
266 }
267}
268
Willy Tarreau4de2cda2022-03-02 14:53:00 +0100269/* 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 */
273void 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 Tarreau864e8802019-08-08 18:48:12 +0200284struct 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
295const 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100303/* 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 */
311static int trace_parse_statement(char **args, const char **msg)
Willy Tarreau864e8802019-08-08 18:48:12 +0200312{
313 struct trace_source *src;
314 uint64_t *ev_ptr = NULL;
315
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100316 /* no error by default */
317 *msg = NULL;
Willy Tarreau864e8802019-08-08 18:48:12 +0200318
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100330 *msg = trash.area;
331 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200332 }
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100338 *msg = "All traces now stopped";
339 return LOG_NOTICE;
Willy Tarreau864e8802019-08-08 18:48:12 +0200340 }
341
342 src = trace_find_source(args[1]);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100343 if (!src) {
344 *msg = "No such trace source";
345 return LOG_ERR;
346 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200347
348 if (!*args[2]) {
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100349 *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 Tarreau864e8802019-08-08 18:48:12 +0200360 }
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 Tarreau99282dd2019-08-30 07:37:32 +0200384 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 Tarreau864e8802019-08-08 18:48:12 +0200387 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200388 chunk_appendf(&trash, " %c %-12s : %s\n",
Willy Tarreau864e8802019-08-08 18:48:12 +0200389 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100393 *msg = trash.area;
394 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200395 }
396
397 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
398 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200399 if (ev_ptr == &src->pause_events) {
400 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200401 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200402 }
403 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200404 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200405 }
406 else if (ev_ptr == &src->stop_events) {
407 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200408 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200409 }
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100410 *msg = NULL;
Willy Tarreau864e8802019-08-08 18:48:12 +0200411 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100420 if (!ev) {
421 *msg = "No such trace event";
422 return LOG_ERR;
423 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200424
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100444 *msg = trash.area;
445 return LOG_WARNING;
Willy Tarreau864e8802019-08-08 18:48:12 +0200446 }
447
448 if (strcmp(name, "none") == 0)
449 sink = NULL;
450 else {
451 sink = sink_find(name);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100452 if (!sink) {
453 *msg = "No such sink";
454 return LOG_ERR;
455 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200456 }
457
458 HA_ATOMIC_STORE(&src->sink, sink);
459 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200460 else if (strcmp(args[2], "level") == 0) {
461 const char *name = args[3];
462
463 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200464 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreaua1f12742020-12-01 09:46:46 +0100465 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 Tarreauaaaf4112019-08-12 17:57:57 +0200468 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200469 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200470 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200471 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200472 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200473 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 Tarreauaaaf4112019-08-12 17:57:57 +0200476 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
477 trash.area[trash.data] = 0;
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100478 *msg = trash.area;
479 return LOG_WARNING;
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200480 }
481
Willy Tarreaua1f12742020-12-01 09:46:46 +0100482 if (strcmp(name, "error") == 0)
483 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_ERROR);
484 else if (strcmp(name, "user") == 0)
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200485 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200486 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 Tarreau2ea549b2019-08-29 08:01:48 +0200490 else if (strcmp(name, "data") == 0)
491 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200492 else if (strcmp(name, "developer") == 0)
493 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100494 else {
495 *msg = "No such trace level";
496 return LOG_ERR;
497 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200498 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200499 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 Faulet6d80b632021-04-12 11:53:16 +0200508 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 Tarreau60e4c9f2019-08-20 19:24:10 +0200512 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100563 *msg = trash.area;
564 return LOG_WARNING;
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200565 }
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 Faulet6d80b632021-04-12 11:53:16 +0200570 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 Tarreau60e4c9f2019-08-20 19:24:10 +0200574 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100622 else {
623 *msg = "Unsupported lock-on criterion";
624 return LOG_ERR;
625 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200626 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200627 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100645 *msg = trash.area;
646 return LOG_WARNING;
Willy Tarreau370a6942019-08-29 08:24:16 +0200647 }
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 Tarreauc11f1cd2022-11-16 17:18:04 +0100654 else {
655 *msg = "No such verbosity level";
656 return LOG_ERR;
657 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200658 } else {
659 for (nd = src->decoding; nd->name && nd->desc; nd++)
660 if (strcmp(name, nd->name) == 0)
661 break;
662
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100663 if (!nd->name || !nd->desc) {
664 *msg = "No such verbosiry level";
665 return LOG_ERR;
666 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200667
668 HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1);
669 }
670 }
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100671 else {
672 *msg = "Unknown trace keyword";
673 return LOG_ERR;
674 }
675 return 0;
676
677}
678
Willy Tarreau9fd05422022-11-16 17:29:12 +0100679/* parse a "trace" statement in the "global" section, returns 1 if a message is returned, otherwise zero */
680static 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 Tarreauc11f1cd2022-11-16 17:18:04 +0100701/* parse the command, returns 1 if a message is returned, otherwise zero */
702static 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 Tarreau864e8802019-08-08 18:48:12 +0200713
Willy Tarreauc11f1cd2022-11-16 17:18:04 +0100714 /* total success */
Willy Tarreau864e8802019-08-08 18:48:12 +0200715 return 0;
716}
717
Willy Tarreau85b15752019-08-12 16:44:33 +0200718/* parse the command, returns 1 if a message is returned, otherwise zero */
719static 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 Tarreau99282dd2019-08-30 07:37:32 +0200758 chunk_appendf(&trash, " - event name : report start stop pause\n");
Willy Tarreau85b15752019-08-12 16:44:33 +0200759 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200760 chunk_appendf(&trash, " %-12s : %c %c %c %c\n",
Willy Tarreau85b15752019-08-12 16:44:33 +0200761 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 Tarreau864e8802019-08-08 18:48:12 +0200772static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200773 { { "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 Tarreau864e8802019-08-08 18:48:12 +0200775 {{},}
776}};
777
778INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
779
Willy Tarreau9fd05422022-11-16 17:29:12 +0100780static struct cfg_kw_list cfg_kws = {ILH, {
781 { CFG_GLOBAL, "trace", cfg_parse_trace, KWF_EXPERIMENTAL },
782 { /* END */ },
783}};
784
785INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
786
Willy Tarreau4151c752019-08-08 18:21:26 +0200787/*
788 * Local variables:
789 * c-indent-level: 8
790 * c-basic-offset: 8
791 * End:
792 */