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
);
246 int config_parse_value(const char *value
)
249 char *endptr
, *lower_str
;
259 v
= strtoul(value
, &endptr
, 10);
260 if (endptr
!= value
) {
265 lower_str
= zmalloc(len
+ 1);
272 for (i
= 0; i
< len
; i
++) {
273 lower_str
[i
] = tolower(value
[i
]);
276 if (!strcmp(lower_str
, config_str_yes
) ||
277 !strcmp(lower_str
, config_str_true
) ||
278 !strcmp(lower_str
, config_str_on
)) {
280 } else if (!strcmp(lower_str
, config_str_no
) ||
281 !strcmp(lower_str
, config_str_false
) ||
282 !strcmp(lower_str
, config_str_off
)) {
294 * Returns a xmlChar string which must be released using xmlFree().
296 static xmlChar
*encode_string(const char *in_str
)
298 xmlChar
*out_str
= NULL
;
299 xmlCharEncodingHandlerPtr handler
;
300 int out_len
, ret
, in_len
;
304 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
306 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
310 in_len
= strlen(in_str
);
312 * Add 1 byte for the NULL terminted character. The factor 2 here is
313 * because UTF-8 can be on two bytes so this fits the worst case for each
316 out_len
= (in_len
* 2) + 1;
317 out_str
= xmlMalloc(out_len
);
322 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
329 /* out_len is now the size of out_str */
330 out_str
[out_len
] = '\0';
336 struct config_writer
*config_writer_create(int fd_output
)
339 struct config_writer
*writer
;
340 xmlOutputBufferPtr buffer
;
342 writer
= zmalloc(sizeof(struct config_writer
));
344 PERROR("zmalloc config_writer_create");
348 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
353 writer
->writer
= xmlNewTextWriter(buffer
);
354 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
355 config_xml_encoding
, NULL
);
360 ret
= xmlTextWriterSetIndentString(writer
->writer
,
361 BAD_CAST config_xml_indent_string
);
366 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
374 config_writer_destroy(writer
);
379 int config_writer_destroy(struct config_writer
*writer
)
388 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
389 WARN("Could not close XML document");
393 if (writer
->writer
) {
394 xmlFreeTextWriter(writer
->writer
);
403 int config_writer_open_element(struct config_writer
*writer
,
404 const char *element_name
)
407 xmlChar
*encoded_element_name
;
409 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
414 encoded_element_name
= encode_string(element_name
);
415 if (!encoded_element_name
) {
420 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
421 xmlFree(encoded_element_name
);
423 return ret
> 0 ? 0 : ret
;
427 int config_writer_close_element(struct config_writer
*writer
)
431 if (!writer
|| !writer
->writer
) {
436 ret
= xmlTextWriterEndElement(writer
->writer
);
438 return ret
> 0 ? 0 : ret
;
442 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
443 const char *element_name
, uint64_t value
)
446 xmlChar
*encoded_element_name
;
448 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
453 encoded_element_name
= encode_string(element_name
);
454 if (!encoded_element_name
) {
459 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
460 encoded_element_name
, "%" PRIu64
, value
);
461 xmlFree(encoded_element_name
);
463 return ret
> 0 ? 0 : ret
;
467 int config_writer_write_element_signed_int(struct config_writer
*writer
,
468 const char *element_name
, int64_t value
)
471 xmlChar
*encoded_element_name
;
473 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
478 encoded_element_name
= encode_string(element_name
);
479 if (!encoded_element_name
) {
484 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
485 encoded_element_name
, "%" PRIi64
, value
);
486 xmlFree(encoded_element_name
);
488 return ret
> 0 ? 0 : ret
;
492 int config_writer_write_element_bool(struct config_writer
*writer
,
493 const char *element_name
, int value
)
495 return config_writer_write_element_string(writer
, element_name
,
496 value
? config_xml_true
: config_xml_false
);
500 int config_writer_write_element_string(struct config_writer
*writer
,
501 const char *element_name
, const char *value
)
504 xmlChar
*encoded_element_name
= NULL
;
505 xmlChar
*encoded_value
= NULL
;
507 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
513 encoded_element_name
= encode_string(element_name
);
514 if (!encoded_element_name
) {
519 encoded_value
= encode_string(value
);
520 if (!encoded_value
) {
525 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
528 xmlFree(encoded_element_name
);
529 xmlFree(encoded_value
);
530 return ret
> 0 ? 0 : ret
;
534 void xml_error_handler(void *ctx
, const char *format
, ...)
540 va_start(args
, format
);
541 ret
= vasprintf(&errMsg
, format
, args
);
544 ERR("String allocation failed in xml error handler");
548 fprintf(stderr
, "XML Error: %s", errMsg
);
553 void fini_session_config_validation_ctx(
554 struct session_config_validation_ctx
*ctx
)
556 if (ctx
->parser_ctx
) {
557 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
561 xmlSchemaFree(ctx
->schema
);
564 if (ctx
->schema_validation_ctx
) {
565 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
568 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
572 char *get_session_config_xsd_path()
575 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
576 size_t base_path_len
;
580 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
583 base_path_len
= strlen(base_path
);
584 max_path_len
= base_path_len
+
585 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
586 xsd_path
= zmalloc(max_path_len
);
591 strncpy(xsd_path
, base_path
, max_path_len
);
592 if (xsd_path
[base_path_len
- 1] != '/') {
593 xsd_path
[base_path_len
++] = '/';
596 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
597 max_path_len
- base_path_len
);
603 int init_session_config_validation_ctx(
604 struct session_config_validation_ctx
*ctx
)
607 char *xsd_path
= get_session_config_xsd_path();
610 ret
= -LTTNG_ERR_NOMEM
;
614 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
615 if (!ctx
->parser_ctx
) {
616 ERR("XSD parser context creation failed");
617 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
620 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
621 xml_error_handler
, NULL
);
623 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
625 ERR("XSD parsing failed");
626 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
630 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
631 if (!ctx
->schema_validation_ctx
) {
632 ERR("XSD validation context creation failed");
633 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
637 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
638 xml_error_handler
, NULL
);
643 fini_session_config_validation_ctx(ctx
);
651 int parse_uint(xmlChar
*str
, uint64_t *val
)
661 *val
= strtoull((const char *) str
, &endptr
, 10);
662 if (!endptr
|| *endptr
) {
673 int parse_int(xmlChar
*str
, int64_t *val
)
683 *val
= strtoll((const char *) str
, &endptr
, 10);
684 if (!endptr
|| *endptr
) {
695 int parse_bool(xmlChar
*str
, int *val
)
704 if (!strcmp((const char *) str
, config_xml_true
)) {
706 } else if (!strcmp((const char *) str
, config_xml_false
)) {
709 WARN("Invalid boolean value encoutered (%s).",
718 int get_domain_type(xmlChar
*domain
)
726 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
727 ret
= LTTNG_DOMAIN_KERNEL
;
728 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
729 ret
= LTTNG_DOMAIN_UST
;
730 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
731 ret
= LTTNG_DOMAIN_JUL
;
742 int get_buffer_type(xmlChar
*buffer_type
)
750 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
751 ret
= LTTNG_BUFFER_GLOBAL
;
752 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
753 ret
= LTTNG_BUFFER_PER_UID
;
754 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
755 ret
= LTTNG_BUFFER_PER_PID
;
766 int get_overwrite_mode(xmlChar
*overwrite_mode
)
770 if (!overwrite_mode
) {
774 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
776 } else if (!strcmp((char *) overwrite_mode
,
777 config_overwrite_mode_discard
)) {
789 int get_output_type(xmlChar
*output_type
)
797 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
798 ret
= LTTNG_EVENT_MMAP
;
799 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
800 ret
= LTTNG_EVENT_SPLICE
;
811 int get_event_type(xmlChar
*event_type
)
819 if (!strcmp((char *) event_type
, config_event_type_all
)) {
820 ret
= LTTNG_EVENT_ALL
;
821 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
822 ret
= LTTNG_EVENT_TRACEPOINT
;
823 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
824 ret
= LTTNG_EVENT_PROBE
;
825 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
826 ret
= LTTNG_EVENT_FUNCTION
;
827 } else if (!strcmp((char *) event_type
,
828 config_event_type_function_entry
)) {
829 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
830 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
831 ret
= LTTNG_EVENT_NOOP
;
832 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
833 ret
= LTTNG_EVENT_SYSCALL
;
844 int get_loglevel_type(xmlChar
*loglevel_type
)
848 if (!loglevel_type
) {
852 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
853 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
854 } else if (!strcmp((char *) loglevel_type
,
855 config_loglevel_type_range
)) {
856 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
857 } else if (!strcmp((char *) loglevel_type
,
858 config_loglevel_type_single
)) {
859 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
870 * Return the context type or -1 on error.
873 int get_context_type(xmlChar
*context_type
)
881 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
882 ret
= LTTNG_EVENT_CONTEXT_PID
;
883 } else if (!strcmp((char *) context_type
,
884 config_event_context_procname
)) {
885 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
886 } else if (!strcmp((char *) context_type
,
887 config_event_context_prio
)) {
888 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
889 } else if (!strcmp((char *) context_type
,
890 config_event_context_nice
)) {
891 ret
= LTTNG_EVENT_CONTEXT_NICE
;
892 } else if (!strcmp((char *) context_type
,
893 config_event_context_vpid
)) {
894 ret
= LTTNG_EVENT_CONTEXT_VPID
;
895 } else if (!strcmp((char *) context_type
,
896 config_event_context_tid
)) {
897 ret
= LTTNG_EVENT_CONTEXT_TID
;
898 } else if (!strcmp((char *) context_type
,
899 config_event_context_vtid
)) {
900 ret
= LTTNG_EVENT_CONTEXT_VTID
;
901 } else if (!strcmp((char *) context_type
,
902 config_event_context_ppid
)) {
903 ret
= LTTNG_EVENT_CONTEXT_PPID
;
904 } else if (!strcmp((char *) context_type
,
905 config_event_context_vppid
)) {
906 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
907 } else if (!strcmp((char *) context_type
,
908 config_event_context_pthread_id
)) {
909 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
910 } else if (!strcmp((char *) context_type
,
911 config_event_context_hostname
)) {
912 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
913 } else if (!strcmp((char *) context_type
,
914 config_event_context_ip
)) {
915 ret
= LTTNG_EVENT_CONTEXT_IP
;
926 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
931 for (node
= xmlFirstElementChild(domain_node
); node
;
932 node
= xmlNextElementSibling(node
)) {
933 if (!strcmp((const char *) node
->name
, config_element_type
)) {
935 xmlChar
*node_content
= xmlNodeGetContent(node
);
937 ret
= -LTTNG_ERR_NOMEM
;
941 ret
= get_domain_type(node_content
);
944 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
949 } else if (!strcmp((const char *) node
->name
,
950 config_element_buffer_type
)) {
952 xmlChar
*node_content
= xmlNodeGetContent(node
);
954 ret
= -LTTNG_ERR_NOMEM
;
958 ret
= get_buffer_type(node_content
);
961 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
965 domain
->buf_type
= ret
;
974 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
979 for (node
= xmlFirstElementChild(net_output_node
); node
;
980 node
= xmlNextElementSibling(node
)) {
981 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
983 *control_uri
= (char *) xmlNodeGetContent(node
);
989 *data_uri
= (char *) xmlNodeGetContent(node
);
996 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1000 int process_consumer_output(xmlNodePtr consumer_output_node
,
1001 struct consumer_output
*output
)
1008 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1009 node
= xmlNextElementSibling(node
)) {
1010 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1011 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1015 ret
= -LTTNG_ERR_NOMEM
;
1019 ret
= parse_bool(enabled_str
, &output
->enabled
);
1025 xmlNodePtr output_type_node
;
1028 output_type_node
= xmlFirstElementChild(node
);
1029 if (!output_type_node
) {
1030 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1034 if (!strcmp((const char *) output_type_node
->name
,
1035 config_element_path
)) {
1037 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1038 if (!output
->path
) {
1039 ret
= -LTTNG_ERR_NOMEM
;
1044 ret
= get_net_output_uris(output_type_node
,
1045 &output
->control_uri
, &output
->data_uri
);
1057 free(output
->control_uri
);
1058 free(output
->data_uri
);
1059 memset(output
, 0, sizeof(struct consumer_output
));
1065 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1066 const char *control_uri
, const char *data_uri
)
1069 struct lttng_handle
*handle
;
1070 const char *uri
= NULL
;
1075 handle
= lttng_create_handle(name
, domain
);
1077 ret
= -LTTNG_ERR_NOMEM
;
1081 if (!control_uri
|| !data_uri
) {
1082 uri
= control_uri
? control_uri
: data_uri
;
1087 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1088 lttng_destroy_handle(handle
);
1094 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1097 xmlNodePtr node
= NULL
;
1098 xmlNodePtr snapshot_output_list_node
;
1099 xmlNodePtr snapshot_output_node
;
1101 assert(session_name
);
1103 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1112 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1114 /* Parse and create snapshot outputs */
1116 for (snapshot_output_node
=
1117 xmlFirstElementChild(snapshot_output_list_node
);
1118 snapshot_output_node
; snapshot_output_node
=
1119 xmlNextElementSibling(snapshot_output_node
)) {
1121 uint64_t max_size
= UINT64_MAX
;
1122 struct consumer_output output
= { 0 };
1123 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1125 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1126 node
= xmlNextElementSibling(node
)) {
1127 if (!strcmp((const char *) node
->name
,
1128 config_element_name
)) {
1130 name
= (char *) xmlNodeGetContent(node
);
1132 ret
= -LTTNG_ERR_NOMEM
;
1133 goto error_snapshot_output
;
1135 } else if (!strcmp((const char *) node
->name
,
1136 config_element_max_size
)) {
1137 xmlChar
*content
= xmlNodeGetContent(node
);
1141 ret
= -LTTNG_ERR_NOMEM
;
1142 goto error_snapshot_output
;
1144 ret
= parse_uint(content
, &max_size
);
1147 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1148 goto error_snapshot_output
;
1151 /* consumer_output */
1152 ret
= process_consumer_output(node
, &output
);
1154 goto error_snapshot_output
;
1159 snapshot_output
= lttng_snapshot_output_create();
1160 if (!snapshot_output
) {
1161 ret
= -LTTNG_ERR_NOMEM
;
1162 goto error_snapshot_output
;
1165 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1167 goto error_snapshot_output
;
1170 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1172 goto error_snapshot_output
;
1176 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1179 goto error_snapshot_output
;
1182 if (output
.control_uri
) {
1183 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1186 goto error_snapshot_output
;
1190 if (output
.data_uri
) {
1191 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1194 goto error_snapshot_output
;
1199 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1200 error_snapshot_output
:
1203 free(output
.control_uri
);
1204 free(output
.data_uri
);
1205 lttng_snapshot_output_destroy(snapshot_output
);
1215 int create_session(const char *name
,
1216 struct lttng_domain
*kernel_domain
,
1217 struct lttng_domain
*ust_domain
,
1218 struct lttng_domain
*jul_domain
,
1219 xmlNodePtr output_node
,
1220 uint64_t live_timer_interval
)
1223 struct consumer_output output
= { 0 };
1224 xmlNodePtr consumer_output_node
;
1229 consumer_output_node
= xmlFirstElementChild(output_node
);
1230 if (!consumer_output_node
) {
1231 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1235 if (strcmp((const char *) consumer_output_node
->name
,
1236 config_element_consumer_output
)) {
1237 WARN("Invalid output type, expected %s node",
1238 config_element_consumer_output
);
1239 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1243 ret
= process_consumer_output(consumer_output_node
, &output
);
1249 if (live_timer_interval
!= UINT64_MAX
&&
1250 !output
.control_uri
&& !output
.data_uri
) {
1251 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1255 if (output
.control_uri
|| output
.data_uri
) {
1257 struct lttng_domain
*domain
;
1258 struct lttng_domain
*domains
[] =
1259 { kernel_domain
, ust_domain
, jul_domain
};
1261 /* network destination */
1262 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1264 * URLs are provided for sure since the test above make sure that
1265 * with a live timer the data and control URIs are provided. So,
1266 * NULL is passed here and will be set right after.
1268 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1270 ret
= lttng_create_session(name
, NULL
);
1276 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1277 domain
= domains
[i
];
1282 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1289 /* either local output or no output */
1290 ret
= lttng_create_session(name
, output
.path
);
1297 free(output
.control_uri
);
1298 free(output
.data_uri
);
1302 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1303 struct lttng_event_probe_attr
*attr
)
1307 assert(probe_attribute_node
);
1310 if (!strcmp((const char *) probe_attribute_node
->name
,
1311 config_element_address
)) {
1316 content
= xmlNodeGetContent(probe_attribute_node
);
1318 ret
= -LTTNG_ERR_NOMEM
;
1322 ret
= parse_uint(content
, &addr
);
1325 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1330 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1331 config_element_offset
)) {
1333 uint64_t offset
= 0;
1336 content
= xmlNodeGetContent(probe_attribute_node
);
1338 ret
= -LTTNG_ERR_NOMEM
;
1342 ret
= parse_uint(content
, &offset
);
1345 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1349 attr
->offset
= offset
;
1350 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1351 config_element_symbol_name
)) {
1356 content
= xmlNodeGetContent(probe_attribute_node
);
1358 ret
= -LTTNG_ERR_NOMEM
;
1362 name_len
= strlen((char *) content
);
1363 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1364 WARN("symbol_name too long.");
1365 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1370 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1379 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1380 const char *channel_name
)
1384 struct lttng_event event
;
1385 char **exclusions
= NULL
;
1386 unsigned long exclusion_count
= 0;
1387 char *filter_expression
= NULL
;
1391 assert(channel_name
);
1393 memset(&event
, 0, sizeof(event
));
1395 for (node
= xmlFirstElementChild(event_node
); node
;
1396 node
= xmlNextElementSibling(node
)) {
1397 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1402 content
= xmlNodeGetContent(node
);
1404 ret
= -LTTNG_ERR_NOMEM
;
1408 name_len
= strlen((char *) content
);
1409 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1410 WARN("Channel name too long.");
1411 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1416 strncpy(event
.name
, (const char *) content
, name_len
);
1418 } else if (!strcmp((const char *) node
->name
,
1419 config_element_enabled
)) {
1420 xmlChar
*content
= xmlNodeGetContent(node
);
1424 ret
= -LTTNG_ERR_NOMEM
;
1428 ret
= parse_bool(content
, &event
.enabled
);
1431 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1434 } else if (!strcmp((const char *) node
->name
,
1435 config_element_type
)) {
1436 xmlChar
*content
= xmlNodeGetContent(node
);
1440 ret
= -LTTNG_ERR_NOMEM
;
1444 ret
= get_event_type(content
);
1447 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1452 } else if (!strcmp((const char *) node
->name
,
1453 config_element_loglevel_type
)) {
1454 xmlChar
*content
= xmlNodeGetContent(node
);
1458 ret
= -LTTNG_ERR_NOMEM
;
1462 ret
= get_loglevel_type(content
);
1465 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1469 event
.loglevel_type
= ret
;
1470 } else if (!strcmp((const char *) node
->name
,
1471 config_element_loglevel
)) {
1473 int64_t loglevel
= 0;
1476 content
= xmlNodeGetContent(node
);
1478 ret
= -LTTNG_ERR_NOMEM
;
1482 ret
= parse_int(content
, &loglevel
);
1485 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1489 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1490 WARN("loglevel out of range.");
1491 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1495 event
.loglevel
= loglevel
;
1496 } else if (!strcmp((const char *) node
->name
,
1497 config_element_filter
)) {
1499 xmlNodeGetContent(node
);
1503 ret
= -LTTNG_ERR_NOMEM
;
1507 filter_expression
= strdup((char *) content
);
1509 if (!filter_expression
) {
1510 ret
= -LTTNG_ERR_NOMEM
;
1513 } else if (!strcmp((const char *) node
->name
,
1514 config_element_exclusions
)) {
1515 xmlNodePtr exclusion_node
;
1516 int exclusion_index
= 0;
1521 * Exclusions has already been initialized,
1524 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1528 exclusion_count
= xmlChildElementCount(node
);
1529 if (!exclusion_count
) {
1533 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1535 exclusion_count
= 0;
1536 ret
= -LTTNG_ERR_NOMEM
;
1540 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1541 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1543 xmlNodeGetContent(exclusion_node
);
1546 ret
= -LTTNG_ERR_NOMEM
;
1550 exclusions
[exclusion_index
] = strdup((const char *) content
);
1552 if (!exclusions
[exclusion_index
]) {
1553 ret
= -LTTNG_ERR_NOMEM
;
1559 event
.exclusion
= 1;
1560 } else if (!strcmp((const char *) node
->name
,
1561 config_element_attributes
)) {
1562 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1565 if (!attribute_node
) {
1566 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1570 if (!strcmp((const char *) node
->name
,
1571 config_element_probe_attributes
)) {
1572 xmlNodePtr probe_attribute_node
;
1574 /* probe_attributes */
1575 for (probe_attribute_node
=
1576 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1577 probe_attribute_node
= xmlNextElementSibling(
1578 probe_attribute_node
)) {
1580 ret
= process_probe_attribute_node(probe_attribute_node
,
1589 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1591 /* function_attributes */
1592 content
= xmlNodeGetContent(symbol_node
);
1594 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1598 sym_len
= strlen((char *) content
);
1599 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1600 WARN("Function name too long.");
1601 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1606 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1613 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1614 filter_expression
, exclusion_count
, exclusions
);
1616 for (i
= 0; i
< exclusion_count
; i
++) {
1617 free(exclusions
[i
]);
1621 free(filter_expression
);
1626 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1627 const char *channel_name
)
1632 assert(events_node
);
1634 assert(channel_name
);
1636 for (node
= xmlFirstElementChild(events_node
); node
;
1637 node
= xmlNextElementSibling(node
)) {
1638 ret
= process_event_node(node
, handle
, channel_name
);
1648 int process_channel_attr_node(xmlNodePtr attr_node
,
1649 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1650 xmlNodePtr
*events_node
)
1656 assert(contexts_node
);
1657 assert(events_node
);
1659 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1664 content
= xmlNodeGetContent(attr_node
);
1666 ret
= -LTTNG_ERR_NOMEM
;
1670 name_len
= strlen((char *) content
);
1671 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1672 WARN("Channel name too long.");
1673 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1678 strncpy(channel
->name
, (const char *) content
, name_len
);
1680 } else if (!strcmp((const char *) attr_node
->name
,
1681 config_element_enabled
)) {
1686 content
= xmlNodeGetContent(attr_node
);
1688 ret
= -LTTNG_ERR_NOMEM
;
1692 ret
= parse_bool(content
, &enabled
);
1695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1699 channel
->enabled
= enabled
;
1700 } else if (!strcmp((const char *) attr_node
->name
,
1701 config_element_overwrite_mode
)) {
1704 /* overwrite_mode */
1705 content
= xmlNodeGetContent(attr_node
);
1707 ret
= -LTTNG_ERR_NOMEM
;
1711 ret
= get_overwrite_mode(content
);
1714 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1718 channel
->attr
.overwrite
= ret
;
1719 } else if (!strcmp((const char *) attr_node
->name
,
1720 config_element_subbuf_size
)) {
1723 /* subbuffer_size */
1724 content
= xmlNodeGetContent(attr_node
);
1726 ret
= -LTTNG_ERR_NOMEM
;
1730 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1733 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1736 } else if (!strcmp((const char *) attr_node
->name
,
1737 config_element_num_subbuf
)) {
1740 /* subbuffer_count */
1741 content
= xmlNodeGetContent(attr_node
);
1743 ret
= -LTTNG_ERR_NOMEM
;
1747 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1750 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1753 } else if (!strcmp((const char *) attr_node
->name
,
1754 config_element_switch_timer_interval
)) {
1756 uint64_t switch_timer_interval
= 0;
1758 /* switch_timer_interval */
1759 content
= xmlNodeGetContent(attr_node
);
1761 ret
= -LTTNG_ERR_NOMEM
;
1765 ret
= parse_uint(content
, &switch_timer_interval
);
1768 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1772 if (switch_timer_interval
> UINT_MAX
) {
1773 WARN("switch_timer_interval out of range.");
1774 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1778 channel
->attr
.switch_timer_interval
=
1779 switch_timer_interval
;
1780 } else if (!strcmp((const char *) attr_node
->name
,
1781 config_element_read_timer_interval
)) {
1783 uint64_t read_timer_interval
= 0;
1785 /* read_timer_interval */
1786 content
= xmlNodeGetContent(attr_node
);
1788 ret
= -LTTNG_ERR_NOMEM
;
1792 ret
= parse_uint(content
, &read_timer_interval
);
1795 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1799 if (read_timer_interval
> UINT_MAX
) {
1800 WARN("read_timer_interval out of range.");
1801 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1805 channel
->attr
.read_timer_interval
=
1806 read_timer_interval
;
1807 } else if (!strcmp((const char *) attr_node
->name
,
1808 config_element_output_type
)) {
1812 content
= xmlNodeGetContent(attr_node
);
1814 ret
= -LTTNG_ERR_NOMEM
;
1818 ret
= get_output_type(content
);
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 channel
->attr
.output
= ret
;
1826 } else if (!strcmp((const char *) attr_node
->name
,
1827 config_element_tracefile_size
)) {
1830 /* tracefile_size */
1831 content
= xmlNodeGetContent(attr_node
);
1833 ret
= -LTTNG_ERR_NOMEM
;
1837 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1840 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1843 } else if (!strcmp((const char *) attr_node
->name
,
1844 config_element_tracefile_count
)) {
1847 /* tracefile_count */
1848 content
= xmlNodeGetContent(attr_node
);
1850 ret
= -LTTNG_ERR_NOMEM
;
1854 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1857 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1860 } else if (!strcmp((const char *) attr_node
->name
,
1861 config_element_live_timer_interval
)) {
1863 uint64_t live_timer_interval
= 0;
1865 /* live_timer_interval */
1866 content
= xmlNodeGetContent(attr_node
);
1868 ret
= -LTTNG_ERR_NOMEM
;
1872 ret
= parse_uint(content
, &live_timer_interval
);
1875 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1879 if (live_timer_interval
> UINT_MAX
) {
1880 WARN("live_timer_interval out of range.");
1881 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1885 channel
->attr
.live_timer_interval
=
1886 live_timer_interval
;
1887 } else if (!strcmp((const char *) attr_node
->name
,
1888 config_element_events
)) {
1890 *events_node
= attr_node
;
1893 *contexts_node
= attr_node
;
1901 int process_context_node(xmlNodePtr context_node
,
1902 struct lttng_handle
*handle
, const char *channel_name
)
1905 struct lttng_event_context context
;
1906 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1909 assert(channel_name
);
1911 if (!context_child_node
) {
1912 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1916 memset(&context
, 0, sizeof(context
));
1918 if (!strcmp((const char *) context_child_node
->name
,
1919 config_element_type
)) {
1921 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1923 ret
= -LTTNG_ERR_NOMEM
;
1927 ret
= get_context_type(content
);
1930 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1936 xmlNodePtr perf_attr_node
;
1939 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1940 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1941 perf_attr_node
; perf_attr_node
=
1942 xmlNextElementSibling(perf_attr_node
)) {
1943 if (!strcmp((const char *) perf_attr_node
->name
,
1944 config_element_type
)) {
1949 content
= xmlNodeGetContent(perf_attr_node
);
1951 ret
= -LTTNG_ERR_NOMEM
;
1955 ret
= parse_uint(content
, &type
);
1958 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1962 if (type
> UINT32_MAX
) {
1963 WARN("perf context type out of range.");
1964 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1968 context
.u
.perf_counter
.type
= type
;
1969 } else if (!strcmp((const char *) perf_attr_node
->name
,
1970 config_element_config
)) {
1972 uint64_t config
= 0;
1975 content
= xmlNodeGetContent(perf_attr_node
);
1977 ret
= -LTTNG_ERR_NOMEM
;
1981 ret
= parse_uint(content
, &config
);
1984 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1988 context
.u
.perf_counter
.config
= config
;
1989 } else if (!strcmp((const char *) perf_attr_node
->name
,
1990 config_element_name
)) {
1995 content
= xmlNodeGetContent(perf_attr_node
);
1997 ret
= -LTTNG_ERR_NOMEM
;
2001 name_len
= strlen((char *) content
);
2002 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2003 WARN("perf context name too long.");
2004 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2009 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2016 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2022 int process_contexts_node(xmlNodePtr contexts_node
,
2023 struct lttng_handle
*handle
, const char *channel_name
)
2026 xmlNodePtr context_node
;
2028 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2029 context_node
= xmlNextElementSibling(context_node
)) {
2030 ret
= process_context_node(context_node
, handle
, channel_name
);
2040 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2043 struct lttng_domain domain
= { 0 };
2044 struct lttng_handle
*handle
= NULL
;
2045 xmlNodePtr channels_node
= NULL
;
2048 assert(session_name
);
2050 ret
= init_domain(domain_node
, &domain
);
2055 handle
= lttng_create_handle(session_name
, &domain
);
2057 ret
= -LTTNG_ERR_NOMEM
;
2061 /* get the channels node */
2062 for (node
= xmlFirstElementChild(domain_node
); node
;
2063 node
= xmlNextElementSibling(node
)) {
2064 if (!strcmp((const char *) node
->name
,
2065 config_element_channels
)) {
2066 channels_node
= node
;
2071 if (!channels_node
) {
2075 /* create all channels */
2076 for (node
= xmlFirstElementChild(channels_node
); node
;
2077 node
= xmlNextElementSibling(node
)) {
2078 struct lttng_channel channel
;
2079 xmlNodePtr contexts_node
= NULL
;
2080 xmlNodePtr events_node
= NULL
;
2081 xmlNodePtr channel_attr_node
;
2083 memset(&channel
, 0, sizeof(channel
));
2084 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2086 for (channel_attr_node
= xmlFirstElementChild(node
);
2087 channel_attr_node
; channel_attr_node
=
2088 xmlNextElementSibling(channel_attr_node
)) {
2089 ret
= process_channel_attr_node(channel_attr_node
,
2090 &channel
, &contexts_node
, &events_node
);
2096 ret
= lttng_enable_channel(handle
, &channel
);
2101 ret
= process_events_node(events_node
, handle
, channel
.name
);
2106 ret
= process_contexts_node(contexts_node
, handle
,
2113 lttng_destroy_handle(handle
);
2118 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2121 int ret
, started
= -1, snapshot_mode
= -1;
2122 uint64_t live_timer_interval
= UINT64_MAX
;
2124 xmlNodePtr domains_node
= NULL
;
2125 xmlNodePtr output_node
= NULL
;
2127 struct lttng_domain
*kernel_domain
= NULL
;
2128 struct lttng_domain
*ust_domain
= NULL
;
2129 struct lttng_domain
*jul_domain
= NULL
;
2131 for (node
= xmlFirstElementChild(session_node
); node
;
2132 node
= xmlNextElementSibling(node
)) {
2133 if (!name
&& !strcmp((const char *) node
->name
,
2134 config_element_name
)) {
2136 xmlChar
*node_content
= xmlNodeGetContent(node
);
2137 if (!node_content
) {
2138 ret
= -LTTNG_ERR_NOMEM
;
2142 name
= (char *) node_content
;
2143 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2144 config_element_domains
)) {
2146 domains_node
= node
;
2147 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2148 config_element_started
)) {
2150 xmlChar
*node_content
= xmlNodeGetContent(node
);
2151 if (!node_content
) {
2152 ret
= -LTTNG_ERR_NOMEM
;
2156 ret
= parse_bool(node_content
, &started
);
2159 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2162 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2163 config_element_output
)) {
2167 /* attributes, snapshot_mode or live_timer_interval */
2168 xmlNodePtr attributes_child
=
2169 xmlFirstElementChild(node
);
2171 if (!strcmp((const char *) attributes_child
->name
,
2172 config_element_snapshot_mode
)) {
2174 xmlChar
*snapshot_mode_content
=
2175 xmlNodeGetContent(attributes_child
);
2176 if (!snapshot_mode_content
) {
2177 ret
= -LTTNG_ERR_NOMEM
;
2181 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2182 free(snapshot_mode_content
);
2184 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2188 /* live_timer_interval */
2189 xmlChar
*timer_interval_content
=
2190 xmlNodeGetContent(attributes_child
);
2191 if (!timer_interval_content
) {
2192 ret
= -LTTNG_ERR_NOMEM
;
2196 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2197 free(timer_interval_content
);
2199 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2207 /* Mandatory attribute, as defined in the session XSD */
2208 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2212 if (session_name
&& strcmp(name
, session_name
)) {
2213 /* This is not the session we are looking for */
2214 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2218 /* Init domains to create the session handles */
2219 for (node
= xmlFirstElementChild(domains_node
); node
;
2220 node
= xmlNextElementSibling(node
)) {
2221 struct lttng_domain
*domain
;
2223 domain
= zmalloc(sizeof(*domain
));
2225 ret
= -LTTNG_ERR_NOMEM
;
2229 ret
= init_domain(node
, domain
);
2231 goto domain_init_error
;
2234 switch (domain
->type
) {
2235 case LTTNG_DOMAIN_KERNEL
:
2236 if (kernel_domain
) {
2237 /* Same domain seen twice, invalid! */
2238 goto domain_init_error
;
2240 kernel_domain
= domain
;
2242 case LTTNG_DOMAIN_UST
:
2244 /* Same domain seen twice, invalid! */
2245 goto domain_init_error
;
2247 ust_domain
= domain
;
2249 case LTTNG_DOMAIN_JUL
:
2251 /* Same domain seen twice, invalid! */
2252 goto domain_init_error
;
2254 jul_domain
= domain
;
2257 WARN("Invalid domain type");
2258 goto domain_init_error
;
2263 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2268 /* Destroy session if it exists */
2269 ret
= lttng_destroy_session(name
);
2270 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2271 ERR("Failed to destroy existing session.");
2276 /* Create session type depending on output type */
2277 if (snapshot_mode
&& snapshot_mode
!= -1) {
2278 ret
= create_snapshot_session(name
, output_node
);
2279 } else if (live_timer_interval
&&
2280 live_timer_interval
!= UINT64_MAX
) {
2281 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2282 output_node
, live_timer_interval
);
2284 /* regular session */
2285 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2286 output_node
, UINT64_MAX
);
2293 for (node
= xmlFirstElementChild(domains_node
); node
;
2294 node
= xmlNextElementSibling(node
)) {
2295 ret
= process_domain_node(node
, name
);
2302 ret
= lttng_start_tracing(name
);
2308 free(kernel_domain
);
2316 int load_session_from_file(const char *path
, const char *session_name
,
2317 struct session_config_validation_ctx
*validation_ctx
, int override
)
2319 int ret
, session_found
= !session_name
;
2320 xmlDocPtr doc
= NULL
;
2321 xmlNodePtr sessions_node
;
2322 xmlNodePtr session_node
;
2326 assert(validation_ctx
);
2328 ret
= stat(path
, &sb
);
2330 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2334 doc
= xmlParseFile(path
);
2336 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2340 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2342 ERR("Session configuration file validation failed");
2343 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2347 sessions_node
= xmlDocGetRootElement(doc
);
2348 if (!sessions_node
) {
2352 for (session_node
= xmlFirstElementChild(sessions_node
);
2353 session_node
; session_node
=
2354 xmlNextElementSibling(session_node
)) {
2355 ret
= process_session_node(session_node
,
2356 session_name
, override
);
2357 if (session_name
&& ret
== 0) {
2358 /* Target session found and loaded */
2366 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2372 int load_session_from_path(const char *path
, const char *session_name
,
2373 struct session_config_validation_ctx
*validation_ctx
, int override
)
2375 int ret
, session_found
= !session_name
;
2376 DIR *directory
= NULL
;
2379 assert(validation_ctx
);
2381 directory
= opendir(path
);
2385 /* Try the file loading. */
2388 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2391 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2396 struct dirent
*entry
;
2397 struct dirent
*result
;
2398 char *file_path
= NULL
;
2399 size_t path_len
= strlen(path
);
2401 if (path_len
>= PATH_MAX
) {
2402 ret
= -LTTNG_ERR_INVALID
;
2406 entry
= zmalloc(sizeof(*entry
));
2408 ret
= -LTTNG_ERR_NOMEM
;
2412 file_path
= zmalloc(PATH_MAX
);
2414 ret
= -LTTNG_ERR_NOMEM
;
2419 strncpy(file_path
, path
, path_len
);
2420 if (file_path
[path_len
- 1] != '/') {
2421 file_path
[path_len
++] = '/';
2425 /* Search for *.lttng files */
2426 while (!readdir_r(directory
, entry
, &result
) && result
) {
2427 size_t file_name_len
= strlen(result
->d_name
);
2429 if (file_name_len
<=
2430 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2434 if (path_len
+ file_name_len
> PATH_MAX
) {
2438 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2439 result
->d_name
+ file_name_len
- sizeof(
2440 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2444 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2445 file_path
[path_len
+ file_name_len
] = '\0';
2447 ret
= load_session_from_file(file_path
, session_name
,
2448 validation_ctx
, override
);
2449 if (session_name
&& !ret
) {
2458 ret
= load_session_from_file(path
, session_name
,
2459 validation_ctx
, override
);
2469 if (closedir(directory
)) {
2474 if (!session_found
) {
2475 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2482 int config_load_session(const char *path
, const char *session_name
,
2486 struct session_config_validation_ctx validation_ctx
= { 0 };
2488 ret
= init_session_config_validation_ctx(&validation_ctx
);
2495 char *home_path
= utils_get_home_dir();
2499 ret
= asprintf(&path
, DEFAULT_SESSION_HOME_CONFIGPATH
,
2505 ret
= load_session_from_path(path
, session_name
,
2506 &validation_ctx
, 0);
2507 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2508 /* Session found or an error occured */
2516 /* Try system session configuration path */
2517 ret
= load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH
,
2518 session_name
, &validation_ctx
, 0);
2519 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2520 /* Session found or an error occured */
2524 ret
= access(path
, F_OK
);
2529 ret
= -LTTNG_ERR_INVALID
;
2530 WARN("Session configuration path does not exist.");
2533 ret
= -LTTNG_ERR_EPERM
;
2536 ret
= -LTTNG_ERR_UNK
;
2542 ret
= load_session_from_path(path
, session_name
,
2543 &validation_ctx
, override
);
2546 fini_session_config_validation_ctx(&validation_ctx
);