Cleanup lttng enable event command
[lttng-tools.git] / lttng / commands / enable_events.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28 #include <ctype.h>
29
30 #include "../cmd.h"
31 #include "../conf.h"
32 #include "../utils.h"
33
34 static char *opt_event_list;
35 static int opt_event_type;
36 static int opt_kernel;
37 static char *opt_session_name;
38 static int opt_pid_all;
39 static int opt_userspace;
40 static char *opt_cmd_name;
41 static int opt_enable_all;
42 static pid_t opt_pid;
43 static char *opt_probe;
44 static char *opt_function;
45 static char *opt_function_entry_symbol;
46 static char *opt_channel_name;
47
48 enum {
49 OPT_HELP = 1,
50 OPT_TRACEPOINT,
51 OPT_PROBE,
52 OPT_FUNCTION,
53 OPT_FUNCTION_ENTRY,
54 OPT_SYSCALL,
55 OPT_USERSPACE,
56 OPT_TRACEPOINT_LOGLEVEL,
57 };
58
59 static struct lttng_handle *handle;
60
61 static struct poptOption long_options[] = {
62 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
63 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
64 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
65 {"all-events", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
66 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
67 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
68 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
69 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
70 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
71 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
72 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
73 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
74 #if 0
75 /*
76 * Currently removed from lttng kernel tracer. Removed from
77 * lttng UI to discourage its use.
78 */
79 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
80 #endif
81 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
82 {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0},
83 {0, 0, 0, 0, 0, 0, 0}
84 };
85
86 /*
87 * usage
88 */
89 static void usage(FILE *ofp)
90 {
91 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
92 fprintf(ofp, "\n");
93 fprintf(ofp, " -h, --help Show this help\n");
94 fprintf(ofp, " -s, --session Apply on session name\n");
95 fprintf(ofp, " -c, --channel Apply on this channel\n");
96 fprintf(ofp, " -a, --all-events Enable all tracepoints\n");
97 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
98 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
99 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
100 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
101 fprintf(ofp, "\n");
102 fprintf(ofp, "Event options:\n");
103 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
104 fprintf(ofp, " --loglevel Tracepoint loglevel\n");
105 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
106 fprintf(ofp, " Dynamic probe.\n");
107 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
108 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
109 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
110 fprintf(ofp, " Dynamic function entry/return probe.\n");
111 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
112 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
113 #if 0
114 fprintf(ofp, " --function:entry symbol\n");
115 fprintf(ofp, " Function tracer event\n");
116 #endif
117 fprintf(ofp, " --syscall System call event\n");
118 fprintf(ofp, "\n");
119 }
120
121 /*
122 * Parse probe options.
123 */
124 static int parse_probe_opts(struct lttng_event *ev, char *opt)
125 {
126 int ret;
127 char s_hex[19];
128 char name[LTTNG_SYMBOL_NAME_LEN];
129
130 if (opt == NULL) {
131 ret = -1;
132 goto end;
133 }
134
135 /* Check for symbol+offset */
136 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
137 if (ret == 2) {
138 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
139 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
140 DBG("probe symbol %s", ev->attr.probe.symbol_name);
141 if (strlen(s_hex) == 0) {
142 ERR("Invalid probe offset %s", s_hex);
143 ret = -1;
144 goto end;
145 }
146 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
147 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
148 ev->attr.probe.addr = 0;
149 goto end;
150 }
151
152 /* Check for symbol */
153 if (isalpha(name[0])) {
154 ret = sscanf(opt, "%s", name);
155 if (ret == 1) {
156 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
157 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
158 DBG("probe symbol %s", ev->attr.probe.symbol_name);
159 ev->attr.probe.offset = 0;
160 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
161 ev->attr.probe.addr = 0;
162 goto end;
163 }
164 }
165
166 /* Check for address */
167 ret = sscanf(opt, "%s", s_hex);
168 if (ret > 0) {
169 if (strlen(s_hex) == 0) {
170 ERR("Invalid probe address %s", s_hex);
171 ret = -1;
172 goto end;
173 }
174 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
175 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
176 ev->attr.probe.offset = 0;
177 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
178 goto end;
179 }
180
181 /* No match */
182 ret = -1;
183
184 end:
185 return ret;
186 }
187
188 /*
189 * Enabling event using the lttng API.
190 */
191 static int enable_events(char *session_name)
192 {
193 int err, ret = CMD_SUCCESS;
194 char *event_name, *channel_name = NULL;
195 struct lttng_event ev;
196 struct lttng_domain dom;
197
198 if (opt_channel_name == NULL) {
199 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
200 if (err < 0) {
201 ret = CMD_FATAL;
202 goto error;
203 }
204 } else {
205 channel_name = opt_channel_name;
206 }
207
208 if (opt_kernel && opt_userspace) {
209 MSG("Choose only one of --kernel or --userspace");
210 ret = CMD_FATAL;
211 goto error;
212 }
213
214 /* Create lttng domain */
215 if (opt_kernel) {
216 dom.type = LTTNG_DOMAIN_KERNEL;
217 } else if (opt_pid != 0) {
218 dom.type = LTTNG_DOMAIN_UST_PID;
219 dom.attr.pid = opt_pid;
220 DBG("PID %d set to lttng handle", opt_pid);
221 } else if (opt_userspace && opt_cmd_name == NULL) {
222 dom.type = LTTNG_DOMAIN_UST;
223 } else if (opt_userspace && opt_cmd_name != NULL) {
224 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
225 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
226 } else {
227 ERR("Please specify a tracer (--kernel or --userspace)");
228 ret = CMD_NOT_IMPLEMENTED;
229 goto error;
230 }
231
232 handle = lttng_create_handle(session_name, &dom);
233 if (handle == NULL) {
234 ret = -1;
235 goto error;
236 }
237
238 if (opt_enable_all) {
239 /* Default setup for enable all */
240 ev.name[0] = '\0';
241 ev.type = opt_event_type;
242
243 ret = lttng_enable_event(handle, &ev, channel_name);
244 if (ret < 0) {
245 goto error;
246 }
247
248 switch (opt_event_type) {
249 case LTTNG_EVENT_TRACEPOINT:
250 MSG("All %s tracepoints are enabled in channel %s",
251 opt_kernel ? "kernel" : "UST", channel_name);
252 break;
253 case LTTNG_EVENT_SYSCALL:
254 if (opt_kernel) {
255 MSG("All kernel system calls are enabled in channel %s",
256 channel_name);
257 }
258 break;
259 case LTTNG_EVENT_ALL:
260 MSG("All %s events are enabled in channel %s",
261 opt_kernel ? "kernel" : "UST", channel_name);
262 break;
263 default:
264 /*
265 * We should not be here since lttng_enable_event should have
266 * failed on the event type.
267 */
268 goto error;
269 }
270 goto end;
271 }
272
273 /* Strip event list */
274 event_name = strtok(opt_event_list, ",");
275 while (event_name != NULL) {
276 /* Copy name and type of the event */
277 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
278 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
279 ev.type = opt_event_type;
280
281 /* Kernel tracer action */
282 if (opt_kernel) {
283 DBG("Enabling kernel event %s for channel %s",
284 event_name, channel_name);
285
286 switch (opt_event_type) {
287 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
288 ev.type = LTTNG_EVENT_TRACEPOINT;
289 /* Fall-through */
290 case LTTNG_EVENT_TRACEPOINT:
291 break;
292 case LTTNG_EVENT_PROBE:
293 ret = parse_probe_opts(&ev, opt_probe);
294 if (ret < 0) {
295 ERR("Unable to parse probe options");
296 ret = 0;
297 goto error;
298 }
299 break;
300 case LTTNG_EVENT_FUNCTION:
301 ret = parse_probe_opts(&ev, opt_function);
302 if (ret < 0) {
303 ERR("Unable to parse function probe options");
304 ret = 0;
305 goto error;
306 }
307 break;
308 case LTTNG_EVENT_FUNCTION_ENTRY:
309 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
310 LTTNG_SYMBOL_NAME_LEN);
311 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
312 break;
313 case LTTNG_EVENT_SYSCALL:
314 MSG("per-syscall selection not supported yet. Use \"-a\" "
315 "for all syscalls.");
316 default:
317 ret = CMD_NOT_IMPLEMENTED;
318 goto error;
319 }
320 } else if (opt_userspace) { /* User-space tracer action */
321 if (!opt_pid_all) {
322 MSG("Only supporting tracing all UST processes "
323 "(-u --all) for now.");
324 ret = CMD_NOT_IMPLEMENTED;
325 goto error;
326 }
327
328 DBG("Enabling UST event %s for channel %s", event_name,
329 channel_name);
330
331 switch (opt_event_type) {
332 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
333 ev.type = LTTNG_EVENT_TRACEPOINT;
334 /* Fall-through */
335 case LTTNG_EVENT_TRACEPOINT:
336 break;
337 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
338 ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
339 break;
340 case LTTNG_EVENT_PROBE:
341 case LTTNG_EVENT_FUNCTION:
342 case LTTNG_EVENT_FUNCTION_ENTRY:
343 case LTTNG_EVENT_SYSCALL:
344 default:
345 ret = CMD_NOT_IMPLEMENTED;
346 goto error;
347 }
348 } else {
349 ERR("Please specify a tracer (--kernel or --userspace)");
350 goto error;
351 }
352
353 ret = lttng_enable_event(handle, &ev, channel_name);
354 if (ret == 0) {
355 MSG("%s event %s created in channel %s",
356 opt_kernel ? "kernel": "UST", event_name, channel_name);
357 }
358
359 /* Next event */
360 event_name = strtok(NULL, ",");
361 }
362
363 end:
364 error:
365 if (opt_channel_name == NULL) {
366 free(channel_name);
367 }
368 lttng_destroy_handle(handle);
369
370 return ret;
371 }
372
373 /*
374 * Add event to trace session
375 */
376 int cmd_enable_events(int argc, const char **argv)
377 {
378 int opt, ret;
379 static poptContext pc;
380 char *session_name = NULL;
381
382 pc = poptGetContext(NULL, argc, argv, long_options, 0);
383 poptReadDefaultConfig(pc, 0);
384
385 /* Default event type */
386 opt_event_type = LTTNG_EVENT_ALL;
387
388 while ((opt = poptGetNextOpt(pc)) != -1) {
389 switch (opt) {
390 case OPT_HELP:
391 usage(stderr);
392 ret = CMD_SUCCESS;
393 goto end;
394 case OPT_TRACEPOINT:
395 opt_event_type = LTTNG_EVENT_TRACEPOINT;
396 break;
397 case OPT_PROBE:
398 opt_event_type = LTTNG_EVENT_PROBE;
399 break;
400 case OPT_FUNCTION:
401 opt_event_type = LTTNG_EVENT_FUNCTION;
402 break;
403 case OPT_FUNCTION_ENTRY:
404 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
405 break;
406 case OPT_SYSCALL:
407 opt_event_type = LTTNG_EVENT_SYSCALL;
408 break;
409 case OPT_USERSPACE:
410 opt_userspace = 1;
411 break;
412 case OPT_TRACEPOINT_LOGLEVEL:
413 opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
414 break;
415 default:
416 usage(stderr);
417 ret = CMD_UNDEFINED;
418 goto end;
419 }
420 }
421
422 opt_event_list = (char*) poptGetArg(pc);
423 if (opt_event_list == NULL && opt_enable_all == 0) {
424 ERR("Missing event name(s).\n");
425 usage(stderr);
426 ret = CMD_SUCCESS;
427 goto end;
428 }
429
430 if (!opt_session_name) {
431 session_name = get_session_name();
432 if (session_name == NULL) {
433 ret = -1;
434 goto end;
435 }
436 } else {
437 session_name = opt_session_name;
438 }
439
440 ret = enable_events(session_name);
441
442 end:
443 if (opt_session_name == NULL) {
444 free(session_name);
445 }
446
447 return ret;
448 }
This page took 0.038754 seconds and 4 git commands to generate.