Fix: print lttng strerror on enable event with filter
[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 and context.\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. Wildcard match any sequence of,\n");
179 fprintf(ofp, " characters including an empty sub-string (match 0 or\n");
180 fprintf(ofp, " more characters).\n");
181 fprintf(ofp, "\n");
182 fprintf(ofp, " Context information can be used for filtering. The\n");
183 fprintf(ofp, " examples below show usage of context filtering on\n");
184 fprintf(ofp, " process name (with a wildcard), process ID range, and\n");
185 fprintf(ofp, " unique thread ID for filtering. The process and\n");
186 fprintf(ofp, " thread ID of running applications can be found under\n");
187 fprintf(ofp, " columns \"PID\" and \"LWP\" of the \"ps -eLf\" command.\n");
188 fprintf(ofp, "\n");
189 fprintf(ofp, " '$ctx.procname == \"demo*\"'\n");
190 fprintf(ofp, " '$ctx.vpid >= 4433 && $ctx.vpid < 4455'\n");
191 fprintf(ofp, " '$ctx.vtid == 1234'\n");
192 fprintf(ofp, "\n");
193 }
194
195 /*
196 * Parse probe options.
197 */
198 static int parse_probe_opts(struct lttng_event *ev, char *opt)
199 {
200 int ret;
201 char s_hex[19];
202 char name[LTTNG_SYMBOL_NAME_LEN];
203
204 if (opt == NULL) {
205 ret = -1;
206 goto end;
207 }
208
209 /* Check for symbol+offset */
210 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
211 if (ret == 2) {
212 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
213 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
214 DBG("probe symbol %s", ev->attr.probe.symbol_name);
215 if (*s_hex == '\0') {
216 ERR("Invalid probe offset %s", s_hex);
217 ret = -1;
218 goto end;
219 }
220 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
221 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
222 ev->attr.probe.addr = 0;
223 goto end;
224 }
225
226 /* Check for symbol */
227 if (isalpha(name[0])) {
228 ret = sscanf(opt, "%s", name);
229 if (ret == 1) {
230 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
231 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
232 DBG("probe symbol %s", ev->attr.probe.symbol_name);
233 ev->attr.probe.offset = 0;
234 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
235 ev->attr.probe.addr = 0;
236 goto end;
237 }
238 }
239
240 /* Check for address */
241 ret = sscanf(opt, "%s", s_hex);
242 if (ret > 0) {
243 if (*s_hex == '\0') {
244 ERR("Invalid probe address %s", s_hex);
245 ret = -1;
246 goto end;
247 }
248 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
249 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
250 ev->attr.probe.offset = 0;
251 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
252 goto end;
253 }
254
255 /* No match */
256 ret = -1;
257
258 end:
259 return ret;
260 }
261
262 /*
263 * Maps loglevel from string to value
264 */
265 static
266 int loglevel_str_to_value(const char *inputstr)
267 {
268 int i = 0;
269 char str[LTTNG_SYMBOL_NAME_LEN];
270
271 while (i < LTTNG_SYMBOL_NAME_LEN && inputstr[i] != '\0') {
272 str[i] = toupper(inputstr[i]);
273 i++;
274 }
275 str[i] = '\0';
276 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
277 return LTTNG_LOGLEVEL_EMERG;
278 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
279 return LTTNG_LOGLEVEL_ALERT;
280 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
281 return LTTNG_LOGLEVEL_CRIT;
282 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
283 return LTTNG_LOGLEVEL_ERR;
284 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
285 return LTTNG_LOGLEVEL_WARNING;
286 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
287 return LTTNG_LOGLEVEL_NOTICE;
288 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
289 return LTTNG_LOGLEVEL_INFO;
290 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
291 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
292 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
293 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
294 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
295 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
296 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
297 return LTTNG_LOGLEVEL_DEBUG_MODULE;
298 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
299 return LTTNG_LOGLEVEL_DEBUG_UNIT;
300 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
301 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
302 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
303 return LTTNG_LOGLEVEL_DEBUG_LINE;
304 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
305 return LTTNG_LOGLEVEL_DEBUG;
306 } else {
307 return -1;
308 }
309 }
310
311 /*
312 * Enabling event using the lttng API.
313 */
314 static int enable_events(char *session_name)
315 {
316 int err, ret = CMD_SUCCESS, warn = 0;
317 char *event_name, *channel_name = NULL;
318 struct lttng_event ev;
319 struct lttng_domain dom;
320
321 memset(&ev, 0, sizeof(ev));
322 memset(&dom, 0, sizeof(dom));
323
324 if (opt_kernel) {
325 if (opt_filter) {
326 ERR("Filter not implement for kernel tracing yet");
327 ret = CMD_ERROR;
328 goto error;
329 }
330 }
331
332 /* Create lttng domain */
333 if (opt_kernel) {
334 dom.type = LTTNG_DOMAIN_KERNEL;
335 dom.buf_type = LTTNG_BUFFER_GLOBAL;
336 } else if (opt_userspace) {
337 dom.type = LTTNG_DOMAIN_UST;
338 /* Default. */
339 dom.buf_type = LTTNG_BUFFER_PER_PID;
340 } else {
341 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
342 ret = CMD_ERROR;
343 goto error;
344 }
345
346 if (opt_channel_name == NULL) {
347 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
348 if (err < 0) {
349 ret = CMD_FATAL;
350 goto error;
351 }
352 } else {
353 channel_name = opt_channel_name;
354 }
355
356 handle = lttng_create_handle(session_name, &dom);
357 if (handle == NULL) {
358 ret = -1;
359 goto error;
360 }
361
362 if (opt_enable_all) {
363 /* Default setup for enable all */
364 if (opt_kernel) {
365 ev.type = opt_event_type;
366 ev.name[0] = '\0';
367 /* kernel loglevels not implemented */
368 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
369 } else {
370 ev.type = LTTNG_EVENT_TRACEPOINT;
371 strcpy(ev.name, "*");
372 ev.loglevel_type = opt_loglevel_type;
373 if (opt_loglevel) {
374 ev.loglevel = loglevel_str_to_value(opt_loglevel);
375 if (ev.loglevel == -1) {
376 ERR("Unknown loglevel %s", opt_loglevel);
377 ret = -LTTNG_ERR_INVALID;
378 goto error;
379 }
380 } else {
381 ev.loglevel = -1;
382 }
383 }
384
385 if (!opt_filter) {
386 ret = lttng_enable_event(handle, &ev, channel_name);
387 if (ret < 0) {
388 switch (-ret) {
389 case LTTNG_ERR_KERN_EVENT_EXIST:
390 WARN("Kernel events already enabled (channel %s, session %s)",
391 channel_name, session_name);
392 break;
393 default:
394 ERR("Events: %s (channel %s, session %s)",
395 lttng_strerror(ret), channel_name, session_name);
396 break;
397 }
398 goto end;
399 }
400
401 switch (opt_event_type) {
402 case LTTNG_EVENT_TRACEPOINT:
403 if (opt_loglevel) {
404 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
405 opt_kernel ? "kernel" : "UST", channel_name,
406 opt_loglevel);
407 } else {
408 MSG("All %s tracepoints are enabled in channel %s",
409 opt_kernel ? "kernel" : "UST", channel_name);
410
411 }
412 break;
413 case LTTNG_EVENT_SYSCALL:
414 if (opt_kernel) {
415 MSG("All kernel system calls are enabled in channel %s",
416 channel_name);
417 }
418 break;
419 case LTTNG_EVENT_ALL:
420 if (opt_loglevel) {
421 MSG("All %s events are enabled in channel %s for loglevel %s",
422 opt_kernel ? "kernel" : "UST", channel_name,
423 opt_loglevel);
424 } else {
425 MSG("All %s events are enabled in channel %s",
426 opt_kernel ? "kernel" : "UST", channel_name);
427 }
428 break;
429 default:
430 /*
431 * We should not be here since lttng_enable_event should have
432 * failed on the event type.
433 */
434 goto error;
435 }
436 }
437 if (opt_filter) {
438 ret = lttng_enable_event_with_filter(handle, &ev, channel_name,
439 opt_filter);
440 if (ret < 0) {
441 switch (-ret) {
442 case LTTNG_ERR_FILTER_EXIST:
443 WARN("Filter on events is already enabled"
444 " (channel %s, session %s)",
445 channel_name, session_name);
446 break;
447 case LTTNG_ERR_FILTER_INVAL:
448 case LTTNG_ERR_FILTER_NOMEM:
449 default:
450 ERR("%s", lttng_strerror(ret));
451 break;
452 }
453 goto error;
454 } else {
455 MSG("Filter '%s' successfully set", opt_filter);
456 }
457 }
458 goto end;
459 }
460
461 /* Strip event list */
462 event_name = strtok(opt_event_list, ",");
463 while (event_name != NULL) {
464 /* Copy name and type of the event */
465 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
466 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
467 ev.type = opt_event_type;
468
469 /* Kernel tracer action */
470 if (opt_kernel) {
471 DBG("Enabling kernel event %s for channel %s",
472 event_name, channel_name);
473
474 switch (opt_event_type) {
475 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
476 ev.type = LTTNG_EVENT_TRACEPOINT;
477 /* Fall-through */
478 case LTTNG_EVENT_TRACEPOINT:
479 break;
480 case LTTNG_EVENT_PROBE:
481 ret = parse_probe_opts(&ev, opt_probe);
482 if (ret < 0) {
483 ERR("Unable to parse probe options");
484 ret = 0;
485 goto error;
486 }
487 break;
488 case LTTNG_EVENT_FUNCTION:
489 ret = parse_probe_opts(&ev, opt_function);
490 if (ret < 0) {
491 ERR("Unable to parse function probe options");
492 ret = 0;
493 goto error;
494 }
495 break;
496 case LTTNG_EVENT_FUNCTION_ENTRY:
497 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
498 LTTNG_SYMBOL_NAME_LEN);
499 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
500 break;
501 case LTTNG_EVENT_SYSCALL:
502 MSG("per-syscall selection not supported yet. Use \"-a\" "
503 "for all syscalls.");
504 default:
505 ret = CMD_UNDEFINED;
506 goto error;
507 }
508
509 if (opt_loglevel) {
510 MSG("Kernel loglevels are not supported.");
511 ret = CMD_UNSUPPORTED;
512 goto error;
513 }
514
515 /* kernel loglevels not implemented */
516 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
517 } else if (opt_userspace) { /* User-space tracer action */
518 #if 0
519 if (opt_cmd_name != NULL || opt_pid) {
520 MSG("Only supporting tracing all UST processes (-u) for now.");
521 ret = CMD_UNDEFINED;
522 goto error;
523 }
524 #endif
525
526 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
527 channel_name, opt_loglevel ? : "<all>");
528
529 switch (opt_event_type) {
530 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
531 /* Fall-through */
532 case LTTNG_EVENT_TRACEPOINT:
533 /* Copy name and type of the event */
534 ev.type = LTTNG_EVENT_TRACEPOINT;
535 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
536 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
537 break;
538 case LTTNG_EVENT_PROBE:
539 case LTTNG_EVENT_FUNCTION:
540 case LTTNG_EVENT_FUNCTION_ENTRY:
541 case LTTNG_EVENT_SYSCALL:
542 default:
543 ERR("Event type not available for user-space tracing");
544 ret = CMD_UNSUPPORTED;
545 goto error;
546 }
547
548 ev.loglevel_type = opt_loglevel_type;
549 if (opt_loglevel) {
550 ev.loglevel = loglevel_str_to_value(opt_loglevel);
551 if (ev.loglevel == -1) {
552 ERR("Unknown loglevel %s", opt_loglevel);
553 ret = -LTTNG_ERR_INVALID;
554 goto error;
555 }
556 } else {
557 ev.loglevel = -1;
558 }
559 } else {
560 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
561 ret = CMD_ERROR;
562 goto error;
563 }
564
565 if (!opt_filter) {
566 ret = lttng_enable_event(handle, &ev, channel_name);
567 if (ret < 0) {
568 /* Turn ret to positive value to handle the positive error code */
569 switch (-ret) {
570 case LTTNG_ERR_KERN_EVENT_EXIST:
571 WARN("Kernel event %s already enabled (channel %s, session %s)",
572 event_name, channel_name, session_name);
573 break;
574 default:
575 ERR("Event %s: %s (channel %s, session %s)", event_name,
576 lttng_strerror(ret), channel_name, session_name);
577 break;
578 }
579 warn = 1;
580 } else {
581 MSG("%s event %s created in channel %s",
582 opt_kernel ? "kernel": "UST", event_name, channel_name);
583 }
584 }
585
586 if (opt_filter) {
587 ret = lttng_enable_event_with_filter(handle, &ev, channel_name,
588 opt_filter);
589 if (ret < 0) {
590 switch (-ret) {
591 case LTTNG_ERR_FILTER_EXIST:
592 WARN("Filter on event %s is already enabled"
593 " (channel %s, session %s)",
594 event_name, channel_name, session_name);
595 break;
596 case LTTNG_ERR_FILTER_INVAL:
597 case LTTNG_ERR_FILTER_NOMEM:
598 ERR("%s", lttng_strerror(ret));
599 default:
600 ERR("Setting filter for event %s: '%s'", ev.name,
601 opt_filter);
602 break;
603 }
604 goto error;
605 } else {
606 MSG("Filter '%s' successfully set", opt_filter);
607 }
608 }
609
610 /* Next event */
611 event_name = strtok(NULL, ",");
612 }
613
614 end:
615 error:
616 if (warn) {
617 ret = CMD_WARNING;
618 }
619 if (opt_channel_name == NULL) {
620 free(channel_name);
621 }
622 lttng_destroy_handle(handle);
623
624 return ret;
625 }
626
627 /*
628 * Add event to trace session
629 */
630 int cmd_enable_events(int argc, const char **argv)
631 {
632 int opt, ret = CMD_SUCCESS;
633 static poptContext pc;
634 char *session_name = NULL;
635 int event_type = -1;
636
637 pc = poptGetContext(NULL, argc, argv, long_options, 0);
638 poptReadDefaultConfig(pc, 0);
639
640 /* Default event type */
641 opt_event_type = LTTNG_EVENT_ALL;
642
643 while ((opt = poptGetNextOpt(pc)) != -1) {
644 switch (opt) {
645 case OPT_HELP:
646 usage(stdout);
647 goto end;
648 case OPT_TRACEPOINT:
649 opt_event_type = LTTNG_EVENT_TRACEPOINT;
650 break;
651 case OPT_PROBE:
652 opt_event_type = LTTNG_EVENT_PROBE;
653 break;
654 case OPT_FUNCTION:
655 opt_event_type = LTTNG_EVENT_FUNCTION;
656 break;
657 case OPT_FUNCTION_ENTRY:
658 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
659 break;
660 case OPT_SYSCALL:
661 opt_event_type = LTTNG_EVENT_SYSCALL;
662 break;
663 case OPT_USERSPACE:
664 opt_userspace = 1;
665 break;
666 case OPT_LOGLEVEL:
667 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
668 opt_loglevel = poptGetOptArg(pc);
669 break;
670 case OPT_LOGLEVEL_ONLY:
671 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
672 opt_loglevel = poptGetOptArg(pc);
673 break;
674 case OPT_LIST_OPTIONS:
675 list_cmd_options(stdout, long_options);
676 goto end;
677 case OPT_FILTER:
678 break;
679 default:
680 usage(stderr);
681 ret = CMD_UNDEFINED;
682 goto end;
683 }
684
685 /* Validate event type. Multiple event type are not supported. */
686 if (event_type == -1) {
687 event_type = opt_event_type;
688 } else {
689 if (event_type != opt_event_type) {
690 ERR("Multiple event type not supported.");
691 ret = CMD_ERROR;
692 goto end;
693 }
694 }
695 }
696
697 opt_event_list = (char*) poptGetArg(pc);
698 if (opt_event_list == NULL && opt_enable_all == 0) {
699 ERR("Missing event name(s).\n");
700 usage(stderr);
701 ret = CMD_ERROR;
702 goto end;
703 }
704
705 if (!opt_session_name) {
706 session_name = get_session_name();
707 if (session_name == NULL) {
708 ret = CMD_ERROR;
709 goto end;
710 }
711 } else {
712 session_name = opt_session_name;
713 }
714
715 ret = enable_events(session_name);
716
717 end:
718 if (opt_session_name == NULL) {
719 free(session_name);
720 }
721
722 poptFreeContext(pc);
723 return ret;
724 }
This page took 0.047222 seconds and 6 git commands to generate.