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>
26 #include <common/mi-lttng.h>
28 #include <lttng/event-internal.h>
30 #include "../command.h"
31 #include "../loglevel.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 const char *print_channel_name(const char *name
)
183 return name
? : DEFAULT_CHANNEL_NAME
;
187 const char *print_raw_channel_name(const char *name
)
189 return name
? : "<default>";
193 * Mi print exlcusion list
196 int mi_print_exclusion(char **names
)
199 int count
= names
? strutils_array_of_strings_len(names
) : 0;
207 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
212 for (i
= 0; i
< count
; i
++) {
213 ret
= mi_lttng_writer_write_element_string(writer
,
214 config_element_exclusion
, names
[i
]);
220 /* Close exclusions element */
221 ret
= mi_lttng_writer_close_element(writer
);
228 * Return allocated string for pretty-printing exclusion names.
231 char *print_exclusions(char **names
)
235 const char preamble
[] = " excluding ";
237 int count
= names
? strutils_array_of_strings_len(names
) : 0;
243 /* calculate total required length */
244 for (i
= 0; i
< count
; i
++) {
245 length
+= strlen(names
[i
]) + 4;
248 length
+= sizeof(preamble
);
249 ret
= zmalloc(length
);
253 strncpy(ret
, preamble
, length
);
254 for (i
= 0; i
< count
; i
++) {
256 strcat(ret
, names
[i
]);
258 if (i
!= count
- 1) {
267 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
271 const char *e
= event_name
;
272 const char *x
= exclusion
;
274 /* Scan both the excluder and the event letter by letter */
288 /* Event is a subset of the excluder */
289 ERR("Event %s: %s excludes all events from %s",
290 event_name
, exclusion
, event_name
);
296 * Reached the end of the event name before the
297 * end of the exclusion: this is valid.
319 WARN("Event %s: %s does not exclude any events from %s",
320 event_name
, exclusion
, event_name
);
327 * FIXME: find a good place to declare this since add trigger also uses it
330 int create_exclusion_list_and_validate(const char *event_name
,
331 const char *exclusions_arg
,
332 char ***exclusion_list
);
336 int create_exclusion_list_and_validate(const char *event_name
,
337 const char *exclusions_arg
,
338 char ***exclusion_list
)
341 char **exclusions
= NULL
;
343 /* Event name must be a valid globbing pattern to allow exclusions. */
344 if (!strutils_is_star_glob_pattern(event_name
)) {
345 ERR("Event %s: Exclusions can only be used with a globbing pattern",
350 /* Split exclusions. */
351 exclusions
= strutils_split(exclusions_arg
, ',', true);
357 * If the event name is a star-at-end only globbing pattern,
358 * then we can validate the individual exclusions. Otherwise
359 * all exclusions are passed to the session daemon.
361 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
362 char * const *exclusion
;
364 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
365 if (!strutils_is_star_glob_pattern(*exclusion
) ||
366 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
367 ret
= check_exclusion_subsets(event_name
, *exclusion
);
375 *exclusion_list
= exclusions
;
381 strutils_free_null_terminated_array_of_strings(exclusions
);
387 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
390 char * const *exclusion
;
392 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
393 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
394 WARN("Event exclusion \"%s\" will be truncated",
402 * Enabling event using the lttng API.
403 * Note: in case of error only the last error code will be return.
405 static int enable_events(char *session_name
)
407 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
408 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
409 char *event_name
, *channel_name
= NULL
;
410 struct lttng_event
*ev
;
411 struct lttng_domain dom
;
412 char **exclusion_list
= NULL
;
413 struct lttng_userspace_probe_location
*uprobe_loc
= NULL
;
415 memset(&dom
, 0, sizeof(dom
));
417 ev
= lttng_event_create();
425 WARN("Kernel loglevels are not supported.");
429 /* Create lttng domain */
431 dom
.type
= LTTNG_DOMAIN_KERNEL
;
432 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
433 } else if (opt_userspace
) {
434 dom
.type
= LTTNG_DOMAIN_UST
;
436 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
437 } else if (opt_jul
) {
438 dom
.type
= LTTNG_DOMAIN_JUL
;
440 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
441 } else if (opt_log4j
) {
442 dom
.type
= LTTNG_DOMAIN_LOG4J
;
444 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
445 } else if (opt_python
) {
446 dom
.type
= LTTNG_DOMAIN_PYTHON
;
448 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
450 /* Checked by the caller. */
456 case LTTNG_DOMAIN_KERNEL
:
457 case LTTNG_DOMAIN_JUL
:
458 case LTTNG_DOMAIN_LOG4J
:
459 case LTTNG_DOMAIN_PYTHON
:
460 ERR("Event name exclusions are not yet implemented for %s events",
461 get_domain_str(dom
.type
));
464 case LTTNG_DOMAIN_UST
:
465 /* Exclusions supported */
473 * Adding a filter to a probe, function or userspace-probe would be
474 * denied by the kernel tracer as it's not supported at the moment. We
475 * do an early check here to warn the user.
477 if (opt_filter
&& opt_kernel
) {
478 switch (opt_event_type
) {
479 case LTTNG_EVENT_ALL
:
480 case LTTNG_EVENT_TRACEPOINT
:
481 case LTTNG_EVENT_SYSCALL
:
483 case LTTNG_EVENT_PROBE
:
484 case LTTNG_EVENT_USERSPACE_PROBE
:
485 case LTTNG_EVENT_FUNCTION
:
486 ERR("Filter expressions are not supported for %s events",
487 get_event_type_str(opt_event_type
));
496 channel_name
= opt_channel_name
;
498 handle
= lttng_create_handle(session_name
, &dom
);
499 if (handle
== NULL
) {
506 /* Open a events element */
507 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
514 if (opt_enable_all
) {
515 /* Default setup for enable all */
517 ev
->type
= opt_event_type
;
518 strcpy(ev
->name
, "*");
519 /* kernel loglevels not implemented */
520 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
522 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
523 strcpy(ev
->name
, "*");
524 ev
->loglevel_type
= opt_loglevel_type
;
528 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
531 enum lttng_loglevel loglevel
;
533 name_search_ret
= loglevel_name_to_value(opt_loglevel
, &loglevel
);
534 ev
->loglevel
= (int) loglevel
;
535 } else if (opt_jul
) {
536 enum lttng_loglevel_jul loglevel
;
538 name_search_ret
= loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
539 ev
->loglevel
= (int) loglevel
;
540 } else if (opt_log4j
) {
541 enum lttng_loglevel_log4j loglevel
;
543 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
, &loglevel
);
544 ev
->loglevel
= (int) loglevel
;
547 enum lttng_loglevel_python loglevel
;
549 name_search_ret
= loglevel_python_name_to_value(opt_loglevel
, &loglevel
);
550 ev
->loglevel
= (int) loglevel
;
553 if (name_search_ret
== -1) {
554 ERR("Unknown loglevel %s", opt_loglevel
);
555 ret
= -LTTNG_ERR_INVALID
;
559 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
562 } else if (opt_jul
) {
563 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
564 } else if (opt_log4j
) {
565 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
566 } else if (opt_python
) {
567 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
573 ret
= create_exclusion_list_and_validate("*",
574 opt_exclude
, &exclusion_list
);
581 warn_on_truncated_exclusion_names(exclusion_list
,
585 ret
= lttng_enable_event_with_exclusions(handle
,
588 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
592 case LTTNG_ERR_KERN_EVENT_EXIST
:
593 WARN("Kernel events already enabled (channel %s, session %s)",
594 print_channel_name(channel_name
), session_name
);
597 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
599 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
600 ERR("Events: %s (channel %s, session %s)",
602 print_channel_name(channel_name
),
608 ERR("Events: %s (channel %s, session %s)",
610 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
611 ? print_raw_channel_name(channel_name
)
612 : print_channel_name(channel_name
),
620 switch (opt_event_type
) {
621 case LTTNG_EVENT_TRACEPOINT
:
622 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
623 char *exclusion_string
= print_exclusions(exclusion_list
);
625 if (!exclusion_string
) {
626 PERROR("Cannot allocate exclusion_string");
630 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
631 get_domain_str(dom
.type
),
633 print_channel_name(channel_name
),
635 free(exclusion_string
);
637 char *exclusion_string
= print_exclusions(exclusion_list
);
639 if (!exclusion_string
) {
640 PERROR("Cannot allocate exclusion_string");
644 MSG("All %s tracepoints%s are enabled in channel %s",
645 get_domain_str(dom
.type
),
647 print_channel_name(channel_name
));
648 free(exclusion_string
);
651 case LTTNG_EVENT_SYSCALL
:
653 MSG("All %s system calls are enabled in channel %s",
654 get_domain_str(dom
.type
),
655 print_channel_name(channel_name
));
658 case LTTNG_EVENT_ALL
:
659 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
660 char *exclusion_string
= print_exclusions(exclusion_list
);
662 if (!exclusion_string
) {
663 PERROR("Cannot allocate exclusion_string");
667 MSG("All %s events%s are enabled in channel %s for loglevel %s",
668 get_domain_str(dom
.type
),
670 print_channel_name(channel_name
),
672 free(exclusion_string
);
674 char *exclusion_string
= print_exclusions(exclusion_list
);
676 if (!exclusion_string
) {
677 PERROR("Cannot allocate exclusion_string");
681 MSG("All %s events%s are enabled in channel %s",
682 get_domain_str(dom
.type
),
684 print_channel_name(channel_name
));
685 free(exclusion_string
);
690 * We should not be here since lttng_enable_event should have
691 * failed on the event type.
698 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
700 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
702 if (command_ret
< 0) {
703 switch (-command_ret
) {
704 case LTTNG_ERR_FILTER_EXIST
:
705 WARN("Filter on all events is already enabled"
706 " (channel %s, session %s)",
707 print_channel_name(channel_name
), session_name
);
710 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
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("All events: %s (channel %s, session %s, filter \'%s\')",
715 print_channel_name(channel_name
),
716 session_name
, opt_filter
);
721 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
722 lttng_strerror(command_ret
),
723 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
724 ? print_raw_channel_name(channel_name
)
725 : print_channel_name(channel_name
),
726 session_name
, opt_filter
);
730 error_holder
= command_ret
;
733 MSG("Filter '%s' successfully set", opt_filter
);
738 /* The wildcard * is used for kernel and ust domain to
739 * represent ALL. We copy * in event name to force the wildcard use
742 * Note: this is strictly for semantic and printing while in
743 * machine interface mode.
745 strcpy(ev
->name
, "*");
747 /* If we reach here the events are enabled */
748 if (!error
&& !warn
) {
754 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
760 /* print exclusion */
761 ret
= mi_print_exclusion(exclusion_list
);
768 ret
= mi_lttng_writer_write_element_bool(writer
,
769 mi_lttng_element_command_success
, success
);
775 /* Close event element */
776 ret
= mi_lttng_writer_close_element(writer
);
786 /* Strip event list */
787 event_name
= strtok(opt_event_list
, ",");
788 while (event_name
!= NULL
) {
789 /* Copy name and type of the event */
790 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
791 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
792 ev
->type
= opt_event_type
;
794 /* Kernel tracer action */
796 DBG("Enabling kernel event %s for channel %s",
798 print_channel_name(channel_name
));
800 switch (opt_event_type
) {
801 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
802 /* If event name differs from *, select tracepoint. */
803 if (strcmp(ev
->name
, "*")) {
804 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
807 case LTTNG_EVENT_TRACEPOINT
:
809 case LTTNG_EVENT_PROBE
:
810 ret
= parse_probe_opts(ev
, opt_probe
);
812 ERR("Unable to parse probe options");
817 case LTTNG_EVENT_USERSPACE_PROBE
:
818 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
820 ret
= parse_userspace_probe_opts(opt_userspace_probe
, &uprobe_loc
);
823 case CMD_UNSUPPORTED
:
825 * Error message describing
826 * what is not supported was
827 * printed in the function.
832 ERR("Unable to parse userspace probe options");
838 ret
= lttng_event_set_userspace_probe_location(ev
, uprobe_loc
);
840 WARN("Failed to set probe location on event");
845 /* Ownership of the uprobe location was transferred to the event. */
848 case LTTNG_EVENT_FUNCTION
:
849 ret
= parse_probe_opts(ev
, opt_function
);
851 ERR("Unable to parse function probe options");
856 case LTTNG_EVENT_SYSCALL
:
857 ev
->type
= LTTNG_EVENT_SYSCALL
;
864 /* kernel loglevels not implemented */
865 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
866 } else if (opt_userspace
) { /* User-space tracer action */
867 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
868 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
870 switch (opt_event_type
) {
871 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
873 case LTTNG_EVENT_TRACEPOINT
:
874 /* Copy name and type of the event */
875 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
876 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
877 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
879 case LTTNG_EVENT_PROBE
:
880 case LTTNG_EVENT_FUNCTION
:
881 case LTTNG_EVENT_SYSCALL
:
882 case LTTNG_EVENT_USERSPACE_PROBE
:
884 ERR("Event type not available for user-space tracing");
885 ret
= CMD_UNSUPPORTED
;
891 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
892 ERR("Exclusion option can only be used with tracepoint events");
896 /* Free previously allocated items */
897 strutils_free_null_terminated_array_of_strings(
899 exclusion_list
= NULL
;
900 ret
= create_exclusion_list_and_validate(
901 event_name
, opt_exclude
,
908 warn_on_truncated_exclusion_names(
909 exclusion_list
, &warn
);
912 ev
->loglevel_type
= opt_loglevel_type
;
914 enum lttng_loglevel loglevel
;
915 const int name_search_ret
= loglevel_name_to_value(opt_loglevel
, &loglevel
);
917 if (name_search_ret
== -1) {
918 ERR("Unknown loglevel %s", opt_loglevel
);
919 ret
= -LTTNG_ERR_INVALID
;
923 ev
->loglevel
= (int) loglevel
;
927 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
928 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
929 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
930 ERR("Event type not supported for domain.");
931 ret
= CMD_UNSUPPORTED
;
935 ev
->loglevel_type
= opt_loglevel_type
;
940 enum lttng_loglevel_jul loglevel
;
942 name_search_ret
= loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
943 ev
->loglevel
= (int) loglevel
;
944 } else if (opt_log4j
) {
945 enum lttng_loglevel_log4j loglevel
;
947 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
, &loglevel
);
948 ev
->loglevel
= (int) loglevel
;
951 enum lttng_loglevel_python loglevel
;
953 name_search_ret
= loglevel_python_name_to_value(opt_loglevel
, &loglevel
);
954 ev
->loglevel
= (int) loglevel
;
957 if (name_search_ret
) {
958 ERR("Unknown loglevel %s", opt_loglevel
);
959 ret
= -LTTNG_ERR_INVALID
;
964 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
965 } else if (opt_log4j
) {
966 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
967 } else if (opt_python
) {
968 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
971 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
972 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
973 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
979 char *exclusion_string
;
981 command_ret
= lttng_enable_event_with_exclusions(handle
,
984 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
986 exclusion_string
= print_exclusions(exclusion_list
);
987 if (!exclusion_string
) {
988 PERROR("Cannot allocate exclusion_string");
992 if (command_ret
< 0) {
993 /* Turn ret to positive value to handle the positive error code */
994 switch (-command_ret
) {
995 case LTTNG_ERR_KERN_EVENT_EXIST
:
996 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
999 print_channel_name(channel_name
), session_name
);
1002 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1004 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1005 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1008 print_channel_name(channel_name
),
1013 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1014 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1015 event_name
, print_channel_name(channel_name
),
1020 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1022 lttng_strerror(command_ret
),
1023 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1024 ? print_raw_channel_name(channel_name
)
1025 : print_channel_name(channel_name
),
1030 error_holder
= command_ret
;
1033 case LTTNG_DOMAIN_KERNEL
:
1034 case LTTNG_DOMAIN_UST
:
1035 MSG("%s event %s%s created in channel %s",
1036 get_domain_str(dom
.type
),
1039 print_channel_name(channel_name
));
1041 case LTTNG_DOMAIN_JUL
:
1042 case LTTNG_DOMAIN_LOG4J
:
1043 case LTTNG_DOMAIN_PYTHON
:
1045 * Don't print the default channel
1046 * name for agent domains.
1048 MSG("%s event %s%s enabled",
1049 get_domain_str(dom
.type
),
1057 free(exclusion_string
);
1061 char *exclusion_string
;
1063 /* Filter present */
1066 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1068 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1070 exclusion_string
= print_exclusions(exclusion_list
);
1071 if (!exclusion_string
) {
1072 PERROR("Cannot allocate exclusion_string");
1076 if (command_ret
< 0) {
1077 switch (-command_ret
) {
1078 case LTTNG_ERR_FILTER_EXIST
:
1079 WARN("Filter on event %s%s is already enabled"
1080 " (channel %s, session %s)",
1083 print_channel_name(channel_name
), session_name
);
1086 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1088 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1089 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1092 print_channel_name(channel_name
),
1093 session_name
, opt_filter
);
1098 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1100 lttng_strerror(command_ret
),
1101 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1102 ? print_raw_channel_name(channel_name
)
1103 : print_channel_name(channel_name
),
1104 session_name
, opt_filter
);
1108 error_holder
= command_ret
;
1111 MSG("Event %s%s: Filter '%s' successfully set",
1112 event_name
, exclusion_string
,
1115 free(exclusion_string
);
1126 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1132 /* print exclusion */
1133 ret
= mi_print_exclusion(exclusion_list
);
1140 ret
= mi_lttng_writer_write_element_bool(writer
,
1141 mi_lttng_element_command_success
, success
);
1147 /* Close event element */
1148 ret
= mi_lttng_writer_close_element(writer
);
1156 event_name
= strtok(NULL
, ",");
1157 /* Reset warn, error and success */
1164 /* Close events element */
1165 ret
= mi_lttng_writer_close_element(writer
);
1178 lttng_destroy_handle(handle
);
1179 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1180 lttng_userspace_probe_location_destroy(uprobe_loc
);
1182 /* Overwrite ret with error_holder if there was an actual error with
1183 * enabling an event.
1185 ret
= error_holder
? error_holder
: ret
;
1187 lttng_event_destroy(ev
);
1192 * Add event to trace session
1194 int cmd_enable_events(int argc
, const char **argv
)
1196 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1197 static poptContext pc
;
1198 char *session_name
= NULL
;
1199 const char *leftover
= NULL
;
1200 int event_type
= -1;
1202 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1203 poptReadDefaultConfig(pc
, 0);
1205 /* Default event type */
1206 opt_event_type
= LTTNG_EVENT_ALL
;
1208 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1213 case OPT_TRACEPOINT
:
1214 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1217 opt_event_type
= LTTNG_EVENT_PROBE
;
1219 case OPT_USERSPACE_PROBE
:
1220 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1223 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1226 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1232 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1233 opt_loglevel
= poptGetOptArg(pc
);
1235 case OPT_LOGLEVEL_ONLY
:
1236 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1237 opt_loglevel
= poptGetOptArg(pc
);
1239 case OPT_LIST_OPTIONS
:
1240 list_cmd_options(stdout
, long_options
);
1247 ret
= CMD_UNDEFINED
;
1251 /* Validate event type. Multiple event type are not supported. */
1252 if (event_type
== -1) {
1253 event_type
= opt_event_type
;
1255 if (event_type
!= opt_event_type
) {
1256 ERR("Multiple event type not supported.");
1263 ret
= print_missing_or_multiple_domains(
1264 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1274 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1276 ret
= -LTTNG_ERR_NOMEM
;
1280 /* Open command element */
1281 ret
= mi_lttng_writer_command_open(writer
,
1282 mi_lttng_element_command_enable_event
);
1288 /* Open output element */
1289 ret
= mi_lttng_writer_open_element(writer
,
1290 mi_lttng_element_command_output
);
1297 opt_event_list
= (char*) poptGetArg(pc
);
1298 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1299 ERR("Missing event name(s).\n");
1304 leftover
= poptGetArg(pc
);
1306 ERR("Unknown argument: %s", leftover
);
1311 if (!opt_session_name
) {
1312 session_name
= get_session_name();
1313 if (session_name
== NULL
) {
1314 command_ret
= CMD_ERROR
;
1319 session_name
= opt_session_name
;
1322 command_ret
= enable_events(session_name
);
1331 /* Close output element */
1332 ret
= mi_lttng_writer_close_element(writer
);
1338 ret
= mi_lttng_writer_write_element_bool(writer
,
1339 mi_lttng_element_command_success
, success
);
1345 /* Command element close */
1346 ret
= mi_lttng_writer_command_close(writer
);
1355 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1356 /* Preserve original error code */
1357 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1360 if (opt_session_name
== NULL
) {
1364 /* Overwrite ret if an error occurred in enable_events */
1365 ret
= command_ret
? command_ret
: ret
;
1367 poptFreeContext(pc
);