blob: 85eb2cf917afb2c0953e0cda11d3331337c2ed32 [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;
88 const void *lockon_ptr = NULL;
Willy Tarreau09fb0df2019-08-29 08:40:59 +020089 struct ist ist_func = ist(func);
Willy Tarreaua9f5b962019-08-28 10:08:58 +020090 char tnum[4];
Willy Tarreau052ad362019-08-29 09:08:36 +020091 struct ist line[12];
Willy Tarreau09fb0df2019-08-29 08:40:59 +020092 int words = 0;
Willy Tarreau419bd492019-08-12 17:27:09 +020093
Willy Tarreau4c2ae482019-08-12 15:51:58 +020094 if (likely(src->state == TRACE_STATE_STOPPED))
95 return;
96
97 /* check that at least one action is interested by this event */
98 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
99 return;
100
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200101 /* retrieve available information from the caller's arguments */
102 if (src->arg_def & TRC_ARGS_CONN)
103 conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4);
104
105 if (src->arg_def & TRC_ARGS_SESS)
106 sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4);
107
108 if (src->arg_def & TRC_ARGS_STRM)
109 strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4);
110
111 if (!sess && strm)
112 sess = strm->sess;
113 else if (!sess && conn)
114 sess = conn->owner;
115
116 if (sess) {
117 fe = sess->fe;
118 li = sess->listener;
119 }
120
121 if (!li && conn)
122 li = objt_listener(conn->target);
123
124 if (li && !fe)
125 fe = li->bind_conf->frontend;
126
127 if (strm) {
128 be = strm->be;
129 srv = strm->srv_conn;
130 }
131
132 if (!srv && conn)
133 srv = objt_server(conn->target);
134
135 if (srv && !be)
136 be = srv->proxy;
137
138 if (!be && conn)
139 be = objt_proxy(conn->target);
140
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200141 /* TODO: add handling of filters here, return if no match (not even update states) */
142
143 /* check if we need to start the trace now */
144 if (src->state == TRACE_STATE_WAITING) {
145 if ((src->start_events & mask) == 0)
146 return;
147
148 /* TODO: add update of lockon+lockon_ptr here */
149 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
150 }
151
Willy Tarreaud8b99ed2019-08-21 17:05:46 +0200152 /* we may want to lock on a particular object */
153 if (src->lockon != TRACE_LOCKON_NOTHING) {
154 switch (src->lockon) {
155 case TRACE_LOCKON_BACKEND: lockon_ptr = be; break;
156 case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break;
157 case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break;
158 case TRACE_LOCKON_LISTENER: lockon_ptr = li; break;
159 case TRACE_LOCKON_SERVER: lockon_ptr = srv; break;
160 case TRACE_LOCKON_SESSION: lockon_ptr = sess; break;
161 case TRACE_LOCKON_STREAM: lockon_ptr = strm; break;
162 case TRACE_LOCKON_THREAD: lockon_ptr = ti; break;
163 case TRACE_LOCKON_ARG1: lockon_ptr = a1; break;
164 case TRACE_LOCKON_ARG2: lockon_ptr = a2; break;
165 case TRACE_LOCKON_ARG3: lockon_ptr = a3; break;
166 case TRACE_LOCKON_ARG4: lockon_ptr = a4; break;
167 default: break; // silence stupid gcc -Wswitch
168 }
169
170 if (src->lockon_ptr && src->lockon_ptr != lockon_ptr)
171 return;
172
173 if (!src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING)
174 HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr);
175 }
176
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200177 /* here the trace is running and is tracking a desired item */
178
Willy Tarreau5da40882019-08-12 17:57:57 +0200179 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200180 goto end;
181
Willy Tarreau419bd492019-08-12 17:27:09 +0200182 /* log the logging location truncated to 10 chars from the right so that
183 * the line number and the end of the file name are there.
184 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200185 line[words++] = ist("[");
Willy Tarreaua9f5b962019-08-28 10:08:58 +0200186 tnum[0] = '0' + tid / 10;
187 tnum[1] = '0' + tid % 10;
188 tnum[2] = '|';
189 tnum[3] = 0;
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200190 line[words++] = ist(tnum);
191 line[words++] = src->name;
192 line[words++] = ist("|");
Frédéric Lécaillefd183142020-12-02 16:51:00 +0100193 line[words++] = ist2("012345" + level, 1); // "0" to "5"
Willy Tarreau052ad362019-08-29 09:08:36 +0200194 line[words++] = ist("|");
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200195 line[words] = where;
196 if (line[words].len > 13) {
197 line[words].ptr += (line[words].len - 13);
198 line[words].len = 13;
Willy Tarreau419bd492019-08-12 17:27:09 +0200199 }
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200200 words++;
201 line[words++] = ist("] ");
202
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100203 if (isttest(ist_func)) {
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200204 line[words++] = ist_func;
205 line[words++] = ist("(): ");
206 }
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200207
Willy Tarreau3da00262019-08-28 07:03:58 +0200208 if (!cb)
209 cb = src->default_cb;
210
Willy Tarreau370a6942019-08-29 08:24:16 +0200211 if (cb && src->verbosity) {
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200212 /* decode function passed, we want to pre-fill the
213 * buffer with the message and let the decode function
214 * do its job, possibly even overwriting it.
215 */
216 b_reset(&trace_buf);
217 b_istput(&trace_buf, msg);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200218 cb(level, mask, src, where, ist_func, a1, a2, a3, a4);
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100219 line[words] = ist2(trace_buf.area, trace_buf.data);
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200220 words++;
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200221 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200222 else {
223 /* Note that here we could decide to print some args whose type
224 * is known, when verbosity is above the quiet level, and even
225 * to print the name and values of those which are declared for
226 * lock-on.
227 */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200228 line[words++] = msg;
Willy Tarreau370a6942019-08-29 08:24:16 +0200229 }
Willy Tarreau419bd492019-08-12 17:27:09 +0200230
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200231 if (src->sink)
Emeric Brun54648852020-07-06 15:54:06 +0200232 sink_write(src->sink, line, words, 0, 0, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200233
234 end:
235 /* check if we need to stop the trace now */
236 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200237 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200238 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
239 }
240 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200241 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200242 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
243 }
244}
245
Willy Tarreau864e8802019-08-08 18:48:12 +0200246struct trace_source *trace_find_source(const char *name)
247{
248 struct trace_source *src;
249 const struct ist iname = ist(name);
250
251 list_for_each_entry(src, &trace_sources, source_link)
252 if (isteq(src->name, iname))
253 return src;
254 return NULL;
255}
256
257const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
258{
259 for (; ev && ev->mask; ev++)
260 if (strcmp(ev->name, name) == 0)
261 return ev;
262 return NULL;
263}
264
265/* parse the command, returns 1 if a message is returned, otherwise zero */
266static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
267{
268 struct trace_source *src;
269 uint64_t *ev_ptr = NULL;
270
271 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
272 return 1;
273
274 if (!*args[1]) {
275 /* no arg => report the list of supported sources as a warning */
276 chunk_printf(&trash,
277 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
278 " [.] 0 : not a source, will immediately stop all traces\n"
279 );
280
281 list_for_each_entry(src, &trace_sources, source_link)
282 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
283
284 trash.area[trash.data] = 0;
285 return cli_msg(appctx, LOG_WARNING, trash.area);
286 }
287
288 if (strcmp(args[1], "0") == 0) {
289 /* emergency stop of all traces */
290 list_for_each_entry(src, &trace_sources, source_link)
291 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
292 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
293 }
294
295 src = trace_find_source(args[1]);
296 if (!src)
297 return cli_err(appctx, "No such trace source");
298
299 if (!*args[2]) {
300 return cli_msg(appctx, LOG_WARNING,
301 "Supported commands:\n"
Willy Tarreau370a6942019-08-29 08:24:16 +0200302 " event : list/enable/disable source-specific event reporting\n"
303 //" filter : list/enable/disable generic filters\n"
304 " level : list/set trace reporting level\n"
305 " lock : automatic lock on thread/connection/stream/...\n"
306 " pause : pause and automatically restart after a specific event\n"
307 " sink : list/set event sinks\n"
308 " start : start immediately or after a specific event\n"
309 " stop : stop immediately or after a specific event\n"
310 " verbosity : list/set trace output verbosity\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200311 );
312 }
313 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
314 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
315 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
316 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
317 const struct trace_event *ev;
318 const char *name = args[3];
319 int neg = 0;
320 int i;
321
322 /* skip prefix '!', '-', '+' and remind negation */
323 while (*name) {
324 if (*name == '!' || *name == '-')
325 neg = 1;
326 else if (*name == '+')
327 neg = 0;
328 else
329 break;
330 name++;
331 }
332
333 if (!*name) {
334 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
335 if (ev_ptr != &src->report_events)
Willy Tarreau99282dd2019-08-30 07:37:32 +0200336 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
337 chunk_appendf(&trash, " - none : disable all event types\n");
338 chunk_appendf(&trash, " - any : enable all event types\n");
Willy Tarreau864e8802019-08-08 18:48:12 +0200339 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200340 chunk_appendf(&trash, " %c %-12s : %s\n",
Willy Tarreau864e8802019-08-08 18:48:12 +0200341 trace_event_char(*ev_ptr, src->known_events[i].mask),
342 src->known_events[i].name, src->known_events[i].desc);
343 }
344 trash.area[trash.data] = 0;
345 return cli_msg(appctx, LOG_WARNING, trash.area);
346 }
347
348 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
349 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200350 if (ev_ptr == &src->pause_events) {
351 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200352 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200353 }
354 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200355 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200356 }
357 else if (ev_ptr == &src->stop_events) {
358 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200359 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200360 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200361 return 0;
362 }
363
364 if (strcmp(name, "none") == 0)
365 HA_ATOMIC_STORE(ev_ptr, 0);
366 else if (strcmp(name, "any") == 0)
367 HA_ATOMIC_STORE(ev_ptr, ~0);
368 else {
369 ev = trace_find_event(src->known_events, name);
370 if (!ev)
371 return cli_err(appctx, "No such trace event");
372
373 if (!neg)
374 HA_ATOMIC_OR(ev_ptr, ev->mask);
375 else
376 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
377 }
378 }
379 else if (strcmp(args[2], "sink") == 0) {
380 const char *name = args[3];
381 struct sink *sink;
382
383 if (!*name) {
384 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
385 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
386 list_for_each_entry(sink, &sink_list, sink_list) {
387 chunk_appendf(&trash, " %c %-10s : %s\n",
388 src->sink == sink ? '*' : ' ',
389 sink->name, sink->desc);
390 }
391 trash.area[trash.data] = 0;
392 return cli_msg(appctx, LOG_WARNING, trash.area);
393 }
394
395 if (strcmp(name, "none") == 0)
396 sink = NULL;
397 else {
398 sink = sink_find(name);
399 if (!sink)
400 return cli_err(appctx, "No such sink");
401 }
402
403 HA_ATOMIC_STORE(&src->sink, sink);
404 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200405 else if (strcmp(args[2], "level") == 0) {
406 const char *name = args[3];
407
408 if (!*name) {
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200409 chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr);
Willy Tarreaua1f12742020-12-01 09:46:46 +0100410 chunk_appendf(&trash, " %c error : report errors\n",
411 src->level == TRACE_LEVEL_ERROR ? '*' : ' ');
412 chunk_appendf(&trash, " %c user : also information useful to the end user\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200413 src->level == TRACE_LEVEL_USER ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200414 chunk_appendf(&trash, " %c proto : also protocol-level updates\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200415 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200416 chunk_appendf(&trash, " %c state : also report internal state changes\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200417 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200418 chunk_appendf(&trash, " %c data : also report data transfers\n",
419 src->level == TRACE_LEVEL_DATA ? '*' : ' ');
420 chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n",
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200421 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
422 trash.area[trash.data] = 0;
423 return cli_msg(appctx, LOG_WARNING, trash.area);
424 }
425
Willy Tarreaua1f12742020-12-01 09:46:46 +0100426 if (strcmp(name, "error") == 0)
427 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_ERROR);
428 else if (strcmp(name, "user") == 0)
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200429 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200430 else if (strcmp(name, "proto") == 0)
431 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
432 else if (strcmp(name, "state") == 0)
433 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
Willy Tarreau2ea549b2019-08-29 08:01:48 +0200434 else if (strcmp(name, "data") == 0)
435 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA);
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200436 else if (strcmp(name, "developer") == 0)
437 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
438 else
439 return cli_err(appctx, "No such trace level");
440 }
Willy Tarreau60e4c9f2019-08-20 19:24:10 +0200441 else if (strcmp(args[2], "lock") == 0) {
442 const char *name = args[3];
443
444 if (!*name) {
445 chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr);
446 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
447 chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n",
448 src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' ');
449
450 if (src->arg_def & TRC_ARGS_CONN)
451 chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n",
452 src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' ');
453
454 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
455 chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n",
456 src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' ');
457
458 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
459 chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n",
460 src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' ');
461
462 chunk_appendf(&trash, " %c nothing : do not lock on anything\n",
463 src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' ');
464
465 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM))
466 chunk_appendf(&trash, " %c server : lock on the server that started the trace\n",
467 src->lockon == TRACE_LOCKON_SERVER ? '*' : ' ');
468
469 if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM))
470 chunk_appendf(&trash, " %c session : lock on the session that started the trace\n",
471 src->lockon == TRACE_LOCKON_SESSION ? '*' : ' ');
472
473 if (src->arg_def & TRC_ARGS_STRM)
474 chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n",
475 src->lockon == TRACE_LOCKON_STREAM ? '*' : ' ');
476
477 chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n",
478 src->lockon == TRACE_LOCKON_THREAD ? '*' : ' ');
479
480 if (src->lockon_args && src->lockon_args[0].name)
481 chunk_appendf(&trash, " %c %-10s : %s\n",
482 src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ',
483 src->lockon_args[0].name, src->lockon_args[0].desc);
484
485 if (src->lockon_args && src->lockon_args[1].name)
486 chunk_appendf(&trash, " %c %-10s : %s\n",
487 src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ',
488 src->lockon_args[1].name, src->lockon_args[1].desc);
489
490 if (src->lockon_args && src->lockon_args[2].name)
491 chunk_appendf(&trash, " %c %-10s : %s\n",
492 src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ',
493 src->lockon_args[2].name, src->lockon_args[2].desc);
494
495 if (src->lockon_args && src->lockon_args[3].name)
496 chunk_appendf(&trash, " %c %-10s : %s\n",
497 src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ',
498 src->lockon_args[3].name, src->lockon_args[3].desc);
499
500 trash.area[trash.data] = 0;
501 return cli_msg(appctx, LOG_WARNING, trash.area);
502 }
503 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) {
504 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND);
505 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
506 }
507 else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) {
508 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION);
509 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
510 }
511 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) {
512 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND);
513 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
514 }
515 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) {
516 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER);
517 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
518 }
519 else if (strcmp(name, "nothing") == 0) {
520 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING);
521 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
522 }
523 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) {
524 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER);
525 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
526 }
527 else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) {
528 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION);
529 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
530 }
531 else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) {
532 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM);
533 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
534 }
535 else if (strcmp(name, "thread") == 0) {
536 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD);
537 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
538 }
539 else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) {
540 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1);
541 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
542 }
543 else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) {
544 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2);
545 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
546 }
547 else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) {
548 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3);
549 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
550 }
551 else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) {
552 HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4);
553 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
554 }
555 else
556 return cli_err(appctx, "Unsupported lock-on criterion");
557 }
Willy Tarreau370a6942019-08-29 08:24:16 +0200558 else if (strcmp(args[2], "verbosity") == 0) {
559 const char *name = args[3];
560 const struct name_desc *nd;
561
562 if (!*name) {
563 chunk_printf(&trash, "Supported trace verbosities for source %s:\n", src->name.ptr);
564 chunk_appendf(&trash, " %c quiet : only report basic information with no decoding\n",
565 src->verbosity == 0 ? '*' : ' ');
566 if (!src->decoding || !src->decoding[0].name) {
567 chunk_appendf(&trash, " %c default : report extra information when available\n",
568 src->verbosity > 0 ? '*' : ' ');
569 } else {
570 for (nd = src->decoding; nd->name && nd->desc; nd++)
571 chunk_appendf(&trash, " %c %-10s : %s\n",
572 nd == (src->decoding + src->verbosity - 1) ? '*' : ' ',
573 nd->name, nd->desc);
574 }
575 trash.area[trash.data] = 0;
576 return cli_msg(appctx, LOG_WARNING, trash.area);
577 }
578
579 if (strcmp(name, "quiet") == 0)
580 HA_ATOMIC_STORE(&src->verbosity, 0);
581 else if (!src->decoding || !src->decoding[0].name) {
582 if (strcmp(name, "default") == 0)
583 HA_ATOMIC_STORE(&src->verbosity, 1);
584 else
585 return cli_err(appctx, "No such verbosity level");
586 } else {
587 for (nd = src->decoding; nd->name && nd->desc; nd++)
588 if (strcmp(name, nd->name) == 0)
589 break;
590
591 if (!nd->name || !nd->desc)
592 return cli_err(appctx, "No such verbosity level");
593
594 HA_ATOMIC_STORE(&src->verbosity, (nd - src->decoding) + 1);
595 }
596 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200597 else
598 return cli_err(appctx, "Unknown trace keyword");
599
600 return 0;
601}
602
Willy Tarreau85b15752019-08-12 16:44:33 +0200603/* parse the command, returns 1 if a message is returned, otherwise zero */
604static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
605{
606 struct trace_source *src;
607 const struct sink *sink;
608 int i;
609
610 args++; // make args[1] the 1st arg
611
612 if (!*args[1]) {
613 /* no arg => report the list of supported sources */
614 chunk_printf(&trash,
615 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
616 );
617
618 list_for_each_entry(src, &trace_sources, source_link) {
619 sink = src->sink;
620 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
621 trace_state_char(src->state), src->name.ptr,
622 sink ? sink->name : "none",
623 sink ? sink->ctx.dropped : 0,
624 src->desc);
625 }
626
627 trash.area[trash.data] = 0;
628 return cli_msg(appctx, LOG_INFO, trash.area);
629 }
630
631 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
632 return 1;
633
634 src = trace_find_source(args[1]);
635 if (!src)
636 return cli_err(appctx, "No such trace source");
637
638 sink = src->sink;
639 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
640 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
641 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
642
Willy Tarreau99282dd2019-08-30 07:37:32 +0200643 chunk_appendf(&trash, " - event name : report start stop pause\n");
Willy Tarreau85b15752019-08-12 16:44:33 +0200644 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
Willy Tarreau99282dd2019-08-30 07:37:32 +0200645 chunk_appendf(&trash, " %-12s : %c %c %c %c\n",
Willy Tarreau85b15752019-08-12 16:44:33 +0200646 src->known_events[i].name,
647 trace_event_char(src->report_events, src->known_events[i].mask),
648 trace_event_char(src->start_events, src->known_events[i].mask),
649 trace_event_char(src->stop_events, src->known_events[i].mask),
650 trace_event_char(src->pause_events, src->known_events[i].mask));
651 }
652
653 trash.area[trash.data] = 0;
654 return cli_msg(appctx, LOG_WARNING, trash.area);
655}
656
Willy Tarreau864e8802019-08-08 18:48:12 +0200657static struct cli_kw_list cli_kws = {{ },{
658 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200659 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200660 {{},}
661}};
662
663INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
664
Willy Tarreau4151c752019-08-08 18:21:26 +0200665/*
666 * Local variables:
667 * c-indent-level: 8
668 * c-basic-offset: 8
669 * End:
670 */