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