2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/compat/string.h>
31 #include <common/compat/getenv.h>
32 #include <common/string-utils/string-utils.h>
33 #include <common/utils.h>
35 #include <lttng/constant.h>
37 #include <common/mi-lttng.h>
39 #include "../command.h"
41 #if (LTTNG_SYMBOL_NAME_LEN == 256)
42 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
45 static char *opt_event_list
;
46 static int opt_event_type
;
47 static const char *opt_loglevel
;
48 static int opt_loglevel_type
;
49 static int opt_kernel
;
50 static char *opt_session_name
;
51 static int opt_userspace
;
54 static int opt_python
;
55 static int opt_enable_all
;
56 static char *opt_probe
;
57 static char *opt_userspace_probe
;
58 static char *opt_function
;
59 static char *opt_channel_name
;
60 static char *opt_filter
;
61 static char *opt_exclude
;
63 #ifdef LTTNG_EMBED_HELP
64 static const char help_msg
[] =
65 #include <lttng-enable-event.1.h>
84 static struct lttng_handle
*handle
;
85 static struct mi_writer
*writer
;
87 static struct poptOption long_options
[] = {
88 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
89 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
90 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
91 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
92 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
93 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
94 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
95 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
96 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
97 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
98 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
99 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
100 {"userspace-probe",0, POPT_ARG_STRING
, &opt_userspace_probe
, OPT_USERSPACE_PROBE
, 0, 0},
101 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
102 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
103 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
104 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
105 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
106 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
107 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
108 {0, 0, 0, 0, 0, 0, 0}
112 * Parse probe options.
114 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
116 int ret
= CMD_SUCCESS
;
119 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
120 char name
[LTTNG_SYMBOL_NAME_LEN
];
127 /* Check for symbol+offset */
128 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
129 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
131 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
132 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
133 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
134 if (*s_hex
== '\0') {
135 ERR("Invalid probe offset %s", s_hex
);
139 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
140 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
141 ev
->attr
.probe
.addr
= 0;
145 /* Check for symbol */
146 if (isalpha(name
[0])) {
147 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
150 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
151 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
152 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
153 ev
->attr
.probe
.offset
= 0;
154 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
155 ev
->attr
.probe
.addr
= 0;
160 /* Check for address */
161 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
163 if (*s_hex
== '\0') {
164 ERR("Invalid probe address %s", s_hex
);
168 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
169 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
170 ev
->attr
.probe
.offset
= 0;
171 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
183 * Walk the directories in the PATH environment variable to find the target
184 * binary passed as parameter.
186 * On success, the full path of the binary is copied in binary_full_path out
187 * parameter. This buffer is allocated by the caller and must be at least
188 * LTTNG_PATH_MAX bytes long.
189 * On failure, returns -1;
191 static int walk_command_search_path(const char *binary
, char *binary_full_path
)
193 char *tentative_binary_path
= NULL
;
194 char *command_search_path
= NULL
;
195 char *curr_search_dir_end
= NULL
;
196 char *curr_search_dir
= NULL
;
197 struct stat stat_output
;
200 command_search_path
= lttng_secure_getenv("PATH");
201 if (!command_search_path
) {
207 * Duplicate the $PATH string as the char pointer returned by getenv() should
210 command_search_path
= strdup(command_search_path
);
211 if (!command_search_path
) {
217 * This char array is used to concatenate path to binary to look for
220 tentative_binary_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
221 if (!tentative_binary_path
) {
226 curr_search_dir
= command_search_path
;
229 * Split on ':'. The return value of this call points to the
230 * matching character.
232 curr_search_dir_end
= strchr(curr_search_dir
, ':');
233 if (curr_search_dir_end
!= NULL
) {
235 * Add a NULL byte to the end of the first token so it
236 * can be used as a string.
238 curr_search_dir_end
[0] = '\0';
241 /* Empty the tentative path */
242 memset(tentative_binary_path
, 0, LTTNG_PATH_MAX
* sizeof(char));
245 * Build the tentative path to the binary using the current
246 * search directory and the name of the binary.
248 ret
= snprintf(tentative_binary_path
, LTTNG_PATH_MAX
, "%s/%s",
249 curr_search_dir
, binary
);
251 goto free_binary_path
;
253 if (ret
< LTTNG_PATH_MAX
) {
255 * Use STAT(2) to see if the file exists.
257 ret
= stat(tentative_binary_path
, &stat_output
);
260 * Verify that it is a regular file or a
261 * symlink and not a special file (e.g.
264 if (S_ISREG(stat_output
.st_mode
)
265 || S_ISLNK(stat_output
.st_mode
)) {
267 * Found a match, set the out parameter
268 * and return success.
270 ret
= lttng_strncpy(binary_full_path
,
271 tentative_binary_path
,
274 ERR("Source path does not fit "
275 "in destination buffer.");
277 goto free_binary_path
;
281 /* Go to the next entry in the $PATH variable. */
282 curr_search_dir
= curr_search_dir_end
+ 1;
283 } while (curr_search_dir_end
!= NULL
);
286 free(tentative_binary_path
);
288 free(command_search_path
);
294 * Check if the symbol field passed by the user is in fact an address or an
295 * offset from a symbol. Those two instrumentation types are not supported yet.
296 * It's expected to be a common mistake because of the existing --probe option
297 * that does support these formats.
299 * Here are examples of these unsupported formats for the --userspace-probe
301 * elf:/path/to/binary:0x400430
302 * elf:/path/to/binary:4194364
303 * elf:/path/to/binary:my_symbol+0x323
304 * elf:/path/to/binary:my_symbol+43
306 static int warn_userspace_probe_syntax(const char *symbol
)
310 /* Check if the symbol field is an hex address. */
311 ret
= sscanf(symbol
, "0x%*x");
313 /* If there is a match, print a warning and return an error. */
314 ERR("Userspace probe on address not supported yet.");
315 ret
= CMD_UNSUPPORTED
;
319 /* Check if the symbol field is an decimal address. */
320 ret
= sscanf(symbol
, "%*u");
322 /* If there is a match, print a warning and return an error. */
323 ERR("Userspace probe on address not supported yet.");
324 ret
= CMD_UNSUPPORTED
;
328 /* Check if the symbol field is symbol+hex_offset. */
329 ret
= sscanf(symbol
, "%*[^+]+0x%*x");
331 /* If there is a match, print a warning and return an error. */
332 ERR("Userspace probe on symbol+offset not supported yet.");
333 ret
= CMD_UNSUPPORTED
;
337 /* Check if the symbol field is symbol+decimal_offset. */
338 ret
= sscanf(symbol
, "%*[^+]+%*u");
340 /* If there is a match, print a warning and return an error. */
341 ERR("Userspace probe on symbol+offset not supported yet.");
342 ret
= CMD_UNSUPPORTED
;
353 * Parse userspace probe options
354 * Set the userspace probe fields in the lttng_event struct and set the
355 * target_path to the path to the binary.
357 static int parse_userspace_probe_opts(struct lttng_event
*ev
, char *opt
)
359 int ret
= CMD_SUCCESS
;
362 char *target_path
= NULL
;
363 char *unescaped_target_path
= NULL
;
364 char *real_target_path
= NULL
;
365 char *symbol_name
= NULL
, *probe_name
= NULL
, *provider_name
= NULL
;
366 struct lttng_userspace_probe_location
*probe_location
= NULL
;
367 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
376 case LTTNG_EVENT_USERSPACE_PROBE
:
383 * userspace probe fields are separated by ':'.
385 tokens
= strutils_split(opt
, ':', 1);
386 num_token
= strutils_array_of_strings_len(tokens
);
389 * Early sanity check that the number of parameter is between 2 and 4
392 * std:PATH:PROVIDER_NAME:PROBE_NAME
393 * PATH:SYMBOL (same behavior as ELF)
395 if (num_token
< 2 || num_token
> 4) {
401 * Looking up the first parameter will tell the technique to use to
402 * interpret the userspace probe/function description.
406 /* When the probe type is omitted we assume ELF for now. */
408 if (num_token
== 3 && strcmp(tokens
[0], "elf") == 0) {
409 target_path
= tokens
[1];
410 symbol_name
= tokens
[2];
411 } else if (num_token
== 2) {
412 target_path
= tokens
[0];
413 symbol_name
= tokens
[1];
419 lttng_userspace_probe_location_lookup_method_function_elf_create();
420 if (!lookup_method
) {
421 WARN("Failed to create ELF lookup method");
427 if (strcmp(tokens
[0], "sdt") == 0) {
428 target_path
= tokens
[1];
429 provider_name
= tokens
[2];
430 probe_name
= tokens
[3];
436 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
437 if (!lookup_method
) {
438 WARN("Failed to create ELF lookup method");
448 /* strutils_unescape_string allocates a new char *. */
449 unescaped_target_path
= strutils_unescape_string(target_path
, 0);
450 if (!unescaped_target_path
) {
452 goto end_destroy_lookup_method
;
456 * If there is not forward slash in the path. Walk the $PATH else
459 if (strchr(unescaped_target_path
, '/') == NULL
) {
460 /* Walk the $PATH variable to find the targeted binary. */
461 real_target_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
462 if (!real_target_path
) {
463 PERROR("Error allocating path buffer");
465 goto end_destroy_lookup_method
;
467 ret
= walk_command_search_path(unescaped_target_path
, real_target_path
);
469 ERR("Binary not found.");
471 goto end_destroy_lookup_method
;
475 * Expand references to `/./` and `/../`. This function does not check
476 * if the file exists. This call returns an allocated buffer on
479 real_target_path
= utils_expand_path_keep_symlink(unescaped_target_path
);
480 if (!real_target_path
) {
481 ERR("Error expanding the path to binary.");
483 goto end_destroy_lookup_method
;
487 * Check if the file exists using access(2). If it does not, walk the
490 ret
= access(real_target_path
, F_OK
);
492 ERR("Cannot find binary at path: %s.", real_target_path
);
494 goto end_destroy_lookup_method
;
498 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method
)) {
499 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
501 * Check for common mistakes in userspace probe description syntax.
503 ret
= warn_userspace_probe_syntax(symbol_name
);
505 goto end_destroy_lookup_method
;
508 probe_location
= lttng_userspace_probe_location_function_create(
509 real_target_path
, symbol_name
, lookup_method
);
510 if (!probe_location
) {
511 WARN("Failed to create function probe location");
513 goto end_destroy_lookup_method
;
516 /* Ownership transferred to probe_location. */
517 lookup_method
= NULL
;
519 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
521 WARN("Failed to set probe location on event");
523 goto end_destroy_location
;
526 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
527 probe_location
= lttng_userspace_probe_location_tracepoint_create(
528 real_target_path
, provider_name
, probe_name
, lookup_method
);
529 if (!probe_location
) {
530 WARN("Failed to create function probe location");
532 goto end_destroy_lookup_method
;
535 /* Ownership transferred to probe_location. */
536 lookup_method
= NULL
;
538 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
540 WARN("Failed to set probe location on event");
542 goto end_destroy_location
;
547 goto end_destroy_lookup_method
;
550 /* Successful parsing, now clean up everything and return. */
553 end_destroy_location
:
554 lttng_userspace_probe_location_destroy(probe_location
);
555 end_destroy_lookup_method
:
556 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
558 strutils_free_null_terminated_array_of_strings(tokens
);
560 * Freeing both char * here makes the error handling simplier. free()
561 * performs not action if the pointer is NULL.
563 free(real_target_path
);
564 free(unescaped_target_path
);
570 * Maps LOG4j loglevel from string to value
572 static int loglevel_log4j_str_to_value(const char *inputstr
)
575 char str
[LTTNG_SYMBOL_NAME_LEN
];
577 if (!inputstr
|| strlen(inputstr
) == 0) {
582 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
583 * added at the end of the loop so a the upper bound we avoid the overflow.
585 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
586 str
[i
] = toupper(inputstr
[i
]);
591 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
592 return LTTNG_LOGLEVEL_LOG4J_OFF
;
593 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
594 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
595 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
596 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
597 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
598 return LTTNG_LOGLEVEL_LOG4J_WARN
;
599 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
600 return LTTNG_LOGLEVEL_LOG4J_INFO
;
601 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
602 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
603 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
604 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
605 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
606 return LTTNG_LOGLEVEL_LOG4J_ALL
;
613 * Maps JUL loglevel from string to value
615 static int loglevel_jul_str_to_value(const char *inputstr
)
618 char str
[LTTNG_SYMBOL_NAME_LEN
];
620 if (!inputstr
|| strlen(inputstr
) == 0) {
625 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
626 * added at the end of the loop so a the upper bound we avoid the overflow.
628 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
629 str
[i
] = toupper(inputstr
[i
]);
634 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
635 return LTTNG_LOGLEVEL_JUL_OFF
;
636 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
637 return LTTNG_LOGLEVEL_JUL_SEVERE
;
638 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
639 return LTTNG_LOGLEVEL_JUL_WARNING
;
640 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
641 return LTTNG_LOGLEVEL_JUL_INFO
;
642 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
643 return LTTNG_LOGLEVEL_JUL_CONFIG
;
644 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
645 return LTTNG_LOGLEVEL_JUL_FINE
;
646 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
647 return LTTNG_LOGLEVEL_JUL_FINER
;
648 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
649 return LTTNG_LOGLEVEL_JUL_FINEST
;
650 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
651 return LTTNG_LOGLEVEL_JUL_ALL
;
658 * Maps Python loglevel from string to value
660 static int loglevel_python_str_to_value(const char *inputstr
)
663 char str
[LTTNG_SYMBOL_NAME_LEN
];
665 if (!inputstr
|| strlen(inputstr
) == 0) {
670 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
671 * added at the end of the loop so a the upper bound we avoid the overflow.
673 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
674 str
[i
] = toupper(inputstr
[i
]);
679 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
680 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
681 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
682 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
683 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
684 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
685 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
686 return LTTNG_LOGLEVEL_PYTHON_INFO
;
687 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
688 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
689 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
690 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
697 * Maps loglevel from string to value
700 int loglevel_str_to_value(const char *inputstr
)
703 char str
[LTTNG_SYMBOL_NAME_LEN
];
705 if (!inputstr
|| strlen(inputstr
) == 0) {
710 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
711 * added at the end of the loop so a the upper bound we avoid the overflow.
713 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
714 str
[i
] = toupper(inputstr
[i
]);
718 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
719 return LTTNG_LOGLEVEL_EMERG
;
720 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
721 return LTTNG_LOGLEVEL_ALERT
;
722 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
723 return LTTNG_LOGLEVEL_CRIT
;
724 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
725 return LTTNG_LOGLEVEL_ERR
;
726 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
727 return LTTNG_LOGLEVEL_WARNING
;
728 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
729 return LTTNG_LOGLEVEL_NOTICE
;
730 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
731 return LTTNG_LOGLEVEL_INFO
;
732 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
733 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
734 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
735 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
736 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
737 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
738 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
739 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
740 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
741 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
742 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
743 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
744 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
745 return LTTNG_LOGLEVEL_DEBUG_LINE
;
746 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
747 return LTTNG_LOGLEVEL_DEBUG
;
754 const char *print_channel_name(const char *name
)
756 return name
? : DEFAULT_CHANNEL_NAME
;
760 const char *print_raw_channel_name(const char *name
)
762 return name
? : "<default>";
766 * Mi print exlcusion list
769 int mi_print_exclusion(char **names
)
772 int count
= names
? strutils_array_of_strings_len(names
) : 0;
780 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
785 for (i
= 0; i
< count
; i
++) {
786 ret
= mi_lttng_writer_write_element_string(writer
,
787 config_element_exclusion
, names
[i
]);
793 /* Close exclusions element */
794 ret
= mi_lttng_writer_close_element(writer
);
801 * Return allocated string for pretty-printing exclusion names.
804 char *print_exclusions(char **names
)
808 const char *preamble
= " excluding ";
810 int count
= names
? strutils_array_of_strings_len(names
) : 0;
816 /* calculate total required length */
817 for (i
= 0; i
< count
; i
++) {
818 length
+= strlen(names
[i
]) + 4;
821 /* add length of preamble + one for NUL - one for last (missing) comma */
822 length
+= strlen(preamble
);
823 ret
= zmalloc(length
+ 1);
827 strncpy(ret
, preamble
, length
);
828 for (i
= 0; i
< count
; i
++) {
830 strcat(ret
, names
[i
]);
832 if (i
!= count
- 1) {
841 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
845 const char *e
= event_name
;
846 const char *x
= exclusion
;
848 /* Scan both the excluder and the event letter by letter */
862 /* Event is a subset of the excluder */
863 ERR("Event %s: %s excludes all events from %s",
864 event_name
, exclusion
, event_name
);
870 * Reached the end of the event name before the
871 * end of the exclusion: this is valid.
893 WARN("Event %s: %s does not exclude any events from %s",
894 event_name
, exclusion
, event_name
);
901 int create_exclusion_list_and_validate(const char *event_name
,
902 const char *exclusions_arg
,
903 char ***exclusion_list
)
906 char **exclusions
= NULL
;
908 /* Event name must be a valid globbing pattern to allow exclusions. */
909 if (!strutils_is_star_glob_pattern(event_name
)) {
910 ERR("Event %s: Exclusions can only be used with a globbing pattern",
915 /* Split exclusions. */
916 exclusions
= strutils_split(exclusions_arg
, ',', true);
922 * If the event name is a star-at-end only globbing pattern,
923 * then we can validate the individual exclusions. Otherwise
924 * all exclusions are passed to the session daemon.
926 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
927 char * const *exclusion
;
929 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
930 if (!strutils_is_star_glob_pattern(*exclusion
) ||
931 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
932 ret
= check_exclusion_subsets(event_name
, *exclusion
);
940 *exclusion_list
= exclusions
;
946 strutils_free_null_terminated_array_of_strings(exclusions
);
952 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
955 char * const *exclusion
;
957 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
958 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
959 WARN("Event exclusion \"%s\" will be truncated",
967 * Enabling event using the lttng API.
968 * Note: in case of error only the last error code will be return.
970 static int enable_events(char *session_name
)
972 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
973 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
974 char *event_name
, *channel_name
= NULL
;
975 struct lttng_event
*ev
;
976 struct lttng_domain dom
;
977 char **exclusion_list
= NULL
;
979 memset(&dom
, 0, sizeof(dom
));
981 ev
= lttng_event_create();
989 WARN("Kernel loglevels are not supported.");
993 /* Create lttng domain */
995 dom
.type
= LTTNG_DOMAIN_KERNEL
;
996 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
997 } else if (opt_userspace
) {
998 dom
.type
= LTTNG_DOMAIN_UST
;
1000 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1001 } else if (opt_jul
) {
1002 dom
.type
= LTTNG_DOMAIN_JUL
;
1004 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1005 } else if (opt_log4j
) {
1006 dom
.type
= LTTNG_DOMAIN_LOG4J
;
1008 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1009 } else if (opt_python
) {
1010 dom
.type
= LTTNG_DOMAIN_PYTHON
;
1012 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1014 /* Checked by the caller. */
1020 case LTTNG_DOMAIN_KERNEL
:
1021 case LTTNG_DOMAIN_JUL
:
1022 case LTTNG_DOMAIN_LOG4J
:
1023 case LTTNG_DOMAIN_PYTHON
:
1024 ERR("Event name exclusions are not yet implemented for %s events",
1025 get_domain_str(dom
.type
));
1028 case LTTNG_DOMAIN_UST
:
1029 /* Exclusions supported */
1037 * Adding a filter to a probe, function or userspace-probe would be
1038 * denied by the kernel tracer as it's not supported at the moment. We
1039 * do an early check here to warn the user.
1041 if (opt_filter
&& opt_kernel
) {
1042 switch (opt_event_type
) {
1043 case LTTNG_EVENT_ALL
:
1044 case LTTNG_EVENT_TRACEPOINT
:
1045 case LTTNG_EVENT_SYSCALL
:
1047 case LTTNG_EVENT_PROBE
:
1048 case LTTNG_EVENT_USERSPACE_PROBE
:
1049 case LTTNG_EVENT_FUNCTION
:
1050 ERR("Filter expressions are not supported for %s events",
1051 get_event_type_str(opt_event_type
));
1055 ret
= CMD_UNDEFINED
;
1060 channel_name
= opt_channel_name
;
1062 handle
= lttng_create_handle(session_name
, &dom
);
1063 if (handle
== NULL
) {
1070 /* Open a events element */
1071 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
1078 if (opt_enable_all
) {
1079 /* Default setup for enable all */
1081 ev
->type
= opt_event_type
;
1082 strcpy(ev
->name
, "*");
1083 /* kernel loglevels not implemented */
1084 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1086 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1087 strcpy(ev
->name
, "*");
1088 ev
->loglevel_type
= opt_loglevel_type
;
1090 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1091 if (opt_userspace
) {
1092 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1093 } else if (opt_jul
) {
1094 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1095 } else if (opt_log4j
) {
1096 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1097 } else if (opt_python
) {
1098 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1100 if (ev
->loglevel
== -1) {
1101 ERR("Unknown loglevel %s", opt_loglevel
);
1102 ret
= -LTTNG_ERR_INVALID
;
1106 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1107 if (opt_userspace
) {
1109 } else if (opt_jul
) {
1110 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1111 } else if (opt_log4j
) {
1112 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1113 } else if (opt_python
) {
1114 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1120 ret
= create_exclusion_list_and_validate("*",
1121 opt_exclude
, &exclusion_list
);
1128 warn_on_truncated_exclusion_names(exclusion_list
,
1132 ret
= lttng_enable_event_with_exclusions(handle
,
1135 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1139 case LTTNG_ERR_KERN_EVENT_EXIST
:
1140 WARN("Kernel events already enabled (channel %s, session %s)",
1141 print_channel_name(channel_name
), session_name
);
1144 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1146 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1147 ERR("Events: %s (channel %s, session %s)",
1149 print_channel_name(channel_name
),
1155 ERR("Events: %s (channel %s, session %s)",
1156 lttng_strerror(ret
),
1157 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1158 ? print_raw_channel_name(channel_name
)
1159 : print_channel_name(channel_name
),
1167 switch (opt_event_type
) {
1168 case LTTNG_EVENT_TRACEPOINT
:
1169 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1170 char *exclusion_string
= print_exclusions(exclusion_list
);
1172 if (!exclusion_string
) {
1173 PERROR("Cannot allocate exclusion_string");
1177 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1178 get_domain_str(dom
.type
),
1180 print_channel_name(channel_name
),
1182 free(exclusion_string
);
1184 char *exclusion_string
= print_exclusions(exclusion_list
);
1186 if (!exclusion_string
) {
1187 PERROR("Cannot allocate exclusion_string");
1191 MSG("All %s tracepoints%s are enabled in channel %s",
1192 get_domain_str(dom
.type
),
1194 print_channel_name(channel_name
));
1195 free(exclusion_string
);
1198 case LTTNG_EVENT_SYSCALL
:
1200 MSG("All %s system calls are enabled in channel %s",
1201 get_domain_str(dom
.type
),
1202 print_channel_name(channel_name
));
1205 case LTTNG_EVENT_ALL
:
1206 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1207 char *exclusion_string
= print_exclusions(exclusion_list
);
1209 if (!exclusion_string
) {
1210 PERROR("Cannot allocate exclusion_string");
1214 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1215 get_domain_str(dom
.type
),
1217 print_channel_name(channel_name
),
1219 free(exclusion_string
);
1221 char *exclusion_string
= print_exclusions(exclusion_list
);
1223 if (!exclusion_string
) {
1224 PERROR("Cannot allocate exclusion_string");
1228 MSG("All %s events%s are enabled in channel %s",
1229 get_domain_str(dom
.type
),
1231 print_channel_name(channel_name
));
1232 free(exclusion_string
);
1237 * We should not be here since lttng_enable_event should have
1238 * failed on the event type.
1245 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1247 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1249 if (command_ret
< 0) {
1250 switch (-command_ret
) {
1251 case LTTNG_ERR_FILTER_EXIST
:
1252 WARN("Filter on all events is already enabled"
1253 " (channel %s, session %s)",
1254 print_channel_name(channel_name
), session_name
);
1257 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1259 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1260 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1262 print_channel_name(channel_name
),
1263 session_name
, opt_filter
);
1268 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1269 lttng_strerror(command_ret
),
1270 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1271 ? print_raw_channel_name(channel_name
)
1272 : print_channel_name(channel_name
),
1273 session_name
, opt_filter
);
1277 error_holder
= command_ret
;
1280 MSG("Filter '%s' successfully set", opt_filter
);
1285 /* The wildcard * is used for kernel and ust domain to
1286 * represent ALL. We copy * in event name to force the wildcard use
1289 * Note: this is strictly for semantic and printing while in
1290 * machine interface mode.
1292 strcpy(ev
->name
, "*");
1294 /* If we reach here the events are enabled */
1295 if (!error
&& !warn
) {
1301 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1307 /* print exclusion */
1308 ret
= mi_print_exclusion(exclusion_list
);
1315 ret
= mi_lttng_writer_write_element_bool(writer
,
1316 mi_lttng_element_command_success
, success
);
1322 /* Close event element */
1323 ret
= mi_lttng_writer_close_element(writer
);
1333 /* Strip event list */
1334 event_name
= strtok(opt_event_list
, ",");
1335 while (event_name
!= NULL
) {
1336 /* Copy name and type of the event */
1337 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1338 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1339 ev
->type
= opt_event_type
;
1341 /* Kernel tracer action */
1343 DBG("Enabling kernel event %s for channel %s",
1345 print_channel_name(channel_name
));
1347 switch (opt_event_type
) {
1348 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1349 /* If event name differs from *, select tracepoint. */
1350 if (strcmp(ev
->name
, "*")) {
1351 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1354 case LTTNG_EVENT_TRACEPOINT
:
1356 case LTTNG_EVENT_PROBE
:
1357 ret
= parse_probe_opts(ev
, opt_probe
);
1359 ERR("Unable to parse probe options");
1364 case LTTNG_EVENT_USERSPACE_PROBE
:
1365 ret
= parse_userspace_probe_opts(ev
, opt_userspace_probe
);
1368 case CMD_UNSUPPORTED
:
1370 * Error message describing
1371 * what is not supported was
1372 * printed in the function.
1377 ERR("Unable to parse userspace probe options");
1383 case LTTNG_EVENT_FUNCTION
:
1384 ret
= parse_probe_opts(ev
, opt_function
);
1386 ERR("Unable to parse function probe options");
1391 case LTTNG_EVENT_SYSCALL
:
1392 ev
->type
= LTTNG_EVENT_SYSCALL
;
1395 ret
= CMD_UNDEFINED
;
1399 /* kernel loglevels not implemented */
1400 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1401 } else if (opt_userspace
) { /* User-space tracer action */
1402 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1403 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1405 switch (opt_event_type
) {
1406 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1408 case LTTNG_EVENT_TRACEPOINT
:
1409 /* Copy name and type of the event */
1410 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1411 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1412 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1414 case LTTNG_EVENT_PROBE
:
1415 case LTTNG_EVENT_FUNCTION
:
1416 case LTTNG_EVENT_SYSCALL
:
1417 case LTTNG_EVENT_USERSPACE_PROBE
:
1419 ERR("Event type not available for user-space tracing");
1420 ret
= CMD_UNSUPPORTED
;
1426 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1427 ERR("Exclusion option can only be used with tracepoint events");
1431 /* Free previously allocated items */
1432 strutils_free_null_terminated_array_of_strings(
1434 exclusion_list
= NULL
;
1435 ret
= create_exclusion_list_and_validate(
1436 event_name
, opt_exclude
,
1443 warn_on_truncated_exclusion_names(
1444 exclusion_list
, &warn
);
1447 ev
->loglevel_type
= opt_loglevel_type
;
1449 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1450 if (ev
->loglevel
== -1) {
1451 ERR("Unknown loglevel %s", opt_loglevel
);
1452 ret
= -LTTNG_ERR_INVALID
;
1458 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1459 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1460 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1461 ERR("Event type not supported for domain.");
1462 ret
= CMD_UNSUPPORTED
;
1466 ev
->loglevel_type
= opt_loglevel_type
;
1469 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1470 } else if (opt_log4j
) {
1471 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1472 } else if (opt_python
) {
1473 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1475 if (ev
->loglevel
== -1) {
1476 ERR("Unknown loglevel %s", opt_loglevel
);
1477 ret
= -LTTNG_ERR_INVALID
;
1482 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1483 } else if (opt_log4j
) {
1484 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1485 } else if (opt_python
) {
1486 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1489 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1490 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1491 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1497 char *exclusion_string
;
1499 command_ret
= lttng_enable_event_with_exclusions(handle
,
1502 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1504 exclusion_string
= print_exclusions(exclusion_list
);
1505 if (!exclusion_string
) {
1506 PERROR("Cannot allocate exclusion_string");
1510 if (command_ret
< 0) {
1511 /* Turn ret to positive value to handle the positive error code */
1512 switch (-command_ret
) {
1513 case LTTNG_ERR_KERN_EVENT_EXIST
:
1514 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1517 print_channel_name(channel_name
), session_name
);
1520 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1522 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1523 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1526 print_channel_name(channel_name
),
1531 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1532 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1533 event_name
, print_channel_name(channel_name
),
1538 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1540 lttng_strerror(command_ret
),
1541 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1542 ? print_raw_channel_name(channel_name
)
1543 : print_channel_name(channel_name
),
1548 error_holder
= command_ret
;
1551 case LTTNG_DOMAIN_KERNEL
:
1552 case LTTNG_DOMAIN_UST
:
1553 MSG("%s event %s%s created in channel %s",
1554 get_domain_str(dom
.type
),
1557 print_channel_name(channel_name
));
1559 case LTTNG_DOMAIN_JUL
:
1560 case LTTNG_DOMAIN_LOG4J
:
1561 case LTTNG_DOMAIN_PYTHON
:
1563 * Don't print the default channel
1564 * name for agent domains.
1566 MSG("%s event %s%s enabled",
1567 get_domain_str(dom
.type
),
1575 free(exclusion_string
);
1579 char *exclusion_string
;
1581 /* Filter present */
1584 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1586 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1588 exclusion_string
= print_exclusions(exclusion_list
);
1589 if (!exclusion_string
) {
1590 PERROR("Cannot allocate exclusion_string");
1594 if (command_ret
< 0) {
1595 switch (-command_ret
) {
1596 case LTTNG_ERR_FILTER_EXIST
:
1597 WARN("Filter on event %s%s is already enabled"
1598 " (channel %s, session %s)",
1601 print_channel_name(channel_name
), session_name
);
1604 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1606 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1607 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1610 print_channel_name(channel_name
),
1611 session_name
, opt_filter
);
1616 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1618 lttng_strerror(command_ret
),
1619 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1620 ? print_raw_channel_name(channel_name
)
1621 : print_channel_name(channel_name
),
1622 session_name
, opt_filter
);
1626 error_holder
= command_ret
;
1629 MSG("Event %s%s: Filter '%s' successfully set",
1630 event_name
, exclusion_string
,
1633 free(exclusion_string
);
1644 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1650 /* print exclusion */
1651 ret
= mi_print_exclusion(exclusion_list
);
1658 ret
= mi_lttng_writer_write_element_bool(writer
,
1659 mi_lttng_element_command_success
, success
);
1665 /* Close event element */
1666 ret
= mi_lttng_writer_close_element(writer
);
1674 event_name
= strtok(NULL
, ",");
1675 /* Reset warn, error and success */
1682 /* Close events element */
1683 ret
= mi_lttng_writer_close_element(writer
);
1696 lttng_destroy_handle(handle
);
1697 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1699 /* Overwrite ret with error_holder if there was an actual error with
1700 * enabling an event.
1702 ret
= error_holder
? error_holder
: ret
;
1704 lttng_event_destroy(ev
);
1709 * Add event to trace session
1711 int cmd_enable_events(int argc
, const char **argv
)
1713 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1714 static poptContext pc
;
1715 char *session_name
= NULL
;
1716 const char *leftover
= NULL
;
1717 int event_type
= -1;
1719 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1720 poptReadDefaultConfig(pc
, 0);
1722 /* Default event type */
1723 opt_event_type
= LTTNG_EVENT_ALL
;
1725 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1730 case OPT_TRACEPOINT
:
1731 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1734 opt_event_type
= LTTNG_EVENT_PROBE
;
1736 case OPT_USERSPACE_PROBE
:
1737 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1740 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1743 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1749 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1750 opt_loglevel
= poptGetOptArg(pc
);
1752 case OPT_LOGLEVEL_ONLY
:
1753 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1754 opt_loglevel
= poptGetOptArg(pc
);
1756 case OPT_LIST_OPTIONS
:
1757 list_cmd_options(stdout
, long_options
);
1764 ret
= CMD_UNDEFINED
;
1768 /* Validate event type. Multiple event type are not supported. */
1769 if (event_type
== -1) {
1770 event_type
= opt_event_type
;
1772 if (event_type
!= opt_event_type
) {
1773 ERR("Multiple event type not supported.");
1780 ret
= print_missing_or_multiple_domains(
1781 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1789 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1791 ret
= -LTTNG_ERR_NOMEM
;
1795 /* Open command element */
1796 ret
= mi_lttng_writer_command_open(writer
,
1797 mi_lttng_element_command_enable_event
);
1803 /* Open output element */
1804 ret
= mi_lttng_writer_open_element(writer
,
1805 mi_lttng_element_command_output
);
1812 opt_event_list
= (char*) poptGetArg(pc
);
1813 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1814 ERR("Missing event name(s).\n");
1819 leftover
= poptGetArg(pc
);
1821 ERR("Unknown argument: %s", leftover
);
1826 if (!opt_session_name
) {
1827 session_name
= get_session_name();
1828 if (session_name
== NULL
) {
1829 command_ret
= CMD_ERROR
;
1834 session_name
= opt_session_name
;
1837 command_ret
= enable_events(session_name
);
1846 /* Close output element */
1847 ret
= mi_lttng_writer_close_element(writer
);
1853 ret
= mi_lttng_writer_write_element_bool(writer
,
1854 mi_lttng_element_command_success
, success
);
1860 /* Command element close */
1861 ret
= mi_lttng_writer_command_close(writer
);
1870 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1871 /* Preserve original error code */
1872 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1875 if (opt_session_name
== NULL
) {
1879 /* Overwrite ret if an error occurred in enable_events */
1880 ret
= command_ret
? command_ret
: ret
;
1882 poptFreeContext(pc
);