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/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 create_exclusion_list_and_validate(const char *event_name
,
509 const char *exclusions_arg
,
510 char ***exclusion_list
)
513 char **exclusions
= NULL
;
515 /* Event name must be a valid globbing pattern to allow exclusions. */
516 if (!strutils_is_star_glob_pattern(event_name
)) {
517 ERR("Event %s: Exclusions can only be used with a globbing pattern",
522 /* Split exclusions. */
523 exclusions
= strutils_split(exclusions_arg
, ',', true);
529 * If the event name is a star-at-end only globbing pattern,
530 * then we can validate the individual exclusions. Otherwise
531 * all exclusions are passed to the session daemon.
533 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
534 char * const *exclusion
;
536 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
537 if (!strutils_is_star_glob_pattern(*exclusion
) ||
538 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
539 ret
= check_exclusion_subsets(event_name
, *exclusion
);
547 *exclusion_list
= exclusions
;
553 strutils_free_null_terminated_array_of_strings(exclusions
);
559 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
562 char * const *exclusion
;
564 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
565 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
566 WARN("Event exclusion \"%s\" will be truncated",
574 * Enabling event using the lttng API.
575 * Note: in case of error only the last error code will be return.
577 static int enable_events(char *session_name
)
579 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
580 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
581 char *event_name
, *channel_name
= NULL
;
582 struct lttng_event
*ev
;
583 struct lttng_domain dom
;
584 char **exclusion_list
= NULL
;
586 memset(&dom
, 0, sizeof(dom
));
588 ev
= lttng_event_create();
596 WARN("Kernel loglevels are not supported.");
600 /* Create lttng domain */
602 dom
.type
= LTTNG_DOMAIN_KERNEL
;
603 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
604 } else if (opt_userspace
) {
605 dom
.type
= LTTNG_DOMAIN_UST
;
607 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
608 } else if (opt_jul
) {
609 dom
.type
= LTTNG_DOMAIN_JUL
;
611 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
612 } else if (opt_log4j
) {
613 dom
.type
= LTTNG_DOMAIN_LOG4J
;
615 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
616 } else if (opt_python
) {
617 dom
.type
= LTTNG_DOMAIN_PYTHON
;
619 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
621 /* Checked by the caller. */
627 case LTTNG_DOMAIN_KERNEL
:
628 case LTTNG_DOMAIN_JUL
:
629 case LTTNG_DOMAIN_LOG4J
:
630 case LTTNG_DOMAIN_PYTHON
:
631 ERR("Event name exclusions are not yet implemented for %s events",
632 get_domain_str(dom
.type
));
635 case LTTNG_DOMAIN_UST
:
636 /* Exclusions supported */
643 channel_name
= opt_channel_name
;
645 handle
= lttng_create_handle(session_name
, &dom
);
646 if (handle
== NULL
) {
653 /* Open a events element */
654 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
661 if (opt_enable_all
) {
662 /* Default setup for enable all */
664 ev
->type
= opt_event_type
;
665 strcpy(ev
->name
, "*");
666 /* kernel loglevels not implemented */
667 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
669 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
670 strcpy(ev
->name
, "*");
671 ev
->loglevel_type
= opt_loglevel_type
;
673 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
675 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
676 } else if (opt_jul
) {
677 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
678 } else if (opt_log4j
) {
679 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
680 } else if (opt_python
) {
681 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
683 if (ev
->loglevel
== -1) {
684 ERR("Unknown loglevel %s", opt_loglevel
);
685 ret
= -LTTNG_ERR_INVALID
;
689 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
692 } else if (opt_jul
) {
693 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
694 } else if (opt_log4j
) {
695 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
696 } else if (opt_python
) {
697 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
703 ret
= create_exclusion_list_and_validate("*",
704 opt_exclude
, &exclusion_list
);
711 warn_on_truncated_exclusion_names(exclusion_list
,
715 ret
= lttng_enable_event_with_exclusions(handle
,
718 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
722 case LTTNG_ERR_KERN_EVENT_EXIST
:
723 WARN("Kernel events already enabled (channel %s, session %s)",
724 print_channel_name(channel_name
), session_name
);
727 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
729 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
730 ERR("Events: %s (channel %s, session %s)",
732 print_channel_name(channel_name
),
738 ERR("Events: %s (channel %s, session %s)",
740 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
741 ? print_raw_channel_name(channel_name
)
742 : print_channel_name(channel_name
),
750 switch (opt_event_type
) {
751 case LTTNG_EVENT_TRACEPOINT
:
752 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
753 char *exclusion_string
= print_exclusions(exclusion_list
);
755 if (!exclusion_string
) {
756 PERROR("Cannot allocate exclusion_string");
760 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
761 get_domain_str(dom
.type
),
763 print_channel_name(channel_name
),
765 free(exclusion_string
);
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",
775 get_domain_str(dom
.type
),
777 print_channel_name(channel_name
));
778 free(exclusion_string
);
781 case LTTNG_EVENT_SYSCALL
:
783 MSG("All %s system calls are enabled in channel %s",
784 get_domain_str(dom
.type
),
785 print_channel_name(channel_name
));
788 case LTTNG_EVENT_ALL
:
789 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
790 char *exclusion_string
= print_exclusions(exclusion_list
);
792 if (!exclusion_string
) {
793 PERROR("Cannot allocate exclusion_string");
797 MSG("All %s events%s are enabled in channel %s for loglevel %s",
798 get_domain_str(dom
.type
),
800 print_channel_name(channel_name
),
802 free(exclusion_string
);
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",
812 get_domain_str(dom
.type
),
814 print_channel_name(channel_name
));
815 free(exclusion_string
);
820 * We should not be here since lttng_enable_event should have
821 * failed on the event type.
828 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
830 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
832 if (command_ret
< 0) {
833 switch (-command_ret
) {
834 case LTTNG_ERR_FILTER_EXIST
:
835 WARN("Filter on all events is already enabled"
836 " (channel %s, session %s)",
837 print_channel_name(channel_name
), session_name
);
840 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
842 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
843 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
845 print_channel_name(channel_name
),
846 session_name
, opt_filter
);
851 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
852 lttng_strerror(command_ret
),
853 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
854 ? print_raw_channel_name(channel_name
)
855 : print_channel_name(channel_name
),
856 session_name
, opt_filter
);
860 error_holder
= command_ret
;
863 MSG("Filter '%s' successfully set", opt_filter
);
868 /* The wildcard * is used for kernel and ust domain to
869 * represent ALL. We copy * in event name to force the wildcard use
872 * Note: this is strictly for semantic and printing while in
873 * machine interface mode.
875 strcpy(ev
->name
, "*");
877 /* If we reach here the events are enabled */
878 if (!error
&& !warn
) {
884 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
890 /* print exclusion */
891 ret
= mi_print_exclusion(exclusion_list
);
898 ret
= mi_lttng_writer_write_element_bool(writer
,
899 mi_lttng_element_command_success
, success
);
905 /* Close event element */
906 ret
= mi_lttng_writer_close_element(writer
);
916 /* Strip event list */
917 event_name
= strtok(opt_event_list
, ",");
918 while (event_name
!= NULL
) {
919 /* Copy name and type of the event */
920 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
921 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
922 ev
->type
= opt_event_type
;
924 /* Kernel tracer action */
926 DBG("Enabling kernel event %s for channel %s",
928 print_channel_name(channel_name
));
930 switch (opt_event_type
) {
931 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
932 /* If event name differs from *, select tracepoint. */
933 if (strcmp(ev
->name
, "*")) {
934 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
937 case LTTNG_EVENT_TRACEPOINT
:
939 case LTTNG_EVENT_PROBE
:
940 ret
= parse_probe_opts(ev
, opt_probe
);
942 ERR("Unable to parse probe options");
947 case LTTNG_EVENT_FUNCTION
:
948 ret
= parse_probe_opts(ev
, opt_function
);
950 ERR("Unable to parse function probe options");
955 case LTTNG_EVENT_SYSCALL
:
956 ev
->type
= LTTNG_EVENT_SYSCALL
;
963 /* kernel loglevels not implemented */
964 ev
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
965 } else if (opt_userspace
) { /* User-space tracer action */
966 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
967 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
969 switch (opt_event_type
) {
970 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
972 case LTTNG_EVENT_TRACEPOINT
:
973 /* Copy name and type of the event */
974 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
975 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
976 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
978 case LTTNG_EVENT_PROBE
:
979 case LTTNG_EVENT_FUNCTION
:
980 case LTTNG_EVENT_SYSCALL
:
982 ERR("Event type not available for user-space tracing");
983 ret
= CMD_UNSUPPORTED
;
989 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
990 ERR("Exclusion option can only be used with tracepoint events");
994 /* Free previously allocated items */
995 strutils_free_null_terminated_array_of_strings(
997 exclusion_list
= NULL
;
998 ret
= create_exclusion_list_and_validate(
999 event_name
, opt_exclude
,
1006 warn_on_truncated_exclusion_names(
1007 exclusion_list
, &warn
);
1010 ev
->loglevel_type
= opt_loglevel_type
;
1012 ev
->loglevel
= loglevel_str_to_value(opt_loglevel
);
1013 if (ev
->loglevel
== -1) {
1014 ERR("Unknown loglevel %s", opt_loglevel
);
1015 ret
= -LTTNG_ERR_INVALID
;
1021 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1022 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1023 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1024 ERR("Event type not supported for domain.");
1025 ret
= CMD_UNSUPPORTED
;
1029 ev
->loglevel_type
= opt_loglevel_type
;
1032 ev
->loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1033 } else if (opt_log4j
) {
1034 ev
->loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1035 } else if (opt_python
) {
1036 ev
->loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1038 if (ev
->loglevel
== -1) {
1039 ERR("Unknown loglevel %s", opt_loglevel
);
1040 ret
= -LTTNG_ERR_INVALID
;
1045 ev
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1046 } else if (opt_log4j
) {
1047 ev
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1048 } else if (opt_python
) {
1049 ev
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1052 ev
->type
= LTTNG_EVENT_TRACEPOINT
;
1053 strncpy(ev
->name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1054 ev
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1060 char *exclusion_string
;
1062 command_ret
= lttng_enable_event_with_exclusions(handle
,
1065 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1067 exclusion_string
= print_exclusions(exclusion_list
);
1068 if (!exclusion_string
) {
1069 PERROR("Cannot allocate exclusion_string");
1073 if (command_ret
< 0) {
1074 /* Turn ret to positive value to handle the positive error code */
1075 switch (-command_ret
) {
1076 case LTTNG_ERR_KERN_EVENT_EXIST
:
1077 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1080 print_channel_name(channel_name
), session_name
);
1083 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1085 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1086 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1089 print_channel_name(channel_name
),
1095 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1097 lttng_strerror(command_ret
),
1098 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1099 ? print_raw_channel_name(channel_name
)
1100 : print_channel_name(channel_name
),
1105 error_holder
= command_ret
;
1108 case LTTNG_DOMAIN_KERNEL
:
1109 case LTTNG_DOMAIN_UST
:
1110 MSG("%s event %s%s created in channel %s",
1111 get_domain_str(dom
.type
),
1114 print_channel_name(channel_name
));
1116 case LTTNG_DOMAIN_JUL
:
1117 case LTTNG_DOMAIN_LOG4J
:
1118 case LTTNG_DOMAIN_PYTHON
:
1120 * Don't print the default channel
1121 * name for agent domains.
1123 MSG("%s event %s%s enabled",
1124 get_domain_str(dom
.type
),
1132 free(exclusion_string
);
1136 char *exclusion_string
;
1138 /* Filter present */
1141 command_ret
= lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1143 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1145 exclusion_string
= print_exclusions(exclusion_list
);
1146 if (!exclusion_string
) {
1147 PERROR("Cannot allocate exclusion_string");
1151 if (command_ret
< 0) {
1152 switch (-command_ret
) {
1153 case LTTNG_ERR_FILTER_EXIST
:
1154 WARN("Filter on event %s%s is already enabled"
1155 " (channel %s, session %s)",
1158 print_channel_name(channel_name
), session_name
);
1161 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1163 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1164 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1167 print_channel_name(channel_name
),
1168 session_name
, opt_filter
);
1173 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
->name
,
1175 lttng_strerror(command_ret
),
1176 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1177 ? print_raw_channel_name(channel_name
)
1178 : print_channel_name(channel_name
),
1179 session_name
, opt_filter
);
1183 error_holder
= command_ret
;
1186 MSG("Event %s%s: Filter '%s' successfully set",
1187 event_name
, exclusion_string
,
1190 free(exclusion_string
);
1201 ret
= mi_lttng_event(writer
, ev
, 1, handle
->domain
.type
);
1207 /* print exclusion */
1208 ret
= mi_print_exclusion(exclusion_list
);
1215 ret
= mi_lttng_writer_write_element_bool(writer
,
1216 mi_lttng_element_command_success
, success
);
1222 /* Close event element */
1223 ret
= mi_lttng_writer_close_element(writer
);
1231 event_name
= strtok(NULL
, ",");
1232 /* Reset warn, error and success */
1239 /* Close events element */
1240 ret
= mi_lttng_writer_close_element(writer
);
1253 lttng_destroy_handle(handle
);
1254 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1256 /* Overwrite ret with error_holder if there was an actual error with
1257 * enabling an event.
1259 ret
= error_holder
? error_holder
: ret
;
1261 lttng_event_destroy(ev
);
1266 * Add event to trace session
1268 int cmd_enable_events(int argc
, const char **argv
)
1270 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1271 static poptContext pc
;
1272 char *session_name
= NULL
;
1273 const char *leftover
= NULL
;
1274 int event_type
= -1;
1276 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1277 poptReadDefaultConfig(pc
, 0);
1279 /* Default event type */
1280 opt_event_type
= LTTNG_EVENT_ALL
;
1282 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1287 case OPT_TRACEPOINT
:
1288 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1291 opt_event_type
= LTTNG_EVENT_PROBE
;
1294 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1297 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1303 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1304 opt_loglevel
= poptGetOptArg(pc
);
1306 case OPT_LOGLEVEL_ONLY
:
1307 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1308 opt_loglevel
= poptGetOptArg(pc
);
1310 case OPT_LIST_OPTIONS
:
1311 list_cmd_options(stdout
, long_options
);
1318 ret
= CMD_UNDEFINED
;
1322 /* Validate event type. Multiple event type are not supported. */
1323 if (event_type
== -1) {
1324 event_type
= opt_event_type
;
1326 if (event_type
!= opt_event_type
) {
1327 ERR("Multiple event type not supported.");
1334 ret
= print_missing_or_multiple_domains(
1335 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1343 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1345 ret
= -LTTNG_ERR_NOMEM
;
1349 /* Open command element */
1350 ret
= mi_lttng_writer_command_open(writer
,
1351 mi_lttng_element_command_enable_event
);
1357 /* Open output element */
1358 ret
= mi_lttng_writer_open_element(writer
,
1359 mi_lttng_element_command_output
);
1366 opt_event_list
= (char*) poptGetArg(pc
);
1367 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1368 ERR("Missing event name(s).\n");
1373 leftover
= poptGetArg(pc
);
1375 ERR("Unknown argument: %s", leftover
);
1380 if (!opt_session_name
) {
1381 session_name
= get_session_name();
1382 if (session_name
== NULL
) {
1383 command_ret
= CMD_ERROR
;
1388 session_name
= opt_session_name
;
1391 command_ret
= enable_events(session_name
);
1400 /* Close output element */
1401 ret
= mi_lttng_writer_close_element(writer
);
1407 ret
= mi_lttng_writer_write_element_bool(writer
,
1408 mi_lttng_element_command_success
, success
);
1414 /* Command element close */
1415 ret
= mi_lttng_writer_command_close(writer
);
1424 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1425 /* Preserve original error code */
1426 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1429 if (opt_session_name
== NULL
) {
1433 /* Overwrite ret if an error occurred in enable_events */
1434 ret
= command_ret
? command_ret
: ret
;
1436 poptFreeContext(pc
);