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
)
194 FILE *config_file
= NULL
;
195 struct handler_filter_args filter
= { section
, handler
, user_data
};
198 config_file
= fopen(override_path
, "r");
200 DBG("Loaded daemon configuration file at %s",
203 ERR("Failed to open daemon configuration file at %s",
209 char *path
= utils_get_home_dir();
211 /* Try to open the user's daemon configuration file */
213 ret
= asprintf(&path
, DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
219 config_file
= fopen(path
, "r");
221 DBG("Loaded daemon configuration file at %s", path
);
227 /* Try to open the system daemon configuration file */
229 config_file
= fopen(DEFAULT_DAEMON_HOME_CONFIGPATH
, "r");
234 DBG("No daemon configuration file found.");
238 ret
= ini_parse_file(config_file
,
239 (ini_entry_handler
) config_entry_handler_filter
, (void *) &filter
);
247 int config_parse_value(const char *value
)
250 char *endptr
, *lower_str
;
260 v
= strtoul(value
, &endptr
, 10);
261 if (endptr
!= value
) {
266 lower_str
= zmalloc(len
+ 1);
273 for (i
= 0; i
< len
; i
++) {
274 lower_str
[i
] = tolower(value
[i
]);
277 if (!strcmp(lower_str
, config_str_yes
) ||
278 !strcmp(lower_str
, config_str_true
) ||
279 !strcmp(lower_str
, config_str_on
)) {
281 } else if (!strcmp(lower_str
, config_str_no
) ||
282 !strcmp(lower_str
, config_str_false
) ||
283 !strcmp(lower_str
, config_str_off
)) {
295 * Returns a xmlChar string which must be released using xmlFree().
297 static xmlChar
*encode_string(const char *in_str
)
299 xmlChar
*out_str
= NULL
;
300 xmlCharEncodingHandlerPtr handler
;
301 int out_len
, ret
, in_len
;
305 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
307 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
311 in_len
= strlen(in_str
);
313 * Add 1 byte for the NULL terminted character. The factor 2 here is
314 * because UTF-8 can be on two bytes so this fits the worst case for each
317 out_len
= (in_len
* 2) + 1;
318 out_str
= xmlMalloc(out_len
);
323 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
330 /* out_len is now the size of out_str */
331 out_str
[out_len
] = '\0';
337 struct config_writer
*config_writer_create(int fd_output
)
340 struct config_writer
*writer
;
341 xmlOutputBufferPtr buffer
;
343 writer
= zmalloc(sizeof(struct config_writer
));
345 PERROR("zmalloc config_writer_create");
349 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
354 writer
->writer
= xmlNewTextWriter(buffer
);
355 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
356 config_xml_encoding
, NULL
);
361 ret
= xmlTextWriterSetIndentString(writer
->writer
,
362 BAD_CAST config_xml_indent_string
);
367 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
375 config_writer_destroy(writer
);
380 int config_writer_destroy(struct config_writer
*writer
)
389 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
390 WARN("Could not close XML document");
394 if (writer
->writer
) {
395 xmlFreeTextWriter(writer
->writer
);
404 int config_writer_open_element(struct config_writer
*writer
,
405 const char *element_name
)
408 xmlChar
*encoded_element_name
;
410 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
415 encoded_element_name
= encode_string(element_name
);
416 if (!encoded_element_name
) {
421 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
422 xmlFree(encoded_element_name
);
424 return ret
> 0 ? 0 : ret
;
428 int config_writer_close_element(struct config_writer
*writer
)
432 if (!writer
|| !writer
->writer
) {
437 ret
= xmlTextWriterEndElement(writer
->writer
);
439 return ret
> 0 ? 0 : ret
;
443 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
444 const char *element_name
, uint64_t value
)
447 xmlChar
*encoded_element_name
;
449 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
454 encoded_element_name
= encode_string(element_name
);
455 if (!encoded_element_name
) {
460 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
461 encoded_element_name
, "%" PRIu64
, value
);
462 xmlFree(encoded_element_name
);
464 return ret
> 0 ? 0 : ret
;
468 int config_writer_write_element_signed_int(struct config_writer
*writer
,
469 const char *element_name
, int64_t value
)
472 xmlChar
*encoded_element_name
;
474 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
479 encoded_element_name
= encode_string(element_name
);
480 if (!encoded_element_name
) {
485 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
486 encoded_element_name
, "%" PRIi64
, value
);
487 xmlFree(encoded_element_name
);
489 return ret
> 0 ? 0 : ret
;
493 int config_writer_write_element_bool(struct config_writer
*writer
,
494 const char *element_name
, int value
)
496 return config_writer_write_element_string(writer
, element_name
,
497 value
? config_xml_true
: config_xml_false
);
501 int config_writer_write_element_string(struct config_writer
*writer
,
502 const char *element_name
, const char *value
)
505 xmlChar
*encoded_element_name
= NULL
;
506 xmlChar
*encoded_value
= NULL
;
508 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
514 encoded_element_name
= encode_string(element_name
);
515 if (!encoded_element_name
) {
520 encoded_value
= encode_string(value
);
521 if (!encoded_value
) {
526 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
529 xmlFree(encoded_element_name
);
530 xmlFree(encoded_value
);
531 return ret
> 0 ? 0 : ret
;
535 void xml_error_handler(void *ctx
, const char *format
, ...)
541 va_start(args
, format
);
542 ret
= vasprintf(&errMsg
, format
, args
);
545 ERR("String allocation failed in xml error handler");
549 fprintf(stderr
, "XML Error: %s", errMsg
);
554 void fini_session_config_validation_ctx(
555 struct session_config_validation_ctx
*ctx
)
557 if (ctx
->parser_ctx
) {
558 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
562 xmlSchemaFree(ctx
->schema
);
565 if (ctx
->schema_validation_ctx
) {
566 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
569 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
573 char *get_session_config_xsd_path()
576 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
577 size_t base_path_len
;
581 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
584 base_path_len
= strlen(base_path
);
585 max_path_len
= base_path_len
+
586 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
587 xsd_path
= zmalloc(max_path_len
);
592 strncpy(xsd_path
, base_path
, max_path_len
);
593 if (xsd_path
[base_path_len
- 1] != '/') {
594 xsd_path
[base_path_len
++] = '/';
597 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
598 max_path_len
- base_path_len
);
604 int init_session_config_validation_ctx(
605 struct session_config_validation_ctx
*ctx
)
608 char *xsd_path
= get_session_config_xsd_path();
611 ret
= -LTTNG_ERR_NOMEM
;
615 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
616 if (!ctx
->parser_ctx
) {
617 ERR("XSD parser context creation failed");
618 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
621 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
622 xml_error_handler
, NULL
);
624 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
626 ERR("XSD parsing failed");
627 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
631 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
632 if (!ctx
->schema_validation_ctx
) {
633 ERR("XSD validation context creation failed");
634 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
638 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
639 xml_error_handler
, NULL
);
644 fini_session_config_validation_ctx(ctx
);
652 int parse_uint(xmlChar
*str
, uint64_t *val
)
662 *val
= strtoull((const char *) str
, &endptr
, 10);
663 if (!endptr
|| *endptr
) {
674 int parse_int(xmlChar
*str
, int64_t *val
)
684 *val
= strtoll((const char *) str
, &endptr
, 10);
685 if (!endptr
|| *endptr
) {
696 int parse_bool(xmlChar
*str
, int *val
)
705 if (!strcmp((const char *) str
, config_xml_true
)) {
707 } else if (!strcmp((const char *) str
, config_xml_false
)) {
710 WARN("Invalid boolean value encoutered (%s).",
719 int get_domain_type(xmlChar
*domain
)
727 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
728 ret
= LTTNG_DOMAIN_KERNEL
;
729 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
730 ret
= LTTNG_DOMAIN_UST
;
731 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
732 ret
= LTTNG_DOMAIN_JUL
;
743 int get_buffer_type(xmlChar
*buffer_type
)
751 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
752 ret
= LTTNG_BUFFER_GLOBAL
;
753 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
754 ret
= LTTNG_BUFFER_PER_UID
;
755 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
756 ret
= LTTNG_BUFFER_PER_PID
;
767 int get_overwrite_mode(xmlChar
*overwrite_mode
)
771 if (!overwrite_mode
) {
775 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
777 } else if (!strcmp((char *) overwrite_mode
,
778 config_overwrite_mode_discard
)) {
790 int get_output_type(xmlChar
*output_type
)
798 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
799 ret
= LTTNG_EVENT_MMAP
;
800 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
801 ret
= LTTNG_EVENT_SPLICE
;
812 int get_event_type(xmlChar
*event_type
)
820 if (!strcmp((char *) event_type
, config_event_type_all
)) {
821 ret
= LTTNG_EVENT_ALL
;
822 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
823 ret
= LTTNG_EVENT_TRACEPOINT
;
824 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
825 ret
= LTTNG_EVENT_PROBE
;
826 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
827 ret
= LTTNG_EVENT_FUNCTION
;
828 } else if (!strcmp((char *) event_type
,
829 config_event_type_function_entry
)) {
830 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
831 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
832 ret
= LTTNG_EVENT_NOOP
;
833 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
834 ret
= LTTNG_EVENT_SYSCALL
;
845 int get_loglevel_type(xmlChar
*loglevel_type
)
849 if (!loglevel_type
) {
853 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
854 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
855 } else if (!strcmp((char *) loglevel_type
,
856 config_loglevel_type_range
)) {
857 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
858 } else if (!strcmp((char *) loglevel_type
,
859 config_loglevel_type_single
)) {
860 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
871 * Return the context type or -1 on error.
874 int get_context_type(xmlChar
*context_type
)
882 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
883 ret
= LTTNG_EVENT_CONTEXT_PID
;
884 } else if (!strcmp((char *) context_type
,
885 config_event_context_procname
)) {
886 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
887 } else if (!strcmp((char *) context_type
,
888 config_event_context_prio
)) {
889 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
890 } else if (!strcmp((char *) context_type
,
891 config_event_context_nice
)) {
892 ret
= LTTNG_EVENT_CONTEXT_NICE
;
893 } else if (!strcmp((char *) context_type
,
894 config_event_context_vpid
)) {
895 ret
= LTTNG_EVENT_CONTEXT_VPID
;
896 } else if (!strcmp((char *) context_type
,
897 config_event_context_tid
)) {
898 ret
= LTTNG_EVENT_CONTEXT_TID
;
899 } else if (!strcmp((char *) context_type
,
900 config_event_context_vtid
)) {
901 ret
= LTTNG_EVENT_CONTEXT_VTID
;
902 } else if (!strcmp((char *) context_type
,
903 config_event_context_ppid
)) {
904 ret
= LTTNG_EVENT_CONTEXT_PPID
;
905 } else if (!strcmp((char *) context_type
,
906 config_event_context_vppid
)) {
907 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
908 } else if (!strcmp((char *) context_type
,
909 config_event_context_pthread_id
)) {
910 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
911 } else if (!strcmp((char *) context_type
,
912 config_event_context_hostname
)) {
913 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
914 } else if (!strcmp((char *) context_type
,
915 config_event_context_ip
)) {
916 ret
= LTTNG_EVENT_CONTEXT_IP
;
927 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
932 for (node
= xmlFirstElementChild(domain_node
); node
;
933 node
= xmlNextElementSibling(node
)) {
934 if (!strcmp((const char *) node
->name
, config_element_type
)) {
936 xmlChar
*node_content
= xmlNodeGetContent(node
);
938 ret
= -LTTNG_ERR_NOMEM
;
942 ret
= get_domain_type(node_content
);
945 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
950 } else if (!strcmp((const char *) node
->name
,
951 config_element_buffer_type
)) {
953 xmlChar
*node_content
= xmlNodeGetContent(node
);
955 ret
= -LTTNG_ERR_NOMEM
;
959 ret
= get_buffer_type(node_content
);
962 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
966 domain
->buf_type
= ret
;
975 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
980 for (node
= xmlFirstElementChild(net_output_node
); node
;
981 node
= xmlNextElementSibling(node
)) {
982 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
984 *control_uri
= (char *) xmlNodeGetContent(node
);
990 *data_uri
= (char *) xmlNodeGetContent(node
);
997 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1001 int process_consumer_output(xmlNodePtr consumer_output_node
,
1002 struct consumer_output
*output
)
1009 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1010 node
= xmlNextElementSibling(node
)) {
1011 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1012 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1016 ret
= -LTTNG_ERR_NOMEM
;
1020 ret
= parse_bool(enabled_str
, &output
->enabled
);
1026 xmlNodePtr output_type_node
;
1029 output_type_node
= xmlFirstElementChild(node
);
1030 if (!output_type_node
) {
1031 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1035 if (!strcmp((const char *) output_type_node
->name
,
1036 config_element_path
)) {
1038 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1039 if (!output
->path
) {
1040 ret
= -LTTNG_ERR_NOMEM
;
1045 ret
= get_net_output_uris(output_type_node
,
1046 &output
->control_uri
, &output
->data_uri
);
1058 free(output
->control_uri
);
1059 free(output
->data_uri
);
1060 memset(output
, 0, sizeof(struct consumer_output
));
1066 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1067 const char *control_uri
, const char *data_uri
)
1070 struct lttng_handle
*handle
;
1071 const char *uri
= NULL
;
1076 handle
= lttng_create_handle(name
, domain
);
1078 ret
= -LTTNG_ERR_NOMEM
;
1082 if (!control_uri
|| !data_uri
) {
1083 uri
= control_uri
? control_uri
: data_uri
;
1088 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1089 lttng_destroy_handle(handle
);
1095 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1098 xmlNodePtr node
= NULL
;
1099 xmlNodePtr snapshot_output_list_node
;
1100 xmlNodePtr snapshot_output_node
;
1102 assert(session_name
);
1104 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1113 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1115 /* Parse and create snapshot outputs */
1117 for (snapshot_output_node
=
1118 xmlFirstElementChild(snapshot_output_list_node
);
1119 snapshot_output_node
; snapshot_output_node
=
1120 xmlNextElementSibling(snapshot_output_node
)) {
1122 uint64_t max_size
= UINT64_MAX
;
1123 struct consumer_output output
= { 0 };
1124 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1126 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1127 node
= xmlNextElementSibling(node
)) {
1128 if (!strcmp((const char *) node
->name
,
1129 config_element_name
)) {
1131 name
= (char *) xmlNodeGetContent(node
);
1133 ret
= -LTTNG_ERR_NOMEM
;
1134 goto error_snapshot_output
;
1136 } else if (!strcmp((const char *) node
->name
,
1137 config_element_max_size
)) {
1138 xmlChar
*content
= xmlNodeGetContent(node
);
1142 ret
= -LTTNG_ERR_NOMEM
;
1143 goto error_snapshot_output
;
1145 ret
= parse_uint(content
, &max_size
);
1148 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1149 goto error_snapshot_output
;
1152 /* consumer_output */
1153 ret
= process_consumer_output(node
, &output
);
1155 goto error_snapshot_output
;
1160 snapshot_output
= lttng_snapshot_output_create();
1161 if (!snapshot_output
) {
1162 ret
= -LTTNG_ERR_NOMEM
;
1163 goto error_snapshot_output
;
1166 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1168 goto error_snapshot_output
;
1171 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1173 goto error_snapshot_output
;
1177 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1180 goto error_snapshot_output
;
1183 if (output
.control_uri
) {
1184 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1187 goto error_snapshot_output
;
1191 if (output
.data_uri
) {
1192 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1195 goto error_snapshot_output
;
1200 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1201 error_snapshot_output
:
1204 free(output
.control_uri
);
1205 free(output
.data_uri
);
1206 lttng_snapshot_output_destroy(snapshot_output
);
1216 int create_session(const char *name
,
1217 struct lttng_domain
*kernel_domain
,
1218 struct lttng_domain
*ust_domain
,
1219 struct lttng_domain
*jul_domain
,
1220 xmlNodePtr output_node
,
1221 uint64_t live_timer_interval
)
1224 struct consumer_output output
= { 0 };
1225 xmlNodePtr consumer_output_node
;
1230 consumer_output_node
= xmlFirstElementChild(output_node
);
1231 if (!consumer_output_node
) {
1232 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1236 if (strcmp((const char *) consumer_output_node
->name
,
1237 config_element_consumer_output
)) {
1238 WARN("Invalid output type, expected %s node",
1239 config_element_consumer_output
);
1240 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1244 ret
= process_consumer_output(consumer_output_node
, &output
);
1250 if (live_timer_interval
!= UINT64_MAX
&&
1251 !output
.control_uri
&& !output
.data_uri
) {
1252 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1256 if (output
.control_uri
|| output
.data_uri
) {
1258 struct lttng_domain
*domain
;
1259 struct lttng_domain
*domains
[] =
1260 { kernel_domain
, ust_domain
, jul_domain
};
1262 /* network destination */
1263 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1265 * URLs are provided for sure since the test above make sure that
1266 * with a live timer the data and control URIs are provided. So,
1267 * NULL is passed here and will be set right after.
1269 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1271 ret
= lttng_create_session(name
, NULL
);
1277 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1278 domain
= domains
[i
];
1283 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1290 /* either local output or no output */
1291 ret
= lttng_create_session(name
, output
.path
);
1298 free(output
.control_uri
);
1299 free(output
.data_uri
);
1303 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1304 struct lttng_event_probe_attr
*attr
)
1308 assert(probe_attribute_node
);
1311 if (!strcmp((const char *) probe_attribute_node
->name
,
1312 config_element_address
)) {
1317 content
= xmlNodeGetContent(probe_attribute_node
);
1319 ret
= -LTTNG_ERR_NOMEM
;
1323 ret
= parse_uint(content
, &addr
);
1326 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1331 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1332 config_element_offset
)) {
1334 uint64_t offset
= 0;
1337 content
= xmlNodeGetContent(probe_attribute_node
);
1339 ret
= -LTTNG_ERR_NOMEM
;
1343 ret
= parse_uint(content
, &offset
);
1346 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1350 attr
->offset
= offset
;
1351 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1352 config_element_symbol_name
)) {
1357 content
= xmlNodeGetContent(probe_attribute_node
);
1359 ret
= -LTTNG_ERR_NOMEM
;
1363 name_len
= strlen((char *) content
);
1364 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1365 WARN("symbol_name too long.");
1366 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1371 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1380 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1381 const char *channel_name
)
1385 struct lttng_event event
;
1386 char **exclusions
= NULL
;
1387 unsigned long exclusion_count
= 0;
1388 char *filter_expression
= NULL
;
1392 assert(channel_name
);
1394 memset(&event
, 0, sizeof(event
));
1396 for (node
= xmlFirstElementChild(event_node
); node
;
1397 node
= xmlNextElementSibling(node
)) {
1398 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1403 content
= xmlNodeGetContent(node
);
1405 ret
= -LTTNG_ERR_NOMEM
;
1409 name_len
= strlen((char *) content
);
1410 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1411 WARN("Channel name too long.");
1412 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1417 strncpy(event
.name
, (const char *) content
, name_len
);
1419 } else if (!strcmp((const char *) node
->name
,
1420 config_element_enabled
)) {
1421 xmlChar
*content
= xmlNodeGetContent(node
);
1425 ret
= -LTTNG_ERR_NOMEM
;
1429 ret
= parse_bool(content
, &event
.enabled
);
1432 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1435 } else if (!strcmp((const char *) node
->name
,
1436 config_element_type
)) {
1437 xmlChar
*content
= xmlNodeGetContent(node
);
1441 ret
= -LTTNG_ERR_NOMEM
;
1445 ret
= get_event_type(content
);
1448 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1453 } else if (!strcmp((const char *) node
->name
,
1454 config_element_loglevel_type
)) {
1455 xmlChar
*content
= xmlNodeGetContent(node
);
1459 ret
= -LTTNG_ERR_NOMEM
;
1463 ret
= get_loglevel_type(content
);
1466 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1470 event
.loglevel_type
= ret
;
1471 } else if (!strcmp((const char *) node
->name
,
1472 config_element_loglevel
)) {
1474 int64_t loglevel
= 0;
1477 content
= xmlNodeGetContent(node
);
1479 ret
= -LTTNG_ERR_NOMEM
;
1483 ret
= parse_int(content
, &loglevel
);
1486 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1490 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1491 WARN("loglevel out of range.");
1492 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1496 event
.loglevel
= loglevel
;
1497 } else if (!strcmp((const char *) node
->name
,
1498 config_element_filter
)) {
1500 xmlNodeGetContent(node
);
1504 ret
= -LTTNG_ERR_NOMEM
;
1508 filter_expression
= strdup((char *) content
);
1510 if (!filter_expression
) {
1511 ret
= -LTTNG_ERR_NOMEM
;
1514 } else if (!strcmp((const char *) node
->name
,
1515 config_element_exclusions
)) {
1516 xmlNodePtr exclusion_node
;
1517 int exclusion_index
= 0;
1522 * Exclusions has already been initialized,
1525 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1529 exclusion_count
= xmlChildElementCount(node
);
1530 if (!exclusion_count
) {
1534 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1536 exclusion_count
= 0;
1537 ret
= -LTTNG_ERR_NOMEM
;
1541 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1542 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1544 xmlNodeGetContent(exclusion_node
);
1547 ret
= -LTTNG_ERR_NOMEM
;
1551 exclusions
[exclusion_index
] = strdup((const char *) content
);
1553 if (!exclusions
[exclusion_index
]) {
1554 ret
= -LTTNG_ERR_NOMEM
;
1560 event
.exclusion
= 1;
1561 } else if (!strcmp((const char *) node
->name
,
1562 config_element_attributes
)) {
1563 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1566 if (!attribute_node
) {
1567 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1571 if (!strcmp((const char *) node
->name
,
1572 config_element_probe_attributes
)) {
1573 xmlNodePtr probe_attribute_node
;
1575 /* probe_attributes */
1576 for (probe_attribute_node
=
1577 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1578 probe_attribute_node
= xmlNextElementSibling(
1579 probe_attribute_node
)) {
1581 ret
= process_probe_attribute_node(probe_attribute_node
,
1590 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1592 /* function_attributes */
1593 content
= xmlNodeGetContent(symbol_node
);
1595 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1599 sym_len
= strlen((char *) content
);
1600 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1601 WARN("Function name too long.");
1602 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1607 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1614 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1615 filter_expression
, exclusion_count
, exclusions
);
1617 for (i
= 0; i
< exclusion_count
; i
++) {
1618 free(exclusions
[i
]);
1622 free(filter_expression
);
1627 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1628 const char *channel_name
)
1633 assert(events_node
);
1635 assert(channel_name
);
1637 for (node
= xmlFirstElementChild(events_node
); node
;
1638 node
= xmlNextElementSibling(node
)) {
1639 ret
= process_event_node(node
, handle
, channel_name
);
1649 int process_channel_attr_node(xmlNodePtr attr_node
,
1650 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1651 xmlNodePtr
*events_node
)
1657 assert(contexts_node
);
1658 assert(events_node
);
1660 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1665 content
= xmlNodeGetContent(attr_node
);
1667 ret
= -LTTNG_ERR_NOMEM
;
1671 name_len
= strlen((char *) content
);
1672 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1673 WARN("Channel name too long.");
1674 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1679 strncpy(channel
->name
, (const char *) content
, name_len
);
1681 } else if (!strcmp((const char *) attr_node
->name
,
1682 config_element_enabled
)) {
1687 content
= xmlNodeGetContent(attr_node
);
1689 ret
= -LTTNG_ERR_NOMEM
;
1693 ret
= parse_bool(content
, &enabled
);
1696 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1700 channel
->enabled
= enabled
;
1701 } else if (!strcmp((const char *) attr_node
->name
,
1702 config_element_overwrite_mode
)) {
1705 /* overwrite_mode */
1706 content
= xmlNodeGetContent(attr_node
);
1708 ret
= -LTTNG_ERR_NOMEM
;
1712 ret
= get_overwrite_mode(content
);
1715 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1719 channel
->attr
.overwrite
= ret
;
1720 } else if (!strcmp((const char *) attr_node
->name
,
1721 config_element_subbuf_size
)) {
1724 /* subbuffer_size */
1725 content
= xmlNodeGetContent(attr_node
);
1727 ret
= -LTTNG_ERR_NOMEM
;
1731 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1734 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1737 } else if (!strcmp((const char *) attr_node
->name
,
1738 config_element_num_subbuf
)) {
1741 /* subbuffer_count */
1742 content
= xmlNodeGetContent(attr_node
);
1744 ret
= -LTTNG_ERR_NOMEM
;
1748 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1751 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1754 } else if (!strcmp((const char *) attr_node
->name
,
1755 config_element_switch_timer_interval
)) {
1757 uint64_t switch_timer_interval
= 0;
1759 /* switch_timer_interval */
1760 content
= xmlNodeGetContent(attr_node
);
1762 ret
= -LTTNG_ERR_NOMEM
;
1766 ret
= parse_uint(content
, &switch_timer_interval
);
1769 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1773 if (switch_timer_interval
> UINT_MAX
) {
1774 WARN("switch_timer_interval out of range.");
1775 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1779 channel
->attr
.switch_timer_interval
=
1780 switch_timer_interval
;
1781 } else if (!strcmp((const char *) attr_node
->name
,
1782 config_element_read_timer_interval
)) {
1784 uint64_t read_timer_interval
= 0;
1786 /* read_timer_interval */
1787 content
= xmlNodeGetContent(attr_node
);
1789 ret
= -LTTNG_ERR_NOMEM
;
1793 ret
= parse_uint(content
, &read_timer_interval
);
1796 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1800 if (read_timer_interval
> UINT_MAX
) {
1801 WARN("read_timer_interval out of range.");
1802 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1806 channel
->attr
.read_timer_interval
=
1807 read_timer_interval
;
1808 } else if (!strcmp((const char *) attr_node
->name
,
1809 config_element_output_type
)) {
1813 content
= xmlNodeGetContent(attr_node
);
1815 ret
= -LTTNG_ERR_NOMEM
;
1819 ret
= get_output_type(content
);
1822 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1826 channel
->attr
.output
= ret
;
1827 } else if (!strcmp((const char *) attr_node
->name
,
1828 config_element_tracefile_size
)) {
1831 /* tracefile_size */
1832 content
= xmlNodeGetContent(attr_node
);
1834 ret
= -LTTNG_ERR_NOMEM
;
1838 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1841 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1844 } else if (!strcmp((const char *) attr_node
->name
,
1845 config_element_tracefile_count
)) {
1848 /* tracefile_count */
1849 content
= xmlNodeGetContent(attr_node
);
1851 ret
= -LTTNG_ERR_NOMEM
;
1855 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1858 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1861 } else if (!strcmp((const char *) attr_node
->name
,
1862 config_element_live_timer_interval
)) {
1864 uint64_t live_timer_interval
= 0;
1866 /* live_timer_interval */
1867 content
= xmlNodeGetContent(attr_node
);
1869 ret
= -LTTNG_ERR_NOMEM
;
1873 ret
= parse_uint(content
, &live_timer_interval
);
1876 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1880 if (live_timer_interval
> UINT_MAX
) {
1881 WARN("live_timer_interval out of range.");
1882 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1886 channel
->attr
.live_timer_interval
=
1887 live_timer_interval
;
1888 } else if (!strcmp((const char *) attr_node
->name
,
1889 config_element_events
)) {
1891 *events_node
= attr_node
;
1894 *contexts_node
= attr_node
;
1902 int process_context_node(xmlNodePtr context_node
,
1903 struct lttng_handle
*handle
, const char *channel_name
)
1906 struct lttng_event_context context
;
1907 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1910 assert(channel_name
);
1912 if (!context_child_node
) {
1913 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1917 memset(&context
, 0, sizeof(context
));
1919 if (!strcmp((const char *) context_child_node
->name
,
1920 config_element_type
)) {
1922 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1924 ret
= -LTTNG_ERR_NOMEM
;
1928 ret
= get_context_type(content
);
1931 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1937 xmlNodePtr perf_attr_node
;
1940 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1941 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1942 perf_attr_node
; perf_attr_node
=
1943 xmlNextElementSibling(perf_attr_node
)) {
1944 if (!strcmp((const char *) perf_attr_node
->name
,
1945 config_element_type
)) {
1950 content
= xmlNodeGetContent(perf_attr_node
);
1952 ret
= -LTTNG_ERR_NOMEM
;
1956 ret
= parse_uint(content
, &type
);
1959 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1963 if (type
> UINT32_MAX
) {
1964 WARN("perf context type out of range.");
1965 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1969 context
.u
.perf_counter
.type
= type
;
1970 } else if (!strcmp((const char *) perf_attr_node
->name
,
1971 config_element_config
)) {
1973 uint64_t config
= 0;
1976 content
= xmlNodeGetContent(perf_attr_node
);
1978 ret
= -LTTNG_ERR_NOMEM
;
1982 ret
= parse_uint(content
, &config
);
1985 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1989 context
.u
.perf_counter
.config
= config
;
1990 } else if (!strcmp((const char *) perf_attr_node
->name
,
1991 config_element_name
)) {
1996 content
= xmlNodeGetContent(perf_attr_node
);
1998 ret
= -LTTNG_ERR_NOMEM
;
2002 name_len
= strlen((char *) content
);
2003 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2004 WARN("perf context name too long.");
2005 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2010 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2017 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2023 int process_contexts_node(xmlNodePtr contexts_node
,
2024 struct lttng_handle
*handle
, const char *channel_name
)
2027 xmlNodePtr context_node
;
2029 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2030 context_node
= xmlNextElementSibling(context_node
)) {
2031 ret
= process_context_node(context_node
, handle
, channel_name
);
2041 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2044 struct lttng_domain domain
= { 0 };
2045 struct lttng_handle
*handle
= NULL
;
2046 xmlNodePtr channels_node
= NULL
;
2049 assert(session_name
);
2051 ret
= init_domain(domain_node
, &domain
);
2056 handle
= lttng_create_handle(session_name
, &domain
);
2058 ret
= -LTTNG_ERR_NOMEM
;
2062 /* get the channels node */
2063 for (node
= xmlFirstElementChild(domain_node
); node
;
2064 node
= xmlNextElementSibling(node
)) {
2065 if (!strcmp((const char *) node
->name
,
2066 config_element_channels
)) {
2067 channels_node
= node
;
2072 if (!channels_node
) {
2076 /* create all channels */
2077 for (node
= xmlFirstElementChild(channels_node
); node
;
2078 node
= xmlNextElementSibling(node
)) {
2079 struct lttng_channel channel
;
2080 xmlNodePtr contexts_node
= NULL
;
2081 xmlNodePtr events_node
= NULL
;
2082 xmlNodePtr channel_attr_node
;
2084 memset(&channel
, 0, sizeof(channel
));
2085 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2087 for (channel_attr_node
= xmlFirstElementChild(node
);
2088 channel_attr_node
; channel_attr_node
=
2089 xmlNextElementSibling(channel_attr_node
)) {
2090 ret
= process_channel_attr_node(channel_attr_node
,
2091 &channel
, &contexts_node
, &events_node
);
2097 ret
= lttng_enable_channel(handle
, &channel
);
2102 ret
= process_events_node(events_node
, handle
, channel
.name
);
2107 ret
= process_contexts_node(contexts_node
, handle
,
2114 lttng_destroy_handle(handle
);
2119 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2122 int ret
, started
= -1, snapshot_mode
= -1;
2123 uint64_t live_timer_interval
= UINT64_MAX
;
2125 xmlNodePtr domains_node
= NULL
;
2126 xmlNodePtr output_node
= NULL
;
2128 struct lttng_domain
*kernel_domain
= NULL
;
2129 struct lttng_domain
*ust_domain
= NULL
;
2130 struct lttng_domain
*jul_domain
= NULL
;
2132 for (node
= xmlFirstElementChild(session_node
); node
;
2133 node
= xmlNextElementSibling(node
)) {
2134 if (!name
&& !strcmp((const char *) node
->name
,
2135 config_element_name
)) {
2137 xmlChar
*node_content
= xmlNodeGetContent(node
);
2138 if (!node_content
) {
2139 ret
= -LTTNG_ERR_NOMEM
;
2143 name
= (char *) node_content
;
2144 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2145 config_element_domains
)) {
2147 domains_node
= node
;
2148 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2149 config_element_started
)) {
2151 xmlChar
*node_content
= xmlNodeGetContent(node
);
2152 if (!node_content
) {
2153 ret
= -LTTNG_ERR_NOMEM
;
2157 ret
= parse_bool(node_content
, &started
);
2160 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2163 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2164 config_element_output
)) {
2168 /* attributes, snapshot_mode or live_timer_interval */
2169 xmlNodePtr attributes_child
=
2170 xmlFirstElementChild(node
);
2172 if (!strcmp((const char *) attributes_child
->name
,
2173 config_element_snapshot_mode
)) {
2175 xmlChar
*snapshot_mode_content
=
2176 xmlNodeGetContent(attributes_child
);
2177 if (!snapshot_mode_content
) {
2178 ret
= -LTTNG_ERR_NOMEM
;
2182 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2183 free(snapshot_mode_content
);
2185 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2189 /* live_timer_interval */
2190 xmlChar
*timer_interval_content
=
2191 xmlNodeGetContent(attributes_child
);
2192 if (!timer_interval_content
) {
2193 ret
= -LTTNG_ERR_NOMEM
;
2197 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2198 free(timer_interval_content
);
2200 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2208 /* Mandatory attribute, as defined in the session XSD */
2209 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2213 if (session_name
&& strcmp(name
, session_name
)) {
2214 /* This is not the session we are looking for */
2215 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2219 /* Init domains to create the session handles */
2220 for (node
= xmlFirstElementChild(domains_node
); node
;
2221 node
= xmlNextElementSibling(node
)) {
2222 struct lttng_domain
*domain
;
2224 domain
= zmalloc(sizeof(*domain
));
2226 ret
= -LTTNG_ERR_NOMEM
;
2230 ret
= init_domain(node
, domain
);
2232 goto domain_init_error
;
2235 switch (domain
->type
) {
2236 case LTTNG_DOMAIN_KERNEL
:
2237 if (kernel_domain
) {
2238 /* Same domain seen twice, invalid! */
2239 goto domain_init_error
;
2241 kernel_domain
= domain
;
2243 case LTTNG_DOMAIN_UST
:
2245 /* Same domain seen twice, invalid! */
2246 goto domain_init_error
;
2248 ust_domain
= domain
;
2250 case LTTNG_DOMAIN_JUL
:
2252 /* Same domain seen twice, invalid! */
2253 goto domain_init_error
;
2255 jul_domain
= domain
;
2258 WARN("Invalid domain type");
2259 goto domain_init_error
;
2264 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2269 /* Destroy session if it exists */
2270 ret
= lttng_destroy_session(name
);
2271 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2272 ERR("Failed to destroy existing session.");
2277 /* Create session type depending on output type */
2278 if (snapshot_mode
&& snapshot_mode
!= -1) {
2279 ret
= create_snapshot_session(name
, output_node
);
2280 } else if (live_timer_interval
&&
2281 live_timer_interval
!= UINT64_MAX
) {
2282 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2283 output_node
, live_timer_interval
);
2285 /* regular session */
2286 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2287 output_node
, UINT64_MAX
);
2294 for (node
= xmlFirstElementChild(domains_node
); node
;
2295 node
= xmlNextElementSibling(node
)) {
2296 ret
= process_domain_node(node
, name
);
2303 ret
= lttng_start_tracing(name
);
2310 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2311 lttng_destroy_session(name
);
2314 free(kernel_domain
);
2322 int load_session_from_file(const char *path
, const char *session_name
,
2323 struct session_config_validation_ctx
*validation_ctx
, int override
)
2325 int ret
, session_found
= !session_name
;
2326 xmlDocPtr doc
= NULL
;
2327 xmlNodePtr sessions_node
;
2328 xmlNodePtr session_node
;
2332 assert(validation_ctx
);
2334 ret
= stat(path
, &sb
);
2336 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2340 doc
= xmlParseFile(path
);
2342 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2346 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2348 ERR("Session configuration file validation failed");
2349 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2353 sessions_node
= xmlDocGetRootElement(doc
);
2354 if (!sessions_node
) {
2358 for (session_node
= xmlFirstElementChild(sessions_node
);
2359 session_node
; session_node
=
2360 xmlNextElementSibling(session_node
)) {
2361 ret
= process_session_node(session_node
,
2362 session_name
, override
);
2363 if (session_name
&& ret
== 0) {
2364 /* Target session found and loaded */
2372 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2378 int load_session_from_path(const char *path
, const char *session_name
,
2379 struct session_config_validation_ctx
*validation_ctx
, int override
)
2381 int ret
, session_found
= !session_name
;
2382 DIR *directory
= NULL
;
2385 assert(validation_ctx
);
2387 directory
= opendir(path
);
2391 /* Try the file loading. */
2394 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2397 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2402 struct dirent
*entry
;
2403 struct dirent
*result
;
2404 char *file_path
= NULL
;
2405 size_t path_len
= strlen(path
);
2407 if (path_len
>= PATH_MAX
) {
2408 ret
= -LTTNG_ERR_INVALID
;
2412 entry
= zmalloc(sizeof(*entry
));
2414 ret
= -LTTNG_ERR_NOMEM
;
2418 file_path
= zmalloc(PATH_MAX
);
2420 ret
= -LTTNG_ERR_NOMEM
;
2425 strncpy(file_path
, path
, path_len
);
2426 if (file_path
[path_len
- 1] != '/') {
2427 file_path
[path_len
++] = '/';
2431 /* Search for *.lttng files */
2432 while (!readdir_r(directory
, entry
, &result
) && result
) {
2433 size_t file_name_len
= strlen(result
->d_name
);
2435 if (file_name_len
<=
2436 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2440 if (path_len
+ file_name_len
>= PATH_MAX
) {
2444 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2445 result
->d_name
+ file_name_len
- sizeof(
2446 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2450 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2451 file_path
[path_len
+ file_name_len
] = '\0';
2453 ret
= load_session_from_file(file_path
, session_name
,
2454 validation_ctx
, override
);
2455 if (session_name
&& !ret
) {
2464 ret
= load_session_from_file(path
, session_name
,
2465 validation_ctx
, override
);
2475 if (closedir(directory
)) {
2480 if (!session_found
) {
2481 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2488 int config_load_session(const char *path
, const char *session_name
,
2492 struct session_config_validation_ctx validation_ctx
= { 0 };
2494 ret
= init_session_config_validation_ctx(&validation_ctx
);
2501 char *home_path
= utils_get_home_dir();
2505 ret
= asprintf(&path
, DEFAULT_SESSION_HOME_CONFIGPATH
,
2511 ret
= load_session_from_path(path
, session_name
,
2512 &validation_ctx
, 0);
2513 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2514 /* Session found or an error occured */
2522 /* Try system session configuration path */
2523 ret
= load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH
,
2524 session_name
, &validation_ctx
, 0);
2525 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2526 /* Session found or an error occured */
2530 ret
= access(path
, F_OK
);
2535 ret
= -LTTNG_ERR_INVALID
;
2536 WARN("Session configuration path does not exist.");
2539 ret
= -LTTNG_ERR_EPERM
;
2542 ret
= -LTTNG_ERR_UNK
;
2548 ret
= load_session_from_path(path
, session_name
,
2549 &validation_ctx
, override
);
2552 fini_session_config_validation_ctx(&validation_ctx
);
2553 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2555 * Don't report an error if no sessions are found when called
2556 * without a session_name or a search path.