2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
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>
25 #include <lttng/constant.h>
27 #include <common/mi-lttng.h>
29 #include <lttng/event-internal.h>
31 #include "../command.h"
32 #include "../uprobe.h"
34 #if (LTTNG_SYMBOL_NAME_LEN == 256)
35 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
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
;
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
;
56 #ifdef LTTNG_EMBED_HELP
57 static const char help_msg
[] =
58 #include <lttng-enable-event.1.h>
77 static struct lttng_handle
*handle
;
78 static struct mi_writer
*writer
;
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}
105 * Parse probe options.
107 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
109 int ret
= CMD_SUCCESS
;
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
];
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
);
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
);
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;
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",
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;
153 /* Check for address */
154 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
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.
161 if (*s_hex
== '\0' || !isdigit(*s_hex
)) {
162 ERR("Invalid probe description %s", s_hex
);
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
);
181 * Maps LOG4j loglevel from string to value
184 int loglevel_log4j_str_to_value(const char *inputstr
)
187 char str
[LTTNG_SYMBOL_NAME_LEN
];
189 if (!inputstr
|| strlen(inputstr
) == 0) {
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.
197 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
198 str
[i
] = toupper(inputstr
[i
]);
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
;
225 * Maps JUL loglevel from string to value
228 int loglevel_jul_str_to_value(const char *inputstr
)
231 char str
[LTTNG_SYMBOL_NAME_LEN
];
233 if (!inputstr
|| strlen(inputstr
) == 0) {
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.
241 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
242 str
[i
] = toupper(inputstr
[i
]);
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
;
271 * Maps Python loglevel from string to value
274 int loglevel_python_str_to_value(const char *inputstr
)
277 char str
[LTTNG_SYMBOL_NAME_LEN
];
279 if (!inputstr
|| strlen(inputstr
) == 0) {
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.
287 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
288 str
[i
] = toupper(inputstr
[i
]);
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
;
311 * Maps loglevel from string to value
314 int loglevel_str_to_value(const char *inputstr
)
317 char str
[LTTNG_SYMBOL_NAME_LEN
];
319 if (!inputstr
|| strlen(inputstr
) == 0) {
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.
327 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
328 str
[i
] = toupper(inputstr
[i
]);
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
;
368 const char *print_channel_name(const char *name
)
370 return name
? : DEFAULT_CHANNEL_NAME
;
374 const char *print_raw_channel_name(const char *name
)
376 return name
? : "<default>";
380 * Mi print exlcusion list
383 int mi_print_exclusion(char **names
)
386 int count
= names
? strutils_array_of_strings_len(names
) : 0;
394 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
399 for (i
= 0; i
< count
; i
++) {
400 ret
= mi_lttng_writer_write_element_string(writer
,
401 config_element_exclusion
, names
[i
]);
407 /* Close exclusions element */
408 ret
= mi_lttng_writer_close_element(writer
);
415 * Return allocated string for pretty-printing exclusion names.
418 char *print_exclusions(char **names
)
422 const char preamble
[] = " excluding ";
424 int count
= names
? strutils_array_of_strings_len(names
) : 0;
430 /* calculate total required length */
431 for (i
= 0; i
< count
; i
++) {
432 length
+= strlen(names
[i
]) + 4;
435 length
+= sizeof(preamble
);
436 ret
= zmalloc(length
);
440 strncpy(ret
, preamble
, length
);
441 for (i
= 0; i
< count
; i
++) {
443 strcat(ret
, names
[i
]);
445 if (i
!= count
- 1) {
454 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
458 const char *e
= event_name
;
459 const char *x
= exclusion
;
461 /* Scan both the excluder and the event letter by letter */
475 /* Event is a subset of the excluder */
476 ERR("Event %s: %s excludes all events from %s",
477 event_name
, exclusion
, event_name
);
483 * Reached the end of the event name before the
484 * end of the exclusion: this is valid.
506 WARN("Event %s: %s does not exclude any events from %s",
507 event_name
, exclusion
, event_name
);
514 * FIXME: find a good place to declare this since add trigger also uses it
517 int create_exclusion_list_and_validate(const char *event_name
,
518 const char *exclusions_arg
,
519 char ***exclusion_list
);
523 int create_exclusion_list_and_validate(const char *event_name
,
524 const char *exclusions_arg
,
525 char ***exclusion_list
)
528 char **exclusions
= NULL
;
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",
537 /* Split exclusions. */
538 exclusions
= strutils_split(exclusions_arg
, ',', true);
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.
548 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
549 char * const *exclusion
;
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
);
562 *exclusion_list
= exclusions
;
568 strutils_free_null_terminated_array_of_strings(exclusions
);
574 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
577 char * const *exclusion
;
579 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
580 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
581 WARN("Event exclusion \"%s\" will be truncated",
589 * Enabling event using the lttng API.
590 * Note: in case of error only the last error code will be return.
592 static int enable_events(char *session_name
)
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
;
602 memset(&dom
, 0, sizeof(dom
));
604 ev
= lttng_event_create();
612 WARN("Kernel loglevels are not supported.");
616 /* Create lttng domain */
618 dom
.type
= LTTNG_DOMAIN_KERNEL
;
619 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
620 } else if (opt_userspace
) {
621 dom
.type
= LTTNG_DOMAIN_UST
;
623 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
624 } else if (opt_jul
) {
625 dom
.type
= LTTNG_DOMAIN_JUL
;
627 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
628 } else if (opt_log4j
) {
629 dom
.type
= LTTNG_DOMAIN_LOG4J
;
631 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
632 } else if (opt_python
) {
633 dom
.type
= LTTNG_DOMAIN_PYTHON
;
635 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
637 /* Checked by the caller. */
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
));
651 case LTTNG_DOMAIN_UST
:
652 /* Exclusions supported */
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.
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
:
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
));
683 channel_name
= opt_channel_name
;
685 handle
= lttng_create_handle(session_name
, &dom
);
686 if (handle
== NULL
) {
693 /* Open a events element */
694 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
701 if (opt_enable_all
) {
702 /* Default setup for enable all */
704 ev
->type
= opt_event_type
;
705 strcpy(ev
->name
, "*");
706 /* kernel loglevels not implemented */
707 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
709 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
710 strcpy(ev
->name
, "*");
711 ev
->loglevel_type
= opt_loglevel_type
;
713 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
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
);
723 if (ev
->loglevel
== -1) {
724 ERR("Unknown loglevel %s", opt_loglevel
);
725 ret
= -LTTNG_ERR_INVALID
;
729 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
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
;
743 ret
= create_exclusion_list_and_validate("*",
744 opt_exclude
, &exclusion_list
);
751 warn_on_truncated_exclusion_names(exclusion_list
,
755 ret
= lttng_enable_event_with_exclusions(handle
,
758 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
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
);
767 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
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)",
772 print_channel_name(channel_name
),
778 ERR("Events: %s (channel %s, session %s)",
780 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
781 ? print_raw_channel_name(channel_name
)
782 : print_channel_name(channel_name
),
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
);
795 if (!exclusion_string
) {
796 PERROR("Cannot allocate exclusion_string");
800 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
801 get_domain_str(dom
.type
),
803 print_channel_name(channel_name
),
805 free(exclusion_string
);
807 char *exclusion_string
= print_exclusions(exclusion_list
);
809 if (!exclusion_string
) {
810 PERROR("Cannot allocate exclusion_string");
814 MSG("All %s tracepoints%s are enabled in channel %s",
815 get_domain_str(dom
.type
),
817 print_channel_name(channel_name
));
818 free(exclusion_string
);
821 case LTTNG_EVENT_SYSCALL
:
823 MSG("All %s system calls are enabled in channel %s",
824 get_domain_str(dom
.type
),
825 print_channel_name(channel_name
));
828 case LTTNG_EVENT_ALL
:
829 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
830 char *exclusion_string
= print_exclusions(exclusion_list
);
832 if (!exclusion_string
) {
833 PERROR("Cannot allocate exclusion_string");
837 MSG("All %s events%s are enabled in channel %s for loglevel %s",
838 get_domain_str(dom
.type
),
840 print_channel_name(channel_name
),
842 free(exclusion_string
);
844 char *exclusion_string
= print_exclusions(exclusion_list
);
846 if (!exclusion_string
) {
847 PERROR("Cannot allocate exclusion_string");
851 MSG("All %s events%s are enabled in channel %s",
852 get_domain_str(dom
.type
),
854 print_channel_name(channel_name
));
855 free(exclusion_string
);
860 * We should not be here since lttng_enable_event should have
861 * failed on the event type.
868 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
870 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
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
);
880 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
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\')",
885 print_channel_name(channel_name
),
886 session_name
, opt_filter
);
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
);
900 error_holder
= command_ret
;
903 MSG("Filter '%s' successfully set", opt_filter
);
908 /* The wildcard * is used for kernel and ust domain to
909 * represent ALL. We copy * in event name to force the wildcard use
912 * Note: this is strictly for semantic and printing while in
913 * machine interface mode.
915 strcpy(ev
->name
, "*");
917 /* If we reach here the events are enabled */
918 if (!error
&& !warn
) {
924 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
930 /* print exclusion */
931 ret
= mi_print_exclusion(exclusion_list
);
938 ret
= mi_lttng_writer_write_element_bool(writer
,
939 mi_lttng_element_command_success
, success
);
945 /* Close event element */
946 ret
= mi_lttng_writer_close_element(writer
);
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
;
964 /* Kernel tracer action */
966 DBG("Enabling kernel event %s for channel %s",
968 print_channel_name(channel_name
));
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
;
977 case LTTNG_EVENT_TRACEPOINT
:
979 case LTTNG_EVENT_PROBE
:
980 ret
= parse_probe_opts(ev
, opt_probe
);
982 ERR("Unable to parse probe options");
987 case LTTNG_EVENT_USERSPACE_PROBE
:
988 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
990 ret
= parse_userspace_probe_opts(opt_userspace_probe
, &uprobe_loc
);
993 case CMD_UNSUPPORTED
:
995 * Error message describing
996 * what is not supported was
997 * printed in the function.
1002 ERR("Unable to parse userspace probe options");
1008 ret
= lttng_event_set_userspace_probe_location(ev
, uprobe_loc
);
1010 WARN("Failed to set probe location on event");
1015 /* Ownership of the uprobe location was transferred to the event. */
1018 case LTTNG_EVENT_FUNCTION
:
1019 ret
= parse_probe_opts(ev
, opt_function
);
1021 ERR("Unable to parse function probe options");
1026 case LTTNG_EVENT_SYSCALL
:
1027 ev
->type
= LTTNG_EVENT_SYSCALL
;
1030 ret
= CMD_UNDEFINED
;
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>");
1040 switch (opt_event_type
) {
1041 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
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';
1049 case LTTNG_EVENT_PROBE
:
1050 case LTTNG_EVENT_FUNCTION
:
1051 case LTTNG_EVENT_SYSCALL
:
1052 case LTTNG_EVENT_USERSPACE_PROBE
:
1054 ERR("Event type not available for user-space tracing");
1055 ret
= CMD_UNSUPPORTED
;
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");
1066 /* Free previously allocated items */
1067 strutils_free_null_terminated_array_of_strings(
1069 exclusion_list
= NULL
;
1070 ret
= create_exclusion_list_and_validate(
1071 event_name
, opt_exclude
,
1078 warn_on_truncated_exclusion_names(
1079 exclusion_list
, &warn
);
1082 ev
->loglevel_type
= opt_loglevel_type
;
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
;
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
;
1101 ev
->loglevel_type
= opt_loglevel_type
;
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
);
1110 if (ev
->loglevel
== -1) {
1111 ERR("Unknown loglevel %s", opt_loglevel
);
1112 ret
= -LTTNG_ERR_INVALID
;
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
;
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';
1132 char *exclusion_string
;
1134 command_ret
= lttng_enable_event_with_exclusions(handle
,
1137 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1139 exclusion_string
= print_exclusions(exclusion_list
);
1140 if (!exclusion_string
) {
1141 PERROR("Cannot allocate exclusion_string");
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)",
1152 print_channel_name(channel_name
), session_name
);
1155 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
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
,
1161 print_channel_name(channel_name
),
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
),
1173 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
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
),
1183 error_holder
= command_ret
;
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
),
1192 print_channel_name(channel_name
));
1194 case LTTNG_DOMAIN_JUL
:
1195 case LTTNG_DOMAIN_LOG4J
:
1196 case LTTNG_DOMAIN_PYTHON
:
1198 * Don't print the default channel
1199 * name for agent domains.
1201 MSG("%s event %s%s enabled",
1202 get_domain_str(dom
.type
),
1210 free(exclusion_string
);
1214 char *exclusion_string
;
1216 /* Filter present */
1219 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1221 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1223 exclusion_string
= print_exclusions(exclusion_list
);
1224 if (!exclusion_string
) {
1225 PERROR("Cannot allocate exclusion_string");
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)",
1236 print_channel_name(channel_name
), session_name
);
1239 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
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
,
1245 print_channel_name(channel_name
),
1246 session_name
, opt_filter
);
1251 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
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
);
1261 error_holder
= command_ret
;
1264 MSG("Event %s%s: Filter '%s' successfully set",
1265 event_name
, exclusion_string
,
1268 free(exclusion_string
);
1279 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1285 /* print exclusion */
1286 ret
= mi_print_exclusion(exclusion_list
);
1293 ret
= mi_lttng_writer_write_element_bool(writer
,
1294 mi_lttng_element_command_success
, success
);
1300 /* Close event element */
1301 ret
= mi_lttng_writer_close_element(writer
);
1309 event_name
= strtok(NULL
, ",");
1310 /* Reset warn, error and success */
1317 /* Close events element */
1318 ret
= mi_lttng_writer_close_element(writer
);
1331 lttng_destroy_handle(handle
);
1332 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1333 lttng_userspace_probe_location_destroy(uprobe_loc
);
1335 /* Overwrite ret with error_holder if there was an actual error with
1336 * enabling an event.
1338 ret
= error_holder
? error_holder
: ret
;
1340 lttng_event_destroy(ev
);
1345 * Add event to trace session
1347 int cmd_enable_events(int argc
, const char **argv
)
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;
1355 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1356 poptReadDefaultConfig(pc
, 0);
1358 /* Default event type */
1359 opt_event_type
= LTTNG_EVENT_ALL
;
1361 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1366 case OPT_TRACEPOINT
:
1367 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1370 opt_event_type
= LTTNG_EVENT_PROBE
;
1372 case OPT_USERSPACE_PROBE
:
1373 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1376 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1379 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1385 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1386 opt_loglevel
= poptGetOptArg(pc
);
1388 case OPT_LOGLEVEL_ONLY
:
1389 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1390 opt_loglevel
= poptGetOptArg(pc
);
1392 case OPT_LIST_OPTIONS
:
1393 list_cmd_options(stdout
, long_options
);
1400 ret
= CMD_UNDEFINED
;
1404 /* Validate event type. Multiple event type are not supported. */
1405 if (event_type
== -1) {
1406 event_type
= opt_event_type
;
1408 if (event_type
!= opt_event_type
) {
1409 ERR("Multiple event type not supported.");
1416 ret
= print_missing_or_multiple_domains(
1417 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1427 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1429 ret
= -LTTNG_ERR_NOMEM
;
1433 /* Open command element */
1434 ret
= mi_lttng_writer_command_open(writer
,
1435 mi_lttng_element_command_enable_event
);
1441 /* Open output element */
1442 ret
= mi_lttng_writer_open_element(writer
,
1443 mi_lttng_element_command_output
);
1450 opt_event_list
= (char*) poptGetArg(pc
);
1451 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1452 ERR("Missing event name(s).\n");
1457 leftover
= poptGetArg(pc
);
1459 ERR("Unknown argument: %s", leftover
);
1464 if (!opt_session_name
) {
1465 session_name
= get_session_name();
1466 if (session_name
== NULL
) {
1467 command_ret
= CMD_ERROR
;
1472 session_name
= opt_session_name
;
1475 command_ret
= enable_events(session_name
);
1484 /* Close output element */
1485 ret
= mi_lttng_writer_close_element(writer
);
1491 ret
= mi_lttng_writer_write_element_bool(writer
,
1492 mi_lttng_element_command_success
, success
);
1498 /* Command element close */
1499 ret
= mi_lttng_writer_command_close(writer
);
1508 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1509 /* Preserve original error code */
1510 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1513 if (opt_session_name
== NULL
) {
1517 /* Overwrite ret if an error occurred in enable_events */
1518 ret
= command_ret
? command_ret
: ret
;
1520 poptFreeContext(pc
);