51388298954768d35990b38e540617fc008e4020
[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 modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <inttypes.h>
27 #include <ctype.h>
28
29 #include "../command.h"
30 #include <src/common/sessiond-comm/sessiond-comm.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 static char *opt_filter;
45 #if 0
46 /* Not implemented yet */
47 static char *opt_cmd_name;
48 static pid_t opt_pid;
49 #endif
50
51 enum {
52 OPT_HELP = 1,
53 OPT_TRACEPOINT,
54 OPT_PROBE,
55 OPT_FUNCTION,
56 OPT_FUNCTION_ENTRY,
57 OPT_SYSCALL,
58 OPT_USERSPACE,
59 OPT_LOGLEVEL,
60 OPT_LOGLEVEL_ONLY,
61 OPT_LIST_OPTIONS,
62 OPT_FILTER,
63 };
64
65 static struct lttng_handle *handle;
66
67 static struct poptOption long_options[] = {
68 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
69 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
70 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
71 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
72 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
73 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
74 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
75 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
76 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
77 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
78 #if 0
79 /*
80 * Currently removed from lttng kernel tracer. Removed from
81 * lttng UI to discourage its use.
82 */
83 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
84 #endif
85 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
86 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
87 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
88 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
89 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
90 {0, 0, 0, 0, 0, 0, 0}
91 };
92
93 /*
94 * usage
95 */
96 static void usage(FILE *ofp)
97 {
98 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [-k|-u] [OPTIONS] \n");
99 fprintf(ofp, "\n");
100 fprintf(ofp, "Options:\n");
101 fprintf(ofp, " -h, --help Show this help\n");
102 fprintf(ofp, " --list-options Simple listing of options\n");
103 fprintf(ofp, " -s, --session NAME Apply to session name\n");
104 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
105 fprintf(ofp, " -a, --all Enable all tracepoints and syscalls\n");
106 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
107 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
108 fprintf(ofp, "\n");
109 fprintf(ofp, "Event options:\n");
110 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
111 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
112 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
113 fprintf(ofp, " e.g.:\n");
114 fprintf(ofp, " \"*\"\n");
115 fprintf(ofp, " \"app_component:na*\"\n");
116 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
117 fprintf(ofp, " Dynamic probe.\n");
118 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
119 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
120 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
121 fprintf(ofp, " Dynamic function entry/return probe.\n");
122 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
123 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
124 #if 0
125 fprintf(ofp, " --function:entry symbol\n");
126 fprintf(ofp, " Function tracer event\n");
127 #endif
128 fprintf(ofp, " --syscall System call event\n");
129 fprintf(ofp, "\n");
130 fprintf(ofp, " --loglevel name\n");
131 fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel\n");
132 fprintf(ofp, " --loglevel-only name\n");
133 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
134 fprintf(ofp, "\n");
135 fprintf(ofp, " The loglevel or loglevel-only options should be\n");
136 fprintf(ofp, " combined with a tracepoint name or tracepoint\n");
137 fprintf(ofp, " wildcard.\n");
138 fprintf(ofp, " Available loglevels:\n");
139 fprintf(ofp, " (higher value is more verbose)\n");
140 fprintf(ofp, " TRACE_EMERG = 0\n");
141 fprintf(ofp, " TRACE_ALERT = 1\n");
142 fprintf(ofp, " TRACE_CRIT = 2\n");
143 fprintf(ofp, " TRACE_ERR = 3\n");
144 fprintf(ofp, " TRACE_WARNING = 4\n");
145 fprintf(ofp, " TRACE_NOTICE = 5\n");
146 fprintf(ofp, " TRACE_INFO = 6\n");
147 fprintf(ofp, " TRACE_DEBUG_SYSTEM = 7\n");
148 fprintf(ofp, " TRACE_DEBUG_PROGRAM = 8\n");
149 fprintf(ofp, " TRACE_DEBUG_PROCESS = 9\n");
150 fprintf(ofp, " TRACE_DEBUG_MODULE = 10\n");
151 fprintf(ofp, " TRACE_DEBUG_UNIT = 11\n");
152 fprintf(ofp, " TRACE_DEBUG_FUNCTION = 12\n");
153 fprintf(ofp, " TRACE_DEBUG_LINE = 13\n");
154 fprintf(ofp, " TRACE_DEBUG = 14\n");
155 fprintf(ofp, " (shortcuts such as \"system\" are allowed)\n");
156 fprintf(ofp, " --filter \'expression\'\n");
157 fprintf(ofp, " Filter expression on event fields,\n");
158 fprintf(ofp, " event recording depends on evaluation.\n");
159 fprintf(ofp, " Only specify on first activation of\n");
160 fprintf(ofp, " a given event within a session.\n");
161 fprintf(ofp, " Filter only allowed when enabling\n");
162 fprintf(ofp, " events within a session before tracing\n");
163 fprintf(ofp, " is started. If the filter fails to link\n");
164 fprintf(ofp, " with the event within the traced domain,\n");
165 fprintf(ofp, " the event will be discarded. Currently,\n");
166 fprintf(ofp, " filter is only implemented for the user-space\n");
167 fprintf(ofp, " tracer.\n");
168 fprintf(ofp, " Expression examples:.\n");
169 fprintf(ofp, " \n");
170 fprintf(ofp, " 'intfield > 500 && intfield < 503'\n");
171 fprintf(ofp, " '(stringfield == \"test\" || intfield != 10) && intfield > 33'\n");
172 fprintf(ofp, " 'doublefield > 1.1 && intfield < 5.3'\n");
173 fprintf(ofp, " \n");
174 fprintf(ofp, " Wildcards are allowed at the end of strings:\n");
175 fprintf(ofp, " 'seqfield1 == \"te*\"'\n");
176 fprintf(ofp, " In string literals, the escape character is '\\'.\n");
177 fprintf(ofp, " Use '\\*' for the '*' character, and '\\\\' for\n");
178 fprintf(ofp, " the '\\' character.\n");
179 fprintf(ofp, "\n");
180 }
181
182 /*
183 * Parse probe options.
184 */
185 static int parse_probe_opts(struct lttng_event *ev, char *opt)
186 {
187 int ret;
188 char s_hex[19];
189 char name[LTTNG_SYMBOL_NAME_LEN];
190
191 if (opt == NULL) {
192 ret = -1;
193 goto end;
194 }
195
196 /* Check for symbol+offset */
197 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
198 if (ret == 2) {
199 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
200 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
201 DBG("probe symbol %s", ev->attr.probe.symbol_name);
202 if (strlen(s_hex) == 0) {
203 ERR("Invalid probe offset %s", s_hex);
204 ret = -1;
205 goto end;
206 }
207 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
208 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
209 ev->attr.probe.addr = 0;
210 goto end;
211 }
212
213 /* Check for symbol */
214 if (isalpha(name[0])) {
215 ret = sscanf(opt, "%s", name);
216 if (ret == 1) {
217 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
218 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
219 DBG("probe symbol %s", ev->attr.probe.symbol_name);
220 ev->attr.probe.offset = 0;
221 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
222 ev->attr.probe.addr = 0;
223 goto end;
224 }
225 }
226
227 /* Check for address */
228 ret = sscanf(opt, "%s", s_hex);
229 if (ret > 0) {
230 if (strlen(s_hex) == 0) {
231 ERR("Invalid probe address %s", s_hex);
232 ret = -1;
233 goto end;
234 }
235 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
236 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
237 ev->attr.probe.offset = 0;
238 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
239 goto end;
240 }
241
242 /* No match */
243 ret = -1;
244
245 end:
246 return ret;
247 }
248
249 /*
250 * Maps loglevel from string to value
251 */
252 static
253 int loglevel_str_to_value(const char *inputstr)
254 {
255 int i = 0;
256 char str[LTTNG_SYMBOL_NAME_LEN];
257
258 while (inputstr[i] != '\0' && i < LTTNG_SYMBOL_NAME_LEN) {
259 str[i] = toupper(inputstr[i]);
260 i++;
261 }
262 str[i] = '\0';
263 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
264 return LTTNG_LOGLEVEL_EMERG;
265 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
266 return LTTNG_LOGLEVEL_ALERT;
267 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
268 return LTTNG_LOGLEVEL_CRIT;
269 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
270 return LTTNG_LOGLEVEL_ERR;
271 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
272 return LTTNG_LOGLEVEL_WARNING;
273 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
274 return LTTNG_LOGLEVEL_NOTICE;
275 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
276 return LTTNG_LOGLEVEL_INFO;
277 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
278 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
279 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
280 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
281 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
282 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
283 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
284 return LTTNG_LOGLEVEL_DEBUG_MODULE;
285 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
286 return LTTNG_LOGLEVEL_DEBUG_UNIT;
287 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
288 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
289 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
290 return LTTNG_LOGLEVEL_DEBUG_LINE;
291 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
292 return LTTNG_LOGLEVEL_DEBUG;
293 } else {
294 return -1;
295 }
296 }
297
298 /*
299 * Enabling event using the lttng API.
300 */
301 static int enable_events(char *session_name)
302 {
303 int err, ret = CMD_SUCCESS, warn = 0;
304 char *event_name, *channel_name = NULL;
305 struct lttng_event ev;
306 struct lttng_domain dom;
307
308 memset(&ev, 0, sizeof(ev));
309 memset(&dom, 0, sizeof(dom));
310
311 if (opt_kernel) {
312 if (opt_filter) {
313 ERR("Filter not implement for kernel tracing yet");
314 ret = CMD_ERROR;
315 goto error;
316 }
317 }
318
319 /* Create lttng domain */
320 if (opt_kernel) {
321 dom.type = LTTNG_DOMAIN_KERNEL;
322 } else if (opt_userspace) {
323 dom.type = LTTNG_DOMAIN_UST;
324 } else {
325 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
326 ret = CMD_ERROR;
327 goto error;
328 }
329
330 if (opt_channel_name == NULL) {
331 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
332 if (err < 0) {
333 ret = CMD_FATAL;
334 goto error;
335 }
336 } else {
337 channel_name = opt_channel_name;
338 }
339
340 handle = lttng_create_handle(session_name, &dom);
341 if (handle == NULL) {
342 ret = -1;
343 goto error;
344 }
345
346 if (opt_enable_all) {
347 /* Default setup for enable all */
348 if (opt_kernel) {
349 ev.type = opt_event_type;
350 ev.name[0] = '\0';
351 /* kernel loglevels not implemented */
352 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
353 } else {
354 ev.type = LTTNG_EVENT_TRACEPOINT;
355 strcpy(ev.name, "*");
356 ev.loglevel_type = opt_loglevel_type;
357 if (opt_loglevel) {
358 ev.loglevel = loglevel_str_to_value(opt_loglevel);
359 if (ev.loglevel == -1) {
360 ERR("Unknown loglevel %s", opt_loglevel);
361 ret = -LTTNG_ERR_INVALID;
362 goto error;
363 }
364 } else {
365 ev.loglevel = -1;
366 }
367 }
368
369 ret = lttng_enable_event(handle, &ev, channel_name);
370 if (ret < 0) {
371 switch (-ret) {
372 case LTTNG_ERR_KERN_EVENT_EXIST:
373 WARN("Kernel events already enabled (channel %s, session %s)",
374 channel_name, session_name);
375 break;
376 default:
377 ERR("Events: %s (channel %s, session %s)",
378 lttng_strerror(ret), channel_name, session_name);
379 break;
380 }
381 goto end;
382 }
383
384 switch (opt_event_type) {
385 case LTTNG_EVENT_TRACEPOINT:
386 if (opt_loglevel) {
387 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
388 opt_kernel ? "kernel" : "UST", channel_name,
389 opt_loglevel);
390 } else {
391 MSG("All %s tracepoints are enabled in channel %s",
392 opt_kernel ? "kernel" : "UST", channel_name);
393
394 }
395 break;
396 case LTTNG_EVENT_SYSCALL:
397 if (opt_kernel) {
398 MSG("All kernel system calls are enabled in channel %s",
399 channel_name);
400 }
401 break;
402 case LTTNG_EVENT_ALL:
403 if (opt_loglevel) {
404 MSG("All %s events are enabled in channel %s for loglevel %s",
405 opt_kernel ? "kernel" : "UST", channel_name,
406 opt_loglevel);
407 } else {
408 MSG("All %s events are enabled in channel %s",
409 opt_kernel ? "kernel" : "UST", channel_name);
410 }
411 break;
412 default:
413 /*
414 * We should not be here since lttng_enable_event should have
415 * failed on the event type.
416 */
417 goto error;
418 }
419 if (opt_filter) {
420 ret = lttng_set_event_filter(handle, ev.name, channel_name,
421 opt_filter);
422 if (ret < 0) {
423 switch (-ret) {
424 case LTTNG_ERR_FILTER_EXIST:
425 WARN("Filter on events is already enabled"
426 " (channel %s, session %s)",
427 channel_name, session_name);
428 break;
429 case LTTNG_ERR_FILTER_INVAL:
430 case LTTNG_ERR_FILTER_NOMEM:
431 ERR("%s", lttng_strerror(ret));
432 default:
433 ERR("Setting filter: '%s'", opt_filter);
434 /*
435 * The event was successfully enabled before so when
436 * failing to set a filter, disable the event. This has
437 * been discussed in bug #343 on why we do that.
438 */
439 err = lttng_disable_event(handle, ev.name, channel_name);
440 if (err < 0) {
441 ERR("Disabling all events after filter error: %s",
442 lttng_strerror(err));
443 } else {
444 WARN("All events of channel %s have been disabled due "
445 "to a filter error", channel_name);
446 }
447 break;
448 }
449 goto error;
450 } else {
451 MSG("Filter '%s' successfully set", opt_filter);
452 }
453 }
454 goto end;
455 }
456
457 /* Strip event list */
458 event_name = strtok(opt_event_list, ",");
459 while (event_name != NULL) {
460 /* Copy name and type of the event */
461 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
462 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
463 ev.type = opt_event_type;
464
465 /* Kernel tracer action */
466 if (opt_kernel) {
467 DBG("Enabling kernel event %s for channel %s",
468 event_name, channel_name);
469
470 switch (opt_event_type) {
471 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
472 ev.type = LTTNG_EVENT_TRACEPOINT;
473 /* Fall-through */
474 case LTTNG_EVENT_TRACEPOINT:
475 break;
476 case LTTNG_EVENT_PROBE:
477 ret = parse_probe_opts(&ev, opt_probe);
478 if (ret < 0) {
479 ERR("Unable to parse probe options");
480 ret = 0;
481 goto error;
482 }
483 break;
484 case LTTNG_EVENT_FUNCTION:
485 ret = parse_probe_opts(&ev, opt_function);
486 if (ret < 0) {
487 ERR("Unable to parse function probe options");
488 ret = 0;
489 goto error;
490 }
491 break;
492 case LTTNG_EVENT_FUNCTION_ENTRY:
493 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
494 LTTNG_SYMBOL_NAME_LEN);
495 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
496 break;
497 case LTTNG_EVENT_SYSCALL:
498 MSG("per-syscall selection not supported yet. Use \"-a\" "
499 "for all syscalls.");
500 default:
501 ret = CMD_UNDEFINED;
502 goto error;
503 }
504
505 if (opt_loglevel) {
506 MSG("Kernel loglevels are not supported.");
507 ret = CMD_UNDEFINED;
508 goto error;
509 }
510
511 /* kernel loglevels not implemented */
512 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
513 } else if (opt_userspace) { /* User-space tracer action */
514 #if 0
515 if (opt_cmd_name != NULL || opt_pid) {
516 MSG("Only supporting tracing all UST processes (-u) for now.");
517 ret = CMD_UNDEFINED;
518 goto error;
519 }
520 #endif
521
522 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
523 channel_name, opt_loglevel ? : "<all>");
524
525 switch (opt_event_type) {
526 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
527 /* Fall-through */
528 case LTTNG_EVENT_TRACEPOINT:
529 /* Copy name and type of the event */
530 ev.type = LTTNG_EVENT_TRACEPOINT;
531 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
532 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
533 break;
534 case LTTNG_EVENT_PROBE:
535 case LTTNG_EVENT_FUNCTION:
536 case LTTNG_EVENT_FUNCTION_ENTRY:
537 case LTTNG_EVENT_SYSCALL:
538 default:
539 ERR("Event type not available for user-space tracing");
540 ret = CMD_UNDEFINED;
541 goto error;
542 }
543
544 ev.loglevel_type = opt_loglevel_type;
545 if (opt_loglevel) {
546 ev.loglevel = loglevel_str_to_value(opt_loglevel);
547 if (ev.loglevel == -1) {
548 ERR("Unknown loglevel %s", opt_loglevel);
549 ret = -LTTNG_ERR_INVALID;
550 goto error;
551 }
552 } else {
553 ev.loglevel = -1;
554 }
555 } else {
556 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
557 ret = CMD_ERROR;
558 goto error;
559 }
560
561 ret = lttng_enable_event(handle, &ev, channel_name);
562 if (ret < 0) {
563 /* Turn ret to positive value to handle the positive error code */
564 switch (-ret) {
565 case LTTNG_ERR_KERN_EVENT_EXIST:
566 WARN("Kernel event %s already enabled (channel %s, session %s)",
567 event_name, channel_name, session_name);
568 break;
569 default:
570 ERR("Event %s: %s (channel %s, session %s)", event_name,
571 lttng_strerror(ret), channel_name, session_name);
572 break;
573 }
574 warn = 1;
575 } else {
576 MSG("%s event %s created in channel %s",
577 opt_kernel ? "kernel": "UST", event_name, channel_name);
578 }
579 if (opt_filter) {
580 ret = lttng_set_event_filter(handle, ev.name, channel_name,
581 opt_filter);
582 if (ret < 0) {
583 switch (-ret) {
584 case LTTNG_ERR_FILTER_EXIST:
585 WARN("Filter on event %s is already enabled"
586 " (channel %s, session %s)",
587 event_name, channel_name, session_name);
588 break;
589 case LTTNG_ERR_FILTER_INVAL:
590 case LTTNG_ERR_FILTER_NOMEM:
591 ERR("%s", lttng_strerror(ret));
592 default:
593 ERR("Setting filter for event %s: '%s'", ev.name,
594 opt_filter);
595 /*
596 * The event was successfully enabled before so when
597 * failing to set a filter, disable the event. This has
598 * been discussed in bug #343 on why we do that.
599 */
600 err = lttng_disable_event(handle, ev.name, channel_name);
601 if (err < 0) {
602 ERR("Disabling event %s after filter error: %s",
603 ev.name, lttng_strerror(err));
604 } else {
605 WARN("Event %s of channel %s has been disabled due "
606 "to a filter error", ev.name, channel_name);
607 }
608 break;
609 }
610 goto error;
611 } else {
612 MSG("Filter '%s' successfully set", opt_filter);
613 }
614 }
615
616 /* Next event */
617 event_name = strtok(NULL, ",");
618 }
619
620 end:
621 error:
622 if (warn) {
623 ret = CMD_WARNING;
624 }
625 if (opt_channel_name == NULL) {
626 free(channel_name);
627 }
628 lttng_destroy_handle(handle);
629
630 return ret;
631 }
632
633 /*
634 * Add event to trace session
635 */
636 int cmd_enable_events(int argc, const char **argv)
637 {
638 int opt, ret = CMD_SUCCESS;
639 static poptContext pc;
640 char *session_name = NULL;
641
642 pc = poptGetContext(NULL, argc, argv, long_options, 0);
643 poptReadDefaultConfig(pc, 0);
644
645 /* Default event type */
646 opt_event_type = LTTNG_EVENT_ALL;
647
648 while ((opt = poptGetNextOpt(pc)) != -1) {
649 switch (opt) {
650 case OPT_HELP:
651 usage(stdout);
652 goto end;
653 case OPT_TRACEPOINT:
654 opt_event_type = LTTNG_EVENT_TRACEPOINT;
655 break;
656 case OPT_PROBE:
657 opt_event_type = LTTNG_EVENT_PROBE;
658 break;
659 case OPT_FUNCTION:
660 opt_event_type = LTTNG_EVENT_FUNCTION;
661 break;
662 case OPT_FUNCTION_ENTRY:
663 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
664 break;
665 case OPT_SYSCALL:
666 opt_event_type = LTTNG_EVENT_SYSCALL;
667 break;
668 case OPT_USERSPACE:
669 opt_userspace = 1;
670 break;
671 case OPT_LOGLEVEL:
672 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
673 opt_loglevel = poptGetOptArg(pc);
674 break;
675 case OPT_LOGLEVEL_ONLY:
676 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
677 opt_loglevel = poptGetOptArg(pc);
678 break;
679 case OPT_LIST_OPTIONS:
680 list_cmd_options(stdout, long_options);
681 goto end;
682 case OPT_FILTER:
683 break;
684 default:
685 usage(stderr);
686 ret = CMD_UNDEFINED;
687 goto end;
688 }
689 }
690
691 opt_event_list = (char*) poptGetArg(pc);
692 if (opt_event_list == NULL && opt_enable_all == 0) {
693 ERR("Missing event name(s).\n");
694 usage(stderr);
695 ret = CMD_ERROR;
696 goto end;
697 }
698
699 if (!opt_session_name) {
700 session_name = get_session_name();
701 if (session_name == NULL) {
702 ret = CMD_ERROR;
703 goto end;
704 }
705 } else {
706 session_name = opt_session_name;
707 }
708
709 ret = enable_events(session_name);
710
711 end:
712 if (opt_session_name == NULL) {
713 free(session_name);
714 }
715
716 poptFreeContext(pc);
717 return ret;
718 }
This page took 0.044279 seconds and 3 git commands to generate.