blob: 039bf3a706551b25a264dd04ea91135571400e46 [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;
181 if (line[1].len > 10) {
182 line[1].ptr += (line[1].len - 10);
183 line[1].len = 10;
184 }
185 line[2] = ist("] ");
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200186
187 if (cb) {
188 /* decode function passed, we want to pre-fill the
189 * buffer with the message and let the decode function
190 * do its job, possibly even overwriting it.
191 */
192 b_reset(&trace_buf);
193 b_istput(&trace_buf, msg);
194 cb(level, mask, src, where, a1, a2, a3, a4);
195 line[3].ptr = trace_buf.area;
196 line[3].len = trace_buf.data;
197 }
198 else
199 line[3] = msg;
Willy Tarreau419bd492019-08-12 17:27:09 +0200200
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200201 if (src->sink)
Willy Tarreau419bd492019-08-12 17:27:09 +0200202 sink_write(src->sink, line, 4);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200203
204 end:
205 /* check if we need to stop the trace now */
206 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200207 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200208 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
209 }
210 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200211 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200212 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
213 }
214}
215
Willy Tarreau864e8802019-08-08 18:48:12 +0200216struct trace_source *trace_find_source(const char *name)
217{
218 struct trace_source *src;
219 const struct ist iname = ist(name);
220
221 list_for_each_entry(src, &trace_sources, source_link)
222 if (isteq(src->name, iname))
223 return src;
224 return NULL;
225}
226
227const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
228{
229 for (; ev && ev->mask; ev++)
230 if (strcmp(ev->name, name) == 0)
231 return ev;
232 return NULL;
233}
234
235/* parse the command, returns 1 if a message is returned, otherwise zero */
236static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
237{
238 struct trace_source *src;
239 uint64_t *ev_ptr = NULL;
240
241 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
242 return 1;
243
244 if (!*args[1]) {
245 /* no arg => report the list of supported sources as a warning */
246 chunk_printf(&trash,
247 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
248 " [.] 0 : not a source, will immediately stop all traces\n"
249 );
250
251 list_for_each_entry(src, &trace_sources, source_link)
252 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
253
254 trash.area[trash.data] = 0;
255 return cli_msg(appctx, LOG_WARNING, trash.area);
256 }
257
258 if (strcmp(args[1], "0") == 0) {
259 /* emergency stop of all traces */
260 list_for_each_entry(src, &trace_sources, source_link)
261 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
262 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
263 }
264
265 src = trace_find_source(args[1]);
266 if (!src)
267 return cli_err(appctx, "No such trace source");
268
269 if (!*args[2]) {
270 return cli_msg(appctx, LOG_WARNING,
271 "Supported commands:\n"
272 " event : list/enable/disable source-specific event reporting\n"
273 //" filter : list/enable/disable generic filters\n"
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200274 " level : list/set detail level\n"
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200275 " lock : automatic lock on thread/connection/stream/...\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200276 " pause : pause and automatically restart after a specific event\n"
277 " sink : list/set event sinks\n"
278 " start : start immediately or after a specific event\n"
279 " stop : stop immediately or after a specific event\n"
280 );
281 }
282 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
283 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
284 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
285 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
286 const struct trace_event *ev;
287 const char *name = args[3];
288 int neg = 0;
289 int i;
290
291 /* skip prefix '!', '-', '+' and remind negation */
292 while (*name) {
293 if (*name == '!' || *name == '-')
294 neg = 1;
295 else if (*name == '+')
296 neg = 0;
297 else
298 break;
299 name++;
300 }
301
302 if (!*name) {
303 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
304 if (ev_ptr != &src->report_events)
305 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
306 chunk_appendf(&trash, " - none : disable all event types\n");
307 chunk_appendf(&trash, " - any : enable all event types\n");
308 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
309 chunk_appendf(&trash, " %c %-10s : %s\n",
310 trace_event_char(*ev_ptr, src->known_events[i].mask),
311 src->known_events[i].name, src->known_events[i].desc);
312 }
313 trash.area[trash.data] = 0;
314 return cli_msg(appctx, LOG_WARNING, trash.area);
315 }
316
317 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
318 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200319 if (ev_ptr == &src->pause_events) {
320 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200321 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200322 }
323 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200324 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200325 }
326 else if (ev_ptr == &src->stop_events) {
327 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200328 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200329 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200330 return 0;
331 }
332
333 if (strcmp(name, "none") == 0)
334 HA_ATOMIC_STORE(ev_ptr, 0);
335 else if (strcmp(name, "any") == 0)
336 HA_ATOMIC_STORE(ev_ptr, ~0);
337 else {
338 ev = trace_find_event(src->known_events, name);
339 if (!ev)
340 return cli_err(appctx, "No such trace event");
341
342 if (!neg)
343 HA_ATOMIC_OR(ev_ptr, ev->mask);
344 else
345 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
346 }
347 }
348 else if (strcmp(args[2], "sink") == 0) {
349 const char *name = args[3];
350 struct sink *sink;
351
352 if (!*name) {
353 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
354 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
355 list_for_each_entry(sink, &sink_list, sink_list) {
356 chunk_appendf(&trash, " %c %-10s : %s\n",
357 src->sink == sink ? '*' : ' ',
358 sink->name, sink->desc);
359 }
360 trash.area[trash.data] = 0;
361 return cli_msg(appctx, LOG_WARNING, trash.area);
362 }
363
364 if (strcmp(name, "none") == 0)
365 sink = NULL;
366 else {
367 sink = sink_find(name);
368 if (!sink)
369 return cli_err(appctx, "No such sink");
370 }
371
372 HA_ATOMIC_STORE(&src->sink, sink);
373 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200374 else if (strcmp(args[2], "level") == 0) {
375 const char *name = args[3];
376
377 if (!*name) {
378 chunk_printf(&trash, "Supported detail levels for source %s:\n", src->name.ptr);
379 chunk_appendf(&trash, " %c user : information useful to the end user\n",
380 src->level == TRACE_LEVEL_USER ? '*' : ' ');
381 chunk_appendf(&trash, " %c payload : add information relevant to the payload\n",
382 src->level == TRACE_LEVEL_PAYLOAD ? '*' : ' ');
383 chunk_appendf(&trash, " %c proto : add information relevant to the protocol\n",
384 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
385 chunk_appendf(&trash, " %c state : add information relevant to the state machine\n",
386 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
387 chunk_appendf(&trash, " %c developer : add information useful only to the developer\n",
388 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
389 trash.area[trash.data] = 0;
390 return cli_msg(appctx, LOG_WARNING, trash.area);
391 }
392
393 if (strcmp(name, "user") == 0)
394 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
395 else if (strcmp(name, "payload") == 0)
396 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PAYLOAD);
397 else if (strcmp(name, "proto") == 0)
398 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
399 else if (strcmp(name, "state") == 0)
400 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
401 else if (strcmp(name, "developer") == 0)
402 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
403 else
404 return cli_err(appctx, "No such trace level");
405 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200406 else if (strcmp(args[2], "lock") == 0) {
407 const char *name = args[3];
408
409 if (!*name) {
410 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
411 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
412 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
413 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
414
415 if (src->arg_def & TRC_ARGS_CONN)
416 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
417 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
418
419 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
420 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
421 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
422
423 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
424 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
425 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
426
427 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
428 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
429
430 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
431 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
432 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
433
434 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
435 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
436 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
437
438 if (src->arg_def & TRC_ARGS_STRM)
439 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
440 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
441
442 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
443 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
444
445 if (src->lockon_args && src->lockon_args[0].name)
446 chunk_appendf(&trash, " %c %-10s : %s\n",
447 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
448 src->lockon_args[0].name, src->lockon_args[0].desc);
449
450 if (src->lockon_args && src->lockon_args[1].name)
451 chunk_appendf(&trash, " %c %-10s : %s\n",
452 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
453 src->lockon_args[1].name, src->lockon_args[1].desc);
454
455 if (src->lockon_args && src->lockon_args[2].name)
456 chunk_appendf(&trash, " %c %-10s : %s\n",
457 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
458 src->lockon_args[2].name, src->lockon_args[2].desc);
459
460 if (src->lockon_args && src->lockon_args[3].name)
461 chunk_appendf(&trash, " %c %-10s : %s\n",
462 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
463 src->lockon_args[3].name, src->lockon_args[3].desc);
464
465 trash.area[trash.data] = 0;
466 return cli_msg(appctx, LOG_WARNING, trash.area);
467 }
468 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
469 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
470 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
471 }
472 else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) {
473 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
474 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
475 }
476 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
477 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
478 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
479 }
480 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
481 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
482 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
483 }
484 else if (strcmp(name, "nothing") == 0) {
485 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
486 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
487 }
488 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
489 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
490 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
491 }
492 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
493 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
494 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
495 }
496 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
497 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
498 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
499 }
500 else if (strcmp(name, "thread") == 0) {
501 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
502 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
503 }
504 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
505 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
506 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
507 }
508 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
509 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
510 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
511 }
512 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
513 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
514 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
515 }
516 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
517 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
518 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
519 }
520 else
521 return cli_err(appctx, "Unsupported lock-on criterion");
522 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200523 else
524 return cli_err(appctx, "Unknown trace keyword");
525
526 return 0;
527}
528
Willy Tarreau85b15752019-08-12 16:44:33 +0200529/* parse the command, returns 1 if a message is returned, otherwise zero */
530static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
531{
532 struct trace_source *src;
533 const struct sink *sink;
534 int i;
535
536 args++; // make args[1] the 1st arg
537
538 if (!*args[1]) {
539 /* no arg => report the list of supported sources */
540 chunk_printf(&trash,
541 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
542 );
543
544 list_for_each_entry(src, &trace_sources, source_link) {
545 sink = src->sink;
546 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
547 trace_state_char(src->state), src->name.ptr,
548 sink ? sink->name : "none",
549 sink ? sink->ctx.dropped : 0,
550 src->desc);
551 }
552
553 trash.area[trash.data] = 0;
554 return cli_msg(appctx, LOG_INFO, trash.area);
555 }
556
557 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
558 return 1;
559
560 src = trace_find_source(args[1]);
561 if (!src)
562 return cli_err(appctx, "No such trace source");
563
564 sink = src->sink;
565 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
566 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
567 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
568
569 chunk_appendf(&trash, " - event name : report start stop pause\n");
570 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
571 chunk_appendf(&trash, " %-10s : %c %c %c %c\n",
572 src->known_events[i].name,
573 trace_event_char(src->report_events, src->known_events[i].mask),
574 trace_event_char(src->start_events, src->known_events[i].mask),
575 trace_event_char(src->stop_events, src->known_events[i].mask),
576 trace_event_char(src->pause_events, src->known_events[i].mask));
577 }
578
579 trash.area[trash.data] = 0;
580 return cli_msg(appctx, LOG_WARNING, trash.area);
581}
582
Willy Tarreau864e8802019-08-08 18:48:12 +0200583static struct cli_kw_list cli_kws = {{ },{
584 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200585 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200586 {{},}
587}};
588
589INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
590
Willy Tarreau4151c752019-08-08 18:21:26 +0200591/*
592 * Local variables:
593 * c-indent-level: 8
594 * c-basic-offset: 8
595 * End:
596 */