49bae56b23872013abda97ffe665acfc014d72da
[lttng-tools.git] / src / bin / 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 "../command.h"
31
32 static char *opt_event_list;
33 static int opt_event_type;
34 static const char *opt_loglevel;
35 static int opt_loglevel_type;
36 static int opt_kernel;
37 static char *opt_session_name;
38 static int opt_userspace;
39 static int opt_enable_all;
40 static char *opt_probe;
41 static char *opt_function;
42 static char *opt_function_entry_symbol;
43 static char *opt_channel_name;
44 #if 0
45 /* Not implemented yet */
46 static char *opt_cmd_name;
47 static pid_t opt_pid;
48 #endif
49
50 enum {
51 OPT_HELP = 1,
52 OPT_TRACEPOINT,
53 OPT_PROBE,
54 OPT_FUNCTION,
55 OPT_FUNCTION_ENTRY,
56 OPT_SYSCALL,
57 OPT_USERSPACE,
58 OPT_LOGLEVEL,
59 OPT_LOGLEVEL_ONLY,
60 OPT_LIST_OPTIONS,
61 };
62
63 static struct lttng_handle *handle;
64
65 static struct poptOption long_options[] = {
66 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
67 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
68 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
69 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
70 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
71 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
72 #if 0
73 /* Not implemented yet */
74 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
75 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
76 #else
77 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
78 #endif
79 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
80 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
81 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
82 #if 0
83 /*
84 * Currently removed from lttng kernel tracer. Removed from
85 * lttng UI to discourage its use.
86 */
87 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
88 #endif
89 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
90 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
91 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
92 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
93 {0, 0, 0, 0, 0, 0, 0}
94 };
95
96 /*
97 * usage
98 */
99 static void usage(FILE *ofp)
100 {
101 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
102 fprintf(ofp, "\n");
103 fprintf(ofp, " -h, --help Show this help\n");
104 fprintf(ofp, " --list-options Simple listing of options\n");
105 fprintf(ofp, " -s, --session Apply on session name\n");
106 fprintf(ofp, " -c, --channel Apply on this channel\n");
107 fprintf(ofp, " -a, --all Enable all tracepoints\n");
108 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
109 #if 0
110 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
111 fprintf(ofp, " If no CMD, the domain used is UST global\n");
112 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
113 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
114 #else
115 fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n");
116 #endif
117 fprintf(ofp, "\n");
118 fprintf(ofp, "Event options:\n");
119 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
120 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
121 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
122 fprintf(ofp, " e.g.:\n");
123 fprintf(ofp, " \"*\"\n");
124 fprintf(ofp, " \"app_component:na*\"\n");
125 fprintf(ofp, " --loglevel name\n");
126 fprintf(ofp, " Tracepoint loglevel (range: 0 to loglevel)\n");
127 fprintf(ofp, " --loglevel-only name\n");
128 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
129 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
130 fprintf(ofp, " Dynamic probe.\n");
131 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
132 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
133 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
134 fprintf(ofp, " Dynamic function entry/return probe.\n");
135 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
136 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
137 #if 0
138 fprintf(ofp, " --function:entry symbol\n");
139 fprintf(ofp, " Function tracer event\n");
140 #endif
141 fprintf(ofp, " --syscall System call event\n");
142 fprintf(ofp, "\n");
143 }
144
145 /*
146 * Parse probe options.
147 */
148 static int parse_probe_opts(struct lttng_event *ev, char *opt)
149 {
150 int ret;
151 char s_hex[19];
152 char name[LTTNG_SYMBOL_NAME_LEN];
153
154 if (opt == NULL) {
155 ret = -1;
156 goto end;
157 }
158
159 /* Check for symbol+offset */
160 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
161 if (ret == 2) {
162 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
163 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
164 DBG("probe symbol %s", ev->attr.probe.symbol_name);
165 if (strlen(s_hex) == 0) {
166 ERR("Invalid probe offset %s", s_hex);
167 ret = -1;
168 goto end;
169 }
170 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
171 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
172 ev->attr.probe.addr = 0;
173 goto end;
174 }
175
176 /* Check for symbol */
177 if (isalpha(name[0])) {
178 ret = sscanf(opt, "%s", name);
179 if (ret == 1) {
180 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
181 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
182 DBG("probe symbol %s", ev->attr.probe.symbol_name);
183 ev->attr.probe.offset = 0;
184 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
185 ev->attr.probe.addr = 0;
186 goto end;
187 }
188 }
189
190 /* Check for address */
191 ret = sscanf(opt, "%s", s_hex);
192 if (ret > 0) {
193 if (strlen(s_hex) == 0) {
194 ERR("Invalid probe address %s", s_hex);
195 ret = -1;
196 goto end;
197 }
198 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
199 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
200 ev->attr.probe.offset = 0;
201 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
202 goto end;
203 }
204
205 /* No match */
206 ret = -1;
207
208 end:
209 return ret;
210 }
211
212 /*
213 * Enabling event using the lttng API.
214 */
215 static int enable_events(char *session_name)
216 {
217 int err, ret = CMD_SUCCESS;
218 char *event_name, *channel_name = NULL;
219 struct lttng_event ev;
220 struct lttng_domain dom;
221
222 if (opt_channel_name == NULL) {
223 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
224 if (err < 0) {
225 ret = CMD_FATAL;
226 goto error;
227 }
228 } else {
229 channel_name = opt_channel_name;
230 }
231
232 if (opt_kernel && opt_userspace) {
233 ERR("Can't use -k/--kernel and -u/--userspace together");
234 ret = CMD_FATAL;
235 goto error;
236 }
237
238 /* Create lttng domain */
239 if (opt_kernel) {
240 dom.type = LTTNG_DOMAIN_KERNEL;
241 } else if (opt_userspace) {
242 dom.type = LTTNG_DOMAIN_UST;
243 } else {
244 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
245 ret = CMD_ERROR;
246 goto error;
247 }
248
249 handle = lttng_create_handle(session_name, &dom);
250 if (handle == NULL) {
251 ret = -1;
252 goto error;
253 }
254
255 if (opt_enable_all) {
256 /* Default setup for enable all */
257
258 if (opt_kernel) {
259 ev.type = opt_event_type;
260 ev.name[0] = '\0';
261 } else {
262 ev.type = LTTNG_EVENT_TRACEPOINT;
263 strcpy(ev.name, "*");
264 }
265
266 ret = lttng_enable_event(handle, &ev, channel_name);
267 if (ret < 0) {
268 goto error;
269 }
270
271 switch (opt_event_type) {
272 case LTTNG_EVENT_TRACEPOINT:
273 MSG("All %s tracepoints are enabled in channel %s",
274 opt_kernel ? "kernel" : "UST", channel_name);
275 break;
276 case LTTNG_EVENT_SYSCALL:
277 if (opt_kernel) {
278 MSG("All kernel system calls are enabled in channel %s",
279 channel_name);
280 }
281 break;
282 case LTTNG_EVENT_ALL:
283 MSG("All %s events are enabled in channel %s",
284 opt_kernel ? "kernel" : "UST", channel_name);
285 break;
286 default:
287 /*
288 * We should not be here since lttng_enable_event should have
289 * failed on the event type.
290 */
291 goto error;
292 }
293 goto end;
294 }
295
296 /* Strip event list */
297 event_name = strtok(opt_event_list, ",");
298 while (event_name != NULL) {
299 /* Copy name and type of the event */
300 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
301 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
302 ev.type = opt_event_type;
303
304 /* Kernel tracer action */
305 if (opt_kernel) {
306 DBG("Enabling kernel event %s for channel %s",
307 event_name, channel_name);
308
309 switch (opt_event_type) {
310 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
311 ev.type = LTTNG_EVENT_TRACEPOINT;
312 /* Fall-through */
313 case LTTNG_EVENT_TRACEPOINT:
314 break;
315 case LTTNG_EVENT_PROBE:
316 ret = parse_probe_opts(&ev, opt_probe);
317 if (ret < 0) {
318 ERR("Unable to parse probe options");
319 ret = 0;
320 goto error;
321 }
322 break;
323 case LTTNG_EVENT_FUNCTION:
324 ret = parse_probe_opts(&ev, opt_function);
325 if (ret < 0) {
326 ERR("Unable to parse function probe options");
327 ret = 0;
328 goto error;
329 }
330 break;
331 case LTTNG_EVENT_FUNCTION_ENTRY:
332 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
333 LTTNG_SYMBOL_NAME_LEN);
334 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
335 break;
336 case LTTNG_EVENT_SYSCALL:
337 MSG("per-syscall selection not supported yet. Use \"-a\" "
338 "for all syscalls.");
339 default:
340 ret = CMD_UNDEFINED;
341 goto error;
342 }
343
344 if (opt_loglevel) {
345 MSG("Kernel loglevels are not supported.");
346 ret = CMD_UNDEFINED;
347 goto error;
348 }
349
350 /* kernel loglevels not implemented */
351 ev.loglevel_type = opt_loglevel_type;
352 ev.loglevel[0] = '\0';
353 } else if (opt_userspace) { /* User-space tracer action */
354 #if 0
355 if (opt_cmd_name != NULL || opt_pid) {
356 MSG("Only supporting tracing all UST processes (-u) for now.");
357 ret = CMD_UNDEFINED;
358 goto error;
359 }
360 #endif
361
362 DBG("Enabling UST event %s for channel %s", event_name,
363 channel_name);
364
365 switch (opt_event_type) {
366 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
367 /* Fall-through */
368 case LTTNG_EVENT_TRACEPOINT:
369 /* Copy name and type of the event */
370 ev.type = LTTNG_EVENT_TRACEPOINT;
371 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
372 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
373 break;
374 case LTTNG_EVENT_PROBE:
375 case LTTNG_EVENT_FUNCTION:
376 case LTTNG_EVENT_FUNCTION_ENTRY:
377 case LTTNG_EVENT_SYSCALL:
378 default:
379 ret = CMD_UNDEFINED;
380 goto error;
381 }
382
383 ev.loglevel_type = opt_loglevel_type;
384 if (opt_loglevel) {
385 strncpy(ev.loglevel, opt_loglevel, LTTNG_SYMBOL_NAME_LEN);
386 ev.loglevel[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
387 } else {
388 ev.loglevel[0] = '\0';
389 }
390 } else {
391 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
392 goto error;
393 }
394
395 ret = lttng_enable_event(handle, &ev, channel_name);
396 if (ret == 0) {
397 MSG("%s event %s created in channel %s",
398 opt_kernel ? "kernel": "UST", event_name, channel_name);
399 }
400
401 /* Next event */
402 event_name = strtok(NULL, ",");
403 }
404
405 end:
406 error:
407 if (opt_channel_name == NULL) {
408 free(channel_name);
409 }
410 lttng_destroy_handle(handle);
411
412 return ret;
413 }
414
415 /*
416 * Add event to trace session
417 */
418 int cmd_enable_events(int argc, const char **argv)
419 {
420 int opt, ret = CMD_SUCCESS;
421 static poptContext pc;
422 char *session_name = NULL;
423
424 pc = poptGetContext(NULL, argc, argv, long_options, 0);
425 poptReadDefaultConfig(pc, 0);
426
427 /* Default event type */
428 opt_event_type = LTTNG_EVENT_ALL;
429
430 while ((opt = poptGetNextOpt(pc)) != -1) {
431 switch (opt) {
432 case OPT_HELP:
433 usage(stdout);
434 goto end;
435 case OPT_TRACEPOINT:
436 opt_event_type = LTTNG_EVENT_TRACEPOINT;
437 break;
438 case OPT_PROBE:
439 opt_event_type = LTTNG_EVENT_PROBE;
440 break;
441 case OPT_FUNCTION:
442 opt_event_type = LTTNG_EVENT_FUNCTION;
443 break;
444 case OPT_FUNCTION_ENTRY:
445 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
446 break;
447 case OPT_SYSCALL:
448 opt_event_type = LTTNG_EVENT_SYSCALL;
449 break;
450 case OPT_USERSPACE:
451 opt_userspace = 1;
452 break;
453 case OPT_LOGLEVEL:
454 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL;
455 opt_loglevel = poptGetOptArg(pc);
456 break;
457 case OPT_LOGLEVEL_ONLY:
458 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_ONLY;
459 opt_loglevel = poptGetOptArg(pc);
460 break;
461 case OPT_LIST_OPTIONS:
462 list_cmd_options(stdout, long_options);
463 goto end;
464 default:
465 usage(stderr);
466 ret = CMD_UNDEFINED;
467 goto end;
468 }
469 }
470
471 opt_event_list = (char*) poptGetArg(pc);
472 if (opt_event_list == NULL && opt_enable_all == 0) {
473 ERR("Missing event name(s).\n");
474 usage(stderr);
475 ret = CMD_ERROR;
476 goto end;
477 }
478
479 if (!opt_session_name) {
480 session_name = get_session_name();
481 if (session_name == NULL) {
482 ret = CMD_ERROR;
483 goto end;
484 }
485 } else {
486 session_name = opt_session_name;
487 }
488
489 ret = enable_events(session_name);
490
491 end:
492 if (opt_session_name == NULL) {
493 free(session_name);
494 }
495
496 poptFreeContext(pc);
497 return ret;
498 }
This page took 0.05942 seconds and 4 git commands to generate.