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
)
308 unsigned long addr
= 0;
312 /* Check if the symbol field is an hex address. */
313 ret
= sscanf(symbol
, "0x%lx", &addr
);
315 /* If there is a match, print a warning and return an error. */
316 ERR("Userspace probe on address not supported yet.");
317 ret
= CMD_UNSUPPORTED
;
321 /* Check if the symbol field is an decimal address. */
322 ret
= sscanf(symbol
, "%lu", &addr
);
324 /* If there is a match, print a warning and return an error. */
325 ERR("Userspace probe on address not supported yet.");
326 ret
= CMD_UNSUPPORTED
;
330 /* Check if the symbol field is symbol+hex_offset. */
331 ret
= sscanf(symbol
, "%*[^+]+0x%lx", &offset
);
333 /* If there is a match, print a warning and return an error. */
334 ERR("Userspace probe on symbol+offset not supported yet.");
335 ret
= CMD_UNSUPPORTED
;
339 /* Check if the symbol field is symbol+decimal_offset. */
340 ret
= sscanf(symbol
, "%*[^+]+%lu", &offset
);
342 /* If there is a match, print a warning and return an error. */
343 ERR("Userspace probe on symbol+offset not supported yet.");
344 ret
= CMD_UNSUPPORTED
;
355 * Parse userspace probe options
356 * Set the userspace probe fields in the lttng_event struct and set the
357 * target_path to the path to the binary.
359 static int parse_userspace_probe_opts(struct lttng_event
*ev
, char *opt
)
361 int ret
= CMD_SUCCESS
;
364 char *target_path
= NULL
;
365 char *unescaped_target_path
= NULL
;
366 char *real_target_path
= NULL
;
367 char *symbol_name
= NULL
, *probe_name
= NULL
, *provider_name
= NULL
;
368 struct lttng_userspace_probe_location
*probe_location
= NULL
;
369 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
378 case LTTNG_EVENT_USERSPACE_PROBE
:
385 * userspace probe fields are separated by ':'.
387 tokens
= strutils_split(opt
, ':', 1);
388 num_token
= strutils_array_of_strings_len(tokens
);
391 * Early sanity check that the number of parameter is between 2 and 4
394 * std:PATH:PROVIDER_NAME:PROBE_NAME
395 * PATH:SYMBOL (same behavior as ELF)
397 if (num_token
< 2 || num_token
> 4) {
403 * Looking up the first parameter will tell the technique to use to
404 * interpret the userspace probe/function description.
408 /* When the probe type is omitted we assume ELF for now. */
410 if (num_token
== 3 && strcmp(tokens
[0], "elf") == 0) {
411 target_path
= tokens
[1];
412 symbol_name
= tokens
[2];
413 } else if (num_token
== 2) {
414 target_path
= tokens
[0];
415 symbol_name
= tokens
[1];
421 lttng_userspace_probe_location_lookup_method_function_elf_create();
422 if (!lookup_method
) {
423 WARN("Failed to create ELF lookup method");
429 if (strcmp(tokens
[0], "sdt") == 0) {
430 target_path
= tokens
[1];
431 provider_name
= tokens
[2];
432 probe_name
= tokens
[3];
438 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
439 if (!lookup_method
) {
440 WARN("Failed to create ELF lookup method");
450 /* strutils_unescape_string allocates a new char *. */
451 unescaped_target_path
= strutils_unescape_string(target_path
, 0);
452 if (!unescaped_target_path
) {
454 goto end_destroy_lookup_method
;
458 * If there is not forward slash in the path. Walk the $PATH else
461 if (strchr(unescaped_target_path
, '/') == NULL
) {
462 /* Walk the $PATH variable to find the targeted binary. */
463 real_target_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
464 if (!real_target_path
) {
465 PERROR("Error allocating path buffer");
467 goto end_destroy_lookup_method
;
469 ret
= walk_command_search_path(unescaped_target_path
, real_target_path
);
471 ERR("Binary not found.");
473 goto end_destroy_lookup_method
;
477 * Expand references to `/./` and `/../`. This function does not check
478 * if the file exists. This call returns an allocated buffer on
481 real_target_path
= utils_expand_path_keep_symlink(unescaped_target_path
);
482 if (!real_target_path
) {
483 ERR("Error expanding the path to binary.");
485 goto end_destroy_lookup_method
;
489 * Check if the file exists using access(2). If it does not, walk the
492 ret
= access(real_target_path
, F_OK
);
495 goto end_destroy_lookup_method
;
499 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method
)) {
500 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
502 * Check for common mistakes in userspace probe description syntax.
504 ret
= warn_userspace_probe_syntax(symbol_name
);
506 goto end_destroy_lookup_method
;
509 probe_location
= lttng_userspace_probe_location_function_create(
510 real_target_path
, symbol_name
, lookup_method
);
511 if (!probe_location
) {
512 WARN("Failed to create function probe location");
514 goto end_destroy_lookup_method
;
517 /* Ownership transferred to probe_location. */
518 lookup_method
= NULL
;
520 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
522 WARN("Failed to set probe location on event");
524 goto end_destroy_location
;
527 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
528 probe_location
= lttng_userspace_probe_location_tracepoint_create(
529 real_target_path
, provider_name
, probe_name
, lookup_method
);
530 if (!probe_location
) {
531 WARN("Failed to create function probe location");
533 goto end_destroy_lookup_method
;
536 /* Ownership transferred to probe_location. */
537 lookup_method
= NULL
;
539 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
541 WARN("Failed to set probe location on event");
543 goto end_destroy_location
;
548 goto end_destroy_lookup_method
;
551 /* Successful parsing, now clean up everything and return. */
554 end_destroy_location
:
555 lttng_userspace_probe_location_destroy(probe_location
);
556 end_destroy_lookup_method
:
557 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
559 strutils_free_null_terminated_array_of_strings(tokens
);
561 * Freeing both char * here makes the error handling simplier. free()
562 * performs not action if the pointer is NULL.
564 free(real_target_path
);
565 free(unescaped_target_path
);
571 * Maps LOG4j loglevel from string to value
573 static int loglevel_log4j_str_to_value(const char *inputstr
)
576 char str
[LTTNG_SYMBOL_NAME_LEN
];
578 if (!inputstr
|| strlen(inputstr
) == 0) {
583 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
584 * added at the end of the loop so a the upper bound we avoid the overflow.
586 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
587 str
[i
] = toupper(inputstr
[i
]);
592 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
593 return LTTNG_LOGLEVEL_LOG4J_OFF
;
594 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
595 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
596 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
597 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
598 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
599 return LTTNG_LOGLEVEL_LOG4J_WARN
;
600 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
601 return LTTNG_LOGLEVEL_LOG4J_INFO
;
602 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
603 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
604 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
605 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
606 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
607 return LTTNG_LOGLEVEL_LOG4J_ALL
;
614 * Maps JUL loglevel from string to value
616 static int loglevel_jul_str_to_value(const char *inputstr
)
619 char str
[LTTNG_SYMBOL_NAME_LEN
];
621 if (!inputstr
|| strlen(inputstr
) == 0) {
626 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
627 * added at the end of the loop so a the upper bound we avoid the overflow.
629 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
630 str
[i
] = toupper(inputstr
[i
]);
635 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
636 return LTTNG_LOGLEVEL_JUL_OFF
;
637 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
638 return LTTNG_LOGLEVEL_JUL_SEVERE
;
639 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
640 return LTTNG_LOGLEVEL_JUL_WARNING
;
641 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
642 return LTTNG_LOGLEVEL_JUL_INFO
;
643 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
644 return LTTNG_LOGLEVEL_JUL_CONFIG
;
645 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
646 return LTTNG_LOGLEVEL_JUL_FINE
;
647 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
648 return LTTNG_LOGLEVEL_JUL_FINER
;
649 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
650 return LTTNG_LOGLEVEL_JUL_FINEST
;
651 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
652 return LTTNG_LOGLEVEL_JUL_ALL
;
659 * Maps Python loglevel from string to value
661 static int loglevel_python_str_to_value(const char *inputstr
)
664 char str
[LTTNG_SYMBOL_NAME_LEN
];
666 if (!inputstr
|| strlen(inputstr
) == 0) {
671 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
672 * added at the end of the loop so a the upper bound we avoid the overflow.
674 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
675 str
[i
] = toupper(inputstr
[i
]);
680 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
681 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
682 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
683 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
684 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
685 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
686 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
687 return LTTNG_LOGLEVEL_PYTHON_INFO
;
688 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
689 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
690 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
691 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
698 * Maps loglevel from string to value
701 int loglevel_str_to_value(const char *inputstr
)
704 char str
[LTTNG_SYMBOL_NAME_LEN
];
706 if (!inputstr
|| strlen(inputstr
) == 0) {
711 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
712 * added at the end of the loop so a the upper bound we avoid the overflow.
714 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
715 str
[i
] = toupper(inputstr
[i
]);
719 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
720 return LTTNG_LOGLEVEL_EMERG
;
721 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
722 return LTTNG_LOGLEVEL_ALERT
;
723 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
724 return LTTNG_LOGLEVEL_CRIT
;
725 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
726 return LTTNG_LOGLEVEL_ERR
;
727 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
728 return LTTNG_LOGLEVEL_WARNING
;
729 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
730 return LTTNG_LOGLEVEL_NOTICE
;
731 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
732 return LTTNG_LOGLEVEL_INFO
;
733 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
734 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
735 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
736 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
737 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
738 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
739 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
740 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
741 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
742 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
743 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
744 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
745 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
746 return LTTNG_LOGLEVEL_DEBUG_LINE
;
747 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
748 return LTTNG_LOGLEVEL_DEBUG
;
755 const char *print_channel_name(const char *name
)
757 return name
? : DEFAULT_CHANNEL_NAME
;
761 const char *print_raw_channel_name(const char *name
)
763 return name
? : "<default>";
767 * Mi print exlcusion list
770 int mi_print_exclusion(char **names
)
773 int count
= names
? strutils_array_of_strings_len(names
) : 0;
781 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
786 for (i
= 0; i
< count
; i
++) {
787 ret
= mi_lttng_writer_write_element_string(writer
,
788 config_element_exclusion
, names
[i
]);
794 /* Close exclusions element */
795 ret
= mi_lttng_writer_close_element(writer
);
802 * Return allocated string for pretty-printing exclusion names.
805 char *print_exclusions(char **names
)
809 const char *preamble
= " excluding ";
811 int count
= names
? strutils_array_of_strings_len(names
) : 0;
817 /* calculate total required length */
818 for (i
= 0; i
< count
; i
++) {
819 length
+= strlen(names
[i
]) + 4;
822 /* add length of preamble + one for NUL - one for last (missing) comma */
823 length
+= strlen(preamble
);
824 ret
= zmalloc(length
+ 1);
828 strncpy(ret
, preamble
, length
);
829 for (i
= 0; i
< count
; i
++) {
831 strcat(ret
, names
[i
]);
833 if (i
!= count
- 1) {
842 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
846 const char *e
= event_name
;
847 const char *x
= exclusion
;
849 /* Scan both the excluder and the event letter by letter */
863 /* Event is a subset of the excluder */
864 ERR("Event %s: %s excludes all events from %s",
865 event_name
, exclusion
, event_name
);
871 * Reached the end of the event name before the
872 * end of the exclusion: this is valid.
894 WARN("Event %s: %s does not exclude any events from %s",
895 event_name
, exclusion
, event_name
);
902 int create_exclusion_list_and_validate(const char *event_name
,
903 const char *exclusions_arg
,
904 char ***exclusion_list
)
907 char **exclusions
= NULL
;
909 /* Event name must be a valid globbing pattern to allow exclusions. */
910 if (!strutils_is_star_glob_pattern(event_name
)) {
911 ERR("Event %s: Exclusions can only be used with a globbing pattern",
916 /* Split exclusions. */
917 exclusions
= strutils_split(exclusions_arg
, ',', true);
923 * If the event name is a star-at-end only globbing pattern,
924 * then we can validate the individual exclusions. Otherwise
925 * all exclusions are passed to the session daemon.
927 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
928 char * const *exclusion
;
930 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
931 if (!strutils_is_star_glob_pattern(*exclusion
) ||
932 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
933 ret
= check_exclusion_subsets(event_name
, *exclusion
);
941 *exclusion_list
= exclusions
;
947 strutils_free_null_terminated_array_of_strings(exclusions
);
953 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
956 char * const *exclusion
;
958 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
959 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
960 WARN("Event exclusion \"%s\" will be truncated",
968 * Enabling event using the lttng API.
969 * Note: in case of error only the last error code will be return.
971 static int enable_events(char *session_name
)
973 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
974 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
975 char *event_name
, *channel_name
= NULL
;
976 struct lttng_event
*ev
;
977 struct lttng_domain dom
;
978 char **exclusion_list
= NULL
;
980 memset(&dom
, 0, sizeof(dom
));
982 ev
= lttng_event_create();
990 WARN("Kernel loglevels are not supported.");
994 /* Create lttng domain */
996 dom
.type
= LTTNG_DOMAIN_KERNEL
;
997 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
998 } else if (opt_userspace
) {
999 dom
.type
= LTTNG_DOMAIN_UST
;
1001 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1002 } else if (opt_jul
) {
1003 dom
.type
= LTTNG_DOMAIN_JUL
;
1005 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1006 } else if (opt_log4j
) {
1007 dom
.type
= LTTNG_DOMAIN_LOG4J
;
1009 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1010 } else if (opt_python
) {
1011 dom
.type
= LTTNG_DOMAIN_PYTHON
;
1013 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1015 /* Checked by the caller. */
1021 case LTTNG_DOMAIN_KERNEL
:
1022 case LTTNG_DOMAIN_JUL
:
1023 case LTTNG_DOMAIN_LOG4J
:
1024 case LTTNG_DOMAIN_PYTHON
:
1025 ERR("Event name exclusions are not yet implemented for %s events",
1026 get_domain_str(dom
.type
));
1029 case LTTNG_DOMAIN_UST
:
1030 /* Exclusions supported */
1038 * Adding a filter to a probe, function or userspace-probe would be
1039 * denied by the kernel tracer as it's not supported at the moment. We
1040 * do an early check here to warn the user.
1042 if (opt_filter
&& opt_kernel
) {
1043 switch (opt_event_type
) {
1044 case LTTNG_EVENT_ALL
:
1045 case LTTNG_EVENT_TRACEPOINT
:
1046 case LTTNG_EVENT_SYSCALL
:
1048 case LTTNG_EVENT_PROBE
:
1049 case LTTNG_EVENT_USERSPACE_PROBE
:
1050 case LTTNG_EVENT_FUNCTION
:
1051 ERR("Filter expressions are not supported for %s events",
1052 get_event_type_str(opt_event_type
));
1056 ret
= CMD_UNDEFINED
;
1061 channel_name
= opt_channel_name
;
1063 handle
= lttng_create_handle(session_name
, &dom
);
1064 if (handle
== NULL
) {
1071 /* Open a events element */
1072 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
1079 if (opt_enable_all
) {
1080 /* Default setup for enable all */
1082 ev
->type
= opt_event_type
;
1083 strcpy(ev
->name
, "*");
1084 /* kernel loglevels not implemented */
1085 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1087 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1088 strcpy(ev
->name
, "*");
1089 ev
->loglevel_type
= opt_loglevel_type
;
1091 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1092 if (opt_userspace
) {
1093 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1094 } else if (opt_jul
) {
1095 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1096 } else if (opt_log4j
) {
1097 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1098 } else if (opt_python
) {
1099 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1101 if (ev
->loglevel
== -1) {
1102 ERR("Unknown loglevel %s", opt_loglevel
);
1103 ret
= -LTTNG_ERR_INVALID
;
1107 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1108 if (opt_userspace
) {
1110 } else if (opt_jul
) {
1111 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1112 } else if (opt_log4j
) {
1113 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1114 } else if (opt_python
) {
1115 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1121 ret
= create_exclusion_list_and_validate("*",
1122 opt_exclude
, &exclusion_list
);
1129 warn_on_truncated_exclusion_names(exclusion_list
,
1133 ret
= lttng_enable_event_with_exclusions(handle
,
1136 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1140 case LTTNG_ERR_KERN_EVENT_EXIST
:
1141 WARN("Kernel events already enabled (channel %s, session %s)",
1142 print_channel_name(channel_name
), session_name
);
1145 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1147 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1148 ERR("Events: %s (channel %s, session %s)",
1150 print_channel_name(channel_name
),
1156 ERR("Events: %s (channel %s, session %s)",
1157 lttng_strerror(ret
),
1158 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1159 ? print_raw_channel_name(channel_name
)
1160 : print_channel_name(channel_name
),
1168 switch (opt_event_type
) {
1169 case LTTNG_EVENT_TRACEPOINT
:
1170 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1171 char *exclusion_string
= print_exclusions(exclusion_list
);
1173 if (!exclusion_string
) {
1174 PERROR("Cannot allocate exclusion_string");
1178 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1179 get_domain_str(dom
.type
),
1181 print_channel_name(channel_name
),
1183 free(exclusion_string
);
1185 char *exclusion_string
= print_exclusions(exclusion_list
);
1187 if (!exclusion_string
) {
1188 PERROR("Cannot allocate exclusion_string");
1192 MSG("All %s tracepoints%s are enabled in channel %s",
1193 get_domain_str(dom
.type
),
1195 print_channel_name(channel_name
));
1196 free(exclusion_string
);
1199 case LTTNG_EVENT_SYSCALL
:
1201 MSG("All %s system calls are enabled in channel %s",
1202 get_domain_str(dom
.type
),
1203 print_channel_name(channel_name
));
1206 case LTTNG_EVENT_ALL
:
1207 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1208 char *exclusion_string
= print_exclusions(exclusion_list
);
1210 if (!exclusion_string
) {
1211 PERROR("Cannot allocate exclusion_string");
1215 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1216 get_domain_str(dom
.type
),
1218 print_channel_name(channel_name
),
1220 free(exclusion_string
);
1222 char *exclusion_string
= print_exclusions(exclusion_list
);
1224 if (!exclusion_string
) {
1225 PERROR("Cannot allocate exclusion_string");
1229 MSG("All %s events%s are enabled in channel %s",
1230 get_domain_str(dom
.type
),
1232 print_channel_name(channel_name
));
1233 free(exclusion_string
);
1238 * We should not be here since lttng_enable_event should have
1239 * failed on the event type.
1246 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1248 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1250 if (command_ret
< 0) {
1251 switch (-command_ret
) {
1252 case LTTNG_ERR_FILTER_EXIST
:
1253 WARN("Filter on all events is already enabled"
1254 " (channel %s, session %s)",
1255 print_channel_name(channel_name
), session_name
);
1258 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1260 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1261 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1263 print_channel_name(channel_name
),
1264 session_name
, opt_filter
);
1269 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1270 lttng_strerror(command_ret
),
1271 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1272 ? print_raw_channel_name(channel_name
)
1273 : print_channel_name(channel_name
),
1274 session_name
, opt_filter
);
1278 error_holder
= command_ret
;
1281 MSG("Filter '%s' successfully set", opt_filter
);
1286 /* The wildcard * is used for kernel and ust domain to
1287 * represent ALL. We copy * in event name to force the wildcard use
1290 * Note: this is strictly for semantic and printing while in
1291 * machine interface mode.
1293 strcpy(ev
->name
, "*");
1295 /* If we reach here the events are enabled */
1296 if (!error
&& !warn
) {
1302 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1308 /* print exclusion */
1309 ret
= mi_print_exclusion(exclusion_list
);
1316 ret
= mi_lttng_writer_write_element_bool(writer
,
1317 mi_lttng_element_command_success
, success
);
1323 /* Close event element */
1324 ret
= mi_lttng_writer_close_element(writer
);
1334 /* Strip event list */
1335 event_name
= strtok(opt_event_list
, ",");
1336 while (event_name
!= NULL
) {
1337 /* Copy name and type of the event */
1338 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1339 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1340 ev
->type
= opt_event_type
;
1342 /* Kernel tracer action */
1344 DBG("Enabling kernel event %s for channel %s",
1346 print_channel_name(channel_name
));
1348 switch (opt_event_type
) {
1349 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1350 /* If event name differs from *, select tracepoint. */
1351 if (strcmp(ev
->name
, "*")) {
1352 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1355 case LTTNG_EVENT_TRACEPOINT
:
1357 case LTTNG_EVENT_PROBE
:
1358 ret
= parse_probe_opts(ev
, opt_probe
);
1360 ERR("Unable to parse probe options");
1365 case LTTNG_EVENT_USERSPACE_PROBE
:
1366 ret
= parse_userspace_probe_opts(ev
, opt_userspace_probe
);
1369 case CMD_UNSUPPORTED
:
1371 * Error message describing
1372 * what is not supported was
1373 * printed in the function.
1378 ERR("Unable to parse userspace probe options");
1384 case LTTNG_EVENT_FUNCTION
:
1385 ret
= parse_probe_opts(ev
, opt_function
);
1387 ERR("Unable to parse function probe options");
1392 case LTTNG_EVENT_SYSCALL
:
1393 ev
->type
= LTTNG_EVENT_SYSCALL
;
1396 ret
= CMD_UNDEFINED
;
1400 /* kernel loglevels not implemented */
1401 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1402 } else if (opt_userspace
) { /* User-space tracer action */
1403 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1404 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1406 switch (opt_event_type
) {
1407 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1409 case LTTNG_EVENT_TRACEPOINT
:
1410 /* Copy name and type of the event */
1411 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1412 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1413 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1415 case LTTNG_EVENT_PROBE
:
1416 case LTTNG_EVENT_FUNCTION
:
1417 case LTTNG_EVENT_SYSCALL
:
1418 case LTTNG_EVENT_USERSPACE_PROBE
:
1420 ERR("Event type not available for user-space tracing");
1421 ret
= CMD_UNSUPPORTED
;
1427 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1428 ERR("Exclusion option can only be used with tracepoint events");
1432 /* Free previously allocated items */
1433 strutils_free_null_terminated_array_of_strings(
1435 exclusion_list
= NULL
;
1436 ret
= create_exclusion_list_and_validate(
1437 event_name
, opt_exclude
,
1444 warn_on_truncated_exclusion_names(
1445 exclusion_list
, &warn
);
1448 ev
->loglevel_type
= opt_loglevel_type
;
1450 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1451 if (ev
->loglevel
== -1) {
1452 ERR("Unknown loglevel %s", opt_loglevel
);
1453 ret
= -LTTNG_ERR_INVALID
;
1459 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1460 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1461 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1462 ERR("Event type not supported for domain.");
1463 ret
= CMD_UNSUPPORTED
;
1467 ev
->loglevel_type
= opt_loglevel_type
;
1470 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1471 } else if (opt_log4j
) {
1472 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1473 } else if (opt_python
) {
1474 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1476 if (ev
->loglevel
== -1) {
1477 ERR("Unknown loglevel %s", opt_loglevel
);
1478 ret
= -LTTNG_ERR_INVALID
;
1483 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1484 } else if (opt_log4j
) {
1485 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1486 } else if (opt_python
) {
1487 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1490 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1491 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1492 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1498 char *exclusion_string
;
1500 command_ret
= lttng_enable_event_with_exclusions(handle
,
1503 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1505 exclusion_string
= print_exclusions(exclusion_list
);
1506 if (!exclusion_string
) {
1507 PERROR("Cannot allocate exclusion_string");
1511 if (command_ret
< 0) {
1512 /* Turn ret to positive value to handle the positive error code */
1513 switch (-command_ret
) {
1514 case LTTNG_ERR_KERN_EVENT_EXIST
:
1515 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1518 print_channel_name(channel_name
), session_name
);
1521 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1523 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1524 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1527 print_channel_name(channel_name
),
1532 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1533 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1534 event_name
, print_channel_name(channel_name
),
1539 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1541 lttng_strerror(command_ret
),
1542 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1543 ? print_raw_channel_name(channel_name
)
1544 : print_channel_name(channel_name
),
1549 error_holder
= command_ret
;
1552 case LTTNG_DOMAIN_KERNEL
:
1553 case LTTNG_DOMAIN_UST
:
1554 MSG("%s event %s%s created in channel %s",
1555 get_domain_str(dom
.type
),
1558 print_channel_name(channel_name
));
1560 case LTTNG_DOMAIN_JUL
:
1561 case LTTNG_DOMAIN_LOG4J
:
1562 case LTTNG_DOMAIN_PYTHON
:
1564 * Don't print the default channel
1565 * name for agent domains.
1567 MSG("%s event %s%s enabled",
1568 get_domain_str(dom
.type
),
1576 free(exclusion_string
);
1580 char *exclusion_string
;
1582 /* Filter present */
1585 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1587 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1589 exclusion_string
= print_exclusions(exclusion_list
);
1590 if (!exclusion_string
) {
1591 PERROR("Cannot allocate exclusion_string");
1595 if (command_ret
< 0) {
1596 switch (-command_ret
) {
1597 case LTTNG_ERR_FILTER_EXIST
:
1598 WARN("Filter on event %s%s is already enabled"
1599 " (channel %s, session %s)",
1602 print_channel_name(channel_name
), session_name
);
1605 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1607 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1608 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1611 print_channel_name(channel_name
),
1612 session_name
, opt_filter
);
1617 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1619 lttng_strerror(command_ret
),
1620 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1621 ? print_raw_channel_name(channel_name
)
1622 : print_channel_name(channel_name
),
1623 session_name
, opt_filter
);
1627 error_holder
= command_ret
;
1630 MSG("Event %s%s: Filter '%s' successfully set",
1631 event_name
, exclusion_string
,
1634 free(exclusion_string
);
1645 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1651 /* print exclusion */
1652 ret
= mi_print_exclusion(exclusion_list
);
1659 ret
= mi_lttng_writer_write_element_bool(writer
,
1660 mi_lttng_element_command_success
, success
);
1666 /* Close event element */
1667 ret
= mi_lttng_writer_close_element(writer
);
1675 event_name
= strtok(NULL
, ",");
1676 /* Reset warn, error and success */
1683 /* Close events element */
1684 ret
= mi_lttng_writer_close_element(writer
);
1697 lttng_destroy_handle(handle
);
1698 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1700 /* Overwrite ret with error_holder if there was an actual error with
1701 * enabling an event.
1703 ret
= error_holder
? error_holder
: ret
;
1705 lttng_event_destroy(ev
);
1710 * Add event to trace session
1712 int cmd_enable_events(int argc
, const char **argv
)
1714 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1715 static poptContext pc
;
1716 char *session_name
= NULL
;
1717 const char *leftover
= NULL
;
1718 int event_type
= -1;
1720 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1721 poptReadDefaultConfig(pc
, 0);
1723 /* Default event type */
1724 opt_event_type
= LTTNG_EVENT_ALL
;
1726 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1731 case OPT_TRACEPOINT
:
1732 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1735 opt_event_type
= LTTNG_EVENT_PROBE
;
1737 case OPT_USERSPACE_PROBE
:
1738 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1741 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1744 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1750 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1751 opt_loglevel
= poptGetOptArg(pc
);
1753 case OPT_LOGLEVEL_ONLY
:
1754 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1755 opt_loglevel
= poptGetOptArg(pc
);
1757 case OPT_LIST_OPTIONS
:
1758 list_cmd_options(stdout
, long_options
);
1765 ret
= CMD_UNDEFINED
;
1769 /* Validate event type. Multiple event type are not supported. */
1770 if (event_type
== -1) {
1771 event_type
= opt_event_type
;
1773 if (event_type
!= opt_event_type
) {
1774 ERR("Multiple event type not supported.");
1781 ret
= print_missing_or_multiple_domains(
1782 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1790 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1792 ret
= -LTTNG_ERR_NOMEM
;
1796 /* Open command element */
1797 ret
= mi_lttng_writer_command_open(writer
,
1798 mi_lttng_element_command_enable_event
);
1804 /* Open output element */
1805 ret
= mi_lttng_writer_open_element(writer
,
1806 mi_lttng_element_command_output
);
1813 opt_event_list
= (char*) poptGetArg(pc
);
1814 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1815 ERR("Missing event name(s).\n");
1820 leftover
= poptGetArg(pc
);
1822 ERR("Unknown argument: %s", leftover
);
1827 if (!opt_session_name
) {
1828 session_name
= get_session_name();
1829 if (session_name
== NULL
) {
1830 command_ret
= CMD_ERROR
;
1835 session_name
= opt_session_name
;
1838 command_ret
= enable_events(session_name
);
1847 /* Close output element */
1848 ret
= mi_lttng_writer_close_element(writer
);
1854 ret
= mi_lttng_writer_write_element_bool(writer
,
1855 mi_lttng_element_command_success
, success
);
1861 /* Command element close */
1862 ret
= mi_lttng_writer_command_close(writer
);
1871 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1872 /* Preserve original error code */
1873 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1876 if (opt_session_name
== NULL
) {
1880 /* Overwrite ret if an error occurred in enable_events */
1881 ret
= command_ret
? command_ret
: ret
;
1883 poptFreeContext(pc
);