2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
29 #include <src/common/sessiond-comm/sessiond-comm.h>
30 #include <common/compat/string.h>
33 #include <common/mi-lttng.h>
35 #include "../command.h"
37 #if (LTTNG_SYMBOL_NAME_LEN == 256)
38 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
41 static char *opt_event_list
;
42 static int opt_event_type
;
43 static const char *opt_loglevel
;
44 static int opt_loglevel_type
;
45 static int opt_kernel
;
46 static char *opt_session_name
;
47 static int opt_userspace
;
50 static int opt_python
;
51 static int opt_enable_all
;
52 static char *opt_probe
;
53 static char *opt_function
;
54 static char *opt_channel_name
;
55 static char *opt_filter
;
56 static char *opt_exclude
;
58 #ifdef LTTNG_EMBED_HELP
59 static const char help_msg
[] =
60 #include <lttng-enable-event.1.h>
78 static struct lttng_handle
*handle
;
79 static struct mi_writer
*writer
;
81 static struct poptOption long_options
[] = {
82 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
83 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
84 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
85 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
86 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
87 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
88 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
89 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
90 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
91 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
92 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
93 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
94 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
95 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
96 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
97 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
98 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
99 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
100 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
101 {0, 0, 0, 0, 0, 0, 0}
105 * Parse probe options.
107 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
109 int ret
= CMD_SUCCESS
;
112 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
113 char name
[LTTNG_SYMBOL_NAME_LEN
];
120 /* Check for symbol+offset */
121 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
122 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
124 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
125 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
126 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
127 if (*s_hex
== '\0') {
128 ERR("Invalid probe offset %s", s_hex
);
132 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
133 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
134 ev
->attr
.probe
.addr
= 0;
138 /* Check for symbol */
139 if (isalpha(name
[0])) {
140 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
143 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
144 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
145 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
146 ev
->attr
.probe
.offset
= 0;
147 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
148 ev
->attr
.probe
.addr
= 0;
153 /* Check for address */
154 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
156 if (*s_hex
== '\0') {
157 ERR("Invalid probe address %s", s_hex
);
161 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
162 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
163 ev
->attr
.probe
.offset
= 0;
164 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
176 * Maps LOG4j loglevel from string to value
178 static int loglevel_log4j_str_to_value(const char *inputstr
)
181 char str
[LTTNG_SYMBOL_NAME_LEN
];
183 if (!inputstr
|| strlen(inputstr
) == 0) {
188 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
189 * added at the end of the loop so a the upper bound we avoid the overflow.
191 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
192 str
[i
] = toupper(inputstr
[i
]);
197 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
198 return LTTNG_LOGLEVEL_LOG4J_OFF
;
199 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
200 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
201 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
202 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
203 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
204 return LTTNG_LOGLEVEL_LOG4J_WARN
;
205 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
206 return LTTNG_LOGLEVEL_LOG4J_INFO
;
207 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
208 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
209 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
210 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
211 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
212 return LTTNG_LOGLEVEL_LOG4J_ALL
;
219 * Maps JUL loglevel from string to value
221 static int loglevel_jul_str_to_value(const char *inputstr
)
224 char str
[LTTNG_SYMBOL_NAME_LEN
];
226 if (!inputstr
|| strlen(inputstr
) == 0) {
231 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
232 * added at the end of the loop so a the upper bound we avoid the overflow.
234 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
235 str
[i
] = toupper(inputstr
[i
]);
240 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
241 return LTTNG_LOGLEVEL_JUL_OFF
;
242 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
243 return LTTNG_LOGLEVEL_JUL_SEVERE
;
244 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
245 return LTTNG_LOGLEVEL_JUL_WARNING
;
246 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
247 return LTTNG_LOGLEVEL_JUL_INFO
;
248 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
249 return LTTNG_LOGLEVEL_JUL_CONFIG
;
250 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
251 return LTTNG_LOGLEVEL_JUL_FINE
;
252 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
253 return LTTNG_LOGLEVEL_JUL_FINER
;
254 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
255 return LTTNG_LOGLEVEL_JUL_FINEST
;
256 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
257 return LTTNG_LOGLEVEL_JUL_ALL
;
264 * Maps Python loglevel from string to value
266 static int loglevel_python_str_to_value(const char *inputstr
)
269 char str
[LTTNG_SYMBOL_NAME_LEN
];
271 if (!inputstr
|| strlen(inputstr
) == 0) {
276 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
277 * added at the end of the loop so a the upper bound we avoid the overflow.
279 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
280 str
[i
] = toupper(inputstr
[i
]);
285 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
286 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
287 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
288 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
289 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
290 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
291 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
292 return LTTNG_LOGLEVEL_PYTHON_INFO
;
293 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
294 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
295 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
296 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
303 * Maps loglevel from string to value
306 int loglevel_str_to_value(const char *inputstr
)
309 char str
[LTTNG_SYMBOL_NAME_LEN
];
311 if (!inputstr
|| strlen(inputstr
) == 0) {
316 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
317 * added at the end of the loop so a the upper bound we avoid the overflow.
319 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
320 str
[i
] = toupper(inputstr
[i
]);
324 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
325 return LTTNG_LOGLEVEL_EMERG
;
326 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
327 return LTTNG_LOGLEVEL_ALERT
;
328 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
329 return LTTNG_LOGLEVEL_CRIT
;
330 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
331 return LTTNG_LOGLEVEL_ERR
;
332 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
333 return LTTNG_LOGLEVEL_WARNING
;
334 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
335 return LTTNG_LOGLEVEL_NOTICE
;
336 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
337 return LTTNG_LOGLEVEL_INFO
;
338 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
339 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
340 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
341 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
342 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
343 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
344 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
345 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
346 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
347 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
348 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
349 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
350 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
351 return LTTNG_LOGLEVEL_DEBUG_LINE
;
352 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
353 return LTTNG_LOGLEVEL_DEBUG
;
360 const char *print_channel_name(const char *name
)
362 return name
? : DEFAULT_CHANNEL_NAME
;
366 const char *print_raw_channel_name(const char *name
)
368 return name
? : "<default>";
372 * Mi print exlcusion list
375 int mi_print_exclusion(int count
, char **names
)
385 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
390 for (i
= 0; i
< count
; i
++) {
391 ret
= mi_lttng_writer_write_element_string(writer
,
392 config_element_exclusion
, names
[i
]);
398 /* Close exclusions element */
399 ret
= mi_lttng_writer_close_element(writer
);
406 * Return allocated string for pretty-printing exclusion names.
409 char *print_exclusions(int count
, char **names
)
413 const char *preamble
= " excluding ";
420 /* calculate total required length */
421 for (i
= 0; i
< count
; i
++) {
422 length
+= strlen(names
[i
]) + 1;
425 /* add length of preamble + one for NUL - one for last (missing) comma */
426 length
+= strlen(preamble
);
427 ret
= zmalloc(length
);
431 strncpy(ret
, preamble
, length
);
432 for (i
= 0; i
< count
; i
++) {
433 strcat(ret
, names
[i
]);
434 if (i
!= count
- 1) {
443 * Compare list of exclusions against an event name.
444 * Return a list of legal exclusion names.
445 * Produce an error or a warning about others (depending on the situation)
448 int check_exclusion_subsets(const char *event_name
,
449 const char *exclusions
,
450 int *exclusion_count_ptr
,
451 char ***exclusion_list_ptr
)
453 const char *excluder_ptr
;
454 const char *event_ptr
;
455 const char *next_excluder
;
457 int exclusion_count
= 0;
458 char **exclusion_list
= NULL
;
459 int ret
= CMD_SUCCESS
;
461 if (event_name
[strlen(event_name
) - 1] != '*') {
462 ERR("Event %s: Excluders can only be used with wildcarded events", event_name
);
466 next_excluder
= exclusions
;
467 while (*next_excluder
!= 0) {
468 event_ptr
= event_name
;
469 excluder_ptr
= next_excluder
;
470 excluder_length
= strcspn(next_excluder
, ",");
472 /* Scan both the excluder and the event letter by letter */
480 /* Event is a subset of the excluder */
481 ERR("Event %s: %.*s excludes all events from %s",
490 char **new_exclusion_list
;
492 /* Excluder is a proper subset of event */
493 string
= lttng_strndup(next_excluder
, excluder_length
);
495 PERROR("lttng_strndup error");
498 new_exclusion_list
= realloc(exclusion_list
,
499 sizeof(char *) * (exclusion_count
+ 1));
500 if (!new_exclusion_list
) {
505 exclusion_list
= new_exclusion_list
;
507 exclusion_list
[exclusion_count
- 1] = string
;
511 /* Excluder and event sets have no common elements */
512 WARN("Event %s: %.*s does not exclude any events from %s",
523 next_excluder
+= excluder_length
;
524 if (*next_excluder
== ',') {
530 while (exclusion_count
--) {
531 free(exclusion_list
[exclusion_count
]);
533 if (exclusion_list
!= NULL
) {
534 free(exclusion_list
);
536 exclusion_list
= NULL
;
540 *exclusion_count_ptr
= exclusion_count
;
541 *exclusion_list_ptr
= exclusion_list
;
545 static void warn_on_truncated_exclusion_names(char **exclusion_list
,
546 int exclusion_count
, int *warn
)
550 for (i
= 0; i
< exclusion_count
; ++i
) {
551 const char *name
= exclusion_list
[i
];
552 size_t len
= strlen(name
);
554 if (len
>= LTTNG_SYMBOL_NAME_LEN
) {
555 WARN("Event exclusion \"%s\" will be truncated",
563 * Enabling event using the lttng API.
564 * Note: in case of error only the last error code will be return.
566 static int enable_events(char *session_name
)
568 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
569 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
570 char *event_name
, *channel_name
= NULL
;
571 struct lttng_event ev
;
572 struct lttng_domain dom
;
573 int exclusion_count
= 0;
574 char **exclusion_list
= NULL
;
576 memset(&ev
, 0, sizeof(ev
));
577 memset(&dom
, 0, sizeof(dom
));
581 WARN("Kernel loglevels are not supported.");
585 /* Create lttng domain */
587 dom
.type
= LTTNG_DOMAIN_KERNEL
;
588 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
589 } else if (opt_userspace
) {
590 dom
.type
= LTTNG_DOMAIN_UST
;
592 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
593 } else if (opt_jul
) {
594 dom
.type
= LTTNG_DOMAIN_JUL
;
596 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
597 } else if (opt_log4j
) {
598 dom
.type
= LTTNG_DOMAIN_LOG4J
;
600 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
601 } else if (opt_python
) {
602 dom
.type
= LTTNG_DOMAIN_PYTHON
;
604 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
606 /* Checked by the caller. */
612 case LTTNG_DOMAIN_KERNEL
:
613 case LTTNG_DOMAIN_JUL
:
614 case LTTNG_DOMAIN_LOG4J
:
615 case LTTNG_DOMAIN_PYTHON
:
616 ERR("Event name exclusions are not yet implemented for %s events",
617 get_domain_str(dom
.type
));
620 case LTTNG_DOMAIN_UST
:
621 /* Exclusions supported */
628 channel_name
= opt_channel_name
;
630 handle
= lttng_create_handle(session_name
, &dom
);
631 if (handle
== NULL
) {
638 /* Open a events element */
639 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
646 if (opt_enable_all
) {
647 /* Default setup for enable all */
649 ev
.type
= opt_event_type
;
650 strcpy(ev
.name
, "*");
651 /* kernel loglevels not implemented */
652 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
654 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
655 strcpy(ev
.name
, "*");
656 ev
.loglevel_type
= opt_loglevel_type
;
658 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
660 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
661 } else if (opt_jul
) {
662 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
663 } else if (opt_log4j
) {
664 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
665 } else if (opt_python
) {
666 ev
.loglevel
= loglevel_python_str_to_value(opt_loglevel
);
668 if (ev
.loglevel
== -1) {
669 ERR("Unknown loglevel %s", opt_loglevel
);
670 ret
= -LTTNG_ERR_INVALID
;
674 assert(opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
);
677 } else if (opt_jul
) {
678 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
679 } else if (opt_log4j
) {
680 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
681 } else if (opt_python
) {
682 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
688 ret
= check_exclusion_subsets("*", opt_exclude
,
689 &exclusion_count
, &exclusion_list
);
690 if (ret
== CMD_ERROR
) {
695 warn_on_truncated_exclusion_names(exclusion_list
,
696 exclusion_count
, &warn
);
699 ret
= lttng_enable_event_with_exclusions(handle
,
702 exclusion_count
, exclusion_list
);
705 case LTTNG_ERR_KERN_EVENT_EXIST
:
706 WARN("Kernel events already enabled (channel %s, session %s)",
707 print_channel_name(channel_name
), session_name
);
710 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
712 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
713 ERR("Events: %s (channel %s, session %s)",
715 print_channel_name(channel_name
),
721 ERR("Events: %s (channel %s, session %s)",
723 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
724 ? print_raw_channel_name(channel_name
)
725 : print_channel_name(channel_name
),
733 switch (opt_event_type
) {
734 case LTTNG_EVENT_TRACEPOINT
:
735 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
736 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
738 if (!exclusion_string
) {
739 PERROR("Cannot allocate exclusion_string");
743 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
744 get_domain_str(dom
.type
),
746 print_channel_name(channel_name
),
748 free(exclusion_string
);
750 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
752 if (!exclusion_string
) {
753 PERROR("Cannot allocate exclusion_string");
757 MSG("All %s tracepoints%s are enabled in channel %s",
758 get_domain_str(dom
.type
),
760 print_channel_name(channel_name
));
761 free(exclusion_string
);
764 case LTTNG_EVENT_SYSCALL
:
766 MSG("All %s system calls are enabled in channel %s",
767 get_domain_str(dom
.type
),
768 print_channel_name(channel_name
));
771 case LTTNG_EVENT_ALL
:
772 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
773 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
775 if (!exclusion_string
) {
776 PERROR("Cannot allocate exclusion_string");
780 MSG("All %s events%s are enabled in channel %s for loglevel %s",
781 get_domain_str(dom
.type
),
783 print_channel_name(channel_name
),
785 free(exclusion_string
);
787 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
789 if (!exclusion_string
) {
790 PERROR("Cannot allocate exclusion_string");
794 MSG("All %s events%s are enabled in channel %s",
795 get_domain_str(dom
.type
),
797 print_channel_name(channel_name
));
798 free(exclusion_string
);
803 * We should not be here since lttng_enable_event should have
804 * failed on the event type.
811 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
812 opt_filter
, exclusion_count
, exclusion_list
);
813 if (command_ret
< 0) {
814 switch (-command_ret
) {
815 case LTTNG_ERR_FILTER_EXIST
:
816 WARN("Filter on all events is already enabled"
817 " (channel %s, session %s)",
818 print_channel_name(channel_name
), session_name
);
821 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
823 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
824 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
826 print_channel_name(channel_name
),
827 session_name
, opt_filter
);
832 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
833 lttng_strerror(command_ret
),
834 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
835 ? print_raw_channel_name(channel_name
)
836 : print_channel_name(channel_name
),
837 session_name
, opt_filter
);
841 error_holder
= command_ret
;
844 MSG("Filter '%s' successfully set", opt_filter
);
849 /* The wildcard * is used for kernel and ust domain to
850 * represent ALL. We copy * in event name to force the wildcard use
853 * Note: this is strictly for semantic and printing while in
854 * machine interface mode.
856 strcpy(ev
.name
, "*");
858 /* If we reach here the events are enabled */
859 if (!error
&& !warn
) {
865 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
871 /* print exclusion */
872 ret
= mi_print_exclusion(exclusion_count
, exclusion_list
);
879 ret
= mi_lttng_writer_write_element_bool(writer
,
880 mi_lttng_element_command_success
, success
);
886 /* Close event element */
887 ret
= mi_lttng_writer_close_element(writer
);
897 /* Strip event list */
898 event_name
= strtok(opt_event_list
, ",");
899 while (event_name
!= NULL
) {
900 /* Copy name and type of the event */
901 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
902 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
903 ev
.type
= opt_event_type
;
905 /* Kernel tracer action */
907 DBG("Enabling kernel event %s for channel %s",
909 print_channel_name(channel_name
));
911 switch (opt_event_type
) {
912 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
913 /* If event name differs from *, select tracepoint. */
914 if (strcmp(ev
.name
, "*")) {
915 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
918 case LTTNG_EVENT_TRACEPOINT
:
920 case LTTNG_EVENT_PROBE
:
921 ret
= parse_probe_opts(&ev
, opt_probe
);
923 ERR("Unable to parse probe options");
928 case LTTNG_EVENT_FUNCTION
:
929 ret
= parse_probe_opts(&ev
, opt_function
);
931 ERR("Unable to parse function probe options");
936 case LTTNG_EVENT_SYSCALL
:
937 ev
.type
= LTTNG_EVENT_SYSCALL
;
944 /* kernel loglevels not implemented */
945 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
946 } else if (opt_userspace
) { /* User-space tracer action */
947 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
948 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
950 switch (opt_event_type
) {
951 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
953 case LTTNG_EVENT_TRACEPOINT
:
954 /* Copy name and type of the event */
955 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
956 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
957 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
959 case LTTNG_EVENT_PROBE
:
960 case LTTNG_EVENT_FUNCTION
:
961 case LTTNG_EVENT_SYSCALL
:
963 ERR("Event type not available for user-space tracing");
964 ret
= CMD_UNSUPPORTED
;
970 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
971 ERR("Exclusion option can only be used with tracepoint events");
975 /* Free previously allocated items */
976 if (exclusion_list
!= NULL
) {
977 while (exclusion_count
--) {
978 free(exclusion_list
[exclusion_count
]);
980 free(exclusion_list
);
981 exclusion_list
= NULL
;
983 /* Check for proper subsets */
984 ret
= check_exclusion_subsets(event_name
, opt_exclude
,
985 &exclusion_count
, &exclusion_list
);
986 if (ret
== CMD_ERROR
) {
990 warn_on_truncated_exclusion_names(
991 exclusion_list
, exclusion_count
, &warn
);
994 ev
.loglevel_type
= opt_loglevel_type
;
996 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
997 if (ev
.loglevel
== -1) {
998 ERR("Unknown loglevel %s", opt_loglevel
);
999 ret
= -LTTNG_ERR_INVALID
;
1005 } else if (opt_jul
|| opt_log4j
|| opt_python
) {
1006 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1007 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1008 ERR("Event type not supported for domain.");
1009 ret
= CMD_UNSUPPORTED
;
1013 ev
.loglevel_type
= opt_loglevel_type
;
1016 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1017 } else if (opt_log4j
) {
1018 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1019 } else if (opt_python
) {
1020 ev
.loglevel
= loglevel_python_str_to_value(opt_loglevel
);
1022 if (ev
.loglevel
== -1) {
1023 ERR("Unknown loglevel %s", opt_loglevel
);
1024 ret
= -LTTNG_ERR_INVALID
;
1029 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1030 } else if (opt_log4j
) {
1031 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1032 } else if (opt_python
) {
1033 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1036 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1037 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1038 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1044 char *exclusion_string
;
1046 command_ret
= lttng_enable_event_with_exclusions(handle
,
1048 NULL
, exclusion_count
, exclusion_list
);
1049 exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
1050 if (!exclusion_string
) {
1051 PERROR("Cannot allocate exclusion_string");
1055 if (command_ret
< 0) {
1056 /* Turn ret to positive value to handle the positive error code */
1057 switch (-command_ret
) {
1058 case LTTNG_ERR_KERN_EVENT_EXIST
:
1059 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1062 print_channel_name(channel_name
), session_name
);
1065 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1067 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1068 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1071 print_channel_name(channel_name
),
1077 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1079 lttng_strerror(command_ret
),
1080 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1081 ? print_raw_channel_name(channel_name
)
1082 : print_channel_name(channel_name
),
1087 error_holder
= command_ret
;
1090 case LTTNG_DOMAIN_KERNEL
:
1091 case LTTNG_DOMAIN_UST
:
1092 MSG("%s event %s%s created in channel %s",
1093 get_domain_str(dom
.type
),
1096 print_channel_name(channel_name
));
1098 case LTTNG_DOMAIN_JUL
:
1099 case LTTNG_DOMAIN_LOG4J
:
1100 case LTTNG_DOMAIN_PYTHON
:
1102 * Don't print the default channel
1103 * name for agent domains.
1105 MSG("%s event %s%s enabled",
1106 get_domain_str(dom
.type
),
1114 free(exclusion_string
);
1118 char *exclusion_string
;
1120 /* Filter present */
1123 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
1124 opt_filter
, exclusion_count
, exclusion_list
);
1125 exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
1126 if (!exclusion_string
) {
1127 PERROR("Cannot allocate exclusion_string");
1131 if (command_ret
< 0) {
1132 switch (-command_ret
) {
1133 case LTTNG_ERR_FILTER_EXIST
:
1134 WARN("Filter on event %s%s is already enabled"
1135 " (channel %s, session %s)",
1138 print_channel_name(channel_name
), session_name
);
1141 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1143 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1144 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1147 print_channel_name(channel_name
),
1148 session_name
, opt_filter
);
1153 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1155 lttng_strerror(command_ret
),
1156 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1157 ? print_raw_channel_name(channel_name
)
1158 : print_channel_name(channel_name
),
1159 session_name
, opt_filter
);
1163 error_holder
= command_ret
;
1166 MSG("Event %s%s: Filter '%s' successfully set",
1167 event_name
, exclusion_string
,
1170 free(exclusion_string
);
1181 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
1187 /* print exclusion */
1188 ret
= mi_print_exclusion(exclusion_count
, exclusion_list
);
1195 ret
= mi_lttng_writer_write_element_bool(writer
,
1196 mi_lttng_element_command_success
, success
);
1202 /* Close event element */
1203 ret
= mi_lttng_writer_close_element(writer
);
1211 event_name
= strtok(NULL
, ",");
1212 /* Reset warn, error and success */
1219 /* Close events element */
1220 ret
= mi_lttng_writer_close_element(writer
);
1233 lttng_destroy_handle(handle
);
1235 if (exclusion_list
!= NULL
) {
1236 while (exclusion_count
--) {
1237 free(exclusion_list
[exclusion_count
]);
1239 free(exclusion_list
);
1242 /* Overwrite ret with error_holder if there was an actual error with
1243 * enabling an event.
1245 ret
= error_holder
? error_holder
: ret
;
1251 * Add event to trace session
1253 int cmd_enable_events(int argc
, const char **argv
)
1255 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1256 static poptContext pc
;
1257 char *session_name
= NULL
;
1258 int event_type
= -1;
1260 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1261 poptReadDefaultConfig(pc
, 0);
1263 /* Default event type */
1264 opt_event_type
= LTTNG_EVENT_ALL
;
1266 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1271 case OPT_TRACEPOINT
:
1272 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1275 opt_event_type
= LTTNG_EVENT_PROBE
;
1278 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1281 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1287 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1288 opt_loglevel
= poptGetOptArg(pc
);
1290 case OPT_LOGLEVEL_ONLY
:
1291 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1292 opt_loglevel
= poptGetOptArg(pc
);
1294 case OPT_LIST_OPTIONS
:
1295 list_cmd_options(stdout
, long_options
);
1302 ret
= CMD_UNDEFINED
;
1306 /* Validate event type. Multiple event type are not supported. */
1307 if (event_type
== -1) {
1308 event_type
= opt_event_type
;
1310 if (event_type
!= opt_event_type
) {
1311 ERR("Multiple event type not supported.");
1318 ret
= print_missing_or_multiple_domains(
1319 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
1327 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1329 ret
= -LTTNG_ERR_NOMEM
;
1333 /* Open command element */
1334 ret
= mi_lttng_writer_command_open(writer
,
1335 mi_lttng_element_command_enable_event
);
1341 /* Open output element */
1342 ret
= mi_lttng_writer_open_element(writer
,
1343 mi_lttng_element_command_output
);
1350 opt_event_list
= (char*) poptGetArg(pc
);
1351 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1352 ERR("Missing event name(s).\n");
1357 if (!opt_session_name
) {
1358 session_name
= get_session_name();
1359 if (session_name
== NULL
) {
1360 command_ret
= CMD_ERROR
;
1365 session_name
= opt_session_name
;
1368 command_ret
= enable_events(session_name
);
1377 /* Close output element */
1378 ret
= mi_lttng_writer_close_element(writer
);
1384 ret
= mi_lttng_writer_write_element_bool(writer
,
1385 mi_lttng_element_command_success
, success
);
1391 /* Command element close */
1392 ret
= mi_lttng_writer_command_close(writer
);
1401 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1402 /* Preserve original error code */
1403 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1406 if (opt_session_name
== NULL
) {
1410 /* Overwrite ret if an error occurred in enable_events */
1411 ret
= command_ret
? command_ret
: ret
;
1413 poptFreeContext(pc
);