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