blob: 8d68b664229bd598d24336a62b87c3e5d9e47b73 [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
21#include <common/buffer.h>
22#include <common/compat.h>
23#include <common/config.h>
24#include <common/ist.h>
25#include <common/mini-clist.h>
Willy Tarreau864e8802019-08-08 18:48:12 +020026#include <proto/cli.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020027#include <proto/log.h>
Willy Tarreau864e8802019-08-08 18:48:12 +020028#include <proto/sink.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020029#include <proto/trace.h>
30
31struct list trace_sources = LIST_HEAD_INIT(trace_sources);
Willy Tarreau88ebd402019-08-19 15:55:34 +020032THREAD_LOCAL struct buffer trace_buf = { };
33
34/* allocates the trace buffers. Returns 0 in case of failure. It is safe to
35 * call to call this function multiple times if the size changes.
36 */
37static int alloc_trace_buffers_per_thread()
38{
39 chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize);
40 return !!trash.area;
41}
42
43static void free_trace_buffers_per_thread()
44{
45 chunk_destroy(&trace_buf);
46}
Willy Tarreau4151c752019-08-08 18:21:26 +020047
Willy Tarreau88ebd402019-08-19 15:55:34 +020048REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread);
49REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread);
Willy Tarreau864e8802019-08-08 18:48:12 +020050
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020051/* pick the lowest non-null argument with a non-null arg_def mask */
52static inline const void *trace_pick_arg(uint32_t arg_def, const void *a1, const void *a2, const void *a3, const void *a4)
53{
54 if (arg_def & 0x0000FFFF) {
55 if ((arg_def & 0x000000FF) && a1)
56 return a1;
57 if ((arg_def & 0x0000FF00) && a2)
58 return a2;
59 }
60
61 if (arg_def & 0xFFFF0000) {
62 if ((arg_def & 0x00FF0000) && a3)
63 return a3;
64 if ((arg_def & 0xFF000000) && a4)
65 return a4;
66 }
67
68 return NULL;
69}
70
Willy Tarreau4c2ae482019-08-12 15:51:58 +020071/* write a message for the given trace source */
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020072void __trace(enum trace_level level, uint64_t mask, struct trace_source *src, const struct ist where,
73 const void *a1, const void *a2, const void *a3, const void *a4,
74 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, const struct ist where,
75 const void *a1, const void *a2, const void *a3, const void *a4),
76 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020077{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020078 const struct listener *li = NULL;
79 const struct proxy *fe = NULL;
80 const struct proxy *be = NULL;
81 const struct server *srv = NULL;
82 const struct session *sess = NULL;
83 const struct stream *strm = NULL;
84 const struct connection *conn = NULL;
85 const void *lockon_ptr = NULL;
Willy Tarreau419bd492019-08-12 17:27:09 +020086 struct ist line[8];
87
Willy Tarreau4c2ae482019-08-12 15:51:58 +020088 if (likely(src->state == TRACE_STATE_STOPPED))
89 return;
90
91 /* check that at least one action is interested by this event */
92 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
93 return;
94
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020095 /* retrieve available information from the caller's arguments */
96 if (src->arg_def & TRC_ARGS_CONN)
97 conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4);
98
99 if (src->arg_def & TRC_ARGS_SESS)
100 sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4);
101
102 if (src->arg_def & TRC_ARGS_STRM)
103 strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4);
104
105 if (!sess && strm)
106 sess = strm->sess;
107 else if (!sess && conn)
108 sess = conn->owner;
109
110 if (sess) {
111 fe = sess->fe;
112 li = sess->listener;
113 }
114
115 if (!li && conn)
116 li = objt_listener(conn->target);
117
118 if (li && !fe)
119 fe = li->bind_conf->frontend;
120
121 if (strm) {
122 be = strm->be;
123 srv = strm->srv_conn;
124 }
125
126 if (!srv && conn)
127 srv = objt_server(conn->target);
128
129 if (srv && !be)
130 be = srv->proxy;
131
132 if (!be && conn)
133 be = objt_proxy(conn->target);
134
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200135 /* TODO: add handling of filters here, return if no match (not even update states) */
136
137 /* check if we need to start the trace now */
138 if (src->state == TRACE_STATE_WAITING) {
139 if ((src->start_events & mask) == 0)
140 return;
141
142 /* TODO: add update of lockon+lockon_ptr here */
143 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
144 }
145
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200146 /* we may want to lock on a particular object */
147 if (src->lockon != TRACE_LOCKON_NOTHING) {
148 switch (src->lockon) {
149 case TRACE_LOCKON_BACKEND: lockon_ptr = be; break;
150 case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break;
151 case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break;
152 case TRACE_LOCKON_LISTENER: lockon_ptr = li; break;
153 case TRACE_LOCKON_SERVER: lockon_ptr = srv; break;
154 case TRACE_LOCKON_SESSION: lockon_ptr = sess; break;
155 case TRACE_LOCKON_STREAM: lockon_ptr = strm; break;
156 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
157 case TRACE_LOCKON_ARG1: lockon_ptr = a1; break;
158 case TRACE_LOCKON_ARG2: lockon_ptr = a2; break;
159 case TRACE_LOCKON_ARG3: lockon_ptr = a3; break;
160 case TRACE_LOCKON_ARG4: lockon_ptr = a4; break;
161 default: break; // silence stupid gcc -Wswitch
162 }
163
164 if (src->lockon_ptr && src->lockon_ptr != lockon_ptr)
165 return;
166
167 if (!src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
168 HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr);
169 }
170
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200171 /* here the trace is running and is tracking a desired item */
172
Willy Tarreau5da40882019-08-12 17:57:57 +0200173 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200174 goto end;
175
Willy Tarreau419bd492019-08-12 17:27:09 +0200176 /* log the logging location truncated to 10 chars from the right so that
177 * the line number and the end of the file name are there.
178 */
179 line[0] = ist("[");
180 line[1] = where;
Willy Tarreaub3f7a722019-08-28 09:59:49 +0200181 if (line[1].len > 13) {
182 line[1].ptr += (line[1].len - 13);
183 line[1].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200184 }
185 line[2] = ist("] ");
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200186
Willy Tarreau3da00262019-08-28 07:03:58 +0200187 if (!cb)
188 cb = src->default_cb;
189
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200190 if (cb) {
191 /* decode function passed, we want to pre-fill the
192 * buffer with the message and let the decode function
193 * do its job, possibly even overwriting it.
194 */
195 b_reset(&trace_buf);
196 b_istput(&trace_buf, msg);
197 cb(level, mask, src, where, a1, a2, a3, a4);
198 line[3].ptr = trace_buf.area;
199 line[3].len = trace_buf.data;
200 }
201 else
202 line[3] = msg;
Willy Tarreau419bd492019-08-12 17:27:09 +0200203
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200204 if (src->sink)
Willy Tarreau419bd492019-08-12 17:27:09 +0200205 sink_write(src->sink, line, 4);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200206
207 end:
208 /* check if we need to stop the trace now */
209 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200210 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200211 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
212 }
213 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200214 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200215 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
216 }
217}
218
Willy Tarreau864e8802019-08-08 18:48:12 +0200219struct trace_source *trace_find_source(const char *name)
220{
221 struct trace_source *src;
222 const struct ist iname = ist(name);
223
224 list_for_each_entry(src, &trace_sources, source_link)
225 if (isteq(src->name, iname))
226 return src;
227 return NULL;
228}
229
230const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
231{
232 for (; ev && ev->mask; ev++)
233 if (strcmp(ev->name, name) == 0)
234 return ev;
235 return NULL;
236}
237
238/* parse the command, returns 1 if a message is returned, otherwise zero */
239static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
240{
241 struct trace_source *src;
242 uint64_t *ev_ptr = NULL;
243
244 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
245 return 1;
246
247 if (!*args[1]) {
248 /* no arg => report the list of supported sources as a warning */
249 chunk_printf(&trash,
250 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
251 " [.] 0 : not a source, will immediately stop all traces\n"
252 );
253
254 list_for_each_entry(src, &trace_sources, source_link)
255 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
256
257 trash.area[trash.data] = 0;
258 return cli_msg(appctx, LOG_WARNING, trash.area);
259 }
260
261 if (strcmp(args[1], "0") == 0) {
262 /* emergency stop of all traces */
263 list_for_each_entry(src, &trace_sources, source_link)
264 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
265 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
266 }
267
268 src = trace_find_source(args[1]);
269 if (!src)
270 return cli_err(appctx, "No such trace source");
271
272 if (!*args[2]) {
273 return cli_msg(appctx, LOG_WARNING,
274 "Supported commands:\n"
275 " event : list/enable/disable source-specific event reporting\n"
276 //" filter : list/enable/disable generic filters\n"
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200277 " level : list/set detail level\n"
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200278 " lock : automatic lock on thread/connection/stream/...\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200279 " pause : pause and automatically restart after a specific event\n"
280 " sink : list/set event sinks\n"
281 " start : start immediately or after a specific event\n"
282 " stop : stop immediately or after a specific event\n"
283 );
284 }
285 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
286 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
287 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
288 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
289 const struct trace_event *ev;
290 const char *name = args[3];
291 int neg = 0;
292 int i;
293
294 /* skip prefix '!', '-', '+' and remind negation */
295 while (*name) {
296 if (*name == '!' || *name == '-')
297 neg = 1;
298 else if (*name == '+')
299 neg = 0;
300 else
301 break;
302 name++;
303 }
304
305 if (!*name) {
306 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
307 if (ev_ptr != &src->report_events)
308 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
309 chunk_appendf(&trash, " - none : disable all event types\n");
310 chunk_appendf(&trash, " - any : enable all event types\n");
311 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
312 chunk_appendf(&trash, " %c %-10s : %s\n",
313 trace_event_char(*ev_ptr, src->known_events[i].mask),
314 src->known_events[i].name, src->known_events[i].desc);
315 }
316 trash.area[trash.data] = 0;
317 return cli_msg(appctx, LOG_WARNING, trash.area);
318 }
319
320 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
321 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200322 if (ev_ptr == &src->pause_events) {
323 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200324 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200325 }
326 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200327 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200328 }
329 else if (ev_ptr == &src->stop_events) {
330 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200331 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200332 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200333 return 0;
334 }
335
336 if (strcmp(name, "none") == 0)
337 HA_ATOMIC_STORE(ev_ptr, 0);
338 else if (strcmp(name, "any") == 0)
339 HA_ATOMIC_STORE(ev_ptr, ~0);
340 else {
341 ev = trace_find_event(src->known_events, name);
342 if (!ev)
343 return cli_err(appctx, "No such trace event");
344
345 if (!neg)
346 HA_ATOMIC_OR(ev_ptr, ev->mask);
347 else
348 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
349 }
350 }
351 else if (strcmp(args[2], "sink") == 0) {
352 const char *name = args[3];
353 struct sink *sink;
354
355 if (!*name) {
356 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
357 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
358 list_for_each_entry(sink, &sink_list, sink_list) {
359 chunk_appendf(&trash, " %c %-10s : %s\n",
360 src->sink == sink ? '*' : ' ',
361 sink->name, sink->desc);
362 }
363 trash.area[trash.data] = 0;
364 return cli_msg(appctx, LOG_WARNING, trash.area);
365 }
366
367 if (strcmp(name, "none") == 0)
368 sink = NULL;
369 else {
370 sink = sink_find(name);
371 if (!sink)
372 return cli_err(appctx, "No such sink");
373 }
374
375 HA_ATOMIC_STORE(&src->sink, sink);
376 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200377 else if (strcmp(args[2], "level") == 0) {
378 const char *name = args[3];
379
380 if (!*name) {
381 chunk_printf(&trash, "Supported detail levels for source %s:\n", src->name.ptr);
382 chunk_appendf(&trash, " %c user : information useful to the end user\n",
383 src->level == TRACE_LEVEL_USER ? '*' : ' ');
384 chunk_appendf(&trash, " %c payload : add information relevant to the payload\n",
385 src->level == TRACE_LEVEL_PAYLOAD ? '*' : ' ');
386 chunk_appendf(&trash, " %c proto : add information relevant to the protocol\n",
387 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
388 chunk_appendf(&trash, " %c state : add information relevant to the state machine\n",
389 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
390 chunk_appendf(&trash, " %c developer : add information useful only to the developer\n",
391 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
392 trash.area[trash.data] = 0;
393 return cli_msg(appctx, LOG_WARNING, trash.area);
394 }
395
396 if (strcmp(name, "user") == 0)
397 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
398 else if (strcmp(name, "payload") == 0)
399 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PAYLOAD);
400 else if (strcmp(name, "proto") == 0)
401 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
402 else if (strcmp(name, "state") == 0)
403 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
404 else if (strcmp(name, "developer") == 0)
405 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
406 else
407 return cli_err(appctx, "No such trace level");
408 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200409 else if (strcmp(args[2], "lock") == 0) {
410 const char *name = args[3];
411
412 if (!*name) {
413 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
414 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
415 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
416 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
417
418 if (src->arg_def & TRC_ARGS_CONN)
419 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
420 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
421
422 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
423 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
424 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
425
426 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
427 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
428 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
429
430 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
431 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
432
433 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
434 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
435 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
436
437 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
438 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
439 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
440
441 if (src->arg_def & TRC_ARGS_STRM)
442 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
443 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
444
445 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
446 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
447
448 if (src->lockon_args && src->lockon_args[0].name)
449 chunk_appendf(&trash, " %c %-10s : %s\n",
450 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
451 src->lockon_args[0].name, src->lockon_args[0].desc);
452
453 if (src->lockon_args && src->lockon_args[1].name)
454 chunk_appendf(&trash, " %c %-10s : %s\n",
455 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
456 src->lockon_args[1].name, src->lockon_args[1].desc);
457
458 if (src->lockon_args && src->lockon_args[2].name)
459 chunk_appendf(&trash, " %c %-10s : %s\n",
460 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
461 src->lockon_args[2].name, src->lockon_args[2].desc);
462
463 if (src->lockon_args && src->lockon_args[3].name)
464 chunk_appendf(&trash, " %c %-10s : %s\n",
465 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
466 src->lockon_args[3].name, src->lockon_args[3].desc);
467
468 trash.area[trash.data] = 0;
469 return cli_msg(appctx, LOG_WARNING, trash.area);
470 }
471 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
472 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
473 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
474 }
475 else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) {
476 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
477 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
478 }
479 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
480 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
481 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
482 }
483 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
484 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
485 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
486 }
487 else if (strcmp(name, "nothing") == 0) {
488 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
489 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
490 }
491 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
492 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
493 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
494 }
495 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
496 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
497 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
498 }
499 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
500 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
501 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
502 }
503 else if (strcmp(name, "thread") == 0) {
504 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
505 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
506 }
507 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
508 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
509 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
510 }
511 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
512 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
513 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
514 }
515 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
516 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
517 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
518 }
519 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
520 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
521 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
522 }
523 else
524 return cli_err(appctx, "Unsupported lock-on criterion");
525 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200526 else
527 return cli_err(appctx, "Unknown trace keyword");
528
529 return 0;
530}
531
Willy Tarreau85b15752019-08-12 16:44:33 +0200532/* parse the command, returns 1 if a message is returned, otherwise zero */
533static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
534{
535 struct trace_source *src;
536 const struct sink *sink;
537 int i;
538
539 args++; // make args[1] the 1st arg
540
541 if (!*args[1]) {
542 /* no arg => report the list of supported sources */
543 chunk_printf(&trash,
544 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
545 );
546
547 list_for_each_entry(src, &trace_sources, source_link) {
548 sink = src->sink;
549 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
550 trace_state_char(src->state), src->name.ptr,
551 sink ? sink->name : "none",
552 sink ? sink->ctx.dropped : 0,
553 src->desc);
554 }
555
556 trash.area[trash.data] = 0;
557 return cli_msg(appctx, LOG_INFO, trash.area);
558 }
559
560 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
561 return 1;
562
563 src = trace_find_source(args[1]);
564 if (!src)
565 return cli_err(appctx, "No such trace source");
566
567 sink = src->sink;
568 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
569 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
570 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
571
572 chunk_appendf(&trash, " - event name : report start stop pause\n");
573 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
574 chunk_appendf(&trash, " %-10s : %c %c %c %c\n",
575 src->known_events[i].name,
576 trace_event_char(src->report_events, src->known_events[i].mask),
577 trace_event_char(src->start_events, src->known_events[i].mask),
578 trace_event_char(src->stop_events, src->known_events[i].mask),
579 trace_event_char(src->pause_events, src->known_events[i].mask));
580 }
581
582 trash.area[trash.data] = 0;
583 return cli_msg(appctx, LOG_WARNING, trash.area);
584}
585
Willy Tarreau864e8802019-08-08 18:48:12 +0200586static struct cli_kw_list cli_kws = {{ },{
587 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200588 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200589 {{},}
590}};
591
592INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
593
Willy Tarreau4151c752019-08-08 18:21:26 +0200594/*
595 * Local variables:
596 * c-indent-level: 8
597 * c-basic-offset: 8
598 * End:
599 */