Prettify channel listing
[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 <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 create_exclusion_list_and_validate(const char *event_name,
509 const char *exclusions_arg,
510 char ***exclusion_list)
511 {
512 int ret = 0;
513 char **exclusions = NULL;
514
515 /* Event name must be a valid globbing pattern to allow exclusions. */
516 if (!strutils_is_star_glob_pattern(event_name)) {
517 ERR("Event %s: Exclusions can only be used with a globbing pattern",
518 event_name);
519 goto error;
520 }
521
522 /* Split exclusions. */
523 exclusions = strutils_split(exclusions_arg, ',', true);
524 if (!exclusions) {
525 goto error;
526 }
527
528 /*
529 * If the event name is a star-at-end only globbing pattern,
530 * then we can validate the individual exclusions. Otherwise
531 * all exclusions are passed to the session daemon.
532 */
533 if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
534 char * const *exclusion;
535
536 for (exclusion = exclusions; *exclusion; exclusion++) {
537 if (!strutils_is_star_glob_pattern(*exclusion) ||
538 strutils_is_star_at_the_end_only_glob_pattern(*exclusion)) {
539 ret = check_exclusion_subsets(event_name, *exclusion);
540 if (ret) {
541 goto error;
542 }
543 }
544 }
545 }
546
547 *exclusion_list = exclusions;
548
549 goto end;
550
551 error:
552 ret = -1;
553 strutils_free_null_terminated_array_of_strings(exclusions);
554
555 end:
556 return ret;
557 }
558
559 static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
560 int *warn)
561 {
562 char * const *exclusion;
563
564 for (exclusion = exclusion_list; *exclusion; exclusion++) {
565 if (strlen(*exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
566 WARN("Event exclusion \"%s\" will be truncated",
567 *exclusion);
568 *warn = 1;
569 }
570 }
571 }
572
573 /*
574 * Enabling event using the lttng API.
575 * Note: in case of error only the last error code will be return.
576 */
577 static int enable_events(char *session_name)
578 {
579 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
580 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
581 char *event_name, *channel_name = NULL;
582 struct lttng_event ev;
583 struct lttng_domain dom;
584 char **exclusion_list = NULL;
585
586 memset(&ev, 0, sizeof(ev));
587 memset(&dom, 0, sizeof(dom));
588
589 if (opt_kernel) {
590 if (opt_loglevel) {
591 WARN("Kernel loglevels are not supported.");
592 }
593 }
594
595 /* Create lttng domain */
596 if (opt_kernel) {
597 dom.type = LTTNG_DOMAIN_KERNEL;
598 dom.buf_type = LTTNG_BUFFER_GLOBAL;
599 } else if (opt_userspace) {
600 dom.type = LTTNG_DOMAIN_UST;
601 /* Default. */
602 dom.buf_type = LTTNG_BUFFER_PER_UID;
603 } else if (opt_jul) {
604 dom.type = LTTNG_DOMAIN_JUL;
605 /* Default. */
606 dom.buf_type = LTTNG_BUFFER_PER_UID;
607 } else if (opt_log4j) {
608 dom.type = LTTNG_DOMAIN_LOG4J;
609 /* Default. */
610 dom.buf_type = LTTNG_BUFFER_PER_UID;
611 } else if (opt_python) {
612 dom.type = LTTNG_DOMAIN_PYTHON;
613 /* Default. */
614 dom.buf_type = LTTNG_BUFFER_PER_UID;
615 } else {
616 /* Checked by the caller. */
617 assert(0);
618 }
619
620 if (opt_exclude) {
621 switch (dom.type) {
622 case LTTNG_DOMAIN_KERNEL:
623 case LTTNG_DOMAIN_JUL:
624 case LTTNG_DOMAIN_LOG4J:
625 case LTTNG_DOMAIN_PYTHON:
626 ERR("Event name exclusions are not yet implemented for %s events",
627 get_domain_str(dom.type));
628 ret = CMD_ERROR;
629 goto error;
630 case LTTNG_DOMAIN_UST:
631 /* Exclusions supported */
632 break;
633 default:
634 assert(0);
635 }
636 }
637
638 channel_name = opt_channel_name;
639
640 handle = lttng_create_handle(session_name, &dom);
641 if (handle == NULL) {
642 ret = -1;
643 goto error;
644 }
645
646 /* Prepare Mi */
647 if (lttng_opt_mi) {
648 /* Open a events element */
649 ret = mi_lttng_writer_open_element(writer, config_element_events);
650 if (ret) {
651 ret = CMD_ERROR;
652 goto error;
653 }
654 }
655
656 if (opt_enable_all) {
657 /* Default setup for enable all */
658 if (opt_kernel) {
659 ev.type = opt_event_type;
660 strcpy(ev.name, "*");
661 /* kernel loglevels not implemented */
662 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
663 } else {
664 ev.type = LTTNG_EVENT_TRACEPOINT;
665 strcpy(ev.name, "*");
666 ev.loglevel_type = opt_loglevel_type;
667 if (opt_loglevel) {
668 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
669 if (opt_userspace) {
670 ev.loglevel = loglevel_str_to_value(opt_loglevel);
671 } else if (opt_jul) {
672 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
673 } else if (opt_log4j) {
674 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
675 } else if (opt_python) {
676 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
677 }
678 if (ev.loglevel == -1) {
679 ERR("Unknown loglevel %s", opt_loglevel);
680 ret = -LTTNG_ERR_INVALID;
681 goto error;
682 }
683 } else {
684 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
685 if (opt_userspace) {
686 ev.loglevel = -1;
687 } else if (opt_jul) {
688 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
689 } else if (opt_log4j) {
690 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
691 } else if (opt_python) {
692 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
693 }
694 }
695 }
696
697 if (opt_exclude) {
698 ret = create_exclusion_list_and_validate("*",
699 opt_exclude, &exclusion_list);
700 if (ret) {
701 ret = CMD_ERROR;
702 goto error;
703 }
704
705 ev.exclusion = 1;
706 warn_on_truncated_exclusion_names(exclusion_list,
707 &warn);
708 }
709 if (!opt_filter) {
710 ret = lttng_enable_event_with_exclusions(handle,
711 &ev, channel_name,
712 NULL,
713 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
714 exclusion_list);
715 if (ret < 0) {
716 switch (-ret) {
717 case LTTNG_ERR_KERN_EVENT_EXIST:
718 WARN("Kernel events already enabled (channel %s, session %s)",
719 print_channel_name(channel_name), session_name);
720 warn = 1;
721 break;
722 case LTTNG_ERR_TRACE_ALREADY_STARTED:
723 {
724 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
725 ERR("Events: %s (channel %s, session %s)",
726 msg,
727 print_channel_name(channel_name),
728 session_name);
729 error = 1;
730 break;
731 }
732 default:
733 ERR("Events: %s (channel %s, session %s)",
734 lttng_strerror(ret),
735 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
736 ? print_raw_channel_name(channel_name)
737 : print_channel_name(channel_name),
738 session_name);
739 error = 1;
740 break;
741 }
742 goto end;
743 }
744
745 switch (opt_event_type) {
746 case LTTNG_EVENT_TRACEPOINT:
747 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
748 char *exclusion_string = print_exclusions(exclusion_list);
749
750 if (!exclusion_string) {
751 PERROR("Cannot allocate exclusion_string");
752 error = 1;
753 goto end;
754 }
755 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
756 get_domain_str(dom.type),
757 exclusion_string,
758 print_channel_name(channel_name),
759 opt_loglevel);
760 free(exclusion_string);
761 } else {
762 char *exclusion_string = print_exclusions(exclusion_list);
763
764 if (!exclusion_string) {
765 PERROR("Cannot allocate exclusion_string");
766 error = 1;
767 goto end;
768 }
769 MSG("All %s tracepoints%s are enabled in channel %s",
770 get_domain_str(dom.type),
771 exclusion_string,
772 print_channel_name(channel_name));
773 free(exclusion_string);
774 }
775 break;
776 case LTTNG_EVENT_SYSCALL:
777 if (opt_kernel) {
778 MSG("All %s system calls are enabled in channel %s",
779 get_domain_str(dom.type),
780 print_channel_name(channel_name));
781 }
782 break;
783 case LTTNG_EVENT_ALL:
784 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
785 char *exclusion_string = print_exclusions(exclusion_list);
786
787 if (!exclusion_string) {
788 PERROR("Cannot allocate exclusion_string");
789 error = 1;
790 goto end;
791 }
792 MSG("All %s events%s are enabled in channel %s for loglevel %s",
793 get_domain_str(dom.type),
794 exclusion_string,
795 print_channel_name(channel_name),
796 opt_loglevel);
797 free(exclusion_string);
798 } else {
799 char *exclusion_string = print_exclusions(exclusion_list);
800
801 if (!exclusion_string) {
802 PERROR("Cannot allocate exclusion_string");
803 error = 1;
804 goto end;
805 }
806 MSG("All %s events%s are enabled in channel %s",
807 get_domain_str(dom.type),
808 exclusion_string,
809 print_channel_name(channel_name));
810 free(exclusion_string);
811 }
812 break;
813 default:
814 /*
815 * We should not be here since lttng_enable_event should have
816 * failed on the event type.
817 */
818 goto error;
819 }
820 }
821
822 if (opt_filter) {
823 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
824 opt_filter,
825 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
826 exclusion_list);
827 if (command_ret < 0) {
828 switch (-command_ret) {
829 case LTTNG_ERR_FILTER_EXIST:
830 WARN("Filter on all events is already enabled"
831 " (channel %s, session %s)",
832 print_channel_name(channel_name), session_name);
833 warn = 1;
834 break;
835 case LTTNG_ERR_TRACE_ALREADY_STARTED:
836 {
837 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
838 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
839 msg,
840 print_channel_name(channel_name),
841 session_name, opt_filter);
842 error = 1;
843 break;
844 }
845 default:
846 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
847 lttng_strerror(command_ret),
848 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
849 ? print_raw_channel_name(channel_name)
850 : print_channel_name(channel_name),
851 session_name, opt_filter);
852 error = 1;
853 break;
854 }
855 error_holder = command_ret;
856 } else {
857 ev.filter = 1;
858 MSG("Filter '%s' successfully set", opt_filter);
859 }
860 }
861
862 if (lttng_opt_mi) {
863 /* The wildcard * is used for kernel and ust domain to
864 * represent ALL. We copy * in event name to force the wildcard use
865 * for kernel domain
866 *
867 * Note: this is strictly for semantic and printing while in
868 * machine interface mode.
869 */
870 strcpy(ev.name, "*");
871
872 /* If we reach here the events are enabled */
873 if (!error && !warn) {
874 ev.enabled = 1;
875 } else {
876 ev.enabled = 0;
877 success = 0;
878 }
879 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
880 if (ret) {
881 ret = CMD_ERROR;
882 goto error;
883 }
884
885 /* print exclusion */
886 ret = mi_print_exclusion(exclusion_list);
887 if (ret) {
888 ret = CMD_ERROR;
889 goto error;
890 }
891
892 /* Success ? */
893 ret = mi_lttng_writer_write_element_bool(writer,
894 mi_lttng_element_command_success, success);
895 if (ret) {
896 ret = CMD_ERROR;
897 goto error;
898 }
899
900 /* Close event element */
901 ret = mi_lttng_writer_close_element(writer);
902 if (ret) {
903 ret = CMD_ERROR;
904 goto error;
905 }
906 }
907
908 goto end;
909 }
910
911 /* Strip event list */
912 event_name = strtok(opt_event_list, ",");
913 while (event_name != NULL) {
914 /* Copy name and type of the event */
915 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
916 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
917 ev.type = opt_event_type;
918
919 /* Kernel tracer action */
920 if (opt_kernel) {
921 DBG("Enabling kernel event %s for channel %s",
922 event_name,
923 print_channel_name(channel_name));
924
925 switch (opt_event_type) {
926 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
927 /* If event name differs from *, select tracepoint. */
928 if (strcmp(ev.name, "*")) {
929 ev.type = LTTNG_EVENT_TRACEPOINT;
930 }
931 break;
932 case LTTNG_EVENT_TRACEPOINT:
933 break;
934 case LTTNG_EVENT_PROBE:
935 ret = parse_probe_opts(&ev, opt_probe);
936 if (ret) {
937 ERR("Unable to parse probe options");
938 ret = 0;
939 goto error;
940 }
941 break;
942 case LTTNG_EVENT_FUNCTION:
943 ret = parse_probe_opts(&ev, opt_function);
944 if (ret) {
945 ERR("Unable to parse function probe options");
946 ret = 0;
947 goto error;
948 }
949 break;
950 case LTTNG_EVENT_SYSCALL:
951 ev.type = LTTNG_EVENT_SYSCALL;
952 break;
953 default:
954 ret = CMD_UNDEFINED;
955 goto error;
956 }
957
958 /* kernel loglevels not implemented */
959 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
960 } else if (opt_userspace) { /* User-space tracer action */
961 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
962 print_channel_name(channel_name), opt_loglevel ? : "<all>");
963
964 switch (opt_event_type) {
965 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
966 /* Fall-through */
967 case LTTNG_EVENT_TRACEPOINT:
968 /* Copy name and type of the event */
969 ev.type = LTTNG_EVENT_TRACEPOINT;
970 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
971 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
972 break;
973 case LTTNG_EVENT_PROBE:
974 case LTTNG_EVENT_FUNCTION:
975 case LTTNG_EVENT_SYSCALL:
976 default:
977 ERR("Event type not available for user-space tracing");
978 ret = CMD_UNSUPPORTED;
979 goto error;
980 }
981
982 if (opt_exclude) {
983 ev.exclusion = 1;
984 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
985 ERR("Exclusion option can only be used with tracepoint events");
986 ret = CMD_ERROR;
987 goto error;
988 }
989 /* Free previously allocated items */
990 strutils_free_null_terminated_array_of_strings(
991 exclusion_list);
992 exclusion_list = NULL;
993 ret = create_exclusion_list_and_validate(
994 event_name, opt_exclude,
995 &exclusion_list);
996 if (ret) {
997 ret = CMD_ERROR;
998 goto error;
999 }
1000
1001 warn_on_truncated_exclusion_names(
1002 exclusion_list, &warn);
1003 }
1004
1005 ev.loglevel_type = opt_loglevel_type;
1006 if (opt_loglevel) {
1007 ev.loglevel = loglevel_str_to_value(opt_loglevel);
1008 if (ev.loglevel == -1) {
1009 ERR("Unknown loglevel %s", opt_loglevel);
1010 ret = -LTTNG_ERR_INVALID;
1011 goto error;
1012 }
1013 } else {
1014 ev.loglevel = -1;
1015 }
1016 } else if (opt_jul || opt_log4j || opt_python) {
1017 if (opt_event_type != LTTNG_EVENT_ALL &&
1018 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1019 ERR("Event type not supported for domain.");
1020 ret = CMD_UNSUPPORTED;
1021 goto error;
1022 }
1023
1024 ev.loglevel_type = opt_loglevel_type;
1025 if (opt_loglevel) {
1026 if (opt_jul) {
1027 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
1028 } else if (opt_log4j) {
1029 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
1030 } else if (opt_python) {
1031 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
1032 }
1033 if (ev.loglevel == -1) {
1034 ERR("Unknown loglevel %s", opt_loglevel);
1035 ret = -LTTNG_ERR_INVALID;
1036 goto error;
1037 }
1038 } else {
1039 if (opt_jul) {
1040 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1041 } else if (opt_log4j) {
1042 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1043 } else if (opt_python) {
1044 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1045 }
1046 }
1047 ev.type = LTTNG_EVENT_TRACEPOINT;
1048 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1049 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1050 } else {
1051 assert(0);
1052 }
1053
1054 if (!opt_filter) {
1055 char *exclusion_string;
1056
1057 command_ret = lttng_enable_event_with_exclusions(handle,
1058 &ev, channel_name,
1059 NULL,
1060 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1061 exclusion_list);
1062 exclusion_string = print_exclusions(exclusion_list);
1063 if (!exclusion_string) {
1064 PERROR("Cannot allocate exclusion_string");
1065 error = 1;
1066 goto end;
1067 }
1068 if (command_ret < 0) {
1069 /* Turn ret to positive value to handle the positive error code */
1070 switch (-command_ret) {
1071 case LTTNG_ERR_KERN_EVENT_EXIST:
1072 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1073 event_name,
1074 exclusion_string,
1075 print_channel_name(channel_name), session_name);
1076 warn = 1;
1077 break;
1078 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1079 {
1080 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1081 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1082 exclusion_string,
1083 msg,
1084 print_channel_name(channel_name),
1085 session_name);
1086 error = 1;
1087 break;
1088 }
1089 default:
1090 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1091 exclusion_string,
1092 lttng_strerror(command_ret),
1093 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1094 ? print_raw_channel_name(channel_name)
1095 : print_channel_name(channel_name),
1096 session_name);
1097 error = 1;
1098 break;
1099 }
1100 error_holder = command_ret;
1101 } else {
1102 switch (dom.type) {
1103 case LTTNG_DOMAIN_KERNEL:
1104 case LTTNG_DOMAIN_UST:
1105 MSG("%s event %s%s created in channel %s",
1106 get_domain_str(dom.type),
1107 event_name,
1108 exclusion_string,
1109 print_channel_name(channel_name));
1110 break;
1111 case LTTNG_DOMAIN_JUL:
1112 case LTTNG_DOMAIN_LOG4J:
1113 case LTTNG_DOMAIN_PYTHON:
1114 /*
1115 * Don't print the default channel
1116 * name for agent domains.
1117 */
1118 MSG("%s event %s%s enabled",
1119 get_domain_str(dom.type),
1120 event_name,
1121 exclusion_string);
1122 break;
1123 default:
1124 assert(0);
1125 }
1126 }
1127 free(exclusion_string);
1128 }
1129
1130 if (opt_filter) {
1131 char *exclusion_string;
1132
1133 /* Filter present */
1134 ev.filter = 1;
1135
1136 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
1137 opt_filter,
1138 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1139 exclusion_list);
1140 exclusion_string = print_exclusions(exclusion_list);
1141 if (!exclusion_string) {
1142 PERROR("Cannot allocate exclusion_string");
1143 error = 1;
1144 goto end;
1145 }
1146 if (command_ret < 0) {
1147 switch (-command_ret) {
1148 case LTTNG_ERR_FILTER_EXIST:
1149 WARN("Filter on event %s%s is already enabled"
1150 " (channel %s, session %s)",
1151 event_name,
1152 exclusion_string,
1153 print_channel_name(channel_name), session_name);
1154 warn = 1;
1155 break;
1156 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1157 {
1158 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1159 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1160 exclusion_string,
1161 msg,
1162 print_channel_name(channel_name),
1163 session_name, opt_filter);
1164 error = 1;
1165 break;
1166 }
1167 default:
1168 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1169 exclusion_string,
1170 lttng_strerror(command_ret),
1171 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1172 ? print_raw_channel_name(channel_name)
1173 : print_channel_name(channel_name),
1174 session_name, opt_filter);
1175 error = 1;
1176 break;
1177 }
1178 error_holder = command_ret;
1179
1180 } else {
1181 MSG("Event %s%s: Filter '%s' successfully set",
1182 event_name, exclusion_string,
1183 opt_filter);
1184 }
1185 free(exclusion_string);
1186 }
1187
1188 if (lttng_opt_mi) {
1189 if (command_ret) {
1190 success = 0;
1191 ev.enabled = 0;
1192 } else {
1193 ev.enabled = 1;
1194 }
1195
1196 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
1197 if (ret) {
1198 ret = CMD_ERROR;
1199 goto error;
1200 }
1201
1202 /* print exclusion */
1203 ret = mi_print_exclusion(exclusion_list);
1204 if (ret) {
1205 ret = CMD_ERROR;
1206 goto error;
1207 }
1208
1209 /* Success ? */
1210 ret = mi_lttng_writer_write_element_bool(writer,
1211 mi_lttng_element_command_success, success);
1212 if (ret) {
1213 ret = CMD_ERROR;
1214 goto end;
1215 }
1216
1217 /* Close event element */
1218 ret = mi_lttng_writer_close_element(writer);
1219 if (ret) {
1220 ret = CMD_ERROR;
1221 goto end;
1222 }
1223 }
1224
1225 /* Next event */
1226 event_name = strtok(NULL, ",");
1227 /* Reset warn, error and success */
1228 success = 1;
1229 }
1230
1231 end:
1232 /* Close Mi */
1233 if (lttng_opt_mi) {
1234 /* Close events element */
1235 ret = mi_lttng_writer_close_element(writer);
1236 if (ret) {
1237 ret = CMD_ERROR;
1238 goto error;
1239 }
1240 }
1241 error:
1242 if (warn) {
1243 ret = CMD_WARNING;
1244 }
1245 if (error) {
1246 ret = CMD_ERROR;
1247 }
1248 lttng_destroy_handle(handle);
1249 strutils_free_null_terminated_array_of_strings(exclusion_list);
1250
1251 /* Overwrite ret with error_holder if there was an actual error with
1252 * enabling an event.
1253 */
1254 ret = error_holder ? error_holder : ret;
1255
1256 return ret;
1257 }
1258
1259 /*
1260 * Add event to trace session
1261 */
1262 int cmd_enable_events(int argc, const char **argv)
1263 {
1264 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
1265 static poptContext pc;
1266 char *session_name = NULL;
1267 int event_type = -1;
1268
1269 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1270 poptReadDefaultConfig(pc, 0);
1271
1272 /* Default event type */
1273 opt_event_type = LTTNG_EVENT_ALL;
1274
1275 while ((opt = poptGetNextOpt(pc)) != -1) {
1276 switch (opt) {
1277 case OPT_HELP:
1278 SHOW_HELP();
1279 goto end;
1280 case OPT_TRACEPOINT:
1281 opt_event_type = LTTNG_EVENT_TRACEPOINT;
1282 break;
1283 case OPT_PROBE:
1284 opt_event_type = LTTNG_EVENT_PROBE;
1285 break;
1286 case OPT_FUNCTION:
1287 opt_event_type = LTTNG_EVENT_FUNCTION;
1288 break;
1289 case OPT_SYSCALL:
1290 opt_event_type = LTTNG_EVENT_SYSCALL;
1291 break;
1292 case OPT_USERSPACE:
1293 opt_userspace = 1;
1294 break;
1295 case OPT_LOGLEVEL:
1296 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
1297 opt_loglevel = poptGetOptArg(pc);
1298 break;
1299 case OPT_LOGLEVEL_ONLY:
1300 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
1301 opt_loglevel = poptGetOptArg(pc);
1302 break;
1303 case OPT_LIST_OPTIONS:
1304 list_cmd_options(stdout, long_options);
1305 goto end;
1306 case OPT_FILTER:
1307 break;
1308 case OPT_EXCLUDE:
1309 break;
1310 default:
1311 ret = CMD_UNDEFINED;
1312 goto end;
1313 }
1314
1315 /* Validate event type. Multiple event type are not supported. */
1316 if (event_type == -1) {
1317 event_type = opt_event_type;
1318 } else {
1319 if (event_type != opt_event_type) {
1320 ERR("Multiple event type not supported.");
1321 ret = CMD_ERROR;
1322 goto end;
1323 }
1324 }
1325 }
1326
1327 ret = print_missing_or_multiple_domains(
1328 opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
1329 if (ret) {
1330 ret = CMD_ERROR;
1331 goto end;
1332 }
1333
1334 /* Mi check */
1335 if (lttng_opt_mi) {
1336 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1337 if (!writer) {
1338 ret = -LTTNG_ERR_NOMEM;
1339 goto end;
1340 }
1341
1342 /* Open command element */
1343 ret = mi_lttng_writer_command_open(writer,
1344 mi_lttng_element_command_enable_event);
1345 if (ret) {
1346 ret = CMD_ERROR;
1347 goto end;
1348 }
1349
1350 /* Open output element */
1351 ret = mi_lttng_writer_open_element(writer,
1352 mi_lttng_element_command_output);
1353 if (ret) {
1354 ret = CMD_ERROR;
1355 goto end;
1356 }
1357 }
1358
1359 opt_event_list = (char*) poptGetArg(pc);
1360 if (opt_event_list == NULL && opt_enable_all == 0) {
1361 ERR("Missing event name(s).\n");
1362 ret = CMD_ERROR;
1363 goto end;
1364 }
1365
1366 if (!opt_session_name) {
1367 session_name = get_session_name();
1368 if (session_name == NULL) {
1369 command_ret = CMD_ERROR;
1370 success = 0;
1371 goto mi_closing;
1372 }
1373 } else {
1374 session_name = opt_session_name;
1375 }
1376
1377 command_ret = enable_events(session_name);
1378 if (command_ret) {
1379 success = 0;
1380 goto mi_closing;
1381 }
1382
1383 mi_closing:
1384 /* Mi closing */
1385 if (lttng_opt_mi) {
1386 /* Close output element */
1387 ret = mi_lttng_writer_close_element(writer);
1388 if (ret) {
1389 ret = CMD_ERROR;
1390 goto end;
1391 }
1392
1393 ret = mi_lttng_writer_write_element_bool(writer,
1394 mi_lttng_element_command_success, success);
1395 if (ret) {
1396 ret = CMD_ERROR;
1397 goto end;
1398 }
1399
1400 /* Command element close */
1401 ret = mi_lttng_writer_command_close(writer);
1402 if (ret) {
1403 ret = CMD_ERROR;
1404 goto end;
1405 }
1406 }
1407
1408 end:
1409 /* Mi clean-up */
1410 if (writer && mi_lttng_writer_destroy(writer)) {
1411 /* Preserve original error code */
1412 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1413 }
1414
1415 if (opt_session_name == NULL) {
1416 free(session_name);
1417 }
1418
1419 /* Overwrite ret if an error occurred in enable_events */
1420 ret = command_ret ? command_ret : ret;
1421
1422 poptFreeContext(pc);
1423 return ret;
1424 }
1425
This page took 0.060626 seconds and 4 git commands to generate.