2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include <common/compat/getenv.hpp>
10 #include <common/compat/string.hpp>
11 #include <common/make-unique-wrapper.hpp>
12 #include <common/scope-exit.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <common/string-utils/string-utils.hpp>
15 #include <common/utils.hpp>
25 #include <sys/types.h>
30 #include "../command.hpp"
31 #include "../loglevel.hpp"
32 #include "../uprobe.hpp"
34 #include <common/mi-lttng.hpp>
36 #include <lttng/domain-internal.hpp>
37 #include <lttng/event-internal.hpp>
39 #if (LTTNG_SYMBOL_NAME_LEN == 256)
40 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
44 void _mi_lttng_writer_deleter_func(mi_writer
*writer
)
46 if (writer
&& mi_lttng_writer_destroy(writer
)) {
47 LTTNG_THROW_ERROR("Failed to destroy mi_writer instance");
51 using mi_writer_uptr
= std::unique_ptr
<
53 lttng::memory::create_deleter_class
<mi_writer
, _mi_lttng_writer_deleter_func
>::deleter
>;
54 using event_rule_patterns
= std::vector
<std::string
>;
57 const char *opt_loglevel
;
58 int opt_loglevel_type
;
60 char *opt_session_name
;
67 char *opt_userspace_probe
;
69 char *opt_channel_name
;
73 struct lttng_handle
*handle
;
74 mi_writer_uptr writer
;
76 #ifdef LTTNG_EMBED_HELP
77 static const char help_msg
[] =
78 #include <lttng-enable-event.1.h>
97 struct poptOption long_options
[] = {
98 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
99 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
100 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
101 { "all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, nullptr, nullptr },
102 { "channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, nullptr, nullptr },
103 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
104 { "userspace", 'u', POPT_ARG_NONE
, nullptr, OPT_USERSPACE
, nullptr, nullptr },
105 { "jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, nullptr, nullptr },
106 { "log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, nullptr, nullptr },
107 { "python", 'p', POPT_ARG_VAL
, &opt_python
, 1, nullptr, nullptr },
108 { "tracepoint", 0, POPT_ARG_NONE
, nullptr, OPT_TRACEPOINT
, nullptr, nullptr },
109 { "probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, nullptr, nullptr },
113 &opt_userspace_probe
,
117 { "function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, nullptr, nullptr },
118 { "syscall", 0, POPT_ARG_NONE
, nullptr, OPT_SYSCALL
, nullptr, nullptr },
119 { "loglevel", 0, POPT_ARG_STRING
, nullptr, OPT_LOGLEVEL
, nullptr, nullptr },
120 { "loglevel-only", 0, POPT_ARG_STRING
, nullptr, OPT_LOGLEVEL_ONLY
, nullptr, nullptr },
121 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
122 { "filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, nullptr, nullptr },
123 { "exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, nullptr, nullptr },
124 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
128 * Parse probe options.
130 int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
132 int ret
= CMD_SUCCESS
;
135 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
136 char name
[LTTNG_SYMBOL_NAME_LEN
];
138 if (opt
== nullptr) {
143 /* Check for symbol+offset */
145 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
146 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s",
150 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
151 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
152 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
153 if (*s_hex
== '\0') {
154 ERR("Invalid probe offset %s", s_hex
);
158 ev
->attr
.probe
.offset
= strtoull(s_hex
, nullptr, 0);
159 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
160 ev
->attr
.probe
.addr
= 0;
164 /* Check for symbol */
165 if (isalpha(name
[0]) || name
[0] == '_') {
166 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s", name
);
168 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
169 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
170 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
171 ev
->attr
.probe
.offset
= 0;
172 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
173 ev
->attr
.probe
.addr
= 0;
178 /* Check for address */
179 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
182 * Return an error if the first character of the tentative
183 * address is NULL or not a digit. It can be "0" if the address
184 * is in hexadecimal and can be 1 to 9 if it's in decimal.
186 if (*s_hex
== '\0' || !isdigit(*s_hex
)) {
187 ERR("Invalid probe description `%s`", s_hex
);
191 ev
->attr
.probe
.addr
= strtoull(s_hex
, nullptr, 0);
192 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
193 ev
->attr
.probe
.offset
= 0;
194 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
205 const char *print_channel_name(const char *name
)
207 return name
?: DEFAULT_CHANNEL_NAME
;
210 const char *print_raw_channel_name(const char *name
)
212 return name
?: "<default>";
216 * Mi print exlcusion list
218 int mi_print_exclusion(const struct lttng_dynamic_pointer_array
*exclusions
)
222 const size_t count
= lttng_dynamic_pointer_array_get_count(exclusions
);
224 LTTNG_ASSERT(writer
);
231 ret
= mi_lttng_writer_open_element(writer
.get(), config_element_exclusions
);
236 for (i
= 0; i
< count
; i
++) {
237 const char *exclusion
=
238 (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions
, i
);
240 ret
= mi_lttng_writer_write_element_string(
241 writer
.get(), config_element_exclusion
, exclusion
);
247 /* Close exclusions element */
248 ret
= mi_lttng_writer_close_element(writer
.get());
255 * Return allocated string for pretty-printing exclusion names.
257 char *print_exclusions(const struct lttng_dynamic_pointer_array
*exclusions
)
261 const char preamble
[] = " excluding ";
263 const size_t count
= lttng_dynamic_pointer_array_get_count(exclusions
);
269 /* Calculate total required length. */
270 for (i
= 0; i
< count
; i
++) {
271 const char *exclusion
=
272 (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions
, i
);
274 length
+= strlen(exclusion
) + 4;
277 length
+= sizeof(preamble
);
278 ret
= calloc
<char>(length
);
283 strncpy(ret
, preamble
, length
);
284 for (i
= 0; i
< count
; i
++) {
285 const char *exclusion
=
286 (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions
, i
);
289 strcat(ret
, exclusion
);
291 if (i
!= count
- 1) {
299 int check_exclusion_subsets(const char *pattern
, const char *exclusion
)
303 const char *e
= pattern
;
304 const char *x
= exclusion
;
306 /* Scan both the excluder and the event letter by letter */
320 /* Event is a subset of the excluder */
321 ERR("Exclusion pattern `%s` excludes all events from `%s`",
329 * Reached the end of the event name before the
330 * end of the exclusion: this is valid.
352 WARN("Exclusion pattern `%s` does not exclude any event from `%s`",
360 int create_exclusion_list_and_validate(const char *pattern
,
361 const char *exclusions_arg
,
362 struct lttng_dynamic_pointer_array
*exclusions
)
366 /* Split exclusions. */
367 ret
= strutils_split(exclusions_arg
, ',', true, exclusions
);
372 if (validate_exclusion_list(pattern
, exclusions
) != 0) {
380 lttng_dynamic_pointer_array_reset(exclusions
);
386 void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer_array
*exclusions
,
390 const size_t num_exclusions
= lttng_dynamic_pointer_array_get_count(exclusions
);
392 for (i
= 0; i
< num_exclusions
; i
++) {
393 const char *const exclusion
=
394 (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions
, i
);
396 if (strlen(exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
397 WARN("Event exclusion \"%s\" will be truncated", exclusion
);
404 * Enabling event using the lttng API.
405 * Note: in case of error only the last error code will be return.
407 int enable_events(const std::string
& session_name
, const event_rule_patterns
& patterns
)
409 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
410 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
411 char *channel_name
= nullptr;
412 struct lttng_event
*ev
;
413 struct lttng_domain dom
= {};
414 struct lttng_dynamic_pointer_array exclusions
;
415 struct lttng_userspace_probe_location
*uprobe_loc
= nullptr;
417 lttng_dynamic_pointer_array_init(&exclusions
, nullptr);
419 ev
= lttng_event_create();
427 WARN("Kernel log levels are not supported");
431 /* Create lttng domain */
433 dom
.type
= LTTNG_DOMAIN_KERNEL
;
434 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
435 } else if (opt_userspace
) {
436 dom
.type
= LTTNG_DOMAIN_UST
;
438 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
439 } else if (opt_jul
) {
440 dom
.type
= LTTNG_DOMAIN_JUL
;
442 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
443 } else if (opt_log4j
) {
444 dom
.type
= LTTNG_DOMAIN_LOG4J
;
446 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
447 } else if (opt_python
) {
448 dom
.type
= LTTNG_DOMAIN_PYTHON
;
450 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
452 /* Checked by the caller. */
458 case LTTNG_DOMAIN_KERNEL
:
459 case LTTNG_DOMAIN_JUL
:
460 case LTTNG_DOMAIN_LOG4J
:
461 case LTTNG_DOMAIN_PYTHON
:
462 ERR("Event name exclusions are not supported for %s event rules",
463 lttng_domain_type_str(dom
.type
));
466 case LTTNG_DOMAIN_UST
:
467 /* Exclusions supported */
475 * Adding a filter to a probe, function or userspace-probe would be
476 * denied by the kernel tracer as it's not supported at the moment. We
477 * do an early check here to warn the user.
479 if (opt_filter
&& opt_kernel
) {
480 switch (opt_event_type
) {
481 case LTTNG_EVENT_ALL
:
482 case LTTNG_EVENT_TRACEPOINT
:
483 case LTTNG_EVENT_SYSCALL
:
485 case LTTNG_EVENT_PROBE
:
486 case LTTNG_EVENT_USERSPACE_PROBE
:
487 case LTTNG_EVENT_FUNCTION
:
488 ERR("Filter expressions are not supported for %s event rules",
489 get_event_type_str((lttng_event_type
) opt_event_type
));
498 channel_name
= opt_channel_name
;
500 handle
= lttng_create_handle(session_name
.c_str(), &dom
);
501 if (handle
== nullptr) {
508 /* Open a events element */
509 ret
= mi_lttng_writer_open_element(writer
.get(), config_element_events
);
516 for (const auto& pattern
: patterns
) {
517 /* Copy name and type of the event */
518 strncpy(ev
->name
, pattern
.c_str(), LTTNG_SYMBOL_NAME_LEN
);
519 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
520 ev
->type
= (lttng_event_type
) opt_event_type
;
522 /* Kernel tracer action */
524 DBG_FMT("Enabling kernel event rule: pattern=`{}`, channel_name=`{}`",
526 print_channel_name(channel_name
));
528 switch (opt_event_type
) {
529 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
530 /* If event name differs from *, select tracepoint. */
531 if (strcmp(ev
->name
, "*") != 0) {
532 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
535 case LTTNG_EVENT_TRACEPOINT
:
537 case LTTNG_EVENT_PROBE
:
538 ret
= parse_probe_opts(ev
, opt_probe
);
540 ERR("Unable to parse probe options");
545 case LTTNG_EVENT_USERSPACE_PROBE
:
546 LTTNG_ASSERT(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
548 ret
= parse_userspace_probe_opts(opt_userspace_probe
, &uprobe_loc
);
551 case CMD_UNSUPPORTED
:
553 * Error message describing
554 * what is not supported was
555 * printed in the function.
560 ERR("Unable to parse user space probe options");
566 ret
= lttng_event_set_userspace_probe_location(ev
, uprobe_loc
);
568 WARN("Failed to set probe location on event");
573 /* Ownership of the uprobe location was transferred to the event. */
574 uprobe_loc
= nullptr;
576 case LTTNG_EVENT_FUNCTION
:
577 ret
= parse_probe_opts(ev
, opt_function
);
579 ERR("Unable to parse function probe options");
584 case LTTNG_EVENT_SYSCALL
:
585 ev
->type
= LTTNG_EVENT_SYSCALL
;
592 /* kernel loglevels not implemented */
593 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
594 } else if (opt_userspace
) { /* User-space tracer action */
595 DBG_FMT("Enabling user space event rule: pattern=`{}`, channel_name=`{}`, log_level=`{}`",
597 print_channel_name(channel_name
),
598 opt_loglevel
?: "all");
600 switch (opt_event_type
) {
601 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
603 case LTTNG_EVENT_TRACEPOINT
:
604 /* Copy name and type of the event */
605 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
606 strncpy(ev
->name
, pattern
.c_str(), LTTNG_SYMBOL_NAME_LEN
);
607 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
609 case LTTNG_EVENT_PROBE
:
610 case LTTNG_EVENT_FUNCTION
:
611 case LTTNG_EVENT_SYSCALL
:
612 case LTTNG_EVENT_USERSPACE_PROBE
:
614 ERR("Instrumentation point type not supported for the %s domain",
615 lttng_domain_type_str(dom
.type
));
616 ret
= CMD_UNSUPPORTED
;
622 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
623 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
624 ERR("Exclusion option can only be used with tracepoint events");
628 /* Free previously allocated items. */
629 lttng_dynamic_pointer_array_reset(&exclusions
);
630 ret
= create_exclusion_list_and_validate(
631 pattern
.c_str(), opt_exclude
, &exclusions
);
637 warn_on_truncated_exclusion_names(&exclusions
, &warn
);
640 ev
->loglevel_type
= (lttng_loglevel_type
) opt_loglevel_type
;
642 enum lttng_loglevel loglevel
;
643 const int name_search_ret
=
644 loglevel_name_to_value(opt_loglevel
, &loglevel
);
646 if (name_search_ret
== -1) {
647 ERR("Unknown log level `%s`", opt_loglevel
);
648 ret
= -LTTNG_ERR_INVALID
;
652 ev
->loglevel
= (int) loglevel
;
656 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
657 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
658 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
659 ERR("Instrumentation point type not supported for the %s domain",
660 lttng_domain_type_str(dom
.type
));
661 ret
= CMD_UNSUPPORTED
;
665 ev
->loglevel_type
= (lttng_loglevel_type
) opt_loglevel_type
;
670 enum lttng_loglevel_jul loglevel
;
673 loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
674 ev
->loglevel
= (int) loglevel
;
675 } else if (opt_log4j
) {
676 enum lttng_loglevel_log4j loglevel
;
678 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
,
680 ev
->loglevel
= (int) loglevel
;
683 enum lttng_loglevel_python loglevel
;
685 name_search_ret
= loglevel_python_name_to_value(
686 opt_loglevel
, &loglevel
);
687 ev
->loglevel
= (int) loglevel
;
690 if (name_search_ret
) {
691 ERR("Unknown log level `%s`", opt_loglevel
);
692 ret
= -LTTNG_ERR_INVALID
;
697 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
698 } else if (opt_log4j
) {
699 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
700 } else if (opt_python
) {
701 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
704 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
705 strncpy(ev
->name
, pattern
.c_str(), LTTNG_SYMBOL_NAME_LEN
);
706 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
712 char *exclusion_string
;
714 command_ret
= lttng_enable_event_with_exclusions(
719 lttng_dynamic_pointer_array_get_count(&exclusions
),
720 (char **) exclusions
.array
.buffer
.data
);
721 exclusion_string
= print_exclusions(&exclusions
);
722 if (!exclusion_string
) {
723 PERROR("Cannot allocate exclusion_string");
728 if (command_ret
< 0) {
729 /* Turn ret to positive value to handle the positive error code */
730 switch (-command_ret
) {
731 case LTTNG_ERR_KERN_EVENT_EXIST
:
732 WARN("Kernel event rule %s%s and attached to channel `%s` is already enabled (session `%s`)",
733 opt_enable_all
? "matching all events" :
734 (std::string("with pattern `") +
735 pattern
+ std::string("`"))
738 print_channel_name(channel_name
),
739 session_name
.c_str());
742 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
744 ERR("Can't enable an event in a new domain for a session that has already been started once (session `%s`)",
745 session_name
.c_str());
749 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
750 ERR("Failed to enable event rule with pattern `%s` and attached to channel `%s` as SDT probes guarded by semaphores are not supported (session `%s`)",
752 print_channel_name(channel_name
),
753 session_name
.c_str());
757 ERR("Failed to enable event rule %s%s and attached to channel `%s`: %s (session `%s`)",
758 opt_enable_all
? "matching all events" :
759 (std::string("with pattern `") +
760 pattern
+ std::string("`"))
763 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
?
764 print_raw_channel_name(channel_name
) :
765 print_channel_name(channel_name
),
766 lttng_strerror(command_ret
),
767 session_name
.c_str());
771 error_holder
= command_ret
;
774 case LTTNG_DOMAIN_KERNEL
:
775 case LTTNG_DOMAIN_UST
:
777 MSG("Enabled %s event rule %s%s and attached to channel `%s`",
778 lttng_domain_type_str(dom
.type
),
779 opt_enable_all
? "matching all events" :
780 (std::string("with pattern `") +
781 pattern
+ std::string("`"))
784 print_channel_name(channel_name
));
787 case LTTNG_DOMAIN_JUL
:
788 case LTTNG_DOMAIN_LOG4J
:
789 case LTTNG_DOMAIN_PYTHON
:
791 * Don't print the default channel
792 * name for agent domains.
794 MSG("Enabled %s event rule %s%s",
795 lttng_domain_type_str(dom
.type
),
796 opt_enable_all
? "matching all events" :
797 (std::string("with pattern `") +
798 pattern
+ std::string("`"))
807 free(exclusion_string
);
809 char *exclusion_string
;
814 command_ret
= lttng_enable_event_with_exclusions(
819 lttng_dynamic_pointer_array_get_count(&exclusions
),
820 (char **) exclusions
.array
.buffer
.data
);
821 exclusion_string
= print_exclusions(&exclusions
);
822 if (!exclusion_string
) {
823 PERROR("Failed allocate exclusion string");
827 if (command_ret
< 0) {
828 switch (-command_ret
) {
829 case LTTNG_ERR_FILTER_EXIST
:
830 WARN("An event rule %s%s and filter expression `%s` is already attached to channel `%s`",
831 opt_enable_all
? "matching all events" :
832 (std::string("with pattern `") +
833 pattern
+ std::string("`"))
837 print_channel_name(channel_name
));
840 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
842 ERR("Can't enable an event in a new domain for a session that has already been started once (session `%s`)",
843 session_name
.c_str());
848 ERR("Failed to enable event rule %s%s, with filter expression `%s`, and attached to channel `%s`: %s (session `%s`)",
849 opt_enable_all
? "matching all events" :
850 (std::string("with pattern `") +
851 pattern
+ std::string("`"))
855 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
?
856 print_raw_channel_name(channel_name
) :
857 print_channel_name(channel_name
),
858 lttng_strerror(command_ret
),
859 session_name
.c_str());
864 error_holder
= command_ret
;
866 MSG("Enabled %s event rule %s%s and filter expression `%s`",
867 lttng_domain_type_str(dom
.type
),
868 opt_enable_all
? "matching all events" :
869 (std::string("with pattern `") + pattern
+
877 free(exclusion_string
);
888 ret
= mi_lttng_event(writer
.get(), ev
, 1, handle
->domain
.type
);
894 /* print exclusion */
895 ret
= mi_print_exclusion(&exclusions
);
902 ret
= mi_lttng_writer_write_element_bool(
903 writer
.get(), mi_lttng_element_command_success
, success
);
909 /* Close event element */
910 ret
= mi_lttng_writer_close_element(writer
.get());
917 /* Reset warn, error and success */
924 /* Close events element */
925 ret
= mi_lttng_writer_close_element(writer
.get());
938 lttng_destroy_handle(handle
);
939 lttng_dynamic_pointer_array_reset(&exclusions
);
940 lttng_userspace_probe_location_destroy(uprobe_loc
);
942 /* Overwrite ret with error_holder if there was an actual error with
945 ret
= error_holder
? error_holder
: ret
;
947 lttng_event_destroy(ev
);
951 void _poptContextFree_deleter_func(poptContext ctx
)
953 poptFreeContext(ctx
);
958 int validate_exclusion_list(const char *pattern
,
959 const struct lttng_dynamic_pointer_array
*exclusions
)
963 /* Event name pattern must be a valid globbing pattern to allow exclusions. */
964 if (!strutils_is_star_glob_pattern(pattern
)) {
965 ERR("Event name pattern must contain wildcard characters to use exclusions");
970 * If the event name is a star-at-end only globbing pattern,
971 * then we can validate the individual exclusions. Otherwise
972 * all exclusions are passed to the session daemon.
974 if (strutils_is_star_at_the_end_only_glob_pattern(pattern
)) {
975 size_t i
, num_exclusions
;
977 num_exclusions
= lttng_dynamic_pointer_array_get_count(exclusions
);
979 for (i
= 0; i
< num_exclusions
; i
++) {
980 const char *exclusion
=
981 (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions
,
984 if (!strutils_is_star_glob_pattern(exclusion
) ||
985 strutils_is_star_at_the_end_only_glob_pattern(exclusion
)) {
986 ret
= check_exclusion_subsets(pattern
, exclusion
);
1005 * Add event to trace session
1007 int cmd_enable_events(int argc
, const char **argv
)
1009 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1010 std::string session_name
;
1011 const char *arg_event_list
= nullptr;
1012 const char *leftover
= nullptr;
1013 int event_type
= -1;
1014 event_rule_patterns patterns
;
1016 auto pc
= lttng::make_unique_wrapper
<poptContext_s
, _poptContextFree_deleter_func
>(
1017 poptGetContext(nullptr, argc
, argv
, long_options
, 0));
1018 poptReadDefaultConfig(pc
.get(), 0);
1020 /* Default event type */
1021 opt_event_type
= LTTNG_EVENT_ALL
;
1023 while ((opt
= poptGetNextOpt(pc
.get())) != -1) {
1028 case OPT_TRACEPOINT
:
1029 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1032 opt_event_type
= LTTNG_EVENT_PROBE
;
1034 case OPT_USERSPACE_PROBE
:
1035 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1038 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1041 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1047 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1048 opt_loglevel
= poptGetOptArg(pc
.get());
1050 case OPT_LOGLEVEL_ONLY
:
1051 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1052 opt_loglevel
= poptGetOptArg(pc
.get());
1054 case OPT_LIST_OPTIONS
:
1055 list_cmd_options(stdout
, long_options
);
1062 return CMD_UNDEFINED
;
1065 /* Validate event type. Multiple event type are not supported. */
1066 if (event_type
== -1) {
1067 event_type
= opt_event_type
;
1069 if (event_type
!= opt_event_type
) {
1070 ERR("Only one event type may be enabled at once");
1076 ret
= print_missing_or_multiple_domains(
1077 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
, true);
1084 writer
= mi_writer_uptr(mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
));
1086 LTTNG_THROW_ERROR(lttng::format(
1087 "Failed to create MI writer: format_code={}", lttng_opt_mi
));
1090 /* Open command element */
1091 ret
= mi_lttng_writer_command_open(writer
.get(),
1092 mi_lttng_element_command_enable_event
);
1094 LTTNG_THROW_ERROR(lttng::format(
1095 "Failed to open MI command element: command_name=`{}`",
1096 mi_lttng_element_command_enable_event
));
1099 /* Open output element */
1100 ret
= mi_lttng_writer_open_element(writer
.get(), mi_lttng_element_command_output
);
1103 lttng::format("Failed to open MI element: element_name=`{}`",
1104 mi_lttng_element_command_output
));
1108 /* Close the MI command context when leaving the function, no matter the result. */
1109 const auto close_mi_on_exit
= lttng::make_scope_exit([&success
]() noexcept
{
1110 if (!lttng_opt_mi
) {
1114 /* Close output element. */
1115 if (mi_lttng_writer_close_element(writer
.get())) {
1116 ERR_FMT("Failed to close MI output element");
1120 if (mi_lttng_writer_write_element_bool(
1121 writer
.get(), mi_lttng_element_command_success
, success
)) {
1122 ERR_FMT("Failed to write MI element: element_name=`{}`, value={}",
1123 mi_lttng_element_command_success
,
1128 /* Command element close. */
1129 if (mi_lttng_writer_command_close(writer
.get())) {
1130 ERR_FMT("Failed to close MI command element");
1135 arg_event_list
= poptGetArg(pc
.get());
1136 if (arg_event_list
== nullptr && opt_enable_all
== 0) {
1137 ERR("Missing event name pattern(s)");
1141 if (opt_enable_all
) {
1142 patterns
.emplace_back("*");
1144 std::stringstream
event_list_arg_stream(arg_event_list
);
1146 for (std::string line
; std::getline(event_list_arg_stream
, line
, ',');) {
1147 patterns
.emplace_back(std::move(line
));
1151 leftover
= poptGetArg(pc
.get());
1153 ERR("Unknown argument: %s", leftover
);
1157 if (!opt_session_name
) {
1158 const auto rc_file_session_name
=
1159 lttng::make_unique_wrapper
<char, lttng::free
>(get_session_name());
1161 if (!rc_file_session_name
) {
1165 session_name
= rc_file_session_name
.get();
1167 session_name
= opt_session_name
;
1170 command_ret
= enable_events(session_name
, patterns
);