blob: 3f6ff0543ded92e7edee3daac63250a0b9ea0f20 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020022#include <common/buffer.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020023#include <import/ist.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020024#include <common/mini-clist.h>
Willy Tarreau864e8802019-08-08 18:48:12 +020025#include <proto/cli.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020026#include <proto/log.h>
Willy Tarreau864e8802019-08-08 18:48:12 +020027#include <proto/sink.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020028#include <proto/trace.h>
29
30struct list trace_sources = LIST_HEAD_INIT(trace_sources);
Willy Tarreau88ebd402019-08-19 15:55:34 +020031THREAD_LOCAL struct buffer trace_buf = { };
32
33/* allocates the trace buffers. Returns 0 in case of failure. It is safe to
34 * call to call this function multiple times if the size changes.
35 */
36static int alloc_trace_buffers_per_thread()
37{
38 chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2e7fdfc2019-11-25 19:43:31 +010039 return !!trace_buf.area;
Willy Tarreau88ebd402019-08-19 15:55:34 +020040}
41
42static void free_trace_buffers_per_thread()
43{
44 chunk_destroy(&trace_buf);
45}
Willy Tarreau4151c752019-08-08 18:21:26 +020046
Willy Tarreau88ebd402019-08-19 15:55:34 +020047REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread);
48REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread);
Willy Tarreau864e8802019-08-08 18:48:12 +020049
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020050/* pick the lowest non-null argument with a non-null arg_def mask */
51static inline const void *trace_pick_arg(uint32_t arg_def, const void *a1, const void *a2, const void *a3, const void *a4)
52{
53 if (arg_def & 0x0000FFFF) {
54 if ((arg_def & 0x000000FF) && a1)
55 return a1;
56 if ((arg_def & 0x0000FF00) && a2)
57 return a2;
58 }
59
60 if (arg_def & 0xFFFF0000) {
61 if ((arg_def & 0x00FF0000) && a3)
62 return a3;
63 if ((arg_def & 0xFF000000) && a4)
64 return a4;
65 }
66
67 return NULL;
68}
69
Willy Tarreau4c2ae482019-08-12 15:51:58 +020070/* write a message for the given trace source */
Willy Tarreau09fb0df2019-08-29 08:40:59 +020071void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
72 const struct ist where, const char *func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020073 const void *a1, const void *a2, const void *a3, const void *a4,
Willy Tarreau09fb0df2019-08-29 08:40:59 +020074 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
75 const struct ist where, const struct ist func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020076 const void *a1, const void *a2, const void *a3, const void *a4),
77 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020078{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020079 const struct listener *li = NULL;
80 const struct proxy *fe = NULL;
81 const struct proxy *be = NULL;
82 const struct server *srv = NULL;
83 const struct session *sess = NULL;
84 const struct stream *strm = NULL;
85 const struct connection *conn = NULL;
86 const void *lockon_ptr = NULL;
Willy Tarreau09fb0df2019-08-29 08:40:59 +020087 struct ist ist_func = ist(func);
Willy Tarreaua9f5b962019-08-28 10:08:58 +020088 char tnum[4];
Willy Tarreau052ad362019-08-29 09:08:36 +020089 struct ist line[12];
Willy Tarreau09fb0df2019-08-29 08:40:59 +020090 int words = 0;
Willy Tarreau419bd492019-08-12 17:27:09 +020091
Willy Tarreau4c2ae482019-08-12 15:51:58 +020092 if (likely(src->state == TRACE_STATE_STOPPED))
93 return;
94
95 /* check that at least one action is interested by this event */
96 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
97 return;
98
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020099 /* retrieve available information from the caller's arguments */
100 if (src->arg_def & TRC_ARGS_CONN)
101 conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4);
102
103 if (src->arg_def & TRC_ARGS_SESS)
104 sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4);
105
106 if (src->arg_def & TRC_ARGS_STRM)
107 strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4);
108
109 if (!sess && strm)
110 sess = strm->sess;
111 else if (!sess && conn)
112 sess = conn->owner;
113
114 if (sess) {
115 fe = sess->fe;
116 li = sess->listener;
117 }
118
119 if (!li && conn)
120 li = objt_listener(conn->target);
121
122 if (li && !fe)
123 fe = li->bind_conf->frontend;
124
125 if (strm) {
126 be = strm->be;
127 srv = strm->srv_conn;
128 }
129
130 if (!srv && conn)
131 srv = objt_server(conn->target);
132
133 if (srv && !be)
134 be = srv->proxy;
135
136 if (!be && conn)
137 be = objt_proxy(conn->target);
138
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200139 /* TODO: add handling of filters here, return if no match (not even update states) */
140
141 /* check if we need to start the trace now */
142 if (src->state == TRACE_STATE_WAITING) {
143 if ((src->start_events & mask) == 0)
144 return;
145
146 /* TODO: add update of lockon+lockon_ptr here */
147 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
148 }
149
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200150 /* we may want to lock on a particular object */
151 if (src->lockon != TRACE_LOCKON_NOTHING) {
152 switch (src->lockon) {
153 case TRACE_LOCKON_BACKEND: lockon_ptr = be; break;
154 case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break;
155 case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break;
156 case TRACE_LOCKON_LISTENER: lockon_ptr = li; break;
157 case TRACE_LOCKON_SERVER: lockon_ptr = srv; break;
158 case TRACE_LOCKON_SESSION: lockon_ptr = sess; break;
159 case TRACE_LOCKON_STREAM: lockon_ptr = strm; break;
160 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
161 case TRACE_LOCKON_ARG1: lockon_ptr = a1; break;
162 case TRACE_LOCKON_ARG2: lockon_ptr = a2; break;
163 case TRACE_LOCKON_ARG3: lockon_ptr = a3; break;
164 case TRACE_LOCKON_ARG4: lockon_ptr = a4; break;
165 default: break; // silence stupid gcc -Wswitch
166 }
167
168 if (src->lockon_ptr && src->lockon_ptr != lockon_ptr)
169 return;
170
171 if (!src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
172 HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr);
173 }
174
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200175 /* here the trace is running and is tracking a desired item */
176
Willy Tarreau5da40882019-08-12 17:57:57 +0200177 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200178 goto end;
179
Willy Tarreau419bd492019-08-12 17:27:09 +0200180 /* log the logging location truncated to 10 chars from the right so that
181 * the line number and the end of the file name are there.
182 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200183 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200184 tnum[0] = '0' + tid / 10;
185 tnum[1] = '0' + tid % 10;
186 tnum[2] = '|';
187 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200188 line[words++] = ist(tnum);
189 line[words++] = src->name;
190 line[words++] = ist("|");
Willy Tarreau052ad362019-08-29 09:08:36 +0200191 line[words++] = ist2("01234" + level, 1); // "0" to "4"
192 line[words++] = ist("|");
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200193 line[words] = where;
194 if (line[words].len > 13) {
195 line[words].ptr += (line[words].len - 13);
196 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200197 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200198 words++;
199 line[words++] = ist("] ");
200
201 if (ist_func.ptr) {
202 line[words++] = ist_func;
203 line[words++] = ist("(): ");
204 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200205
Willy Tarreau3da00262019-08-28 07:03:58 +0200206 if (!cb)
207 cb = src->default_cb;
208
Willy Tarreau370a6942019-08-29 08:24:16 +0200209 if (cb && src->verbosity) {
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200210 /* decode function passed, we want to pre-fill the
211 * buffer with the message and let the decode function
212 * do its job, possibly even overwriting it.
213 */
214 b_reset(&trace_buf);
215 b_istput(&trace_buf, msg);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200216 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
217 line[words].ptr = trace_buf.area;
218 line[words].len = trace_buf.data;
219 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200220 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200221 else {
222 /* Note that here we could decide to print some args whose type
223 * is known, when verbosity is above the quiet level, and even
224 * to print the name and values of those which are declared for
225 * lock-on.
226 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200227 line[words++] = msg;
Willy Tarreau370a6942019-08-29 08:24:16 +0200228 }
Willy Tarreau419bd492019-08-12 17:27:09 +0200229
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200230 if (src->sink)
Emeric Brunbd163812020-05-06 14:33:46 +0200231 sink_write(src->sink, line, words, 0, 0, NULL, NULL, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200232
233 end:
234 /* check if we need to stop the trace now */
235 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200236 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200237 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
238 }
239 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200240 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200241 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
242 }
243}
244
Willy Tarreau864e8802019-08-08 18:48:12 +0200245struct trace_source *trace_find_source(const char *name)
246{
247 struct trace_source *src;
248 const struct ist iname = ist(name);
249
250 list_for_each_entry(src, &trace_sources, source_link)
251 if (isteq(src->name, iname))
252 return src;
253 return NULL;
254}
255
256const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
257{
258 for (; ev && ev->mask; ev++)
259 if (strcmp(ev->name, name) == 0)
260 return ev;
261 return NULL;
262}
263
264/* parse the command, returns 1 if a message is returned, otherwise zero */
265static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
266{
267 struct trace_source *src;
268 uint64_t *ev_ptr = NULL;
269
270 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
271 return 1;
272
273 if (!*args[1]) {
274 /* no arg => report the list of supported sources as a warning */
275 chunk_printf(&trash,
276 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
277 " [.] 0 : not a source, will immediately stop all traces\n"
278 );
279
280 list_for_each_entry(src, &trace_sources, source_link)
281 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
282
283 trash.area[trash.data] = 0;
284 return cli_msg(appctx, LOG_WARNING, trash.area);
285 }
286
287 if (strcmp(args[1], "0") == 0) {
288 /* emergency stop of all traces */
289 list_for_each_entry(src, &trace_sources, source_link)
290 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
291 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
292 }
293
294 src = trace_find_source(args[1]);
295 if (!src)
296 return cli_err(appctx, "No such trace source");
297
298 if (!*args[2]) {
299 return cli_msg(appctx, LOG_WARNING,
300 "Supported commands:\n"
Willy Tarreau370a6942019-08-29 08:24:16 +0200301 " event : list/enable/disable source-specific event reporting\n"
302 //" filter : list/enable/disable generic filters\n"
303 " level : list/set trace reporting level\n"
304 " lock : automatic lock on thread/connection/stream/...\n"
305 " pause : pause and automatically restart after a specific event\n"
306 " sink : list/set event sinks\n"
307 " start : start immediately or after a specific event\n"
308 " stop : stop immediately or after a specific event\n"
309 " verbosity : list/set trace output verbosity\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200310 );
311 }
312 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
313 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
314 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
315 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
316 const struct trace_event *ev;
317 const char *name = args[3];
318 int neg = 0;
319 int i;
320
321 /* skip prefix '!', '-', '+' and remind negation */
322 while (*name) {
323 if (*name == '!' || *name == '-')
324 neg = 1;
325 else if (*name == '+')
326 neg = 0;
327 else
328 break;
329 name++;
330 }
331
332 if (!*name) {
333 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
334 if (ev_ptr != &src->report_events)
Willy Tarreau99282dd2019-08-30 07:37:32 +0200335 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
336 chunk_appendf(&trash, " - none : disable all event types\n");
337 chunk_appendf(&trash, " - any : enable all event types\n");
Willy Tarreau864e8802019-08-08 18:48:12 +0200338 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200339 chunk_appendf(&trash, " %c %-12s : %s\n",
Willy Tarreau864e8802019-08-08 18:48:12 +0200340 trace_event_char(*ev_ptr, src->known_events[i].mask),
341 src->known_events[i].name, src->known_events[i].desc);
342 }
343 trash.area[trash.data] = 0;
344 return cli_msg(appctx, LOG_WARNING, trash.area);
345 }
346
347 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
348 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200349 if (ev_ptr == &src->pause_events) {
350 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200351 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200352 }
353 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200354 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200355 }
356 else if (ev_ptr == &src->stop_events) {
357 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200358 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200359 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200360 return 0;
361 }
362
363 if (strcmp(name, "none") == 0)
364 HA_ATOMIC_STORE(ev_ptr, 0);
365 else if (strcmp(name, "any") == 0)
366 HA_ATOMIC_STORE(ev_ptr, ~0);
367 else {
368 ev = trace_find_event(src->known_events, name);
369 if (!ev)
370 return cli_err(appctx, "No such trace event");
371
372 if (!neg)
373 HA_ATOMIC_OR(ev_ptr, ev->mask);
374 else
375 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
376 }
377 }
378 else if (strcmp(args[2], "sink") == 0) {
379 const char *name = args[3];
380 struct sink *sink;
381
382 if (!*name) {
383 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
384 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
385 list_for_each_entry(sink, &sink_list, sink_list) {
386 chunk_appendf(&trash, " %c %-10s : %s\n",
387 src->sink == sink ? '*' : ' ',
388 sink->name, sink->desc);
389 }
390 trash.area[trash.data] = 0;
391 return cli_msg(appctx, LOG_WARNING, trash.area);
392 }
393
394 if (strcmp(name, "none") == 0)
395 sink = NULL;
396 else {
397 sink = sink_find(name);
398 if (!sink)
399 return cli_err(appctx, "No such sink");
400 }
401
402 HA_ATOMIC_STORE(&src->sink, sink);
403 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200404 else if (strcmp(args[2], "level") == 0) {
405 const char *name = args[3];
406
407 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200408 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200409 chunk_appendf(&trash, " %c user : information useful to the end user\n",
410 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200411 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200412 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200413 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200414 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200415 chunk_appendf(&trash, " %c data : also report data transfers\n",
416 src->level == TRACE_LEVEL_DATA ? '*' : ' ');
417 chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200418 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
419 trash.area[trash.data] = 0;
420 return cli_msg(appctx, LOG_WARNING, trash.area);
421 }
422
423 if (strcmp(name, "user") == 0)
424 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200425 else if (strcmp(name, "proto") == 0)
426 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
427 else if (strcmp(name, "state") == 0)
428 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200429 else if (strcmp(name, "data") == 0)
430 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200431 else if (strcmp(name, "developer") == 0)
432 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
433 else
434 return cli_err(appctx, "No such trace level");
435 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200436 else if (strcmp(args[2], "lock") == 0) {
437 const char *name = args[3];
438
439 if (!*name) {
440 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
441 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
442 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
443 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
444
445 if (src->arg_def & TRC_ARGS_CONN)
446 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
447 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
448
449 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
450 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
451 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
452
453 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
454 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
455 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
456
457 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
458 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
459
460 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
461 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
462 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
463
464 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
465 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
466 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
467
468 if (src->arg_def & TRC_ARGS_STRM)
469 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
470 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
471
472 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
473 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
474
475 if (src->lockon_args && src->lockon_args[0].name)
476 chunk_appendf(&trash, " %c %-10s : %s\n",
477 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
478 src->lockon_args[0].name, src->lockon_args[0].desc);
479
480 if (src->lockon_args && src->lockon_args[1].name)
481 chunk_appendf(&trash, " %c %-10s : %s\n",
482 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
483 src->lockon_args[1].name, src->lockon_args[1].desc);
484
485 if (src->lockon_args && src->lockon_args[2].name)
486 chunk_appendf(&trash, " %c %-10s : %s\n",
487 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
488 src->lockon_args[2].name, src->lockon_args[2].desc);
489
490 if (src->lockon_args && src->lockon_args[3].name)
491 chunk_appendf(&trash, " %c %-10s : %s\n",
492 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
493 src->lockon_args[3].name, src->lockon_args[3].desc);
494
495 trash.area[trash.data] = 0;
496 return cli_msg(appctx, LOG_WARNING, trash.area);
497 }
498 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
499 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
500 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
501 }
502 else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) {
503 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
504 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
505 }
506 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
507 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
508 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
509 }
510 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
511 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
512 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
513 }
514 else if (strcmp(name, "nothing") == 0) {
515 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
516 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
517 }
518 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
519 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
520 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
521 }
522 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
523 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
524 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
525 }
526 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
527 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
528 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
529 }
530 else if (strcmp(name, "thread") == 0) {
531 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
532 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
533 }
534 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
535 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
536 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
537 }
538 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
539 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
540 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
541 }
542 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
543 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
544 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
545 }
546 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
547 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
548 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
549 }
550 else
551 return cli_err(appctx, "Unsupported lock-on criterion");
552 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200553 else if (strcmp(args[2], "verbosity") == 0) {
554 const char *name = args[3];
555 const struct name_desc *nd;
556
557 if (!*name) {
558 chunk_printf(&trash, "Supported trace verbosities for source %s:\n", src->name.ptr);
559 chunk_appendf(&trash, " %c quiet : only report basic information with no decoding\n",
560 src->verbosity == 0 ? '*' : ' ');
561 if (!src->decoding || !src->decoding[0].name) {
562 chunk_appendf(&trash, " %c default : report extra information when available\n",
563 src->verbosity > 0 ? '*' : ' ');
564 } else {
565 for (nd = src->decoding; nd->name && nd->desc; nd++)
566 chunk_appendf(&trash, " %c %-10s : %s\n",
567 nd == (src->decoding + src->verbosity - 1) ? '*' : ' ',
568 nd->name, nd->desc);
569 }
570 trash.area[trash.data] = 0;
571 return cli_msg(appctx, LOG_WARNING, trash.area);
572 }
573
574 if (strcmp(name, "quiet") == 0)
575 HA_ATOMIC_STORE(&src->verbosity, 0);
576 else if (!src->decoding || !src->decoding[0].name) {
577 if (strcmp(name, "default") == 0)
578 HA_ATOMIC_STORE(&src->verbosity, 1);
579 else
580 return cli_err(appctx, "No such verbosity level");
581 } else {
582 for (nd = src->decoding; nd->name && nd->desc; nd++)
583 if (strcmp(name, nd->name) == 0)
584 break;
585
586 if (!nd->name || !nd->desc)
587 return cli_err(appctx, "No such verbosity level");
588
589 HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1);
590 }
591 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200592 else
593 return cli_err(appctx, "Unknown trace keyword");
594
595 return 0;
596}
597
Willy Tarreau85b15752019-08-12 16:44:33 +0200598/* parse the command, returns 1 if a message is returned, otherwise zero */
599static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
600{
601 struct trace_source *src;
602 const struct sink *sink;
603 int i;
604
605 args++; // make args[1] the 1st arg
606
607 if (!*args[1]) {
608 /* no arg => report the list of supported sources */
609 chunk_printf(&trash,
610 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
611 );
612
613 list_for_each_entry(src, &trace_sources, source_link) {
614 sink = src->sink;
615 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
616 trace_state_char(src->state), src->name.ptr,
617 sink ? sink->name : "none",
618 sink ? sink->ctx.dropped : 0,
619 src->desc);
620 }
621
622 trash.area[trash.data] = 0;
623 return cli_msg(appctx, LOG_INFO, trash.area);
624 }
625
626 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
627 return 1;
628
629 src = trace_find_source(args[1]);
630 if (!src)
631 return cli_err(appctx, "No such trace source");
632
633 sink = src->sink;
634 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
635 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
636 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
637
Willy Tarreau99282dd2019-08-30 07:37:32 +0200638 chunk_appendf(&trash, " - event name : report start stop pause\n");
Willy Tarreau85b15752019-08-12 16:44:33 +0200639 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200640 chunk_appendf(&trash, " %-12s : %c %c %c %c\n",
Willy Tarreau85b15752019-08-12 16:44:33 +0200641 src->known_events[i].name,
642 trace_event_char(src->report_events, src->known_events[i].mask),
643 trace_event_char(src->start_events, src->known_events[i].mask),
644 trace_event_char(src->stop_events, src->known_events[i].mask),
645 trace_event_char(src->pause_events, src->known_events[i].mask));
646 }
647
648 trash.area[trash.data] = 0;
649 return cli_msg(appctx, LOG_WARNING, trash.area);
650}
651
Willy Tarreau864e8802019-08-08 18:48:12 +0200652static struct cli_kw_list cli_kws = {{ },{
653 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200654 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200655 {{},}
656}};
657
658INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
659
Willy Tarreau4151c752019-08-08 18:21:26 +0200660/*
661 * Local variables:
662 * c-indent-level: 8
663 * c-basic-offset: 8
664 * End:
665 */