blob: 15e01487fd6f21931993e9da6a9c35ff8dfa5608 [file] [log] [blame]
Willy Tarreau4151c752019-08-08 18:21:26 +02001/*
2 * Runtime tracing API
3 *
4 * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Willy Tarreaueb6f7012020-05-27 16:21:26 +020021#include <import/ist.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020022#include <haproxy/api.h>
23#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020024#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <haproxy/errors.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020026#include <haproxy/istbuf.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020027#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreauba2f73d2020-06-03 20:02:28 +020029#include <haproxy/sink.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020030#include <haproxy/trace.h>
Willy Tarreau4151c752019-08-08 18:21:26 +020031
32struct list trace_sources = LIST_HEAD_INIT(trace_sources);
Willy Tarreau88ebd402019-08-19 15:55:34 +020033THREAD_LOCAL struct buffer trace_buf = { };
34
35/* allocates the trace buffers. Returns 0 in case of failure. It is safe to
36 * call to call this function multiple times if the size changes.
37 */
38static int alloc_trace_buffers_per_thread()
39{
40 chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize);
Willy Tarreau2e7fdfc2019-11-25 19:43:31 +010041 return !!trace_buf.area;
Willy Tarreau88ebd402019-08-19 15:55:34 +020042}
43
44static void free_trace_buffers_per_thread()
45{
46 chunk_destroy(&trace_buf);
47}
Willy Tarreau4151c752019-08-08 18:21:26 +020048
Willy Tarreau88ebd402019-08-19 15:55:34 +020049REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread);
50REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread);
Willy Tarreau864e8802019-08-08 18:48:12 +020051
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020052/* pick the lowest non-null argument with a non-null arg_def mask */
53static inline const void *trace_pick_arg(uint32_t arg_def, const void *a1, const void *a2, const void *a3, const void *a4)
54{
55 if (arg_def & 0x0000FFFF) {
56 if ((arg_def & 0x000000FF) && a1)
57 return a1;
58 if ((arg_def & 0x0000FF00) && a2)
59 return a2;
60 }
61
62 if (arg_def & 0xFFFF0000) {
63 if ((arg_def & 0x00FF0000) && a3)
64 return a3;
65 if ((arg_def & 0xFF000000) && a4)
66 return a4;
67 }
68
69 return NULL;
70}
71
Willy Tarreau4c2ae482019-08-12 15:51:58 +020072/* write a message for the given trace source */
Willy Tarreau09fb0df2019-08-29 08:40:59 +020073void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
74 const struct ist where, const char *func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020075 const void *a1, const void *a2, const void *a3, const void *a4,
Willy Tarreau09fb0df2019-08-29 08:40:59 +020076 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
77 const struct ist where, const struct ist func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020078 const void *a1, const void *a2, const void *a3, const void *a4),
79 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020080{
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020081 const struct listener *li = NULL;
82 const struct proxy *fe = NULL;
83 const struct proxy *be = NULL;
84 const struct server *srv = NULL;
85 const struct session *sess = NULL;
86 const struct stream *strm = NULL;
87 const struct connection *conn = NULL;
Christopher Faulet6d80b632021-04-12 11:53:16 +020088 const struct check *check = NULL;
Amaury Denoyellebaea9642021-12-21 11:26:02 +010089#ifdef USE_QUIC
90 const struct quic_conn *qc = NULL;
91#endif
Willy Tarreaud8b99ed2019-08-21 17:05:46 +020092 const void *lockon_ptr = NULL;
Willy Tarreau09fb0df2019-08-29 08:40:59 +020093 struct ist ist_func = ist(func);
Willy Tarreaua9f5b962019-08-28 10:08:58 +020094 char tnum[4];
Willy Tarreau052ad362019-08-29 09:08:36 +020095 struct ist line[12];
Willy Tarreau09fb0df2019-08-29 08:40:59 +020096 int words = 0;
Willy Tarreau419bd492019-08-12 17:27:09 +020097
Willy Tarreau4c2ae482019-08-12 15:51:58 +020098 if (likely(src->state == TRACE_STATE_STOPPED))
99 return;
100
101 /* check that at least one action is interested by this event */
102 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
103 return;
104
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200105 /* retrieve available information from the caller's arguments */
106 if (src->arg_def & TRC_ARGS_CONN)
107 conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4);
108
109 if (src->arg_def & TRC_ARGS_SESS)
110 sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4);
111
112 if (src->arg_def & TRC_ARGS_STRM)
113 strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4);
114
Christopher Faulet6d80b632021-04-12 11:53:16 +0200115 if (src->arg_def & TRC_ARGS_CHK)
116 check = trace_pick_arg(src->arg_def & TRC_ARGS_CHK, a1, a2, a3, a4);
117
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100118#ifdef USE_QUIC
119 if (src->arg_def & TRC_ARGS_QCON)
120 qc = trace_pick_arg(src->arg_def & TRC_ARGS_QCON, a1, a2, a3, a4);
121#endif
122
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200123 if (!sess && strm)
124 sess = strm->sess;
125 else if (!sess && conn)
126 sess = conn->owner;
Christopher Faulet6d80b632021-04-12 11:53:16 +0200127 else if (!sess && check)
128 sess = check->sess;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200129
130 if (sess) {
131 fe = sess->fe;
132 li = sess->listener;
133 }
134
135 if (!li && conn)
136 li = objt_listener(conn->target);
137
138 if (li && !fe)
139 fe = li->bind_conf->frontend;
140
141 if (strm) {
142 be = strm->be;
143 srv = strm->srv_conn;
144 }
Christopher Faulet6d80b632021-04-12 11:53:16 +0200145 if (check) {
146 srv = check->server;
147 be = srv->proxy;
148 }
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200149
150 if (!srv && conn)
151 srv = objt_server(conn->target);
152
153 if (srv && !be)
154 be = srv->proxy;
155
156 if (!be && conn)
157 be = objt_proxy(conn->target);
158
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200159 /* TODO: add handling of filters here, return if no match (not even update states) */
160
161 /* check if we need to start the trace now */
162 if (src->state == TRACE_STATE_WAITING) {
163 if ((src->start_events & mask) == 0)
164 return;
165
166 /* TODO: add update of lockon+lockon_ptr here */
167 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
168 }
169
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200170 /* we may want to lock on a particular object */
171 if (src->lockon != TRACE_LOCKON_NOTHING) {
172 switch (src->lockon) {
173 case TRACE_LOCKON_BACKEND: lockon_ptr = be; break;
174 case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break;
175 case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break;
176 case TRACE_LOCKON_LISTENER: lockon_ptr = li; break;
177 case TRACE_LOCKON_SERVER: lockon_ptr = srv; break;
178 case TRACE_LOCKON_SESSION: lockon_ptr = sess; break;
179 case TRACE_LOCKON_STREAM: lockon_ptr = strm; break;
Christopher Faulet6d80b632021-04-12 11:53:16 +0200180 case TRACE_LOCKON_CHECK: lockon_ptr = check; break;
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200181 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
Amaury Denoyellebaea9642021-12-21 11:26:02 +0100182#ifdef USE_QUIC
183 case TRACE_LOCKON_QCON: lockon_ptr = qc; break;
184#endif
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200185 case TRACE_LOCKON_ARG1: lockon_ptr = a1; break;
186 case TRACE_LOCKON_ARG2: lockon_ptr = a2; break;
187 case TRACE_LOCKON_ARG3: lockon_ptr = a3; break;
188 case TRACE_LOCKON_ARG4: lockon_ptr = a4; break;
189 default: break; // silence stupid gcc -Wswitch
190 }
191
192 if (src->lockon_ptr && src->lockon_ptr != lockon_ptr)
193 return;
194
195 if (!src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
196 HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr);
197 }
198
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200199 /* here the trace is running and is tracking a desired item */
200
Willy Tarreau5da40882019-08-12 17:57:57 +0200201 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200202 goto end;
203
Willy Tarreau419bd492019-08-12 17:27:09 +0200204 /* log the logging location truncated to 10 chars from the right so that
205 * the line number and the end of the file name are there.
206 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200207 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200208 tnum[0] = '0' + tid / 10;
209 tnum[1] = '0' + tid % 10;
210 tnum[2] = '|';
211 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200212 line[words++] = ist(tnum);
213 line[words++] = src->name;
214 line[words++] = ist("|");
Frédéric Lécaillefd183142020-12-02 16:51:00 +0100215 line[words++] = ist2("012345" + level, 1); // "0" to "5"
Willy Tarreau052ad362019-08-29 09:08:36 +0200216 line[words++] = ist("|");
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200217 line[words] = where;
218 if (line[words].len > 13) {
219 line[words].ptr += (line[words].len - 13);
220 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200221 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200222 words++;
223 line[words++] = ist("] ");
224
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100225 if (isttest(ist_func)) {
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200226 line[words++] = ist_func;
227 line[words++] = ist("(): ");
228 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200229
Willy Tarreau3da00262019-08-28 07:03:58 +0200230 if (!cb)
231 cb = src->default_cb;
232
Willy Tarreau370a6942019-08-29 08:24:16 +0200233 if (cb && src->verbosity) {
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200234 /* decode function passed, we want to pre-fill the
235 * buffer with the message and let the decode function
236 * do its job, possibly even overwriting it.
237 */
238 b_reset(&trace_buf);
239 b_istput(&trace_buf, msg);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200240 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100241 line[words] = ist2(trace_buf.area, trace_buf.data);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200242 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200243 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200244 else {
245 /* Note that here we could decide to print some args whose type
246 * is known, when verbosity is above the quiet level, and even
247 * to print the name and values of those which are declared for
248 * lock-on.
249 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200250 line[words++] = msg;
Willy Tarreau370a6942019-08-29 08:24:16 +0200251 }
Willy Tarreau419bd492019-08-12 17:27:09 +0200252
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200253 if (src->sink)
Emeric Brun54648852020-07-06 15:54:06 +0200254 sink_write(src->sink, line, words, 0, 0, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200255
256 end:
257 /* check if we need to stop the trace now */
258 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200259 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200260 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
261 }
262 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200263 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200264 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
265 }
266}
267
Willy Tarreau864e8802019-08-08 18:48:12 +0200268struct trace_source *trace_find_source(const char *name)
269{
270 struct trace_source *src;
271 const struct ist iname = ist(name);
272
273 list_for_each_entry(src, &trace_sources, source_link)
274 if (isteq(src->name, iname))
275 return src;
276 return NULL;
277}
278
279const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
280{
281 for (; ev && ev->mask; ev++)
282 if (strcmp(ev->name, name) == 0)
283 return ev;
284 return NULL;
285}
286
287/* parse the command, returns 1 if a message is returned, otherwise zero */
288static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
289{
290 struct trace_source *src;
291 uint64_t *ev_ptr = NULL;
292
293 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
294 return 1;
295
296 if (!*args[1]) {
297 /* no arg => report the list of supported sources as a warning */
298 chunk_printf(&trash,
299 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
300 " [.] 0 : not a source, will immediately stop all traces\n"
301 );
302
303 list_for_each_entry(src, &trace_sources, source_link)
304 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
305
306 trash.area[trash.data] = 0;
307 return cli_msg(appctx, LOG_WARNING, trash.area);
308 }
309
310 if (strcmp(args[1], "0") == 0) {
311 /* emergency stop of all traces */
312 list_for_each_entry(src, &trace_sources, source_link)
313 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
314 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
315 }
316
317 src = trace_find_source(args[1]);
318 if (!src)
319 return cli_err(appctx, "No such trace source");
320
321 if (!*args[2]) {
322 return cli_msg(appctx, LOG_WARNING,
323 "Supported commands:\n"
Willy Tarreau370a6942019-08-29 08:24:16 +0200324 " event : list/enable/disable source-specific event reporting\n"
325 //" filter : list/enable/disable generic filters\n"
326 " level : list/set trace reporting level\n"
327 " lock : automatic lock on thread/connection/stream/...\n"
328 " pause : pause and automatically restart after a specific event\n"
329 " sink : list/set event sinks\n"
330 " start : start immediately or after a specific event\n"
331 " stop : stop immediately or after a specific event\n"
332 " verbosity : list/set trace output verbosity\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200333 );
334 }
335 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
336 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
337 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
338 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
339 const struct trace_event *ev;
340 const char *name = args[3];
341 int neg = 0;
342 int i;
343
344 /* skip prefix '!', '-', '+' and remind negation */
345 while (*name) {
346 if (*name == '!' || *name == '-')
347 neg = 1;
348 else if (*name == '+')
349 neg = 0;
350 else
351 break;
352 name++;
353 }
354
355 if (!*name) {
356 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
357 if (ev_ptr != &src->report_events)
Willy Tarreau99282dd2019-08-30 07:37:32 +0200358 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
359 chunk_appendf(&trash, " - none : disable all event types\n");
360 chunk_appendf(&trash, " - any : enable all event types\n");
Willy Tarreau864e8802019-08-08 18:48:12 +0200361 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200362 chunk_appendf(&trash, " %c %-12s : %s\n",
Willy Tarreau864e8802019-08-08 18:48:12 +0200363 trace_event_char(*ev_ptr, src->known_events[i].mask),
364 src->known_events[i].name, src->known_events[i].desc);
365 }
366 trash.area[trash.data] = 0;
367 return cli_msg(appctx, LOG_WARNING, trash.area);
368 }
369
370 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
371 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200372 if (ev_ptr == &src->pause_events) {
373 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200374 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200375 }
376 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200377 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200378 }
379 else if (ev_ptr == &src->stop_events) {
380 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200381 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200382 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200383 return 0;
384 }
385
386 if (strcmp(name, "none") == 0)
387 HA_ATOMIC_STORE(ev_ptr, 0);
388 else if (strcmp(name, "any") == 0)
389 HA_ATOMIC_STORE(ev_ptr, ~0);
390 else {
391 ev = trace_find_event(src->known_events, name);
392 if (!ev)
393 return cli_err(appctx, "No such trace event");
394
395 if (!neg)
396 HA_ATOMIC_OR(ev_ptr, ev->mask);
397 else
398 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
399 }
400 }
401 else if (strcmp(args[2], "sink") == 0) {
402 const char *name = args[3];
403 struct sink *sink;
404
405 if (!*name) {
406 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
407 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
408 list_for_each_entry(sink, &sink_list, sink_list) {
409 chunk_appendf(&trash, " %c %-10s : %s\n",
410 src->sink == sink ? '*' : ' ',
411 sink->name, sink->desc);
412 }
413 trash.area[trash.data] = 0;
414 return cli_msg(appctx, LOG_WARNING, trash.area);
415 }
416
417 if (strcmp(name, "none") == 0)
418 sink = NULL;
419 else {
420 sink = sink_find(name);
421 if (!sink)
422 return cli_err(appctx, "No such sink");
423 }
424
425 HA_ATOMIC_STORE(&src->sink, sink);
426 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200427 else if (strcmp(args[2], "level") == 0) {
428 const char *name = args[3];
429
430 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200431 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreaua1f12742020-12-01 09:46:46 +0100432 chunk_appendf(&trash, " %c error : report errors\n",
433 src->level == TRACE_LEVEL_ERROR ? '*' : ' ');
434 chunk_appendf(&trash, " %c user : also information useful to the end user\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200435 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200436 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200437 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200438 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200439 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200440 chunk_appendf(&trash, " %c data : also report data transfers\n",
441 src->level == TRACE_LEVEL_DATA ? '*' : ' ');
442 chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200443 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
444 trash.area[trash.data] = 0;
445 return cli_msg(appctx, LOG_WARNING, trash.area);
446 }
447
Willy Tarreaua1f12742020-12-01 09:46:46 +0100448 if (strcmp(name, "error") == 0)
449 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_ERROR);
450 else if (strcmp(name, "user") == 0)
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200451 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200452 else if (strcmp(name, "proto") == 0)
453 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
454 else if (strcmp(name, "state") == 0)
455 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200456 else if (strcmp(name, "data") == 0)
457 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200458 else if (strcmp(name, "developer") == 0)
459 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
460 else
461 return cli_err(appctx, "No such trace level");
462 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200463 else if (strcmp(args[2], "lock") == 0) {
464 const char *name = args[3];
465
466 if (!*name) {
467 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
468 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
469 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
470 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
471
Christopher Faulet6d80b632021-04-12 11:53:16 +0200472 if (src->arg_def & TRC_ARGS_CHK)
473 chunk_appendf(&trash, " %c check : lock on the check that started the trace\n",
474 src->lockon == TRACE_LOCKON_CHECK ? '*' : ' ');
475
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200476 if (src->arg_def & TRC_ARGS_CONN)
477 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
478 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
479
480 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
481 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
482 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
483
484 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
485 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
486 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
487
488 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
489 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
490
491 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
492 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
493 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
494
495 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
496 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
497 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
498
499 if (src->arg_def & TRC_ARGS_STRM)
500 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
501 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
502
503 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
504 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
505
506 if (src->lockon_args && src->lockon_args[0].name)
507 chunk_appendf(&trash, " %c %-10s : %s\n",
508 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
509 src->lockon_args[0].name, src->lockon_args[0].desc);
510
511 if (src->lockon_args && src->lockon_args[1].name)
512 chunk_appendf(&trash, " %c %-10s : %s\n",
513 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
514 src->lockon_args[1].name, src->lockon_args[1].desc);
515
516 if (src->lockon_args && src->lockon_args[2].name)
517 chunk_appendf(&trash, " %c %-10s : %s\n",
518 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
519 src->lockon_args[2].name, src->lockon_args[2].desc);
520
521 if (src->lockon_args && src->lockon_args[3].name)
522 chunk_appendf(&trash, " %c %-10s : %s\n",
523 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
524 src->lockon_args[3].name, src->lockon_args[3].desc);
525
526 trash.area[trash.data] = 0;
527 return cli_msg(appctx, LOG_WARNING, trash.area);
528 }
529 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
530 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
531 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
532 }
Christopher Faulet6d80b632021-04-12 11:53:16 +0200533 else if ((src->arg_def & TRC_ARGS_CHK) && strcmp(name, "check") == 0) {
534 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CHECK);
535 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
536 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200537 else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) {
538 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
539 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
540 }
541 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
542 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
543 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
544 }
545 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
546 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
547 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
548 }
549 else if (strcmp(name, "nothing") == 0) {
550 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
551 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
552 }
553 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
554 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
555 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
556 }
557 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
558 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
559 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
560 }
561 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
562 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
563 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
564 }
565 else if (strcmp(name, "thread") == 0) {
566 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
567 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
568 }
569 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
570 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
571 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
572 }
573 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
574 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
575 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
576 }
577 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
578 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
579 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
580 }
581 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
582 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
583 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
584 }
585 else
586 return cli_err(appctx, "Unsupported lock-on criterion");
587 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200588 else if (strcmp(args[2], "verbosity") == 0) {
589 const char *name = args[3];
590 const struct name_desc *nd;
591
592 if (!*name) {
593 chunk_printf(&trash, "Supported trace verbosities for source %s:\n", src->name.ptr);
594 chunk_appendf(&trash, " %c quiet : only report basic information with no decoding\n",
595 src->verbosity == 0 ? '*' : ' ');
596 if (!src->decoding || !src->decoding[0].name) {
597 chunk_appendf(&trash, " %c default : report extra information when available\n",
598 src->verbosity > 0 ? '*' : ' ');
599 } else {
600 for (nd = src->decoding; nd->name && nd->desc; nd++)
601 chunk_appendf(&trash, " %c %-10s : %s\n",
602 nd == (src->decoding + src->verbosity - 1) ? '*' : ' ',
603 nd->name, nd->desc);
604 }
605 trash.area[trash.data] = 0;
606 return cli_msg(appctx, LOG_WARNING, trash.area);
607 }
608
609 if (strcmp(name, "quiet") == 0)
610 HA_ATOMIC_STORE(&src->verbosity, 0);
611 else if (!src->decoding || !src->decoding[0].name) {
612 if (strcmp(name, "default") == 0)
613 HA_ATOMIC_STORE(&src->verbosity, 1);
614 else
615 return cli_err(appctx, "No such verbosity level");
616 } else {
617 for (nd = src->decoding; nd->name && nd->desc; nd++)
618 if (strcmp(name, nd->name) == 0)
619 break;
620
621 if (!nd->name || !nd->desc)
622 return cli_err(appctx, "No such verbosity level");
623
624 HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1);
625 }
626 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200627 else
628 return cli_err(appctx, "Unknown trace keyword");
629
630 return 0;
631}
632
Willy Tarreau85b15752019-08-12 16:44:33 +0200633/* parse the command, returns 1 if a message is returned, otherwise zero */
634static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
635{
636 struct trace_source *src;
637 const struct sink *sink;
638 int i;
639
640 args++; // make args[1] the 1st arg
641
642 if (!*args[1]) {
643 /* no arg => report the list of supported sources */
644 chunk_printf(&trash,
645 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
646 );
647
648 list_for_each_entry(src, &trace_sources, source_link) {
649 sink = src->sink;
650 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
651 trace_state_char(src->state), src->name.ptr,
652 sink ? sink->name : "none",
653 sink ? sink->ctx.dropped : 0,
654 src->desc);
655 }
656
657 trash.area[trash.data] = 0;
658 return cli_msg(appctx, LOG_INFO, trash.area);
659 }
660
661 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
662 return 1;
663
664 src = trace_find_source(args[1]);
665 if (!src)
666 return cli_err(appctx, "No such trace source");
667
668 sink = src->sink;
669 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
670 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
671 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
672
Willy Tarreau99282dd2019-08-30 07:37:32 +0200673 chunk_appendf(&trash, " - event name : report start stop pause\n");
Willy Tarreau85b15752019-08-12 16:44:33 +0200674 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200675 chunk_appendf(&trash, " %-12s : %c %c %c %c\n",
Willy Tarreau85b15752019-08-12 16:44:33 +0200676 src->known_events[i].name,
677 trace_event_char(src->report_events, src->known_events[i].mask),
678 trace_event_char(src->start_events, src->known_events[i].mask),
679 trace_event_char(src->stop_events, src->known_events[i].mask),
680 trace_event_char(src->pause_events, src->known_events[i].mask));
681 }
682
683 trash.area[trash.data] = 0;
684 return cli_msg(appctx, LOG_WARNING, trash.area);
685}
686
Willy Tarreau864e8802019-08-08 18:48:12 +0200687static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200688 { { "trace", NULL }, "trace [<module>|0] [cmd [args...]] : manage live tracing (empty to list, 0 to stop all)", cli_parse_trace, NULL, NULL },
689 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200690 {{},}
691}};
692
693INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
694
Willy Tarreau4151c752019-08-08 18:21:26 +0200695/*
696 * Local variables:
697 * c-indent-level: 8
698 * c-basic-offset: 8
699 * End:
700 */