blob: 63ad1f9b4711062e639b0583384d55a2d21ed207 [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 Tarreau09fb0df2019-08-29 08:40:59 +020072void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
73 const struct ist where, const char *func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020074 const void *a1, const void *a2, const void *a3, const void *a4,
Willy Tarreau09fb0df2019-08-29 08:40:59 +020075 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
76 const struct ist where, const struct ist func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020077 const void *a1, const void *a2, const void *a3, const void *a4),
78 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020079{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020080 const struct listener *li = NULL;
81 const struct proxy *fe = NULL;
82 const struct proxy *be = NULL;
83 const struct server *srv = NULL;
84 const struct session *sess = NULL;
85 const struct stream *strm = NULL;
86 const struct connection *conn = NULL;
87 const void *lockon_ptr = NULL;
Willy Tarreau09fb0df2019-08-29 08:40:59 +020088 struct ist ist_func = ist(func);
Willy Tarreaua9f5b962019-08-28 10:08:58 +020089 char tnum[4];
Willy Tarreau09fb0df2019-08-29 08:40:59 +020090 struct ist line[10];
91 int words = 0;
Willy Tarreau419bd492019-08-12 17:27:09 +020092
Willy Tarreau4c2ae482019-08-12 15:51:58 +020093 if (likely(src->state == TRACE_STATE_STOPPED))
94 return;
95
96 /* check that at least one action is interested by this event */
97 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
98 return;
99
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200100 /* retrieve available information from the caller's arguments */
101 if (src->arg_def & TRC_ARGS_CONN)
102 conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4);
103
104 if (src->arg_def & TRC_ARGS_SESS)
105 sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4);
106
107 if (src->arg_def & TRC_ARGS_STRM)
108 strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4);
109
110 if (!sess && strm)
111 sess = strm->sess;
112 else if (!sess && conn)
113 sess = conn->owner;
114
115 if (sess) {
116 fe = sess->fe;
117 li = sess->listener;
118 }
119
120 if (!li && conn)
121 li = objt_listener(conn->target);
122
123 if (li && !fe)
124 fe = li->bind_conf->frontend;
125
126 if (strm) {
127 be = strm->be;
128 srv = strm->srv_conn;
129 }
130
131 if (!srv && conn)
132 srv = objt_server(conn->target);
133
134 if (srv && !be)
135 be = srv->proxy;
136
137 if (!be && conn)
138 be = objt_proxy(conn->target);
139
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200140 /* TODO: add handling of filters here, return if no match (not even update states) */
141
142 /* check if we need to start the trace now */
143 if (src->state == TRACE_STATE_WAITING) {
144 if ((src->start_events & mask) == 0)
145 return;
146
147 /* TODO: add update of lockon+lockon_ptr here */
148 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
149 }
150
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200151 /* we may want to lock on a particular object */
152 if (src->lockon != TRACE_LOCKON_NOTHING) {
153 switch (src->lockon) {
154 case TRACE_LOCKON_BACKEND: lockon_ptr = be; break;
155 case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break;
156 case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break;
157 case TRACE_LOCKON_LISTENER: lockon_ptr = li; break;
158 case TRACE_LOCKON_SERVER: lockon_ptr = srv; break;
159 case TRACE_LOCKON_SESSION: lockon_ptr = sess; break;
160 case TRACE_LOCKON_STREAM: lockon_ptr = strm; break;
161 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
162 case TRACE_LOCKON_ARG1: lockon_ptr = a1; break;
163 case TRACE_LOCKON_ARG2: lockon_ptr = a2; break;
164 case TRACE_LOCKON_ARG3: lockon_ptr = a3; break;
165 case TRACE_LOCKON_ARG4: lockon_ptr = a4; break;
166 default: break; // silence stupid gcc -Wswitch
167 }
168
169 if (src->lockon_ptr && src->lockon_ptr != lockon_ptr)
170 return;
171
172 if (!src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
173 HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr);
174 }
175
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200176 /* here the trace is running and is tracking a desired item */
177
Willy Tarreau5da40882019-08-12 17:57:57 +0200178 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200179 goto end;
180
Willy Tarreau419bd492019-08-12 17:27:09 +0200181 /* log the logging location truncated to 10 chars from the right so that
182 * the line number and the end of the file name are there.
183 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200184 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200185 tnum[0] = '0' + tid / 10;
186 tnum[1] = '0' + tid % 10;
187 tnum[2] = '|';
188 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200189 line[words++] = ist(tnum);
190 line[words++] = src->name;
191 line[words++] = ist("|");
192 line[words] = where;
193 if (line[words].len > 13) {
194 line[words].ptr += (line[words].len - 13);
195 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200196 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200197 words++;
198 line[words++] = ist("] ");
199
200 if (ist_func.ptr) {
201 line[words++] = ist_func;
202 line[words++] = ist("(): ");
203 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200204
Willy Tarreau3da00262019-08-28 07:03:58 +0200205 if (!cb)
206 cb = src->default_cb;
207
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200208 if (cb) {
209 /* decode function passed, we want to pre-fill the
210 * buffer with the message and let the decode function
211 * do its job, possibly even overwriting it.
212 */
213 b_reset(&trace_buf);
214 b_istput(&trace_buf, msg);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200215 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
216 line[words].ptr = trace_buf.area;
217 line[words].len = trace_buf.data;
218 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200219 }
220 else
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200221 line[words++] = msg;
Willy Tarreau419bd492019-08-12 17:27:09 +0200222
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200223 if (src->sink)
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200224 sink_write(src->sink, line, words);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200225
226 end:
227 /* check if we need to stop the trace now */
228 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200229 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200230 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
231 }
232 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200233 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200234 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
235 }
236}
237
Willy Tarreau864e8802019-08-08 18:48:12 +0200238struct trace_source *trace_find_source(const char *name)
239{
240 struct trace_source *src;
241 const struct ist iname = ist(name);
242
243 list_for_each_entry(src, &trace_sources, source_link)
244 if (isteq(src->name, iname))
245 return src;
246 return NULL;
247}
248
249const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
250{
251 for (; ev && ev->mask; ev++)
252 if (strcmp(ev->name, name) == 0)
253 return ev;
254 return NULL;
255}
256
257/* parse the command, returns 1 if a message is returned, otherwise zero */
258static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
259{
260 struct trace_source *src;
261 uint64_t *ev_ptr = NULL;
262
263 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
264 return 1;
265
266 if (!*args[1]) {
267 /* no arg => report the list of supported sources as a warning */
268 chunk_printf(&trash,
269 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
270 " [.] 0 : not a source, will immediately stop all traces\n"
271 );
272
273 list_for_each_entry(src, &trace_sources, source_link)
274 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
275
276 trash.area[trash.data] = 0;
277 return cli_msg(appctx, LOG_WARNING, trash.area);
278 }
279
280 if (strcmp(args[1], "0") == 0) {
281 /* emergency stop of all traces */
282 list_for_each_entry(src, &trace_sources, source_link)
283 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
284 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
285 }
286
287 src = trace_find_source(args[1]);
288 if (!src)
289 return cli_err(appctx, "No such trace source");
290
291 if (!*args[2]) {
292 return cli_msg(appctx, LOG_WARNING,
293 "Supported commands:\n"
294 " event : list/enable/disable source-specific event reporting\n"
295 //" filter : list/enable/disable generic filters\n"
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200296 " level : list/set trace reporting level\n"
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200297 " lock : automatic lock on thread/connection/stream/...\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200298 " pause : pause and automatically restart after a specific event\n"
299 " sink : list/set event sinks\n"
300 " start : start immediately or after a specific event\n"
301 " stop : stop immediately or after a specific event\n"
302 );
303 }
304 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
305 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
306 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
307 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
308 const struct trace_event *ev;
309 const char *name = args[3];
310 int neg = 0;
311 int i;
312
313 /* skip prefix '!', '-', '+' and remind negation */
314 while (*name) {
315 if (*name == '!' || *name == '-')
316 neg = 1;
317 else if (*name == '+')
318 neg = 0;
319 else
320 break;
321 name++;
322 }
323
324 if (!*name) {
325 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
326 if (ev_ptr != &src->report_events)
327 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
328 chunk_appendf(&trash, " - none : disable all event types\n");
329 chunk_appendf(&trash, " - any : enable all event types\n");
330 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
331 chunk_appendf(&trash, " %c %-10s : %s\n",
332 trace_event_char(*ev_ptr, src->known_events[i].mask),
333 src->known_events[i].name, src->known_events[i].desc);
334 }
335 trash.area[trash.data] = 0;
336 return cli_msg(appctx, LOG_WARNING, trash.area);
337 }
338
339 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
340 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200341 if (ev_ptr == &src->pause_events) {
342 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200343 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200344 }
345 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200346 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200347 }
348 else if (ev_ptr == &src->stop_events) {
349 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200350 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200351 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200352 return 0;
353 }
354
355 if (strcmp(name, "none") == 0)
356 HA_ATOMIC_STORE(ev_ptr, 0);
357 else if (strcmp(name, "any") == 0)
358 HA_ATOMIC_STORE(ev_ptr, ~0);
359 else {
360 ev = trace_find_event(src->known_events, name);
361 if (!ev)
362 return cli_err(appctx, "No such trace event");
363
364 if (!neg)
365 HA_ATOMIC_OR(ev_ptr, ev->mask);
366 else
367 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
368 }
369 }
370 else if (strcmp(args[2], "sink") == 0) {
371 const char *name = args[3];
372 struct sink *sink;
373
374 if (!*name) {
375 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
376 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
377 list_for_each_entry(sink, &sink_list, sink_list) {
378 chunk_appendf(&trash, " %c %-10s : %s\n",
379 src->sink == sink ? '*' : ' ',
380 sink->name, sink->desc);
381 }
382 trash.area[trash.data] = 0;
383 return cli_msg(appctx, LOG_WARNING, trash.area);
384 }
385
386 if (strcmp(name, "none") == 0)
387 sink = NULL;
388 else {
389 sink = sink_find(name);
390 if (!sink)
391 return cli_err(appctx, "No such sink");
392 }
393
394 HA_ATOMIC_STORE(&src->sink, sink);
395 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200396 else if (strcmp(args[2], "level") == 0) {
397 const char *name = args[3];
398
399 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200400 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200401 chunk_appendf(&trash, " %c user : information useful to the end user\n",
402 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200403 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200404 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200405 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200406 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200407 chunk_appendf(&trash, " %c data : also report data transfers\n",
408 src->level == TRACE_LEVEL_DATA ? '*' : ' ');
409 chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200410 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
411 trash.area[trash.data] = 0;
412 return cli_msg(appctx, LOG_WARNING, trash.area);
413 }
414
415 if (strcmp(name, "user") == 0)
416 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200417 else if (strcmp(name, "proto") == 0)
418 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
419 else if (strcmp(name, "state") == 0)
420 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200421 else if (strcmp(name, "data") == 0)
422 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200423 else if (strcmp(name, "developer") == 0)
424 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
425 else
426 return cli_err(appctx, "No such trace level");
427 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200428 else if (strcmp(args[2], "lock") == 0) {
429 const char *name = args[3];
430
431 if (!*name) {
432 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
433 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
434 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
435 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
436
437 if (src->arg_def & TRC_ARGS_CONN)
438 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
439 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
440
441 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
442 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
443 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
444
445 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
446 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
447 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
448
449 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
450 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
451
452 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
453 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
454 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
455
456 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
457 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
458 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
459
460 if (src->arg_def & TRC_ARGS_STRM)
461 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
462 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
463
464 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
465 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
466
467 if (src->lockon_args && src->lockon_args[0].name)
468 chunk_appendf(&trash, " %c %-10s : %s\n",
469 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
470 src->lockon_args[0].name, src->lockon_args[0].desc);
471
472 if (src->lockon_args && src->lockon_args[1].name)
473 chunk_appendf(&trash, " %c %-10s : %s\n",
474 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
475 src->lockon_args[1].name, src->lockon_args[1].desc);
476
477 if (src->lockon_args && src->lockon_args[2].name)
478 chunk_appendf(&trash, " %c %-10s : %s\n",
479 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
480 src->lockon_args[2].name, src->lockon_args[2].desc);
481
482 if (src->lockon_args && src->lockon_args[3].name)
483 chunk_appendf(&trash, " %c %-10s : %s\n",
484 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
485 src->lockon_args[3].name, src->lockon_args[3].desc);
486
487 trash.area[trash.data] = 0;
488 return cli_msg(appctx, LOG_WARNING, trash.area);
489 }
490 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
491 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
492 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
493 }
494 else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) {
495 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
496 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
497 }
498 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
499 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
500 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
501 }
502 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
503 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
504 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
505 }
506 else if (strcmp(name, "nothing") == 0) {
507 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
508 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
509 }
510 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
511 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
512 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
513 }
514 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
515 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
516 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
517 }
518 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
519 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
520 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
521 }
522 else if (strcmp(name, "thread") == 0) {
523 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
524 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
525 }
526 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
527 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
528 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
529 }
530 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
531 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
532 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
533 }
534 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
535 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
536 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
537 }
538 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
539 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
540 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
541 }
542 else
543 return cli_err(appctx, "Unsupported lock-on criterion");
544 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200545 else
546 return cli_err(appctx, "Unknown trace keyword");
547
548 return 0;
549}
550
Willy Tarreau85b15752019-08-12 16:44:33 +0200551/* parse the command, returns 1 if a message is returned, otherwise zero */
552static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
553{
554 struct trace_source *src;
555 const struct sink *sink;
556 int i;
557
558 args++; // make args[1] the 1st arg
559
560 if (!*args[1]) {
561 /* no arg => report the list of supported sources */
562 chunk_printf(&trash,
563 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
564 );
565
566 list_for_each_entry(src, &trace_sources, source_link) {
567 sink = src->sink;
568 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
569 trace_state_char(src->state), src->name.ptr,
570 sink ? sink->name : "none",
571 sink ? sink->ctx.dropped : 0,
572 src->desc);
573 }
574
575 trash.area[trash.data] = 0;
576 return cli_msg(appctx, LOG_INFO, trash.area);
577 }
578
579 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
580 return 1;
581
582 src = trace_find_source(args[1]);
583 if (!src)
584 return cli_err(appctx, "No such trace source");
585
586 sink = src->sink;
587 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
588 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
589 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
590
591 chunk_appendf(&trash, " - event name : report start stop pause\n");
592 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
593 chunk_appendf(&trash, " %-10s : %c %c %c %c\n",
594 src->known_events[i].name,
595 trace_event_char(src->report_events, src->known_events[i].mask),
596 trace_event_char(src->start_events, src->known_events[i].mask),
597 trace_event_char(src->stop_events, src->known_events[i].mask),
598 trace_event_char(src->pause_events, src->known_events[i].mask));
599 }
600
601 trash.area[trash.data] = 0;
602 return cli_msg(appctx, LOG_WARNING, trash.area);
603}
604
Willy Tarreau864e8802019-08-08 18:48:12 +0200605static struct cli_kw_list cli_kws = {{ },{
606 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200607 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200608 {{},}
609}};
610
611INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
612
Willy Tarreau4151c752019-08-08 18:21:26 +0200613/*
614 * Local variables:
615 * c-indent-level: 8
616 * c-basic-offset: 8
617 * End:
618 */