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