2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
30 #include <common/defaults.h>
31 #include <common/error.h>
32 #include <common/macros.h>
33 #include <common/utils.h>
34 #include <common/compat/getenv.h>
35 #include <lttng/lttng-error.h>
36 #include <libxml/parser.h>
37 #include <libxml/valid.h>
38 #include <libxml/xmlschemas.h>
39 #include <libxml/tree.h>
40 #include <lttng/lttng.h>
41 #include <lttng/snapshot.h>
43 #include "session-config.h"
44 #include "config-internal.h"
46 struct handler_filter_args
{
48 config_entry_handler_cb handler
;
52 struct session_config_validation_ctx
{
53 xmlSchemaParserCtxtPtr parser_ctx
;
55 xmlSchemaValidCtxtPtr schema_validation_ctx
;
58 const char * const config_str_yes
= "yes";
59 const char * const config_str_true
= "true";
60 const char * const config_str_on
= "on";
61 const char * const config_str_no
= "no";
62 const char * const config_str_false
= "false";
63 const char * const config_str_off
= "off";
64 const char * const config_xml_encoding
= "UTF-8";
65 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
66 const char * const config_xml_indent_string
= "\t";
67 const char * const config_xml_true
= "true";
68 const char * const config_xml_false
= "false";
70 const char * const config_element_channel
= "channel";
71 const char * const config_element_channels
= "channels";
72 const char * const config_element_domain
= "domain";
73 const char * const config_element_domains
= "domains";
74 const char * const config_element_event
= "event";
75 const char * const config_element_events
= "events";
76 const char * const config_element_context
= "context";
77 const char * const config_element_contexts
= "contexts";
78 const char * const config_element_attributes
= "attributes";
79 const char * const config_element_exclusion
= "exclusion";
80 const char * const config_element_exclusions
= "exclusions";
81 const char * const config_element_function_attributes
= "function_attributes";
82 const char * const config_element_probe_attributes
= "probe_attributes";
83 const char * const config_element_symbol_name
= "symbol_name";
84 const char * const config_element_address
= "address";
85 const char * const config_element_offset
= "offset";
86 const char * const config_element_name
= "name";
87 const char * const config_element_enabled
= "enabled";
88 const char * const config_element_overwrite_mode
= "overwrite_mode";
89 const char * const config_element_subbuf_size
= "subbuffer_size";
90 const char * const config_element_num_subbuf
= "subbuffer_count";
91 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
92 const char * const config_element_read_timer_interval
= "read_timer_interval";
93 const char * const config_element_output
= "output";
94 const char * const config_element_output_type
= "output_type";
95 const char * const config_element_tracefile_size
= "tracefile_size";
96 const char * const config_element_tracefile_count
= "tracefile_count";
97 const char * const config_element_live_timer_interval
= "live_timer_interval";
98 const char * const config_element_type
= "type";
99 const char * const config_element_buffer_type
= "buffer_type";
100 const char * const config_element_session
= "session";
101 const char * const config_element_sessions
= "sessions";
102 const char * const config_element_context_perf
= "perf";
103 const char * const config_element_context_app
= "app";
104 const char * const config_element_context_app_provider_name
= "provider_name";
105 const char * const config_element_context_app_ctx_name
= "ctx_name";
106 const char * const config_element_config
= "config";
107 const char * const config_element_started
= "started";
108 const char * const config_element_snapshot_mode
= "snapshot_mode";
109 const char * const config_element_loglevel
= "loglevel";
110 const char * const config_element_loglevel_type
= "loglevel_type";
111 const char * const config_element_filter
= "filter";
112 const char * const config_element_filter_expression
= "filter_expression";
113 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
114 const char * const config_element_consumer_output
= "consumer_output";
115 const char * const config_element_destination
= "destination";
116 const char * const config_element_path
= "path";
117 const char * const config_element_net_output
= "net_output";
118 const char * const config_element_control_uri
= "control_uri";
119 const char * const config_element_data_uri
= "data_uri";
120 const char * const config_element_max_size
= "max_size";
121 const char * const config_element_pid
= "pid";
122 const char * const config_element_pids
= "pids";
123 const char * const config_element_shared_memory_path
= "shared_memory_path";
124 const char * const config_element_pid_tracker
= "pid_tracker";
125 const char * const config_element_trackers
= "trackers";
126 const char * const config_element_targets
= "targets";
127 const char * const config_element_target_pid
= "pid_target";
129 const char * const config_domain_type_kernel
= "KERNEL";
130 const char * const config_domain_type_ust
= "UST";
131 const char * const config_domain_type_jul
= "JUL";
132 const char * const config_domain_type_log4j
= "LOG4J";
133 const char * const config_domain_type_python
= "PYTHON";
135 const char * const config_buffer_type_per_pid
= "PER_PID";
136 const char * const config_buffer_type_per_uid
= "PER_UID";
137 const char * const config_buffer_type_global
= "GLOBAL";
139 const char * const config_overwrite_mode_discard
= "DISCARD";
140 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
142 const char * const config_output_type_splice
= "SPLICE";
143 const char * const config_output_type_mmap
= "MMAP";
145 const char * const config_loglevel_type_all
= "ALL";
146 const char * const config_loglevel_type_range
= "RANGE";
147 const char * const config_loglevel_type_single
= "SINGLE";
149 const char * const config_event_type_all
= "ALL";
150 const char * const config_event_type_tracepoint
= "TRACEPOINT";
151 const char * const config_event_type_probe
= "PROBE";
152 const char * const config_event_type_function
= "FUNCTION";
153 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
154 const char * const config_event_type_noop
= "NOOP";
155 const char * const config_event_type_syscall
= "SYSCALL";
156 const char * const config_event_type_kprobe
= "KPROBE";
157 const char * const config_event_type_kretprobe
= "KRETPROBE";
159 const char * const config_event_context_pid
= "PID";
160 const char * const config_event_context_procname
= "PROCNAME";
161 const char * const config_event_context_prio
= "PRIO";
162 const char * const config_event_context_nice
= "NICE";
163 const char * const config_event_context_vpid
= "VPID";
164 const char * const config_event_context_tid
= "TID";
165 const char * const config_event_context_vtid
= "VTID";
166 const char * const config_event_context_ppid
= "PPID";
167 const char * const config_event_context_vppid
= "VPPID";
168 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
169 const char * const config_event_context_hostname
= "HOSTNAME";
170 const char * const config_event_context_ip
= "IP";
171 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
172 const char * const config_event_context_app
= "APP";
175 struct consumer_output
{
182 static int config_entry_handler_filter(struct handler_filter_args
*args
,
183 const char *section
, const char *name
, const char *value
)
186 struct config_entry entry
= { section
, name
, value
};
190 if (!section
|| !name
|| !value
) {
196 if (strcmp(args
->section
, section
)) {
201 ret
= args
->handler(&entry
, args
->user_data
);
207 int config_get_section_entries(const char *override_path
, const char *section
,
208 config_entry_handler_cb handler
, void *user_data
)
212 FILE *config_file
= NULL
;
213 struct handler_filter_args filter
= { section
, handler
, user_data
};
215 /* First, try system-wide conf. file. */
216 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
218 config_file
= fopen(path
, "r");
220 DBG("Loading daemon conf file at %s", path
);
222 * Return value is not very important here since error or not, we
223 * continue and try the next possible conf. file.
225 (void) ini_parse_file(config_file
,
226 (ini_entry_handler
) config_entry_handler_filter
,
231 /* Second is the user local configuration. */
232 path
= utils_get_home_dir();
234 char fullpath
[PATH_MAX
];
236 ret
= snprintf(fullpath
, sizeof(fullpath
),
237 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
239 PERROR("snprintf user conf. path");
243 config_file
= fopen(fullpath
, "r");
245 DBG("Loading daemon user conf file at %s", path
);
247 * Return value is not very important here since error or not, we
248 * continue and try the next possible conf. file.
250 (void) ini_parse_file(config_file
,
251 (ini_entry_handler
) config_entry_handler_filter
,
257 /* Final path is the one that the user might have provided. */
259 config_file
= fopen(override_path
, "r");
261 DBG("Loading daemon command line conf file at %s", override_path
);
262 (void) ini_parse_file(config_file
,
263 (ini_entry_handler
) config_entry_handler_filter
,
267 ERR("Failed to open daemon configuration file at %s",
274 /* Everything went well. */
282 int config_parse_value(const char *value
)
285 char *endptr
, *lower_str
;
295 v
= strtoul(value
, &endptr
, 10);
296 if (endptr
!= value
) {
301 lower_str
= zmalloc(len
+ 1);
308 for (i
= 0; i
< len
; i
++) {
309 lower_str
[i
] = tolower(value
[i
]);
312 if (!strcmp(lower_str
, config_str_yes
) ||
313 !strcmp(lower_str
, config_str_true
) ||
314 !strcmp(lower_str
, config_str_on
)) {
316 } else if (!strcmp(lower_str
, config_str_no
) ||
317 !strcmp(lower_str
, config_str_false
) ||
318 !strcmp(lower_str
, config_str_off
)) {
330 * Returns a xmlChar string which must be released using xmlFree().
332 static xmlChar
*encode_string(const char *in_str
)
334 xmlChar
*out_str
= NULL
;
335 xmlCharEncodingHandlerPtr handler
;
336 int out_len
, ret
, in_len
;
340 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
342 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
346 in_len
= strlen(in_str
);
348 * Add 1 byte for the NULL terminted character. The factor 4 here is
349 * used because UTF-8 characters can take up to 4 bytes.
351 out_len
= (in_len
* 4) + 1;
352 out_str
= xmlMalloc(out_len
);
357 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
364 /* out_len is now the size of out_str */
365 out_str
[out_len
] = '\0';
371 struct config_writer
*config_writer_create(int fd_output
, int indent
)
374 struct config_writer
*writer
;
375 xmlOutputBufferPtr buffer
;
377 writer
= zmalloc(sizeof(struct config_writer
));
379 PERROR("zmalloc config_writer_create");
383 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
388 writer
->writer
= xmlNewTextWriter(buffer
);
389 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
390 config_xml_encoding
, NULL
);
395 ret
= xmlTextWriterSetIndentString(writer
->writer
,
396 BAD_CAST config_xml_indent_string
);
401 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
409 config_writer_destroy(writer
);
414 int config_writer_destroy(struct config_writer
*writer
)
423 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
424 WARN("Could not close XML document");
428 if (writer
->writer
) {
429 xmlFreeTextWriter(writer
->writer
);
438 int config_writer_open_element(struct config_writer
*writer
,
439 const char *element_name
)
442 xmlChar
*encoded_element_name
;
444 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
449 encoded_element_name
= encode_string(element_name
);
450 if (!encoded_element_name
) {
455 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
456 xmlFree(encoded_element_name
);
458 return ret
>= 0 ? 0 : ret
;
462 int config_writer_write_attribute(struct config_writer
*writer
,
463 const char *name
, const char *value
)
466 xmlChar
*encoded_name
= NULL
;
467 xmlChar
*encoded_value
= NULL
;
469 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
474 encoded_name
= encode_string(name
);
480 encoded_value
= encode_string(value
);
481 if (!encoded_value
) {
486 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
489 xmlFree(encoded_name
);
490 xmlFree(encoded_value
);
491 return ret
>= 0 ? 0 : ret
;
495 int config_writer_close_element(struct config_writer
*writer
)
499 if (!writer
|| !writer
->writer
) {
504 ret
= xmlTextWriterEndElement(writer
->writer
);
506 return ret
>= 0 ? 0 : ret
;
510 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
511 const char *element_name
, uint64_t value
)
514 xmlChar
*encoded_element_name
;
516 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
521 encoded_element_name
= encode_string(element_name
);
522 if (!encoded_element_name
) {
527 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
528 encoded_element_name
, "%" PRIu64
, value
);
529 xmlFree(encoded_element_name
);
531 return ret
>= 0 ? 0 : ret
;
535 int config_writer_write_element_signed_int(struct config_writer
*writer
,
536 const char *element_name
, int64_t value
)
539 xmlChar
*encoded_element_name
;
541 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
546 encoded_element_name
= encode_string(element_name
);
547 if (!encoded_element_name
) {
552 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
553 encoded_element_name
, "%" PRIi64
, value
);
554 xmlFree(encoded_element_name
);
556 return ret
>= 0 ? 0 : ret
;
560 int config_writer_write_element_bool(struct config_writer
*writer
,
561 const char *element_name
, int value
)
563 return config_writer_write_element_string(writer
, element_name
,
564 value
? config_xml_true
: config_xml_false
);
568 int config_writer_write_element_string(struct config_writer
*writer
,
569 const char *element_name
, const char *value
)
572 xmlChar
*encoded_element_name
= NULL
;
573 xmlChar
*encoded_value
= NULL
;
575 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
581 encoded_element_name
= encode_string(element_name
);
582 if (!encoded_element_name
) {
587 encoded_value
= encode_string(value
);
588 if (!encoded_value
) {
593 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
596 xmlFree(encoded_element_name
);
597 xmlFree(encoded_value
);
598 return ret
>= 0 ? 0 : ret
;
602 void xml_error_handler(void *ctx
, const char *format
, ...)
608 va_start(args
, format
);
609 ret
= vasprintf(&errMsg
, format
, args
);
612 ERR("String allocation failed in xml error handler");
616 fprintf(stderr
, "XML Error: %s", errMsg
);
621 void fini_session_config_validation_ctx(
622 struct session_config_validation_ctx
*ctx
)
624 if (ctx
->parser_ctx
) {
625 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
629 xmlSchemaFree(ctx
->schema
);
632 if (ctx
->schema_validation_ctx
) {
633 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
636 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
640 char *get_session_config_xsd_path()
643 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
644 size_t base_path_len
;
648 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
651 base_path_len
= strlen(base_path
);
652 max_path_len
= base_path_len
+
653 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
654 xsd_path
= zmalloc(max_path_len
);
659 strncpy(xsd_path
, base_path
, max_path_len
);
660 if (xsd_path
[base_path_len
- 1] != '/') {
661 xsd_path
[base_path_len
++] = '/';
664 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
665 max_path_len
- base_path_len
);
671 int init_session_config_validation_ctx(
672 struct session_config_validation_ctx
*ctx
)
675 char *xsd_path
= get_session_config_xsd_path();
678 ret
= -LTTNG_ERR_NOMEM
;
682 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
683 if (!ctx
->parser_ctx
) {
684 ERR("XSD parser context creation failed");
685 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
688 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
689 xml_error_handler
, NULL
);
691 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
693 ERR("XSD parsing failed");
694 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
698 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
699 if (!ctx
->schema_validation_ctx
) {
700 ERR("XSD validation context creation failed");
701 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
705 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
706 xml_error_handler
, NULL
);
711 fini_session_config_validation_ctx(ctx
);
719 int parse_uint(xmlChar
*str
, uint64_t *val
)
729 *val
= strtoull((const char *) str
, &endptr
, 10);
730 if (!endptr
|| *endptr
) {
741 int parse_int(xmlChar
*str
, int64_t *val
)
751 *val
= strtoll((const char *) str
, &endptr
, 10);
752 if (!endptr
|| *endptr
) {
763 int parse_bool(xmlChar
*str
, int *val
)
772 if (!strcmp((const char *) str
, config_xml_true
)) {
774 } else if (!strcmp((const char *) str
, config_xml_false
)) {
777 WARN("Invalid boolean value encoutered (%s).",
786 int get_domain_type(xmlChar
*domain
)
794 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
795 ret
= LTTNG_DOMAIN_KERNEL
;
796 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
797 ret
= LTTNG_DOMAIN_UST
;
798 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
799 ret
= LTTNG_DOMAIN_JUL
;
800 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
801 ret
= LTTNG_DOMAIN_LOG4J
;
802 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
803 ret
= LTTNG_DOMAIN_PYTHON
;
814 int get_buffer_type(xmlChar
*buffer_type
)
822 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
823 ret
= LTTNG_BUFFER_GLOBAL
;
824 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
825 ret
= LTTNG_BUFFER_PER_UID
;
826 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
827 ret
= LTTNG_BUFFER_PER_PID
;
838 int get_overwrite_mode(xmlChar
*overwrite_mode
)
842 if (!overwrite_mode
) {
846 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
848 } else if (!strcmp((char *) overwrite_mode
,
849 config_overwrite_mode_discard
)) {
861 int get_output_type(xmlChar
*output_type
)
869 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
870 ret
= LTTNG_EVENT_MMAP
;
871 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
872 ret
= LTTNG_EVENT_SPLICE
;
883 int get_event_type(xmlChar
*event_type
)
891 if (!strcmp((char *) event_type
, config_event_type_all
)) {
892 ret
= LTTNG_EVENT_ALL
;
893 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
894 ret
= LTTNG_EVENT_TRACEPOINT
;
895 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
896 ret
= LTTNG_EVENT_PROBE
;
897 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
898 ret
= LTTNG_EVENT_FUNCTION
;
899 } else if (!strcmp((char *) event_type
,
900 config_event_type_function_entry
)) {
901 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
902 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
903 ret
= LTTNG_EVENT_NOOP
;
904 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
905 ret
= LTTNG_EVENT_SYSCALL
;
916 int get_loglevel_type(xmlChar
*loglevel_type
)
920 if (!loglevel_type
) {
924 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
925 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
926 } else if (!strcmp((char *) loglevel_type
,
927 config_loglevel_type_range
)) {
928 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
929 } else if (!strcmp((char *) loglevel_type
,
930 config_loglevel_type_single
)) {
931 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
942 * Return the context type or -1 on error.
945 int get_context_type(xmlChar
*context_type
)
953 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
954 ret
= LTTNG_EVENT_CONTEXT_PID
;
955 } else if (!strcmp((char *) context_type
,
956 config_event_context_procname
)) {
957 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
958 } else if (!strcmp((char *) context_type
,
959 config_event_context_prio
)) {
960 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
961 } else if (!strcmp((char *) context_type
,
962 config_event_context_nice
)) {
963 ret
= LTTNG_EVENT_CONTEXT_NICE
;
964 } else if (!strcmp((char *) context_type
,
965 config_event_context_vpid
)) {
966 ret
= LTTNG_EVENT_CONTEXT_VPID
;
967 } else if (!strcmp((char *) context_type
,
968 config_event_context_tid
)) {
969 ret
= LTTNG_EVENT_CONTEXT_TID
;
970 } else if (!strcmp((char *) context_type
,
971 config_event_context_vtid
)) {
972 ret
= LTTNG_EVENT_CONTEXT_VTID
;
973 } else if (!strcmp((char *) context_type
,
974 config_event_context_ppid
)) {
975 ret
= LTTNG_EVENT_CONTEXT_PPID
;
976 } else if (!strcmp((char *) context_type
,
977 config_event_context_vppid
)) {
978 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
979 } else if (!strcmp((char *) context_type
,
980 config_event_context_pthread_id
)) {
981 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
982 } else if (!strcmp((char *) context_type
,
983 config_event_context_hostname
)) {
984 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
985 } else if (!strcmp((char *) context_type
,
986 config_event_context_ip
)) {
987 ret
= LTTNG_EVENT_CONTEXT_IP
;
998 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1003 for (node
= xmlFirstElementChild(domain_node
); node
;
1004 node
= xmlNextElementSibling(node
)) {
1005 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1007 xmlChar
*node_content
= xmlNodeGetContent(node
);
1008 if (!node_content
) {
1009 ret
= -LTTNG_ERR_NOMEM
;
1013 ret
= get_domain_type(node_content
);
1016 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1021 } else if (!strcmp((const char *) node
->name
,
1022 config_element_buffer_type
)) {
1024 xmlChar
*node_content
= xmlNodeGetContent(node
);
1025 if (!node_content
) {
1026 ret
= -LTTNG_ERR_NOMEM
;
1030 ret
= get_buffer_type(node_content
);
1033 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1037 domain
->buf_type
= ret
;
1046 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1051 for (node
= xmlFirstElementChild(net_output_node
); node
;
1052 node
= xmlNextElementSibling(node
)) {
1053 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1055 *control_uri
= (char *) xmlNodeGetContent(node
);
1056 if (!*control_uri
) {
1061 *data_uri
= (char *) xmlNodeGetContent(node
);
1068 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1072 int process_consumer_output(xmlNodePtr consumer_output_node
,
1073 struct consumer_output
*output
)
1080 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1081 node
= xmlNextElementSibling(node
)) {
1082 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1083 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1087 ret
= -LTTNG_ERR_NOMEM
;
1091 ret
= parse_bool(enabled_str
, &output
->enabled
);
1097 xmlNodePtr output_type_node
;
1100 output_type_node
= xmlFirstElementChild(node
);
1101 if (!output_type_node
) {
1102 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1106 if (!strcmp((const char *) output_type_node
->name
,
1107 config_element_path
)) {
1109 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1110 if (!output
->path
) {
1111 ret
= -LTTNG_ERR_NOMEM
;
1116 ret
= get_net_output_uris(output_type_node
,
1117 &output
->control_uri
, &output
->data_uri
);
1129 free(output
->control_uri
);
1130 free(output
->data_uri
);
1131 memset(output
, 0, sizeof(struct consumer_output
));
1137 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1138 const char *control_uri
, const char *data_uri
)
1141 struct lttng_handle
*handle
;
1142 const char *uri
= NULL
;
1147 handle
= lttng_create_handle(name
, domain
);
1149 ret
= -LTTNG_ERR_NOMEM
;
1153 if (!control_uri
|| !data_uri
) {
1154 uri
= control_uri
? control_uri
: data_uri
;
1159 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1160 lttng_destroy_handle(handle
);
1166 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1169 xmlNodePtr node
= NULL
;
1170 xmlNodePtr snapshot_output_list_node
;
1171 xmlNodePtr snapshot_output_node
;
1173 assert(session_name
);
1175 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1184 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1186 /* Parse and create snapshot outputs */
1188 for (snapshot_output_node
=
1189 xmlFirstElementChild(snapshot_output_list_node
);
1190 snapshot_output_node
; snapshot_output_node
=
1191 xmlNextElementSibling(snapshot_output_node
)) {
1193 uint64_t max_size
= UINT64_MAX
;
1194 struct consumer_output output
= { 0 };
1195 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1197 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1198 node
= xmlNextElementSibling(node
)) {
1199 if (!strcmp((const char *) node
->name
,
1200 config_element_name
)) {
1202 name
= (char *) xmlNodeGetContent(node
);
1204 ret
= -LTTNG_ERR_NOMEM
;
1205 goto error_snapshot_output
;
1207 } else if (!strcmp((const char *) node
->name
,
1208 config_element_max_size
)) {
1209 xmlChar
*content
= xmlNodeGetContent(node
);
1213 ret
= -LTTNG_ERR_NOMEM
;
1214 goto error_snapshot_output
;
1216 ret
= parse_uint(content
, &max_size
);
1219 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1220 goto error_snapshot_output
;
1223 /* consumer_output */
1224 ret
= process_consumer_output(node
, &output
);
1226 goto error_snapshot_output
;
1231 snapshot_output
= lttng_snapshot_output_create();
1232 if (!snapshot_output
) {
1233 ret
= -LTTNG_ERR_NOMEM
;
1234 goto error_snapshot_output
;
1237 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1239 goto error_snapshot_output
;
1242 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1244 goto error_snapshot_output
;
1248 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1251 goto error_snapshot_output
;
1254 if (output
.control_uri
) {
1255 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1258 goto error_snapshot_output
;
1262 if (output
.data_uri
) {
1263 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1266 goto error_snapshot_output
;
1271 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1272 error_snapshot_output
:
1275 free(output
.control_uri
);
1276 free(output
.data_uri
);
1277 lttng_snapshot_output_destroy(snapshot_output
);
1287 int create_session(const char *name
,
1288 struct lttng_domain
*kernel_domain
,
1289 struct lttng_domain
*ust_domain
,
1290 struct lttng_domain
*jul_domain
,
1291 struct lttng_domain
*log4j_domain
,
1292 xmlNodePtr output_node
,
1293 uint64_t live_timer_interval
)
1296 struct consumer_output output
= { 0 };
1297 xmlNodePtr consumer_output_node
;
1302 consumer_output_node
= xmlFirstElementChild(output_node
);
1303 if (!consumer_output_node
) {
1304 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1308 if (strcmp((const char *) consumer_output_node
->name
,
1309 config_element_consumer_output
)) {
1310 WARN("Invalid output type, expected %s node",
1311 config_element_consumer_output
);
1312 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1316 ret
= process_consumer_output(consumer_output_node
, &output
);
1322 if (live_timer_interval
!= UINT64_MAX
&&
1323 !output
.control_uri
&& !output
.data_uri
) {
1324 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1328 if (output
.control_uri
|| output
.data_uri
) {
1330 struct lttng_domain
*domain
;
1331 struct lttng_domain
*domains
[] =
1332 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1334 /* network destination */
1335 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1337 * URLs are provided for sure since the test above make sure that
1338 * with a live timer the data and control URIs are provided. So,
1339 * NULL is passed here and will be set right after.
1341 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1343 ret
= lttng_create_session(name
, NULL
);
1349 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1350 domain
= domains
[i
];
1355 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1362 /* either local output or no output */
1363 ret
= lttng_create_session(name
, output
.path
);
1370 free(output
.control_uri
);
1371 free(output
.data_uri
);
1375 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1376 struct lttng_event_probe_attr
*attr
)
1380 assert(probe_attribute_node
);
1383 if (!strcmp((const char *) probe_attribute_node
->name
,
1384 config_element_address
)) {
1389 content
= xmlNodeGetContent(probe_attribute_node
);
1391 ret
= -LTTNG_ERR_NOMEM
;
1395 ret
= parse_uint(content
, &addr
);
1398 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1403 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1404 config_element_offset
)) {
1406 uint64_t offset
= 0;
1409 content
= xmlNodeGetContent(probe_attribute_node
);
1411 ret
= -LTTNG_ERR_NOMEM
;
1415 ret
= parse_uint(content
, &offset
);
1418 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1422 attr
->offset
= offset
;
1423 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1424 config_element_symbol_name
)) {
1429 content
= xmlNodeGetContent(probe_attribute_node
);
1431 ret
= -LTTNG_ERR_NOMEM
;
1435 name_len
= strlen((char *) content
);
1436 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1437 WARN("symbol_name too long.");
1438 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1443 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1452 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1453 const char *channel_name
)
1457 struct lttng_event event
;
1458 char **exclusions
= NULL
;
1459 unsigned long exclusion_count
= 0;
1460 char *filter_expression
= NULL
;
1464 assert(channel_name
);
1466 memset(&event
, 0, sizeof(event
));
1468 /* Initialize default log level which varies by domain */
1469 switch (handle
->domain
.type
)
1471 case LTTNG_DOMAIN_JUL
:
1472 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1474 case LTTNG_DOMAIN_LOG4J
:
1475 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1477 case LTTNG_DOMAIN_PYTHON
:
1478 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1480 case LTTNG_DOMAIN_UST
:
1481 case LTTNG_DOMAIN_KERNEL
:
1482 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1488 for (node
= xmlFirstElementChild(event_node
); node
;
1489 node
= xmlNextElementSibling(node
)) {
1490 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1495 content
= xmlNodeGetContent(node
);
1497 ret
= -LTTNG_ERR_NOMEM
;
1501 name_len
= strlen((char *) content
);
1502 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1503 WARN("Channel name too long.");
1504 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1509 strncpy(event
.name
, (const char *) content
, name_len
);
1511 } else if (!strcmp((const char *) node
->name
,
1512 config_element_enabled
)) {
1513 xmlChar
*content
= xmlNodeGetContent(node
);
1517 ret
= -LTTNG_ERR_NOMEM
;
1521 ret
= parse_bool(content
, &event
.enabled
);
1524 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1527 } else if (!strcmp((const char *) node
->name
,
1528 config_element_type
)) {
1529 xmlChar
*content
= xmlNodeGetContent(node
);
1533 ret
= -LTTNG_ERR_NOMEM
;
1537 ret
= get_event_type(content
);
1540 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1545 } else if (!strcmp((const char *) node
->name
,
1546 config_element_loglevel_type
)) {
1547 xmlChar
*content
= xmlNodeGetContent(node
);
1551 ret
= -LTTNG_ERR_NOMEM
;
1555 ret
= get_loglevel_type(content
);
1558 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1562 event
.loglevel_type
= ret
;
1563 } else if (!strcmp((const char *) node
->name
,
1564 config_element_loglevel
)) {
1566 int64_t loglevel
= 0;
1569 content
= xmlNodeGetContent(node
);
1571 ret
= -LTTNG_ERR_NOMEM
;
1575 ret
= parse_int(content
, &loglevel
);
1578 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1582 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1583 WARN("loglevel out of range.");
1584 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1588 event
.loglevel
= loglevel
;
1589 } else if (!strcmp((const char *) node
->name
,
1590 config_element_filter
)) {
1592 xmlNodeGetContent(node
);
1596 ret
= -LTTNG_ERR_NOMEM
;
1600 filter_expression
= strdup((char *) content
);
1602 if (!filter_expression
) {
1603 ret
= -LTTNG_ERR_NOMEM
;
1606 } else if (!strcmp((const char *) node
->name
,
1607 config_element_exclusions
)) {
1608 xmlNodePtr exclusion_node
;
1609 int exclusion_index
= 0;
1614 * Exclusions has already been initialized,
1617 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1621 exclusion_count
= xmlChildElementCount(node
);
1622 if (!exclusion_count
) {
1626 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1628 exclusion_count
= 0;
1629 ret
= -LTTNG_ERR_NOMEM
;
1633 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1634 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1636 xmlNodeGetContent(exclusion_node
);
1639 ret
= -LTTNG_ERR_NOMEM
;
1643 exclusions
[exclusion_index
] = strdup((const char *) content
);
1645 if (!exclusions
[exclusion_index
]) {
1646 ret
= -LTTNG_ERR_NOMEM
;
1652 event
.exclusion
= 1;
1653 } else if (!strcmp((const char *) node
->name
,
1654 config_element_attributes
)) {
1655 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1658 if (!attribute_node
) {
1659 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1663 if (!strcmp((const char *) node
->name
,
1664 config_element_probe_attributes
)) {
1665 xmlNodePtr probe_attribute_node
;
1667 /* probe_attributes */
1668 for (probe_attribute_node
=
1669 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1670 probe_attribute_node
= xmlNextElementSibling(
1671 probe_attribute_node
)) {
1673 ret
= process_probe_attribute_node(probe_attribute_node
,
1682 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1684 /* function_attributes */
1685 content
= xmlNodeGetContent(symbol_node
);
1687 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1691 sym_len
= strlen((char *) content
);
1692 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1693 WARN("Function name too long.");
1694 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1699 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1706 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1707 filter_expression
, exclusion_count
, exclusions
);
1712 if (!event
.enabled
) {
1714 * Note that we should use lttng_disable_event_ext() (2.6+) to
1715 * eliminate the risk of clashing on events of the same
1716 * name (with different event types and loglevels).
1718 * Unfortunately, lttng_disable_event_ext() only performs a
1719 * match on the name and event type and errors out if any other
1720 * event attribute is not set to its default value.
1722 * This will disable all events that match this name.
1724 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1727 for (i
= 0; i
< exclusion_count
; i
++) {
1728 free(exclusions
[i
]);
1732 free(filter_expression
);
1737 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1738 const char *channel_name
)
1743 assert(events_node
);
1745 assert(channel_name
);
1747 for (node
= xmlFirstElementChild(events_node
); node
;
1748 node
= xmlNextElementSibling(node
)) {
1749 ret
= process_event_node(node
, handle
, channel_name
);
1759 int process_channel_attr_node(xmlNodePtr attr_node
,
1760 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1761 xmlNodePtr
*events_node
)
1767 assert(contexts_node
);
1768 assert(events_node
);
1770 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1775 content
= xmlNodeGetContent(attr_node
);
1777 ret
= -LTTNG_ERR_NOMEM
;
1781 name_len
= strlen((char *) content
);
1782 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1783 WARN("Channel name too long.");
1784 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1789 strncpy(channel
->name
, (const char *) content
, name_len
);
1791 } else if (!strcmp((const char *) attr_node
->name
,
1792 config_element_enabled
)) {
1797 content
= xmlNodeGetContent(attr_node
);
1799 ret
= -LTTNG_ERR_NOMEM
;
1803 ret
= parse_bool(content
, &enabled
);
1806 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1810 channel
->enabled
= enabled
;
1811 } else if (!strcmp((const char *) attr_node
->name
,
1812 config_element_overwrite_mode
)) {
1815 /* overwrite_mode */
1816 content
= xmlNodeGetContent(attr_node
);
1818 ret
= -LTTNG_ERR_NOMEM
;
1822 ret
= get_overwrite_mode(content
);
1825 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1829 channel
->attr
.overwrite
= ret
;
1830 } else if (!strcmp((const char *) attr_node
->name
,
1831 config_element_subbuf_size
)) {
1834 /* subbuffer_size */
1835 content
= xmlNodeGetContent(attr_node
);
1837 ret
= -LTTNG_ERR_NOMEM
;
1841 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1844 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1847 } else if (!strcmp((const char *) attr_node
->name
,
1848 config_element_num_subbuf
)) {
1851 /* subbuffer_count */
1852 content
= xmlNodeGetContent(attr_node
);
1854 ret
= -LTTNG_ERR_NOMEM
;
1858 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1861 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1864 } else if (!strcmp((const char *) attr_node
->name
,
1865 config_element_switch_timer_interval
)) {
1867 uint64_t switch_timer_interval
= 0;
1869 /* switch_timer_interval */
1870 content
= xmlNodeGetContent(attr_node
);
1872 ret
= -LTTNG_ERR_NOMEM
;
1876 ret
= parse_uint(content
, &switch_timer_interval
);
1879 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1883 if (switch_timer_interval
> UINT_MAX
) {
1884 WARN("switch_timer_interval out of range.");
1885 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1889 channel
->attr
.switch_timer_interval
=
1890 switch_timer_interval
;
1891 } else if (!strcmp((const char *) attr_node
->name
,
1892 config_element_read_timer_interval
)) {
1894 uint64_t read_timer_interval
= 0;
1896 /* read_timer_interval */
1897 content
= xmlNodeGetContent(attr_node
);
1899 ret
= -LTTNG_ERR_NOMEM
;
1903 ret
= parse_uint(content
, &read_timer_interval
);
1906 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1910 if (read_timer_interval
> UINT_MAX
) {
1911 WARN("read_timer_interval out of range.");
1912 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1916 channel
->attr
.read_timer_interval
=
1917 read_timer_interval
;
1918 } else if (!strcmp((const char *) attr_node
->name
,
1919 config_element_output_type
)) {
1923 content
= xmlNodeGetContent(attr_node
);
1925 ret
= -LTTNG_ERR_NOMEM
;
1929 ret
= get_output_type(content
);
1932 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1936 channel
->attr
.output
= ret
;
1937 } else if (!strcmp((const char *) attr_node
->name
,
1938 config_element_tracefile_size
)) {
1941 /* tracefile_size */
1942 content
= xmlNodeGetContent(attr_node
);
1944 ret
= -LTTNG_ERR_NOMEM
;
1948 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1951 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1954 } else if (!strcmp((const char *) attr_node
->name
,
1955 config_element_tracefile_count
)) {
1958 /* tracefile_count */
1959 content
= xmlNodeGetContent(attr_node
);
1961 ret
= -LTTNG_ERR_NOMEM
;
1965 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1968 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1971 } else if (!strcmp((const char *) attr_node
->name
,
1972 config_element_live_timer_interval
)) {
1974 uint64_t live_timer_interval
= 0;
1976 /* live_timer_interval */
1977 content
= xmlNodeGetContent(attr_node
);
1979 ret
= -LTTNG_ERR_NOMEM
;
1983 ret
= parse_uint(content
, &live_timer_interval
);
1986 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1990 if (live_timer_interval
> UINT_MAX
) {
1991 WARN("live_timer_interval out of range.");
1992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1996 channel
->attr
.live_timer_interval
=
1997 live_timer_interval
;
1998 } else if (!strcmp((const char *) attr_node
->name
,
1999 config_element_events
)) {
2001 *events_node
= attr_node
;
2004 *contexts_node
= attr_node
;
2012 int process_context_node(xmlNodePtr context_node
,
2013 struct lttng_handle
*handle
, const char *channel_name
)
2016 struct lttng_event_context context
;
2017 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2020 assert(channel_name
);
2022 if (!context_child_node
) {
2023 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2027 memset(&context
, 0, sizeof(context
));
2029 if (!strcmp((const char *) context_child_node
->name
,
2030 config_element_type
)) {
2032 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2035 ret
= -LTTNG_ERR_NOMEM
;
2039 ret
= get_context_type(content
);
2042 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2047 } else if (!strcmp((const char *) context_child_node
->name
,
2048 config_element_context_perf
)) {
2050 xmlNodePtr perf_attr_node
;
2052 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2053 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2054 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2055 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2056 perf_attr_node
; perf_attr_node
=
2057 xmlNextElementSibling(perf_attr_node
)) {
2058 if (!strcmp((const char *) perf_attr_node
->name
,
2059 config_element_type
)) {
2064 content
= xmlNodeGetContent(perf_attr_node
);
2066 ret
= -LTTNG_ERR_NOMEM
;
2070 ret
= parse_uint(content
, &type
);
2073 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2077 if (type
> UINT32_MAX
) {
2078 WARN("perf context type out of range.");
2079 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2083 context
.u
.perf_counter
.type
= type
;
2084 } else if (!strcmp((const char *) perf_attr_node
->name
,
2085 config_element_config
)) {
2087 uint64_t config
= 0;
2090 content
= xmlNodeGetContent(perf_attr_node
);
2092 ret
= -LTTNG_ERR_NOMEM
;
2096 ret
= parse_uint(content
, &config
);
2099 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2103 context
.u
.perf_counter
.config
= config
;
2104 } else if (!strcmp((const char *) perf_attr_node
->name
,
2105 config_element_name
)) {
2110 content
= xmlNodeGetContent(perf_attr_node
);
2112 ret
= -LTTNG_ERR_NOMEM
;
2116 name_len
= strlen((char *) content
);
2117 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2118 WARN("perf context name too long.");
2119 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2124 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2129 } else if (!strcmp((const char *) context_child_node
->name
,
2130 config_element_context_app
)) {
2131 /* application context */
2132 xmlNodePtr app_ctx_node
;
2134 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2135 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2136 app_ctx_node
; app_ctx_node
=
2137 xmlNextElementSibling(app_ctx_node
)) {
2139 char **target
= strcmp(
2140 (const char *) app_ctx_node
->name
,
2141 config_element_context_app_provider_name
) == 0 ?
2142 &context
.u
.app_ctx
.provider_name
:
2143 &context
.u
.app_ctx
.ctx_name
;
2145 content
= xmlNodeGetContent(app_ctx_node
);
2147 ret
= -LTTNG_ERR_NOMEM
;
2151 *target
= (char *) content
;
2154 /* Unrecognized context type */
2155 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2159 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2160 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2161 free(context
.u
.app_ctx
.provider_name
);
2162 free(context
.u
.app_ctx
.ctx_name
);
2169 int process_contexts_node(xmlNodePtr contexts_node
,
2170 struct lttng_handle
*handle
, const char *channel_name
)
2173 xmlNodePtr context_node
;
2175 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2176 context_node
= xmlNextElementSibling(context_node
)) {
2177 ret
= process_context_node(context_node
, handle
, channel_name
);
2187 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2188 struct lttng_handle
*handle
)
2191 xmlNodePtr targets_node
= NULL
;
2195 assert(pid_tracker_node
);
2196 /* get the targets node */
2197 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2198 node
= xmlNextElementSibling(node
)) {
2199 if (!strcmp((const char *) node
->name
,
2200 config_element_targets
)) {
2201 targets_node
= node
;
2206 if (!targets_node
) {
2207 ret
= LTTNG_ERR_INVALID
;
2211 /* Go through all pid_target node */
2212 child
= xmlChildElementCount(targets_node
);
2214 /* The session is explicitly set to target nothing. */
2215 ret
= lttng_untrack_pid(handle
, -1);
2220 for (node
= xmlFirstElementChild(targets_node
); node
;
2221 node
= xmlNextElementSibling(node
)) {
2222 xmlNodePtr pid_target_node
= node
;
2224 /* get pid node and track it */
2225 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2226 node
= xmlNextElementSibling(node
)) {
2227 if (!strcmp((const char *) node
->name
,
2228 config_element_pid
)) {
2230 xmlChar
*content
= NULL
;
2232 content
= xmlNodeGetContent(node
);
2234 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2238 ret
= parse_int(content
, &pid
);
2241 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2245 ret
= lttng_track_pid(handle
, (int) pid
);
2251 node
= pid_target_node
;
2260 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2263 struct lttng_domain domain
= { 0 };
2264 struct lttng_handle
*handle
= NULL
;
2265 xmlNodePtr channels_node
= NULL
;
2266 xmlNodePtr trackers_node
= NULL
;
2267 xmlNodePtr pid_tracker_node
= NULL
;
2270 assert(session_name
);
2272 ret
= init_domain(domain_node
, &domain
);
2277 handle
= lttng_create_handle(session_name
, &domain
);
2279 ret
= -LTTNG_ERR_NOMEM
;
2283 /* get the channels node */
2284 for (node
= xmlFirstElementChild(domain_node
); node
;
2285 node
= xmlNextElementSibling(node
)) {
2286 if (!strcmp((const char *) node
->name
,
2287 config_element_channels
)) {
2288 channels_node
= node
;
2293 if (!channels_node
) {
2297 /* create all channels */
2298 for (node
= xmlFirstElementChild(channels_node
); node
;
2299 node
= xmlNextElementSibling(node
)) {
2300 struct lttng_channel channel
;
2301 xmlNodePtr contexts_node
= NULL
;
2302 xmlNodePtr events_node
= NULL
;
2303 xmlNodePtr channel_attr_node
;
2305 memset(&channel
, 0, sizeof(channel
));
2306 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2308 for (channel_attr_node
= xmlFirstElementChild(node
);
2309 channel_attr_node
; channel_attr_node
=
2310 xmlNextElementSibling(channel_attr_node
)) {
2311 ret
= process_channel_attr_node(channel_attr_node
,
2312 &channel
, &contexts_node
, &events_node
);
2318 ret
= lttng_enable_channel(handle
, &channel
);
2323 ret
= process_events_node(events_node
, handle
, channel
.name
);
2328 ret
= process_contexts_node(contexts_node
, handle
,
2335 /* get the trackers node */
2336 for (node
= xmlFirstElementChild(domain_node
); node
;
2337 node
= xmlNextElementSibling(node
)) {
2338 if (!strcmp((const char *) node
->name
,
2339 config_element_trackers
)) {
2340 trackers_node
= node
;
2345 if (!trackers_node
) {
2349 for (node
= xmlFirstElementChild(trackers_node
); node
;
2350 node
= xmlNextElementSibling(node
)) {
2351 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2352 pid_tracker_node
= node
;
2353 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2360 if (!pid_tracker_node
) {
2361 lttng_track_pid(handle
, -1);
2365 lttng_destroy_handle(handle
);
2370 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2373 int ret
, started
= -1, snapshot_mode
= -1;
2374 uint64_t live_timer_interval
= UINT64_MAX
;
2375 xmlChar
*name
= NULL
;
2376 xmlChar
*shm_path
= NULL
;
2377 xmlNodePtr domains_node
= NULL
;
2378 xmlNodePtr output_node
= NULL
;
2380 struct lttng_domain
*kernel_domain
= NULL
;
2381 struct lttng_domain
*ust_domain
= NULL
;
2382 struct lttng_domain
*jul_domain
= NULL
;
2383 struct lttng_domain
*log4j_domain
= NULL
;
2384 struct lttng_domain
*python_domain
= NULL
;
2386 for (node
= xmlFirstElementChild(session_node
); node
;
2387 node
= xmlNextElementSibling(node
)) {
2388 if (!name
&& !strcmp((const char *) node
->name
,
2389 config_element_name
)) {
2391 xmlChar
*node_content
= xmlNodeGetContent(node
);
2392 if (!node_content
) {
2393 ret
= -LTTNG_ERR_NOMEM
;
2397 name
= node_content
;
2398 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2399 config_element_domains
)) {
2401 domains_node
= node
;
2402 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2403 config_element_started
)) {
2405 xmlChar
*node_content
= xmlNodeGetContent(node
);
2406 if (!node_content
) {
2407 ret
= -LTTNG_ERR_NOMEM
;
2411 ret
= parse_bool(node_content
, &started
);
2414 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2417 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2418 config_element_output
)) {
2421 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2422 config_element_shared_memory_path
)) {
2423 /* shared memory path */
2424 xmlChar
*node_content
= xmlNodeGetContent(node
);
2425 if (!node_content
) {
2426 ret
= -LTTNG_ERR_NOMEM
;
2430 shm_path
= node_content
;
2432 /* attributes, snapshot_mode or live_timer_interval */
2433 xmlNodePtr attributes_child
=
2434 xmlFirstElementChild(node
);
2436 if (!strcmp((const char *) attributes_child
->name
,
2437 config_element_snapshot_mode
)) {
2439 xmlChar
*snapshot_mode_content
=
2440 xmlNodeGetContent(attributes_child
);
2441 if (!snapshot_mode_content
) {
2442 ret
= -LTTNG_ERR_NOMEM
;
2446 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2447 free(snapshot_mode_content
);
2449 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2453 /* live_timer_interval */
2454 xmlChar
*timer_interval_content
=
2455 xmlNodeGetContent(attributes_child
);
2456 if (!timer_interval_content
) {
2457 ret
= -LTTNG_ERR_NOMEM
;
2461 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2462 free(timer_interval_content
);
2464 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2472 /* Mandatory attribute, as defined in the session XSD */
2473 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2477 if (session_name
&& strcmp((char *) name
, session_name
)) {
2478 /* This is not the session we are looking for */
2479 ret
= -LTTNG_ERR_NO_SESSION
;
2483 /* Init domains to create the session handles */
2484 for (node
= xmlFirstElementChild(domains_node
); node
;
2485 node
= xmlNextElementSibling(node
)) {
2486 struct lttng_domain
*domain
;
2488 domain
= zmalloc(sizeof(*domain
));
2490 ret
= -LTTNG_ERR_NOMEM
;
2494 ret
= init_domain(node
, domain
);
2496 goto domain_init_error
;
2499 switch (domain
->type
) {
2500 case LTTNG_DOMAIN_KERNEL
:
2501 if (kernel_domain
) {
2502 /* Same domain seen twice, invalid! */
2503 goto domain_init_error
;
2505 kernel_domain
= domain
;
2507 case LTTNG_DOMAIN_UST
:
2509 /* Same domain seen twice, invalid! */
2510 goto domain_init_error
;
2512 ust_domain
= domain
;
2514 case LTTNG_DOMAIN_JUL
:
2516 /* Same domain seen twice, invalid! */
2517 goto domain_init_error
;
2519 jul_domain
= domain
;
2521 case LTTNG_DOMAIN_LOG4J
:
2523 /* Same domain seen twice, invalid! */
2524 goto domain_init_error
;
2526 log4j_domain
= domain
;
2528 case LTTNG_DOMAIN_PYTHON
:
2529 if (python_domain
) {
2530 /* Same domain seen twice, invalid! */
2531 goto domain_init_error
;
2533 python_domain
= domain
;
2536 WARN("Invalid domain type");
2537 goto domain_init_error
;
2542 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2547 /* Destroy session if it exists */
2548 ret
= lttng_destroy_session((const char *) name
);
2549 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2550 ERR("Failed to destroy existing session.");
2555 /* Create session type depending on output type */
2556 if (snapshot_mode
&& snapshot_mode
!= -1) {
2557 ret
= create_snapshot_session((const char *) name
, output_node
);
2558 } else if (live_timer_interval
&&
2559 live_timer_interval
!= UINT64_MAX
) {
2560 ret
= create_session((const char *) name
, kernel_domain
,
2561 ust_domain
, jul_domain
, log4j_domain
,
2562 output_node
, live_timer_interval
);
2564 /* regular session */
2565 ret
= create_session((const char *) name
, kernel_domain
,
2566 ust_domain
, jul_domain
, log4j_domain
,
2567 output_node
, UINT64_MAX
);
2574 ret
= lttng_set_session_shm_path((const char *) name
,
2575 (const char *) shm_path
);
2581 for (node
= xmlFirstElementChild(domains_node
); node
;
2582 node
= xmlNextElementSibling(node
)) {
2583 ret
= process_domain_node(node
, (const char *) name
);
2590 ret
= lttng_start_tracing((const char *) name
);
2598 ERR("Failed to load session %s: %s", (const char *) name
,
2599 lttng_strerror(ret
));
2600 lttng_destroy_session((const char *) name
);
2604 free(kernel_domain
);
2608 free(python_domain
);
2615 * Return 1 if the given path is readable by the current UID or 0 if not.
2616 * Return -1 if the path is EPERM.
2618 static int validate_file_read_creds(const char *path
)
2624 /* Can we read the file. */
2625 ret
= access(path
, R_OK
);
2629 if (errno
== EACCES
) {
2640 int load_session_from_file(const char *path
, const char *session_name
,
2641 struct session_config_validation_ctx
*validation_ctx
, int override
)
2643 int ret
, session_found
= !session_name
;
2644 xmlDocPtr doc
= NULL
;
2645 xmlNodePtr sessions_node
;
2646 xmlNodePtr session_node
;
2649 assert(validation_ctx
);
2651 ret
= validate_file_read_creds(path
);
2654 ret
= -LTTNG_ERR_EPERM
;
2656 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2661 doc
= xmlParseFile(path
);
2663 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2667 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2669 ERR("Session configuration file validation failed");
2670 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2674 sessions_node
= xmlDocGetRootElement(doc
);
2675 if (!sessions_node
) {
2679 for (session_node
= xmlFirstElementChild(sessions_node
);
2680 session_node
; session_node
=
2681 xmlNextElementSibling(session_node
)) {
2682 ret
= process_session_node(session_node
,
2683 session_name
, override
);
2684 if (session_name
&& ret
== 0) {
2685 /* Target session found and loaded */
2693 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2698 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2700 struct dirent
*alloc_dirent(const char *path
)
2704 struct dirent
*entry
;
2706 name_max
= pathconf(path
, _PC_NAME_MAX
);
2707 if (name_max
== -1) {
2708 name_max
= PATH_MAX
;
2710 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2711 entry
= zmalloc(len
);
2716 int load_session_from_path(const char *path
, const char *session_name
,
2717 struct session_config_validation_ctx
*validation_ctx
, int override
)
2719 int ret
, session_found
= !session_name
;
2720 DIR *directory
= NULL
;
2723 assert(validation_ctx
);
2725 directory
= opendir(path
);
2729 /* Try the file loading. */
2732 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2735 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2740 struct dirent
*entry
;
2741 struct dirent
*result
;
2742 char *file_path
= NULL
;
2743 size_t path_len
= strlen(path
);
2745 if (path_len
>= PATH_MAX
) {
2746 ret
= -LTTNG_ERR_INVALID
;
2750 entry
= alloc_dirent(path
);
2752 ret
= -LTTNG_ERR_NOMEM
;
2756 file_path
= zmalloc(PATH_MAX
);
2758 ret
= -LTTNG_ERR_NOMEM
;
2763 strncpy(file_path
, path
, path_len
);
2764 if (file_path
[path_len
- 1] != '/') {
2765 file_path
[path_len
++] = '/';
2769 /* Search for *.lttng files */
2770 while (!readdir_r(directory
, entry
, &result
) && result
) {
2771 size_t file_name_len
= strlen(result
->d_name
);
2773 if (file_name_len
<=
2774 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2778 if (path_len
+ file_name_len
>= PATH_MAX
) {
2782 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2783 result
->d_name
+ file_name_len
- sizeof(
2784 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2788 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2789 file_path
[path_len
+ file_name_len
] = '\0';
2791 ret
= load_session_from_file(file_path
, session_name
,
2792 validation_ctx
, override
);
2793 if (session_name
&& !ret
) {
2802 ret
= load_session_from_file(path
, session_name
,
2803 validation_ctx
, override
);
2813 if (closedir(directory
)) {
2818 if (!session_found
) {
2819 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2826 * Validate that the given path's credentials and the current process have the
2827 * same UID. If so, return 1 else return 0 if it does NOT match.
2829 static int validate_path_creds(const char *path
)
2831 int ret
, uid
= getuid();
2840 ret
= stat(path
, &buf
);
2842 if (errno
!= ENOENT
) {
2845 ret
= -LTTNG_ERR_INVALID
;
2849 if (buf
.st_uid
!= uid
) {
2860 int config_load_session(const char *path
, const char *session_name
,
2861 int override
, unsigned int autoload
)
2864 const char *path_ptr
= NULL
;
2865 struct session_config_validation_ctx validation_ctx
= { 0 };
2867 ret
= init_session_config_validation_ctx(&validation_ctx
);
2874 const char *sys_path
;
2877 home_path
= utils_get_home_dir();
2879 char path
[PATH_MAX
];
2882 * Try user session configuration path. Ignore error here so we can
2883 * continue loading the system wide sessions.
2886 ret
= snprintf(path
, sizeof(path
),
2887 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2888 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2890 PERROR("snprintf session autoload home config path");
2895 * Credentials are only validated for the autoload in order to
2896 * avoid any user session daemon to try to load kernel sessions
2897 * automatically and failing all the times.
2899 ret
= validate_path_creds(path
);
2904 ret
= snprintf(path
, sizeof(path
),
2905 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2907 PERROR("snprintf session home config path");
2913 ret
= load_session_from_path(path_ptr
, session_name
,
2914 &validation_ctx
, override
);
2915 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2919 * Continue even if the session was found since we have to try
2920 * the system wide sessions.
2925 /* Reset path pointer for the system wide dir. */
2928 /* Try system wide configuration directory. */
2930 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2931 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2932 ret
= validate_path_creds(sys_path
);
2934 path_ptr
= sys_path
;
2937 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2938 path_ptr
= sys_path
;
2942 ret
= load_session_from_path(path_ptr
, session_name
,
2943 &validation_ctx
, override
);
2946 ret
= access(path
, F_OK
);
2951 ret
= -LTTNG_ERR_INVALID
;
2952 WARN("Session configuration path does not exist.");
2955 ret
= -LTTNG_ERR_EPERM
;
2958 ret
= -LTTNG_ERR_UNK
;
2964 ret
= load_session_from_path(path
, session_name
,
2965 &validation_ctx
, override
);
2968 fini_session_config_validation_ctx(&validation_ctx
);
2969 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2971 * Don't report an error if no sessions are found when called
2972 * without a session_name or a search path.