2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <sys/types.h>
18 #include <common/sessiond-comm/sessiond-comm.hpp>
19 #include <common/compat/string.hpp>
20 #include <common/compat/getenv.hpp>
21 #include <common/string-utils/string-utils.hpp>
22 #include <common/utils.hpp>
25 #include <common/mi-lttng.hpp>
27 #include <lttng/domain-internal.hpp>
28 #include <lttng/event-internal.hpp>
30 #include "../command.hpp"
31 #include "../loglevel.hpp"
32 #include "../uprobe.hpp"
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
= strtoull(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
= strtoull(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(const struct lttng_dynamic_pointer_array
*exclusions
)
200 const size_t count
= lttng_dynamic_pointer_array_get_count(exclusions
);
202 LTTNG_ASSERT(writer
);
209 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
214 for (i
= 0; i
< count
; i
++) {
215 const char *exclusion
= (const char *) lttng_dynamic_pointer_array_get_pointer(
218 ret
= mi_lttng_writer_write_element_string(writer
,
219 config_element_exclusion
, exclusion
);
225 /* Close exclusions element */
226 ret
= mi_lttng_writer_close_element(writer
);
233 * Return allocated string for pretty-printing exclusion names.
236 char *print_exclusions(const struct lttng_dynamic_pointer_array
*exclusions
)
240 const char preamble
[] = " excluding ";
242 const size_t count
= lttng_dynamic_pointer_array_get_count(exclusions
);
248 /* Calculate total required length. */
249 for (i
= 0; i
< count
; i
++) {
250 const char *exclusion
= (const char *) lttng_dynamic_pointer_array_get_pointer(
253 length
+= strlen(exclusion
) + 4;
256 length
+= sizeof(preamble
);
257 ret
= calloc
<char>(length
);
262 strncpy(ret
, preamble
, length
);
263 for (i
= 0; i
< count
; i
++) {
264 const char *exclusion
= (const char *) lttng_dynamic_pointer_array_get_pointer(
268 strcat(ret
, exclusion
);
270 if (i
!= count
- 1) {
279 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
283 const char *e
= event_name
;
284 const char *x
= exclusion
;
286 /* Scan both the excluder and the event letter by letter */
300 /* Event is a subset of the excluder */
301 ERR("Event %s: %s excludes all events from %s",
302 event_name
, exclusion
, event_name
);
308 * Reached the end of the event name before the
309 * end of the exclusion: this is valid.
331 WARN("Event %s: %s does not exclude any events from %s",
332 event_name
, exclusion
, event_name
);
338 int validate_exclusion_list(const char *event_name
,
339 const struct lttng_dynamic_pointer_array
*exclusions
)
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",
351 * If the event name is a star-at-end only globbing pattern,
352 * then we can validate the individual exclusions. Otherwise
353 * all exclusions are passed to the session daemon.
355 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
356 size_t i
, num_exclusions
;
358 num_exclusions
= lttng_dynamic_pointer_array_get_count(exclusions
);
360 for (i
= 0; i
< num_exclusions
; i
++) {
361 const char *exclusion
=
362 (const char *) lttng_dynamic_pointer_array_get_pointer(
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
);
385 static int create_exclusion_list_and_validate(const char *event_name
,
386 const char *exclusions_arg
,
387 struct lttng_dynamic_pointer_array
*exclusions
)
391 /* Split exclusions. */
392 ret
= strutils_split(exclusions_arg
, ',', true, exclusions
);
397 if (validate_exclusion_list(event_name
, exclusions
) !=
406 lttng_dynamic_pointer_array_reset(exclusions
);
412 static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer_array
*exclusions
,
416 const size_t num_exclusions
= lttng_dynamic_pointer_array_get_count(exclusions
);
418 for (i
= 0; i
< num_exclusions
; i
++) {
419 const char * const exclusion
= (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions
, i
);
421 if (strlen(exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
422 WARN("Event exclusion \"%s\" will be truncated",
430 * Enabling event using the lttng API.
431 * Note: in case of error only the last error code will be return.
433 static int enable_events(char *session_name
)
435 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
436 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
437 char *event_name
, *channel_name
= NULL
;
438 struct lttng_event
*ev
;
439 struct lttng_domain dom
= {};
440 struct lttng_dynamic_pointer_array exclusions
;
441 struct lttng_userspace_probe_location
*uprobe_loc
= NULL
;
443 lttng_dynamic_pointer_array_init(&exclusions
, NULL
);
445 ev
= lttng_event_create();
453 WARN("Kernel loglevels are not supported.");
457 /* Create lttng domain */
459 dom
.type
= LTTNG_DOMAIN_KERNEL
;
460 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
461 } else if (opt_userspace
) {
462 dom
.type
= LTTNG_DOMAIN_UST
;
464 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
465 } else if (opt_jul
) {
466 dom
.type
= LTTNG_DOMAIN_JUL
;
468 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
469 } else if (opt_log4j
) {
470 dom
.type
= LTTNG_DOMAIN_LOG4J
;
472 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
473 } else if (opt_python
) {
474 dom
.type
= LTTNG_DOMAIN_PYTHON
;
476 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
478 /* Checked by the caller. */
484 case LTTNG_DOMAIN_KERNEL
:
485 case LTTNG_DOMAIN_JUL
:
486 case LTTNG_DOMAIN_LOG4J
:
487 case LTTNG_DOMAIN_PYTHON
:
488 ERR("Event name exclusions are not yet implemented for %s events",
489 lttng_domain_type_str(dom
.type
));
492 case LTTNG_DOMAIN_UST
:
493 /* Exclusions supported */
501 * Adding a filter to a probe, function or userspace-probe would be
502 * denied by the kernel tracer as it's not supported at the moment. We
503 * do an early check here to warn the user.
505 if (opt_filter
&& opt_kernel
) {
506 switch (opt_event_type
) {
507 case LTTNG_EVENT_ALL
:
508 case LTTNG_EVENT_TRACEPOINT
:
509 case LTTNG_EVENT_SYSCALL
:
511 case LTTNG_EVENT_PROBE
:
512 case LTTNG_EVENT_USERSPACE_PROBE
:
513 case LTTNG_EVENT_FUNCTION
:
514 ERR("Filter expressions are not supported for %s events",
515 get_event_type_str((lttng_event_type
) opt_event_type
));
524 channel_name
= opt_channel_name
;
526 handle
= lttng_create_handle(session_name
, &dom
);
527 if (handle
== NULL
) {
534 /* Open a events element */
535 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
542 if (opt_enable_all
) {
543 /* Default setup for enable all */
545 ev
->type
= (lttng_event_type
) opt_event_type
;
546 strcpy(ev
->name
, "*");
547 /* kernel loglevels not implemented */
548 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
550 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
551 strcpy(ev
->name
, "*");
552 ev
->loglevel_type
= (lttng_loglevel_type
) opt_loglevel_type
;
556 LTTNG_ASSERT(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
559 enum lttng_loglevel loglevel
;
561 name_search_ret
= loglevel_name_to_value(opt_loglevel
, &loglevel
);
562 ev
->loglevel
= (int) loglevel
;
563 } else if (opt_jul
) {
564 enum lttng_loglevel_jul loglevel
;
566 name_search_ret
= loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
567 ev
->loglevel
= (int) loglevel
;
568 } else if (opt_log4j
) {
569 enum lttng_loglevel_log4j loglevel
;
571 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
, &loglevel
);
572 ev
->loglevel
= (int) loglevel
;
575 enum lttng_loglevel_python loglevel
;
577 name_search_ret
= loglevel_python_name_to_value(opt_loglevel
, &loglevel
);
578 ev
->loglevel
= (int) loglevel
;
581 if (name_search_ret
== -1) {
582 ERR("Unknown loglevel %s", opt_loglevel
);
583 ret
= -LTTNG_ERR_INVALID
;
587 LTTNG_ASSERT(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
590 } else if (opt_jul
) {
591 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
592 } else if (opt_log4j
) {
593 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
594 } else if (opt_python
) {
595 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
601 ret
= create_exclusion_list_and_validate("*",
602 opt_exclude
, &exclusions
);
609 warn_on_truncated_exclusion_names(&exclusions
,
613 ret
= lttng_enable_event_with_exclusions(handle
,
616 lttng_dynamic_pointer_array_get_count(&exclusions
),
617 (char **) exclusions
.array
.buffer
.data
);
620 case LTTNG_ERR_KERN_EVENT_EXIST
:
621 WARN("Kernel events already enabled (channel %s, session %s)",
622 print_channel_name(channel_name
), session_name
);
625 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
627 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
628 ERR("Events: %s (channel %s, session %s)",
630 print_channel_name(channel_name
),
636 ERR("Events: %s (channel %s, session %s)",
638 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
639 ? print_raw_channel_name(channel_name
)
640 : print_channel_name(channel_name
),
648 switch (opt_event_type
) {
649 case LTTNG_EVENT_TRACEPOINT
:
650 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
651 char *exclusion_string
= print_exclusions(&exclusions
);
653 if (!exclusion_string
) {
654 PERROR("Cannot allocate exclusion_string");
658 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
659 lttng_domain_type_str(dom
.type
),
661 print_channel_name(channel_name
),
663 free(exclusion_string
);
665 char *exclusion_string
= print_exclusions(&exclusions
);
667 if (!exclusion_string
) {
668 PERROR("Cannot allocate exclusion_string");
672 MSG("All %s tracepoints%s are enabled in channel %s",
673 lttng_domain_type_str(dom
.type
),
675 print_channel_name(channel_name
));
676 free(exclusion_string
);
679 case LTTNG_EVENT_SYSCALL
:
681 MSG("All %s system calls are enabled in channel %s",
682 lttng_domain_type_str(dom
.type
),
683 print_channel_name(channel_name
));
686 case LTTNG_EVENT_ALL
:
687 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
688 char *exclusion_string
= print_exclusions(&exclusions
);
690 if (!exclusion_string
) {
691 PERROR("Cannot allocate exclusion_string");
695 MSG("All %s events%s are enabled in channel %s for loglevel %s",
696 lttng_domain_type_str(dom
.type
),
698 print_channel_name(channel_name
),
700 free(exclusion_string
);
702 char *exclusion_string
= print_exclusions(&exclusions
);
704 if (!exclusion_string
) {
705 PERROR("Cannot allocate exclusion_string");
709 MSG("All %s events%s are enabled in channel %s",
710 lttng_domain_type_str(dom
.type
),
712 print_channel_name(channel_name
));
713 free(exclusion_string
);
718 * We should not be here since lttng_enable_event should have
719 * failed on the event type.
726 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
728 lttng_dynamic_pointer_array_get_count(&exclusions
),
729 (char **) exclusions
.array
.buffer
.data
);
730 if (command_ret
< 0) {
731 switch (-command_ret
) {
732 case LTTNG_ERR_FILTER_EXIST
:
733 WARN("Filter on all events is already enabled"
734 " (channel %s, session %s)",
735 print_channel_name(channel_name
), session_name
);
738 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
740 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
741 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
743 print_channel_name(channel_name
),
744 session_name
, opt_filter
);
749 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
750 lttng_strerror(command_ret
),
751 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
752 ? print_raw_channel_name(channel_name
)
753 : print_channel_name(channel_name
),
754 session_name
, opt_filter
);
758 error_holder
= command_ret
;
761 MSG("Filter '%s' successfully set", opt_filter
);
766 /* The wildcard * is used for kernel and ust domain to
767 * represent ALL. We copy * in event name to force the wildcard use
770 * Note: this is strictly for semantic and printing while in
771 * machine interface mode.
773 strcpy(ev
->name
, "*");
775 /* If we reach here the events are enabled */
776 if (!error
&& !warn
) {
782 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
788 /* print exclusion */
789 ret
= mi_print_exclusion(&exclusions
);
796 ret
= mi_lttng_writer_write_element_bool(writer
,
797 mi_lttng_element_command_success
, success
);
803 /* Close event element */
804 ret
= mi_lttng_writer_close_element(writer
);
814 /* Strip event list */
815 event_name
= strtok(opt_event_list
, ",");
816 while (event_name
!= NULL
) {
817 /* Copy name and type of the event */
818 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
819 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
820 ev
->type
= (lttng_event_type
) opt_event_type
;
822 /* Kernel tracer action */
824 DBG("Enabling kernel event %s for channel %s",
826 print_channel_name(channel_name
));
828 switch (opt_event_type
) {
829 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
830 /* If event name differs from *, select tracepoint. */
831 if (strcmp(ev
->name
, "*")) {
832 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
835 case LTTNG_EVENT_TRACEPOINT
:
837 case LTTNG_EVENT_PROBE
:
838 ret
= parse_probe_opts(ev
, opt_probe
);
840 ERR("Unable to parse probe options");
845 case LTTNG_EVENT_USERSPACE_PROBE
:
846 LTTNG_ASSERT(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
848 ret
= parse_userspace_probe_opts(opt_userspace_probe
, &uprobe_loc
);
851 case CMD_UNSUPPORTED
:
853 * Error message describing
854 * what is not supported was
855 * printed in the function.
860 ERR("Unable to parse userspace probe options");
866 ret
= lttng_event_set_userspace_probe_location(ev
, uprobe_loc
);
868 WARN("Failed to set probe location on event");
873 /* Ownership of the uprobe location was transferred to the event. */
876 case LTTNG_EVENT_FUNCTION
:
877 ret
= parse_probe_opts(ev
, opt_function
);
879 ERR("Unable to parse function probe options");
884 case LTTNG_EVENT_SYSCALL
:
885 ev
->type
= LTTNG_EVENT_SYSCALL
;
892 /* kernel loglevels not implemented */
893 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
894 } else if (opt_userspace
) { /* User-space tracer action */
895 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
896 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
898 switch (opt_event_type
) {
899 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
901 case LTTNG_EVENT_TRACEPOINT
:
902 /* Copy name and type of the event */
903 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
904 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
905 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
907 case LTTNG_EVENT_PROBE
:
908 case LTTNG_EVENT_FUNCTION
:
909 case LTTNG_EVENT_SYSCALL
:
910 case LTTNG_EVENT_USERSPACE_PROBE
:
912 ERR("Event type not available for user-space tracing");
913 ret
= CMD_UNSUPPORTED
;
919 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
920 ERR("Exclusion option can only be used with tracepoint events");
924 /* Free previously allocated items. */
925 lttng_dynamic_pointer_array_reset(&exclusions
);
926 ret
= create_exclusion_list_and_validate(
927 event_name
, opt_exclude
,
934 warn_on_truncated_exclusion_names(
938 ev
->loglevel_type
= (lttng_loglevel_type
) opt_loglevel_type
;
940 enum lttng_loglevel loglevel
;
941 const int name_search_ret
= loglevel_name_to_value(opt_loglevel
, &loglevel
);
943 if (name_search_ret
== -1) {
944 ERR("Unknown loglevel %s", opt_loglevel
);
945 ret
= -LTTNG_ERR_INVALID
;
949 ev
->loglevel
= (int) loglevel
;
953 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
954 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
955 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
956 ERR("Event type not supported for domain.");
957 ret
= CMD_UNSUPPORTED
;
961 ev
->loglevel_type
= (lttng_loglevel_type
) opt_loglevel_type
;
966 enum lttng_loglevel_jul loglevel
;
968 name_search_ret
= loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
969 ev
->loglevel
= (int) loglevel
;
970 } else if (opt_log4j
) {
971 enum lttng_loglevel_log4j loglevel
;
973 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
, &loglevel
);
974 ev
->loglevel
= (int) loglevel
;
977 enum lttng_loglevel_python loglevel
;
979 name_search_ret
= loglevel_python_name_to_value(opt_loglevel
, &loglevel
);
980 ev
->loglevel
= (int) loglevel
;
983 if (name_search_ret
) {
984 ERR("Unknown loglevel %s", opt_loglevel
);
985 ret
= -LTTNG_ERR_INVALID
;
990 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
991 } else if (opt_log4j
) {
992 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
993 } else if (opt_python
) {
994 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
997 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
998 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
999 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1005 char *exclusion_string
;
1007 command_ret
= lttng_enable_event_with_exclusions(handle
,
1010 lttng_dynamic_pointer_array_get_count(&exclusions
),
1011 (char **) exclusions
.array
.buffer
.data
);
1012 exclusion_string
= print_exclusions(&exclusions
);
1013 if (!exclusion_string
) {
1014 PERROR("Cannot allocate exclusion_string");
1018 if (command_ret
< 0) {
1019 /* Turn ret to positive value to handle the positive error code */
1020 switch (-command_ret
) {
1021 case LTTNG_ERR_KERN_EVENT_EXIST
:
1022 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1025 print_channel_name(channel_name
), session_name
);
1028 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1030 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1031 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1034 print_channel_name(channel_name
),
1039 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1040 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1041 event_name
, print_channel_name(channel_name
),
1046 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1048 lttng_strerror(command_ret
),
1049 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1050 ? print_raw_channel_name(channel_name
)
1051 : print_channel_name(channel_name
),
1056 error_holder
= command_ret
;
1059 case LTTNG_DOMAIN_KERNEL
:
1060 case LTTNG_DOMAIN_UST
:
1061 MSG("%s event %s%s created in channel %s",
1062 lttng_domain_type_str(dom
.type
),
1065 print_channel_name(channel_name
));
1067 case LTTNG_DOMAIN_JUL
:
1068 case LTTNG_DOMAIN_LOG4J
:
1069 case LTTNG_DOMAIN_PYTHON
:
1071 * Don't print the default channel
1072 * name for agent domains.
1074 MSG("%s event %s%s enabled",
1075 lttng_domain_type_str(dom
.type
),
1083 free(exclusion_string
);
1087 char *exclusion_string
;
1089 /* Filter present */
1092 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1094 lttng_dynamic_pointer_array_get_count(&exclusions
),
1095 (char **) exclusions
.array
.buffer
.data
);
1096 exclusion_string
= print_exclusions(&exclusions
);
1097 if (!exclusion_string
) {
1098 PERROR("Cannot allocate exclusion_string");
1102 if (command_ret
< 0) {
1103 switch (-command_ret
) {
1104 case LTTNG_ERR_FILTER_EXIST
:
1105 WARN("Filter on event %s%s is already enabled"
1106 " (channel %s, session %s)",
1109 print_channel_name(channel_name
), session_name
);
1112 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1114 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1115 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1118 print_channel_name(channel_name
),
1119 session_name
, opt_filter
);
1124 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1126 lttng_strerror(command_ret
),
1127 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1128 ? print_raw_channel_name(channel_name
)
1129 : print_channel_name(channel_name
),
1130 session_name
, opt_filter
);
1134 error_holder
= command_ret
;
1137 MSG("Event %s%s: Filter '%s' successfully set",
1138 event_name
, exclusion_string
,
1141 free(exclusion_string
);
1152 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1158 /* print exclusion */
1159 ret
= mi_print_exclusion(&exclusions
);
1166 ret
= mi_lttng_writer_write_element_bool(writer
,
1167 mi_lttng_element_command_success
, success
);
1173 /* Close event element */
1174 ret
= mi_lttng_writer_close_element(writer
);
1182 event_name
= strtok(NULL
, ",");
1183 /* Reset warn, error and success */
1190 /* Close events element */
1191 ret
= mi_lttng_writer_close_element(writer
);
1204 lttng_destroy_handle(handle
);
1205 lttng_dynamic_pointer_array_reset(&exclusions
);
1206 lttng_userspace_probe_location_destroy(uprobe_loc
);
1208 /* Overwrite ret with error_holder if there was an actual error with
1209 * enabling an event.
1211 ret
= error_holder
? error_holder
: ret
;
1213 lttng_event_destroy(ev
);
1218 * Add event to trace session
1220 int cmd_enable_events(int argc
, const char **argv
)
1222 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1223 static poptContext pc
;
1224 char *session_name
= NULL
;
1225 const char *leftover
= NULL
;
1226 int event_type
= -1;
1228 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1229 poptReadDefaultConfig(pc
, 0);
1231 /* Default event type */
1232 opt_event_type
= LTTNG_EVENT_ALL
;
1234 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1239 case OPT_TRACEPOINT
:
1240 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1243 opt_event_type
= LTTNG_EVENT_PROBE
;
1245 case OPT_USERSPACE_PROBE
:
1246 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1249 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1252 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1258 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1259 opt_loglevel
= poptGetOptArg(pc
);
1261 case OPT_LOGLEVEL_ONLY
:
1262 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1263 opt_loglevel
= poptGetOptArg(pc
);
1265 case OPT_LIST_OPTIONS
:
1266 list_cmd_options(stdout
, long_options
);
1273 ret
= CMD_UNDEFINED
;
1277 /* Validate event type. Multiple event type are not supported. */
1278 if (event_type
== -1) {
1279 event_type
= opt_event_type
;
1281 if (event_type
!= opt_event_type
) {
1282 ERR("Multiple event type not supported.");
1289 ret
= print_missing_or_multiple_domains(
1290 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1300 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1302 ret
= -LTTNG_ERR_NOMEM
;
1306 /* Open command element */
1307 ret
= mi_lttng_writer_command_open(writer
,
1308 mi_lttng_element_command_enable_event
);
1314 /* Open output element */
1315 ret
= mi_lttng_writer_open_element(writer
,
1316 mi_lttng_element_command_output
);
1323 opt_event_list
= (char*) poptGetArg(pc
);
1324 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1325 ERR("Missing event name(s).\n");
1330 leftover
= poptGetArg(pc
);
1332 ERR("Unknown argument: %s", leftover
);
1337 if (!opt_session_name
) {
1338 session_name
= get_session_name();
1339 if (session_name
== NULL
) {
1340 command_ret
= CMD_ERROR
;
1345 session_name
= opt_session_name
;
1348 command_ret
= enable_events(session_name
);
1357 /* Close output element */
1358 ret
= mi_lttng_writer_close_element(writer
);
1364 ret
= mi_lttng_writer_write_element_bool(writer
,
1365 mi_lttng_element_command_success
, success
);
1371 /* Command element close */
1372 ret
= mi_lttng_writer_command_close(writer
);
1381 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1382 /* Preserve original error code */
1383 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1386 if (opt_session_name
== NULL
) {
1390 /* Overwrite ret if an error occurred in enable_events */
1391 ret
= command_ret
? command_ret
: ret
;
1393 poptFreeContext(pc
);