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