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 <src/common/sessiond-comm/sessiond-comm.h>
30 #include <common/compat/string.h>
31 #include <common/string-utils/string-utils.h>
34 #include <common/mi-lttng.h>
36 #include "../command.h"
38 #if (LTTNG_SYMBOL_NAME_LEN == 256)
39 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
42 static char *opt_event_list
;
43 static int opt_event_type
;
44 static const char *opt_loglevel
;
45 static int opt_loglevel_type
;
46 static int opt_kernel
;
47 static char *opt_session_name
;
48 static int opt_userspace
;
51 static int opt_python
;
52 static int opt_enable_all
;
53 static char *opt_probe
;
54 static char *opt_function
;
55 static char *opt_channel_name
;
56 static char *opt_filter
;
57 static char *opt_exclude
;
59 #ifdef LTTNG_EMBED_HELP
60 static const char help_msg
[] =
61 #include <lttng-enable-event.1.h>
79 static struct lttng_handle
*handle
;
80 static struct mi_writer
*writer
;
82 static struct poptOption long_options
[] = {
83 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
84 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
85 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
86 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
87 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
88 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
89 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
90 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
91 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
92 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
93 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
94 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
95 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
96 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
97 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
98 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
99 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
100 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
101 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
102 {0, 0, 0, 0, 0, 0, 0}
106 * Parse probe options.
108 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
110 int ret
= CMD_SUCCESS
;
113 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
114 char name
[LTTNG_SYMBOL_NAME_LEN
];
121 /* Check for symbol+offset */
122 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
123 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
125 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
126 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
127 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
128 if (*s_hex
== '\0') {
129 ERR("Invalid probe offset %s", s_hex
);
133 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
134 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
135 ev
->attr
.probe
.addr
= 0;
139 /* Check for symbol */
140 if (isalpha(name
[0])) {
141 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
144 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
145 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
146 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
147 ev
->attr
.probe
.offset
= 0;
148 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
149 ev
->attr
.probe
.addr
= 0;
154 /* Check for address */
155 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
157 if (*s_hex
== '\0') {
158 ERR("Invalid probe address %s", s_hex
);
162 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
163 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
164 ev
->attr
.probe
.offset
= 0;
165 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
177 * Maps LOG4j loglevel from string to value
179 static int loglevel_log4j_str_to_value(const char *inputstr
)
182 char str
[LTTNG_SYMBOL_NAME_LEN
];
184 if (!inputstr
|| strlen(inputstr
) == 0) {
189 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
190 * added at the end of the loop so a the upper bound we avoid the overflow.
192 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
193 str
[i
] = toupper(inputstr
[i
]);
198 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
199 return LTTNG_LOGLEVEL_LOG4J_OFF
;
200 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
201 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
202 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
203 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
204 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
205 return LTTNG_LOGLEVEL_LOG4J_WARN
;
206 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
207 return LTTNG_LOGLEVEL_LOG4J_INFO
;
208 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
209 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
210 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
211 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
212 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
213 return LTTNG_LOGLEVEL_LOG4J_ALL
;
220 * Maps JUL loglevel from string to value
222 static int loglevel_jul_str_to_value(const char *inputstr
)
225 char str
[LTTNG_SYMBOL_NAME_LEN
];
227 if (!inputstr
|| strlen(inputstr
) == 0) {
232 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
233 * added at the end of the loop so a the upper bound we avoid the overflow.
235 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
236 str
[i
] = toupper(inputstr
[i
]);
241 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
242 return LTTNG_LOGLEVEL_JUL_OFF
;
243 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
244 return LTTNG_LOGLEVEL_JUL_SEVERE
;
245 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
246 return LTTNG_LOGLEVEL_JUL_WARNING
;
247 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
248 return LTTNG_LOGLEVEL_JUL_INFO
;
249 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
250 return LTTNG_LOGLEVEL_JUL_CONFIG
;
251 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
252 return LTTNG_LOGLEVEL_JUL_FINE
;
253 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
254 return LTTNG_LOGLEVEL_JUL_FINER
;
255 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
256 return LTTNG_LOGLEVEL_JUL_FINEST
;
257 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
258 return LTTNG_LOGLEVEL_JUL_ALL
;
265 * Maps Python loglevel from string to value
267 static int loglevel_python_str_to_value(const char *inputstr
)
270 char str
[LTTNG_SYMBOL_NAME_LEN
];
272 if (!inputstr
|| strlen(inputstr
) == 0) {
277 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
278 * added at the end of the loop so a the upper bound we avoid the overflow.
280 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
281 str
[i
] = toupper(inputstr
[i
]);
286 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
287 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
288 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
289 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
290 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
291 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
292 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
293 return LTTNG_LOGLEVEL_PYTHON_INFO
;
294 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
295 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
296 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
297 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
304 * Maps loglevel from string to value
307 int loglevel_str_to_value(const char *inputstr
)
310 char str
[LTTNG_SYMBOL_NAME_LEN
];
312 if (!inputstr
|| strlen(inputstr
) == 0) {
317 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
318 * added at the end of the loop so a the upper bound we avoid the overflow.
320 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
321 str
[i
] = toupper(inputstr
[i
]);
325 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
326 return LTTNG_LOGLEVEL_EMERG
;
327 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
328 return LTTNG_LOGLEVEL_ALERT
;
329 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
330 return LTTNG_LOGLEVEL_CRIT
;
331 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
332 return LTTNG_LOGLEVEL_ERR
;
333 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
334 return LTTNG_LOGLEVEL_WARNING
;
335 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
336 return LTTNG_LOGLEVEL_NOTICE
;
337 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
338 return LTTNG_LOGLEVEL_INFO
;
339 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
340 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
341 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
342 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
343 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
344 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
345 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
346 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
347 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
348 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
349 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
350 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
351 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
352 return LTTNG_LOGLEVEL_DEBUG_LINE
;
353 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
354 return LTTNG_LOGLEVEL_DEBUG
;
361 const char *print_channel_name(const char *name
)
363 return name
? : DEFAULT_CHANNEL_NAME
;
367 const char *print_raw_channel_name(const char *name
)
369 return name
? : "<default>";
373 * Mi print exlcusion list
376 int mi_print_exclusion(char **names
)
379 int count
= names
? strutils_array_of_strings_len(names
) : 0;
387 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
392 for (i
= 0; i
< count
; i
++) {
393 ret
= mi_lttng_writer_write_element_string(writer
,
394 config_element_exclusion
, names
[i
]);
400 /* Close exclusions element */
401 ret
= mi_lttng_writer_close_element(writer
);
408 * Return allocated string for pretty-printing exclusion names.
411 char *print_exclusions(char **names
)
415 const char *preamble
= " excluding ";
417 int count
= names
? strutils_array_of_strings_len(names
) : 0;
423 /* calculate total required length */
424 for (i
= 0; i
< count
; i
++) {
425 length
+= strlen(names
[i
]) + 4;
428 /* add length of preamble + one for NUL - one for last (missing) comma */
429 length
+= strlen(preamble
);
430 ret
= zmalloc(length
+ 1);
434 strncpy(ret
, preamble
, length
);
435 for (i
= 0; i
< count
; i
++) {
437 strcat(ret
, names
[i
]);
439 if (i
!= count
- 1) {
448 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
452 const char *e
= event_name
;
453 const char *x
= exclusion
;
455 /* Scan both the excluder and the event letter by letter */
469 /* Event is a subset of the excluder */
470 ERR("Event %s: %s excludes all events from %s",
471 event_name
, exclusion
, event_name
);
477 * Reached the end of the event name before the
478 * end of the exclusion: this is valid.
500 WARN("Event %s: %s does not exclude any events from %s",
501 event_name
, exclusion
, event_name
);
508 int check_exclusions_subsets(const char *event_name
,
509 char * const *exclusions
)
514 for (item
= exclusions
; *item
; item
++) {
515 ret
= check_exclusion_subsets(event_name
, *item
);
526 int create_exclusion_list_and_validate(const char *event_name
,
527 const char *exclusions_arg
,
528 char ***exclusion_list
)
531 char **exclusions
= NULL
;
533 /* Event name must be a valid globbing pattern to allow exclusions. */
534 if (!strutils_is_star_glob_pattern(event_name
)) {
535 ERR("Event %s: Exclusions can only be used with a globbing pattern",
540 /* Split exclusions. */
541 exclusions
= strutils_split(exclusions_arg
, ',', true);
547 * If the event name is a star-at-end only globbing pattern,
548 * then we can validate the individual exclusions. Otherwise
549 * all exclusions are passed to the session daemon.
551 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
552 char * const *exclusion
;
554 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
555 if (!strutils_is_star_glob_pattern(*exclusion
) ||
556 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
557 ret
= check_exclusions_subsets(
558 event_name
, exclusion
);
566 *exclusion_list
= exclusions
;
572 strutils_free_null_terminated_array_of_strings(exclusions
);
578 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
581 char * const *exclusion
;
583 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
584 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
585 WARN("Event exclusion \"%s\" will be truncated",
593 * Enabling event using the lttng API.
594 * Note: in case of error only the last error code will be return.
596 static int enable_events(char *session_name
)
598 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
599 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
600 char *event_name
, *channel_name
= NULL
;
601 struct lttng_event ev
;
602 struct lttng_domain dom
;
603 char **exclusion_list
= NULL
;
605 memset(&ev
, 0, sizeof(ev
));
606 memset(&dom
, 0, sizeof(dom
));
610 WARN("Kernel loglevels are not supported.");
614 /* Create lttng domain */
616 dom
.type
= LTTNG_DOMAIN_KERNEL
;
617 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
618 } else if (opt_userspace
) {
619 dom
.type
= LTTNG_DOMAIN_UST
;
621 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
622 } else if (opt_jul
) {
623 dom
.type
= LTTNG_DOMAIN_JUL
;
625 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
626 } else if (opt_log4j
) {
627 dom
.type
= LTTNG_DOMAIN_LOG4J
;
629 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
630 } else if (opt_python
) {
631 dom
.type
= LTTNG_DOMAIN_PYTHON
;
633 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
635 /* Checked by the caller. */
641 case LTTNG_DOMAIN_KERNEL
:
642 case LTTNG_DOMAIN_JUL
:
643 case LTTNG_DOMAIN_LOG4J
:
644 case LTTNG_DOMAIN_PYTHON
:
645 ERR("Event name exclusions are not yet implemented for %s events",
646 get_domain_str(dom
.type
));
649 case LTTNG_DOMAIN_UST
:
650 /* Exclusions supported */
657 channel_name
= opt_channel_name
;
659 handle
= lttng_create_handle(session_name
, &dom
);
660 if (handle
== NULL
) {
667 /* Open a events element */
668 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
675 if (opt_enable_all
) {
676 /* Default setup for enable all */
678 ev
.type
= opt_event_type
;
679 strcpy(ev
.name
, "*");
680 /* kernel loglevels not implemented */
681 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
683 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
684 strcpy(ev
.name
, "*");
685 ev
.loglevel_type
= opt_loglevel_type
;
687 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
689 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
690 } else if (opt_jul
) {
691 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
692 } else if (opt_log4j
) {
693 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
694 } else if (opt_python
) {
695 ev
.loglevel
= loglevel_python_str_to_value(opt_loglevel
);
697 if (ev
.loglevel
== -1) {
698 ERR("Unknown loglevel %s", opt_loglevel
);
699 ret
= -LTTNG_ERR_INVALID
;
703 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
706 } else if (opt_jul
) {
707 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
708 } else if (opt_log4j
) {
709 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
710 } else if (opt_python
) {
711 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
717 ret
= create_exclusion_list_and_validate("*",
718 opt_exclude
, &exclusion_list
);
725 warn_on_truncated_exclusion_names(exclusion_list
,
729 ret
= lttng_enable_event_with_exclusions(handle
,
732 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
736 case LTTNG_ERR_KERN_EVENT_EXIST
:
737 WARN("Kernel events already enabled (channel %s, session %s)",
738 print_channel_name(channel_name
), session_name
);
741 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
743 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
744 ERR("Events: %s (channel %s, session %s)",
746 print_channel_name(channel_name
),
752 ERR("Events: %s (channel %s, session %s)",
754 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
755 ? print_raw_channel_name(channel_name
)
756 : print_channel_name(channel_name
),
764 switch (opt_event_type
) {
765 case LTTNG_EVENT_TRACEPOINT
:
766 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
767 char *exclusion_string
= print_exclusions(exclusion_list
);
769 if (!exclusion_string
) {
770 PERROR("Cannot allocate exclusion_string");
774 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
775 get_domain_str(dom
.type
),
777 print_channel_name(channel_name
),
779 free(exclusion_string
);
781 char *exclusion_string
= print_exclusions(exclusion_list
);
783 if (!exclusion_string
) {
784 PERROR("Cannot allocate exclusion_string");
788 MSG("All %s tracepoints%s are enabled in channel %s",
789 get_domain_str(dom
.type
),
791 print_channel_name(channel_name
));
792 free(exclusion_string
);
795 case LTTNG_EVENT_SYSCALL
:
797 MSG("All %s system calls are enabled in channel %s",
798 get_domain_str(dom
.type
),
799 print_channel_name(channel_name
));
802 case LTTNG_EVENT_ALL
:
803 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
804 char *exclusion_string
= print_exclusions(exclusion_list
);
806 if (!exclusion_string
) {
807 PERROR("Cannot allocate exclusion_string");
811 MSG("All %s events%s are enabled in channel %s for loglevel %s",
812 get_domain_str(dom
.type
),
814 print_channel_name(channel_name
),
816 free(exclusion_string
);
818 char *exclusion_string
= print_exclusions(exclusion_list
);
820 if (!exclusion_string
) {
821 PERROR("Cannot allocate exclusion_string");
825 MSG("All %s events%s are enabled in channel %s",
826 get_domain_str(dom
.type
),
828 print_channel_name(channel_name
));
829 free(exclusion_string
);
834 * We should not be here since lttng_enable_event should have
835 * failed on the event type.
842 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
844 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
846 if (command_ret
< 0) {
847 switch (-command_ret
) {
848 case LTTNG_ERR_FILTER_EXIST
:
849 WARN("Filter on all events is already enabled"
850 " (channel %s, session %s)",
851 print_channel_name(channel_name
), session_name
);
854 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
856 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
857 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
859 print_channel_name(channel_name
),
860 session_name
, opt_filter
);
865 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
866 lttng_strerror(command_ret
),
867 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
868 ? print_raw_channel_name(channel_name
)
869 : print_channel_name(channel_name
),
870 session_name
, opt_filter
);
874 error_holder
= command_ret
;
877 MSG("Filter '%s' successfully set", opt_filter
);
882 /* The wildcard * is used for kernel and ust domain to
883 * represent ALL. We copy * in event name to force the wildcard use
886 * Note: this is strictly for semantic and printing while in
887 * machine interface mode.
889 strcpy(ev
.name
, "*");
891 /* If we reach here the events are enabled */
892 if (!error
&& !warn
) {
898 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
904 /* print exclusion */
905 ret
= mi_print_exclusion(exclusion_list
);
912 ret
= mi_lttng_writer_write_element_bool(writer
,
913 mi_lttng_element_command_success
, success
);
919 /* Close event element */
920 ret
= mi_lttng_writer_close_element(writer
);
930 /* Strip event list */
931 event_name
= strtok(opt_event_list
, ",");
932 while (event_name
!= NULL
) {
933 /* Copy name and type of the event */
934 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
935 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
936 ev
.type
= opt_event_type
;
938 /* Kernel tracer action */
940 DBG("Enabling kernel event %s for channel %s",
942 print_channel_name(channel_name
));
944 switch (opt_event_type
) {
945 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
946 /* If event name differs from *, select tracepoint. */
947 if (strcmp(ev
.name
, "*")) {
948 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
951 case LTTNG_EVENT_TRACEPOINT
:
953 case LTTNG_EVENT_PROBE
:
954 ret
= parse_probe_opts(&ev
, opt_probe
);
956 ERR("Unable to parse probe options");
961 case LTTNG_EVENT_FUNCTION
:
962 ret
= parse_probe_opts(&ev
, opt_function
);
964 ERR("Unable to parse function probe options");
969 case LTTNG_EVENT_SYSCALL
:
970 ev
.type
= LTTNG_EVENT_SYSCALL
;
977 /* kernel loglevels not implemented */
978 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
979 } else if (opt_userspace
) { /* User-space tracer action */
980 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
981 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
983 switch (opt_event_type
) {
984 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
986 case LTTNG_EVENT_TRACEPOINT
:
987 /* Copy name and type of the event */
988 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
989 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
990 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
992 case LTTNG_EVENT_PROBE
:
993 case LTTNG_EVENT_FUNCTION
:
994 case LTTNG_EVENT_SYSCALL
:
996 ERR("Event type not available for user-space tracing");
997 ret
= CMD_UNSUPPORTED
;
1003 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1004 ERR("Exclusion option can only be used with tracepoint events");
1008 /* Free previously allocated items */
1009 strutils_free_null_terminated_array_of_strings(
1011 exclusion_list
= NULL
;
1012 ret
= create_exclusion_list_and_validate(
1013 event_name
, opt_exclude
,
1020 warn_on_truncated_exclusion_names(
1021 exclusion_list
, &warn
);
1024 ev
.loglevel_type
= opt_loglevel_type
;
1026 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
1027 if (ev
.loglevel
== -1) {
1028 ERR("Unknown loglevel %s", opt_loglevel
);
1029 ret
= -LTTNG_ERR_INVALID
;
1035 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1036 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1037 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1038 ERR("Event type not supported for domain.");
1039 ret
= CMD_UNSUPPORTED
;
1043 ev
.loglevel_type
= opt_loglevel_type
;
1046 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1047 } else if (opt_log4j
) {
1048 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1049 } else if (opt_python
) {
1050 ev
.loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1052 if (ev
.loglevel
== -1) {
1053 ERR("Unknown loglevel %s", opt_loglevel
);
1054 ret
= -LTTNG_ERR_INVALID
;
1059 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1060 } else if (opt_log4j
) {
1061 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1062 } else if (opt_python
) {
1063 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1066 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1067 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1068 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1074 char *exclusion_string
;
1076 command_ret
= lttng_enable_event_with_exclusions(handle
,
1079 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1081 exclusion_string
= print_exclusions(exclusion_list
);
1082 if (!exclusion_string
) {
1083 PERROR("Cannot allocate exclusion_string");
1087 if (command_ret
< 0) {
1088 /* Turn ret to positive value to handle the positive error code */
1089 switch (-command_ret
) {
1090 case LTTNG_ERR_KERN_EVENT_EXIST
:
1091 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1094 print_channel_name(channel_name
), session_name
);
1097 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1099 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1100 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1103 print_channel_name(channel_name
),
1109 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1111 lttng_strerror(command_ret
),
1112 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1113 ? print_raw_channel_name(channel_name
)
1114 : print_channel_name(channel_name
),
1119 error_holder
= command_ret
;
1122 case LTTNG_DOMAIN_KERNEL
:
1123 case LTTNG_DOMAIN_UST
:
1124 MSG("%s event %s%s created in channel %s",
1125 get_domain_str(dom
.type
),
1128 print_channel_name(channel_name
));
1130 case LTTNG_DOMAIN_JUL
:
1131 case LTTNG_DOMAIN_LOG4J
:
1132 case LTTNG_DOMAIN_PYTHON
:
1134 * Don't print the default channel
1135 * name for agent domains.
1137 MSG("%s event %s%s enabled",
1138 get_domain_str(dom
.type
),
1146 free(exclusion_string
);
1150 char *exclusion_string
;
1152 /* Filter present */
1155 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
1157 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1159 exclusion_string
= print_exclusions(exclusion_list
);
1160 if (!exclusion_string
) {
1161 PERROR("Cannot allocate exclusion_string");
1165 if (command_ret
< 0) {
1166 switch (-command_ret
) {
1167 case LTTNG_ERR_FILTER_EXIST
:
1168 WARN("Filter on event %s%s is already enabled"
1169 " (channel %s, session %s)",
1172 print_channel_name(channel_name
), session_name
);
1175 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1177 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1178 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1181 print_channel_name(channel_name
),
1182 session_name
, opt_filter
);
1187 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1189 lttng_strerror(command_ret
),
1190 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1191 ? print_raw_channel_name(channel_name
)
1192 : print_channel_name(channel_name
),
1193 session_name
, opt_filter
);
1197 error_holder
= command_ret
;
1200 MSG("Event %s%s: Filter '%s' successfully set",
1201 event_name
, exclusion_string
,
1204 free(exclusion_string
);
1215 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
1221 /* print exclusion */
1222 ret
= mi_print_exclusion(exclusion_list
);
1229 ret
= mi_lttng_writer_write_element_bool(writer
,
1230 mi_lttng_element_command_success
, success
);
1236 /* Close event element */
1237 ret
= mi_lttng_writer_close_element(writer
);
1245 event_name
= strtok(NULL
, ",");
1246 /* Reset warn, error and success */
1253 /* Close events element */
1254 ret
= mi_lttng_writer_close_element(writer
);
1267 lttng_destroy_handle(handle
);
1268 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1270 /* Overwrite ret with error_holder if there was an actual error with
1271 * enabling an event.
1273 ret
= error_holder
? error_holder
: ret
;
1279 * Add event to trace session
1281 int cmd_enable_events(int argc
, const char **argv
)
1283 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1284 static poptContext pc
;
1285 char *session_name
= NULL
;
1286 int event_type
= -1;
1288 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1289 poptReadDefaultConfig(pc
, 0);
1291 /* Default event type */
1292 opt_event_type
= LTTNG_EVENT_ALL
;
1294 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1299 case OPT_TRACEPOINT
:
1300 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1303 opt_event_type
= LTTNG_EVENT_PROBE
;
1306 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1309 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1315 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1316 opt_loglevel
= poptGetOptArg(pc
);
1318 case OPT_LOGLEVEL_ONLY
:
1319 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1320 opt_loglevel
= poptGetOptArg(pc
);
1322 case OPT_LIST_OPTIONS
:
1323 list_cmd_options(stdout
, long_options
);
1330 ret
= CMD_UNDEFINED
;
1334 /* Validate event type. Multiple event type are not supported. */
1335 if (event_type
== -1) {
1336 event_type
= opt_event_type
;
1338 if (event_type
!= opt_event_type
) {
1339 ERR("Multiple event type not supported.");
1346 ret
= print_missing_or_multiple_domains(
1347 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1355 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1357 ret
= -LTTNG_ERR_NOMEM
;
1361 /* Open command element */
1362 ret
= mi_lttng_writer_command_open(writer
,
1363 mi_lttng_element_command_enable_event
);
1369 /* Open output element */
1370 ret
= mi_lttng_writer_open_element(writer
,
1371 mi_lttng_element_command_output
);
1378 opt_event_list
= (char*) poptGetArg(pc
);
1379 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1380 ERR("Missing event name(s).\n");
1385 if (!opt_session_name
) {
1386 session_name
= get_session_name();
1387 if (session_name
== NULL
) {
1388 command_ret
= CMD_ERROR
;
1393 session_name
= opt_session_name
;
1396 command_ret
= enable_events(session_name
);
1405 /* Close output element */
1406 ret
= mi_lttng_writer_close_element(writer
);
1412 ret
= mi_lttng_writer_write_element_bool(writer
,
1413 mi_lttng_element_command_success
, success
);
1419 /* Command element close */
1420 ret
= mi_lttng_writer_command_close(writer
);
1429 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1430 /* Preserve original error code */
1431 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1434 if (opt_session_name
== NULL
) {
1438 /* Overwrite ret if an error occurred in enable_events */
1439 ret
= command_ret
? command_ret
: ret
;
1441 poptFreeContext(pc
);