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