blob: f68ff105823c1be705dc56bed5cd8c6c3a7b61ec [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 Tarreau419bd492019-08-12 17:27:09 +020052void __trace(uint64_t mask, struct trace_source *src, const struct ist where, const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +020053{
Willy Tarreau419bd492019-08-12 17:27:09 +020054 struct ist line[8];
55
Willy Tarreau4c2ae482019-08-12 15:51:58 +020056 if (likely(src->state == TRACE_STATE_STOPPED))
57 return;
58
59 /* check that at least one action is interested by this event */
60 if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0)
61 return;
62
63 /* TODO: add handling of filters here, return if no match (not even update states) */
64
65 /* check if we need to start the trace now */
66 if (src->state == TRACE_STATE_WAITING) {
67 if ((src->start_events & mask) == 0)
68 return;
69
70 /* TODO: add update of lockon+lockon_ptr here */
71 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
72 }
73
74 /* TODO: add check of lockon+lockon_ptr here, return if no match */
75 /* here the trace is running and is tracking a desired item */
76
77 if ((src->report_events & mask) == 0)
78 goto end;
79
Willy Tarreau419bd492019-08-12 17:27:09 +020080 /* log the logging location truncated to 10 chars from the right so that
81 * the line number and the end of the file name are there.
82 */
83 line[0] = ist("[");
84 line[1] = where;
85 if (line[1].len > 10) {
86 line[1].ptr += (line[1].len - 10);
87 line[1].len = 10;
88 }
89 line[2] = ist("] ");
90 line[3] = msg;
91
Willy Tarreau4c2ae482019-08-12 15:51:58 +020092 if (src->sink)
Willy Tarreau419bd492019-08-12 17:27:09 +020093 sink_write(src->sink, line, 4);
Willy Tarreau4c2ae482019-08-12 15:51:58 +020094
95 end:
96 /* check if we need to stop the trace now */
97 if ((src->stop_events & mask) != 0) {
98 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
99 }
100 else if ((src->pause_events & mask) != 0) {
101 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
102 }
103}
104
Willy Tarreau864e8802019-08-08 18:48:12 +0200105struct trace_source *trace_find_source(const char *name)
106{
107 struct trace_source *src;
108 const struct ist iname = ist(name);
109
110 list_for_each_entry(src, &trace_sources, source_link)
111 if (isteq(src->name, iname))
112 return src;
113 return NULL;
114}
115
116const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name)
117{
118 for (; ev && ev->mask; ev++)
119 if (strcmp(ev->name, name) == 0)
120 return ev;
121 return NULL;
122}
123
124/* parse the command, returns 1 if a message is returned, otherwise zero */
125static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private)
126{
127 struct trace_source *src;
128 uint64_t *ev_ptr = NULL;
129
130 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
131 return 1;
132
133 if (!*args[1]) {
134 /* no arg => report the list of supported sources as a warning */
135 chunk_printf(&trash,
136 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
137 " [.] 0 : not a source, will immediately stop all traces\n"
138 );
139
140 list_for_each_entry(src, &trace_sources, source_link)
141 chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc);
142
143 trash.area[trash.data] = 0;
144 return cli_msg(appctx, LOG_WARNING, trash.area);
145 }
146
147 if (strcmp(args[1], "0") == 0) {
148 /* emergency stop of all traces */
149 list_for_each_entry(src, &trace_sources, source_link)
150 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
151 return cli_msg(appctx, LOG_NOTICE, "All traces now stopped");
152 }
153
154 src = trace_find_source(args[1]);
155 if (!src)
156 return cli_err(appctx, "No such trace source");
157
158 if (!*args[2]) {
159 return cli_msg(appctx, LOG_WARNING,
160 "Supported commands:\n"
161 " event : list/enable/disable source-specific event reporting\n"
162 //" filter : list/enable/disable generic filters\n"
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200163 " level : list/set detail level\n"
Willy Tarreau864e8802019-08-08 18:48:12 +0200164 //" lock : automatic lock on thread/connection/stream/...\n"
165 " pause : pause and automatically restart after a specific event\n"
166 " sink : list/set event sinks\n"
167 " start : start immediately or after a specific event\n"
168 " stop : stop immediately or after a specific event\n"
169 );
170 }
171 else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) ||
172 (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) ||
173 (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) ||
174 (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) {
175 const struct trace_event *ev;
176 const char *name = args[3];
177 int neg = 0;
178 int i;
179
180 /* skip prefix '!', '-', '+' and remind negation */
181 while (*name) {
182 if (*name == '!' || *name == '-')
183 neg = 1;
184 else if (*name == '+')
185 neg = 0;
186 else
187 break;
188 name++;
189 }
190
191 if (!*name) {
192 chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr);
193 if (ev_ptr != &src->report_events)
194 chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n");
195 chunk_appendf(&trash, " - none : disable all event types\n");
196 chunk_appendf(&trash, " - any : enable all event types\n");
197 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
198 chunk_appendf(&trash, " %c %-10s : %s\n",
199 trace_event_char(*ev_ptr, src->known_events[i].mask),
200 src->known_events[i].name, src->known_events[i].desc);
201 }
202 trash.area[trash.data] = 0;
203 return cli_msg(appctx, LOG_WARNING, trash.area);
204 }
205
206 if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) {
207 HA_ATOMIC_STORE(ev_ptr, 0);
208 if (ev_ptr == &src->pause_events)
209 HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING);
210 else if (ev_ptr == &src->start_events)
211 HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING);
212 else if (ev_ptr == &src->stop_events)
213 HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED);
214 return 0;
215 }
216
217 if (strcmp(name, "none") == 0)
218 HA_ATOMIC_STORE(ev_ptr, 0);
219 else if (strcmp(name, "any") == 0)
220 HA_ATOMIC_STORE(ev_ptr, ~0);
221 else {
222 ev = trace_find_event(src->known_events, name);
223 if (!ev)
224 return cli_err(appctx, "No such trace event");
225
226 if (!neg)
227 HA_ATOMIC_OR(ev_ptr, ev->mask);
228 else
229 HA_ATOMIC_AND(ev_ptr, ~ev->mask);
230 }
231 }
232 else if (strcmp(args[2], "sink") == 0) {
233 const char *name = args[3];
234 struct sink *sink;
235
236 if (!*name) {
237 chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr);
238 chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*');
239 list_for_each_entry(sink, &sink_list, sink_list) {
240 chunk_appendf(&trash, " %c %-10s : %s\n",
241 src->sink == sink ? '*' : ' ',
242 sink->name, sink->desc);
243 }
244 trash.area[trash.data] = 0;
245 return cli_msg(appctx, LOG_WARNING, trash.area);
246 }
247
248 if (strcmp(name, "none") == 0)
249 sink = NULL;
250 else {
251 sink = sink_find(name);
252 if (!sink)
253 return cli_err(appctx, "No such sink");
254 }
255
256 HA_ATOMIC_STORE(&src->sink, sink);
257 }
Willy Tarreauaaaf4112019-08-12 17:57:57 +0200258 else if (strcmp(args[2], "level") == 0) {
259 const char *name = args[3];
260
261 if (!*name) {
262 chunk_printf(&trash, "Supported detail levels for source %s:\n", src->name.ptr);
263 chunk_appendf(&trash, " %c user : information useful to the end user\n",
264 src->level == TRACE_LEVEL_USER ? '*' : ' ');
265 chunk_appendf(&trash, " %c payload : add information relevant to the payload\n",
266 src->level == TRACE_LEVEL_PAYLOAD ? '*' : ' ');
267 chunk_appendf(&trash, " %c proto : add information relevant to the protocol\n",
268 src->level == TRACE_LEVEL_PROTO ? '*' : ' ');
269 chunk_appendf(&trash, " %c state : add information relevant to the state machine\n",
270 src->level == TRACE_LEVEL_STATE ? '*' : ' ');
271 chunk_appendf(&trash, " %c developer : add information useful only to the developer\n",
272 src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' ');
273 trash.area[trash.data] = 0;
274 return cli_msg(appctx, LOG_WARNING, trash.area);
275 }
276
277 if (strcmp(name, "user") == 0)
278 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER);
279 else if (strcmp(name, "payload") == 0)
280 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PAYLOAD);
281 else if (strcmp(name, "proto") == 0)
282 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO);
283 else if (strcmp(name, "state") == 0)
284 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE);
285 else if (strcmp(name, "developer") == 0)
286 HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER);
287 else
288 return cli_err(appctx, "No such trace level");
289 }
Willy Tarreau864e8802019-08-08 18:48:12 +0200290 else
291 return cli_err(appctx, "Unknown trace keyword");
292
293 return 0;
294}
295
Willy Tarreau85b15752019-08-12 16:44:33 +0200296/* parse the command, returns 1 if a message is returned, otherwise zero */
297static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private)
298{
299 struct trace_source *src;
300 const struct sink *sink;
301 int i;
302
303 args++; // make args[1] the 1st arg
304
305 if (!*args[1]) {
306 /* no arg => report the list of supported sources */
307 chunk_printf(&trash,
308 "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n"
309 );
310
311 list_for_each_entry(src, &trace_sources, source_link) {
312 sink = src->sink;
313 chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n",
314 trace_state_char(src->state), src->name.ptr,
315 sink ? sink->name : "none",
316 sink ? sink->ctx.dropped : 0,
317 src->desc);
318 }
319
320 trash.area[trash.data] = 0;
321 return cli_msg(appctx, LOG_INFO, trash.area);
322 }
323
324 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
325 return 1;
326
327 src = trace_find_source(args[1]);
328 if (!src)
329 return cli_err(appctx, "No such trace source");
330
331 sink = src->sink;
332 chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr);
333 chunk_appendf(&trash, " - sink: %s [%u dropped]\n",
334 sink ? sink->name : "none", sink ? sink->ctx.dropped : 0);
335
336 chunk_appendf(&trash, " - event name : report start stop pause\n");
337 for (i = 0; src->known_events && src->known_events[i].mask; i++) {
338 chunk_appendf(&trash, " %-10s : %c %c %c %c\n",
339 src->known_events[i].name,
340 trace_event_char(src->report_events, src->known_events[i].mask),
341 trace_event_char(src->start_events, src->known_events[i].mask),
342 trace_event_char(src->stop_events, src->known_events[i].mask),
343 trace_event_char(src->pause_events, src->known_events[i].mask));
344 }
345
346 trash.area[trash.data] = 0;
347 return cli_msg(appctx, LOG_WARNING, trash.area);
348}
349
Willy Tarreau864e8802019-08-08 18:48:12 +0200350static struct cli_kw_list cli_kws = {{ },{
351 { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL },
Willy Tarreau85b15752019-08-12 16:44:33 +0200352 { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL },
Willy Tarreau864e8802019-08-08 18:48:12 +0200353 {{},}
354}};
355
356INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
357
Willy Tarreau4151c752019-08-08 18:21:26 +0200358/*
359 * Local variables:
360 * c-indent-level: 8
361 * c-basic-offset: 8
362 * End:
363 */