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 "../command.h"
31 #if (LTTNG_SYMBOL_NAME_LEN == 256)
32 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
35 static char *opt_event_list
;
36 static int opt_event_type
;
37 static const char *opt_loglevel
;
38 static int opt_loglevel_type
;
39 static int opt_kernel
;
40 static char *opt_session_name
;
41 static int opt_userspace
;
44 static int opt_python
;
45 static int opt_enable_all
;
46 static char *opt_probe
;
47 static char *opt_userspace_probe
;
48 static char *opt_function
;
49 static char *opt_channel_name
;
50 static char *opt_filter
;
51 static char *opt_exclude
;
53 #ifdef LTTNG_EMBED_HELP
54 static const char help_msg
[] =
55 #include <lttng-enable-event.1.h>
74 static struct lttng_handle
*handle
;
75 static struct mi_writer
*writer
;
77 static struct poptOption long_options
[] = {
78 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
79 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
80 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
81 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
82 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
83 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
84 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
85 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
86 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
87 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
88 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
89 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
90 {"userspace-probe",0, POPT_ARG_STRING
, &opt_userspace_probe
, OPT_USERSPACE_PROBE
, 0, 0},
91 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
92 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
93 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
94 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
95 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
96 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
97 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
102 * Parse probe options.
104 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
106 int ret
= CMD_SUCCESS
;
109 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
110 char name
[LTTNG_SYMBOL_NAME_LEN
];
117 /* Check for symbol+offset */
118 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
119 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
121 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
122 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
123 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
124 if (*s_hex
== '\0') {
125 ERR("Invalid probe offset %s", s_hex
);
129 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
130 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
131 ev
->attr
.probe
.addr
= 0;
135 /* Check for symbol */
136 if (isalpha(name
[0]) || name
[0] == '_') {
137 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
140 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
141 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
142 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
143 ev
->attr
.probe
.offset
= 0;
144 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
145 ev
->attr
.probe
.addr
= 0;
150 /* Check for address */
151 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
154 * Return an error if the first character of the tentative
155 * address is NULL or not a digit. It can be "0" if the address
156 * is in hexadecimal and can be 1 to 9 if it's in decimal.
158 if (*s_hex
== '\0' || !isdigit(*s_hex
)) {
159 ERR("Invalid probe description %s", s_hex
);
163 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
164 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
165 ev
->attr
.probe
.offset
= 0;
166 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
178 * Walk the directories in the PATH environment variable to find the target
179 * binary passed as parameter.
181 * On success, the full path of the binary is copied in binary_full_path out
182 * parameter. This buffer is allocated by the caller and must be at least
183 * LTTNG_PATH_MAX bytes long.
184 * On failure, returns -1;
186 static int walk_command_search_path(const char *binary
, char *binary_full_path
)
188 char *tentative_binary_path
= NULL
;
189 char *command_search_path
= NULL
;
190 char *curr_search_dir_end
= NULL
;
191 char *curr_search_dir
= NULL
;
192 struct stat stat_output
;
195 command_search_path
= lttng_secure_getenv("PATH");
196 if (!command_search_path
) {
202 * Duplicate the $PATH string as the char pointer returned by getenv() should
205 command_search_path
= strdup(command_search_path
);
206 if (!command_search_path
) {
212 * This char array is used to concatenate path to binary to look for
215 tentative_binary_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
216 if (!tentative_binary_path
) {
221 curr_search_dir
= command_search_path
;
224 * Split on ':'. The return value of this call points to the
225 * matching character.
227 curr_search_dir_end
= strchr(curr_search_dir
, ':');
228 if (curr_search_dir_end
!= NULL
) {
230 * Add a NULL byte to the end of the first token so it
231 * can be used as a string.
233 curr_search_dir_end
[0] = '\0';
236 /* Empty the tentative path */
237 memset(tentative_binary_path
, 0, LTTNG_PATH_MAX
* sizeof(char));
240 * Build the tentative path to the binary using the current
241 * search directory and the name of the binary.
243 ret
= snprintf(tentative_binary_path
, LTTNG_PATH_MAX
, "%s/%s",
244 curr_search_dir
, binary
);
246 goto free_binary_path
;
248 if (ret
< LTTNG_PATH_MAX
) {
250 * Use STAT(2) to see if the file exists.
252 ret
= stat(tentative_binary_path
, &stat_output
);
255 * Verify that it is a regular file or a
256 * symlink and not a special file (e.g.
259 if (S_ISREG(stat_output
.st_mode
)
260 || S_ISLNK(stat_output
.st_mode
)) {
262 * Found a match, set the out parameter
263 * and return success.
265 ret
= lttng_strncpy(binary_full_path
,
266 tentative_binary_path
,
269 ERR("Source path does not fit "
270 "in destination buffer.");
272 goto free_binary_path
;
276 /* Go to the next entry in the $PATH variable. */
277 curr_search_dir
= curr_search_dir_end
+ 1;
278 } while (curr_search_dir_end
!= NULL
);
281 free(tentative_binary_path
);
283 free(command_search_path
);
289 * Check if the symbol field passed by the user is in fact an address or an
290 * offset from a symbol. Those two instrumentation types are not supported yet.
291 * It's expected to be a common mistake because of the existing --probe option
292 * that does support these formats.
294 * Here are examples of these unsupported formats for the --userspace-probe
296 * elf:/path/to/binary:0x400430
297 * elf:/path/to/binary:4194364
298 * elf:/path/to/binary:my_symbol+0x323
299 * elf:/path/to/binary:my_symbol+43
301 static int warn_userspace_probe_syntax(const char *symbol
)
305 /* Check if the symbol field is an hex address. */
306 ret
= sscanf(symbol
, "0x%*x");
308 /* If there is a match, print a warning and return an error. */
309 ERR("Userspace probe on address not supported yet.");
310 ret
= CMD_UNSUPPORTED
;
314 /* Check if the symbol field is an decimal address. */
315 ret
= sscanf(symbol
, "%*u");
317 /* If there is a match, print a warning and return an error. */
318 ERR("Userspace probe on address not supported yet.");
319 ret
= CMD_UNSUPPORTED
;
323 /* Check if the symbol field is symbol+hex_offset. */
324 ret
= sscanf(symbol
, "%*[^+]+0x%*x");
326 /* If there is a match, print a warning and return an error. */
327 ERR("Userspace probe on symbol+offset not supported yet.");
328 ret
= CMD_UNSUPPORTED
;
332 /* Check if the symbol field is symbol+decimal_offset. */
333 ret
= sscanf(symbol
, "%*[^+]+%*u");
335 /* If there is a match, print a warning and return an error. */
336 ERR("Userspace probe on symbol+offset not supported yet.");
337 ret
= CMD_UNSUPPORTED
;
348 * Parse userspace probe options
349 * Set the userspace probe fields in the lttng_event struct and set the
350 * target_path to the path to the binary.
352 static int parse_userspace_probe_opts(struct lttng_event
*ev
, char *opt
)
354 int ret
= CMD_SUCCESS
;
357 char *target_path
= NULL
;
358 char *unescaped_target_path
= NULL
;
359 char *real_target_path
= NULL
;
360 char *symbol_name
= NULL
, *probe_name
= NULL
, *provider_name
= NULL
;
361 struct lttng_userspace_probe_location
*probe_location
= NULL
;
362 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
371 case LTTNG_EVENT_USERSPACE_PROBE
:
378 * userspace probe fields are separated by ':'.
380 tokens
= strutils_split(opt
, ':', 1);
381 num_token
= strutils_array_of_strings_len(tokens
);
384 * Early sanity check that the number of parameter is between 2 and 4
387 * std:PATH:PROVIDER_NAME:PROBE_NAME
388 * PATH:SYMBOL (same behavior as ELF)
390 if (num_token
< 2 || num_token
> 4) {
396 * Looking up the first parameter will tell the technique to use to
397 * interpret the userspace probe/function description.
401 /* When the probe type is omitted we assume ELF for now. */
403 if (num_token
== 3 && strcmp(tokens
[0], "elf") == 0) {
404 target_path
= tokens
[1];
405 symbol_name
= tokens
[2];
406 } else if (num_token
== 2) {
407 target_path
= tokens
[0];
408 symbol_name
= tokens
[1];
414 lttng_userspace_probe_location_lookup_method_function_elf_create();
415 if (!lookup_method
) {
416 WARN("Failed to create ELF lookup method");
422 if (strcmp(tokens
[0], "sdt") == 0) {
423 target_path
= tokens
[1];
424 provider_name
= tokens
[2];
425 probe_name
= tokens
[3];
431 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
432 if (!lookup_method
) {
433 WARN("Failed to create SDT lookup method");
443 /* strutils_unescape_string allocates a new char *. */
444 unescaped_target_path
= strutils_unescape_string(target_path
, 0);
445 if (!unescaped_target_path
) {
447 goto end_destroy_lookup_method
;
451 * If there is not forward slash in the path. Walk the $PATH else
454 if (strchr(unescaped_target_path
, '/') == NULL
) {
455 /* Walk the $PATH variable to find the targeted binary. */
456 real_target_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
457 if (!real_target_path
) {
458 PERROR("Error allocating path buffer");
460 goto end_destroy_lookup_method
;
462 ret
= walk_command_search_path(unescaped_target_path
, real_target_path
);
464 ERR("Binary not found.");
466 goto end_destroy_lookup_method
;
470 * Expand references to `/./` and `/../`. This function does not check
471 * if the file exists. This call returns an allocated buffer on
474 real_target_path
= utils_expand_path_keep_symlink(unescaped_target_path
);
475 if (!real_target_path
) {
476 ERR("Error expanding the path to binary.");
478 goto end_destroy_lookup_method
;
482 * Check if the file exists using access(2), If it does not,
485 ret
= access(real_target_path
, F_OK
);
487 ERR("Cannot find binary at path: %s.", real_target_path
);
489 goto end_destroy_lookup_method
;
493 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method
)) {
494 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
496 * Check for common mistakes in userspace probe description syntax.
498 ret
= warn_userspace_probe_syntax(symbol_name
);
500 goto end_destroy_lookup_method
;
503 probe_location
= lttng_userspace_probe_location_function_create(
504 real_target_path
, symbol_name
, lookup_method
);
505 if (!probe_location
) {
506 WARN("Failed to create function probe location");
508 goto end_destroy_lookup_method
;
511 /* Ownership transferred to probe_location. */
512 lookup_method
= NULL
;
514 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
516 WARN("Failed to set probe location on event");
518 goto end_destroy_location
;
521 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
522 probe_location
= lttng_userspace_probe_location_tracepoint_create(
523 real_target_path
, provider_name
, probe_name
, lookup_method
);
524 if (!probe_location
) {
525 WARN("Failed to create function probe location");
527 goto end_destroy_lookup_method
;
530 /* Ownership transferred to probe_location. */
531 lookup_method
= NULL
;
533 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
535 WARN("Failed to set probe location on event");
537 goto end_destroy_location
;
542 goto end_destroy_lookup_method
;
545 /* Successful parsing, now clean up everything and return. */
548 end_destroy_location
:
549 lttng_userspace_probe_location_destroy(probe_location
);
550 end_destroy_lookup_method
:
551 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
553 strutils_free_null_terminated_array_of_strings(tokens
);
555 * Freeing both char * here makes the error handling simplier. free()
556 * performs not action if the pointer is NULL.
558 free(real_target_path
);
559 free(unescaped_target_path
);
565 * Maps LOG4j loglevel from string to value
567 static int loglevel_log4j_str_to_value(const char *inputstr
)
570 char str
[LTTNG_SYMBOL_NAME_LEN
];
572 if (!inputstr
|| strlen(inputstr
) == 0) {
577 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
578 * added at the end of the loop so a the upper bound we avoid the overflow.
580 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
581 str
[i
] = toupper(inputstr
[i
]);
586 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
587 return LTTNG_LOGLEVEL_LOG4J_OFF
;
588 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
589 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
590 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
591 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
592 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
593 return LTTNG_LOGLEVEL_LOG4J_WARN
;
594 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
595 return LTTNG_LOGLEVEL_LOG4J_INFO
;
596 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
597 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
598 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
599 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
600 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
601 return LTTNG_LOGLEVEL_LOG4J_ALL
;
608 * Maps JUL loglevel from string to value
610 static int loglevel_jul_str_to_value(const char *inputstr
)
613 char str
[LTTNG_SYMBOL_NAME_LEN
];
615 if (!inputstr
|| strlen(inputstr
) == 0) {
620 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
621 * added at the end of the loop so a the upper bound we avoid the overflow.
623 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
624 str
[i
] = toupper(inputstr
[i
]);
629 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
630 return LTTNG_LOGLEVEL_JUL_OFF
;
631 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
632 return LTTNG_LOGLEVEL_JUL_SEVERE
;
633 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
634 return LTTNG_LOGLEVEL_JUL_WARNING
;
635 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
636 return LTTNG_LOGLEVEL_JUL_INFO
;
637 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
638 return LTTNG_LOGLEVEL_JUL_CONFIG
;
639 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
640 return LTTNG_LOGLEVEL_JUL_FINE
;
641 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
642 return LTTNG_LOGLEVEL_JUL_FINER
;
643 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
644 return LTTNG_LOGLEVEL_JUL_FINEST
;
645 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
646 return LTTNG_LOGLEVEL_JUL_ALL
;
653 * Maps Python loglevel from string to value
655 static int loglevel_python_str_to_value(const char *inputstr
)
658 char str
[LTTNG_SYMBOL_NAME_LEN
];
660 if (!inputstr
|| strlen(inputstr
) == 0) {
665 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
666 * added at the end of the loop so a the upper bound we avoid the overflow.
668 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
669 str
[i
] = toupper(inputstr
[i
]);
674 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
675 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
676 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
677 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
678 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
679 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
680 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
681 return LTTNG_LOGLEVEL_PYTHON_INFO
;
682 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
683 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
684 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
685 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
692 * Maps loglevel from string to value
695 int loglevel_str_to_value(const char *inputstr
)
698 char str
[LTTNG_SYMBOL_NAME_LEN
];
700 if (!inputstr
|| strlen(inputstr
) == 0) {
705 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
706 * added at the end of the loop so a the upper bound we avoid the overflow.
708 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
709 str
[i
] = toupper(inputstr
[i
]);
713 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
714 return LTTNG_LOGLEVEL_EMERG
;
715 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
716 return LTTNG_LOGLEVEL_ALERT
;
717 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
718 return LTTNG_LOGLEVEL_CRIT
;
719 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
720 return LTTNG_LOGLEVEL_ERR
;
721 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
722 return LTTNG_LOGLEVEL_WARNING
;
723 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
724 return LTTNG_LOGLEVEL_NOTICE
;
725 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
726 return LTTNG_LOGLEVEL_INFO
;
727 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
728 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
729 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
730 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
731 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
732 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
733 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
734 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
735 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
736 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
737 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
738 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
739 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
740 return LTTNG_LOGLEVEL_DEBUG_LINE
;
741 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
742 return LTTNG_LOGLEVEL_DEBUG
;
749 const char *print_channel_name(const char *name
)
751 return name
? : DEFAULT_CHANNEL_NAME
;
755 const char *print_raw_channel_name(const char *name
)
757 return name
? : "<default>";
761 * Mi print exlcusion list
764 int mi_print_exclusion(char **names
)
767 int count
= names
? strutils_array_of_strings_len(names
) : 0;
775 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
780 for (i
= 0; i
< count
; i
++) {
781 ret
= mi_lttng_writer_write_element_string(writer
,
782 config_element_exclusion
, names
[i
]);
788 /* Close exclusions element */
789 ret
= mi_lttng_writer_close_element(writer
);
796 * Return allocated string for pretty-printing exclusion names.
799 char *print_exclusions(char **names
)
803 const char preamble
[] = " excluding ";
805 int count
= names
? strutils_array_of_strings_len(names
) : 0;
811 /* calculate total required length */
812 for (i
= 0; i
< count
; i
++) {
813 length
+= strlen(names
[i
]) + 4;
816 length
+= sizeof(preamble
);
817 ret
= zmalloc(length
);
821 strncpy(ret
, preamble
, length
);
822 for (i
= 0; i
< count
; i
++) {
824 strcat(ret
, names
[i
]);
826 if (i
!= count
- 1) {
835 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
839 const char *e
= event_name
;
840 const char *x
= exclusion
;
842 /* Scan both the excluder and the event letter by letter */
856 /* Event is a subset of the excluder */
857 ERR("Event %s: %s excludes all events from %s",
858 event_name
, exclusion
, event_name
);
864 * Reached the end of the event name before the
865 * end of the exclusion: this is valid.
887 WARN("Event %s: %s does not exclude any events from %s",
888 event_name
, exclusion
, event_name
);
895 int create_exclusion_list_and_validate(const char *event_name
,
896 const char *exclusions_arg
,
897 char ***exclusion_list
)
900 char **exclusions
= NULL
;
902 /* Event name must be a valid globbing pattern to allow exclusions. */
903 if (!strutils_is_star_glob_pattern(event_name
)) {
904 ERR("Event %s: Exclusions can only be used with a globbing pattern",
909 /* Split exclusions. */
910 exclusions
= strutils_split(exclusions_arg
, ',', true);
916 * If the event name is a star-at-end only globbing pattern,
917 * then we can validate the individual exclusions. Otherwise
918 * all exclusions are passed to the session daemon.
920 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
921 char * const *exclusion
;
923 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
924 if (!strutils_is_star_glob_pattern(*exclusion
) ||
925 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
926 ret
= check_exclusion_subsets(event_name
, *exclusion
);
934 *exclusion_list
= exclusions
;
940 strutils_free_null_terminated_array_of_strings(exclusions
);
946 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
949 char * const *exclusion
;
951 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
952 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
953 WARN("Event exclusion \"%s\" will be truncated",
961 * Enabling event using the lttng API.
962 * Note: in case of error only the last error code will be return.
964 static int enable_events(char *session_name
)
966 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
967 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
968 char *event_name
, *channel_name
= NULL
;
969 struct lttng_event
*ev
;
970 struct lttng_domain dom
;
971 char **exclusion_list
= NULL
;
973 memset(&dom
, 0, sizeof(dom
));
975 ev
= lttng_event_create();
983 WARN("Kernel loglevels are not supported.");
987 /* Create lttng domain */
989 dom
.type
= LTTNG_DOMAIN_KERNEL
;
990 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
991 } else if (opt_userspace
) {
992 dom
.type
= LTTNG_DOMAIN_UST
;
994 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
995 } else if (opt_jul
) {
996 dom
.type
= LTTNG_DOMAIN_JUL
;
998 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
999 } else if (opt_log4j
) {
1000 dom
.type
= LTTNG_DOMAIN_LOG4J
;
1002 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1003 } else if (opt_python
) {
1004 dom
.type
= LTTNG_DOMAIN_PYTHON
;
1006 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1008 /* Checked by the caller. */
1014 case LTTNG_DOMAIN_KERNEL
:
1015 case LTTNG_DOMAIN_JUL
:
1016 case LTTNG_DOMAIN_LOG4J
:
1017 case LTTNG_DOMAIN_PYTHON
:
1018 ERR("Event name exclusions are not yet implemented for %s events",
1019 get_domain_str(dom
.type
));
1022 case LTTNG_DOMAIN_UST
:
1023 /* Exclusions supported */
1031 * Adding a filter to a probe, function or userspace-probe would be
1032 * denied by the kernel tracer as it's not supported at the moment. We
1033 * do an early check here to warn the user.
1035 if (opt_filter
&& opt_kernel
) {
1036 switch (opt_event_type
) {
1037 case LTTNG_EVENT_ALL
:
1038 case LTTNG_EVENT_TRACEPOINT
:
1039 case LTTNG_EVENT_SYSCALL
:
1041 case LTTNG_EVENT_PROBE
:
1042 case LTTNG_EVENT_USERSPACE_PROBE
:
1043 case LTTNG_EVENT_FUNCTION
:
1044 ERR("Filter expressions are not supported for %s events",
1045 get_event_type_str(opt_event_type
));
1049 ret
= CMD_UNDEFINED
;
1054 channel_name
= opt_channel_name
;
1056 handle
= lttng_create_handle(session_name
, &dom
);
1057 if (handle
== NULL
) {
1064 /* Open a events element */
1065 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
1072 if (opt_enable_all
) {
1073 /* Default setup for enable all */
1075 ev
->type
= opt_event_type
;
1076 strcpy(ev
->name
, "*");
1077 /* kernel loglevels not implemented */
1078 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1080 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1081 strcpy(ev
->name
, "*");
1082 ev
->loglevel_type
= opt_loglevel_type
;
1084 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1085 if (opt_userspace
) {
1086 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1087 } else if (opt_jul
) {
1088 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1089 } else if (opt_log4j
) {
1090 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1091 } else if (opt_python
) {
1092 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1094 if (ev
->loglevel
== -1) {
1095 ERR("Unknown loglevel %s", opt_loglevel
);
1096 ret
= -LTTNG_ERR_INVALID
;
1100 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1101 if (opt_userspace
) {
1103 } else if (opt_jul
) {
1104 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1105 } else if (opt_log4j
) {
1106 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1107 } else if (opt_python
) {
1108 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1114 ret
= create_exclusion_list_and_validate("*",
1115 opt_exclude
, &exclusion_list
);
1122 warn_on_truncated_exclusion_names(exclusion_list
,
1126 ret
= lttng_enable_event_with_exclusions(handle
,
1129 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1133 case LTTNG_ERR_KERN_EVENT_EXIST
:
1134 WARN("Kernel events already enabled (channel %s, session %s)",
1135 print_channel_name(channel_name
), session_name
);
1138 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1140 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1141 ERR("Events: %s (channel %s, session %s)",
1143 print_channel_name(channel_name
),
1149 ERR("Events: %s (channel %s, session %s)",
1150 lttng_strerror(ret
),
1151 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1152 ? print_raw_channel_name(channel_name
)
1153 : print_channel_name(channel_name
),
1161 switch (opt_event_type
) {
1162 case LTTNG_EVENT_TRACEPOINT
:
1163 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1164 char *exclusion_string
= print_exclusions(exclusion_list
);
1166 if (!exclusion_string
) {
1167 PERROR("Cannot allocate exclusion_string");
1171 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1172 get_domain_str(dom
.type
),
1174 print_channel_name(channel_name
),
1176 free(exclusion_string
);
1178 char *exclusion_string
= print_exclusions(exclusion_list
);
1180 if (!exclusion_string
) {
1181 PERROR("Cannot allocate exclusion_string");
1185 MSG("All %s tracepoints%s are enabled in channel %s",
1186 get_domain_str(dom
.type
),
1188 print_channel_name(channel_name
));
1189 free(exclusion_string
);
1192 case LTTNG_EVENT_SYSCALL
:
1194 MSG("All %s system calls are enabled in channel %s",
1195 get_domain_str(dom
.type
),
1196 print_channel_name(channel_name
));
1199 case LTTNG_EVENT_ALL
:
1200 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1201 char *exclusion_string
= print_exclusions(exclusion_list
);
1203 if (!exclusion_string
) {
1204 PERROR("Cannot allocate exclusion_string");
1208 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1209 get_domain_str(dom
.type
),
1211 print_channel_name(channel_name
),
1213 free(exclusion_string
);
1215 char *exclusion_string
= print_exclusions(exclusion_list
);
1217 if (!exclusion_string
) {
1218 PERROR("Cannot allocate exclusion_string");
1222 MSG("All %s events%s are enabled in channel %s",
1223 get_domain_str(dom
.type
),
1225 print_channel_name(channel_name
));
1226 free(exclusion_string
);
1231 * We should not be here since lttng_enable_event should have
1232 * failed on the event type.
1239 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1241 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1243 if (command_ret
< 0) {
1244 switch (-command_ret
) {
1245 case LTTNG_ERR_FILTER_EXIST
:
1246 WARN("Filter on all events is already enabled"
1247 " (channel %s, session %s)",
1248 print_channel_name(channel_name
), session_name
);
1251 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1253 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1254 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1256 print_channel_name(channel_name
),
1257 session_name
, opt_filter
);
1262 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1263 lttng_strerror(command_ret
),
1264 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1265 ? print_raw_channel_name(channel_name
)
1266 : print_channel_name(channel_name
),
1267 session_name
, opt_filter
);
1271 error_holder
= command_ret
;
1274 MSG("Filter '%s' successfully set", opt_filter
);
1279 /* The wildcard * is used for kernel and ust domain to
1280 * represent ALL. We copy * in event name to force the wildcard use
1283 * Note: this is strictly for semantic and printing while in
1284 * machine interface mode.
1286 strcpy(ev
->name
, "*");
1288 /* If we reach here the events are enabled */
1289 if (!error
&& !warn
) {
1295 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1301 /* print exclusion */
1302 ret
= mi_print_exclusion(exclusion_list
);
1309 ret
= mi_lttng_writer_write_element_bool(writer
,
1310 mi_lttng_element_command_success
, success
);
1316 /* Close event element */
1317 ret
= mi_lttng_writer_close_element(writer
);
1327 /* Strip event list */
1328 event_name
= strtok(opt_event_list
, ",");
1329 while (event_name
!= NULL
) {
1330 /* Copy name and type of the event */
1331 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1332 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1333 ev
->type
= opt_event_type
;
1335 /* Kernel tracer action */
1337 DBG("Enabling kernel event %s for channel %s",
1339 print_channel_name(channel_name
));
1341 switch (opt_event_type
) {
1342 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1343 /* If event name differs from *, select tracepoint. */
1344 if (strcmp(ev
->name
, "*")) {
1345 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1348 case LTTNG_EVENT_TRACEPOINT
:
1350 case LTTNG_EVENT_PROBE
:
1351 ret
= parse_probe_opts(ev
, opt_probe
);
1353 ERR("Unable to parse probe options");
1358 case LTTNG_EVENT_USERSPACE_PROBE
:
1359 ret
= parse_userspace_probe_opts(ev
, opt_userspace_probe
);
1362 case CMD_UNSUPPORTED
:
1364 * Error message describing
1365 * what is not supported was
1366 * printed in the function.
1371 ERR("Unable to parse userspace probe options");
1377 case LTTNG_EVENT_FUNCTION
:
1378 ret
= parse_probe_opts(ev
, opt_function
);
1380 ERR("Unable to parse function probe options");
1385 case LTTNG_EVENT_SYSCALL
:
1386 ev
->type
= LTTNG_EVENT_SYSCALL
;
1389 ret
= CMD_UNDEFINED
;
1393 /* kernel loglevels not implemented */
1394 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1395 } else if (opt_userspace
) { /* User-space tracer action */
1396 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1397 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1399 switch (opt_event_type
) {
1400 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1402 case LTTNG_EVENT_TRACEPOINT
:
1403 /* Copy name and type of the event */
1404 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1405 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1406 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1408 case LTTNG_EVENT_PROBE
:
1409 case LTTNG_EVENT_FUNCTION
:
1410 case LTTNG_EVENT_SYSCALL
:
1411 case LTTNG_EVENT_USERSPACE_PROBE
:
1413 ERR("Event type not available for user-space tracing");
1414 ret
= CMD_UNSUPPORTED
;
1420 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1421 ERR("Exclusion option can only be used with tracepoint events");
1425 /* Free previously allocated items */
1426 strutils_free_null_terminated_array_of_strings(
1428 exclusion_list
= NULL
;
1429 ret
= create_exclusion_list_and_validate(
1430 event_name
, opt_exclude
,
1437 warn_on_truncated_exclusion_names(
1438 exclusion_list
, &warn
);
1441 ev
->loglevel_type
= opt_loglevel_type
;
1443 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1444 if (ev
->loglevel
== -1) {
1445 ERR("Unknown loglevel %s", opt_loglevel
);
1446 ret
= -LTTNG_ERR_INVALID
;
1452 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1453 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1454 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1455 ERR("Event type not supported for domain.");
1456 ret
= CMD_UNSUPPORTED
;
1460 ev
->loglevel_type
= opt_loglevel_type
;
1463 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1464 } else if (opt_log4j
) {
1465 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1466 } else if (opt_python
) {
1467 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1469 if (ev
->loglevel
== -1) {
1470 ERR("Unknown loglevel %s", opt_loglevel
);
1471 ret
= -LTTNG_ERR_INVALID
;
1476 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1477 } else if (opt_log4j
) {
1478 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1479 } else if (opt_python
) {
1480 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1483 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1484 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1485 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1491 char *exclusion_string
;
1493 command_ret
= lttng_enable_event_with_exclusions(handle
,
1496 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1498 exclusion_string
= print_exclusions(exclusion_list
);
1499 if (!exclusion_string
) {
1500 PERROR("Cannot allocate exclusion_string");
1504 if (command_ret
< 0) {
1505 /* Turn ret to positive value to handle the positive error code */
1506 switch (-command_ret
) {
1507 case LTTNG_ERR_KERN_EVENT_EXIST
:
1508 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1511 print_channel_name(channel_name
), session_name
);
1514 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1516 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1517 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1520 print_channel_name(channel_name
),
1525 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1526 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1527 event_name
, print_channel_name(channel_name
),
1532 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1534 lttng_strerror(command_ret
),
1535 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1536 ? print_raw_channel_name(channel_name
)
1537 : print_channel_name(channel_name
),
1542 error_holder
= command_ret
;
1545 case LTTNG_DOMAIN_KERNEL
:
1546 case LTTNG_DOMAIN_UST
:
1547 MSG("%s event %s%s created in channel %s",
1548 get_domain_str(dom
.type
),
1551 print_channel_name(channel_name
));
1553 case LTTNG_DOMAIN_JUL
:
1554 case LTTNG_DOMAIN_LOG4J
:
1555 case LTTNG_DOMAIN_PYTHON
:
1557 * Don't print the default channel
1558 * name for agent domains.
1560 MSG("%s event %s%s enabled",
1561 get_domain_str(dom
.type
),
1569 free(exclusion_string
);
1573 char *exclusion_string
;
1575 /* Filter present */
1578 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1580 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1582 exclusion_string
= print_exclusions(exclusion_list
);
1583 if (!exclusion_string
) {
1584 PERROR("Cannot allocate exclusion_string");
1588 if (command_ret
< 0) {
1589 switch (-command_ret
) {
1590 case LTTNG_ERR_FILTER_EXIST
:
1591 WARN("Filter on event %s%s is already enabled"
1592 " (channel %s, session %s)",
1595 print_channel_name(channel_name
), session_name
);
1598 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1600 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1601 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1604 print_channel_name(channel_name
),
1605 session_name
, opt_filter
);
1610 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1612 lttng_strerror(command_ret
),
1613 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1614 ? print_raw_channel_name(channel_name
)
1615 : print_channel_name(channel_name
),
1616 session_name
, opt_filter
);
1620 error_holder
= command_ret
;
1623 MSG("Event %s%s: Filter '%s' successfully set",
1624 event_name
, exclusion_string
,
1627 free(exclusion_string
);
1638 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1644 /* print exclusion */
1645 ret
= mi_print_exclusion(exclusion_list
);
1652 ret
= mi_lttng_writer_write_element_bool(writer
,
1653 mi_lttng_element_command_success
, success
);
1659 /* Close event element */
1660 ret
= mi_lttng_writer_close_element(writer
);
1668 event_name
= strtok(NULL
, ",");
1669 /* Reset warn, error and success */
1676 /* Close events element */
1677 ret
= mi_lttng_writer_close_element(writer
);
1690 lttng_destroy_handle(handle
);
1691 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1693 /* Overwrite ret with error_holder if there was an actual error with
1694 * enabling an event.
1696 ret
= error_holder
? error_holder
: ret
;
1698 lttng_event_destroy(ev
);
1703 * Add event to trace session
1705 int cmd_enable_events(int argc
, const char **argv
)
1707 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1708 static poptContext pc
;
1709 char *session_name
= NULL
;
1710 const char *leftover
= NULL
;
1711 int event_type
= -1;
1713 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1714 poptReadDefaultConfig(pc
, 0);
1716 /* Default event type */
1717 opt_event_type
= LTTNG_EVENT_ALL
;
1719 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1724 case OPT_TRACEPOINT
:
1725 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1728 opt_event_type
= LTTNG_EVENT_PROBE
;
1730 case OPT_USERSPACE_PROBE
:
1731 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1734 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1737 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1743 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1744 opt_loglevel
= poptGetOptArg(pc
);
1746 case OPT_LOGLEVEL_ONLY
:
1747 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1748 opt_loglevel
= poptGetOptArg(pc
);
1750 case OPT_LIST_OPTIONS
:
1751 list_cmd_options(stdout
, long_options
);
1758 ret
= CMD_UNDEFINED
;
1762 /* Validate event type. Multiple event type are not supported. */
1763 if (event_type
== -1) {
1764 event_type
= opt_event_type
;
1766 if (event_type
!= opt_event_type
) {
1767 ERR("Multiple event type not supported.");
1774 ret
= print_missing_or_multiple_domains(
1775 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1785 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1787 ret
= -LTTNG_ERR_NOMEM
;
1791 /* Open command element */
1792 ret
= mi_lttng_writer_command_open(writer
,
1793 mi_lttng_element_command_enable_event
);
1799 /* Open output element */
1800 ret
= mi_lttng_writer_open_element(writer
,
1801 mi_lttng_element_command_output
);
1808 opt_event_list
= (char*) poptGetArg(pc
);
1809 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1810 ERR("Missing event name(s).\n");
1815 leftover
= poptGetArg(pc
);
1817 ERR("Unknown argument: %s", leftover
);
1822 if (!opt_session_name
) {
1823 session_name
= get_session_name();
1824 if (session_name
== NULL
) {
1825 command_ret
= CMD_ERROR
;
1830 session_name
= opt_session_name
;
1833 command_ret
= enable_events(session_name
);
1842 /* Close output element */
1843 ret
= mi_lttng_writer_close_element(writer
);
1849 ret
= mi_lttng_writer_write_element_bool(writer
,
1850 mi_lttng_element_command_success
, success
);
1856 /* Command element close */
1857 ret
= mi_lttng_writer_command_close(writer
);
1866 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1867 /* Preserve original error code */
1868 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1871 if (opt_session_name
== NULL
) {
1875 /* Overwrite ret if an error occurred in enable_events */
1876 ret
= command_ret
? command_ret
: ret
;
1878 poptFreeContext(pc
);