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