blob: eec34779bdcf9fc102f614662cec79647ef25fed [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) {
115 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
116 }
117 else if ((src->pause_events & mask) != 0) {
118 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
119 }
120}
121
Willy Tarreau864e8802019-08-08 18:48:12 +0200122struct trace_source *trace_find_source(const char *name)
123{
124 struct trace_source *src;
125 const struct ist iname = ist(name);
126
127 list_for_each_entry(src, &trace_sources, source_link)
128 if (isteq(src->name, iname))
129 return src;
130 return NULL;
131}
132
133const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
134{
135 for (; ev && ev->mask; ev++)
136 if (strcmp(ev->name, name) == 0)
137 return ev;
138 return NULL;
139}
140
141/* parse the command, returns 1 if a message is returned, otherwise zero */
142static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
143{
144 struct trace_source *src;
145 uint64_t *ev_ptr = NULL;
146
147 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
148 return 1;
149
150 if (!*args[1]) {
151 /* no arg => report the list of supported sources as a warning */
152 chunk_printf(&trash,
153 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
154 " [.] 0 : not a source, will immediately stop all traces\n"
155 );
156
157 list_for_each_entry(src, &trace_sources, source_link)
158 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
159
160 trash.area[trash.data] = 0;
161 return cli_msg(appctx, LOG_WARNING, trash.area);
162 }
163
164 if (strcmp(args[1], "0") == 0) {
165 /* emergency stop of all traces */
166 list_for_each_entry(src, &trace_sources, source_link)
167 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
168 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
169 }
170
171 src = trace_find_source(args[1]);
172 if (!src)
173 return cli_err(appctx, "No such trace source");
174
175 if (!*args[2]) {
176 return cli_msg(appctx, LOG_WARNING,
177 "Supported commands:\n"
178 " event : list/enable/disable source-specific event reporting\n"
179 //" filter : list/enable/disable generic filters\n"
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200180 " level : list/set detail level\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200181 //" lock : automatic lock on thread/connection/stream/...\n"
182 " pause : pause and automatically restart after a specific event\n"
183 " sink : list/set event sinks\n"
184 " start : start immediately or after a specific event\n"
185 " stop : stop immediately or after a specific event\n"
186 );
187 }
188 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
189 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
190 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
191 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
192 const struct trace_event *ev;
193 const char *name = args[3];
194 int neg = 0;
195 int i;
196
197 /* skip prefix '!', '-', '+' and remind negation */
198 while (*name) {
199 if (*name == '!' || *name == '-')
200 neg = 1;
201 else if (*name == '+')
202 neg = 0;
203 else
204 break;
205 name++;
206 }
207
208 if (!*name) {
209 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
210 if (ev_ptr != &src->report_events)
211 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
212 chunk_appendf(&trash, " - none : disable all event types\n");
213 chunk_appendf(&trash, " - any : enable all event types\n");
214 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
215 chunk_appendf(&trash, " %c %-10s : %s\n",
216 trace_event_char(*ev_ptr, src->known_events[i].mask),
217 src->known_events[i].name, src->known_events[i].desc);
218 }
219 trash.area[trash.data] = 0;
220 return cli_msg(appctx, LOG_WARNING, trash.area);
221 }
222
223 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
224 HA_ATOMIC_STORE(ev_ptr, 0);
225 if (ev_ptr == &src->pause_events)
226 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
227 else if (ev_ptr == &src->start_events)
228 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
229 else if (ev_ptr == &src->stop_events)
230 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
231 return 0;
232 }
233
234 if (strcmp(name, "none") == 0)
235 HA_ATOMIC_STORE(ev_ptr, 0);
236 else if (strcmp(name, "any") == 0)
237 HA_ATOMIC_STORE(ev_ptr, ~0);
238 else {
239 ev = trace_find_event(src->known_events, name);
240 if (!ev)
241 return cli_err(appctx, "No such trace event");
242
243 if (!neg)
244 HA_ATOMIC_OR(ev_ptr, ev->mask);
245 else
246 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
247 }
248 }
249 else if (strcmp(args[2], "sink") == 0) {
250 const char *name = args[3];
251 struct sink *sink;
252
253 if (!*name) {
254 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
255 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
256 list_for_each_entry(sink, &sink_list, sink_list) {
257 chunk_appendf(&trash, " %c %-10s : %s\n",
258 src->sink == sink ? '*' : ' ',
259 sink->name, sink->desc);
260 }
261 trash.area[trash.data] = 0;
262 return cli_msg(appctx, LOG_WARNING, trash.area);
263 }
264
265 if (strcmp(name, "none") == 0)
266 sink = NULL;
267 else {
268 sink = sink_find(name);
269 if (!sink)
270 return cli_err(appctx, "No such sink");
271 }
272
273 HA_ATOMIC_STORE(&src->sink, sink);
274 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200275 else if (strcmp(args[2], "level") == 0) {
276 const char *name = args[3];
277
278 if (!*name) {
279 chunk_printf(&trash, "Supported detail levels for source %s:\n", src->name.ptr);
280 chunk_appendf(&trash, " %c user : information useful to the end user\n",
281 src->level == TRACE_LEVEL_USER ? '*' : ' ');
282 chunk_appendf(&trash, " %c payload : add information relevant to the payload\n",
283 src->level == TRACE_LEVEL_PAYLOAD ? '*' : ' ');
284 chunk_appendf(&trash, " %c proto : add information relevant to the protocol\n",
285 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
286 chunk_appendf(&trash, " %c state : add information relevant to the state machine\n",
287 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
288 chunk_appendf(&trash, " %c developer : add information useful only to the developer\n",
289 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
290 trash.area[trash.data] = 0;
291 return cli_msg(appctx, LOG_WARNING, trash.area);
292 }
293
294 if (strcmp(name, "user") == 0)
295 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
296 else if (strcmp(name, "payload") == 0)
297 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PAYLOAD);
298 else if (strcmp(name, "proto") == 0)
299 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
300 else if (strcmp(name, "state") == 0)
301 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
302 else if (strcmp(name, "developer") == 0)
303 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
304 else
305 return cli_err(appctx, "No such trace level");
306 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200307 else
308 return cli_err(appctx, "Unknown trace keyword");
309
310 return 0;
311}
312
Willy Tarreau85b15752019-08-12 16:44:33 +0200313/* parse the command, returns 1 if a message is returned, otherwise zero */
314static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
315{
316 struct trace_source *src;
317 const struct sink *sink;
318 int i;
319
320 args++; // make args[1] the 1st arg
321
322 if (!*args[1]) {
323 /* no arg => report the list of supported sources */
324 chunk_printf(&trash,
325 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
326 );
327
328 list_for_each_entry(src, &trace_sources, source_link) {
329 sink = src->sink;
330 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
331 trace_state_char(src->state), src->name.ptr,
332 sink ? sink->name : "none",
333 sink ? sink->ctx.dropped : 0,
334 src->desc);
335 }
336
337 trash.area[trash.data] = 0;
338 return cli_msg(appctx, LOG_INFO, trash.area);
339 }
340
341 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
342 return 1;
343
344 src = trace_find_source(args[1]);
345 if (!src)
346 return cli_err(appctx, "No such trace source");
347
348 sink = src->sink;
349 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
350 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
351 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
352
353 chunk_appendf(&trash, " - event name : report start stop pause\n");
354 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
355 chunk_appendf(&trash, " %-10s : %c %c %c %c\n",
356 src->known_events[i].name,
357 trace_event_char(src->report_events, src->known_events[i].mask),
358 trace_event_char(src->start_events, src->known_events[i].mask),
359 trace_event_char(src->stop_events, src->known_events[i].mask),
360 trace_event_char(src->pause_events, src->known_events[i].mask));
361 }
362
363 trash.area[trash.data] = 0;
364 return cli_msg(appctx, LOG_WARNING, trash.area);
365}
366
Willy Tarreau864e8802019-08-08 18:48:12 +0200367static struct cli_kw_list cli_kws = {{ },{
368 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200369 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200370 {{},}
371}};
372
373INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
374
Willy Tarreau4151c752019-08-08 18:21:26 +0200375/*
376 * Local variables:
377 * c-indent-level: 8
378 * c-basic-offset: 8
379 * End:
380 */