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 char *opt_loglevel
;
44 static int opt_loglevel_type
;
45 static int opt_kernel
;
46 static char *opt_session_name
;
47 static int opt_domain
;
48 static int opt_enable_all
;
49 static char *opt_probe
;
50 static char *opt_function
;
51 static char *opt_channel_name
;
52 static char *opt_filter
;
53 static char *opt_exclude
;
54 static char *opt_template_path
;
69 static struct lttng_handle
*handle
;
70 static struct mi_writer
*writer
;
72 static struct poptOption long_options
[] = {
73 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
74 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
75 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
76 {"kernel", 'k', POPT_ARG_VAL
, &opt_domain
, LTTNG_DOMAIN_KERNEL
, 0, 0},
77 {"userspace", 'u', POPT_ARG_VAL
, &opt_domain
, LTTNG_DOMAIN_UST
, 0, 0},
78 {"jul", 'j', POPT_ARG_VAL
, &opt_domain
, LTTNG_DOMAIN_JUL
, 0, 0},
79 {"log4j", 'l', POPT_ARG_VAL
, &opt_domain
, LTTNG_DOMAIN_LOG4J
, 0, 0},
80 {"python", 'p', POPT_ARG_VAL
, &opt_domain
, LTTNG_DOMAIN_PYTHON
, 0, 0},
81 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
82 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
83 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
84 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
85 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
86 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
87 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
88 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
90 {"session", 's', POPT_ARG_NONE
, 0, 0, 0, 0},
91 {"template-path", 't', POPT_ARG_NONE
, 0, 0, 0, 0},
95 static struct poptOption global_long_options
[] = {
96 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
97 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
98 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
99 {"template-path", 't', POPT_ARG_STRING
, &opt_template_path
, 0, 0, 0},
100 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
101 {0, 0, 0, 0, 0, 0, 0}
104 * Parse probe options.
106 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
108 int ret
= CMD_SUCCESS
;
111 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
112 char name
[LTTNG_SYMBOL_NAME_LEN
];
119 /* Check for symbol+offset */
120 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
121 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
123 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
124 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
125 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
126 if (*s_hex
== '\0') {
127 ERR("Invalid probe offset %s", s_hex
);
131 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
132 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
133 ev
->attr
.probe
.addr
= 0;
137 /* Check for symbol */
138 if (isalpha(name
[0])) {
139 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
142 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
143 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
144 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
145 ev
->attr
.probe
.offset
= 0;
146 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
147 ev
->attr
.probe
.addr
= 0;
152 /* Check for address */
153 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
155 if (*s_hex
== '\0') {
156 ERR("Invalid probe address %s", s_hex
);
160 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
161 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
162 ev
->attr
.probe
.offset
= 0;
163 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
175 * Maps LOG4j loglevel from string to value
177 static int loglevel_log4j_str_to_value(const char *inputstr
)
180 char str
[LTTNG_SYMBOL_NAME_LEN
];
182 if (!inputstr
|| strlen(inputstr
) == 0) {
187 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
188 * added at the end of the loop so a the upper bound we avoid the overflow.
190 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
191 str
[i
] = toupper(inputstr
[i
]);
196 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
197 return LTTNG_LOGLEVEL_LOG4J_OFF
;
198 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
199 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
200 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
201 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
202 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
203 return LTTNG_LOGLEVEL_LOG4J_WARN
;
204 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
205 return LTTNG_LOGLEVEL_LOG4J_INFO
;
206 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
207 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
208 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
209 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
210 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
211 return LTTNG_LOGLEVEL_LOG4J_ALL
;
218 * Maps JUL loglevel from string to value
220 static int loglevel_jul_str_to_value(const char *inputstr
)
223 char str
[LTTNG_SYMBOL_NAME_LEN
];
225 if (!inputstr
|| strlen(inputstr
) == 0) {
230 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
231 * added at the end of the loop so a the upper bound we avoid the overflow.
233 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
234 str
[i
] = toupper(inputstr
[i
]);
239 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
240 return LTTNG_LOGLEVEL_JUL_OFF
;
241 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
242 return LTTNG_LOGLEVEL_JUL_SEVERE
;
243 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
244 return LTTNG_LOGLEVEL_JUL_WARNING
;
245 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
246 return LTTNG_LOGLEVEL_JUL_INFO
;
247 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
248 return LTTNG_LOGLEVEL_JUL_CONFIG
;
249 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
250 return LTTNG_LOGLEVEL_JUL_FINE
;
251 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
252 return LTTNG_LOGLEVEL_JUL_FINER
;
253 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
254 return LTTNG_LOGLEVEL_JUL_FINEST
;
255 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
256 return LTTNG_LOGLEVEL_JUL_ALL
;
263 * Maps Python loglevel from string to value
265 static int loglevel_python_str_to_value(const char *inputstr
)
268 char str
[LTTNG_SYMBOL_NAME_LEN
];
270 if (!inputstr
|| strlen(inputstr
) == 0) {
275 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
276 * added at the end of the loop so a the upper bound we avoid the overflow.
278 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
279 str
[i
] = toupper(inputstr
[i
]);
284 if (!strcmp(str
, "PYTHON_CRITICAL") || !strcmp(str
, "CRITICAL")) {
285 return LTTNG_LOGLEVEL_PYTHON_CRITICAL
;
286 } else if (!strcmp(str
, "PYTHON_ERROR") || !strcmp(str
, "ERROR")) {
287 return LTTNG_LOGLEVEL_PYTHON_ERROR
;
288 } else if (!strcmp(str
, "PYTHON_WARNING") || !strcmp(str
, "WARNING")) {
289 return LTTNG_LOGLEVEL_PYTHON_WARNING
;
290 } else if (!strcmp(str
, "PYTHON_INFO") || !strcmp(str
, "INFO")) {
291 return LTTNG_LOGLEVEL_PYTHON_INFO
;
292 } else if (!strcmp(str
, "PYTNON_DEBUG") || !strcmp(str
, "DEBUG")) {
293 return LTTNG_LOGLEVEL_PYTHON_DEBUG
;
294 } else if (!strcmp(str
, "PYTHON_NOTSET") || !strcmp(str
, "NOTSET")) {
295 return LTTNG_LOGLEVEL_PYTHON_NOTSET
;
302 * Maps loglevel from string to value
305 int loglevel_ust_str_to_value(const char *inputstr
)
308 char str
[LTTNG_SYMBOL_NAME_LEN
];
310 if (!inputstr
|| strlen(inputstr
) == 0) {
315 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
316 * added at the end of the loop so a the upper bound we avoid the overflow.
318 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
319 str
[i
] = toupper(inputstr
[i
]);
323 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
324 return LTTNG_LOGLEVEL_EMERG
;
325 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
326 return LTTNG_LOGLEVEL_ALERT
;
327 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
328 return LTTNG_LOGLEVEL_CRIT
;
329 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
330 return LTTNG_LOGLEVEL_ERR
;
331 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
332 return LTTNG_LOGLEVEL_WARNING
;
333 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
334 return LTTNG_LOGLEVEL_NOTICE
;
335 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
336 return LTTNG_LOGLEVEL_INFO
;
337 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
338 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
339 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
340 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
341 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
342 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
343 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
344 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
345 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
346 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
347 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
348 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
349 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
350 return LTTNG_LOGLEVEL_DEBUG_LINE
;
351 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
352 return LTTNG_LOGLEVEL_DEBUG
;
359 * Map a userspace agent loglevel to it's value based on the domain type.
361 * Assert when loglevel is NULL and domain type is LTTNG_DOMAIN_NONE ||
362 * LTTNG_DOMAIN_KERNEL.
364 * return -1 on invalid loglevel.
366 static int loglevel_str_to_value(const char* loglevel
, enum lttng_domain_type type
)
370 case LTTNG_DOMAIN_UST
:
371 ret
= loglevel_ust_str_to_value(loglevel
);
373 case LTTNG_DOMAIN_JUL
:
374 ret
= loglevel_jul_str_to_value(loglevel
);
376 case LTTNG_DOMAIN_LOG4J
:
377 ret
= loglevel_log4j_str_to_value(loglevel
);
379 case LTTNG_DOMAIN_PYTHON
:
380 ret
= loglevel_python_str_to_value(loglevel
);
390 const char *print_channel_name(const char *name
)
392 return name
? : DEFAULT_CHANNEL_NAME
;
396 const char *print_raw_channel_name(const char *name
)
398 return name
? : "<default>";
402 * Mi print exlcusion list
405 int mi_print_exclusion(int count
, char **names
)
415 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
420 for (i
= 0; i
< count
; i
++) {
421 ret
= mi_lttng_writer_write_element_string(writer
,
422 config_element_exclusion
, names
[i
]);
428 /* Close exclusions element */
429 ret
= mi_lttng_writer_close_element(writer
);
436 * Return allocated string for pretty-printing exclusion names.
439 char *print_exclusions(int count
, char **names
)
443 const char *preamble
= " excluding ";
450 /* calculate total required length */
451 for (i
= 0; i
< count
; i
++) {
452 length
+= strlen(names
[i
]) + 1;
455 /* add length of preamble + one for NUL - one for last (missing) comma */
456 length
+= strlen(preamble
);
457 ret
= zmalloc(length
);
461 strncpy(ret
, preamble
, length
);
462 for (i
= 0; i
< count
; i
++) {
463 strcat(ret
, names
[i
]);
464 if (i
!= count
- 1) {
473 * Return an allocated string for pretty printing of exclusion of an event from
476 * e.g: test,test1,test2
479 char *print_exclusions_config(const struct config_element
*event
)
481 const char *path
= "/event/exclusions/exclusion";
482 struct config_element
**element_array
= NULL
;
483 int element_array_size
= 0;
484 char **exclusion_str_array
= NULL
;
485 char *return_string
= NULL
;
488 config_element_get_element_array(event
, path
, &element_array
, &element_array_size
);
489 if (element_array_size
== 0) {
490 return_string
= strdup("");
494 exclusion_str_array
= calloc(element_array_size
, sizeof(char *));
495 if (!exclusion_str_array
) {
496 ERR("calloc exclusion string array");
497 return_string
= NULL
;
501 /* Fetch data and get full length */
502 for (int i
= 0; i
< element_array_size
; i
++) {
503 exclusion_str_array
[i
] = config_element_get_element_value(element_array
[i
], "/exclusion");
504 if (!exclusion_str_array
[i
]) {
505 ERR("Fecthing exlusion %d of event config element", i
);
508 length
+= strlen(exclusion_str_array
[i
]) + 1;
511 return_string
= zmalloc(length
);
512 if (!return_string
) {
513 return_string
= NULL
;
517 /* Construct string */
518 for (int i
= 0; i
< element_array_size
; i
++) {
519 if (!exclusion_str_array
[i
]) {
523 strcat(return_string
, exclusion_str_array
[i
]);
524 if (i
!= element_array_size
- 1) {
525 strcat(return_string
, ",");
530 config_element_free_array(element_array
, element_array_size
);
531 if (exclusion_str_array
) {
532 for (int i
= 0; i
< element_array_size
; i
++) {
533 free(exclusion_str_array
[i
]);
536 free(exclusion_str_array
);
538 return return_string
;
542 * Compare list of exclusions against an event name.
543 * Return a list of legal exclusion names.
544 * Produce an error or a warning about others (depending on the situation)
547 int check_exclusion_subsets(const char *event_name
,
548 const char *exclusions
,
549 int *exclusion_count_ptr
,
550 char ***exclusion_list_ptr
)
552 const char *excluder_ptr
;
553 const char *event_ptr
;
554 const char *next_excluder
;
556 int exclusion_count
= 0;
557 char **exclusion_list
= NULL
;
558 int ret
= CMD_SUCCESS
;
560 if (event_name
[strlen(event_name
) - 1] != '*') {
561 ERR("Event %s: Excluders can only be used with wildcarded events", event_name
);
565 next_excluder
= exclusions
;
566 while (*next_excluder
!= 0) {
567 event_ptr
= event_name
;
568 excluder_ptr
= next_excluder
;
569 excluder_length
= strcspn(next_excluder
, ",");
571 /* Scan both the excluder and the event letter by letter */
579 /* Event is a subset of the excluder */
580 ERR("Event %s: %.*s excludes all events from %s",
589 char **new_exclusion_list
;
591 /* Excluder is a proper subset of event */
592 string
= lttng_strndup(next_excluder
, excluder_length
);
594 PERROR("lttng_strndup error");
597 new_exclusion_list
= realloc(exclusion_list
,
598 sizeof(char *) * (exclusion_count
+ 1));
599 if (!new_exclusion_list
) {
604 exclusion_list
= new_exclusion_list
;
606 exclusion_list
[exclusion_count
- 1] = string
;
610 /* Excluder and event sets have no common elements */
611 WARN("Event %s: %.*s does not exclude any events from %s",
622 next_excluder
+= excluder_length
;
623 if (*next_excluder
== ',') {
629 while (exclusion_count
--) {
630 free(exclusion_list
[exclusion_count
]);
632 if (exclusion_list
!= NULL
) {
633 free(exclusion_list
);
635 exclusion_list
= NULL
;
639 *exclusion_count_ptr
= exclusion_count
;
640 *exclusion_list_ptr
= exclusion_list
;
644 static void warn_on_truncated_exclusion_names(char **exclusion_list
,
645 int exclusion_count
, int *warn
)
649 for (i
= 0; i
< exclusion_count
; ++i
) {
650 const char *name
= exclusion_list
[i
];
651 size_t len
= strlen(name
);
653 if (len
>= LTTNG_SYMBOL_NAME_LEN
) {
654 WARN("Event exclusion \"%s\" will be truncated",
661 struct domain_configuration
{
667 enum lttng_domain_type domain_type
;
676 * Enabling event using the lttng API.
677 * Note: in case of error only the last error code will be return.
679 static int enable_events(char *session_name
, struct domain_configuration
*config
)
681 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
682 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
683 char *event_name
, *channel_name
= NULL
;
684 struct lttng_event ev
;
685 struct lttng_domain dom
;
686 int exclusion_count
= 0;
687 char **exclusion_list
= NULL
;
689 int config_enable_all
;
690 int config_event_type
;
691 char *config_event_list
;
692 char *config_loglevel
;
693 int config_loglevel_type
;
694 enum lttng_domain_type config_domain_type
;
696 char *config_function
;
697 char *config_channel_name
;
699 char *config_exclude
;
703 memset(&ev
, 0, sizeof(ev
));
704 memset(&dom
, 0, sizeof(dom
));
706 config_enable_all
= config
->enable_all
;
707 config_event_type
= config
->event_type
;
708 config_event_list
= config
->event_list
;
709 config_loglevel
= config
->loglevel
;
710 config_loglevel_type
= config
->loglevel_type
;
711 config_domain_type
= config
->domain_type
;
712 config_probe
= config
->probe
;
713 config_function
= config
->function
;
714 config_channel_name
= config
->channel_name
;
715 config_filter
= config
->filter
;
716 config_exclude
= config
->exclude
;
719 /* Create lttng domain */
720 dom
.type
= config_domain_type
;
721 switch (config_domain_type
) {
722 case LTTNG_DOMAIN_KERNEL
:
723 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
725 case LTTNG_DOMAIN_UST
:
726 case LTTNG_DOMAIN_JUL
:
727 case LTTNG_DOMAIN_LOG4J
:
728 case LTTNG_DOMAIN_PYTHON
:
729 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
731 case LTTNG_DOMAIN_NONE
:
738 channel_name
= config_channel_name
;
740 handle
= lttng_create_handle(session_name
, &dom
);
741 if (handle
== NULL
) {
748 /* Open a domain element */
749 ret
= mi_lttng_writer_open_element(writer
, config_element_domain
);
755 /* Specify the domain type */
756 ret
= mi_lttng_writer_write_element_string(writer
,
758 mi_lttng_domaintype_string(config_domain_type
));
764 /* Open a events element */
765 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
772 if (config_enable_all
) {
773 /* Default setup for enable all */
774 if (config_domain_type
== LTTNG_DOMAIN_KERNEL
) {
775 ev
.type
= config_event_type
;
776 strcpy(ev
.name
, "*");
777 /* kernel loglevels not implemented */
778 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
780 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
781 strcpy(ev
.name
, "*");
782 ev
.loglevel_type
= config_loglevel_type
;
783 if (config_loglevel
) {
784 ev
.loglevel
= loglevel_str_to_value(config_loglevel
, config_domain_type
);
785 if (ev
.loglevel
== -1) {
786 ERR("Unknown loglevel %s", config_loglevel
);
787 ret
= -LTTNG_ERR_INVALID
;
791 assert(config_domain_type
!= LTTNG_DOMAIN_NONE
|| config_domain_type
!= LTTNG_DOMAIN_KERNEL
);
792 switch (config_domain_type
) {
793 case LTTNG_DOMAIN_UST
:
796 case LTTNG_DOMAIN_JUL
:
797 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
799 case LTTNG_DOMAIN_LOG4J
:
800 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
802 case LTTNG_DOMAIN_PYTHON
:
803 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
811 if (config_exclude
) {
812 ret
= check_exclusion_subsets("*", config_exclude
,
813 &exclusion_count
, &exclusion_list
);
814 if (ret
== CMD_ERROR
) {
819 warn_on_truncated_exclusion_names(exclusion_list
,
820 exclusion_count
, &warn
);
822 if (!config_filter
) {
823 ret
= lttng_enable_event_with_exclusions(handle
,
826 exclusion_count
, exclusion_list
);
829 case LTTNG_ERR_KERN_EVENT_EXIST
:
830 WARN("Kernel events already enabled (channel %s, session %s)",
831 print_channel_name(channel_name
), session_name
);
834 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
836 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
837 ERR("Events: %s (domain %s, channel %s, session %s)",
839 get_domain_str(dom
.type
),
840 print_channel_name(channel_name
),
846 ERR("Events: %s (domain %s, channel %s, session %s)",
848 get_domain_str(dom
.type
),
849 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
850 ? print_raw_channel_name(channel_name
)
851 : print_channel_name(channel_name
),
859 switch (config_event_type
) {
860 case LTTNG_EVENT_TRACEPOINT
:
861 if (config_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
862 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
864 if (!exclusion_string
) {
865 PERROR("Cannot allocate exclusion_string");
869 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
870 get_domain_str(dom
.type
),
872 print_channel_name(channel_name
),
874 free(exclusion_string
);
876 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
878 if (!exclusion_string
) {
879 PERROR("Cannot allocate exclusion_string");
883 MSG("All %s tracepoints%s are enabled in channel %s",
884 get_domain_str(dom
.type
),
886 print_channel_name(channel_name
));
887 free(exclusion_string
);
890 case LTTNG_EVENT_SYSCALL
:
892 MSG("All %s system calls are enabled in channel %s",
893 get_domain_str(dom
.type
),
894 print_channel_name(channel_name
));
897 case LTTNG_EVENT_ALL
:
898 if (config_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
899 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
901 if (!exclusion_string
) {
902 PERROR("Cannot allocate exclusion_string");
906 MSG("All %s events%s are enabled in channel %s for loglevel %s",
907 get_domain_str(dom
.type
),
909 print_channel_name(channel_name
),
911 free(exclusion_string
);
913 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
915 if (!exclusion_string
) {
916 PERROR("Cannot allocate exclusion_string");
920 MSG("All %s events%s are enabled in channel %s",
921 get_domain_str(dom
.type
),
923 print_channel_name(channel_name
));
924 free(exclusion_string
);
929 * We should not be here since lttng_enable_event should have
930 * failed on the event type.
937 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
938 config_filter
, exclusion_count
, exclusion_list
);
939 if (command_ret
< 0) {
940 switch (-command_ret
) {
941 case LTTNG_ERR_FILTER_EXIST
:
942 WARN("Filter on all events is already enabled"
943 " (domain %s, channel %s, session %s)",
944 get_domain_str(dom
.type
),
945 print_channel_name(channel_name
), session_name
);
948 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
950 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
951 ERR("All events: %s (domain %s, channel %s, session %s, filter \'%s\')",
953 get_domain_str(dom
.type
),
954 print_channel_name(channel_name
),
955 session_name
, config_filter
);
960 ERR("All events: %s (domain %s, channel %s, session %s, filter \'%s\')",
961 lttng_strerror(command_ret
),
962 get_domain_str(dom
.type
),
963 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
964 ? print_raw_channel_name(channel_name
)
965 : print_channel_name(channel_name
),
966 session_name
, config_filter
);
970 error_holder
= command_ret
;
973 MSG("Filter '%s' successfully set", config_filter
);
978 /* The wildcard * is used for kernel and ust domain to
979 * represent ALL. We copy * in event name to force the wildcard use
982 * Note: this is strictly for semantic and printing while in
983 * machine interface mode.
985 strcpy(ev
.name
, "*");
987 /* If we reach here the events are enabled */
988 if (!error
&& !warn
) {
994 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
1000 /* print exclusion */
1001 ret
= mi_print_exclusion(exclusion_count
, exclusion_list
);
1008 ret
= mi_lttng_writer_write_element_bool(writer
,
1009 mi_lttng_element_command_success
, success
);
1015 /* Close event element */
1016 ret
= mi_lttng_writer_close_element(writer
);
1026 /* Strip event list */
1027 event_name
= strtok(config_event_list
, ",");
1028 while (event_name
!= NULL
) {
1029 /* Copy name and type of the event */
1030 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1031 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1032 ev
.type
= config_event_type
;
1034 /* Kernel tracer action */
1035 switch (config_domain_type
) {
1036 case LTTNG_DOMAIN_KERNEL
:
1037 DBG("Enabling kernel event %s for channel %s",
1039 print_channel_name(channel_name
));
1041 switch (config_event_type
) {
1042 case LTTNG_EVENT_ALL
: /* Enable tracepoints and syscalls */
1043 /* If event name differs from *, select tracepoint. */
1044 if (strcmp(ev
.name
, "*")) {
1045 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1048 case LTTNG_EVENT_TRACEPOINT
:
1050 case LTTNG_EVENT_PROBE
:
1051 ret
= parse_probe_opts(&ev
, config_probe
);
1053 ERR("Unable to parse probe options");
1058 case LTTNG_EVENT_FUNCTION
:
1059 ret
= parse_probe_opts(&ev
, config_function
);
1061 ERR("Unable to parse function probe options");
1066 case LTTNG_EVENT_SYSCALL
:
1067 ev
.type
= LTTNG_EVENT_SYSCALL
;
1070 ret
= CMD_UNDEFINED
;
1074 /* kernel loglevels not implemented */
1075 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1077 case LTTNG_DOMAIN_UST
:
1078 /* User-space tracer action */
1079 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1080 print_channel_name(channel_name
), config_loglevel
? : "<all>");
1082 switch (config_event_type
) {
1083 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1085 case LTTNG_EVENT_TRACEPOINT
:
1086 /* Copy name and type of the event */
1087 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1088 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1089 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1091 case LTTNG_EVENT_PROBE
:
1092 case LTTNG_EVENT_FUNCTION
:
1093 case LTTNG_EVENT_SYSCALL
:
1095 ERR("Event type not available for user-space tracing");
1096 ret
= CMD_UNSUPPORTED
;
1100 if (config_exclude
) {
1102 if (config_event_type
!= LTTNG_EVENT_ALL
&& config_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1103 ERR("Exclusion option can only be used with tracepoint events");
1107 /* Free previously allocated items */
1108 if (exclusion_list
!= NULL
) {
1109 while (exclusion_count
--) {
1110 free(exclusion_list
[exclusion_count
]);
1112 free(exclusion_list
);
1113 exclusion_list
= NULL
;
1115 /* Check for proper subsets */
1116 ret
= check_exclusion_subsets(event_name
, config_exclude
,
1117 &exclusion_count
, &exclusion_list
);
1118 if (ret
== CMD_ERROR
) {
1122 warn_on_truncated_exclusion_names(
1123 exclusion_list
, exclusion_count
, &warn
);
1126 ev
.loglevel_type
= config_loglevel_type
;
1127 if (config_loglevel
) {
1128 ev
.loglevel
= loglevel_ust_str_to_value(config_loglevel
);
1129 if (ev
.loglevel
== -1) {
1130 ERR("Unknown loglevel %s", config_loglevel
);
1131 ret
= -LTTNG_ERR_INVALID
;
1138 case LTTNG_DOMAIN_JUL
:
1139 case LTTNG_DOMAIN_LOG4J
:
1140 case LTTNG_DOMAIN_PYTHON
:
1141 if (config_event_type
!= LTTNG_EVENT_ALL
&&
1142 config_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1143 ERR("Event type not supported for domain.");
1144 ret
= CMD_UNSUPPORTED
;
1148 ev
.loglevel_type
= config_loglevel_type
;
1149 if (config_loglevel
) {
1150 ev
.loglevel
= loglevel_str_to_value(config_loglevel
, config_domain_type
);
1151 if (ev
.loglevel
== -1) {
1152 ERR("Unknown loglevel %s", config_loglevel
);
1153 ret
= -LTTNG_ERR_INVALID
;
1157 switch (config_domain_type
) {
1158 case LTTNG_DOMAIN_JUL
:
1159 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1161 case LTTNG_DOMAIN_LOG4J
:
1162 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1164 case LTTNG_DOMAIN_PYTHON
:
1165 ev
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1172 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1173 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1174 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1180 if (!config_filter
) {
1181 char *exclusion_string
;
1183 command_ret
= lttng_enable_event_with_exclusions(handle
,
1185 NULL
, exclusion_count
, exclusion_list
);
1186 exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
1187 if (!exclusion_string
) {
1188 PERROR("Cannot allocate exclusion_string");
1192 if (command_ret
< 0) {
1193 /* Turn ret to positive value to handle the positive error code */
1194 switch (-command_ret
) {
1195 case LTTNG_ERR_KERN_EVENT_EXIST
:
1196 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1199 print_channel_name(channel_name
), session_name
);
1202 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1204 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1205 ERR("Event %s%s: %s (domain %s,channel %s, session %s)", event_name
,
1208 get_domain_str(dom
.type
),
1209 print_channel_name(channel_name
),
1215 ERR("Event %s%s: %s (domain %s, channel %s, session %s)", event_name
,
1217 lttng_strerror(command_ret
),
1218 get_domain_str(dom
.type
),
1219 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1220 ? print_raw_channel_name(channel_name
)
1221 : print_channel_name(channel_name
),
1226 error_holder
= command_ret
;
1229 case LTTNG_DOMAIN_KERNEL
:
1230 case LTTNG_DOMAIN_UST
:
1231 MSG("%s event %s%s created in channel %s",
1232 get_domain_str(dom
.type
),
1235 print_channel_name(channel_name
));
1237 case LTTNG_DOMAIN_JUL
:
1238 case LTTNG_DOMAIN_LOG4J
:
1239 case LTTNG_DOMAIN_PYTHON
:
1241 * Don't print the default channel
1242 * name for agent domains.
1244 MSG("%s event %s%s enabled",
1245 get_domain_str(dom
.type
),
1253 free(exclusion_string
);
1256 if (config_filter
) {
1257 char *exclusion_string
;
1259 /* Filter present */
1262 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
1263 config_filter
, exclusion_count
, exclusion_list
);
1264 exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
1265 if (!exclusion_string
) {
1266 PERROR("Cannot allocate exclusion_string");
1270 if (command_ret
< 0) {
1271 switch (-command_ret
) {
1272 case LTTNG_ERR_FILTER_EXIST
:
1273 WARN("Filter on event %s%s is already enabled"
1274 " (domain %s, channel %s, session %s)",
1277 get_domain_str(dom
.type
),
1278 print_channel_name(channel_name
), session_name
);
1281 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1283 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1284 ERR("Event %s%s: %s (domain %s, channel %s, session %s, filter \'%s\')", ev
.name
,
1287 get_domain_str(dom
.type
),
1288 print_channel_name(channel_name
),
1289 session_name
, config_filter
);
1294 ERR("Event %s%s: %s (domain %s, channel %s, session %s, filter \'%s\')", ev
.name
,
1296 lttng_strerror(command_ret
),
1297 get_domain_str(dom
.type
),
1298 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1299 ? print_raw_channel_name(channel_name
)
1300 : print_channel_name(channel_name
),
1301 session_name
, config_filter
);
1305 error_holder
= command_ret
;
1308 MSG("Event %s%s: Filter '%s' for domain %s successfully set",
1309 event_name
, exclusion_string
,
1311 get_domain_str(dom
.type
));
1313 free(exclusion_string
);
1324 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
1330 /* print exclusion */
1331 ret
= mi_print_exclusion(exclusion_count
, exclusion_list
);
1338 ret
= mi_lttng_writer_write_element_bool(writer
,
1339 mi_lttng_element_command_success
, success
);
1345 /* Close event element */
1346 ret
= mi_lttng_writer_close_element(writer
);
1354 event_name
= strtok(NULL
, ",");
1355 /* Reset warn, error and success */
1362 /* Close events and domain element */
1363 ret
= mi_lttng_close_multi_element(writer
, 2);
1376 lttng_destroy_handle(handle
);
1378 if (exclusion_list
!= NULL
) {
1379 while (exclusion_count
--) {
1380 free(exclusion_list
[exclusion_count
]);
1382 free(exclusion_list
);
1385 /* Overwrite ret with error_holder if there was an actual error with
1386 * enabling an event.
1388 ret
= error_holder
? error_holder
: ret
;
1393 struct exclusions_tuple
{
1394 char **exclusion_list
;
1395 int exclusion_count
;
1398 static int enable_event_template_per_domain(const struct config_document
*document
,
1399 const char* session_name
,
1400 const struct domain_configuration
*config
)
1405 int printed_bytes
= 0;
1407 struct config_element
**element_array
= NULL
;
1408 int element_array_size
= 0;
1409 struct config_element
*config_loglevel_type
= NULL
;
1410 struct config_element
*config_loglevel
= NULL
;
1411 struct config_element
*config_filter
= NULL
;
1412 struct exclusions_tuple
*event_exclusion_array
= NULL
;
1418 /* Fetch contexts element */
1419 printed_bytes
= asprintf(&query
, "//sessions/session/domains/domain[./type = '%s']/channels/channel/contexts", config_get_domain_str(config
->domain_type
));
1420 if (printed_bytes
<= 0) {
1421 ERR("Asprintf template events query");
1426 config_document_get_element_array(document
, query
, &element_array
, &element_array_size
);
1427 if (element_array
) {
1428 if (element_array_size
!= 1) {
1429 ERR("Invalid document");
1432 ret
= config_process_contexts_element(element_array
[0], session_name
, config
->domain_type
, config
->channel_name
);
1434 ERR("Contexts processing for domain %s channel %s : %s", config_get_domain_str(config
->domain_type
), config
->channel_name
, lttng_strerror(ret
));
1437 config_element_free_array(element_array
, element_array_size
);
1438 element_array
= NULL
;
1439 element_array_size
= 0;
1443 /* Fetch event element */
1444 printed_bytes
= asprintf(&query
, "//sessions/session/domains/domain[./type = '%s']/channels/channel/events/event[./enabled = 'true']", config_get_domain_str(config
->domain_type
));
1445 if (printed_bytes
<= 0) {
1446 ERR("Asprintf template events query");
1451 config_document_get_element_array(document
, query
, &element_array
, &element_array_size
);
1452 if (!element_array
) {
1459 * Only userspace domain accept loglevel.
1461 * config-> loglevel is only set when the user pass --logleve/
1462 * --loglevel-only on the command line so use it as a way to
1463 * know if a loglevel override is necessary
1465 if (config
->domain_type
!= LTTNG_DOMAIN_KERNEL
&& config
->loglevel
) {
1466 char *loglevel_value_str
= NULL
;
1467 int loglevel_value
= -1;
1469 loglevel_value
= loglevel_str_to_value(config
->loglevel
, config
->domain_type
);
1470 if (loglevel_value
== -1) {
1471 ERR("Unknown loglevel %s", config
->loglevel
);
1476 printed_bytes
= asprintf(&loglevel_value_str
, "%d", loglevel_value
);
1477 if (printed_bytes
<= 0 ) {
1478 ERR("Asprintf loglevel");
1483 config_loglevel
= config_element_create(config_element_loglevel
, loglevel_value_str
);
1484 free(loglevel_value_str
);
1485 if (!config_loglevel
) {
1486 ERR("Loglevel config creation failed");
1491 config_loglevel_type
= config_element_create(config_element_loglevel_type
, config_get_loglevel_type_string(config
->loglevel_type
));
1492 if (!config_loglevel_type
) {
1493 ERR("Loglevel type config creattion failed");
1499 /* Handle the filter */
1500 if (config
->filter
) {
1501 config_filter
= config_element_create(config_element_filter
, config
->filter
);
1502 if (!config_filter
) {
1503 ERR("Filter config creattion failed");
1510 * Handle exclusion on a per event base
1511 * For now only userspace domains can have exclusions.
1513 event_exclusion_array
= calloc(element_array_size
, sizeof(struct exclusions_tuple
));
1514 if (!event_exclusion_array
) {
1515 ERR("Calloc exclusion list array");
1521 if (config
->domain_type
!= LTTNG_DOMAIN_KERNEL
&& config
->exclude
) {
1522 for (int i
= 0; i
< element_array_size
; i
++) {
1523 struct config_element
*cur_event
= element_array
[i
];
1524 char ***exclusion_list
= &(event_exclusion_array
[i
].exclusion_list
);
1525 int *exclusion_count
= &(event_exclusion_array
[i
].exclusion_count
);
1526 char *event_type_str
= NULL
;
1528 char *event_name
= NULL
;
1530 *exclusion_list
= NULL
;
1531 *exclusion_count
= 0;
1533 /* Get event name */
1534 event_name
= config_element_get_element_value(cur_event
, "/event/name");
1536 ERR("Reading event name from config element");
1539 event_type_str
= config_element_get_element_value(cur_event
, "/event/type");
1540 if (!event_type_str
) {
1541 ERR("Reading event type from config element");
1546 event_type
= config_get_event_type(event_type_str
);
1547 if (event_type
!= LTTNG_EVENT_ALL
&& event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1548 const char *msg
= "Exclusions do not apply to this event";
1549 WARN("Event %s: %s exclusions: %s (domain %s, channel %s, session %s)",
1550 event_name
, msg
, config
->exclude
,
1551 get_domain_str(config
->domain_type
),
1552 print_channel_name(config
->channel_name
),
1554 free(event_type_str
);
1559 /* Check for proper subsets */
1560 ret
= check_exclusion_subsets(event_name
, config
->exclude
,
1561 exclusion_count
, exclusion_list
);
1563 warn_on_truncated_exclusion_names(
1564 *exclusion_list
, *exclusion_count
, &warn
);
1565 free(event_type_str
);
1571 * Create valid events config_element and try to enable them one by one
1573 for (int i
= 0; i
< element_array_size
; i
++) {
1574 const char *sub_msg
= NULL
;
1575 bool success
= false;
1578 struct config_element
*cur_event
= element_array
[i
];
1579 struct config_element
*success_element
= NULL
;
1581 char *exclusions_string
= NULL
;
1582 char *filter_string
= NULL
;
1583 char *event_name
= NULL
;
1585 event_name
= config_element_get_element_value(cur_event
, "/event/name");
1587 sub_msg
= "event name not present abort event creation ";
1588 event_name
= strdup("<Error fetching event name>");
1589 goto enable_event_continue
;
1592 /* Add the loglevel */
1593 if (config_loglevel
&& config_loglevel_type
) {
1594 config_element_add_or_replace_child(cur_event
, config_loglevel
);
1595 config_element_add_or_replace_child(cur_event
, config_loglevel_type
);
1597 if (config_filter
) {
1598 config_element_add_or_replace_child(cur_event
, config_filter
);
1601 if (event_exclusion_array
[i
].exclusion_list
) {
1602 char **exclusion_list
= event_exclusion_array
[i
].exclusion_list
;
1603 const int exclusion_count
= event_exclusion_array
[i
].exclusion_count
;
1604 struct config_element
*config_exclusions
= NULL
;
1605 struct config_element
*exclusion
= NULL
;
1607 config_exclusions
= config_element_create(config_element_exclusions
, NULL
);
1608 if (!config_exclusions
) {
1609 sub_msg
= "Exclusions config element ration failed abort event creation";
1610 goto enable_event_continue
;
1613 for (int j
= 0; j
< exclusion_count
; j
++) {
1614 exclusion
= config_element_create(config_element_exclusion
, exclusion_list
[j
]);
1616 sub_msg
= "Exclusion config element creation failed abort event creation";
1617 config_element_free(config_exclusions
);
1618 goto enable_event_continue
;
1620 ret
= config_element_add_child(config_exclusions
, exclusion
);
1622 sub_msg
= "Exclusion config element child addition failed abort event creation";
1623 config_element_free(config_exclusions
);
1624 config_element_free(exclusion
);
1625 goto enable_event_continue
;
1628 config_element_free(exclusion
);
1631 ret
= config_element_add_or_replace_child(cur_event
, config_exclusions
);
1633 sub_msg
= "Exclusions config element child addition failed abort event creation";
1634 config_element_free(config_exclusions
);
1635 goto enable_event_continue
;
1637 config_element_free(config_exclusions
);
1641 ret
= config_process_event_element(cur_event
, session_name
, config
->domain_type
, config
->channel_name
);
1644 sub_msg
= "created";
1646 /* Mi element insertion */
1647 success_element
= config_element_create(mi_lttng_element_command_success
, "true");
1648 if (success_element
) {
1649 ret
= config_element_add_or_replace_child(cur_event
, success_element
);
1660 sub_msg
= lttng_strerror(ret
);
1661 if (-ret
== LTTNG_ERR_UST_EVENT_ENABLED
|| -ret
== LTTNG_ERR_KERN_EVENT_EXIST
) {
1662 /*This is not an error */
1666 sub_msg
= "creation failed";
1669 /* Mi related insertion */
1670 success_element
= config_element_create(mi_lttng_element_command_success
, "false");
1671 if (success_element
) {
1672 ret
= config_element_add_or_replace_child(cur_event
, success_element
);
1681 enable_event_continue
:
1683 /* Get exclusions string for printing */
1684 exclusions_string
= print_exclusions_config(cur_event
);
1685 filter_string
= config_element_get_element_value(cur_event
, "/event/filter");
1688 /* Domain is already present inside the error or msg */
1689 printed_bytes
= asprintf(&msg
,"%s%sEvent %s: %s (exclusions: [%s] filter: [%s] session %s, channel %s)",
1690 success
? get_domain_str(config
->domain_type
): "",
1695 * TODO: print actual exclusion of loaded event
1697 exclusions_string
? : "",
1699 * TODO: print actual exclusion of loaded event
1701 filter_string
? : "",
1703 print_channel_name(config
->channel_name
));
1705 if (printed_bytes
> 0 && success
) {
1707 } else if (printed_bytes
> 0 && warn
) {
1709 /* At least one event failed */
1711 } else if (printed_bytes
> 0) {
1714 ERR("Asprintf enable event message");
1715 /* At least one event failed */
1718 config_element_free(success_element
);
1719 free(exclusions_string
);
1721 free(filter_string
);
1728 /* Open a domain element */
1729 ret
= mi_lttng_writer_open_element(writer
, config_element_domain
);
1735 /* Specify the domain type */
1736 ret
= mi_lttng_writer_write_element_string(writer
,
1737 config_element_type
,
1738 mi_lttng_domaintype_string(config
->domain_type
));
1744 /* Open a events element */
1745 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
1751 for (int i
= 0; i
< element_array_size
; i
++) {
1752 ret
= mi_lttng_writer_write_config_element(writer
,
1760 ret
= mi_lttng_close_multi_element(writer
, 2);
1769 /* Free exclusion allocated items */
1770 if (event_exclusion_array
!= NULL
) {
1771 for (int i
= 0; i
< element_array_size
; i
++) {
1772 char **exclusion_list
= event_exclusion_array
[i
].exclusion_list
;
1773 int exclusion_count
= event_exclusion_array
[i
].exclusion_count
;
1774 if (exclusion_list
!= NULL
) {
1775 while (exclusion_count
--) {
1776 free(exclusion_list
[exclusion_count
]);
1779 free(exclusion_list
);
1780 exclusion_list
= NULL
;
1782 free(event_exclusion_array
);
1784 config_element_free_array(element_array
, element_array_size
);
1785 config_element_free(config_loglevel
);
1786 config_element_free(config_loglevel_type
);
1787 config_element_free(config_filter
);
1796 static int enable_event_from_template(const struct config_document
*document
,
1797 const char* session_name
,
1798 const struct domain_configuration
*kernel_config
,
1799 const struct domain_configuration
*ust_config
,
1800 const struct domain_configuration
*jul_config
,
1801 const struct domain_configuration
*log4j_config
,
1802 const struct domain_configuration
*python_config
)
1807 ret
= enable_event_template_per_domain(document
, session_name
, kernel_config
);
1811 ret
= enable_event_template_per_domain(document
, session_name
, ust_config
);
1815 ret
= enable_event_template_per_domain(document
, session_name
, jul_config
);
1819 ret
= enable_event_template_per_domain(document
, session_name
, log4j_config
);
1823 ret
= enable_event_template_per_domain(document
, session_name
, python_config
);
1832 * Add event to trace session
1836 int argv_index_start
;
1841 static struct domain_configuration
*initialize_domain_configuration(enum lttng_domain_type type
)
1844 struct domain_configuration
*config
= malloc(sizeof(struct domain_configuration
));
1851 case LTTNG_DOMAIN_KERNEL
:
1852 config
->domain_type
= LTTNG_DOMAIN_KERNEL
;
1854 case LTTNG_DOMAIN_UST
:
1855 config
->domain_type
= LTTNG_DOMAIN_UST
;
1857 case LTTNG_DOMAIN_JUL
:
1858 config
->domain_type
= LTTNG_DOMAIN_JUL
;
1860 case LTTNG_DOMAIN_LOG4J
:
1861 config
->domain_type
= LTTNG_DOMAIN_LOG4J
;
1863 case LTTNG_DOMAIN_PYTHON
:
1864 config
->domain_type
= LTTNG_DOMAIN_PYTHON
;
1866 case LTTNG_DOMAIN_NONE
:
1873 config
->event_type
= LTTNG_EVENT_ALL
;
1874 config
->enable_all
= 0;
1875 config
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1876 config
->loglevel
= NULL
;
1877 config
->probe
= NULL
;
1878 config
->function
= NULL
;
1879 config
->channel_name
= NULL
;
1880 config
->filter
= NULL
;
1881 config
->exclude
= NULL
;
1882 config
->event_list
= NULL
;
1887 int cmd_enable_events(int argc
, const char **argv
)
1889 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1890 static poptContext pc
;
1891 char *session_name
= NULL
;
1892 int event_type
= -1;
1894 int args_tuple_count
= 0;
1895 int arg_state_looking_for_end
= 0;
1896 struct args_tuple
*args_tuple_list
= NULL
;
1897 struct domain_configuration
*tmp_config
= NULL
;
1899 struct config_document
*template = NULL
;
1901 struct domain_configuration
*jul_config
= NULL
;
1902 struct domain_configuration
*kernel_config
= NULL
;
1903 struct domain_configuration
*log4j_config
= NULL
;
1904 struct domain_configuration
*python_config
= NULL
;
1905 struct domain_configuration
*ust_config
= NULL
;
1909 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1911 ret
= -LTTNG_ERR_NOMEM
;
1915 /* Open command element */
1916 ret
= mi_lttng_writer_command_open(writer
,
1917 mi_lttng_element_command_enable_event
);
1923 /* Open output element */
1924 ret
= mi_lttng_writer_open_element(writer
,
1925 mi_lttng_element_command_output
);
1931 /* Open the domains element */
1932 ret
= mi_lttng_domains_open(writer
);
1940 /* Parse global arguments */
1941 pc
= poptGetContext(NULL
, argc
, argv
, global_long_options
, 0);
1942 poptReadDefaultConfig(pc
, 0);
1945 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1950 case OPT_LIST_OPTIONS
:
1951 list_cmd_options(stdout
, long_options
);
1958 /* Dispose the global arguments context */
1959 poptFreeContext(pc
);
1962 if (!opt_session_name
) {
1963 session_name
= get_session_name();
1964 if (session_name
== NULL
) {
1965 command_ret
= CMD_ERROR
;
1970 session_name
= opt_session_name
;
1973 /* Find the number of domain based on the passed arguments */
1974 for (i
= 1; i
< argc
; i
++) {
1976 if (strcmp("-u", argv
[i
]) && strcmp("--userspace", argv
[i
]) &&
1977 strcmp("-j", argv
[i
]) && strcmp("--jul", argv
[i
]) &&
1978 strcmp("-l", argv
[i
]) && strcmp("--log4j", argv
[i
]) &&
1979 strcmp("-p", argv
[i
]) && strcmp("--python", argv
[i
]) &&
1980 strcmp("-k", argv
[i
]) && strcmp("--kernel", argv
[i
])) {
1985 struct args_tuple
*tmp_pointer
= NULL
;
1987 tmp_pointer
= realloc(args_tuple_list
, sizeof(struct args_tuple
) * args_tuple_count
);
1989 ERR("Realoc of args tuple failed");
1993 args_tuple_list
= tmp_pointer
;
1995 if (!arg_state_looking_for_end
) {
1996 if (args_tuple_count
-1 < 0) {
1997 ERR("Args parsing illegal state");
2001 args_tuple_list
[args_tuple_count
-1].argv_index_start
= i
;
2002 arg_state_looking_for_end
= 1;
2004 if (args_tuple_count
- 2 < 0 || args_tuple_count
-1 < 0) {
2005 ERR("Args parsing illegal state");
2010 /* Close the previous tuple */
2011 args_tuple_list
[args_tuple_count
-2].argv_index_end
= i
- 1;
2013 /* Start the new tuple */
2014 args_tuple_list
[args_tuple_count
-1].argv_index_start
= i
;
2018 if (args_tuple_count
== 0 && !opt_template_path
) {
2019 ret
= print_missing_or_multiple_domains(0);
2025 } else if (args_tuple_count
> 0) {
2026 /* Close the last tuple */
2027 args_tuple_list
[args_tuple_count
-1].argv_index_end
= i
- 1;
2029 if (args_tuple_count
== 1) {
2030 /* Preserve the old way with a domain flag that can be anywhere */
2031 args_tuple_list
[0].argv_index_start
= 1;
2035 for (i
= 0; i
< args_tuple_count
; i
++) {
2036 struct args_tuple
*tuple
= &args_tuple_list
[i
];
2037 int cur_argc
= tuple
->argv_index_end
- tuple
-> argv_index_start
+ 1;
2038 const char **cur_argv
= &argv
[tuple
->argv_index_start
];
2040 /* Default options */
2043 opt_domain
= LTTNG_DOMAIN_NONE
;
2047 opt_event_type
= LTTNG_EVENT_ALL
;
2048 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2049 opt_event_list
= NULL
;
2050 opt_loglevel
= NULL
;
2052 opt_function
= NULL
;
2053 opt_channel_name
= NULL
;
2057 pc
= poptGetContext(NULL
, cur_argc
, cur_argv
, long_options
, POPT_CONTEXT_KEEP_FIRST
);
2058 poptReadDefaultConfig(pc
, 0);
2060 /* Default event type */
2061 opt_event_type
= LTTNG_EVENT_ALL
;
2063 while ((opt
= poptGetNextOpt(pc
)) != -1) {
2068 case OPT_TRACEPOINT
:
2069 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
2072 opt_event_type
= LTTNG_EVENT_PROBE
;
2075 opt_event_type
= LTTNG_EVENT_FUNCTION
;
2078 opt_event_type
= LTTNG_EVENT_SYSCALL
;
2081 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2082 opt_loglevel
= poptGetOptArg(pc
);
2084 case OPT_LOGLEVEL_ONLY
:
2085 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2086 opt_loglevel
= poptGetOptArg(pc
);
2088 case OPT_LIST_OPTIONS
:
2089 list_cmd_options(stdout
, long_options
);
2096 ret
= CMD_UNDEFINED
;
2100 /* Validate event type. Multiple event type are not supported. */
2101 if (event_type
== -1) {
2102 event_type
= opt_event_type
;
2104 if (event_type
!= opt_event_type
) {
2105 ERR("Multiple event type not supported.");
2112 tmp_config
= initialize_domain_configuration(opt_domain
);
2114 opt_event_list
= (char*) poptGetArg(pc
);
2115 if (opt_event_list
== NULL
&& opt_enable_all
== 0 && !opt_template_path
) {
2116 ERR("Missing event name(s).\n");
2122 if (opt_domain
== LTTNG_DOMAIN_KERNEL
) {
2124 WARN("Kernel loglevels are not supported.");
2129 switch (opt_domain
) {
2130 case LTTNG_DOMAIN_KERNEL
:
2131 case LTTNG_DOMAIN_JUL
:
2132 case LTTNG_DOMAIN_LOG4J
:
2133 case LTTNG_DOMAIN_PYTHON
:
2134 ERR("Event name exclusions are not yet implemented for %s events",
2135 get_domain_str(opt_domain
));
2138 case LTTNG_DOMAIN_UST
:
2139 /* Exclusions supported */
2146 if (opt_template_path
) {
2147 if (event_type
!= LTTNG_EVENT_ALL
) {
2148 WARN("Type options for events while using a template have no effect (--function,--probe,--syscall,--tracepoint).");
2150 if (opt_enable_all
) {
2151 WARN("The all (-a) shortcut for enabling all events while using a template have no effect.");
2155 tmp_config
->event_type
= event_type
;
2156 tmp_config
->enable_all
= opt_enable_all
;
2157 tmp_config
->loglevel_type
= opt_loglevel_type
;
2158 tmp_config
->loglevel
= opt_loglevel
;
2159 tmp_config
->probe
= opt_probe
;
2160 tmp_config
->function
= opt_function
;
2161 tmp_config
->channel_name
= opt_channel_name
;
2162 tmp_config
->filter
= opt_filter
;
2163 tmp_config
->exclude
= opt_exclude
;
2164 tmp_config
->event_list
= opt_event_list
;
2167 switch(opt_domain
) {
2168 case LTTNG_DOMAIN_KERNEL
:
2169 if (kernel_config
) {
2170 ERR("Only one -k option is permitted per command");
2174 kernel_config
= tmp_config
;
2176 case LTTNG_DOMAIN_UST
:
2178 ERR("Only one -u option is permitted per command");
2182 ust_config
= tmp_config
;
2184 case LTTNG_DOMAIN_JUL
:
2186 ERR("Only one -j option is permitted per command");
2190 jul_config
= tmp_config
;
2192 case LTTNG_DOMAIN_LOG4J
:
2194 ERR("Only one -l option is permitted per command");
2198 log4j_config
= tmp_config
;
2200 case LTTNG_DOMAIN_PYTHON
:
2201 if (python_config
) {
2202 ERR("Only one -p option is permitted per command");
2206 python_config
= tmp_config
;
2208 case LTTNG_DOMAIN_NONE
:
2216 poptFreeContext(pc
);
2221 if (opt_template_path
) {
2222 /* validate template */
2223 template = config_document_get(opt_template_path
, 0);
2225 ERR("Could not load the template");
2229 /* TODO: validate the xml */
2230 /* Only one session, only one channel per domain */
2231 if (!kernel_config
) {
2232 kernel_config
= initialize_domain_configuration(LTTNG_DOMAIN_KERNEL
);
2233 if (!kernel_config
) {
2234 ERR("Default initialization for kernel domain configuration");
2241 ust_config
= initialize_domain_configuration(LTTNG_DOMAIN_UST
);
2243 ERR("Default initialization for ust domain configuration");
2250 jul_config
= initialize_domain_configuration(LTTNG_DOMAIN_JUL
);
2252 ERR("Default initialization for jul domain configuration");
2258 if (!log4j_config
) {
2259 log4j_config
= initialize_domain_configuration(LTTNG_DOMAIN_LOG4J
);
2260 if (!log4j_config
) {
2261 ERR("Default initialization for log4j domain configuration");
2267 if (!python_config
) {
2268 python_config
= initialize_domain_configuration(LTTNG_DOMAIN_PYTHON
);
2269 if (!python_config
) {
2270 ERR("Default initialization for python domain configuration");
2276 command_ret
= enable_event_from_template(template, session_name
, kernel_config
, ust_config
,
2277 jul_config
, log4j_config
, python_config
);
2282 if (kernel_config
) {
2283 command_ret
= enable_events(session_name
, kernel_config
);
2290 command_ret
= enable_events(session_name
, ust_config
);
2297 command_ret
= enable_events(session_name
, jul_config
);
2304 command_ret
= enable_events(session_name
, log4j_config
);
2310 if (python_config
) {
2311 command_ret
= enable_events(session_name
, python_config
);
2320 /* Close domains and output element */
2321 ret
= mi_lttng_close_multi_element(writer
, 2);
2327 ret
= mi_lttng_writer_write_element_bool(writer
,
2328 mi_lttng_element_command_success
, success
);
2334 /* Command element close */
2335 ret
= mi_lttng_writer_command_close(writer
);
2344 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2345 /* Preserve original error code */
2346 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
2349 if (opt_session_name
== NULL
) {
2353 /* Overwrite ret if an error occurred in enable_events */
2354 ret
= command_ret
? command_ret
: ret
;
2356 config_document_free(template);
2357 free(args_tuple_list
);
2360 free(kernel_config
);
2362 free(python_config
);
2364 poptFreeContext(pc
);