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(&ev
, 0, sizeof(ev
));
587 memset(&dom
, 0, sizeof(dom
));
591 WARN("Kernel loglevels are not supported.");
595 /* Create lttng domain */
597 dom
.type
= LTTNG_DOMAIN_KERNEL
;
598 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
599 } else if (opt_userspace
) {
600 dom
.type
= LTTNG_DOMAIN_UST
;
602 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
603 } else if (opt_jul
) {
604 dom
.type
= LTTNG_DOMAIN_JUL
;
606 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
607 } else if (opt_log4j
) {
608 dom
.type
= LTTNG_DOMAIN_LOG4J
;
610 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
611 } else if (opt_python
) {
612 dom
.type
= LTTNG_DOMAIN_PYTHON
;
614 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
616 /* Checked by the caller. */
622 case LTTNG_DOMAIN_KERNEL
:
623 case LTTNG_DOMAIN_JUL
:
624 case LTTNG_DOMAIN_LOG4J
:
625 case LTTNG_DOMAIN_PYTHON
:
626 ERR("Event name exclusions are not yet implemented for %s events",
627 get_domain_str(dom
.type
));
630 case LTTNG_DOMAIN_UST
:
631 /* Exclusions supported */
638 channel_name
= opt_channel_name
;
640 handle
= lttng_create_handle(session_name
, &dom
);
641 if (handle
== NULL
) {
648 /* Open a events element */
649 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
656 if (opt_enable_all
) {
657 /* Default setup for enable all */
659 ev
.type
= opt_event_type
;
660 strcpy(ev
.name
, "*");
661 /* kernel loglevels not implemented */
662 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
664 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
665 strcpy(ev
.name
, "*");
666 ev
.loglevel_type
= opt_loglevel_type
;
668 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
670 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
671 } else if (opt_jul
) {
672 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
673 } else if (opt_log4j
) {
674 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
675 } else if (opt_python
) {
676 ev
.loglevel
= loglevel_python_str_to_value(opt_loglevel
);
678 if (ev
.loglevel
== -1) {
679 ERR("Unknown loglevel %s", opt_loglevel
);
680 ret
= -LTTNG_ERR_INVALID
;
684 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
687 } else if (opt_jul
) {
688 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
689 } else if (opt_log4j
) {
690 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
691 } else if (opt_python
) {
692 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
698 ret
= create_exclusion_list_and_validate("*",
699 opt_exclude
, &exclusion_list
);
706 warn_on_truncated_exclusion_names(exclusion_list
,
710 ret
= lttng_enable_event_with_exclusions(handle
,
713 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
717 case LTTNG_ERR_KERN_EVENT_EXIST
:
718 WARN("Kernel events already enabled (channel %s, session %s)",
719 print_channel_name(channel_name
), session_name
);
722 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
724 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
725 ERR("Events: %s (channel %s, session %s)",
727 print_channel_name(channel_name
),
733 ERR("Events: %s (channel %s, session %s)",
735 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
736 ? print_raw_channel_name(channel_name
)
737 : print_channel_name(channel_name
),
745 switch (opt_event_type
) {
746 case LTTNG_EVENT_TRACEPOINT
:
747 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
748 char *exclusion_string
= print_exclusions(exclusion_list
);
750 if (!exclusion_string
) {
751 PERROR("Cannot allocate exclusion_string");
755 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
756 get_domain_str(dom
.type
),
758 print_channel_name(channel_name
),
760 free(exclusion_string
);
762 char *exclusion_string
= print_exclusions(exclusion_list
);
764 if (!exclusion_string
) {
765 PERROR("Cannot allocate exclusion_string");
769 MSG("All %s tracepoints%s are enabled in channel %s",
770 get_domain_str(dom
.type
),
772 print_channel_name(channel_name
));
773 free(exclusion_string
);
776 case LTTNG_EVENT_SYSCALL
:
778 MSG("All %s system calls are enabled in channel %s",
779 get_domain_str(dom
.type
),
780 print_channel_name(channel_name
));
783 case LTTNG_EVENT_ALL
:
784 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
785 char *exclusion_string
= print_exclusions(exclusion_list
);
787 if (!exclusion_string
) {
788 PERROR("Cannot allocate exclusion_string");
792 MSG("All %s events%s are enabled in channel %s for loglevel %s",
793 get_domain_str(dom
.type
),
795 print_channel_name(channel_name
),
797 free(exclusion_string
);
799 char *exclusion_string
= print_exclusions(exclusion_list
);
801 if (!exclusion_string
) {
802 PERROR("Cannot allocate exclusion_string");
806 MSG("All %s events%s are enabled in channel %s",
807 get_domain_str(dom
.type
),
809 print_channel_name(channel_name
));
810 free(exclusion_string
);
815 * We should not be here since lttng_enable_event should have
816 * failed on the event type.
823 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
825 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
827 if (command_ret
< 0) {
828 switch (-command_ret
) {
829 case LTTNG_ERR_FILTER_EXIST
:
830 WARN("Filter on all events is already enabled"
831 " (channel %s, session %s)",
832 print_channel_name(channel_name
), session_name
);
835 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
837 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
838 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
840 print_channel_name(channel_name
),
841 session_name
, opt_filter
);
846 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
847 lttng_strerror(command_ret
),
848 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
849 ? print_raw_channel_name(channel_name
)
850 : print_channel_name(channel_name
),
851 session_name
, opt_filter
);
855 error_holder
= command_ret
;
858 MSG("Filter '%s' successfully set", opt_filter
);
863 /* The wildcard * is used for kernel and ust domain to
864 * represent ALL. We copy * in event name to force the wildcard use
867 * Note: this is strictly for semantic and printing while in
868 * machine interface mode.
870 strcpy(ev
.name
, "*");
872 /* If we reach here the events are enabled */
873 if (!error
&& !warn
) {
879 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
885 /* print exclusion */
886 ret
= mi_print_exclusion(exclusion_list
);
893 ret
= mi_lttng_writer_write_element_bool(writer
,
894 mi_lttng_element_command_success
, success
);
900 /* Close event element */
901 ret
= mi_lttng_writer_close_element(writer
);
911 /* Strip event list */
912 event_name
= strtok(opt_event_list
, ",");
913 while (event_name
!= NULL
) {
914 /* Copy name and type of the event */
915 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
916 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
917 ev
.type
= opt_event_type
;
919 /* Kernel tracer action */
921 DBG("Enabling kernel event %s for channel %s",
923 print_channel_name(channel_name
));
925 switch (opt_event_type
) {
926 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
927 /* If event name differs from *, select tracepoint. */
928 if (strcmp(ev
.name
, "*")) {
929 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
932 case LTTNG_EVENT_TRACEPOINT
:
934 case LTTNG_EVENT_PROBE
:
935 ret
= parse_probe_opts(&ev
, opt_probe
);
937 ERR("Unable to parse probe options");
942 case LTTNG_EVENT_FUNCTION
:
943 ret
= parse_probe_opts(&ev
, opt_function
);
945 ERR("Unable to parse function probe options");
950 case LTTNG_EVENT_SYSCALL
:
951 ev
.type
= LTTNG_EVENT_SYSCALL
;
958 /* kernel loglevels not implemented */
959 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
960 } else if (opt_userspace
) { /* User-space tracer action */
961 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
962 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
964 switch (opt_event_type
) {
965 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
967 case LTTNG_EVENT_TRACEPOINT
:
968 /* Copy name and type of the event */
969 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
970 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
971 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
973 case LTTNG_EVENT_PROBE
:
974 case LTTNG_EVENT_FUNCTION
:
975 case LTTNG_EVENT_SYSCALL
:
977 ERR("Event type not available for user-space tracing");
978 ret
= CMD_UNSUPPORTED
;
984 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
985 ERR("Exclusion option can only be used with tracepoint events");
989 /* Free previously allocated items */
990 strutils_free_null_terminated_array_of_strings(
992 exclusion_list
= NULL
;
993 ret
= create_exclusion_list_and_validate(
994 event_name
, opt_exclude
,
1001 warn_on_truncated_exclusion_names(
1002 exclusion_list
, &warn
);
1005 ev
.loglevel_type
= opt_loglevel_type
;
1007 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
1008 if (ev
.loglevel
== -1) {
1009 ERR("Unknown loglevel %s", opt_loglevel
);
1010 ret
= -LTTNG_ERR_INVALID
;
1016 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1017 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1018 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1019 ERR("Event type not supported for domain.");
1020 ret
= CMD_UNSUPPORTED
;
1024 ev
.loglevel_type
= opt_loglevel_type
;
1027 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1028 } else if (opt_log4j
) {
1029 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1030 } else if (opt_python
) {
1031 ev
.loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1033 if (ev
.loglevel
== -1) {
1034 ERR("Unknown loglevel %s", opt_loglevel
);
1035 ret
= -LTTNG_ERR_INVALID
;
1040 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1041 } else if (opt_log4j
) {
1042 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1043 } else if (opt_python
) {
1044 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1047 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1048 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1049 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1055 char *exclusion_string
;
1057 command_ret
= lttng_enable_event_with_exclusions(handle
,
1060 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1062 exclusion_string
= print_exclusions(exclusion_list
);
1063 if (!exclusion_string
) {
1064 PERROR("Cannot allocate exclusion_string");
1068 if (command_ret
< 0) {
1069 /* Turn ret to positive value to handle the positive error code */
1070 switch (-command_ret
) {
1071 case LTTNG_ERR_KERN_EVENT_EXIST
:
1072 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1075 print_channel_name(channel_name
), session_name
);
1078 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1080 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1081 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1084 print_channel_name(channel_name
),
1090 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1092 lttng_strerror(command_ret
),
1093 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1094 ? print_raw_channel_name(channel_name
)
1095 : print_channel_name(channel_name
),
1100 error_holder
= command_ret
;
1103 case LTTNG_DOMAIN_KERNEL
:
1104 case LTTNG_DOMAIN_UST
:
1105 MSG("%s event %s%s created in channel %s",
1106 get_domain_str(dom
.type
),
1109 print_channel_name(channel_name
));
1111 case LTTNG_DOMAIN_JUL
:
1112 case LTTNG_DOMAIN_LOG4J
:
1113 case LTTNG_DOMAIN_PYTHON
:
1115 * Don't print the default channel
1116 * name for agent domains.
1118 MSG("%s event %s%s enabled",
1119 get_domain_str(dom
.type
),
1127 free(exclusion_string
);
1131 char *exclusion_string
;
1133 /* Filter present */
1136 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
1138 exclusion_list
? strutils_array_of_strings_len(exclusion_list
) : 0,
1140 exclusion_string
= print_exclusions(exclusion_list
);
1141 if (!exclusion_string
) {
1142 PERROR("Cannot allocate exclusion_string");
1146 if (command_ret
< 0) {
1147 switch (-command_ret
) {
1148 case LTTNG_ERR_FILTER_EXIST
:
1149 WARN("Filter on event %s%s is already enabled"
1150 " (channel %s, session %s)",
1153 print_channel_name(channel_name
), session_name
);
1156 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1158 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1159 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1162 print_channel_name(channel_name
),
1163 session_name
, opt_filter
);
1168 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1170 lttng_strerror(command_ret
),
1171 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1172 ? print_raw_channel_name(channel_name
)
1173 : print_channel_name(channel_name
),
1174 session_name
, opt_filter
);
1178 error_holder
= command_ret
;
1181 MSG("Event %s%s: Filter '%s' successfully set",
1182 event_name
, exclusion_string
,
1185 free(exclusion_string
);
1196 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
1202 /* print exclusion */
1203 ret
= mi_print_exclusion(exclusion_list
);
1210 ret
= mi_lttng_writer_write_element_bool(writer
,
1211 mi_lttng_element_command_success
, success
);
1217 /* Close event element */
1218 ret
= mi_lttng_writer_close_element(writer
);
1226 event_name
= strtok(NULL
, ",");
1227 /* Reset warn, error and success */
1234 /* Close events element */
1235 ret
= mi_lttng_writer_close_element(writer
);
1248 lttng_destroy_handle(handle
);
1249 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1251 /* Overwrite ret with error_holder if there was an actual error with
1252 * enabling an event.
1254 ret
= error_holder
? error_holder
: ret
;
1260 * Add event to trace session
1262 int cmd_enable_events(int argc
, const char **argv
)
1264 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1265 static poptContext pc
;
1266 char *session_name
= NULL
;
1267 int event_type
= -1;
1269 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1270 poptReadDefaultConfig(pc
, 0);
1272 /* Default event type */
1273 opt_event_type
= LTTNG_EVENT_ALL
;
1275 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1280 case OPT_TRACEPOINT
:
1281 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1284 opt_event_type
= LTTNG_EVENT_PROBE
;
1287 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1290 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1296 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1297 opt_loglevel
= poptGetOptArg(pc
);
1299 case OPT_LOGLEVEL_ONLY
:
1300 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1301 opt_loglevel
= poptGetOptArg(pc
);
1303 case OPT_LIST_OPTIONS
:
1304 list_cmd_options(stdout
, long_options
);
1311 ret
= CMD_UNDEFINED
;
1315 /* Validate event type. Multiple event type are not supported. */
1316 if (event_type
== -1) {
1317 event_type
= opt_event_type
;
1319 if (event_type
!= opt_event_type
) {
1320 ERR("Multiple event type not supported.");
1327 ret
= print_missing_or_multiple_domains(
1328 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1336 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1338 ret
= -LTTNG_ERR_NOMEM
;
1342 /* Open command element */
1343 ret
= mi_lttng_writer_command_open(writer
,
1344 mi_lttng_element_command_enable_event
);
1350 /* Open output element */
1351 ret
= mi_lttng_writer_open_element(writer
,
1352 mi_lttng_element_command_output
);
1359 opt_event_list
= (char*) poptGetArg(pc
);
1360 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1361 ERR("Missing event name(s).\n");
1366 if (!opt_session_name
) {
1367 session_name
= get_session_name();
1368 if (session_name
== NULL
) {
1369 command_ret
= CMD_ERROR
;
1374 session_name
= opt_session_name
;
1377 command_ret
= enable_events(session_name
);
1386 /* Close output element */
1387 ret
= mi_lttng_writer_close_element(writer
);
1393 ret
= mi_lttng_writer_write_element_bool(writer
,
1394 mi_lttng_element_command_success
, success
);
1400 /* Command element close */
1401 ret
= mi_lttng_writer_command_close(writer
);
1410 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1411 /* Preserve original error code */
1412 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1415 if (opt_session_name
== NULL
) {
1419 /* Overwrite ret if an error occurred in enable_events */
1420 ret
= command_ret
? command_ret
: ret
;
1422 poptFreeContext(pc
);