blob: 77837021491878b7d9bf46772d8516ea80150fd4 [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 Tarreau4c2ae482019-08-12 15:51:58 +020051/* write a message for the given trace source */
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020052void __trace(enum trace_level level, uint64_t mask, struct trace_source *src, const struct ist where,
53 const void *a1, const void *a2, const void *a3, const void *a4,
54 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, const struct ist where,
55 const void *a1, const void *a2, const void *a3, const void *a4),
56 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020057{
Willy Tarreau419bd492019-08-12 17:27:09 +020058 struct ist line[8];
59
Willy Tarreau4c2ae482019-08-12 15:51:58 +020060 if (likely(src->state == TRACE_STATE_STOPPED))
61 return;
62
63 /* check that at least one action is interested by this event */
64 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
65 return;
66
67 /* TODO: add handling of filters here, return if no match (not even update states) */
68
69 /* check if we need to start the trace now */
70 if (src->state == TRACE_STATE_WAITING) {
71 if ((src->start_events & mask) == 0)
72 return;
73
74 /* TODO: add update of lockon+lockon_ptr here */
75 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
76 }
77
78 /* TODO: add check of lockon+lockon_ptr here, return if no match */
79 /* here the trace is running and is tracking a desired item */
80
Willy Tarreau5da40882019-08-12 17:57:57 +020081 if ((src->report_events & mask) == 0 || level > src->level)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020082 goto end;
83
Willy Tarreau419bd492019-08-12 17:27:09 +020084 /* log the logging location truncated to 10 chars from the right so that
85 * the line number and the end of the file name are there.
86 */
87 line[0] = ist("[");
88 line[1] = where;
89 if (line[1].len > 10) {
90 line[1].ptr += (line[1].len - 10);
91 line[1].len = 10;
92 }
93 line[2] = ist("] ");
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020094
95 if (cb) {
96 /* decode function passed, we want to pre-fill the
97 * buffer with the message and let the decode function
98 * do its job, possibly even overwriting it.
99 */
100 b_reset(&trace_buf);
101 b_istput(&trace_buf, msg);
102 cb(level, mask, src, where, a1, a2, a3, a4);
103 line[3].ptr = trace_buf.area;
104 line[3].len = trace_buf.data;
105 }
106 else
107 line[3] = msg;
Willy Tarreau419bd492019-08-12 17:27:09 +0200108
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200109 if (src->sink)
Willy Tarreau419bd492019-08-12 17:27:09 +0200110 sink_write(src->sink, line, 4);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200111
112 end:
113 /* check if we need to stop the trace now */
114 if ((src->stop_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200115 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200116 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
117 }
118 else if ((src->pause_events & mask) != 0) {
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200119 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200120 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
121 }
122}
123
Willy Tarreau864e8802019-08-08 18:48:12 +0200124struct trace_source *trace_find_source(const char *name)
125{
126 struct trace_source *src;
127 const struct ist iname = ist(name);
128
129 list_for_each_entry(src, &trace_sources, source_link)
130 if (isteq(src->name, iname))
131 return src;
132 return NULL;
133}
134
135const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
136{
137 for (; ev && ev->mask; ev++)
138 if (strcmp(ev->name, name) == 0)
139 return ev;
140 return NULL;
141}
142
143/* parse the command, returns 1 if a message is returned, otherwise zero */
144static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
145{
146 struct trace_source *src;
147 uint64_t *ev_ptr = NULL;
148
149 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
150 return 1;
151
152 if (!*args[1]) {
153 /* no arg => report the list of supported sources as a warning */
154 chunk_printf(&trash,
155 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
156 " [.] 0 : not a source, will immediately stop all traces\n"
157 );
158
159 list_for_each_entry(src, &trace_sources, source_link)
160 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
161
162 trash.area[trash.data] = 0;
163 return cli_msg(appctx, LOG_WARNING, trash.area);
164 }
165
166 if (strcmp(args[1], "0") == 0) {
167 /* emergency stop of all traces */
168 list_for_each_entry(src, &trace_sources, source_link)
169 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
170 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
171 }
172
173 src = trace_find_source(args[1]);
174 if (!src)
175 return cli_err(appctx, "No such trace source");
176
177 if (!*args[2]) {
178 return cli_msg(appctx, LOG_WARNING,
179 "Supported commands:\n"
180 " event : list/enable/disable source-specific event reporting\n"
181 //" filter : list/enable/disable generic filters\n"
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200182 " level : list/set detail level\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200183 //" lock : automatic lock on thread/connection/stream/...\n"
184 " pause : pause and automatically restart after a specific event\n"
185 " sink : list/set event sinks\n"
186 " start : start immediately or after a specific event\n"
187 " stop : stop immediately or after a specific event\n"
188 );
189 }
190 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
191 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
192 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
193 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
194 const struct trace_event *ev;
195 const char *name = args[3];
196 int neg = 0;
197 int i;
198
199 /* skip prefix '!', '-', '+' and remind negation */
200 while (*name) {
201 if (*name == '!' || *name == '-')
202 neg = 1;
203 else if (*name == '+')
204 neg = 0;
205 else
206 break;
207 name++;
208 }
209
210 if (!*name) {
211 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
212 if (ev_ptr != &src->report_events)
213 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
214 chunk_appendf(&trash, " - none : disable all event types\n");
215 chunk_appendf(&trash, " - any : enable all event types\n");
216 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
217 chunk_appendf(&trash, " %c %-10s : %s\n",
218 trace_event_char(*ev_ptr, src->known_events[i].mask),
219 src->known_events[i].name, src->known_events[i].desc);
220 }
221 trash.area[trash.data] = 0;
222 return cli_msg(appctx, LOG_WARNING, trash.area);
223 }
224
225 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
226 HA_ATOMIC_STORE(ev_ptr, 0);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200227 if (ev_ptr == &src->pause_events) {
228 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200229 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200230 }
231 else if (ev_ptr == &src->start_events) {
Willy Tarreau864e8802019-08-08 18:48:12 +0200232 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200233 }
234 else if (ev_ptr == &src->stop_events) {
235 HA_ATOMIC_STORE(&src->lockon_ptr, NULL);
Willy Tarreau864e8802019-08-08 18:48:12 +0200236 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
Willy Tarreaubeadb5c2019-08-20 18:57:48 +0200237 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200238 return 0;
239 }
240
241 if (strcmp(name, "none") == 0)
242 HA_ATOMIC_STORE(ev_ptr, 0);
243 else if (strcmp(name, "any") == 0)
244 HA_ATOMIC_STORE(ev_ptr, ~0);
245 else {
246 ev = trace_find_event(src->known_events, name);
247 if (!ev)
248 return cli_err(appctx, "No such trace event");
249
250 if (!neg)
251 HA_ATOMIC_OR(ev_ptr, ev->mask);
252 else
253 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
254 }
255 }
256 else if (strcmp(args[2], "sink") == 0) {
257 const char *name = args[3];
258 struct sink *sink;
259
260 if (!*name) {
261 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
262 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
263 list_for_each_entry(sink, &sink_list, sink_list) {
264 chunk_appendf(&trash, " %c %-10s : %s\n",
265 src->sink == sink ? '*' : ' ',
266 sink->name, sink->desc);
267 }
268 trash.area[trash.data] = 0;
269 return cli_msg(appctx, LOG_WARNING, trash.area);
270 }
271
272 if (strcmp(name, "none") == 0)
273 sink = NULL;
274 else {
275 sink = sink_find(name);
276 if (!sink)
277 return cli_err(appctx, "No such sink");
278 }
279
280 HA_ATOMIC_STORE(&src->sink, sink);
281 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200282 else if (strcmp(args[2], "level") == 0) {
283 const char *name = args[3];
284
285 if (!*name) {
286 chunk_printf(&trash, "Supported detail levels for source %s:\n", src->name.ptr);
287 chunk_appendf(&trash, " %c user : information useful to the end user\n",
288 src->level == TRACE_LEVEL_USER ? '*' : ' ');
289 chunk_appendf(&trash, " %c payload : add information relevant to the payload\n",
290 src->level == TRACE_LEVEL_PAYLOAD ? '*' : ' ');
291 chunk_appendf(&trash, " %c proto : add information relevant to the protocol\n",
292 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
293 chunk_appendf(&trash, " %c state : add information relevant to the state machine\n",
294 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
295 chunk_appendf(&trash, " %c developer : add information useful only to the developer\n",
296 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
297 trash.area[trash.data] = 0;
298 return cli_msg(appctx, LOG_WARNING, trash.area);
299 }
300
301 if (strcmp(name, "user") == 0)
302 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
303 else if (strcmp(name, "payload") == 0)
304 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PAYLOAD);
305 else if (strcmp(name, "proto") == 0)
306 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
307 else if (strcmp(name, "state") == 0)
308 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
309 else if (strcmp(name, "developer") == 0)
310 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
311 else
312 return cli_err(appctx, "No such trace level");
313 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200314 else
315 return cli_err(appctx, "Unknown trace keyword");
316
317 return 0;
318}
319
Willy Tarreau85b15752019-08-12 16:44:33 +0200320/* parse the command, returns 1 if a message is returned, otherwise zero */
321static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
322{
323 struct trace_source *src;
324 const struct sink *sink;
325 int i;
326
327 args++; // make args[1] the 1st arg
328
329 if (!*args[1]) {
330 /* no arg => report the list of supported sources */
331 chunk_printf(&trash,
332 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
333 );
334
335 list_for_each_entry(src, &trace_sources, source_link) {
336 sink = src->sink;
337 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
338 trace_state_char(src->state), src->name.ptr,
339 sink ? sink->name : "none",
340 sink ? sink->ctx.dropped : 0,
341 src->desc);
342 }
343
344 trash.area[trash.data] = 0;
345 return cli_msg(appctx, LOG_INFO, trash.area);
346 }
347
348 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
349 return 1;
350
351 src = trace_find_source(args[1]);
352 if (!src)
353 return cli_err(appctx, "No such trace source");
354
355 sink = src->sink;
356 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
357 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
358 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
359
360 chunk_appendf(&trash, " - event name : report start stop pause\n");
361 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
362 chunk_appendf(&trash, " %-10s : %c %c %c %c\n",
363 src->known_events[i].name,
364 trace_event_char(src->report_events, src->known_events[i].mask),
365 trace_event_char(src->start_events, src->known_events[i].mask),
366 trace_event_char(src->stop_events, src->known_events[i].mask),
367 trace_event_char(src->pause_events, src->known_events[i].mask));
368 }
369
370 trash.area[trash.data] = 0;
371 return cli_msg(appctx, LOG_WARNING, trash.area);
372}
373
Willy Tarreau864e8802019-08-08 18:48:12 +0200374static struct cli_kw_list cli_kws = {{ },{
375 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200376 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200377 {{},}
378}};
379
380INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
381
Willy Tarreau4151c752019-08-08 18:21:26 +0200382/*
383 * Local variables:
384 * c-indent-level: 8
385 * c-basic-offset: 8
386 * End:
387 */