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]) || 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
);
158 * Return an error if the first character of the tentative
159 * address is NULL or not a digit. It can be "0" if the address
160 * is in hexadecimal and can be 1 to 9 if it's in decimal.
162 if (*s_hex
== '\0' || !isdigit(*s_hex
)) {
163 ERR("Invalid probe description %s", s_hex
);
167 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
168 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
169 ev
->attr
.probe
.offset
= 0;
170 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
182 * Maps LOG4j loglevel from string to value
184 static int loglevel_log4j_str_to_value(const char *inputstr
)
187 char str
[LTTNG_SYMBOL_NAME_LEN
];
189 if (!inputstr
|| strlen(inputstr
) == 0) {
194 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
195 * added at the end of the loop so a the upper bound we avoid the overflow.
197 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
198 str
[i
] = toupper(inputstr
[i
]);
203 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
204 return LTTNG_LOGLEVEL_LOG4J_OFF
;
205 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
206 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
207 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
208 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
209 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
210 return LTTNG_LOGLEVEL_LOG4J_WARN
;
211 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
212 return LTTNG_LOGLEVEL_LOG4J_INFO
;
213 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
214 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
215 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
216 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
217 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
218 return LTTNG_LOGLEVEL_LOG4J_ALL
;
225 * Maps JUL loglevel from string to value
227 static int loglevel_jul_str_to_value(const char *inputstr
)
230 char str
[LTTNG_SYMBOL_NAME_LEN
];
232 if (!inputstr
|| strlen(inputstr
) == 0) {
237 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
238 * added at the end of the loop so a the upper bound we avoid the overflow.
240 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
241 str
[i
] = toupper(inputstr
[i
]);
246 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
247 return LTTNG_LOGLEVEL_JUL_OFF
;
248 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
249 return LTTNG_LOGLEVEL_JUL_SEVERE
;
250 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
251 return LTTNG_LOGLEVEL_JUL_WARNING
;
252 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
253 return LTTNG_LOGLEVEL_JUL_INFO
;
254 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
255 return LTTNG_LOGLEVEL_JUL_CONFIG
;
256 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
257 return LTTNG_LOGLEVEL_JUL_FINE
;
258 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
259 return LTTNG_LOGLEVEL_JUL_FINER
;
260 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
261 return LTTNG_LOGLEVEL_JUL_FINEST
;
262 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
263 return LTTNG_LOGLEVEL_JUL_ALL
;
270 * Maps Python loglevel from string to value
272 static int loglevel_python_str_to_value(const char *inputstr
)
275 char str
[LTTNG_SYMBOL_NAME_LEN
];
277 if (!inputstr
|| strlen(inputstr
) == 0) {
282 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
283 * added at the end of the loop so a the upper bound we avoid the overflow.
285 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
286 str
[i
] = toupper(inputstr
[i
]);
291 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
292 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
293 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
294 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
295 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
296 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
297 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
298 return LTTNG_LOGLEVEL_PYTHON_INFO
;
299 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
300 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
301 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
302 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
309 * Maps loglevel from string to value
312 int loglevel_str_to_value(const char *inputstr
)
315 char str
[LTTNG_SYMBOL_NAME_LEN
];
317 if (!inputstr
|| strlen(inputstr
) == 0) {
322 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
323 * added at the end of the loop so a the upper bound we avoid the overflow.
325 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
326 str
[i
] = toupper(inputstr
[i
]);
330 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
331 return LTTNG_LOGLEVEL_EMERG
;
332 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
333 return LTTNG_LOGLEVEL_ALERT
;
334 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
335 return LTTNG_LOGLEVEL_CRIT
;
336 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
337 return LTTNG_LOGLEVEL_ERR
;
338 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
339 return LTTNG_LOGLEVEL_WARNING
;
340 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
341 return LTTNG_LOGLEVEL_NOTICE
;
342 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
343 return LTTNG_LOGLEVEL_INFO
;
344 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
345 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
346 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
347 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
348 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
349 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
350 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
351 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
352 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
353 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
354 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
355 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
356 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
357 return LTTNG_LOGLEVEL_DEBUG_LINE
;
358 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
359 return LTTNG_LOGLEVEL_DEBUG
;
366 const char *print_channel_name(const char *name
)
368 return name
? : DEFAULT_CHANNEL_NAME
;
372 const char *print_raw_channel_name(const char *name
)
374 return name
? : "<default>";
378 * Mi print exlcusion list
381 int mi_print_exclusion(char **names
)
384 int count
= names
? strutils_array_of_strings_len(names
) : 0;
392 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
397 for (i
= 0; i
< count
; i
++) {
398 ret
= mi_lttng_writer_write_element_string(writer
,
399 config_element_exclusion
, names
[i
]);
405 /* Close exclusions element */
406 ret
= mi_lttng_writer_close_element(writer
);
413 * Return allocated string for pretty-printing exclusion names.
416 char *print_exclusions(char **names
)
420 const char *preamble
= " excluding ";
422 int count
= names
? strutils_array_of_strings_len(names
) : 0;
428 /* calculate total required length */
429 for (i
= 0; i
< count
; i
++) {
430 length
+= strlen(names
[i
]) + 4;
433 /* add length of preamble + one for NUL - one for last (missing) comma */
434 length
+= strlen(preamble
);
435 ret
= zmalloc(length
+ 1);
439 strncpy(ret
, preamble
, length
);
440 for (i
= 0; i
< count
; i
++) {
442 strcat(ret
, names
[i
]);
444 if (i
!= count
- 1) {
453 int check_exclusion_subsets(const char *event_name
, const char *exclusion
)
457 const char *e
= event_name
;
458 const char *x
= exclusion
;
460 /* Scan both the excluder and the event letter by letter */
474 /* Event is a subset of the excluder */
475 ERR("Event %s: %s excludes all events from %s",
476 event_name
, exclusion
, event_name
);
482 * Reached the end of the event name before the
483 * end of the exclusion: this is valid.
505 WARN("Event %s: %s does not exclude any events from %s",
506 event_name
, exclusion
, event_name
);
513 int create_exclusion_list_and_validate(const char *event_name
,
514 const char *exclusions_arg
,
515 char ***exclusion_list
)
518 char **exclusions
= NULL
;
520 /* Event name must be a valid globbing pattern to allow exclusions. */
521 if (!strutils_is_star_glob_pattern(event_name
)) {
522 ERR("Event %s: Exclusions can only be used with a globbing pattern",
527 /* Split exclusions. */
528 exclusions
= strutils_split(exclusions_arg
, ',', true);
534 * If the event name is a star-at-end only globbing pattern,
535 * then we can validate the individual exclusions. Otherwise
536 * all exclusions are passed to the session daemon.
538 if (strutils_is_star_at_the_end_only_glob_pattern(event_name
)) {
539 char * const *exclusion
;
541 for (exclusion
= exclusions
; *exclusion
; exclusion
++) {
542 if (!strutils_is_star_glob_pattern(*exclusion
) ||
543 strutils_is_star_at_the_end_only_glob_pattern(*exclusion
)) {
544 ret
= check_exclusion_subsets(event_name
, *exclusion
);
552 *exclusion_list
= exclusions
;
558 strutils_free_null_terminated_array_of_strings(exclusions
);
564 static void warn_on_truncated_exclusion_names(char * const *exclusion_list
,
567 char * const *exclusion
;
569 for (exclusion
= exclusion_list
; *exclusion
; exclusion
++) {
570 if (strlen(*exclusion
) >= LTTNG_SYMBOL_NAME_LEN
) {
571 WARN("Event exclusion \"%s\" will be truncated",
579 * Enabling event using the lttng API.
580 * Note: in case of error only the last error code will be return.
582 static int enable_events(char *session_name
)
584 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
585 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
586 char *event_name
, *channel_name
= NULL
;
587 struct lttng_event ev
;
588 struct lttng_domain dom
;
589 char **exclusion_list
= NULL
;
591 memset(&ev
, 0, sizeof(ev
));
592 memset(&dom
, 0, sizeof(dom
));
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
;
1265 * Add event to trace session
1267 int cmd_enable_events(int argc
, const char **argv
)
1269 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1270 static poptContext pc
;
1271 char *session_name
= NULL
;
1272 const char *leftover
= NULL
;
1273 int event_type
= -1;
1275 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1276 poptReadDefaultConfig(pc
, 0);
1278 /* Default event type */
1279 opt_event_type
= LTTNG_EVENT_ALL
;
1281 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1286 case OPT_TRACEPOINT
:
1287 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1290 opt_event_type
= LTTNG_EVENT_PROBE
;
1293 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1296 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1302 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1303 opt_loglevel
= poptGetOptArg(pc
);
1305 case OPT_LOGLEVEL_ONLY
:
1306 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1307 opt_loglevel
= poptGetOptArg(pc
);
1309 case OPT_LIST_OPTIONS
:
1310 list_cmd_options(stdout
, long_options
);
1317 ret
= CMD_UNDEFINED
;
1321 /* Validate event type. Multiple event type are not supported. */
1322 if (event_type
== -1) {
1323 event_type
= opt_event_type
;
1325 if (event_type
!= opt_event_type
) {
1326 ERR("Multiple event type not supported.");
1333 ret
= print_missing_or_multiple_domains(
1334 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
1344 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1346 ret
= -LTTNG_ERR_NOMEM
;
1350 /* Open command element */
1351 ret
= mi_lttng_writer_command_open(writer
,
1352 mi_lttng_element_command_enable_event
);
1358 /* Open output element */
1359 ret
= mi_lttng_writer_open_element(writer
,
1360 mi_lttng_element_command_output
);
1367 opt_event_list
= (char*) poptGetArg(pc
);
1368 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1369 ERR("Missing event name(s).\n");
1374 leftover
= poptGetArg(pc
);
1376 ERR("Unknown argument: %s", leftover
);
1381 if (!opt_session_name
) {
1382 session_name
= get_session_name();
1383 if (session_name
== NULL
) {
1384 command_ret
= CMD_ERROR
;
1389 session_name
= opt_session_name
;
1392 command_ret
= enable_events(session_name
);
1401 /* Close output element */
1402 ret
= mi_lttng_writer_close_element(writer
);
1408 ret
= mi_lttng_writer_write_element_bool(writer
,
1409 mi_lttng_element_command_success
, success
);
1415 /* Command element close */
1416 ret
= mi_lttng_writer_command_close(writer
);
1425 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1426 /* Preserve original error code */
1427 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1430 if (opt_session_name
== NULL
) {
1434 /* Overwrite ret if an error occurred in enable_events */
1435 ret
= command_ret
? command_ret
: ret
;
1437 poptFreeContext(pc
);