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 "../loglevel.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 * Walk the directories in the PATH environment variable to find the target
182 * binary passed as parameter.
184 * On success, the full path of the binary is copied in binary_full_path out
185 * parameter. This buffer is allocated by the caller and must be at least
186 * LTTNG_PATH_MAX bytes long.
187 * On failure, returns -1;
189 static int walk_command_search_path(const char *binary
, char *binary_full_path
)
191 char *tentative_binary_path
= NULL
;
192 char *command_search_path
= NULL
;
193 char *curr_search_dir_end
= NULL
;
194 char *curr_search_dir
= NULL
;
195 struct stat stat_output
;
198 command_search_path
= lttng_secure_getenv("PATH");
199 if (!command_search_path
) {
205 * Duplicate the $PATH string as the char pointer returned by getenv() should
208 command_search_path
= strdup(command_search_path
);
209 if (!command_search_path
) {
215 * This char array is used to concatenate path to binary to look for
218 tentative_binary_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
219 if (!tentative_binary_path
) {
224 curr_search_dir
= command_search_path
;
227 * Split on ':'. The return value of this call points to the
228 * matching character.
230 curr_search_dir_end
= strchr(curr_search_dir
, ':');
231 if (curr_search_dir_end
!= NULL
) {
233 * Add a NULL byte to the end of the first token so it
234 * can be used as a string.
236 curr_search_dir_end
[0] = '\0';
239 /* Empty the tentative path */
240 memset(tentative_binary_path
, 0, LTTNG_PATH_MAX
* sizeof(char));
243 * Build the tentative path to the binary using the current
244 * search directory and the name of the binary.
246 ret
= snprintf(tentative_binary_path
, LTTNG_PATH_MAX
, "%s/%s",
247 curr_search_dir
, binary
);
249 goto free_binary_path
;
251 if (ret
< LTTNG_PATH_MAX
) {
253 * Use STAT(2) to see if the file exists.
255 ret
= stat(tentative_binary_path
, &stat_output
);
258 * Verify that it is a regular file or a
259 * symlink and not a special file (e.g.
262 if (S_ISREG(stat_output
.st_mode
)
263 || S_ISLNK(stat_output
.st_mode
)) {
265 * Found a match, set the out parameter
266 * and return success.
268 ret
= lttng_strncpy(binary_full_path
,
269 tentative_binary_path
,
272 ERR("Source path does not fit "
273 "in destination buffer.");
275 goto free_binary_path
;
279 /* Go to the next entry in the $PATH variable. */
280 curr_search_dir
= curr_search_dir_end
+ 1;
281 } while (curr_search_dir_end
!= NULL
);
284 free(tentative_binary_path
);
286 free(command_search_path
);
292 * Check if the symbol field passed by the user is in fact an address or an
293 * offset from a symbol. Those two instrumentation types are not supported yet.
294 * It's expected to be a common mistake because of the existing --probe option
295 * that does support these formats.
297 * Here are examples of these unsupported formats for the --userspace-probe
299 * elf:/path/to/binary:0x400430
300 * elf:/path/to/binary:4194364
301 * elf:/path/to/binary:my_symbol+0x323
302 * elf:/path/to/binary:my_symbol+43
304 static int warn_userspace_probe_syntax(const char *symbol
)
308 /* Check if the symbol field is an hex address. */
309 ret
= sscanf(symbol
, "0x%*x");
311 /* If there is a match, print a warning and return an error. */
312 ERR("Userspace probe on address not supported yet.");
313 ret
= CMD_UNSUPPORTED
;
317 /* Check if the symbol field is an decimal address. */
318 ret
= sscanf(symbol
, "%*u");
320 /* If there is a match, print a warning and return an error. */
321 ERR("Userspace probe on address not supported yet.");
322 ret
= CMD_UNSUPPORTED
;
326 /* Check if the symbol field is symbol+hex_offset. */
327 ret
= sscanf(symbol
, "%*[^+]+0x%*x");
329 /* If there is a match, print a warning and return an error. */
330 ERR("Userspace probe on symbol+offset not supported yet.");
331 ret
= CMD_UNSUPPORTED
;
335 /* Check if the symbol field is symbol+decimal_offset. */
336 ret
= sscanf(symbol
, "%*[^+]+%*u");
338 /* If there is a match, print a warning and return an error. */
339 ERR("Userspace probe on symbol+offset not supported yet.");
340 ret
= CMD_UNSUPPORTED
;
351 * Parse userspace probe options
352 * Set the userspace probe fields in the lttng_event struct and set the
353 * target_path to the path to the binary.
355 static int parse_userspace_probe_opts(struct lttng_event
*ev
, char *opt
)
357 int ret
= CMD_SUCCESS
;
360 char *target_path
= NULL
;
361 char *unescaped_target_path
= NULL
;
362 char *real_target_path
= NULL
;
363 char *symbol_name
= NULL
, *probe_name
= NULL
, *provider_name
= NULL
;
364 struct lttng_userspace_probe_location
*probe_location
= NULL
;
365 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
374 case LTTNG_EVENT_USERSPACE_PROBE
:
381 * userspace probe fields are separated by ':'.
383 tokens
= strutils_split(opt
, ':', 1);
384 num_token
= strutils_array_of_strings_len(tokens
);
387 * Early sanity check that the number of parameter is between 2 and 4
390 * std:PATH:PROVIDER_NAME:PROBE_NAME
391 * PATH:SYMBOL (same behavior as ELF)
393 if (num_token
< 2 || num_token
> 4) {
399 * Looking up the first parameter will tell the technique to use to
400 * interpret the userspace probe/function description.
404 /* When the probe type is omitted we assume ELF for now. */
406 if (num_token
== 3 && strcmp(tokens
[0], "elf") == 0) {
407 target_path
= tokens
[1];
408 symbol_name
= tokens
[2];
409 } else if (num_token
== 2) {
410 target_path
= tokens
[0];
411 symbol_name
= tokens
[1];
417 lttng_userspace_probe_location_lookup_method_function_elf_create();
418 if (!lookup_method
) {
419 WARN("Failed to create ELF lookup method");
425 if (strcmp(tokens
[0], "sdt") == 0) {
426 target_path
= tokens
[1];
427 provider_name
= tokens
[2];
428 probe_name
= tokens
[3];
434 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
435 if (!lookup_method
) {
436 WARN("Failed to create SDT lookup method");
446 /* strutils_unescape_string allocates a new char *. */
447 unescaped_target_path
= strutils_unescape_string(target_path
, 0);
448 if (!unescaped_target_path
) {
450 goto end_destroy_lookup_method
;
454 * If there is not forward slash in the path. Walk the $PATH else
457 if (strchr(unescaped_target_path
, '/') == NULL
) {
458 /* Walk the $PATH variable to find the targeted binary. */
459 real_target_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
460 if (!real_target_path
) {
461 PERROR("Error allocating path buffer");
463 goto end_destroy_lookup_method
;
465 ret
= walk_command_search_path(unescaped_target_path
, real_target_path
);
467 ERR("Binary not found.");
469 goto end_destroy_lookup_method
;
473 * Expand references to `/./` and `/../`. This function does not check
474 * if the file exists. This call returns an allocated buffer on
477 real_target_path
= utils_expand_path_keep_symlink(unescaped_target_path
);
478 if (!real_target_path
) {
479 ERR("Error expanding the path to binary.");
481 goto end_destroy_lookup_method
;
485 * Check if the file exists using access(2), If it does not,
488 ret
= access(real_target_path
, F_OK
);
490 ERR("Cannot find binary at path: %s.", real_target_path
);
492 goto end_destroy_lookup_method
;
496 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method
)) {
497 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
499 * Check for common mistakes in userspace probe description syntax.
501 ret
= warn_userspace_probe_syntax(symbol_name
);
503 goto end_destroy_lookup_method
;
506 probe_location
= lttng_userspace_probe_location_function_create(
507 real_target_path
, symbol_name
, lookup_method
);
508 if (!probe_location
) {
509 WARN("Failed to create function probe location");
511 goto end_destroy_lookup_method
;
514 /* Ownership transferred to probe_location. */
515 lookup_method
= NULL
;
517 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
519 WARN("Failed to set probe location on event");
521 goto end_destroy_location
;
524 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
525 probe_location
= lttng_userspace_probe_location_tracepoint_create(
526 real_target_path
, provider_name
, probe_name
, lookup_method
);
527 if (!probe_location
) {
528 WARN("Failed to create function probe location");
530 goto end_destroy_lookup_method
;
533 /* Ownership transferred to probe_location. */
534 lookup_method
= NULL
;
536 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
538 WARN("Failed to set probe location on event");
540 goto end_destroy_location
;
545 goto end_destroy_lookup_method
;
548 /* Successful parsing, now clean up everything and return. */
551 end_destroy_location
:
552 lttng_userspace_probe_location_destroy(probe_location
);
553 end_destroy_lookup_method
:
554 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
556 strutils_free_null_terminated_array_of_strings(tokens
);
558 * Freeing both char * here makes the error handling simplier. free()
559 * performs not action if the pointer is NULL.
561 free(real_target_path
);
562 free(unescaped_target_path
);
568 const char *print_channel_name(const char *name
)
570 return name
? : DEFAULT_CHANNEL_NAME
;
574 const char *print_raw_channel_name(const char *name
)
576 return name
? : "<default>";
580 * Mi print exlcusion list
583 int mi_print_exclusion(char **names
)
586 int count
= names
? strutils_array_of_strings_len(names
) : 0;
594 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
599 for (i
= 0; i
< count
; i
++) {
600 ret
= mi_lttng_writer_write_element_string(writer
,
601 config_element_exclusion
, names
[i
]);
607 /* Close exclusions element */
608 ret
= mi_lttng_writer_close_element(writer
);
615 * Return allocated string for pretty-printing exclusion names.
618 char *print_exclusions(char **names
)
622 const char preamble
[] = " excluding ";
624 int count
= names
? strutils_array_of_strings_len(names
) : 0;
630 /* calculate total required length */
631 for (i
= 0; i
< count
; i
++) {
632 length
+= strlen(names
[i
]) + 4;
635 length
+= sizeof(preamble
);
636 ret
= zmalloc(length
);
640 strncpy(ret
, preamble
, length
);
641 for (i
= 0; i
< count
; i
++) {
643 strcat(ret
, names
[i
]);
645 if (i
!= count
- 1) {
654 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
658 const char *e
= event_name
;
659 const char *x
= exclusion
;
661 /* Scan both the excluder and the event letter by letter */
675 /* Event is a subset of the excluder */
676 ERR("Event %s: %s excludes all events from %s",
677 event_name
, exclusion
, event_name
);
683 * Reached the end of the event name before the
684 * end of the exclusion: this is valid.
706 WARN("Event %s: %s does not exclude any events from %s",
707 event_name
, exclusion
, event_name
);
714 int create_exclusion_list_and_validate(const char *event_name
,
715 const char *exclusions_arg
,
716 char ***exclusion_list
)
719 char **exclusions
= NULL
;
721 /* Event name must be a valid globbing pattern to allow exclusions. */
722 if (!strutils_is_star_glob_pattern(event_name
)) {
723 ERR("Event %s: Exclusions can only be used with a globbing pattern",
728 /* Split exclusions. */
729 exclusions
= strutils_split(exclusions_arg
, ',', true);
735 * If the event name is a star-at-end only globbing pattern,
736 * then we can validate the individual exclusions. Otherwise
737 * all exclusions are passed to the session daemon.
739 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
740 char * const *exclusion
;
742 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
743 if (!strutils_is_star_glob_pattern(*exclusion
) ||
744 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
745 ret
= check_exclusion_subsets(event_name
, *exclusion
);
753 *exclusion_list
= exclusions
;
759 strutils_free_null_terminated_array_of_strings(exclusions
);
765 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
768 char * const *exclusion
;
770 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
771 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
772 WARN("Event exclusion \"%s\" will be truncated",
780 * Enabling event using the lttng API.
781 * Note: in case of error only the last error code will be return.
783 static int enable_events(char *session_name
)
785 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
786 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
787 char *event_name
, *channel_name
= NULL
;
788 struct lttng_event
*ev
;
789 struct lttng_domain dom
;
790 char **exclusion_list
= NULL
;
792 memset(&dom
, 0, sizeof(dom
));
794 ev
= lttng_event_create();
802 WARN("Kernel loglevels are not supported.");
806 /* Create lttng domain */
808 dom
.type
= LTTNG_DOMAIN_KERNEL
;
809 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
810 } else if (opt_userspace
) {
811 dom
.type
= LTTNG_DOMAIN_UST
;
813 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
814 } else if (opt_jul
) {
815 dom
.type
= LTTNG_DOMAIN_JUL
;
817 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
818 } else if (opt_log4j
) {
819 dom
.type
= LTTNG_DOMAIN_LOG4J
;
821 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
822 } else if (opt_python
) {
823 dom
.type
= LTTNG_DOMAIN_PYTHON
;
825 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
827 /* Checked by the caller. */
833 case LTTNG_DOMAIN_KERNEL
:
834 case LTTNG_DOMAIN_JUL
:
835 case LTTNG_DOMAIN_LOG4J
:
836 case LTTNG_DOMAIN_PYTHON
:
837 ERR("Event name exclusions are not yet implemented for %s events",
838 get_domain_str(dom
.type
));
841 case LTTNG_DOMAIN_UST
:
842 /* Exclusions supported */
850 * Adding a filter to a probe, function or userspace-probe would be
851 * denied by the kernel tracer as it's not supported at the moment. We
852 * do an early check here to warn the user.
854 if (opt_filter
&& opt_kernel
) {
855 switch (opt_event_type
) {
856 case LTTNG_EVENT_ALL
:
857 case LTTNG_EVENT_TRACEPOINT
:
858 case LTTNG_EVENT_SYSCALL
:
860 case LTTNG_EVENT_PROBE
:
861 case LTTNG_EVENT_USERSPACE_PROBE
:
862 case LTTNG_EVENT_FUNCTION
:
863 ERR("Filter expressions are not supported for %s events",
864 get_event_type_str(opt_event_type
));
873 channel_name
= opt_channel_name
;
875 handle
= lttng_create_handle(session_name
, &dom
);
876 if (handle
== NULL
) {
883 /* Open a events element */
884 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
891 if (opt_enable_all
) {
892 /* Default setup for enable all */
894 ev
->type
= opt_event_type
;
895 strcpy(ev
->name
, "*");
896 /* kernel loglevels not implemented */
897 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
899 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
900 strcpy(ev
->name
, "*");
901 ev
->loglevel_type
= opt_loglevel_type
;
905 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
908 enum lttng_loglevel loglevel
;
910 name_search_ret
= loglevel_name_to_value(opt_loglevel
, &loglevel
);
911 ev
->loglevel
= (int) loglevel
;
912 } else if (opt_jul
) {
913 enum lttng_loglevel_jul loglevel
;
915 name_search_ret
= loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
916 ev
->loglevel
= (int) loglevel
;
917 } else if (opt_log4j
) {
918 enum lttng_loglevel_log4j loglevel
;
920 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
, &loglevel
);
921 ev
->loglevel
= (int) loglevel
;
924 enum lttng_loglevel_python loglevel
;
926 name_search_ret
= loglevel_python_name_to_value(opt_loglevel
, &loglevel
);
927 ev
->loglevel
= (int) loglevel
;
930 if (name_search_ret
== -1) {
931 ERR("Unknown loglevel %s", opt_loglevel
);
932 ret
= -LTTNG_ERR_INVALID
;
936 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
939 } else if (opt_jul
) {
940 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
941 } else if (opt_log4j
) {
942 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
943 } else if (opt_python
) {
944 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
950 ret
= create_exclusion_list_and_validate("*",
951 opt_exclude
, &exclusion_list
);
958 warn_on_truncated_exclusion_names(exclusion_list
,
962 ret
= lttng_enable_event_with_exclusions(handle
,
965 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
969 case LTTNG_ERR_KERN_EVENT_EXIST
:
970 WARN("Kernel events already enabled (channel %s, session %s)",
971 print_channel_name(channel_name
), session_name
);
974 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
976 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
977 ERR("Events: %s (channel %s, session %s)",
979 print_channel_name(channel_name
),
985 ERR("Events: %s (channel %s, session %s)",
987 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
988 ? print_raw_channel_name(channel_name
)
989 : print_channel_name(channel_name
),
997 switch (opt_event_type
) {
998 case LTTNG_EVENT_TRACEPOINT
:
999 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1000 char *exclusion_string
= print_exclusions(exclusion_list
);
1002 if (!exclusion_string
) {
1003 PERROR("Cannot allocate exclusion_string");
1007 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1008 get_domain_str(dom
.type
),
1010 print_channel_name(channel_name
),
1012 free(exclusion_string
);
1014 char *exclusion_string
= print_exclusions(exclusion_list
);
1016 if (!exclusion_string
) {
1017 PERROR("Cannot allocate exclusion_string");
1021 MSG("All %s tracepoints%s are enabled in channel %s",
1022 get_domain_str(dom
.type
),
1024 print_channel_name(channel_name
));
1025 free(exclusion_string
);
1028 case LTTNG_EVENT_SYSCALL
:
1030 MSG("All %s system calls are enabled in channel %s",
1031 get_domain_str(dom
.type
),
1032 print_channel_name(channel_name
));
1035 case LTTNG_EVENT_ALL
:
1036 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1037 char *exclusion_string
= print_exclusions(exclusion_list
);
1039 if (!exclusion_string
) {
1040 PERROR("Cannot allocate exclusion_string");
1044 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1045 get_domain_str(dom
.type
),
1047 print_channel_name(channel_name
),
1049 free(exclusion_string
);
1051 char *exclusion_string
= print_exclusions(exclusion_list
);
1053 if (!exclusion_string
) {
1054 PERROR("Cannot allocate exclusion_string");
1058 MSG("All %s events%s are enabled in channel %s",
1059 get_domain_str(dom
.type
),
1061 print_channel_name(channel_name
));
1062 free(exclusion_string
);
1067 * We should not be here since lttng_enable_event should have
1068 * failed on the event type.
1075 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1077 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1079 if (command_ret
< 0) {
1080 switch (-command_ret
) {
1081 case LTTNG_ERR_FILTER_EXIST
:
1082 WARN("Filter on all events is already enabled"
1083 " (channel %s, session %s)",
1084 print_channel_name(channel_name
), session_name
);
1087 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1089 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1090 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1092 print_channel_name(channel_name
),
1093 session_name
, opt_filter
);
1098 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1099 lttng_strerror(command_ret
),
1100 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1101 ? print_raw_channel_name(channel_name
)
1102 : print_channel_name(channel_name
),
1103 session_name
, opt_filter
);
1107 error_holder
= command_ret
;
1110 MSG("Filter '%s' successfully set", opt_filter
);
1115 /* The wildcard * is used for kernel and ust domain to
1116 * represent ALL. We copy * in event name to force the wildcard use
1119 * Note: this is strictly for semantic and printing while in
1120 * machine interface mode.
1122 strcpy(ev
->name
, "*");
1124 /* If we reach here the events are enabled */
1125 if (!error
&& !warn
) {
1131 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1137 /* print exclusion */
1138 ret
= mi_print_exclusion(exclusion_list
);
1145 ret
= mi_lttng_writer_write_element_bool(writer
,
1146 mi_lttng_element_command_success
, success
);
1152 /* Close event element */
1153 ret
= mi_lttng_writer_close_element(writer
);
1163 /* Strip event list */
1164 event_name
= strtok(opt_event_list
, ",");
1165 while (event_name
!= NULL
) {
1166 /* Copy name and type of the event */
1167 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1168 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1169 ev
->type
= opt_event_type
;
1171 /* Kernel tracer action */
1173 DBG("Enabling kernel event %s for channel %s",
1175 print_channel_name(channel_name
));
1177 switch (opt_event_type
) {
1178 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1179 /* If event name differs from *, select tracepoint. */
1180 if (strcmp(ev
->name
, "*")) {
1181 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1184 case LTTNG_EVENT_TRACEPOINT
:
1186 case LTTNG_EVENT_PROBE
:
1187 ret
= parse_probe_opts(ev
, opt_probe
);
1189 ERR("Unable to parse probe options");
1194 case LTTNG_EVENT_USERSPACE_PROBE
:
1195 ret
= parse_userspace_probe_opts(ev
, opt_userspace_probe
);
1198 case CMD_UNSUPPORTED
:
1200 * Error message describing
1201 * what is not supported was
1202 * printed in the function.
1207 ERR("Unable to parse userspace probe options");
1213 case LTTNG_EVENT_FUNCTION
:
1214 ret
= parse_probe_opts(ev
, opt_function
);
1216 ERR("Unable to parse function probe options");
1221 case LTTNG_EVENT_SYSCALL
:
1222 ev
->type
= LTTNG_EVENT_SYSCALL
;
1225 ret
= CMD_UNDEFINED
;
1229 /* kernel loglevels not implemented */
1230 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1231 } else if (opt_userspace
) { /* User-space tracer action */
1232 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1233 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1235 switch (opt_event_type
) {
1236 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1238 case LTTNG_EVENT_TRACEPOINT
:
1239 /* Copy name and type of the event */
1240 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1241 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1242 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1244 case LTTNG_EVENT_PROBE
:
1245 case LTTNG_EVENT_FUNCTION
:
1246 case LTTNG_EVENT_SYSCALL
:
1247 case LTTNG_EVENT_USERSPACE_PROBE
:
1249 ERR("Event type not available for user-space tracing");
1250 ret
= CMD_UNSUPPORTED
;
1256 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1257 ERR("Exclusion option can only be used with tracepoint events");
1261 /* Free previously allocated items */
1262 strutils_free_null_terminated_array_of_strings(
1264 exclusion_list
= NULL
;
1265 ret
= create_exclusion_list_and_validate(
1266 event_name
, opt_exclude
,
1273 warn_on_truncated_exclusion_names(
1274 exclusion_list
, &warn
);
1277 ev
->loglevel_type
= opt_loglevel_type
;
1279 enum lttng_loglevel loglevel
;
1280 const int name_search_ret
= loglevel_name_to_value(opt_loglevel
, &loglevel
);
1282 if (name_search_ret
== -1) {
1283 ERR("Unknown loglevel %s", opt_loglevel
);
1284 ret
= -LTTNG_ERR_INVALID
;
1288 ev
->loglevel
= (int) loglevel
;
1292 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1293 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1294 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1295 ERR("Event type not supported for domain.");
1296 ret
= CMD_UNSUPPORTED
;
1300 ev
->loglevel_type
= opt_loglevel_type
;
1302 int name_search_ret
;
1305 enum lttng_loglevel_jul loglevel
;
1307 name_search_ret
= loglevel_jul_name_to_value(opt_loglevel
, &loglevel
);
1308 ev
->loglevel
= (int) loglevel
;
1309 } else if (opt_log4j
) {
1310 enum lttng_loglevel_log4j loglevel
;
1312 name_search_ret
= loglevel_log4j_name_to_value(opt_loglevel
, &loglevel
);
1313 ev
->loglevel
= (int) loglevel
;
1315 /* python domain. */
1316 enum lttng_loglevel_python loglevel
;
1318 name_search_ret
= loglevel_python_name_to_value(opt_loglevel
, &loglevel
);
1319 ev
->loglevel
= (int) loglevel
;
1322 if (name_search_ret
) {
1323 ERR("Unknown loglevel %s", opt_loglevel
);
1324 ret
= -LTTNG_ERR_INVALID
;
1329 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1330 } else if (opt_log4j
) {
1331 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1332 } else if (opt_python
) {
1333 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1336 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1337 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1338 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1344 char *exclusion_string
;
1346 command_ret
= lttng_enable_event_with_exclusions(handle
,
1349 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1351 exclusion_string
= print_exclusions(exclusion_list
);
1352 if (!exclusion_string
) {
1353 PERROR("Cannot allocate exclusion_string");
1357 if (command_ret
< 0) {
1358 /* Turn ret to positive value to handle the positive error code */
1359 switch (-command_ret
) {
1360 case LTTNG_ERR_KERN_EVENT_EXIST
:
1361 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1364 print_channel_name(channel_name
), session_name
);
1367 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1369 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1370 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1373 print_channel_name(channel_name
),
1378 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1379 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1380 event_name
, print_channel_name(channel_name
),
1385 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1387 lttng_strerror(command_ret
),
1388 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1389 ? print_raw_channel_name(channel_name
)
1390 : print_channel_name(channel_name
),
1395 error_holder
= command_ret
;
1398 case LTTNG_DOMAIN_KERNEL
:
1399 case LTTNG_DOMAIN_UST
:
1400 MSG("%s event %s%s created in channel %s",
1401 get_domain_str(dom
.type
),
1404 print_channel_name(channel_name
));
1406 case LTTNG_DOMAIN_JUL
:
1407 case LTTNG_DOMAIN_LOG4J
:
1408 case LTTNG_DOMAIN_PYTHON
:
1410 * Don't print the default channel
1411 * name for agent domains.
1413 MSG("%s event %s%s enabled",
1414 get_domain_str(dom
.type
),
1422 free(exclusion_string
);
1426 char *exclusion_string
;
1428 /* Filter present */
1431 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1433 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1435 exclusion_string
= print_exclusions(exclusion_list
);
1436 if (!exclusion_string
) {
1437 PERROR("Cannot allocate exclusion_string");
1441 if (command_ret
< 0) {
1442 switch (-command_ret
) {
1443 case LTTNG_ERR_FILTER_EXIST
:
1444 WARN("Filter on event %s%s is already enabled"
1445 " (channel %s, session %s)",
1448 print_channel_name(channel_name
), session_name
);
1451 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1453 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1454 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1457 print_channel_name(channel_name
),
1458 session_name
, opt_filter
);
1463 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1465 lttng_strerror(command_ret
),
1466 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1467 ? print_raw_channel_name(channel_name
)
1468 : print_channel_name(channel_name
),
1469 session_name
, opt_filter
);
1473 error_holder
= command_ret
;
1476 MSG("Event %s%s: Filter '%s' successfully set",
1477 event_name
, exclusion_string
,
1480 free(exclusion_string
);
1491 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1497 /* print exclusion */
1498 ret
= mi_print_exclusion(exclusion_list
);
1505 ret
= mi_lttng_writer_write_element_bool(writer
,
1506 mi_lttng_element_command_success
, success
);
1512 /* Close event element */
1513 ret
= mi_lttng_writer_close_element(writer
);
1521 event_name
= strtok(NULL
, ",");
1522 /* Reset warn, error and success */
1529 /* Close events element */
1530 ret
= mi_lttng_writer_close_element(writer
);
1543 lttng_destroy_handle(handle
);
1544 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1546 /* Overwrite ret with error_holder if there was an actual error with
1547 * enabling an event.
1549 ret
= error_holder
? error_holder
: ret
;
1551 lttng_event_destroy(ev
);
1556 * Add event to trace session
1558 int cmd_enable_events(int argc
, const char **argv
)
1560 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1561 static poptContext pc
;
1562 char *session_name
= NULL
;
1563 const char *leftover
= NULL
;
1564 int event_type
= -1;
1566 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1567 poptReadDefaultConfig(pc
, 0);
1569 /* Default event type */
1570 opt_event_type
= LTTNG_EVENT_ALL
;
1572 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1577 case OPT_TRACEPOINT
:
1578 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1581 opt_event_type
= LTTNG_EVENT_PROBE
;
1583 case OPT_USERSPACE_PROBE
:
1584 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1587 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1590 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1596 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1597 opt_loglevel
= poptGetOptArg(pc
);
1599 case OPT_LOGLEVEL_ONLY
:
1600 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1601 opt_loglevel
= poptGetOptArg(pc
);
1603 case OPT_LIST_OPTIONS
:
1604 list_cmd_options(stdout
, long_options
);
1611 ret
= CMD_UNDEFINED
;
1615 /* Validate event type. Multiple event type are not supported. */
1616 if (event_type
== -1) {
1617 event_type
= opt_event_type
;
1619 if (event_type
!= opt_event_type
) {
1620 ERR("Multiple event type not supported.");
1627 ret
= print_missing_or_multiple_domains(
1628 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1638 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1640 ret
= -LTTNG_ERR_NOMEM
;
1644 /* Open command element */
1645 ret
= mi_lttng_writer_command_open(writer
,
1646 mi_lttng_element_command_enable_event
);
1652 /* Open output element */
1653 ret
= mi_lttng_writer_open_element(writer
,
1654 mi_lttng_element_command_output
);
1661 opt_event_list
= (char*) poptGetArg(pc
);
1662 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1663 ERR("Missing event name(s).\n");
1668 leftover
= poptGetArg(pc
);
1670 ERR("Unknown argument: %s", leftover
);
1675 if (!opt_session_name
) {
1676 session_name
= get_session_name();
1677 if (session_name
== NULL
) {
1678 command_ret
= CMD_ERROR
;
1683 session_name
= opt_session_name
;
1686 command_ret
= enable_events(session_name
);
1695 /* Close output element */
1696 ret
= mi_lttng_writer_close_element(writer
);
1702 ret
= mi_lttng_writer_write_element_bool(writer
,
1703 mi_lttng_element_command_success
, success
);
1709 /* Command element close */
1710 ret
= mi_lttng_writer_command_close(writer
);
1719 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1720 /* Preserve original error code */
1721 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1724 if (opt_session_name
== NULL
) {
1728 /* Overwrite ret if an error occurred in enable_events */
1729 ret
= command_ret
? command_ret
: ret
;
1731 poptFreeContext(pc
);