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 <lttng/lttng-error.h>
35 #include <libxml/parser.h>
36 #include <libxml/valid.h>
37 #include <libxml/xmlschemas.h>
38 #include <libxml/tree.h>
39 #include <lttng/lttng.h>
40 #include <lttng/snapshot.h>
43 #include "config-internal.h"
45 struct handler_filter_args
{
47 config_entry_handler_cb handler
;
51 struct session_config_validation_ctx
{
52 xmlSchemaParserCtxtPtr parser_ctx
;
54 xmlSchemaValidCtxtPtr schema_validation_ctx
;
57 const char * const config_str_yes
= "yes";
58 const char * const config_str_true
= "true";
59 const char * const config_str_on
= "on";
60 const char * const config_str_no
= "no";
61 const char * const config_str_false
= "false";
62 const char * const config_str_off
= "off";
63 const char * const config_xml_encoding
= "UTF-8";
64 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
65 const char * const config_xml_indent_string
= "\t";
66 const char * const config_xml_true
= "true";
67 const char * const config_xml_false
= "false";
69 const char * const config_element_channel
= "channel";
70 const char * const config_element_channels
= "channels";
71 const char * const config_element_domain
= "domain";
72 const char * const config_element_domains
= "domains";
73 const char * const config_element_event
= "event";
74 const char * const config_element_events
= "events";
75 const char * const config_element_context
= "context";
76 const char * const config_element_contexts
= "contexts";
77 const char * const config_element_attributes
= "attributes";
78 const char * const config_element_exclusion
= "exclusion";
79 const char * const config_element_exclusions
= "exclusions";
80 const char * const config_element_function_attributes
= "function_attributes";
81 const char * const config_element_probe_attributes
= "probe_attributes";
82 const char * const config_element_symbol_name
= "symbol_name";
83 const char * const config_element_address
= "address";
84 const char * const config_element_offset
= "offset";
85 const char * const config_element_name
= "name";
86 const char * const config_element_enabled
= "enabled";
87 const char * const config_element_overwrite_mode
= "overwrite_mode";
88 const char * const config_element_subbuf_size
= "subbuffer_size";
89 const char * const config_element_num_subbuf
= "subbuffer_count";
90 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
91 const char * const config_element_read_timer_interval
= "read_timer_interval";
92 const char * const config_element_output
= "output";
93 const char * const config_element_output_type
= "output_type";
94 const char * const config_element_tracefile_size
= "tracefile_size";
95 const char * const config_element_tracefile_count
= "tracefile_count";
96 const char * const config_element_live_timer_interval
= "live_timer_interval";
97 const char * const config_element_type
= "type";
98 const char * const config_element_buffer_type
= "buffer_type";
99 const char * const config_element_session
= "session";
100 const char * const config_element_sessions
= "sessions";
101 const char * const config_element_perf
= "perf";
102 const char * const config_element_config
= "config";
103 const char * const config_element_started
= "started";
104 const char * const config_element_snapshot_mode
= "snapshot_mode";
105 const char * const config_element_loglevel
= "loglevel";
106 const char * const config_element_loglevel_type
= "loglevel_type";
107 const char * const config_element_filter
= "filter";
108 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
109 const char * const config_element_consumer_output
= "consumer_output";
110 const char * const config_element_destination
= "destination";
111 const char * const config_element_path
= "path";
112 const char * const config_element_net_output
= "net_output";
113 const char * const config_element_control_uri
= "control_uri";
114 const char * const config_element_data_uri
= "data_uri";
115 const char * const config_element_max_size
= "max_size";
117 const char * const config_domain_type_kernel
= "KERNEL";
118 const char * const config_domain_type_ust
= "UST";
119 const char * const config_domain_type_jul
= "JUL";
121 const char * const config_buffer_type_per_pid
= "PER_PID";
122 const char * const config_buffer_type_per_uid
= "PER_UID";
123 const char * const config_buffer_type_global
= "GLOBAL";
125 const char * const config_overwrite_mode_discard
= "DISCARD";
126 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
128 const char * const config_output_type_splice
= "SPLICE";
129 const char * const config_output_type_mmap
= "MMAP";
131 const char * const config_loglevel_type_all
= "ALL";
132 const char * const config_loglevel_type_range
= "RANGE";
133 const char * const config_loglevel_type_single
= "SINGLE";
135 const char * const config_event_type_all
= "ALL";
136 const char * const config_event_type_tracepoint
= "TRACEPOINT";
137 const char * const config_event_type_probe
= "PROBE";
138 const char * const config_event_type_function
= "FUNCTION";
139 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
140 const char * const config_event_type_noop
= "NOOP";
141 const char * const config_event_type_syscall
= "SYSCALL";
142 const char * const config_event_type_kprobe
= "KPROBE";
143 const char * const config_event_type_kretprobe
= "KRETPROBE";
145 const char * const config_event_context_pid
= "PID";
146 const char * const config_event_context_procname
= "PROCNAME";
147 const char * const config_event_context_prio
= "PRIO";
148 const char * const config_event_context_nice
= "NICE";
149 const char * const config_event_context_vpid
= "VPID";
150 const char * const config_event_context_tid
= "TID";
151 const char * const config_event_context_vtid
= "VTID";
152 const char * const config_event_context_ppid
= "PPID";
153 const char * const config_event_context_vppid
= "VPPID";
154 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
155 const char * const config_event_context_hostname
= "HOSTNAME";
156 const char * const config_event_context_ip
= "IP";
158 struct consumer_output
{
165 static int config_entry_handler_filter(struct handler_filter_args
*args
,
166 const char *section
, const char *name
, const char *value
)
169 struct config_entry entry
= { section
, name
, value
};
173 if (!section
|| !name
|| !value
) {
179 if (strcmp(args
->section
, section
)) {
184 ret
= args
->handler(&entry
, args
->user_data
);
190 int config_get_section_entries(const char *override_path
, const char *section
,
191 config_entry_handler_cb handler
, void *user_data
)
195 FILE *config_file
= NULL
;
196 struct handler_filter_args filter
= { section
, handler
, user_data
};
198 /* First, try system-wide conf. file. */
199 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
201 config_file
= fopen(path
, "r");
203 DBG("Loading daemon conf file at %s", path
);
205 * Return value is not very important here since error or not, we
206 * continue and try the next possible conf. file.
208 (void) ini_parse_file(config_file
,
209 (ini_entry_handler
) config_entry_handler_filter
,
214 /* Second is the user local configuration. */
215 path
= utils_get_home_dir();
217 char fullpath
[PATH_MAX
];
219 ret
= snprintf(fullpath
, sizeof(fullpath
),
220 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
222 PERROR("snprintf user conf. path");
226 config_file
= fopen(fullpath
, "r");
228 DBG("Loading daemon user conf file at %s", path
);
230 * Return value is not very important here since error or not, we
231 * continue and try the next possible conf. file.
233 (void) ini_parse_file(config_file
,
234 (ini_entry_handler
) config_entry_handler_filter
,
240 /* Final path is the one that the user might have provided. */
242 config_file
= fopen(override_path
, "r");
244 DBG("Loading daemon command line conf file at %s", override_path
);
245 (void) ini_parse_file(config_file
,
246 (ini_entry_handler
) config_entry_handler_filter
,
250 ERR("Failed to open daemon configuration file at %s",
257 /* Everything went well. */
265 int config_parse_value(const char *value
)
268 char *endptr
, *lower_str
;
278 v
= strtoul(value
, &endptr
, 10);
279 if (endptr
!= value
) {
284 lower_str
= zmalloc(len
+ 1);
291 for (i
= 0; i
< len
; i
++) {
292 lower_str
[i
] = tolower(value
[i
]);
295 if (!strcmp(lower_str
, config_str_yes
) ||
296 !strcmp(lower_str
, config_str_true
) ||
297 !strcmp(lower_str
, config_str_on
)) {
299 } else if (!strcmp(lower_str
, config_str_no
) ||
300 !strcmp(lower_str
, config_str_false
) ||
301 !strcmp(lower_str
, config_str_off
)) {
313 * Returns a xmlChar string which must be released using xmlFree().
315 static xmlChar
*encode_string(const char *in_str
)
317 xmlChar
*out_str
= NULL
;
318 xmlCharEncodingHandlerPtr handler
;
319 int out_len
, ret
, in_len
;
323 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
325 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
329 in_len
= strlen(in_str
);
331 * Add 1 byte for the NULL terminted character. The factor 2 here is
332 * because UTF-8 can be on two bytes so this fits the worst case for each
335 out_len
= (in_len
* 2) + 1;
336 out_str
= xmlMalloc(out_len
);
341 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
348 /* out_len is now the size of out_str */
349 out_str
[out_len
] = '\0';
355 struct config_writer
*config_writer_create(int fd_output
)
358 struct config_writer
*writer
;
359 xmlOutputBufferPtr buffer
;
361 writer
= zmalloc(sizeof(struct config_writer
));
363 PERROR("zmalloc config_writer_create");
367 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
372 writer
->writer
= xmlNewTextWriter(buffer
);
373 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
374 config_xml_encoding
, NULL
);
379 ret
= xmlTextWriterSetIndentString(writer
->writer
,
380 BAD_CAST config_xml_indent_string
);
385 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
393 config_writer_destroy(writer
);
398 int config_writer_destroy(struct config_writer
*writer
)
407 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
408 WARN("Could not close XML document");
412 if (writer
->writer
) {
413 xmlFreeTextWriter(writer
->writer
);
422 int config_writer_open_element(struct config_writer
*writer
,
423 const char *element_name
)
426 xmlChar
*encoded_element_name
;
428 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
433 encoded_element_name
= encode_string(element_name
);
434 if (!encoded_element_name
) {
439 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
440 xmlFree(encoded_element_name
);
442 return ret
> 0 ? 0 : ret
;
446 int config_writer_close_element(struct config_writer
*writer
)
450 if (!writer
|| !writer
->writer
) {
455 ret
= xmlTextWriterEndElement(writer
->writer
);
457 return ret
> 0 ? 0 : ret
;
461 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
462 const char *element_name
, uint64_t value
)
465 xmlChar
*encoded_element_name
;
467 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
472 encoded_element_name
= encode_string(element_name
);
473 if (!encoded_element_name
) {
478 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
479 encoded_element_name
, "%" PRIu64
, value
);
480 xmlFree(encoded_element_name
);
482 return ret
> 0 ? 0 : ret
;
486 int config_writer_write_element_signed_int(struct config_writer
*writer
,
487 const char *element_name
, int64_t value
)
490 xmlChar
*encoded_element_name
;
492 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
497 encoded_element_name
= encode_string(element_name
);
498 if (!encoded_element_name
) {
503 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
504 encoded_element_name
, "%" PRIi64
, value
);
505 xmlFree(encoded_element_name
);
507 return ret
> 0 ? 0 : ret
;
511 int config_writer_write_element_bool(struct config_writer
*writer
,
512 const char *element_name
, int value
)
514 return config_writer_write_element_string(writer
, element_name
,
515 value
? config_xml_true
: config_xml_false
);
519 int config_writer_write_element_string(struct config_writer
*writer
,
520 const char *element_name
, const char *value
)
523 xmlChar
*encoded_element_name
= NULL
;
524 xmlChar
*encoded_value
= NULL
;
526 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
532 encoded_element_name
= encode_string(element_name
);
533 if (!encoded_element_name
) {
538 encoded_value
= encode_string(value
);
539 if (!encoded_value
) {
544 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
547 xmlFree(encoded_element_name
);
548 xmlFree(encoded_value
);
549 return ret
> 0 ? 0 : ret
;
553 void xml_error_handler(void *ctx
, const char *format
, ...)
559 va_start(args
, format
);
560 ret
= vasprintf(&errMsg
, format
, args
);
563 ERR("String allocation failed in xml error handler");
567 fprintf(stderr
, "XML Error: %s", errMsg
);
572 void fini_session_config_validation_ctx(
573 struct session_config_validation_ctx
*ctx
)
575 if (ctx
->parser_ctx
) {
576 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
580 xmlSchemaFree(ctx
->schema
);
583 if (ctx
->schema_validation_ctx
) {
584 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
587 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
591 char *get_session_config_xsd_path()
594 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
595 size_t base_path_len
;
599 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
602 base_path_len
= strlen(base_path
);
603 max_path_len
= base_path_len
+
604 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
605 xsd_path
= zmalloc(max_path_len
);
610 strncpy(xsd_path
, base_path
, max_path_len
);
611 if (xsd_path
[base_path_len
- 1] != '/') {
612 xsd_path
[base_path_len
++] = '/';
615 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
616 max_path_len
- base_path_len
);
622 int init_session_config_validation_ctx(
623 struct session_config_validation_ctx
*ctx
)
626 char *xsd_path
= get_session_config_xsd_path();
629 ret
= -LTTNG_ERR_NOMEM
;
633 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
634 if (!ctx
->parser_ctx
) {
635 ERR("XSD parser context creation failed");
636 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
639 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
640 xml_error_handler
, NULL
);
642 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
644 ERR("XSD parsing failed");
645 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
649 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
650 if (!ctx
->schema_validation_ctx
) {
651 ERR("XSD validation context creation failed");
652 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
656 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
657 xml_error_handler
, NULL
);
662 fini_session_config_validation_ctx(ctx
);
670 int parse_uint(xmlChar
*str
, uint64_t *val
)
680 *val
= strtoull((const char *) str
, &endptr
, 10);
681 if (!endptr
|| *endptr
) {
692 int parse_int(xmlChar
*str
, int64_t *val
)
702 *val
= strtoll((const char *) str
, &endptr
, 10);
703 if (!endptr
|| *endptr
) {
714 int parse_bool(xmlChar
*str
, int *val
)
723 if (!strcmp((const char *) str
, config_xml_true
)) {
725 } else if (!strcmp((const char *) str
, config_xml_false
)) {
728 WARN("Invalid boolean value encoutered (%s).",
737 int get_domain_type(xmlChar
*domain
)
745 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
746 ret
= LTTNG_DOMAIN_KERNEL
;
747 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
748 ret
= LTTNG_DOMAIN_UST
;
749 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
750 ret
= LTTNG_DOMAIN_JUL
;
761 int get_buffer_type(xmlChar
*buffer_type
)
769 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
770 ret
= LTTNG_BUFFER_GLOBAL
;
771 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
772 ret
= LTTNG_BUFFER_PER_UID
;
773 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
774 ret
= LTTNG_BUFFER_PER_PID
;
785 int get_overwrite_mode(xmlChar
*overwrite_mode
)
789 if (!overwrite_mode
) {
793 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
795 } else if (!strcmp((char *) overwrite_mode
,
796 config_overwrite_mode_discard
)) {
808 int get_output_type(xmlChar
*output_type
)
816 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
817 ret
= LTTNG_EVENT_MMAP
;
818 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
819 ret
= LTTNG_EVENT_SPLICE
;
830 int get_event_type(xmlChar
*event_type
)
838 if (!strcmp((char *) event_type
, config_event_type_all
)) {
839 ret
= LTTNG_EVENT_ALL
;
840 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
841 ret
= LTTNG_EVENT_TRACEPOINT
;
842 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
843 ret
= LTTNG_EVENT_PROBE
;
844 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
845 ret
= LTTNG_EVENT_FUNCTION
;
846 } else if (!strcmp((char *) event_type
,
847 config_event_type_function_entry
)) {
848 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
849 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
850 ret
= LTTNG_EVENT_NOOP
;
851 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
852 ret
= LTTNG_EVENT_SYSCALL
;
863 int get_loglevel_type(xmlChar
*loglevel_type
)
867 if (!loglevel_type
) {
871 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
872 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
873 } else if (!strcmp((char *) loglevel_type
,
874 config_loglevel_type_range
)) {
875 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
876 } else if (!strcmp((char *) loglevel_type
,
877 config_loglevel_type_single
)) {
878 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
889 * Return the context type or -1 on error.
892 int get_context_type(xmlChar
*context_type
)
900 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
901 ret
= LTTNG_EVENT_CONTEXT_PID
;
902 } else if (!strcmp((char *) context_type
,
903 config_event_context_procname
)) {
904 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
905 } else if (!strcmp((char *) context_type
,
906 config_event_context_prio
)) {
907 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
908 } else if (!strcmp((char *) context_type
,
909 config_event_context_nice
)) {
910 ret
= LTTNG_EVENT_CONTEXT_NICE
;
911 } else if (!strcmp((char *) context_type
,
912 config_event_context_vpid
)) {
913 ret
= LTTNG_EVENT_CONTEXT_VPID
;
914 } else if (!strcmp((char *) context_type
,
915 config_event_context_tid
)) {
916 ret
= LTTNG_EVENT_CONTEXT_TID
;
917 } else if (!strcmp((char *) context_type
,
918 config_event_context_vtid
)) {
919 ret
= LTTNG_EVENT_CONTEXT_VTID
;
920 } else if (!strcmp((char *) context_type
,
921 config_event_context_ppid
)) {
922 ret
= LTTNG_EVENT_CONTEXT_PPID
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_vppid
)) {
925 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_pthread_id
)) {
928 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_hostname
)) {
931 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_ip
)) {
934 ret
= LTTNG_EVENT_CONTEXT_IP
;
945 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
950 for (node
= xmlFirstElementChild(domain_node
); node
;
951 node
= xmlNextElementSibling(node
)) {
952 if (!strcmp((const char *) node
->name
, config_element_type
)) {
954 xmlChar
*node_content
= xmlNodeGetContent(node
);
956 ret
= -LTTNG_ERR_NOMEM
;
960 ret
= get_domain_type(node_content
);
963 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
968 } else if (!strcmp((const char *) node
->name
,
969 config_element_buffer_type
)) {
971 xmlChar
*node_content
= xmlNodeGetContent(node
);
973 ret
= -LTTNG_ERR_NOMEM
;
977 ret
= get_buffer_type(node_content
);
980 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
984 domain
->buf_type
= ret
;
993 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
998 for (node
= xmlFirstElementChild(net_output_node
); node
;
999 node
= xmlNextElementSibling(node
)) {
1000 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1002 *control_uri
= (char *) xmlNodeGetContent(node
);
1003 if (!*control_uri
) {
1008 *data_uri
= (char *) xmlNodeGetContent(node
);
1015 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1019 int process_consumer_output(xmlNodePtr consumer_output_node
,
1020 struct consumer_output
*output
)
1027 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1028 node
= xmlNextElementSibling(node
)) {
1029 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1030 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1034 ret
= -LTTNG_ERR_NOMEM
;
1038 ret
= parse_bool(enabled_str
, &output
->enabled
);
1044 xmlNodePtr output_type_node
;
1047 output_type_node
= xmlFirstElementChild(node
);
1048 if (!output_type_node
) {
1049 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1053 if (!strcmp((const char *) output_type_node
->name
,
1054 config_element_path
)) {
1056 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1057 if (!output
->path
) {
1058 ret
= -LTTNG_ERR_NOMEM
;
1063 ret
= get_net_output_uris(output_type_node
,
1064 &output
->control_uri
, &output
->data_uri
);
1076 free(output
->control_uri
);
1077 free(output
->data_uri
);
1078 memset(output
, 0, sizeof(struct consumer_output
));
1084 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1085 const char *control_uri
, const char *data_uri
)
1088 struct lttng_handle
*handle
;
1089 const char *uri
= NULL
;
1094 handle
= lttng_create_handle(name
, domain
);
1096 ret
= -LTTNG_ERR_NOMEM
;
1100 if (!control_uri
|| !data_uri
) {
1101 uri
= control_uri
? control_uri
: data_uri
;
1106 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1107 lttng_destroy_handle(handle
);
1113 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1116 xmlNodePtr node
= NULL
;
1117 xmlNodePtr snapshot_output_list_node
;
1118 xmlNodePtr snapshot_output_node
;
1120 assert(session_name
);
1122 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1131 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1133 /* Parse and create snapshot outputs */
1135 for (snapshot_output_node
=
1136 xmlFirstElementChild(snapshot_output_list_node
);
1137 snapshot_output_node
; snapshot_output_node
=
1138 xmlNextElementSibling(snapshot_output_node
)) {
1140 uint64_t max_size
= UINT64_MAX
;
1141 struct consumer_output output
= { 0 };
1142 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1144 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1145 node
= xmlNextElementSibling(node
)) {
1146 if (!strcmp((const char *) node
->name
,
1147 config_element_name
)) {
1149 name
= (char *) xmlNodeGetContent(node
);
1151 ret
= -LTTNG_ERR_NOMEM
;
1152 goto error_snapshot_output
;
1154 } else if (!strcmp((const char *) node
->name
,
1155 config_element_max_size
)) {
1156 xmlChar
*content
= xmlNodeGetContent(node
);
1160 ret
= -LTTNG_ERR_NOMEM
;
1161 goto error_snapshot_output
;
1163 ret
= parse_uint(content
, &max_size
);
1166 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1167 goto error_snapshot_output
;
1170 /* consumer_output */
1171 ret
= process_consumer_output(node
, &output
);
1173 goto error_snapshot_output
;
1178 snapshot_output
= lttng_snapshot_output_create();
1179 if (!snapshot_output
) {
1180 ret
= -LTTNG_ERR_NOMEM
;
1181 goto error_snapshot_output
;
1184 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1186 goto error_snapshot_output
;
1189 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1191 goto error_snapshot_output
;
1195 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1198 goto error_snapshot_output
;
1201 if (output
.control_uri
) {
1202 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1205 goto error_snapshot_output
;
1209 if (output
.data_uri
) {
1210 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1213 goto error_snapshot_output
;
1218 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1219 error_snapshot_output
:
1222 free(output
.control_uri
);
1223 free(output
.data_uri
);
1224 lttng_snapshot_output_destroy(snapshot_output
);
1234 int create_session(const char *name
,
1235 struct lttng_domain
*kernel_domain
,
1236 struct lttng_domain
*ust_domain
,
1237 struct lttng_domain
*jul_domain
,
1238 xmlNodePtr output_node
,
1239 uint64_t live_timer_interval
)
1242 struct consumer_output output
= { 0 };
1243 xmlNodePtr consumer_output_node
;
1248 consumer_output_node
= xmlFirstElementChild(output_node
);
1249 if (!consumer_output_node
) {
1250 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1254 if (strcmp((const char *) consumer_output_node
->name
,
1255 config_element_consumer_output
)) {
1256 WARN("Invalid output type, expected %s node",
1257 config_element_consumer_output
);
1258 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1262 ret
= process_consumer_output(consumer_output_node
, &output
);
1268 if (live_timer_interval
!= UINT64_MAX
&&
1269 !output
.control_uri
&& !output
.data_uri
) {
1270 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1274 if (output
.control_uri
|| output
.data_uri
) {
1276 struct lttng_domain
*domain
;
1277 struct lttng_domain
*domains
[] =
1278 { kernel_domain
, ust_domain
, jul_domain
};
1280 /* network destination */
1281 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1283 * URLs are provided for sure since the test above make sure that
1284 * with a live timer the data and control URIs are provided. So,
1285 * NULL is passed here and will be set right after.
1287 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1289 ret
= lttng_create_session(name
, NULL
);
1295 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1296 domain
= domains
[i
];
1301 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1308 /* either local output or no output */
1309 ret
= lttng_create_session(name
, output
.path
);
1316 free(output
.control_uri
);
1317 free(output
.data_uri
);
1321 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1322 struct lttng_event_probe_attr
*attr
)
1326 assert(probe_attribute_node
);
1329 if (!strcmp((const char *) probe_attribute_node
->name
,
1330 config_element_address
)) {
1335 content
= xmlNodeGetContent(probe_attribute_node
);
1337 ret
= -LTTNG_ERR_NOMEM
;
1341 ret
= parse_uint(content
, &addr
);
1344 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1349 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1350 config_element_offset
)) {
1352 uint64_t offset
= 0;
1355 content
= xmlNodeGetContent(probe_attribute_node
);
1357 ret
= -LTTNG_ERR_NOMEM
;
1361 ret
= parse_uint(content
, &offset
);
1364 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1368 attr
->offset
= offset
;
1369 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1370 config_element_symbol_name
)) {
1375 content
= xmlNodeGetContent(probe_attribute_node
);
1377 ret
= -LTTNG_ERR_NOMEM
;
1381 name_len
= strlen((char *) content
);
1382 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1383 WARN("symbol_name too long.");
1384 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1389 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1398 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1399 const char *channel_name
)
1403 struct lttng_event event
;
1404 char **exclusions
= NULL
;
1405 unsigned long exclusion_count
= 0;
1406 char *filter_expression
= NULL
;
1410 assert(channel_name
);
1412 memset(&event
, 0, sizeof(event
));
1414 for (node
= xmlFirstElementChild(event_node
); node
;
1415 node
= xmlNextElementSibling(node
)) {
1416 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1421 content
= xmlNodeGetContent(node
);
1423 ret
= -LTTNG_ERR_NOMEM
;
1427 name_len
= strlen((char *) content
);
1428 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1429 WARN("Channel name too long.");
1430 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1435 strncpy(event
.name
, (const char *) content
, name_len
);
1437 } else if (!strcmp((const char *) node
->name
,
1438 config_element_enabled
)) {
1439 xmlChar
*content
= xmlNodeGetContent(node
);
1443 ret
= -LTTNG_ERR_NOMEM
;
1447 ret
= parse_bool(content
, &event
.enabled
);
1450 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1453 } else if (!strcmp((const char *) node
->name
,
1454 config_element_type
)) {
1455 xmlChar
*content
= xmlNodeGetContent(node
);
1459 ret
= -LTTNG_ERR_NOMEM
;
1463 ret
= get_event_type(content
);
1466 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1471 } else if (!strcmp((const char *) node
->name
,
1472 config_element_loglevel_type
)) {
1473 xmlChar
*content
= xmlNodeGetContent(node
);
1477 ret
= -LTTNG_ERR_NOMEM
;
1481 ret
= get_loglevel_type(content
);
1484 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1488 event
.loglevel_type
= ret
;
1489 } else if (!strcmp((const char *) node
->name
,
1490 config_element_loglevel
)) {
1492 int64_t loglevel
= 0;
1495 content
= xmlNodeGetContent(node
);
1497 ret
= -LTTNG_ERR_NOMEM
;
1501 ret
= parse_int(content
, &loglevel
);
1504 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1508 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1509 WARN("loglevel out of range.");
1510 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1514 event
.loglevel
= loglevel
;
1515 } else if (!strcmp((const char *) node
->name
,
1516 config_element_filter
)) {
1518 xmlNodeGetContent(node
);
1522 ret
= -LTTNG_ERR_NOMEM
;
1526 filter_expression
= strdup((char *) content
);
1528 if (!filter_expression
) {
1529 ret
= -LTTNG_ERR_NOMEM
;
1532 } else if (!strcmp((const char *) node
->name
,
1533 config_element_exclusions
)) {
1534 xmlNodePtr exclusion_node
;
1535 int exclusion_index
= 0;
1540 * Exclusions has already been initialized,
1543 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1547 exclusion_count
= xmlChildElementCount(node
);
1548 if (!exclusion_count
) {
1552 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1554 exclusion_count
= 0;
1555 ret
= -LTTNG_ERR_NOMEM
;
1559 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1560 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1562 xmlNodeGetContent(exclusion_node
);
1565 ret
= -LTTNG_ERR_NOMEM
;
1569 exclusions
[exclusion_index
] = strdup((const char *) content
);
1571 if (!exclusions
[exclusion_index
]) {
1572 ret
= -LTTNG_ERR_NOMEM
;
1578 event
.exclusion
= 1;
1579 } else if (!strcmp((const char *) node
->name
,
1580 config_element_attributes
)) {
1581 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1584 if (!attribute_node
) {
1585 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1589 if (!strcmp((const char *) node
->name
,
1590 config_element_probe_attributes
)) {
1591 xmlNodePtr probe_attribute_node
;
1593 /* probe_attributes */
1594 for (probe_attribute_node
=
1595 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1596 probe_attribute_node
= xmlNextElementSibling(
1597 probe_attribute_node
)) {
1599 ret
= process_probe_attribute_node(probe_attribute_node
,
1608 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1610 /* function_attributes */
1611 content
= xmlNodeGetContent(symbol_node
);
1613 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1617 sym_len
= strlen((char *) content
);
1618 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1619 WARN("Function name too long.");
1620 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1625 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1632 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1633 filter_expression
, exclusion_count
, exclusions
);
1635 for (i
= 0; i
< exclusion_count
; i
++) {
1636 free(exclusions
[i
]);
1640 free(filter_expression
);
1645 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1646 const char *channel_name
)
1651 assert(events_node
);
1653 assert(channel_name
);
1655 for (node
= xmlFirstElementChild(events_node
); node
;
1656 node
= xmlNextElementSibling(node
)) {
1657 ret
= process_event_node(node
, handle
, channel_name
);
1667 int process_channel_attr_node(xmlNodePtr attr_node
,
1668 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1669 xmlNodePtr
*events_node
)
1675 assert(contexts_node
);
1676 assert(events_node
);
1678 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1683 content
= xmlNodeGetContent(attr_node
);
1685 ret
= -LTTNG_ERR_NOMEM
;
1689 name_len
= strlen((char *) content
);
1690 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1691 WARN("Channel name too long.");
1692 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1697 strncpy(channel
->name
, (const char *) content
, name_len
);
1699 } else if (!strcmp((const char *) attr_node
->name
,
1700 config_element_enabled
)) {
1705 content
= xmlNodeGetContent(attr_node
);
1707 ret
= -LTTNG_ERR_NOMEM
;
1711 ret
= parse_bool(content
, &enabled
);
1714 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1718 channel
->enabled
= enabled
;
1719 } else if (!strcmp((const char *) attr_node
->name
,
1720 config_element_overwrite_mode
)) {
1723 /* overwrite_mode */
1724 content
= xmlNodeGetContent(attr_node
);
1726 ret
= -LTTNG_ERR_NOMEM
;
1730 ret
= get_overwrite_mode(content
);
1733 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1737 channel
->attr
.overwrite
= ret
;
1738 } else if (!strcmp((const char *) attr_node
->name
,
1739 config_element_subbuf_size
)) {
1742 /* subbuffer_size */
1743 content
= xmlNodeGetContent(attr_node
);
1745 ret
= -LTTNG_ERR_NOMEM
;
1749 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1752 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1755 } else if (!strcmp((const char *) attr_node
->name
,
1756 config_element_num_subbuf
)) {
1759 /* subbuffer_count */
1760 content
= xmlNodeGetContent(attr_node
);
1762 ret
= -LTTNG_ERR_NOMEM
;
1766 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1769 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1772 } else if (!strcmp((const char *) attr_node
->name
,
1773 config_element_switch_timer_interval
)) {
1775 uint64_t switch_timer_interval
= 0;
1777 /* switch_timer_interval */
1778 content
= xmlNodeGetContent(attr_node
);
1780 ret
= -LTTNG_ERR_NOMEM
;
1784 ret
= parse_uint(content
, &switch_timer_interval
);
1787 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1791 if (switch_timer_interval
> UINT_MAX
) {
1792 WARN("switch_timer_interval out of range.");
1793 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1797 channel
->attr
.switch_timer_interval
=
1798 switch_timer_interval
;
1799 } else if (!strcmp((const char *) attr_node
->name
,
1800 config_element_read_timer_interval
)) {
1802 uint64_t read_timer_interval
= 0;
1804 /* read_timer_interval */
1805 content
= xmlNodeGetContent(attr_node
);
1807 ret
= -LTTNG_ERR_NOMEM
;
1811 ret
= parse_uint(content
, &read_timer_interval
);
1814 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1818 if (read_timer_interval
> UINT_MAX
) {
1819 WARN("read_timer_interval out of range.");
1820 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1824 channel
->attr
.read_timer_interval
=
1825 read_timer_interval
;
1826 } else if (!strcmp((const char *) attr_node
->name
,
1827 config_element_output_type
)) {
1831 content
= xmlNodeGetContent(attr_node
);
1833 ret
= -LTTNG_ERR_NOMEM
;
1837 ret
= get_output_type(content
);
1840 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1844 channel
->attr
.output
= ret
;
1845 } else if (!strcmp((const char *) attr_node
->name
,
1846 config_element_tracefile_size
)) {
1849 /* tracefile_size */
1850 content
= xmlNodeGetContent(attr_node
);
1852 ret
= -LTTNG_ERR_NOMEM
;
1856 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1859 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1862 } else if (!strcmp((const char *) attr_node
->name
,
1863 config_element_tracefile_count
)) {
1866 /* tracefile_count */
1867 content
= xmlNodeGetContent(attr_node
);
1869 ret
= -LTTNG_ERR_NOMEM
;
1873 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1876 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1879 } else if (!strcmp((const char *) attr_node
->name
,
1880 config_element_live_timer_interval
)) {
1882 uint64_t live_timer_interval
= 0;
1884 /* live_timer_interval */
1885 content
= xmlNodeGetContent(attr_node
);
1887 ret
= -LTTNG_ERR_NOMEM
;
1891 ret
= parse_uint(content
, &live_timer_interval
);
1894 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1898 if (live_timer_interval
> UINT_MAX
) {
1899 WARN("live_timer_interval out of range.");
1900 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1904 channel
->attr
.live_timer_interval
=
1905 live_timer_interval
;
1906 } else if (!strcmp((const char *) attr_node
->name
,
1907 config_element_events
)) {
1909 *events_node
= attr_node
;
1912 *contexts_node
= attr_node
;
1920 int process_context_node(xmlNodePtr context_node
,
1921 struct lttng_handle
*handle
, const char *channel_name
)
1924 struct lttng_event_context context
;
1925 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1928 assert(channel_name
);
1930 if (!context_child_node
) {
1931 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1935 memset(&context
, 0, sizeof(context
));
1937 if (!strcmp((const char *) context_child_node
->name
,
1938 config_element_type
)) {
1940 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1942 ret
= -LTTNG_ERR_NOMEM
;
1946 ret
= get_context_type(content
);
1949 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1955 xmlNodePtr perf_attr_node
;
1958 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1959 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1960 perf_attr_node
; perf_attr_node
=
1961 xmlNextElementSibling(perf_attr_node
)) {
1962 if (!strcmp((const char *) perf_attr_node
->name
,
1963 config_element_type
)) {
1968 content
= xmlNodeGetContent(perf_attr_node
);
1970 ret
= -LTTNG_ERR_NOMEM
;
1974 ret
= parse_uint(content
, &type
);
1977 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1981 if (type
> UINT32_MAX
) {
1982 WARN("perf context type out of range.");
1983 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1987 context
.u
.perf_counter
.type
= type
;
1988 } else if (!strcmp((const char *) perf_attr_node
->name
,
1989 config_element_config
)) {
1991 uint64_t config
= 0;
1994 content
= xmlNodeGetContent(perf_attr_node
);
1996 ret
= -LTTNG_ERR_NOMEM
;
2000 ret
= parse_uint(content
, &config
);
2003 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2007 context
.u
.perf_counter
.config
= config
;
2008 } else if (!strcmp((const char *) perf_attr_node
->name
,
2009 config_element_name
)) {
2014 content
= xmlNodeGetContent(perf_attr_node
);
2016 ret
= -LTTNG_ERR_NOMEM
;
2020 name_len
= strlen((char *) content
);
2021 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2022 WARN("perf context name too long.");
2023 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2028 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2035 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2041 int process_contexts_node(xmlNodePtr contexts_node
,
2042 struct lttng_handle
*handle
, const char *channel_name
)
2045 xmlNodePtr context_node
;
2047 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2048 context_node
= xmlNextElementSibling(context_node
)) {
2049 ret
= process_context_node(context_node
, handle
, channel_name
);
2059 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2062 struct lttng_domain domain
= { 0 };
2063 struct lttng_handle
*handle
= NULL
;
2064 xmlNodePtr channels_node
= NULL
;
2067 assert(session_name
);
2069 ret
= init_domain(domain_node
, &domain
);
2074 handle
= lttng_create_handle(session_name
, &domain
);
2076 ret
= -LTTNG_ERR_NOMEM
;
2080 /* get the channels node */
2081 for (node
= xmlFirstElementChild(domain_node
); node
;
2082 node
= xmlNextElementSibling(node
)) {
2083 if (!strcmp((const char *) node
->name
,
2084 config_element_channels
)) {
2085 channels_node
= node
;
2090 if (!channels_node
) {
2094 /* create all channels */
2095 for (node
= xmlFirstElementChild(channels_node
); node
;
2096 node
= xmlNextElementSibling(node
)) {
2097 struct lttng_channel channel
;
2098 xmlNodePtr contexts_node
= NULL
;
2099 xmlNodePtr events_node
= NULL
;
2100 xmlNodePtr channel_attr_node
;
2102 memset(&channel
, 0, sizeof(channel
));
2103 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2105 for (channel_attr_node
= xmlFirstElementChild(node
);
2106 channel_attr_node
; channel_attr_node
=
2107 xmlNextElementSibling(channel_attr_node
)) {
2108 ret
= process_channel_attr_node(channel_attr_node
,
2109 &channel
, &contexts_node
, &events_node
);
2115 ret
= lttng_enable_channel(handle
, &channel
);
2120 ret
= process_events_node(events_node
, handle
, channel
.name
);
2125 ret
= process_contexts_node(contexts_node
, handle
,
2132 lttng_destroy_handle(handle
);
2137 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2140 int ret
, started
= -1, snapshot_mode
= -1;
2141 uint64_t live_timer_interval
= UINT64_MAX
;
2143 xmlNodePtr domains_node
= NULL
;
2144 xmlNodePtr output_node
= NULL
;
2146 struct lttng_domain
*kernel_domain
= NULL
;
2147 struct lttng_domain
*ust_domain
= NULL
;
2148 struct lttng_domain
*jul_domain
= NULL
;
2150 for (node
= xmlFirstElementChild(session_node
); node
;
2151 node
= xmlNextElementSibling(node
)) {
2152 if (!name
&& !strcmp((const char *) node
->name
,
2153 config_element_name
)) {
2155 xmlChar
*node_content
= xmlNodeGetContent(node
);
2156 if (!node_content
) {
2157 ret
= -LTTNG_ERR_NOMEM
;
2161 name
= (char *) node_content
;
2162 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2163 config_element_domains
)) {
2165 domains_node
= node
;
2166 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2167 config_element_started
)) {
2169 xmlChar
*node_content
= xmlNodeGetContent(node
);
2170 if (!node_content
) {
2171 ret
= -LTTNG_ERR_NOMEM
;
2175 ret
= parse_bool(node_content
, &started
);
2178 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2181 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2182 config_element_output
)) {
2186 /* attributes, snapshot_mode or live_timer_interval */
2187 xmlNodePtr attributes_child
=
2188 xmlFirstElementChild(node
);
2190 if (!strcmp((const char *) attributes_child
->name
,
2191 config_element_snapshot_mode
)) {
2193 xmlChar
*snapshot_mode_content
=
2194 xmlNodeGetContent(attributes_child
);
2195 if (!snapshot_mode_content
) {
2196 ret
= -LTTNG_ERR_NOMEM
;
2200 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2201 free(snapshot_mode_content
);
2203 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2207 /* live_timer_interval */
2208 xmlChar
*timer_interval_content
=
2209 xmlNodeGetContent(attributes_child
);
2210 if (!timer_interval_content
) {
2211 ret
= -LTTNG_ERR_NOMEM
;
2215 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2216 free(timer_interval_content
);
2218 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2226 /* Mandatory attribute, as defined in the session XSD */
2227 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2231 if (session_name
&& strcmp(name
, session_name
)) {
2232 /* This is not the session we are looking for */
2233 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2237 /* Init domains to create the session handles */
2238 for (node
= xmlFirstElementChild(domains_node
); node
;
2239 node
= xmlNextElementSibling(node
)) {
2240 struct lttng_domain
*domain
;
2242 domain
= zmalloc(sizeof(*domain
));
2244 ret
= -LTTNG_ERR_NOMEM
;
2248 ret
= init_domain(node
, domain
);
2250 goto domain_init_error
;
2253 switch (domain
->type
) {
2254 case LTTNG_DOMAIN_KERNEL
:
2255 if (kernel_domain
) {
2256 /* Same domain seen twice, invalid! */
2257 goto domain_init_error
;
2259 kernel_domain
= domain
;
2261 case LTTNG_DOMAIN_UST
:
2263 /* Same domain seen twice, invalid! */
2264 goto domain_init_error
;
2266 ust_domain
= domain
;
2268 case LTTNG_DOMAIN_JUL
:
2270 /* Same domain seen twice, invalid! */
2271 goto domain_init_error
;
2273 jul_domain
= domain
;
2276 WARN("Invalid domain type");
2277 goto domain_init_error
;
2282 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2287 /* Destroy session if it exists */
2288 ret
= lttng_destroy_session(name
);
2289 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2290 ERR("Failed to destroy existing session.");
2295 /* Create session type depending on output type */
2296 if (snapshot_mode
&& snapshot_mode
!= -1) {
2297 ret
= create_snapshot_session(name
, output_node
);
2298 } else if (live_timer_interval
&&
2299 live_timer_interval
!= UINT64_MAX
) {
2300 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2301 output_node
, live_timer_interval
);
2303 /* regular session */
2304 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2305 output_node
, UINT64_MAX
);
2312 for (node
= xmlFirstElementChild(domains_node
); node
;
2313 node
= xmlNextElementSibling(node
)) {
2314 ret
= process_domain_node(node
, name
);
2321 ret
= lttng_start_tracing(name
);
2328 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2329 lttng_destroy_session(name
);
2332 free(kernel_domain
);
2340 int load_session_from_file(const char *path
, const char *session_name
,
2341 struct session_config_validation_ctx
*validation_ctx
, int override
)
2343 int ret
, session_found
= !session_name
;
2344 xmlDocPtr doc
= NULL
;
2345 xmlNodePtr sessions_node
;
2346 xmlNodePtr session_node
;
2350 assert(validation_ctx
);
2352 ret
= stat(path
, &sb
);
2354 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2358 doc
= xmlParseFile(path
);
2360 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2364 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2366 ERR("Session configuration file validation failed");
2367 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2371 sessions_node
= xmlDocGetRootElement(doc
);
2372 if (!sessions_node
) {
2376 for (session_node
= xmlFirstElementChild(sessions_node
);
2377 session_node
; session_node
=
2378 xmlNextElementSibling(session_node
)) {
2379 ret
= process_session_node(session_node
,
2380 session_name
, override
);
2381 if (session_name
&& ret
== 0) {
2382 /* Target session found and loaded */
2390 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2396 int load_session_from_path(const char *path
, const char *session_name
,
2397 struct session_config_validation_ctx
*validation_ctx
, int override
)
2399 int ret
, session_found
= !session_name
;
2400 DIR *directory
= NULL
;
2403 assert(validation_ctx
);
2405 directory
= opendir(path
);
2409 /* Try the file loading. */
2412 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2415 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2420 struct dirent
*entry
;
2421 struct dirent
*result
;
2422 char *file_path
= NULL
;
2423 size_t path_len
= strlen(path
);
2425 if (path_len
>= PATH_MAX
) {
2426 ret
= -LTTNG_ERR_INVALID
;
2430 entry
= zmalloc(sizeof(*entry
));
2432 ret
= -LTTNG_ERR_NOMEM
;
2436 file_path
= zmalloc(PATH_MAX
);
2438 ret
= -LTTNG_ERR_NOMEM
;
2443 strncpy(file_path
, path
, path_len
);
2444 if (file_path
[path_len
- 1] != '/') {
2445 file_path
[path_len
++] = '/';
2449 /* Search for *.lttng files */
2450 while (!readdir_r(directory
, entry
, &result
) && result
) {
2451 size_t file_name_len
= strlen(result
->d_name
);
2453 if (file_name_len
<=
2454 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2458 if (path_len
+ file_name_len
>= PATH_MAX
) {
2462 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2463 result
->d_name
+ file_name_len
- sizeof(
2464 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2468 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2469 file_path
[path_len
+ file_name_len
] = '\0';
2471 ret
= load_session_from_file(file_path
, session_name
,
2472 validation_ctx
, override
);
2473 if (session_name
&& !ret
) {
2482 ret
= load_session_from_file(path
, session_name
,
2483 validation_ctx
, override
);
2493 if (closedir(directory
)) {
2498 if (!session_found
) {
2499 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2506 int config_load_session(const char *path
, const char *session_name
,
2510 struct session_config_validation_ctx validation_ctx
= { 0 };
2512 ret
= init_session_config_validation_ctx(&validation_ctx
);
2519 char *home_path
= utils_get_home_dir();
2523 ret
= asprintf(&path
, DEFAULT_SESSION_HOME_CONFIGPATH
,
2529 ret
= load_session_from_path(path
, session_name
,
2530 &validation_ctx
, 0);
2531 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2532 /* Session found or an error occured */
2540 /* Try system session configuration path */
2541 ret
= load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH
,
2542 session_name
, &validation_ctx
, 0);
2543 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2544 /* Session found or an error occured */
2548 ret
= access(path
, F_OK
);
2553 ret
= -LTTNG_ERR_INVALID
;
2554 WARN("Session configuration path does not exist.");
2557 ret
= -LTTNG_ERR_EPERM
;
2560 ret
= -LTTNG_ERR_UNK
;
2566 ret
= load_session_from_path(path
, session_name
,
2567 &validation_ctx
, override
);
2570 fini_session_config_validation_ctx(&validation_ctx
);
2571 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2573 * Don't report an error if no sessions are found when called
2574 * without a session_name or a search path.