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