2 * Copyright (C) 2011 EfficiOS Inc.
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>
24 #include <common/path.h>
26 #include <lttng/constant.h>
28 #include <common/mi-lttng.h>
30 #include "../command.h"
32 #if (LTTNG_SYMBOL_NAME_LEN == 256)
33 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
36 static char *opt_event_list
;
37 static int opt_event_type
;
38 static const char *opt_loglevel
;
39 static int opt_loglevel_type
;
40 static int opt_kernel
;
41 static char *opt_session_name
;
42 static int opt_userspace
;
45 static int opt_python
;
46 static int opt_enable_all
;
47 static char *opt_probe
;
48 static char *opt_userspace_probe
;
49 static char *opt_function
;
50 static char *opt_channel_name
;
51 static char *opt_filter
;
52 static char *opt_exclude
;
54 #ifdef LTTNG_EMBED_HELP
55 static const char help_msg
[] =
56 #include <lttng-enable-event.1.h>
75 static struct lttng_handle
*handle
;
76 static struct mi_writer
*writer
;
78 static struct poptOption long_options
[] = {
79 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
80 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
81 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
82 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
83 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
84 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
85 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
86 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
87 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
88 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
89 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
90 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
91 {"userspace-probe",0, POPT_ARG_STRING
, &opt_userspace_probe
, OPT_USERSPACE_PROBE
, 0, 0},
92 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
93 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
94 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
95 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
96 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
97 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
98 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
103 * Parse probe options.
105 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
107 int ret
= CMD_SUCCESS
;
110 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
111 char name
[LTTNG_SYMBOL_NAME_LEN
];
118 /* Check for symbol+offset */
119 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
120 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
122 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
123 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
124 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
125 if (*s_hex
== '\0') {
126 ERR("Invalid probe offset %s", s_hex
);
130 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
131 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
132 ev
->attr
.probe
.addr
= 0;
136 /* Check for symbol */
137 if (isalpha(name
[0]) || name
[0] == '_') {
138 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
141 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
142 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
143 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
144 ev
->attr
.probe
.offset
= 0;
145 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
146 ev
->attr
.probe
.addr
= 0;
151 /* Check for address */
152 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
155 * Return an error if the first character of the tentative
156 * address is NULL or not a digit. It can be "0" if the address
157 * is in hexadecimal and can be 1 to 9 if it's in decimal.
159 if (*s_hex
== '\0' || !isdigit(*s_hex
)) {
160 ERR("Invalid probe description %s", s_hex
);
164 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
165 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
166 ev
->attr
.probe
.offset
= 0;
167 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
179 * Walk the directories in the PATH environment variable to find the target
180 * binary passed as parameter.
182 * On success, the full path of the binary is copied in binary_full_path out
183 * parameter. This buffer is allocated by the caller and must be at least
184 * LTTNG_PATH_MAX bytes long.
185 * On failure, returns -1;
187 static int walk_command_search_path(const char *binary
, char *binary_full_path
)
189 char *tentative_binary_path
= NULL
;
190 char *command_search_path
= NULL
;
191 char *curr_search_dir_end
= NULL
;
192 char *curr_search_dir
= NULL
;
193 struct stat stat_output
;
196 command_search_path
= lttng_secure_getenv("PATH");
197 if (!command_search_path
) {
203 * Duplicate the $PATH string as the char pointer returned by getenv() should
206 command_search_path
= strdup(command_search_path
);
207 if (!command_search_path
) {
213 * This char array is used to concatenate path to binary to look for
216 tentative_binary_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
217 if (!tentative_binary_path
) {
222 curr_search_dir
= command_search_path
;
225 * Split on ':'. The return value of this call points to the
226 * matching character.
228 curr_search_dir_end
= strchr(curr_search_dir
, ':');
229 if (curr_search_dir_end
!= NULL
) {
231 * Add a NULL byte to the end of the first token so it
232 * can be used as a string.
234 curr_search_dir_end
[0] = '\0';
237 /* Empty the tentative path */
238 memset(tentative_binary_path
, 0, LTTNG_PATH_MAX
* sizeof(char));
241 * Build the tentative path to the binary using the current
242 * search directory and the name of the binary.
244 ret
= snprintf(tentative_binary_path
, LTTNG_PATH_MAX
, "%s/%s",
245 curr_search_dir
, binary
);
247 goto free_binary_path
;
249 if (ret
< LTTNG_PATH_MAX
) {
251 * Use STAT(2) to see if the file exists.
253 ret
= stat(tentative_binary_path
, &stat_output
);
256 * Verify that it is a regular file or a
257 * symlink and not a special file (e.g.
260 if (S_ISREG(stat_output
.st_mode
)
261 || S_ISLNK(stat_output
.st_mode
)) {
263 * Found a match, set the out parameter
264 * and return success.
266 ret
= lttng_strncpy(binary_full_path
,
267 tentative_binary_path
,
270 ERR("Source path does not fit "
271 "in destination buffer.");
273 goto free_binary_path
;
277 /* Go to the next entry in the $PATH variable. */
278 curr_search_dir
= curr_search_dir_end
+ 1;
279 } while (curr_search_dir_end
!= NULL
);
282 free(tentative_binary_path
);
284 free(command_search_path
);
290 * Check if the symbol field passed by the user is in fact an address or an
291 * offset from a symbol. Those two instrumentation types are not supported yet.
292 * It's expected to be a common mistake because of the existing --probe option
293 * that does support these formats.
295 * Here are examples of these unsupported formats for the --userspace-probe
297 * elf:/path/to/binary:0x400430
298 * elf:/path/to/binary:4194364
299 * elf:/path/to/binary:my_symbol+0x323
300 * elf:/path/to/binary:my_symbol+43
302 static int warn_userspace_probe_syntax(const char *symbol
)
306 /* Check if the symbol field is an hex address. */
307 ret
= sscanf(symbol
, "0x%*x");
309 /* If there is a match, print a warning and return an error. */
310 ERR("Userspace probe on address not supported yet.");
311 ret
= CMD_UNSUPPORTED
;
315 /* Check if the symbol field is an decimal address. */
316 ret
= sscanf(symbol
, "%*u");
318 /* If there is a match, print a warning and return an error. */
319 ERR("Userspace probe on address not supported yet.");
320 ret
= CMD_UNSUPPORTED
;
324 /* Check if the symbol field is symbol+hex_offset. */
325 ret
= sscanf(symbol
, "%*[^+]+0x%*x");
327 /* If there is a match, print a warning and return an error. */
328 ERR("Userspace probe on symbol+offset not supported yet.");
329 ret
= CMD_UNSUPPORTED
;
333 /* Check if the symbol field is symbol+decimal_offset. */
334 ret
= sscanf(symbol
, "%*[^+]+%*u");
336 /* If there is a match, print a warning and return an error. */
337 ERR("Userspace probe on symbol+offset not supported yet.");
338 ret
= CMD_UNSUPPORTED
;
349 * Parse userspace probe options
350 * Set the userspace probe fields in the lttng_event struct and set the
351 * target_path to the path to the binary.
353 static int parse_userspace_probe_opts(struct lttng_event
*ev
, char *opt
)
355 int ret
= CMD_SUCCESS
;
358 char *target_path
= NULL
;
359 char *unescaped_target_path
= NULL
;
360 char *real_target_path
= NULL
;
361 char *symbol_name
= NULL
, *probe_name
= NULL
, *provider_name
= NULL
;
362 struct lttng_userspace_probe_location
*probe_location
= NULL
;
363 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
372 case LTTNG_EVENT_USERSPACE_PROBE
:
379 * userspace probe fields are separated by ':'.
381 tokens
= strutils_split(opt
, ':', 1);
382 num_token
= strutils_array_of_strings_len(tokens
);
385 * Early sanity check that the number of parameter is between 2 and 4
388 * std:PATH:PROVIDER_NAME:PROBE_NAME
389 * PATH:SYMBOL (same behavior as ELF)
391 if (num_token
< 2 || num_token
> 4) {
397 * Looking up the first parameter will tell the technique to use to
398 * interpret the userspace probe/function description.
402 /* When the probe type is omitted we assume ELF for now. */
404 if (num_token
== 3 && strcmp(tokens
[0], "elf") == 0) {
405 target_path
= tokens
[1];
406 symbol_name
= tokens
[2];
407 } else if (num_token
== 2) {
408 target_path
= tokens
[0];
409 symbol_name
= tokens
[1];
415 lttng_userspace_probe_location_lookup_method_function_elf_create();
416 if (!lookup_method
) {
417 WARN("Failed to create ELF lookup method");
423 if (strcmp(tokens
[0], "sdt") == 0) {
424 target_path
= tokens
[1];
425 provider_name
= tokens
[2];
426 probe_name
= tokens
[3];
432 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
433 if (!lookup_method
) {
434 WARN("Failed to create SDT lookup method");
444 /* strutils_unescape_string allocates a new char *. */
445 unescaped_target_path
= strutils_unescape_string(target_path
, 0);
446 if (!unescaped_target_path
) {
448 goto end_destroy_lookup_method
;
452 * If there is not forward slash in the path. Walk the $PATH else
455 if (strchr(unescaped_target_path
, '/') == NULL
) {
456 /* Walk the $PATH variable to find the targeted binary. */
457 real_target_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
458 if (!real_target_path
) {
459 PERROR("Error allocating path buffer");
461 goto end_destroy_lookup_method
;
463 ret
= walk_command_search_path(unescaped_target_path
, real_target_path
);
465 ERR("Binary not found.");
467 goto end_destroy_lookup_method
;
471 * Expand references to `/./` and `/../`. This function does not check
472 * if the file exists. This call returns an allocated buffer on
475 real_target_path
= utils_expand_path_keep_symlink(unescaped_target_path
);
476 if (!real_target_path
) {
477 ERR("Error expanding the path to binary.");
479 goto end_destroy_lookup_method
;
483 * Check if the file exists using access(2), If it does not,
486 ret
= access(real_target_path
, F_OK
);
488 ERR("Cannot find binary at path: %s.", real_target_path
);
490 goto end_destroy_lookup_method
;
494 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method
)) {
495 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
497 * Check for common mistakes in userspace probe description syntax.
499 ret
= warn_userspace_probe_syntax(symbol_name
);
501 goto end_destroy_lookup_method
;
504 probe_location
= lttng_userspace_probe_location_function_create(
505 real_target_path
, symbol_name
, lookup_method
);
506 if (!probe_location
) {
507 WARN("Failed to create function probe location");
509 goto end_destroy_lookup_method
;
512 /* Ownership transferred to probe_location. */
513 lookup_method
= NULL
;
515 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
517 WARN("Failed to set probe location on event");
519 goto end_destroy_location
;
522 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
523 probe_location
= lttng_userspace_probe_location_tracepoint_create(
524 real_target_path
, provider_name
, probe_name
, lookup_method
);
525 if (!probe_location
) {
526 WARN("Failed to create function probe location");
528 goto end_destroy_lookup_method
;
531 /* Ownership transferred to probe_location. */
532 lookup_method
= NULL
;
534 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
536 WARN("Failed to set probe location on event");
538 goto end_destroy_location
;
543 goto end_destroy_lookup_method
;
546 /* Successful parsing, now clean up everything and return. */
549 end_destroy_location
:
550 lttng_userspace_probe_location_destroy(probe_location
);
551 end_destroy_lookup_method
:
552 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
554 strutils_free_null_terminated_array_of_strings(tokens
);
556 * Freeing both char * here makes the error handling simplier. free()
557 * performs not action if the pointer is NULL.
559 free(real_target_path
);
560 free(unescaped_target_path
);
566 * Maps LOG4j loglevel from string to value
568 static int loglevel_log4j_str_to_value(const char *inputstr
)
571 char str
[LTTNG_SYMBOL_NAME_LEN
];
573 if (!inputstr
|| strlen(inputstr
) == 0) {
578 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
579 * added at the end of the loop so a the upper bound we avoid the overflow.
581 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
582 str
[i
] = toupper(inputstr
[i
]);
587 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
588 return LTTNG_LOGLEVEL_LOG4J_OFF
;
589 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
590 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
591 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
592 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
593 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
594 return LTTNG_LOGLEVEL_LOG4J_WARN
;
595 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
596 return LTTNG_LOGLEVEL_LOG4J_INFO
;
597 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
598 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
599 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
600 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
601 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
602 return LTTNG_LOGLEVEL_LOG4J_ALL
;
609 * Maps JUL loglevel from string to value
611 static int loglevel_jul_str_to_value(const char *inputstr
)
614 char str
[LTTNG_SYMBOL_NAME_LEN
];
616 if (!inputstr
|| strlen(inputstr
) == 0) {
621 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
622 * added at the end of the loop so a the upper bound we avoid the overflow.
624 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
625 str
[i
] = toupper(inputstr
[i
]);
630 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
631 return LTTNG_LOGLEVEL_JUL_OFF
;
632 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
633 return LTTNG_LOGLEVEL_JUL_SEVERE
;
634 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
635 return LTTNG_LOGLEVEL_JUL_WARNING
;
636 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
637 return LTTNG_LOGLEVEL_JUL_INFO
;
638 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
639 return LTTNG_LOGLEVEL_JUL_CONFIG
;
640 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
641 return LTTNG_LOGLEVEL_JUL_FINE
;
642 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
643 return LTTNG_LOGLEVEL_JUL_FINER
;
644 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
645 return LTTNG_LOGLEVEL_JUL_FINEST
;
646 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
647 return LTTNG_LOGLEVEL_JUL_ALL
;
654 * Maps Python loglevel from string to value
656 static int loglevel_python_str_to_value(const char *inputstr
)
659 char str
[LTTNG_SYMBOL_NAME_LEN
];
661 if (!inputstr
|| strlen(inputstr
) == 0) {
666 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
667 * added at the end of the loop so a the upper bound we avoid the overflow.
669 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
670 str
[i
] = toupper(inputstr
[i
]);
675 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
676 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
677 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
678 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
679 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
680 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
681 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
682 return LTTNG_LOGLEVEL_PYTHON_INFO
;
683 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
684 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
685 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
686 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
693 * Maps loglevel from string to value
696 int loglevel_str_to_value(const char *inputstr
)
699 char str
[LTTNG_SYMBOL_NAME_LEN
];
701 if (!inputstr
|| strlen(inputstr
) == 0) {
706 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
707 * added at the end of the loop so a the upper bound we avoid the overflow.
709 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
710 str
[i
] = toupper(inputstr
[i
]);
714 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
715 return LTTNG_LOGLEVEL_EMERG
;
716 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
717 return LTTNG_LOGLEVEL_ALERT
;
718 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
719 return LTTNG_LOGLEVEL_CRIT
;
720 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
721 return LTTNG_LOGLEVEL_ERR
;
722 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
723 return LTTNG_LOGLEVEL_WARNING
;
724 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
725 return LTTNG_LOGLEVEL_NOTICE
;
726 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
727 return LTTNG_LOGLEVEL_INFO
;
728 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
729 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
730 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
731 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
732 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
733 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
734 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
735 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
736 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
737 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
738 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
739 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
740 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
741 return LTTNG_LOGLEVEL_DEBUG_LINE
;
742 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
743 return LTTNG_LOGLEVEL_DEBUG
;
750 const char *print_channel_name(const char *name
)
752 return name
? : DEFAULT_CHANNEL_NAME
;
756 const char *print_raw_channel_name(const char *name
)
758 return name
? : "<default>";
762 * Mi print exlcusion list
765 int mi_print_exclusion(char **names
)
768 int count
= names
? strutils_array_of_strings_len(names
) : 0;
776 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
781 for (i
= 0; i
< count
; i
++) {
782 ret
= mi_lttng_writer_write_element_string(writer
,
783 config_element_exclusion
, names
[i
]);
789 /* Close exclusions element */
790 ret
= mi_lttng_writer_close_element(writer
);
797 * Return allocated string for pretty-printing exclusion names.
800 char *print_exclusions(char **names
)
804 const char preamble
[] = " excluding ";
806 int count
= names
? strutils_array_of_strings_len(names
) : 0;
812 /* calculate total required length */
813 for (i
= 0; i
< count
; i
++) {
814 length
+= strlen(names
[i
]) + 4;
817 length
+= sizeof(preamble
);
818 ret
= zmalloc(length
);
822 strncpy(ret
, preamble
, length
);
823 for (i
= 0; i
< count
; i
++) {
825 strcat(ret
, names
[i
]);
827 if (i
!= count
- 1) {
836 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
840 const char *e
= event_name
;
841 const char *x
= exclusion
;
843 /* Scan both the excluder and the event letter by letter */
857 /* Event is a subset of the excluder */
858 ERR("Event %s: %s excludes all events from %s",
859 event_name
, exclusion
, event_name
);
865 * Reached the end of the event name before the
866 * end of the exclusion: this is valid.
888 WARN("Event %s: %s does not exclude any events from %s",
889 event_name
, exclusion
, event_name
);
896 int create_exclusion_list_and_validate(const char *event_name
,
897 const char *exclusions_arg
,
898 char ***exclusion_list
)
901 char **exclusions
= NULL
;
903 /* Event name must be a valid globbing pattern to allow exclusions. */
904 if (!strutils_is_star_glob_pattern(event_name
)) {
905 ERR("Event %s: Exclusions can only be used with a globbing pattern",
910 /* Split exclusions. */
911 exclusions
= strutils_split(exclusions_arg
, ',', true);
917 * If the event name is a star-at-end only globbing pattern,
918 * then we can validate the individual exclusions. Otherwise
919 * all exclusions are passed to the session daemon.
921 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
922 char * const *exclusion
;
924 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
925 if (!strutils_is_star_glob_pattern(*exclusion
) ||
926 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
927 ret
= check_exclusion_subsets(event_name
, *exclusion
);
935 *exclusion_list
= exclusions
;
941 strutils_free_null_terminated_array_of_strings(exclusions
);
947 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
950 char * const *exclusion
;
952 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
953 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
954 WARN("Event exclusion \"%s\" will be truncated",
962 * Enabling event using the lttng API.
963 * Note: in case of error only the last error code will be return.
965 static int enable_events(char *session_name
)
967 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
968 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
969 char *event_name
, *channel_name
= NULL
;
970 struct lttng_event
*ev
;
971 struct lttng_domain dom
;
972 char **exclusion_list
= NULL
;
974 memset(&dom
, 0, sizeof(dom
));
976 ev
= lttng_event_create();
984 WARN("Kernel loglevels are not supported.");
988 /* Create lttng domain */
990 dom
.type
= LTTNG_DOMAIN_KERNEL
;
991 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
992 } else if (opt_userspace
) {
993 dom
.type
= LTTNG_DOMAIN_UST
;
995 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
996 } else if (opt_jul
) {
997 dom
.type
= LTTNG_DOMAIN_JUL
;
999 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1000 } else if (opt_log4j
) {
1001 dom
.type
= LTTNG_DOMAIN_LOG4J
;
1003 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1004 } else if (opt_python
) {
1005 dom
.type
= LTTNG_DOMAIN_PYTHON
;
1007 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1009 /* Checked by the caller. */
1015 case LTTNG_DOMAIN_KERNEL
:
1016 case LTTNG_DOMAIN_JUL
:
1017 case LTTNG_DOMAIN_LOG4J
:
1018 case LTTNG_DOMAIN_PYTHON
:
1019 ERR("Event name exclusions are not yet implemented for %s events",
1020 get_domain_str(dom
.type
));
1023 case LTTNG_DOMAIN_UST
:
1024 /* Exclusions supported */
1032 * Adding a filter to a probe, function or userspace-probe would be
1033 * denied by the kernel tracer as it's not supported at the moment. We
1034 * do an early check here to warn the user.
1036 if (opt_filter
&& opt_kernel
) {
1037 switch (opt_event_type
) {
1038 case LTTNG_EVENT_ALL
:
1039 case LTTNG_EVENT_TRACEPOINT
:
1040 case LTTNG_EVENT_SYSCALL
:
1042 case LTTNG_EVENT_PROBE
:
1043 case LTTNG_EVENT_USERSPACE_PROBE
:
1044 case LTTNG_EVENT_FUNCTION
:
1045 ERR("Filter expressions are not supported for %s events",
1046 get_event_type_str(opt_event_type
));
1050 ret
= CMD_UNDEFINED
;
1055 channel_name
= opt_channel_name
;
1057 handle
= lttng_create_handle(session_name
, &dom
);
1058 if (handle
== NULL
) {
1065 /* Open a events element */
1066 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
1073 if (opt_enable_all
) {
1074 /* Default setup for enable all */
1076 ev
->type
= opt_event_type
;
1077 strcpy(ev
->name
, "*");
1078 /* kernel loglevels not implemented */
1079 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1081 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1082 strcpy(ev
->name
, "*");
1083 ev
->loglevel_type
= opt_loglevel_type
;
1085 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1086 if (opt_userspace
) {
1087 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1088 } else if (opt_jul
) {
1089 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1090 } else if (opt_log4j
) {
1091 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1092 } else if (opt_python
) {
1093 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1095 if (ev
->loglevel
== -1) {
1096 ERR("Unknown loglevel %s", opt_loglevel
);
1097 ret
= -LTTNG_ERR_INVALID
;
1101 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1102 if (opt_userspace
) {
1104 } else if (opt_jul
) {
1105 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1106 } else if (opt_log4j
) {
1107 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1108 } else if (opt_python
) {
1109 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1115 ret
= create_exclusion_list_and_validate("*",
1116 opt_exclude
, &exclusion_list
);
1123 warn_on_truncated_exclusion_names(exclusion_list
,
1127 ret
= lttng_enable_event_with_exclusions(handle
,
1130 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1134 case LTTNG_ERR_KERN_EVENT_EXIST
:
1135 WARN("Kernel events already enabled (channel %s, session %s)",
1136 print_channel_name(channel_name
), session_name
);
1139 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1141 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1142 ERR("Events: %s (channel %s, session %s)",
1144 print_channel_name(channel_name
),
1150 ERR("Events: %s (channel %s, session %s)",
1151 lttng_strerror(ret
),
1152 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1153 ? print_raw_channel_name(channel_name
)
1154 : print_channel_name(channel_name
),
1162 switch (opt_event_type
) {
1163 case LTTNG_EVENT_TRACEPOINT
:
1164 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1165 char *exclusion_string
= print_exclusions(exclusion_list
);
1167 if (!exclusion_string
) {
1168 PERROR("Cannot allocate exclusion_string");
1172 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1173 get_domain_str(dom
.type
),
1175 print_channel_name(channel_name
),
1177 free(exclusion_string
);
1179 char *exclusion_string
= print_exclusions(exclusion_list
);
1181 if (!exclusion_string
) {
1182 PERROR("Cannot allocate exclusion_string");
1186 MSG("All %s tracepoints%s are enabled in channel %s",
1187 get_domain_str(dom
.type
),
1189 print_channel_name(channel_name
));
1190 free(exclusion_string
);
1193 case LTTNG_EVENT_SYSCALL
:
1195 MSG("All %s system calls are enabled in channel %s",
1196 get_domain_str(dom
.type
),
1197 print_channel_name(channel_name
));
1200 case LTTNG_EVENT_ALL
:
1201 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1202 char *exclusion_string
= print_exclusions(exclusion_list
);
1204 if (!exclusion_string
) {
1205 PERROR("Cannot allocate exclusion_string");
1209 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1210 get_domain_str(dom
.type
),
1212 print_channel_name(channel_name
),
1214 free(exclusion_string
);
1216 char *exclusion_string
= print_exclusions(exclusion_list
);
1218 if (!exclusion_string
) {
1219 PERROR("Cannot allocate exclusion_string");
1223 MSG("All %s events%s are enabled in channel %s",
1224 get_domain_str(dom
.type
),
1226 print_channel_name(channel_name
));
1227 free(exclusion_string
);
1232 * We should not be here since lttng_enable_event should have
1233 * failed on the event type.
1240 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1242 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1244 if (command_ret
< 0) {
1245 switch (-command_ret
) {
1246 case LTTNG_ERR_FILTER_EXIST
:
1247 WARN("Filter on all events is already enabled"
1248 " (channel %s, session %s)",
1249 print_channel_name(channel_name
), session_name
);
1252 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1254 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1255 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1257 print_channel_name(channel_name
),
1258 session_name
, opt_filter
);
1263 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1264 lttng_strerror(command_ret
),
1265 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1266 ? print_raw_channel_name(channel_name
)
1267 : print_channel_name(channel_name
),
1268 session_name
, opt_filter
);
1272 error_holder
= command_ret
;
1275 MSG("Filter '%s' successfully set", opt_filter
);
1280 /* The wildcard * is used for kernel and ust domain to
1281 * represent ALL. We copy * in event name to force the wildcard use
1284 * Note: this is strictly for semantic and printing while in
1285 * machine interface mode.
1287 strcpy(ev
->name
, "*");
1289 /* If we reach here the events are enabled */
1290 if (!error
&& !warn
) {
1296 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1302 /* print exclusion */
1303 ret
= mi_print_exclusion(exclusion_list
);
1310 ret
= mi_lttng_writer_write_element_bool(writer
,
1311 mi_lttng_element_command_success
, success
);
1317 /* Close event element */
1318 ret
= mi_lttng_writer_close_element(writer
);
1328 /* Strip event list */
1329 event_name
= strtok(opt_event_list
, ",");
1330 while (event_name
!= NULL
) {
1331 /* Copy name and type of the event */
1332 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1333 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1334 ev
->type
= opt_event_type
;
1336 /* Kernel tracer action */
1338 DBG("Enabling kernel event %s for channel %s",
1340 print_channel_name(channel_name
));
1342 switch (opt_event_type
) {
1343 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1344 /* If event name differs from *, select tracepoint. */
1345 if (strcmp(ev
->name
, "*")) {
1346 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1349 case LTTNG_EVENT_TRACEPOINT
:
1351 case LTTNG_EVENT_PROBE
:
1352 ret
= parse_probe_opts(ev
, opt_probe
);
1354 ERR("Unable to parse probe options");
1359 case LTTNG_EVENT_USERSPACE_PROBE
:
1360 ret
= parse_userspace_probe_opts(ev
, opt_userspace_probe
);
1363 case CMD_UNSUPPORTED
:
1365 * Error message describing
1366 * what is not supported was
1367 * printed in the function.
1372 ERR("Unable to parse userspace probe options");
1378 case LTTNG_EVENT_FUNCTION
:
1379 ret
= parse_probe_opts(ev
, opt_function
);
1381 ERR("Unable to parse function probe options");
1386 case LTTNG_EVENT_SYSCALL
:
1387 ev
->type
= LTTNG_EVENT_SYSCALL
;
1390 ret
= CMD_UNDEFINED
;
1394 /* kernel loglevels not implemented */
1395 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1396 } else if (opt_userspace
) { /* User-space tracer action */
1397 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1398 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1400 switch (opt_event_type
) {
1401 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1403 case LTTNG_EVENT_TRACEPOINT
:
1404 /* Copy name and type of the event */
1405 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1406 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1407 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1409 case LTTNG_EVENT_PROBE
:
1410 case LTTNG_EVENT_FUNCTION
:
1411 case LTTNG_EVENT_SYSCALL
:
1412 case LTTNG_EVENT_USERSPACE_PROBE
:
1414 ERR("Event type not available for user-space tracing");
1415 ret
= CMD_UNSUPPORTED
;
1421 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1422 ERR("Exclusion option can only be used with tracepoint events");
1426 /* Free previously allocated items */
1427 strutils_free_null_terminated_array_of_strings(
1429 exclusion_list
= NULL
;
1430 ret
= create_exclusion_list_and_validate(
1431 event_name
, opt_exclude
,
1438 warn_on_truncated_exclusion_names(
1439 exclusion_list
, &warn
);
1442 ev
->loglevel_type
= opt_loglevel_type
;
1444 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1445 if (ev
->loglevel
== -1) {
1446 ERR("Unknown loglevel %s", opt_loglevel
);
1447 ret
= -LTTNG_ERR_INVALID
;
1453 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1454 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1455 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1456 ERR("Event type not supported for domain.");
1457 ret
= CMD_UNSUPPORTED
;
1461 ev
->loglevel_type
= opt_loglevel_type
;
1464 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1465 } else if (opt_log4j
) {
1466 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1467 } else if (opt_python
) {
1468 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1470 if (ev
->loglevel
== -1) {
1471 ERR("Unknown loglevel %s", opt_loglevel
);
1472 ret
= -LTTNG_ERR_INVALID
;
1477 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1478 } else if (opt_log4j
) {
1479 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1480 } else if (opt_python
) {
1481 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1484 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1485 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1486 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1492 char *exclusion_string
;
1494 command_ret
= lttng_enable_event_with_exclusions(handle
,
1497 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1499 exclusion_string
= print_exclusions(exclusion_list
);
1500 if (!exclusion_string
) {
1501 PERROR("Cannot allocate exclusion_string");
1505 if (command_ret
< 0) {
1506 /* Turn ret to positive value to handle the positive error code */
1507 switch (-command_ret
) {
1508 case LTTNG_ERR_KERN_EVENT_EXIST
:
1509 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1512 print_channel_name(channel_name
), session_name
);
1515 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1517 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1518 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1521 print_channel_name(channel_name
),
1526 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1527 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1528 event_name
, print_channel_name(channel_name
),
1533 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1535 lttng_strerror(command_ret
),
1536 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1537 ? print_raw_channel_name(channel_name
)
1538 : print_channel_name(channel_name
),
1543 error_holder
= command_ret
;
1546 case LTTNG_DOMAIN_KERNEL
:
1547 case LTTNG_DOMAIN_UST
:
1548 MSG("%s event %s%s created in channel %s",
1549 get_domain_str(dom
.type
),
1552 print_channel_name(channel_name
));
1554 case LTTNG_DOMAIN_JUL
:
1555 case LTTNG_DOMAIN_LOG4J
:
1556 case LTTNG_DOMAIN_PYTHON
:
1558 * Don't print the default channel
1559 * name for agent domains.
1561 MSG("%s event %s%s enabled",
1562 get_domain_str(dom
.type
),
1570 free(exclusion_string
);
1574 char *exclusion_string
;
1576 /* Filter present */
1579 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1581 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1583 exclusion_string
= print_exclusions(exclusion_list
);
1584 if (!exclusion_string
) {
1585 PERROR("Cannot allocate exclusion_string");
1589 if (command_ret
< 0) {
1590 switch (-command_ret
) {
1591 case LTTNG_ERR_FILTER_EXIST
:
1592 WARN("Filter on event %s%s is already enabled"
1593 " (channel %s, session %s)",
1596 print_channel_name(channel_name
), session_name
);
1599 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1601 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1602 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1605 print_channel_name(channel_name
),
1606 session_name
, opt_filter
);
1611 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1613 lttng_strerror(command_ret
),
1614 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1615 ? print_raw_channel_name(channel_name
)
1616 : print_channel_name(channel_name
),
1617 session_name
, opt_filter
);
1621 error_holder
= command_ret
;
1624 MSG("Event %s%s: Filter '%s' successfully set",
1625 event_name
, exclusion_string
,
1628 free(exclusion_string
);
1639 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1645 /* print exclusion */
1646 ret
= mi_print_exclusion(exclusion_list
);
1653 ret
= mi_lttng_writer_write_element_bool(writer
,
1654 mi_lttng_element_command_success
, success
);
1660 /* Close event element */
1661 ret
= mi_lttng_writer_close_element(writer
);
1669 event_name
= strtok(NULL
, ",");
1670 /* Reset warn, error and success */
1677 /* Close events element */
1678 ret
= mi_lttng_writer_close_element(writer
);
1691 lttng_destroy_handle(handle
);
1692 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1694 /* Overwrite ret with error_holder if there was an actual error with
1695 * enabling an event.
1697 ret
= error_holder
? error_holder
: ret
;
1699 lttng_event_destroy(ev
);
1704 * Add event to trace session
1706 int cmd_enable_events(int argc
, const char **argv
)
1708 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1709 static poptContext pc
;
1710 char *session_name
= NULL
;
1711 const char *leftover
= NULL
;
1712 int event_type
= -1;
1714 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1715 poptReadDefaultConfig(pc
, 0);
1717 /* Default event type */
1718 opt_event_type
= LTTNG_EVENT_ALL
;
1720 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1725 case OPT_TRACEPOINT
:
1726 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1729 opt_event_type
= LTTNG_EVENT_PROBE
;
1731 case OPT_USERSPACE_PROBE
:
1732 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1735 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1738 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1744 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1745 opt_loglevel
= poptGetOptArg(pc
);
1747 case OPT_LOGLEVEL_ONLY
:
1748 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1749 opt_loglevel
= poptGetOptArg(pc
);
1751 case OPT_LIST_OPTIONS
:
1752 list_cmd_options(stdout
, long_options
);
1759 ret
= CMD_UNDEFINED
;
1763 /* Validate event type. Multiple event type are not supported. */
1764 if (event_type
== -1) {
1765 event_type
= opt_event_type
;
1767 if (event_type
!= opt_event_type
) {
1768 ERR("Multiple event type not supported.");
1775 ret
= print_missing_or_multiple_domains(
1776 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1786 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1788 ret
= -LTTNG_ERR_NOMEM
;
1792 /* Open command element */
1793 ret
= mi_lttng_writer_command_open(writer
,
1794 mi_lttng_element_command_enable_event
);
1800 /* Open output element */
1801 ret
= mi_lttng_writer_open_element(writer
,
1802 mi_lttng_element_command_output
);
1809 opt_event_list
= (char*) poptGetArg(pc
);
1810 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1811 ERR("Missing event name(s).\n");
1816 leftover
= poptGetArg(pc
);
1818 ERR("Unknown argument: %s", leftover
);
1823 if (!opt_session_name
) {
1824 session_name
= get_session_name();
1825 if (session_name
== NULL
) {
1826 command_ret
= CMD_ERROR
;
1831 session_name
= opt_session_name
;
1834 command_ret
= enable_events(session_name
);
1843 /* Close output element */
1844 ret
= mi_lttng_writer_close_element(writer
);
1850 ret
= mi_lttng_writer_write_element_bool(writer
,
1851 mi_lttng_element_command_success
, success
);
1857 /* Command element close */
1858 ret
= mi_lttng_writer_command_close(writer
);
1867 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1868 /* Preserve original error code */
1869 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1872 if (opt_session_name
== NULL
) {
1876 /* Overwrite ret if an error occurred in enable_events */
1877 ret
= command_ret
? command_ret
: ret
;
1879 poptFreeContext(pc
);