Add support for "full" star globbing patterns in event names and filters
[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 _LGPL_SOURCE
19 #include <assert.h>
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.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 <src/common/sessiond-comm/sessiond-comm.h>
30 #include <common/compat/string.h>
31 #include <common/string-utils/string-utils.h>
32
33 /* Mi dependancy */
34 #include <common/mi-lttng.h>
35
36 #include "../command.h"
37
38 #if (LTTNG_SYMBOL_NAME_LEN == 256)
39 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
40 #endif
41
42 static char *opt_event_list;
43 static int opt_event_type;
44 static const char *opt_loglevel;
45 static int opt_loglevel_type;
46 static int opt_kernel;
47 static char *opt_session_name;
48 static int opt_userspace;
49 static int opt_jul;
50 static int opt_log4j;
51 static int opt_python;
52 static int opt_enable_all;
53 static char *opt_probe;
54 static char *opt_function;
55 static char *opt_channel_name;
56 static char *opt_filter;
57 static char *opt_exclude;
58
59 #ifdef LTTNG_EMBED_HELP
60 static const char help_msg[] =
61 #include <lttng-enable-event.1.h>
62 ;
63 #endif
64
65 enum {
66 OPT_HELP = 1,
67 OPT_TRACEPOINT,
68 OPT_PROBE,
69 OPT_FUNCTION,
70 OPT_SYSCALL,
71 OPT_USERSPACE,
72 OPT_LOGLEVEL,
73 OPT_LOGLEVEL_ONLY,
74 OPT_LIST_OPTIONS,
75 OPT_FILTER,
76 OPT_EXCLUDE,
77 };
78
79 static struct lttng_handle *handle;
80 static struct mi_writer *writer;
81
82 static struct poptOption long_options[] = {
83 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
84 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
85 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
86 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
87 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
88 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
89 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
90 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
91 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
92 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
93 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
94 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
95 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
96 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
97 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
98 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
99 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
100 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
101 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
102 {0, 0, 0, 0, 0, 0, 0}
103 };
104
105 /*
106 * Parse probe options.
107 */
108 static int parse_probe_opts(struct lttng_event *ev, char *opt)
109 {
110 int ret = CMD_SUCCESS;
111 int match;
112 char s_hex[19];
113 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
114 char name[LTTNG_SYMBOL_NAME_LEN];
115
116 if (opt == NULL) {
117 ret = CMD_ERROR;
118 goto end;
119 }
120
121 /* Check for symbol+offset */
122 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
123 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
124 if (match == 2) {
125 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
126 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
127 DBG("probe symbol %s", ev->attr.probe.symbol_name);
128 if (*s_hex == '\0') {
129 ERR("Invalid probe offset %s", s_hex);
130 ret = CMD_ERROR;
131 goto end;
132 }
133 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
134 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
135 ev->attr.probe.addr = 0;
136 goto end;
137 }
138
139 /* Check for symbol */
140 if (isalpha(name[0])) {
141 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
142 name);
143 if (match == 1) {
144 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
145 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
146 DBG("probe symbol %s", ev->attr.probe.symbol_name);
147 ev->attr.probe.offset = 0;
148 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
149 ev->attr.probe.addr = 0;
150 goto end;
151 }
152 }
153
154 /* Check for address */
155 match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
156 if (match > 0) {
157 if (*s_hex == '\0') {
158 ERR("Invalid probe address %s", s_hex);
159 ret = CMD_ERROR;
160 goto end;
161 }
162 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
163 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
164 ev->attr.probe.offset = 0;
165 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
166 goto end;
167 }
168
169 /* No match */
170 ret = CMD_ERROR;
171
172 end:
173 return ret;
174 }
175
176 /*
177 * Maps LOG4j loglevel from string to value
178 */
179 static int loglevel_log4j_str_to_value(const char *inputstr)
180 {
181 int i = 0;
182 char str[LTTNG_SYMBOL_NAME_LEN];
183
184 if (!inputstr || strlen(inputstr) == 0) {
185 return -1;
186 }
187
188 /*
189 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
190 * added at the end of the loop so a the upper bound we avoid the overflow.
191 */
192 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
193 str[i] = toupper(inputstr[i]);
194 i++;
195 }
196 str[i] = '\0';
197
198 if (!strcmp(str, "LOG4J_OFF") || !strcmp(str, "OFF")) {
199 return LTTNG_LOGLEVEL_LOG4J_OFF;
200 } else if (!strcmp(str, "LOG4J_FATAL") || !strcmp(str, "FATAL")) {
201 return LTTNG_LOGLEVEL_LOG4J_FATAL;
202 } else if (!strcmp(str, "LOG4J_ERROR") || !strcmp(str, "ERROR")) {
203 return LTTNG_LOGLEVEL_LOG4J_ERROR;
204 } else if (!strcmp(str, "LOG4J_WARN") || !strcmp(str, "WARN")) {
205 return LTTNG_LOGLEVEL_LOG4J_WARN;
206 } else if (!strcmp(str, "LOG4J_INFO") || !strcmp(str, "INFO")) {
207 return LTTNG_LOGLEVEL_LOG4J_INFO;
208 } else if (!strcmp(str, "LOG4J_DEBUG") || !strcmp(str, "DEBUG")) {
209 return LTTNG_LOGLEVEL_LOG4J_DEBUG;
210 } else if (!strcmp(str, "LOG4J_TRACE") || !strcmp(str, "TRACE")) {
211 return LTTNG_LOGLEVEL_LOG4J_TRACE;
212 } else if (!strcmp(str, "LOG4J_ALL") || !strcmp(str, "ALL")) {
213 return LTTNG_LOGLEVEL_LOG4J_ALL;
214 } else {
215 return -1;
216 }
217 }
218
219 /*
220 * Maps JUL loglevel from string to value
221 */
222 static int loglevel_jul_str_to_value(const char *inputstr)
223 {
224 int i = 0;
225 char str[LTTNG_SYMBOL_NAME_LEN];
226
227 if (!inputstr || strlen(inputstr) == 0) {
228 return -1;
229 }
230
231 /*
232 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
233 * added at the end of the loop so a the upper bound we avoid the overflow.
234 */
235 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
236 str[i] = toupper(inputstr[i]);
237 i++;
238 }
239 str[i] = '\0';
240
241 if (!strcmp(str, "JUL_OFF") || !strcmp(str, "OFF")) {
242 return LTTNG_LOGLEVEL_JUL_OFF;
243 } else if (!strcmp(str, "JUL_SEVERE") || !strcmp(str, "SEVERE")) {
244 return LTTNG_LOGLEVEL_JUL_SEVERE;
245 } else if (!strcmp(str, "JUL_WARNING") || !strcmp(str, "WARNING")) {
246 return LTTNG_LOGLEVEL_JUL_WARNING;
247 } else if (!strcmp(str, "JUL_INFO") || !strcmp(str, "INFO")) {
248 return LTTNG_LOGLEVEL_JUL_INFO;
249 } else if (!strcmp(str, "JUL_CONFIG") || !strcmp(str, "CONFIG")) {
250 return LTTNG_LOGLEVEL_JUL_CONFIG;
251 } else if (!strcmp(str, "JUL_FINE") || !strcmp(str, "FINE")) {
252 return LTTNG_LOGLEVEL_JUL_FINE;
253 } else if (!strcmp(str, "JUL_FINER") || !strcmp(str, "FINER")) {
254 return LTTNG_LOGLEVEL_JUL_FINER;
255 } else if (!strcmp(str, "JUL_FINEST") || !strcmp(str, "FINEST")) {
256 return LTTNG_LOGLEVEL_JUL_FINEST;
257 } else if (!strcmp(str, "JUL_ALL") || !strcmp(str, "ALL")) {
258 return LTTNG_LOGLEVEL_JUL_ALL;
259 } else {
260 return -1;
261 }
262 }
263
264 /*
265 * Maps Python loglevel from string to value
266 */
267 static int loglevel_python_str_to_value(const char *inputstr)
268 {
269 int i = 0;
270 char str[LTTNG_SYMBOL_NAME_LEN];
271
272 if (!inputstr || strlen(inputstr) == 0) {
273 return -1;
274 }
275
276 /*
277 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
278 * added at the end of the loop so a the upper bound we avoid the overflow.
279 */
280 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
281 str[i] = toupper(inputstr[i]);
282 i++;
283 }
284 str[i] = '\0';
285
286 if (!strcmp(str, "PYTHON_CRITICAL") || !strcmp(str, "CRITICAL")) {
287 return LTTNG_LOGLEVEL_PYTHON_CRITICAL;
288 } else if (!strcmp(str, "PYTHON_ERROR") || !strcmp(str, "ERROR")) {
289 return LTTNG_LOGLEVEL_PYTHON_ERROR;
290 } else if (!strcmp(str, "PYTHON_WARNING") || !strcmp(str, "WARNING")) {
291 return LTTNG_LOGLEVEL_PYTHON_WARNING;
292 } else if (!strcmp(str, "PYTHON_INFO") || !strcmp(str, "INFO")) {
293 return LTTNG_LOGLEVEL_PYTHON_INFO;
294 } else if (!strcmp(str, "PYTNON_DEBUG") || !strcmp(str, "DEBUG")) {
295 return LTTNG_LOGLEVEL_PYTHON_DEBUG;
296 } else if (!strcmp(str, "PYTHON_NOTSET") || !strcmp(str, "NOTSET")) {
297 return LTTNG_LOGLEVEL_PYTHON_NOTSET;
298 } else {
299 return -1;
300 }
301 }
302
303 /*
304 * Maps loglevel from string to value
305 */
306 static
307 int loglevel_str_to_value(const char *inputstr)
308 {
309 int i = 0;
310 char str[LTTNG_SYMBOL_NAME_LEN];
311
312 if (!inputstr || strlen(inputstr) == 0) {
313 return -1;
314 }
315
316 /*
317 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
318 * added at the end of the loop so a the upper bound we avoid the overflow.
319 */
320 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
321 str[i] = toupper(inputstr[i]);
322 i++;
323 }
324 str[i] = '\0';
325 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
326 return LTTNG_LOGLEVEL_EMERG;
327 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
328 return LTTNG_LOGLEVEL_ALERT;
329 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
330 return LTTNG_LOGLEVEL_CRIT;
331 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
332 return LTTNG_LOGLEVEL_ERR;
333 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
334 return LTTNG_LOGLEVEL_WARNING;
335 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
336 return LTTNG_LOGLEVEL_NOTICE;
337 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
338 return LTTNG_LOGLEVEL_INFO;
339 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
340 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
341 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
342 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
343 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
344 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
345 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
346 return LTTNG_LOGLEVEL_DEBUG_MODULE;
347 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
348 return LTTNG_LOGLEVEL_DEBUG_UNIT;
349 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
350 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
351 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
352 return LTTNG_LOGLEVEL_DEBUG_LINE;
353 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
354 return LTTNG_LOGLEVEL_DEBUG;
355 } else {
356 return -1;
357 }
358 }
359
360 static
361 const char *print_channel_name(const char *name)
362 {
363 return name ? : DEFAULT_CHANNEL_NAME;
364 }
365
366 static
367 const char *print_raw_channel_name(const char *name)
368 {
369 return name ? : "<default>";
370 }
371
372 /*
373 * Mi print exlcusion list
374 */
375 static
376 int mi_print_exclusion(char **names)
377 {
378 int i, ret;
379 int count = names ? strutils_array_of_strings_len(names) : 0;
380
381 assert(writer);
382
383 if (count == 0) {
384 ret = 0;
385 goto end;
386 }
387 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
388 if (ret) {
389 goto end;
390 }
391
392 for (i = 0; i < count; i++) {
393 ret = mi_lttng_writer_write_element_string(writer,
394 config_element_exclusion, names[i]);
395 if (ret) {
396 goto end;
397 }
398 }
399
400 /* Close exclusions element */
401 ret = mi_lttng_writer_close_element(writer);
402
403 end:
404 return ret;
405 }
406
407 /*
408 * Return allocated string for pretty-printing exclusion names.
409 */
410 static
411 char *print_exclusions(char **names)
412 {
413 int length = 0;
414 int i;
415 const char *preamble = " excluding ";
416 char *ret;
417 int count = names ? strutils_array_of_strings_len(names) : 0;
418
419 if (count == 0) {
420 return strdup("");
421 }
422
423 /* calculate total required length */
424 for (i = 0; i < count; i++) {
425 length += strlen(names[i]) + 4;
426 }
427
428 /* add length of preamble + one for NUL - one for last (missing) comma */
429 length += strlen(preamble);
430 ret = zmalloc(length + 1);
431 if (!ret) {
432 return NULL;
433 }
434 strncpy(ret, preamble, length);
435 for (i = 0; i < count; i++) {
436 strcat(ret, "\"");
437 strcat(ret, names[i]);
438 strcat(ret, "\"");
439 if (i != count - 1) {
440 strcat(ret, ", ");
441 }
442 }
443
444 return ret;
445 }
446
447 static
448 int check_exclusion_subsets(const char *event_name, const char *exclusion)
449 {
450 bool warn = false;
451 int ret = 0;
452 const char *e = event_name;
453 const char *x = exclusion;
454
455 /* Scan both the excluder and the event letter by letter */
456 while (true) {
457 if (*e == '\\') {
458 if (*x != *e) {
459 warn = true;
460 goto end;
461 }
462
463 e++;
464 x++;
465 goto cmp_chars;
466 }
467
468 if (*x == '*') {
469 /* Event is a subset of the excluder */
470 ERR("Event %s: %s excludes all events from %s",
471 event_name, exclusion, event_name);
472 goto error;
473 }
474
475 if (*e == '*') {
476 /*
477 * Reached the end of the event name before the
478 * end of the exclusion: this is valid.
479 */
480 goto end;
481 }
482
483 cmp_chars:
484 if (*x != *e) {
485 warn = true;
486 break;
487 }
488
489 x++;
490 e++;
491 }
492
493 goto end;
494
495 error:
496 ret = -1;
497
498 end:
499 if (warn) {
500 WARN("Event %s: %s does not exclude any events from %s",
501 event_name, exclusion, event_name);
502 }
503
504 return ret;
505 }
506
507 static
508 int check_exclusions_subsets(const char *event_name,
509 char * const *exclusions)
510 {
511 int ret = 0;
512 char * const *item;
513
514 for (item = exclusions; *item; item++) {
515 ret = check_exclusion_subsets(event_name, *item);
516 if (ret) {
517 goto end;
518 }
519 }
520
521 end:
522 return ret;
523 }
524
525 static
526 int create_exclusion_list_and_validate(const char *event_name,
527 const char *exclusions_arg,
528 char ***exclusion_list)
529 {
530 int ret = 0;
531 char **exclusions = NULL;
532
533 /* Event name must be a valid globbing pattern to allow exclusions. */
534 if (!strutils_is_star_glob_pattern(event_name)) {
535 ERR("Event %s: Exclusions can only be used with a globbing pattern",
536 event_name);
537 goto error;
538 }
539
540 /* Split exclusions. */
541 exclusions = strutils_split(exclusions_arg, ',', true);
542 if (!exclusions) {
543 goto error;
544 }
545
546 /*
547 * If the event name is a star-at-end only globbing pattern,
548 * then we can validate the individual exclusions. Otherwise
549 * all exclusions are passed to the session daemon.
550 */
551 if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
552 char * const *exclusion;
553
554 for (exclusion = exclusions; *exclusion; exclusion++) {
555 if (!strutils_is_star_glob_pattern(*exclusion) ||
556 strutils_is_star_at_the_end_only_glob_pattern(*exclusion)) {
557 ret = check_exclusions_subsets(
558 event_name, exclusion);
559 if (ret) {
560 goto error;
561 }
562 }
563 }
564 }
565
566 *exclusion_list = exclusions;
567
568 goto end;
569
570 error:
571 ret = -1;
572 strutils_free_null_terminated_array_of_strings(exclusions);
573
574 end:
575 return ret;
576 }
577
578 static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
579 int *warn)
580 {
581 char * const *exclusion;
582
583 for (exclusion = exclusion_list; *exclusion; exclusion++) {
584 if (strlen(*exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
585 WARN("Event exclusion \"%s\" will be truncated",
586 *exclusion);
587 *warn = 1;
588 }
589 }
590 }
591
592 /*
593 * Enabling event using the lttng API.
594 * Note: in case of error only the last error code will be return.
595 */
596 static int enable_events(char *session_name)
597 {
598 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
599 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
600 char *event_name, *channel_name = NULL;
601 struct lttng_event ev;
602 struct lttng_domain dom;
603 char **exclusion_list = NULL;
604
605 memset(&ev, 0, sizeof(ev));
606 memset(&dom, 0, sizeof(dom));
607
608 if (opt_kernel) {
609 if (opt_loglevel) {
610 WARN("Kernel loglevels are not supported.");
611 }
612 }
613
614 /* Create lttng domain */
615 if (opt_kernel) {
616 dom.type = LTTNG_DOMAIN_KERNEL;
617 dom.buf_type = LTTNG_BUFFER_GLOBAL;
618 } else if (opt_userspace) {
619 dom.type = LTTNG_DOMAIN_UST;
620 /* Default. */
621 dom.buf_type = LTTNG_BUFFER_PER_UID;
622 } else if (opt_jul) {
623 dom.type = LTTNG_DOMAIN_JUL;
624 /* Default. */
625 dom.buf_type = LTTNG_BUFFER_PER_UID;
626 } else if (opt_log4j) {
627 dom.type = LTTNG_DOMAIN_LOG4J;
628 /* Default. */
629 dom.buf_type = LTTNG_BUFFER_PER_UID;
630 } else if (opt_python) {
631 dom.type = LTTNG_DOMAIN_PYTHON;
632 /* Default. */
633 dom.buf_type = LTTNG_BUFFER_PER_UID;
634 } else {
635 /* Checked by the caller. */
636 assert(0);
637 }
638
639 if (opt_exclude) {
640 switch (dom.type) {
641 case LTTNG_DOMAIN_KERNEL:
642 case LTTNG_DOMAIN_JUL:
643 case LTTNG_DOMAIN_LOG4J:
644 case LTTNG_DOMAIN_PYTHON:
645 ERR("Event name exclusions are not yet implemented for %s events",
646 get_domain_str(dom.type));
647 ret = CMD_ERROR;
648 goto error;
649 case LTTNG_DOMAIN_UST:
650 /* Exclusions supported */
651 break;
652 default:
653 assert(0);
654 }
655 }
656
657 channel_name = opt_channel_name;
658
659 handle = lttng_create_handle(session_name, &dom);
660 if (handle == NULL) {
661 ret = -1;
662 goto error;
663 }
664
665 /* Prepare Mi */
666 if (lttng_opt_mi) {
667 /* Open a events element */
668 ret = mi_lttng_writer_open_element(writer, config_element_events);
669 if (ret) {
670 ret = CMD_ERROR;
671 goto error;
672 }
673 }
674
675 if (opt_enable_all) {
676 /* Default setup for enable all */
677 if (opt_kernel) {
678 ev.type = opt_event_type;
679 strcpy(ev.name, "*");
680 /* kernel loglevels not implemented */
681 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
682 } else {
683 ev.type = LTTNG_EVENT_TRACEPOINT;
684 strcpy(ev.name, "*");
685 ev.loglevel_type = opt_loglevel_type;
686 if (opt_loglevel) {
687 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
688 if (opt_userspace) {
689 ev.loglevel = loglevel_str_to_value(opt_loglevel);
690 } else if (opt_jul) {
691 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
692 } else if (opt_log4j) {
693 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
694 } else if (opt_python) {
695 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
696 }
697 if (ev.loglevel == -1) {
698 ERR("Unknown loglevel %s", opt_loglevel);
699 ret = -LTTNG_ERR_INVALID;
700 goto error;
701 }
702 } else {
703 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
704 if (opt_userspace) {
705 ev.loglevel = -1;
706 } else if (opt_jul) {
707 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
708 } else if (opt_log4j) {
709 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
710 } else if (opt_python) {
711 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
712 }
713 }
714 }
715
716 if (opt_exclude) {
717 ret = create_exclusion_list_and_validate("*",
718 opt_exclude, &exclusion_list);
719 if (ret) {
720 ret = CMD_ERROR;
721 goto error;
722 }
723
724 ev.exclusion = 1;
725 warn_on_truncated_exclusion_names(exclusion_list,
726 &warn);
727 }
728 if (!opt_filter) {
729 ret = lttng_enable_event_with_exclusions(handle,
730 &ev, channel_name,
731 NULL,
732 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
733 exclusion_list);
734 if (ret < 0) {
735 switch (-ret) {
736 case LTTNG_ERR_KERN_EVENT_EXIST:
737 WARN("Kernel events already enabled (channel %s, session %s)",
738 print_channel_name(channel_name), session_name);
739 warn = 1;
740 break;
741 case LTTNG_ERR_TRACE_ALREADY_STARTED:
742 {
743 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
744 ERR("Events: %s (channel %s, session %s)",
745 msg,
746 print_channel_name(channel_name),
747 session_name);
748 error = 1;
749 break;
750 }
751 default:
752 ERR("Events: %s (channel %s, session %s)",
753 lttng_strerror(ret),
754 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
755 ? print_raw_channel_name(channel_name)
756 : print_channel_name(channel_name),
757 session_name);
758 error = 1;
759 break;
760 }
761 goto end;
762 }
763
764 switch (opt_event_type) {
765 case LTTNG_EVENT_TRACEPOINT:
766 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
767 char *exclusion_string = print_exclusions(exclusion_list);
768
769 if (!exclusion_string) {
770 PERROR("Cannot allocate exclusion_string");
771 error = 1;
772 goto end;
773 }
774 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
775 get_domain_str(dom.type),
776 exclusion_string,
777 print_channel_name(channel_name),
778 opt_loglevel);
779 free(exclusion_string);
780 } else {
781 char *exclusion_string = print_exclusions(exclusion_list);
782
783 if (!exclusion_string) {
784 PERROR("Cannot allocate exclusion_string");
785 error = 1;
786 goto end;
787 }
788 MSG("All %s tracepoints%s are enabled in channel %s",
789 get_domain_str(dom.type),
790 exclusion_string,
791 print_channel_name(channel_name));
792 free(exclusion_string);
793 }
794 break;
795 case LTTNG_EVENT_SYSCALL:
796 if (opt_kernel) {
797 MSG("All %s system calls are enabled in channel %s",
798 get_domain_str(dom.type),
799 print_channel_name(channel_name));
800 }
801 break;
802 case LTTNG_EVENT_ALL:
803 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
804 char *exclusion_string = print_exclusions(exclusion_list);
805
806 if (!exclusion_string) {
807 PERROR("Cannot allocate exclusion_string");
808 error = 1;
809 goto end;
810 }
811 MSG("All %s events%s are enabled in channel %s for loglevel %s",
812 get_domain_str(dom.type),
813 exclusion_string,
814 print_channel_name(channel_name),
815 opt_loglevel);
816 free(exclusion_string);
817 } else {
818 char *exclusion_string = print_exclusions(exclusion_list);
819
820 if (!exclusion_string) {
821 PERROR("Cannot allocate exclusion_string");
822 error = 1;
823 goto end;
824 }
825 MSG("All %s events%s are enabled in channel %s",
826 get_domain_str(dom.type),
827 exclusion_string,
828 print_channel_name(channel_name));
829 free(exclusion_string);
830 }
831 break;
832 default:
833 /*
834 * We should not be here since lttng_enable_event should have
835 * failed on the event type.
836 */
837 goto error;
838 }
839 }
840
841 if (opt_filter) {
842 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
843 opt_filter,
844 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
845 exclusion_list);
846 if (command_ret < 0) {
847 switch (-command_ret) {
848 case LTTNG_ERR_FILTER_EXIST:
849 WARN("Filter on all events is already enabled"
850 " (channel %s, session %s)",
851 print_channel_name(channel_name), session_name);
852 warn = 1;
853 break;
854 case LTTNG_ERR_TRACE_ALREADY_STARTED:
855 {
856 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
857 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
858 msg,
859 print_channel_name(channel_name),
860 session_name, opt_filter);
861 error = 1;
862 break;
863 }
864 default:
865 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
866 lttng_strerror(command_ret),
867 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
868 ? print_raw_channel_name(channel_name)
869 : print_channel_name(channel_name),
870 session_name, opt_filter);
871 error = 1;
872 break;
873 }
874 error_holder = command_ret;
875 } else {
876 ev.filter = 1;
877 MSG("Filter '%s' successfully set", opt_filter);
878 }
879 }
880
881 if (lttng_opt_mi) {
882 /* The wildcard * is used for kernel and ust domain to
883 * represent ALL. We copy * in event name to force the wildcard use
884 * for kernel domain
885 *
886 * Note: this is strictly for semantic and printing while in
887 * machine interface mode.
888 */
889 strcpy(ev.name, "*");
890
891 /* If we reach here the events are enabled */
892 if (!error && !warn) {
893 ev.enabled = 1;
894 } else {
895 ev.enabled = 0;
896 success = 0;
897 }
898 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
899 if (ret) {
900 ret = CMD_ERROR;
901 goto error;
902 }
903
904 /* print exclusion */
905 ret = mi_print_exclusion(exclusion_list);
906 if (ret) {
907 ret = CMD_ERROR;
908 goto error;
909 }
910
911 /* Success ? */
912 ret = mi_lttng_writer_write_element_bool(writer,
913 mi_lttng_element_command_success, success);
914 if (ret) {
915 ret = CMD_ERROR;
916 goto error;
917 }
918
919 /* Close event element */
920 ret = mi_lttng_writer_close_element(writer);
921 if (ret) {
922 ret = CMD_ERROR;
923 goto error;
924 }
925 }
926
927 goto end;
928 }
929
930 /* Strip event list */
931 event_name = strtok(opt_event_list, ",");
932 while (event_name != NULL) {
933 /* Copy name and type of the event */
934 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
935 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
936 ev.type = opt_event_type;
937
938 /* Kernel tracer action */
939 if (opt_kernel) {
940 DBG("Enabling kernel event %s for channel %s",
941 event_name,
942 print_channel_name(channel_name));
943
944 switch (opt_event_type) {
945 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
946 /* If event name differs from *, select tracepoint. */
947 if (strcmp(ev.name, "*")) {
948 ev.type = LTTNG_EVENT_TRACEPOINT;
949 }
950 break;
951 case LTTNG_EVENT_TRACEPOINT:
952 break;
953 case LTTNG_EVENT_PROBE:
954 ret = parse_probe_opts(&ev, opt_probe);
955 if (ret) {
956 ERR("Unable to parse probe options");
957 ret = 0;
958 goto error;
959 }
960 break;
961 case LTTNG_EVENT_FUNCTION:
962 ret = parse_probe_opts(&ev, opt_function);
963 if (ret) {
964 ERR("Unable to parse function probe options");
965 ret = 0;
966 goto error;
967 }
968 break;
969 case LTTNG_EVENT_SYSCALL:
970 ev.type = LTTNG_EVENT_SYSCALL;
971 break;
972 default:
973 ret = CMD_UNDEFINED;
974 goto error;
975 }
976
977 /* kernel loglevels not implemented */
978 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
979 } else if (opt_userspace) { /* User-space tracer action */
980 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
981 print_channel_name(channel_name), opt_loglevel ? : "<all>");
982
983 switch (opt_event_type) {
984 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
985 /* Fall-through */
986 case LTTNG_EVENT_TRACEPOINT:
987 /* Copy name and type of the event */
988 ev.type = LTTNG_EVENT_TRACEPOINT;
989 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
990 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
991 break;
992 case LTTNG_EVENT_PROBE:
993 case LTTNG_EVENT_FUNCTION:
994 case LTTNG_EVENT_SYSCALL:
995 default:
996 ERR("Event type not available for user-space tracing");
997 ret = CMD_UNSUPPORTED;
998 goto error;
999 }
1000
1001 if (opt_exclude) {
1002 ev.exclusion = 1;
1003 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1004 ERR("Exclusion option can only be used with tracepoint events");
1005 ret = CMD_ERROR;
1006 goto error;
1007 }
1008 /* Free previously allocated items */
1009 strutils_free_null_terminated_array_of_strings(
1010 exclusion_list);
1011 exclusion_list = NULL;
1012 ret = create_exclusion_list_and_validate(
1013 event_name, opt_exclude,
1014 &exclusion_list);
1015 if (ret) {
1016 ret = CMD_ERROR;
1017 goto error;
1018 }
1019
1020 warn_on_truncated_exclusion_names(
1021 exclusion_list, &warn);
1022 }
1023
1024 ev.loglevel_type = opt_loglevel_type;
1025 if (opt_loglevel) {
1026 ev.loglevel = loglevel_str_to_value(opt_loglevel);
1027 if (ev.loglevel == -1) {
1028 ERR("Unknown loglevel %s", opt_loglevel);
1029 ret = -LTTNG_ERR_INVALID;
1030 goto error;
1031 }
1032 } else {
1033 ev.loglevel = -1;
1034 }
1035 } else if (opt_jul || opt_log4j || opt_python) {
1036 if (opt_event_type != LTTNG_EVENT_ALL &&
1037 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1038 ERR("Event type not supported for domain.");
1039 ret = CMD_UNSUPPORTED;
1040 goto error;
1041 }
1042
1043 ev.loglevel_type = opt_loglevel_type;
1044 if (opt_loglevel) {
1045 if (opt_jul) {
1046 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
1047 } else if (opt_log4j) {
1048 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
1049 } else if (opt_python) {
1050 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
1051 }
1052 if (ev.loglevel == -1) {
1053 ERR("Unknown loglevel %s", opt_loglevel);
1054 ret = -LTTNG_ERR_INVALID;
1055 goto error;
1056 }
1057 } else {
1058 if (opt_jul) {
1059 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1060 } else if (opt_log4j) {
1061 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1062 } else if (opt_python) {
1063 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1064 }
1065 }
1066 ev.type = LTTNG_EVENT_TRACEPOINT;
1067 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1068 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1069 } else {
1070 assert(0);
1071 }
1072
1073 if (!opt_filter) {
1074 char *exclusion_string;
1075
1076 command_ret = lttng_enable_event_with_exclusions(handle,
1077 &ev, channel_name,
1078 NULL,
1079 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1080 exclusion_list);
1081 exclusion_string = print_exclusions(exclusion_list);
1082 if (!exclusion_string) {
1083 PERROR("Cannot allocate exclusion_string");
1084 error = 1;
1085 goto end;
1086 }
1087 if (command_ret < 0) {
1088 /* Turn ret to positive value to handle the positive error code */
1089 switch (-command_ret) {
1090 case LTTNG_ERR_KERN_EVENT_EXIST:
1091 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1092 event_name,
1093 exclusion_string,
1094 print_channel_name(channel_name), session_name);
1095 warn = 1;
1096 break;
1097 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1098 {
1099 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1100 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1101 exclusion_string,
1102 msg,
1103 print_channel_name(channel_name),
1104 session_name);
1105 error = 1;
1106 break;
1107 }
1108 default:
1109 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1110 exclusion_string,
1111 lttng_strerror(command_ret),
1112 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1113 ? print_raw_channel_name(channel_name)
1114 : print_channel_name(channel_name),
1115 session_name);
1116 error = 1;
1117 break;
1118 }
1119 error_holder = command_ret;
1120 } else {
1121 switch (dom.type) {
1122 case LTTNG_DOMAIN_KERNEL:
1123 case LTTNG_DOMAIN_UST:
1124 MSG("%s event %s%s created in channel %s",
1125 get_domain_str(dom.type),
1126 event_name,
1127 exclusion_string,
1128 print_channel_name(channel_name));
1129 break;
1130 case LTTNG_DOMAIN_JUL:
1131 case LTTNG_DOMAIN_LOG4J:
1132 case LTTNG_DOMAIN_PYTHON:
1133 /*
1134 * Don't print the default channel
1135 * name for agent domains.
1136 */
1137 MSG("%s event %s%s enabled",
1138 get_domain_str(dom.type),
1139 event_name,
1140 exclusion_string);
1141 break;
1142 default:
1143 assert(0);
1144 }
1145 }
1146 free(exclusion_string);
1147 }
1148
1149 if (opt_filter) {
1150 char *exclusion_string;
1151
1152 /* Filter present */
1153 ev.filter = 1;
1154
1155 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
1156 opt_filter,
1157 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1158 exclusion_list);
1159 exclusion_string = print_exclusions(exclusion_list);
1160 if (!exclusion_string) {
1161 PERROR("Cannot allocate exclusion_string");
1162 error = 1;
1163 goto end;
1164 }
1165 if (command_ret < 0) {
1166 switch (-command_ret) {
1167 case LTTNG_ERR_FILTER_EXIST:
1168 WARN("Filter on event %s%s is already enabled"
1169 " (channel %s, session %s)",
1170 event_name,
1171 exclusion_string,
1172 print_channel_name(channel_name), session_name);
1173 warn = 1;
1174 break;
1175 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1176 {
1177 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1178 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1179 exclusion_string,
1180 msg,
1181 print_channel_name(channel_name),
1182 session_name, opt_filter);
1183 error = 1;
1184 break;
1185 }
1186 default:
1187 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1188 exclusion_string,
1189 lttng_strerror(command_ret),
1190 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1191 ? print_raw_channel_name(channel_name)
1192 : print_channel_name(channel_name),
1193 session_name, opt_filter);
1194 error = 1;
1195 break;
1196 }
1197 error_holder = command_ret;
1198
1199 } else {
1200 MSG("Event %s%s: Filter '%s' successfully set",
1201 event_name, exclusion_string,
1202 opt_filter);
1203 }
1204 free(exclusion_string);
1205 }
1206
1207 if (lttng_opt_mi) {
1208 if (command_ret) {
1209 success = 0;
1210 ev.enabled = 0;
1211 } else {
1212 ev.enabled = 1;
1213 }
1214
1215 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
1216 if (ret) {
1217 ret = CMD_ERROR;
1218 goto error;
1219 }
1220
1221 /* print exclusion */
1222 ret = mi_print_exclusion(exclusion_list);
1223 if (ret) {
1224 ret = CMD_ERROR;
1225 goto error;
1226 }
1227
1228 /* Success ? */
1229 ret = mi_lttng_writer_write_element_bool(writer,
1230 mi_lttng_element_command_success, success);
1231 if (ret) {
1232 ret = CMD_ERROR;
1233 goto end;
1234 }
1235
1236 /* Close event element */
1237 ret = mi_lttng_writer_close_element(writer);
1238 if (ret) {
1239 ret = CMD_ERROR;
1240 goto end;
1241 }
1242 }
1243
1244 /* Next event */
1245 event_name = strtok(NULL, ",");
1246 /* Reset warn, error and success */
1247 success = 1;
1248 }
1249
1250 end:
1251 /* Close Mi */
1252 if (lttng_opt_mi) {
1253 /* Close events element */
1254 ret = mi_lttng_writer_close_element(writer);
1255 if (ret) {
1256 ret = CMD_ERROR;
1257 goto error;
1258 }
1259 }
1260 error:
1261 if (warn) {
1262 ret = CMD_WARNING;
1263 }
1264 if (error) {
1265 ret = CMD_ERROR;
1266 }
1267 lttng_destroy_handle(handle);
1268 strutils_free_null_terminated_array_of_strings(exclusion_list);
1269
1270 /* Overwrite ret with error_holder if there was an actual error with
1271 * enabling an event.
1272 */
1273 ret = error_holder ? error_holder : ret;
1274
1275 return ret;
1276 }
1277
1278 /*
1279 * Add event to trace session
1280 */
1281 int cmd_enable_events(int argc, const char **argv)
1282 {
1283 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
1284 static poptContext pc;
1285 char *session_name = NULL;
1286 int event_type = -1;
1287
1288 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1289 poptReadDefaultConfig(pc, 0);
1290
1291 /* Default event type */
1292 opt_event_type = LTTNG_EVENT_ALL;
1293
1294 while ((opt = poptGetNextOpt(pc)) != -1) {
1295 switch (opt) {
1296 case OPT_HELP:
1297 SHOW_HELP();
1298 goto end;
1299 case OPT_TRACEPOINT:
1300 opt_event_type = LTTNG_EVENT_TRACEPOINT;
1301 break;
1302 case OPT_PROBE:
1303 opt_event_type = LTTNG_EVENT_PROBE;
1304 break;
1305 case OPT_FUNCTION:
1306 opt_event_type = LTTNG_EVENT_FUNCTION;
1307 break;
1308 case OPT_SYSCALL:
1309 opt_event_type = LTTNG_EVENT_SYSCALL;
1310 break;
1311 case OPT_USERSPACE:
1312 opt_userspace = 1;
1313 break;
1314 case OPT_LOGLEVEL:
1315 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
1316 opt_loglevel = poptGetOptArg(pc);
1317 break;
1318 case OPT_LOGLEVEL_ONLY:
1319 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
1320 opt_loglevel = poptGetOptArg(pc);
1321 break;
1322 case OPT_LIST_OPTIONS:
1323 list_cmd_options(stdout, long_options);
1324 goto end;
1325 case OPT_FILTER:
1326 break;
1327 case OPT_EXCLUDE:
1328 break;
1329 default:
1330 ret = CMD_UNDEFINED;
1331 goto end;
1332 }
1333
1334 /* Validate event type. Multiple event type are not supported. */
1335 if (event_type == -1) {
1336 event_type = opt_event_type;
1337 } else {
1338 if (event_type != opt_event_type) {
1339 ERR("Multiple event type not supported.");
1340 ret = CMD_ERROR;
1341 goto end;
1342 }
1343 }
1344 }
1345
1346 ret = print_missing_or_multiple_domains(
1347 opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
1348 if (ret) {
1349 ret = CMD_ERROR;
1350 goto end;
1351 }
1352
1353 /* Mi check */
1354 if (lttng_opt_mi) {
1355 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1356 if (!writer) {
1357 ret = -LTTNG_ERR_NOMEM;
1358 goto end;
1359 }
1360
1361 /* Open command element */
1362 ret = mi_lttng_writer_command_open(writer,
1363 mi_lttng_element_command_enable_event);
1364 if (ret) {
1365 ret = CMD_ERROR;
1366 goto end;
1367 }
1368
1369 /* Open output element */
1370 ret = mi_lttng_writer_open_element(writer,
1371 mi_lttng_element_command_output);
1372 if (ret) {
1373 ret = CMD_ERROR;
1374 goto end;
1375 }
1376 }
1377
1378 opt_event_list = (char*) poptGetArg(pc);
1379 if (opt_event_list == NULL && opt_enable_all == 0) {
1380 ERR("Missing event name(s).\n");
1381 ret = CMD_ERROR;
1382 goto end;
1383 }
1384
1385 if (!opt_session_name) {
1386 session_name = get_session_name();
1387 if (session_name == NULL) {
1388 command_ret = CMD_ERROR;
1389 success = 0;
1390 goto mi_closing;
1391 }
1392 } else {
1393 session_name = opt_session_name;
1394 }
1395
1396 command_ret = enable_events(session_name);
1397 if (command_ret) {
1398 success = 0;
1399 goto mi_closing;
1400 }
1401
1402 mi_closing:
1403 /* Mi closing */
1404 if (lttng_opt_mi) {
1405 /* Close output element */
1406 ret = mi_lttng_writer_close_element(writer);
1407 if (ret) {
1408 ret = CMD_ERROR;
1409 goto end;
1410 }
1411
1412 ret = mi_lttng_writer_write_element_bool(writer,
1413 mi_lttng_element_command_success, success);
1414 if (ret) {
1415 ret = CMD_ERROR;
1416 goto end;
1417 }
1418
1419 /* Command element close */
1420 ret = mi_lttng_writer_command_close(writer);
1421 if (ret) {
1422 ret = CMD_ERROR;
1423 goto end;
1424 }
1425 }
1426
1427 end:
1428 /* Mi clean-up */
1429 if (writer && mi_lttng_writer_destroy(writer)) {
1430 /* Preserve original error code */
1431 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1432 }
1433
1434 if (opt_session_name == NULL) {
1435 free(session_name);
1436 }
1437
1438 /* Overwrite ret if an error occurred in enable_events */
1439 ret = command_ret ? command_ret : ret;
1440
1441 poptFreeContext(pc);
1442 return ret;
1443 }
1444
This page took 0.099674 seconds and 6 git commands to generate.