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]) || 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
);
164 * Return an error if the first character of the tentative
165 * address is NULL or not a digit. It can be "0" if the address
166 * is in hexadecimal and can be 1 to 9 if it's in decimal.
168 if (*s_hex
== '\0' || !isdigit(*s_hex
)) {
169 ERR("Invalid probe description %s", s_hex
);
173 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
174 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
175 ev
->attr
.probe
.offset
= 0;
176 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
188 * Walk the directories in the PATH environment variable to find the target
189 * binary passed as parameter.
191 * On success, the full path of the binary is copied in binary_full_path out
192 * parameter. This buffer is allocated by the caller and must be at least
193 * LTTNG_PATH_MAX bytes long.
194 * On failure, returns -1;
196 static int walk_command_search_path(const char *binary
, char *binary_full_path
)
198 char *tentative_binary_path
= NULL
;
199 char *command_search_path
= NULL
;
200 char *curr_search_dir_end
= NULL
;
201 char *curr_search_dir
= NULL
;
202 struct stat stat_output
;
205 command_search_path
= lttng_secure_getenv("PATH");
206 if (!command_search_path
) {
212 * Duplicate the $PATH string as the char pointer returned by getenv() should
215 command_search_path
= strdup(command_search_path
);
216 if (!command_search_path
) {
222 * This char array is used to concatenate path to binary to look for
225 tentative_binary_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
226 if (!tentative_binary_path
) {
231 curr_search_dir
= command_search_path
;
234 * Split on ':'. The return value of this call points to the
235 * matching character.
237 curr_search_dir_end
= strchr(curr_search_dir
, ':');
238 if (curr_search_dir_end
!= NULL
) {
240 * Add a NULL byte to the end of the first token so it
241 * can be used as a string.
243 curr_search_dir_end
[0] = '\0';
246 /* Empty the tentative path */
247 memset(tentative_binary_path
, 0, LTTNG_PATH_MAX
* sizeof(char));
250 * Build the tentative path to the binary using the current
251 * search directory and the name of the binary.
253 ret
= snprintf(tentative_binary_path
, LTTNG_PATH_MAX
, "%s/%s",
254 curr_search_dir
, binary
);
256 goto free_binary_path
;
258 if (ret
< LTTNG_PATH_MAX
) {
260 * Use STAT(2) to see if the file exists.
262 ret
= stat(tentative_binary_path
, &stat_output
);
265 * Verify that it is a regular file or a
266 * symlink and not a special file (e.g.
269 if (S_ISREG(stat_output
.st_mode
)
270 || S_ISLNK(stat_output
.st_mode
)) {
272 * Found a match, set the out parameter
273 * and return success.
275 ret
= lttng_strncpy(binary_full_path
,
276 tentative_binary_path
,
279 ERR("Source path does not fit "
280 "in destination buffer.");
282 goto free_binary_path
;
286 /* Go to the next entry in the $PATH variable. */
287 curr_search_dir
= curr_search_dir_end
+ 1;
288 } while (curr_search_dir_end
!= NULL
);
291 free(tentative_binary_path
);
293 free(command_search_path
);
299 * Check if the symbol field passed by the user is in fact an address or an
300 * offset from a symbol. Those two instrumentation types are not supported yet.
301 * It's expected to be a common mistake because of the existing --probe option
302 * that does support these formats.
304 * Here are examples of these unsupported formats for the --userspace-probe
306 * elf:/path/to/binary:0x400430
307 * elf:/path/to/binary:4194364
308 * elf:/path/to/binary:my_symbol+0x323
309 * elf:/path/to/binary:my_symbol+43
311 static int warn_userspace_probe_syntax(const char *symbol
)
315 /* Check if the symbol field is an hex address. */
316 ret
= sscanf(symbol
, "0x%*x");
318 /* If there is a match, print a warning and return an error. */
319 ERR("Userspace probe on address not supported yet.");
320 ret
= CMD_UNSUPPORTED
;
324 /* Check if the symbol field is an decimal address. */
325 ret
= sscanf(symbol
, "%*u");
327 /* If there is a match, print a warning and return an error. */
328 ERR("Userspace probe on address not supported yet.");
329 ret
= CMD_UNSUPPORTED
;
333 /* Check if the symbol field is symbol+hex_offset. */
334 ret
= sscanf(symbol
, "%*[^+]+0x%*x");
336 /* If there is a match, print a warning and return an error. */
337 ERR("Userspace probe on symbol+offset not supported yet.");
338 ret
= CMD_UNSUPPORTED
;
342 /* Check if the symbol field is symbol+decimal_offset. */
343 ret
= sscanf(symbol
, "%*[^+]+%*u");
345 /* If there is a match, print a warning and return an error. */
346 ERR("Userspace probe on symbol+offset not supported yet.");
347 ret
= CMD_UNSUPPORTED
;
358 * Parse userspace probe options
359 * Set the userspace probe fields in the lttng_event struct and set the
360 * target_path to the path to the binary.
362 static int parse_userspace_probe_opts(struct lttng_event
*ev
, char *opt
)
364 int ret
= CMD_SUCCESS
;
367 char *target_path
= NULL
;
368 char *unescaped_target_path
= NULL
;
369 char *real_target_path
= NULL
;
370 char *symbol_name
= NULL
, *probe_name
= NULL
, *provider_name
= NULL
;
371 struct lttng_userspace_probe_location
*probe_location
= NULL
;
372 struct lttng_userspace_probe_location_lookup_method
*lookup_method
=
381 case LTTNG_EVENT_USERSPACE_PROBE
:
388 * userspace probe fields are separated by ':'.
390 tokens
= strutils_split(opt
, ':', 1);
391 num_token
= strutils_array_of_strings_len(tokens
);
394 * Early sanity check that the number of parameter is between 2 and 4
397 * std:PATH:PROVIDER_NAME:PROBE_NAME
398 * PATH:SYMBOL (same behavior as ELF)
400 if (num_token
< 2 || num_token
> 4) {
406 * Looking up the first parameter will tell the technique to use to
407 * interpret the userspace probe/function description.
411 /* When the probe type is omitted we assume ELF for now. */
413 if (num_token
== 3 && strcmp(tokens
[0], "elf") == 0) {
414 target_path
= tokens
[1];
415 symbol_name
= tokens
[2];
416 } else if (num_token
== 2) {
417 target_path
= tokens
[0];
418 symbol_name
= tokens
[1];
424 lttng_userspace_probe_location_lookup_method_function_elf_create();
425 if (!lookup_method
) {
426 WARN("Failed to create ELF lookup method");
432 if (strcmp(tokens
[0], "sdt") == 0) {
433 target_path
= tokens
[1];
434 provider_name
= tokens
[2];
435 probe_name
= tokens
[3];
441 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
442 if (!lookup_method
) {
443 WARN("Failed to create SDT lookup method");
453 /* strutils_unescape_string allocates a new char *. */
454 unescaped_target_path
= strutils_unescape_string(target_path
, 0);
455 if (!unescaped_target_path
) {
457 goto end_destroy_lookup_method
;
461 * If there is not forward slash in the path. Walk the $PATH else
464 if (strchr(unescaped_target_path
, '/') == NULL
) {
465 /* Walk the $PATH variable to find the targeted binary. */
466 real_target_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
467 if (!real_target_path
) {
468 PERROR("Error allocating path buffer");
470 goto end_destroy_lookup_method
;
472 ret
= walk_command_search_path(unescaped_target_path
, real_target_path
);
474 ERR("Binary not found.");
476 goto end_destroy_lookup_method
;
480 * Expand references to `/./` and `/../`. This function does not check
481 * if the file exists. This call returns an allocated buffer on
484 real_target_path
= utils_expand_path_keep_symlink(unescaped_target_path
);
485 if (!real_target_path
) {
486 ERR("Error expanding the path to binary.");
488 goto end_destroy_lookup_method
;
492 * Check if the file exists using access(2), If it does not,
495 ret
= access(real_target_path
, F_OK
);
497 ERR("Cannot find binary at path: %s.", real_target_path
);
499 goto end_destroy_lookup_method
;
503 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method
)) {
504 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
506 * Check for common mistakes in userspace probe description syntax.
508 ret
= warn_userspace_probe_syntax(symbol_name
);
510 goto end_destroy_lookup_method
;
513 probe_location
= lttng_userspace_probe_location_function_create(
514 real_target_path
, symbol_name
, lookup_method
);
515 if (!probe_location
) {
516 WARN("Failed to create function probe location");
518 goto end_destroy_lookup_method
;
521 /* Ownership transferred to probe_location. */
522 lookup_method
= NULL
;
524 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
526 WARN("Failed to set probe location on event");
528 goto end_destroy_location
;
531 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
532 probe_location
= lttng_userspace_probe_location_tracepoint_create(
533 real_target_path
, provider_name
, probe_name
, lookup_method
);
534 if (!probe_location
) {
535 WARN("Failed to create function probe location");
537 goto end_destroy_lookup_method
;
540 /* Ownership transferred to probe_location. */
541 lookup_method
= NULL
;
543 ret
= lttng_event_set_userspace_probe_location(ev
, probe_location
);
545 WARN("Failed to set probe location on event");
547 goto end_destroy_location
;
552 goto end_destroy_lookup_method
;
555 /* Successful parsing, now clean up everything and return. */
558 end_destroy_location
:
559 lttng_userspace_probe_location_destroy(probe_location
);
560 end_destroy_lookup_method
:
561 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
563 strutils_free_null_terminated_array_of_strings(tokens
);
565 * Freeing both char * here makes the error handling simplier. free()
566 * performs not action if the pointer is NULL.
568 free(real_target_path
);
569 free(unescaped_target_path
);
575 * Maps LOG4j loglevel from string to value
577 static int loglevel_log4j_str_to_value(const char *inputstr
)
580 char str
[LTTNG_SYMBOL_NAME_LEN
];
582 if (!inputstr
|| strlen(inputstr
) == 0) {
587 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
588 * added at the end of the loop so a the upper bound we avoid the overflow.
590 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
591 str
[i
] = toupper(inputstr
[i
]);
596 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
597 return LTTNG_LOGLEVEL_LOG4J_OFF
;
598 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
599 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
600 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
601 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
602 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
603 return LTTNG_LOGLEVEL_LOG4J_WARN
;
604 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
605 return LTTNG_LOGLEVEL_LOG4J_INFO
;
606 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
607 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
608 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
609 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
610 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
611 return LTTNG_LOGLEVEL_LOG4J_ALL
;
618 * Maps JUL loglevel from string to value
620 static int loglevel_jul_str_to_value(const char *inputstr
)
623 char str
[LTTNG_SYMBOL_NAME_LEN
];
625 if (!inputstr
|| strlen(inputstr
) == 0) {
630 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
631 * added at the end of the loop so a the upper bound we avoid the overflow.
633 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
634 str
[i
] = toupper(inputstr
[i
]);
639 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
640 return LTTNG_LOGLEVEL_JUL_OFF
;
641 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
642 return LTTNG_LOGLEVEL_JUL_SEVERE
;
643 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
644 return LTTNG_LOGLEVEL_JUL_WARNING
;
645 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
646 return LTTNG_LOGLEVEL_JUL_INFO
;
647 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
648 return LTTNG_LOGLEVEL_JUL_CONFIG
;
649 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
650 return LTTNG_LOGLEVEL_JUL_FINE
;
651 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
652 return LTTNG_LOGLEVEL_JUL_FINER
;
653 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
654 return LTTNG_LOGLEVEL_JUL_FINEST
;
655 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
656 return LTTNG_LOGLEVEL_JUL_ALL
;
663 * Maps Python loglevel from string to value
665 static int loglevel_python_str_to_value(const char *inputstr
)
668 char str
[LTTNG_SYMBOL_NAME_LEN
];
670 if (!inputstr
|| strlen(inputstr
) == 0) {
675 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
676 * added at the end of the loop so a the upper bound we avoid the overflow.
678 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
679 str
[i
] = toupper(inputstr
[i
]);
684 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
685 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
686 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
687 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
688 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
689 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
690 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
691 return LTTNG_LOGLEVEL_PYTHON_INFO
;
692 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
693 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
694 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
695 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
702 * Maps loglevel from string to value
705 int loglevel_str_to_value(const char *inputstr
)
708 char str
[LTTNG_SYMBOL_NAME_LEN
];
710 if (!inputstr
|| strlen(inputstr
) == 0) {
715 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
716 * added at the end of the loop so a the upper bound we avoid the overflow.
718 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
719 str
[i
] = toupper(inputstr
[i
]);
723 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
724 return LTTNG_LOGLEVEL_EMERG
;
725 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
726 return LTTNG_LOGLEVEL_ALERT
;
727 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
728 return LTTNG_LOGLEVEL_CRIT
;
729 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
730 return LTTNG_LOGLEVEL_ERR
;
731 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
732 return LTTNG_LOGLEVEL_WARNING
;
733 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
734 return LTTNG_LOGLEVEL_NOTICE
;
735 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
736 return LTTNG_LOGLEVEL_INFO
;
737 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
738 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
739 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
740 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
741 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
742 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
743 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
744 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
745 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
746 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
747 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
748 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
749 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
750 return LTTNG_LOGLEVEL_DEBUG_LINE
;
751 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
752 return LTTNG_LOGLEVEL_DEBUG
;
759 const char *print_channel_name(const char *name
)
761 return name
? : DEFAULT_CHANNEL_NAME
;
765 const char *print_raw_channel_name(const char *name
)
767 return name
? : "<default>";
771 * Mi print exlcusion list
774 int mi_print_exclusion(char **names
)
777 int count
= names
? strutils_array_of_strings_len(names
) : 0;
785 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
790 for (i
= 0; i
< count
; i
++) {
791 ret
= mi_lttng_writer_write_element_string(writer
,
792 config_element_exclusion
, names
[i
]);
798 /* Close exclusions element */
799 ret
= mi_lttng_writer_close_element(writer
);
806 * Return allocated string for pretty-printing exclusion names.
809 char *print_exclusions(char **names
)
813 const char preamble
[] = " excluding ";
815 int count
= names
? strutils_array_of_strings_len(names
) : 0;
821 /* calculate total required length */
822 for (i
= 0; i
< count
; i
++) {
823 length
+= strlen(names
[i
]) + 4;
826 length
+= sizeof(preamble
);
827 ret
= zmalloc(length
);
831 strncpy(ret
, preamble
, length
);
832 for (i
= 0; i
< count
; i
++) {
834 strcat(ret
, names
[i
]);
836 if (i
!= count
- 1) {
845 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
849 const char *e
= event_name
;
850 const char *x
= exclusion
;
852 /* Scan both the excluder and the event letter by letter */
866 /* Event is a subset of the excluder */
867 ERR("Event %s: %s excludes all events from %s",
868 event_name
, exclusion
, event_name
);
874 * Reached the end of the event name before the
875 * end of the exclusion: this is valid.
897 WARN("Event %s: %s does not exclude any events from %s",
898 event_name
, exclusion
, event_name
);
905 int create_exclusion_list_and_validate(const char *event_name
,
906 const char *exclusions_arg
,
907 char ***exclusion_list
)
910 char **exclusions
= NULL
;
912 /* Event name must be a valid globbing pattern to allow exclusions. */
913 if (!strutils_is_star_glob_pattern(event_name
)) {
914 ERR("Event %s: Exclusions can only be used with a globbing pattern",
919 /* Split exclusions. */
920 exclusions
= strutils_split(exclusions_arg
, ',', true);
926 * If the event name is a star-at-end only globbing pattern,
927 * then we can validate the individual exclusions. Otherwise
928 * all exclusions are passed to the session daemon.
930 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
931 char * const *exclusion
;
933 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
934 if (!strutils_is_star_glob_pattern(*exclusion
) ||
935 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
936 ret
= check_exclusion_subsets(event_name
, *exclusion
);
944 *exclusion_list
= exclusions
;
950 strutils_free_null_terminated_array_of_strings(exclusions
);
956 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
959 char * const *exclusion
;
961 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
962 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
963 WARN("Event exclusion \"%s\" will be truncated",
971 * Enabling event using the lttng API.
972 * Note: in case of error only the last error code will be return.
974 static int enable_events(char *session_name
)
976 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
977 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
978 char *event_name
, *channel_name
= NULL
;
979 struct lttng_event
*ev
;
980 struct lttng_domain dom
;
981 char **exclusion_list
= NULL
;
983 memset(&dom
, 0, sizeof(dom
));
985 ev
= lttng_event_create();
993 WARN("Kernel loglevels are not supported.");
997 /* Create lttng domain */
999 dom
.type
= LTTNG_DOMAIN_KERNEL
;
1000 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
1001 } else if (opt_userspace
) {
1002 dom
.type
= LTTNG_DOMAIN_UST
;
1004 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1005 } else if (opt_jul
) {
1006 dom
.type
= LTTNG_DOMAIN_JUL
;
1008 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1009 } else if (opt_log4j
) {
1010 dom
.type
= LTTNG_DOMAIN_LOG4J
;
1012 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1013 } else if (opt_python
) {
1014 dom
.type
= LTTNG_DOMAIN_PYTHON
;
1016 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
1018 /* Checked by the caller. */
1024 case LTTNG_DOMAIN_KERNEL
:
1025 case LTTNG_DOMAIN_JUL
:
1026 case LTTNG_DOMAIN_LOG4J
:
1027 case LTTNG_DOMAIN_PYTHON
:
1028 ERR("Event name exclusions are not yet implemented for %s events",
1029 get_domain_str(dom
.type
));
1032 case LTTNG_DOMAIN_UST
:
1033 /* Exclusions supported */
1041 * Adding a filter to a probe, function or userspace-probe would be
1042 * denied by the kernel tracer as it's not supported at the moment. We
1043 * do an early check here to warn the user.
1045 if (opt_filter
&& opt_kernel
) {
1046 switch (opt_event_type
) {
1047 case LTTNG_EVENT_ALL
:
1048 case LTTNG_EVENT_TRACEPOINT
:
1049 case LTTNG_EVENT_SYSCALL
:
1051 case LTTNG_EVENT_PROBE
:
1052 case LTTNG_EVENT_USERSPACE_PROBE
:
1053 case LTTNG_EVENT_FUNCTION
:
1054 ERR("Filter expressions are not supported for %s events",
1055 get_event_type_str(opt_event_type
));
1059 ret
= CMD_UNDEFINED
;
1064 channel_name
= opt_channel_name
;
1066 handle
= lttng_create_handle(session_name
, &dom
);
1067 if (handle
== NULL
) {
1074 /* Open a events element */
1075 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
1082 if (opt_enable_all
) {
1083 /* Default setup for enable all */
1085 ev
->type
= opt_event_type
;
1086 strcpy(ev
->name
, "*");
1087 /* kernel loglevels not implemented */
1088 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1090 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1091 strcpy(ev
->name
, "*");
1092 ev
->loglevel_type
= opt_loglevel_type
;
1094 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1095 if (opt_userspace
) {
1096 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1097 } else if (opt_jul
) {
1098 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1099 } else if (opt_log4j
) {
1100 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1101 } else if (opt_python
) {
1102 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1104 if (ev
->loglevel
== -1) {
1105 ERR("Unknown loglevel %s", opt_loglevel
);
1106 ret
= -LTTNG_ERR_INVALID
;
1110 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
1111 if (opt_userspace
) {
1113 } else if (opt_jul
) {
1114 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1115 } else if (opt_log4j
) {
1116 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1117 } else if (opt_python
) {
1118 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1124 ret
= create_exclusion_list_and_validate("*",
1125 opt_exclude
, &exclusion_list
);
1132 warn_on_truncated_exclusion_names(exclusion_list
,
1136 ret
= lttng_enable_event_with_exclusions(handle
,
1139 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1143 case LTTNG_ERR_KERN_EVENT_EXIST
:
1144 WARN("Kernel events already enabled (channel %s, session %s)",
1145 print_channel_name(channel_name
), session_name
);
1148 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1150 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1151 ERR("Events: %s (channel %s, session %s)",
1153 print_channel_name(channel_name
),
1159 ERR("Events: %s (channel %s, session %s)",
1160 lttng_strerror(ret
),
1161 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1162 ? print_raw_channel_name(channel_name
)
1163 : print_channel_name(channel_name
),
1171 switch (opt_event_type
) {
1172 case LTTNG_EVENT_TRACEPOINT
:
1173 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1174 char *exclusion_string
= print_exclusions(exclusion_list
);
1176 if (!exclusion_string
) {
1177 PERROR("Cannot allocate exclusion_string");
1181 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1182 get_domain_str(dom
.type
),
1184 print_channel_name(channel_name
),
1186 free(exclusion_string
);
1188 char *exclusion_string
= print_exclusions(exclusion_list
);
1190 if (!exclusion_string
) {
1191 PERROR("Cannot allocate exclusion_string");
1195 MSG("All %s tracepoints%s are enabled in channel %s",
1196 get_domain_str(dom
.type
),
1198 print_channel_name(channel_name
));
1199 free(exclusion_string
);
1202 case LTTNG_EVENT_SYSCALL
:
1204 MSG("All %s system calls are enabled in channel %s",
1205 get_domain_str(dom
.type
),
1206 print_channel_name(channel_name
));
1209 case LTTNG_EVENT_ALL
:
1210 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
1211 char *exclusion_string
= print_exclusions(exclusion_list
);
1213 if (!exclusion_string
) {
1214 PERROR("Cannot allocate exclusion_string");
1218 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1219 get_domain_str(dom
.type
),
1221 print_channel_name(channel_name
),
1223 free(exclusion_string
);
1225 char *exclusion_string
= print_exclusions(exclusion_list
);
1227 if (!exclusion_string
) {
1228 PERROR("Cannot allocate exclusion_string");
1232 MSG("All %s events%s are enabled in channel %s",
1233 get_domain_str(dom
.type
),
1235 print_channel_name(channel_name
));
1236 free(exclusion_string
);
1241 * We should not be here since lttng_enable_event should have
1242 * failed on the event type.
1249 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1251 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1253 if (command_ret
< 0) {
1254 switch (-command_ret
) {
1255 case LTTNG_ERR_FILTER_EXIST
:
1256 WARN("Filter on all events is already enabled"
1257 " (channel %s, session %s)",
1258 print_channel_name(channel_name
), session_name
);
1261 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1263 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1264 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1266 print_channel_name(channel_name
),
1267 session_name
, opt_filter
);
1272 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1273 lttng_strerror(command_ret
),
1274 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1275 ? print_raw_channel_name(channel_name
)
1276 : print_channel_name(channel_name
),
1277 session_name
, opt_filter
);
1281 error_holder
= command_ret
;
1284 MSG("Filter '%s' successfully set", opt_filter
);
1289 /* The wildcard * is used for kernel and ust domain to
1290 * represent ALL. We copy * in event name to force the wildcard use
1293 * Note: this is strictly for semantic and printing while in
1294 * machine interface mode.
1296 strcpy(ev
->name
, "*");
1298 /* If we reach here the events are enabled */
1299 if (!error
&& !warn
) {
1305 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1311 /* print exclusion */
1312 ret
= mi_print_exclusion(exclusion_list
);
1319 ret
= mi_lttng_writer_write_element_bool(writer
,
1320 mi_lttng_element_command_success
, success
);
1326 /* Close event element */
1327 ret
= mi_lttng_writer_close_element(writer
);
1337 /* Strip event list */
1338 event_name
= strtok(opt_event_list
, ",");
1339 while (event_name
!= NULL
) {
1340 /* Copy name and type of the event */
1341 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1342 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1343 ev
->type
= opt_event_type
;
1345 /* Kernel tracer action */
1347 DBG("Enabling kernel event %s for channel %s",
1349 print_channel_name(channel_name
));
1351 switch (opt_event_type
) {
1352 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1353 /* If event name differs from *, select tracepoint. */
1354 if (strcmp(ev
->name
, "*")) {
1355 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1358 case LTTNG_EVENT_TRACEPOINT
:
1360 case LTTNG_EVENT_PROBE
:
1361 ret
= parse_probe_opts(ev
, opt_probe
);
1363 ERR("Unable to parse probe options");
1368 case LTTNG_EVENT_USERSPACE_PROBE
:
1369 ret
= parse_userspace_probe_opts(ev
, opt_userspace_probe
);
1372 case CMD_UNSUPPORTED
:
1374 * Error message describing
1375 * what is not supported was
1376 * printed in the function.
1381 ERR("Unable to parse userspace probe options");
1387 case LTTNG_EVENT_FUNCTION
:
1388 ret
= parse_probe_opts(ev
, opt_function
);
1390 ERR("Unable to parse function probe options");
1395 case LTTNG_EVENT_SYSCALL
:
1396 ev
->type
= LTTNG_EVENT_SYSCALL
;
1399 ret
= CMD_UNDEFINED
;
1403 /* kernel loglevels not implemented */
1404 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1405 } else if (opt_userspace
) { /* User-space tracer action */
1406 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1407 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1409 switch (opt_event_type
) {
1410 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1412 case LTTNG_EVENT_TRACEPOINT
:
1413 /* Copy name and type of the event */
1414 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1415 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1416 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1418 case LTTNG_EVENT_PROBE
:
1419 case LTTNG_EVENT_FUNCTION
:
1420 case LTTNG_EVENT_SYSCALL
:
1421 case LTTNG_EVENT_USERSPACE_PROBE
:
1423 ERR("Event type not available for user-space tracing");
1424 ret
= CMD_UNSUPPORTED
;
1430 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1431 ERR("Exclusion option can only be used with tracepoint events");
1435 /* Free previously allocated items */
1436 strutils_free_null_terminated_array_of_strings(
1438 exclusion_list
= NULL
;
1439 ret
= create_exclusion_list_and_validate(
1440 event_name
, opt_exclude
,
1447 warn_on_truncated_exclusion_names(
1448 exclusion_list
, &warn
);
1451 ev
->loglevel_type
= opt_loglevel_type
;
1453 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1454 if (ev
->loglevel
== -1) {
1455 ERR("Unknown loglevel %s", opt_loglevel
);
1456 ret
= -LTTNG_ERR_INVALID
;
1462 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1463 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1464 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1465 ERR("Event type not supported for domain.");
1466 ret
= CMD_UNSUPPORTED
;
1470 ev
->loglevel_type
= opt_loglevel_type
;
1473 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1474 } else if (opt_log4j
) {
1475 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1476 } else if (opt_python
) {
1477 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1479 if (ev
->loglevel
== -1) {
1480 ERR("Unknown loglevel %s", opt_loglevel
);
1481 ret
= -LTTNG_ERR_INVALID
;
1486 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1487 } else if (opt_log4j
) {
1488 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1489 } else if (opt_python
) {
1490 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1493 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1494 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1495 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1501 char *exclusion_string
;
1503 command_ret
= lttng_enable_event_with_exclusions(handle
,
1506 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1508 exclusion_string
= print_exclusions(exclusion_list
);
1509 if (!exclusion_string
) {
1510 PERROR("Cannot allocate exclusion_string");
1514 if (command_ret
< 0) {
1515 /* Turn ret to positive value to handle the positive error code */
1516 switch (-command_ret
) {
1517 case LTTNG_ERR_KERN_EVENT_EXIST
:
1518 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1521 print_channel_name(channel_name
), session_name
);
1524 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1526 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1527 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1530 print_channel_name(channel_name
),
1535 case LTTNG_ERR_SDT_PROBE_SEMAPHORE
:
1536 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1537 event_name
, print_channel_name(channel_name
),
1542 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1544 lttng_strerror(command_ret
),
1545 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1546 ? print_raw_channel_name(channel_name
)
1547 : print_channel_name(channel_name
),
1552 error_holder
= command_ret
;
1555 case LTTNG_DOMAIN_KERNEL
:
1556 case LTTNG_DOMAIN_UST
:
1557 MSG("%s event %s%s created in channel %s",
1558 get_domain_str(dom
.type
),
1561 print_channel_name(channel_name
));
1563 case LTTNG_DOMAIN_JUL
:
1564 case LTTNG_DOMAIN_LOG4J
:
1565 case LTTNG_DOMAIN_PYTHON
:
1567 * Don't print the default channel
1568 * name for agent domains.
1570 MSG("%s event %s%s enabled",
1571 get_domain_str(dom
.type
),
1579 free(exclusion_string
);
1583 char *exclusion_string
;
1585 /* Filter present */
1588 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1590 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1592 exclusion_string
= print_exclusions(exclusion_list
);
1593 if (!exclusion_string
) {
1594 PERROR("Cannot allocate exclusion_string");
1598 if (command_ret
< 0) {
1599 switch (-command_ret
) {
1600 case LTTNG_ERR_FILTER_EXIST
:
1601 WARN("Filter on event %s%s is already enabled"
1602 " (channel %s, session %s)",
1605 print_channel_name(channel_name
), session_name
);
1608 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1610 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1611 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1614 print_channel_name(channel_name
),
1615 session_name
, opt_filter
);
1620 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1622 lttng_strerror(command_ret
),
1623 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1624 ? print_raw_channel_name(channel_name
)
1625 : print_channel_name(channel_name
),
1626 session_name
, opt_filter
);
1630 error_holder
= command_ret
;
1633 MSG("Event %s%s: Filter '%s' successfully set",
1634 event_name
, exclusion_string
,
1637 free(exclusion_string
);
1648 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1654 /* print exclusion */
1655 ret
= mi_print_exclusion(exclusion_list
);
1662 ret
= mi_lttng_writer_write_element_bool(writer
,
1663 mi_lttng_element_command_success
, success
);
1669 /* Close event element */
1670 ret
= mi_lttng_writer_close_element(writer
);
1678 event_name
= strtok(NULL
, ",");
1679 /* Reset warn, error and success */
1686 /* Close events element */
1687 ret
= mi_lttng_writer_close_element(writer
);
1700 lttng_destroy_handle(handle
);
1701 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1703 /* Overwrite ret with error_holder if there was an actual error with
1704 * enabling an event.
1706 ret
= error_holder
? error_holder
: ret
;
1708 lttng_event_destroy(ev
);
1713 * Add event to trace session
1715 int cmd_enable_events(int argc
, const char **argv
)
1717 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1718 static poptContext pc
;
1719 char *session_name
= NULL
;
1720 const char *leftover
= NULL
;
1721 int event_type
= -1;
1723 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1724 poptReadDefaultConfig(pc
, 0);
1726 /* Default event type */
1727 opt_event_type
= LTTNG_EVENT_ALL
;
1729 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1734 case OPT_TRACEPOINT
:
1735 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1738 opt_event_type
= LTTNG_EVENT_PROBE
;
1740 case OPT_USERSPACE_PROBE
:
1741 opt_event_type
= LTTNG_EVENT_USERSPACE_PROBE
;
1744 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1747 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1753 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1754 opt_loglevel
= poptGetOptArg(pc
);
1756 case OPT_LOGLEVEL_ONLY
:
1757 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1758 opt_loglevel
= poptGetOptArg(pc
);
1760 case OPT_LIST_OPTIONS
:
1761 list_cmd_options(stdout
, long_options
);
1768 ret
= CMD_UNDEFINED
;
1772 /* Validate event type. Multiple event type are not supported. */
1773 if (event_type
== -1) {
1774 event_type
= opt_event_type
;
1776 if (event_type
!= opt_event_type
) {
1777 ERR("Multiple event type not supported.");
1784 ret
= print_missing_or_multiple_domains(
1785 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1795 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1797 ret
= -LTTNG_ERR_NOMEM
;
1801 /* Open command element */
1802 ret
= mi_lttng_writer_command_open(writer
,
1803 mi_lttng_element_command_enable_event
);
1809 /* Open output element */
1810 ret
= mi_lttng_writer_open_element(writer
,
1811 mi_lttng_element_command_output
);
1818 opt_event_list
= (char*) poptGetArg(pc
);
1819 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1820 ERR("Missing event name(s).\n");
1825 leftover
= poptGetArg(pc
);
1827 ERR("Unknown argument: %s", leftover
);
1832 if (!opt_session_name
) {
1833 session_name
= get_session_name();
1834 if (session_name
== NULL
) {
1835 command_ret
= CMD_ERROR
;
1840 session_name
= opt_session_name
;
1843 command_ret
= enable_events(session_name
);
1852 /* Close output element */
1853 ret
= mi_lttng_writer_close_element(writer
);
1859 ret
= mi_lttng_writer_write_element_bool(writer
,
1860 mi_lttng_element_command_success
, success
);
1866 /* Command element close */
1867 ret
= mi_lttng_writer_command_close(writer
);
1876 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1877 /* Preserve original error code */
1878 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1881 if (opt_session_name
== NULL
) {
1885 /* Overwrite ret if an error occurred in enable_events */
1886 ret
= command_ret
? command_ret
: ret
;
1888 poptFreeContext(pc
);