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_function
;
58 static char *opt_channel_name
;
59 static char *opt_filter
;
60 static char *opt_exclude
;
62 #ifdef LTTNG_EMBED_HELP
63 static const char help_msg
[] =
64 #include <lttng-enable-event.1.h>
82 static struct lttng_handle
*handle
;
83 static struct mi_writer
*writer
;
85 static struct poptOption long_options
[] = {
86 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
87 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
88 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
89 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
90 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
91 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
92 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
93 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
94 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
95 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
96 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
97 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
98 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
99 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
100 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
101 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
102 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
103 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
104 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
105 {0, 0, 0, 0, 0, 0, 0}
109 * Parse probe options.
111 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
113 int ret
= CMD_SUCCESS
;
116 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
117 char name
[LTTNG_SYMBOL_NAME_LEN
];
124 /* Check for symbol+offset */
125 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
126 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
128 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
129 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
130 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
131 if (*s_hex
== '\0') {
132 ERR("Invalid probe offset %s", s_hex
);
136 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
137 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
138 ev
->attr
.probe
.addr
= 0;
142 /* Check for symbol */
143 if (isalpha(name
[0])) {
144 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
147 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
148 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
149 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
150 ev
->attr
.probe
.offset
= 0;
151 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
152 ev
->attr
.probe
.addr
= 0;
157 /* Check for address */
158 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
160 if (*s_hex
== '\0') {
161 ERR("Invalid probe address %s", s_hex
);
165 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
166 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
167 ev
->attr
.probe
.offset
= 0;
168 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
180 * Walk the directories in the PATH environment variable to find the target
181 * binary passed as parameter.
183 * On success, the full path of the binary is copied in binary_full_path out
184 * parameter. This buffer is allocated by the caller and must be at least
185 * LTTNG_PATH_MAX bytes long.
186 * On failure, returns -1;
188 static int walk_command_search_path(const char *binary
, char *binary_full_path
)
190 char *tentative_binary_path
= NULL
;
191 char *command_search_path
= NULL
;
192 char *curr_search_dir_end
= NULL
;
193 char *curr_search_dir
= NULL
;
194 struct stat stat_output
;
197 command_search_path
= lttng_secure_getenv("PATH");
198 if (!command_search_path
) {
204 * Duplicate the $PATH string as the char pointer returned by getenv() should
207 command_search_path
= strdup(command_search_path
);
208 if (!command_search_path
) {
214 * This char array is used to concatenate path to binary to look for
217 tentative_binary_path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
218 if (!tentative_binary_path
) {
223 curr_search_dir
= command_search_path
;
226 * Split on ':'. The return value of this call points to the
227 * matching character.
229 curr_search_dir_end
= strchr(curr_search_dir
, ':');
230 if (curr_search_dir_end
!= NULL
) {
232 * Add a NULL byte to the end of the first token so it
233 * can be used as a string.
235 curr_search_dir_end
[0] = '\0';
238 /* Empty the tentative path */
239 memset(tentative_binary_path
, 0, LTTNG_PATH_MAX
* sizeof(char));
242 * Build the tentative path to the binary using the current
243 * search directory and the name of the binary.
245 ret
= snprintf(tentative_binary_path
, LTTNG_PATH_MAX
, "%s/%s",
246 curr_search_dir
, binary
);
248 goto free_binary_path
;
250 if (ret
< LTTNG_PATH_MAX
) {
252 * Use STAT(2) to see if the file exists.
254 ret
= stat(tentative_binary_path
, &stat_output
);
257 * Verify that it is a regular file or a
258 * symlink and not a special file (e.g.
261 if (S_ISREG(stat_output
.st_mode
)
262 || S_ISLNK(stat_output
.st_mode
)) {
264 * Found a match, set the out parameter
265 * and return success.
267 ret
= lttng_strncpy(binary_full_path
,
268 tentative_binary_path
,
271 ERR("Source path does not fit "
272 "in destination buffer.");
274 goto free_binary_path
;
278 /* Go to the next entry in the $PATH variable. */
279 curr_search_dir
= curr_search_dir_end
+ 1;
280 } while (curr_search_dir_end
!= NULL
);
283 free(tentative_binary_path
);
285 free(command_search_path
);
291 * Maps LOG4j loglevel from string to value
293 static int loglevel_log4j_str_to_value(const char *inputstr
)
296 char str
[LTTNG_SYMBOL_NAME_LEN
];
298 if (!inputstr
|| strlen(inputstr
) == 0) {
303 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
304 * added at the end of the loop so a the upper bound we avoid the overflow.
306 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
307 str
[i
] = toupper(inputstr
[i
]);
312 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
313 return LTTNG_LOGLEVEL_LOG4J_OFF
;
314 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
315 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
316 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
317 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
318 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
319 return LTTNG_LOGLEVEL_LOG4J_WARN
;
320 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
321 return LTTNG_LOGLEVEL_LOG4J_INFO
;
322 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
323 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
324 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
325 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
326 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
327 return LTTNG_LOGLEVEL_LOG4J_ALL
;
334 * Maps JUL loglevel from string to value
336 static int loglevel_jul_str_to_value(const char *inputstr
)
339 char str
[LTTNG_SYMBOL_NAME_LEN
];
341 if (!inputstr
|| strlen(inputstr
) == 0) {
346 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
347 * added at the end of the loop so a the upper bound we avoid the overflow.
349 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
350 str
[i
] = toupper(inputstr
[i
]);
355 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
356 return LTTNG_LOGLEVEL_JUL_OFF
;
357 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
358 return LTTNG_LOGLEVEL_JUL_SEVERE
;
359 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
360 return LTTNG_LOGLEVEL_JUL_WARNING
;
361 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
362 return LTTNG_LOGLEVEL_JUL_INFO
;
363 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
364 return LTTNG_LOGLEVEL_JUL_CONFIG
;
365 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
366 return LTTNG_LOGLEVEL_JUL_FINE
;
367 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
368 return LTTNG_LOGLEVEL_JUL_FINER
;
369 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
370 return LTTNG_LOGLEVEL_JUL_FINEST
;
371 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
372 return LTTNG_LOGLEVEL_JUL_ALL
;
379 * Maps Python loglevel from string to value
381 static int loglevel_python_str_to_value(const char *inputstr
)
384 char str
[LTTNG_SYMBOL_NAME_LEN
];
386 if (!inputstr
|| strlen(inputstr
) == 0) {
391 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
392 * added at the end of the loop so a the upper bound we avoid the overflow.
394 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
395 str
[i
] = toupper(inputstr
[i
]);
400 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
401 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
402 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
403 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
404 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
405 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
406 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
407 return LTTNG_LOGLEVEL_PYTHON_INFO
;
408 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
409 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
410 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
411 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
418 * Maps loglevel from string to value
421 int loglevel_str_to_value(const char *inputstr
)
424 char str
[LTTNG_SYMBOL_NAME_LEN
];
426 if (!inputstr
|| strlen(inputstr
) == 0) {
431 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
432 * added at the end of the loop so a the upper bound we avoid the overflow.
434 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
435 str
[i
] = toupper(inputstr
[i
]);
439 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
440 return LTTNG_LOGLEVEL_EMERG
;
441 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
442 return LTTNG_LOGLEVEL_ALERT
;
443 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
444 return LTTNG_LOGLEVEL_CRIT
;
445 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
446 return LTTNG_LOGLEVEL_ERR
;
447 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
448 return LTTNG_LOGLEVEL_WARNING
;
449 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
450 return LTTNG_LOGLEVEL_NOTICE
;
451 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
452 return LTTNG_LOGLEVEL_INFO
;
453 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
454 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
455 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
456 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
457 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
458 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
459 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
460 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
461 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
462 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
463 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
464 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
465 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
466 return LTTNG_LOGLEVEL_DEBUG_LINE
;
467 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
468 return LTTNG_LOGLEVEL_DEBUG
;
475 const char *print_channel_name(const char *name
)
477 return name
? : DEFAULT_CHANNEL_NAME
;
481 const char *print_raw_channel_name(const char *name
)
483 return name
? : "<default>";
487 * Mi print exlcusion list
490 int mi_print_exclusion(char **names
)
493 int count
= names
? strutils_array_of_strings_len(names
) : 0;
501 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
506 for (i
= 0; i
< count
; i
++) {
507 ret
= mi_lttng_writer_write_element_string(writer
,
508 config_element_exclusion
, names
[i
]);
514 /* Close exclusions element */
515 ret
= mi_lttng_writer_close_element(writer
);
522 * Return allocated string for pretty-printing exclusion names.
525 char *print_exclusions(char **names
)
529 const char *preamble
= " excluding ";
531 int count
= names
? strutils_array_of_strings_len(names
) : 0;
537 /* calculate total required length */
538 for (i
= 0; i
< count
; i
++) {
539 length
+= strlen(names
[i
]) + 4;
542 /* add length of preamble + one for NUL - one for last (missing) comma */
543 length
+= strlen(preamble
);
544 ret
= zmalloc(length
+ 1);
548 strncpy(ret
, preamble
, length
);
549 for (i
= 0; i
< count
; i
++) {
551 strcat(ret
, names
[i
]);
553 if (i
!= count
- 1) {
562 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
566 const char *e
= event_name
;
567 const char *x
= exclusion
;
569 /* Scan both the excluder and the event letter by letter */
583 /* Event is a subset of the excluder */
584 ERR("Event %s: %s excludes all events from %s",
585 event_name
, exclusion
, event_name
);
591 * Reached the end of the event name before the
592 * end of the exclusion: this is valid.
614 WARN("Event %s: %s does not exclude any events from %s",
615 event_name
, exclusion
, event_name
);
622 int create_exclusion_list_and_validate(const char *event_name
,
623 const char *exclusions_arg
,
624 char ***exclusion_list
)
627 char **exclusions
= NULL
;
629 /* Event name must be a valid globbing pattern to allow exclusions. */
630 if (!strutils_is_star_glob_pattern(event_name
)) {
631 ERR("Event %s: Exclusions can only be used with a globbing pattern",
636 /* Split exclusions. */
637 exclusions
= strutils_split(exclusions_arg
, ',', true);
643 * If the event name is a star-at-end only globbing pattern,
644 * then we can validate the individual exclusions. Otherwise
645 * all exclusions are passed to the session daemon.
647 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
648 char * const *exclusion
;
650 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
651 if (!strutils_is_star_glob_pattern(*exclusion
) ||
652 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
653 ret
= check_exclusion_subsets(event_name
, *exclusion
);
661 *exclusion_list
= exclusions
;
667 strutils_free_null_terminated_array_of_strings(exclusions
);
673 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
676 char * const *exclusion
;
678 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
679 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
680 WARN("Event exclusion \"%s\" will be truncated",
688 * Enabling event using the lttng API.
689 * Note: in case of error only the last error code will be return.
691 static int enable_events(char *session_name
)
693 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
694 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
695 char *event_name
, *channel_name
= NULL
;
696 struct lttng_event
*ev
;
697 struct lttng_domain dom
;
698 char **exclusion_list
= NULL
;
700 memset(&dom
, 0, sizeof(dom
));
702 ev
= lttng_event_create();
710 WARN("Kernel loglevels are not supported.");
714 /* Create lttng domain */
716 dom
.type
= LTTNG_DOMAIN_KERNEL
;
717 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
718 } else if (opt_userspace
) {
719 dom
.type
= LTTNG_DOMAIN_UST
;
721 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
722 } else if (opt_jul
) {
723 dom
.type
= LTTNG_DOMAIN_JUL
;
725 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
726 } else if (opt_log4j
) {
727 dom
.type
= LTTNG_DOMAIN_LOG4J
;
729 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
730 } else if (opt_python
) {
731 dom
.type
= LTTNG_DOMAIN_PYTHON
;
733 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
735 /* Checked by the caller. */
741 case LTTNG_DOMAIN_KERNEL
:
742 case LTTNG_DOMAIN_JUL
:
743 case LTTNG_DOMAIN_LOG4J
:
744 case LTTNG_DOMAIN_PYTHON
:
745 ERR("Event name exclusions are not yet implemented for %s events",
746 get_domain_str(dom
.type
));
749 case LTTNG_DOMAIN_UST
:
750 /* Exclusions supported */
757 channel_name
= opt_channel_name
;
759 handle
= lttng_create_handle(session_name
, &dom
);
760 if (handle
== NULL
) {
767 /* Open a events element */
768 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
775 if (opt_enable_all
) {
776 /* Default setup for enable all */
778 ev
->type
= opt_event_type
;
779 strcpy(ev
->name
, "*");
780 /* kernel loglevels not implemented */
781 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
783 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
784 strcpy(ev
->name
, "*");
785 ev
->loglevel_type
= opt_loglevel_type
;
787 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
789 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
790 } else if (opt_jul
) {
791 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
792 } else if (opt_log4j
) {
793 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
794 } else if (opt_python
) {
795 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
797 if (ev
->loglevel
== -1) {
798 ERR("Unknown loglevel %s", opt_loglevel
);
799 ret
= -LTTNG_ERR_INVALID
;
803 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
806 } else if (opt_jul
) {
807 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
808 } else if (opt_log4j
) {
809 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
810 } else if (opt_python
) {
811 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
817 ret
= create_exclusion_list_and_validate("*",
818 opt_exclude
, &exclusion_list
);
825 warn_on_truncated_exclusion_names(exclusion_list
,
829 ret
= lttng_enable_event_with_exclusions(handle
,
832 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
836 case LTTNG_ERR_KERN_EVENT_EXIST
:
837 WARN("Kernel events already enabled (channel %s, session %s)",
838 print_channel_name(channel_name
), session_name
);
841 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
843 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
844 ERR("Events: %s (channel %s, session %s)",
846 print_channel_name(channel_name
),
852 ERR("Events: %s (channel %s, session %s)",
854 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
855 ? print_raw_channel_name(channel_name
)
856 : print_channel_name(channel_name
),
864 switch (opt_event_type
) {
865 case LTTNG_EVENT_TRACEPOINT
:
866 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
867 char *exclusion_string
= print_exclusions(exclusion_list
);
869 if (!exclusion_string
) {
870 PERROR("Cannot allocate exclusion_string");
874 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
875 get_domain_str(dom
.type
),
877 print_channel_name(channel_name
),
879 free(exclusion_string
);
881 char *exclusion_string
= print_exclusions(exclusion_list
);
883 if (!exclusion_string
) {
884 PERROR("Cannot allocate exclusion_string");
888 MSG("All %s tracepoints%s are enabled in channel %s",
889 get_domain_str(dom
.type
),
891 print_channel_name(channel_name
));
892 free(exclusion_string
);
895 case LTTNG_EVENT_SYSCALL
:
897 MSG("All %s system calls are enabled in channel %s",
898 get_domain_str(dom
.type
),
899 print_channel_name(channel_name
));
902 case LTTNG_EVENT_ALL
:
903 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
904 char *exclusion_string
= print_exclusions(exclusion_list
);
906 if (!exclusion_string
) {
907 PERROR("Cannot allocate exclusion_string");
911 MSG("All %s events%s are enabled in channel %s for loglevel %s",
912 get_domain_str(dom
.type
),
914 print_channel_name(channel_name
),
916 free(exclusion_string
);
918 char *exclusion_string
= print_exclusions(exclusion_list
);
920 if (!exclusion_string
) {
921 PERROR("Cannot allocate exclusion_string");
925 MSG("All %s events%s are enabled in channel %s",
926 get_domain_str(dom
.type
),
928 print_channel_name(channel_name
));
929 free(exclusion_string
);
934 * We should not be here since lttng_enable_event should have
935 * failed on the event type.
942 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
944 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
946 if (command_ret
< 0) {
947 switch (-command_ret
) {
948 case LTTNG_ERR_FILTER_EXIST
:
949 WARN("Filter on all events is already enabled"
950 " (channel %s, session %s)",
951 print_channel_name(channel_name
), session_name
);
954 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
956 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
957 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
959 print_channel_name(channel_name
),
960 session_name
, opt_filter
);
965 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
966 lttng_strerror(command_ret
),
967 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
968 ? print_raw_channel_name(channel_name
)
969 : print_channel_name(channel_name
),
970 session_name
, opt_filter
);
974 error_holder
= command_ret
;
977 MSG("Filter '%s' successfully set", opt_filter
);
982 /* The wildcard * is used for kernel and ust domain to
983 * represent ALL. We copy * in event name to force the wildcard use
986 * Note: this is strictly for semantic and printing while in
987 * machine interface mode.
989 strcpy(ev
->name
, "*");
991 /* If we reach here the events are enabled */
992 if (!error
&& !warn
) {
998 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1004 /* print exclusion */
1005 ret
= mi_print_exclusion(exclusion_list
);
1012 ret
= mi_lttng_writer_write_element_bool(writer
,
1013 mi_lttng_element_command_success
, success
);
1019 /* Close event element */
1020 ret
= mi_lttng_writer_close_element(writer
);
1030 /* Strip event list */
1031 event_name
= strtok(opt_event_list
, ",");
1032 while (event_name
!= NULL
) {
1033 /* Copy name and type of the event */
1034 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1035 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1036 ev
->type
= opt_event_type
;
1038 /* Kernel tracer action */
1040 DBG("Enabling kernel event %s for channel %s",
1042 print_channel_name(channel_name
));
1044 switch (opt_event_type
) {
1045 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1046 /* If event name differs from *, select tracepoint. */
1047 if (strcmp(ev
->name
, "*")) {
1048 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1051 case LTTNG_EVENT_TRACEPOINT
:
1053 case LTTNG_EVENT_PROBE
:
1054 ret
= parse_probe_opts(ev
, opt_probe
);
1056 ERR("Unable to parse probe options");
1061 case LTTNG_EVENT_FUNCTION
:
1062 ret
= parse_probe_opts(ev
, opt_function
);
1064 ERR("Unable to parse function probe options");
1069 case LTTNG_EVENT_SYSCALL
:
1070 ev
->type
= LTTNG_EVENT_SYSCALL
;
1073 ret
= CMD_UNDEFINED
;
1077 /* kernel loglevels not implemented */
1078 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1079 } else if (opt_userspace
) { /* User-space tracer action */
1080 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1081 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1083 switch (opt_event_type
) {
1084 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1086 case LTTNG_EVENT_TRACEPOINT
:
1087 /* Copy name and type of the event */
1088 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1089 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1090 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1092 case LTTNG_EVENT_PROBE
:
1093 case LTTNG_EVENT_FUNCTION
:
1094 case LTTNG_EVENT_SYSCALL
:
1096 ERR("Event type not available for user-space tracing");
1097 ret
= CMD_UNSUPPORTED
;
1103 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1104 ERR("Exclusion option can only be used with tracepoint events");
1108 /* Free previously allocated items */
1109 strutils_free_null_terminated_array_of_strings(
1111 exclusion_list
= NULL
;
1112 ret
= create_exclusion_list_and_validate(
1113 event_name
, opt_exclude
,
1120 warn_on_truncated_exclusion_names(
1121 exclusion_list
, &warn
);
1124 ev
->loglevel_type
= opt_loglevel_type
;
1126 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1127 if (ev
->loglevel
== -1) {
1128 ERR("Unknown loglevel %s", opt_loglevel
);
1129 ret
= -LTTNG_ERR_INVALID
;
1135 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1136 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1137 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1138 ERR("Event type not supported for domain.");
1139 ret
= CMD_UNSUPPORTED
;
1143 ev
->loglevel_type
= opt_loglevel_type
;
1146 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1147 } else if (opt_log4j
) {
1148 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1149 } else if (opt_python
) {
1150 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1152 if (ev
->loglevel
== -1) {
1153 ERR("Unknown loglevel %s", opt_loglevel
);
1154 ret
= -LTTNG_ERR_INVALID
;
1159 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1160 } else if (opt_log4j
) {
1161 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1162 } else if (opt_python
) {
1163 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1166 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1167 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1168 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1174 char *exclusion_string
;
1176 command_ret
= lttng_enable_event_with_exclusions(handle
,
1179 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1181 exclusion_string
= print_exclusions(exclusion_list
);
1182 if (!exclusion_string
) {
1183 PERROR("Cannot allocate exclusion_string");
1187 if (command_ret
< 0) {
1188 /* Turn ret to positive value to handle the positive error code */
1189 switch (-command_ret
) {
1190 case LTTNG_ERR_KERN_EVENT_EXIST
:
1191 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1194 print_channel_name(channel_name
), session_name
);
1197 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1199 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1200 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1203 print_channel_name(channel_name
),
1209 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1211 lttng_strerror(command_ret
),
1212 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1213 ? print_raw_channel_name(channel_name
)
1214 : print_channel_name(channel_name
),
1219 error_holder
= command_ret
;
1222 case LTTNG_DOMAIN_KERNEL
:
1223 case LTTNG_DOMAIN_UST
:
1224 MSG("%s event %s%s created in channel %s",
1225 get_domain_str(dom
.type
),
1228 print_channel_name(channel_name
));
1230 case LTTNG_DOMAIN_JUL
:
1231 case LTTNG_DOMAIN_LOG4J
:
1232 case LTTNG_DOMAIN_PYTHON
:
1234 * Don't print the default channel
1235 * name for agent domains.
1237 MSG("%s event %s%s enabled",
1238 get_domain_str(dom
.type
),
1246 free(exclusion_string
);
1250 char *exclusion_string
;
1252 /* Filter present */
1255 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1257 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1259 exclusion_string
= print_exclusions(exclusion_list
);
1260 if (!exclusion_string
) {
1261 PERROR("Cannot allocate exclusion_string");
1265 if (command_ret
< 0) {
1266 switch (-command_ret
) {
1267 case LTTNG_ERR_FILTER_EXIST
:
1268 WARN("Filter on event %s%s is already enabled"
1269 " (channel %s, session %s)",
1272 print_channel_name(channel_name
), session_name
);
1275 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1277 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1278 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1281 print_channel_name(channel_name
),
1282 session_name
, opt_filter
);
1287 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1289 lttng_strerror(command_ret
),
1290 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1291 ? print_raw_channel_name(channel_name
)
1292 : print_channel_name(channel_name
),
1293 session_name
, opt_filter
);
1297 error_holder
= command_ret
;
1300 MSG("Event %s%s: Filter '%s' successfully set",
1301 event_name
, exclusion_string
,
1304 free(exclusion_string
);
1315 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1321 /* print exclusion */
1322 ret
= mi_print_exclusion(exclusion_list
);
1329 ret
= mi_lttng_writer_write_element_bool(writer
,
1330 mi_lttng_element_command_success
, success
);
1336 /* Close event element */
1337 ret
= mi_lttng_writer_close_element(writer
);
1345 event_name
= strtok(NULL
, ",");
1346 /* Reset warn, error and success */
1353 /* Close events element */
1354 ret
= mi_lttng_writer_close_element(writer
);
1367 lttng_destroy_handle(handle
);
1368 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1370 /* Overwrite ret with error_holder if there was an actual error with
1371 * enabling an event.
1373 ret
= error_holder
? error_holder
: ret
;
1375 lttng_event_destroy(ev
);
1380 * Add event to trace session
1382 int cmd_enable_events(int argc
, const char **argv
)
1384 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1385 static poptContext pc
;
1386 char *session_name
= NULL
;
1387 const char *leftover
= NULL
;
1388 int event_type
= -1;
1390 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1391 poptReadDefaultConfig(pc
, 0);
1393 /* Default event type */
1394 opt_event_type
= LTTNG_EVENT_ALL
;
1396 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1401 case OPT_TRACEPOINT
:
1402 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1405 opt_event_type
= LTTNG_EVENT_PROBE
;
1408 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1411 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1417 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1418 opt_loglevel
= poptGetOptArg(pc
);
1420 case OPT_LOGLEVEL_ONLY
:
1421 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1422 opt_loglevel
= poptGetOptArg(pc
);
1424 case OPT_LIST_OPTIONS
:
1425 list_cmd_options(stdout
, long_options
);
1432 ret
= CMD_UNDEFINED
;
1436 /* Validate event type. Multiple event type are not supported. */
1437 if (event_type
== -1) {
1438 event_type
= opt_event_type
;
1440 if (event_type
!= opt_event_type
) {
1441 ERR("Multiple event type not supported.");
1448 ret
= print_missing_or_multiple_domains(
1449 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1457 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1459 ret
= -LTTNG_ERR_NOMEM
;
1463 /* Open command element */
1464 ret
= mi_lttng_writer_command_open(writer
,
1465 mi_lttng_element_command_enable_event
);
1471 /* Open output element */
1472 ret
= mi_lttng_writer_open_element(writer
,
1473 mi_lttng_element_command_output
);
1480 opt_event_list
= (char*) poptGetArg(pc
);
1481 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1482 ERR("Missing event name(s).\n");
1487 leftover
= poptGetArg(pc
);
1489 ERR("Unknown argument: %s", leftover
);
1494 if (!opt_session_name
) {
1495 session_name
= get_session_name();
1496 if (session_name
== NULL
) {
1497 command_ret
= CMD_ERROR
;
1502 session_name
= opt_session_name
;
1505 command_ret
= enable_events(session_name
);
1514 /* Close output element */
1515 ret
= mi_lttng_writer_close_element(writer
);
1521 ret
= mi_lttng_writer_write_element_bool(writer
,
1522 mi_lttng_element_command_success
, success
);
1528 /* Command element close */
1529 ret
= mi_lttng_writer_command_close(writer
);
1538 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1539 /* Preserve original error code */
1540 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1543 if (opt_session_name
== NULL
) {
1547 /* Overwrite ret if an error occurred in enable_events */
1548 ret
= command_ret
? command_ret
: ret
;
1550 poptFreeContext(pc
);