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>
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <lttng/lttng-error.h>
36 #include <libxml/parser.h>
37 #include <libxml/valid.h>
38 #include <libxml/xmlschemas.h>
39 #include <libxml/tree.h>
40 #include <lttng/lttng.h>
41 #include <lttng/snapshot.h>
44 #include "config-internal.h"
46 struct handler_filter_args
{
48 config_entry_handler_cb handler
;
52 struct session_config_validation_ctx
{
53 xmlSchemaParserCtxtPtr parser_ctx
;
55 xmlSchemaValidCtxtPtr schema_validation_ctx
;
58 const char * const config_str_yes
= "yes";
59 const char * const config_str_true
= "true";
60 const char * const config_str_on
= "on";
61 const char * const config_str_no
= "no";
62 const char * const config_str_false
= "false";
63 const char * const config_str_off
= "off";
64 const char * const config_xml_encoding
= "UTF-8";
65 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
66 const char * const config_xml_indent_string
= "\t";
67 const char * const config_xml_true
= "true";
68 const char * const config_xml_false
= "false";
70 const char * const config_element_channel
= "channel";
71 const char * const config_element_channels
= "channels";
72 const char * const config_element_domain
= "domain";
73 const char * const config_element_domains
= "domains";
74 const char * const config_element_event
= "event";
75 const char * const config_element_events
= "events";
76 const char * const config_element_context
= "context";
77 const char * const config_element_contexts
= "contexts";
78 const char * const config_element_attributes
= "attributes";
79 const char * const config_element_exclusion
= "exclusion";
80 const char * const config_element_exclusions
= "exclusions";
81 const char * const config_element_function_attributes
= "function_attributes";
82 const char * const config_element_probe_attributes
= "probe_attributes";
83 const char * const config_element_symbol_name
= "symbol_name";
84 const char * const config_element_address
= "address";
85 const char * const config_element_offset
= "offset";
86 const char * const config_element_name
= "name";
87 const char * const config_element_enabled
= "enabled";
88 const char * const config_element_overwrite_mode
= "overwrite_mode";
89 const char * const config_element_subbuf_size
= "subbuffer_size";
90 const char * const config_element_num_subbuf
= "subbuffer_count";
91 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
92 const char * const config_element_read_timer_interval
= "read_timer_interval";
93 const char * const config_element_output
= "output";
94 const char * const config_element_output_type
= "output_type";
95 const char * const config_element_tracefile_size
= "tracefile_size";
96 const char * const config_element_tracefile_count
= "tracefile_count";
97 const char * const config_element_live_timer_interval
= "live_timer_interval";
98 const char * const config_element_type
= "type";
99 const char * const config_element_buffer_type
= "buffer_type";
100 const char * const config_element_session
= "session";
101 const char * const config_element_sessions
= "sessions";
102 const char * const config_element_perf
= "perf";
103 const char * const config_element_config
= "config";
104 const char * const config_element_started
= "started";
105 const char * const config_element_snapshot_mode
= "snapshot_mode";
106 const char * const config_element_loglevel
= "loglevel";
107 const char * const config_element_loglevel_type
= "loglevel_type";
108 const char * const config_element_filter
= "filter";
109 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
110 const char * const config_element_consumer_output
= "consumer_output";
111 const char * const config_element_destination
= "destination";
112 const char * const config_element_path
= "path";
113 const char * const config_element_net_output
= "net_output";
114 const char * const config_element_control_uri
= "control_uri";
115 const char * const config_element_data_uri
= "data_uri";
116 const char * const config_element_max_size
= "max_size";
118 const char * const config_domain_type_kernel
= "KERNEL";
119 const char * const config_domain_type_ust
= "UST";
120 const char * const config_domain_type_jul
= "JUL";
121 const char * const config_domain_type_log4j
= "LOG4J";
123 const char * const config_buffer_type_per_pid
= "PER_PID";
124 const char * const config_buffer_type_per_uid
= "PER_UID";
125 const char * const config_buffer_type_global
= "GLOBAL";
127 const char * const config_overwrite_mode_discard
= "DISCARD";
128 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
130 const char * const config_output_type_splice
= "SPLICE";
131 const char * const config_output_type_mmap
= "MMAP";
133 const char * const config_loglevel_type_all
= "ALL";
134 const char * const config_loglevel_type_range
= "RANGE";
135 const char * const config_loglevel_type_single
= "SINGLE";
137 const char * const config_event_type_all
= "ALL";
138 const char * const config_event_type_tracepoint
= "TRACEPOINT";
139 const char * const config_event_type_probe
= "PROBE";
140 const char * const config_event_type_function
= "FUNCTION";
141 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
142 const char * const config_event_type_noop
= "NOOP";
143 const char * const config_event_type_syscall
= "SYSCALL";
144 const char * const config_event_type_kprobe
= "KPROBE";
145 const char * const config_event_type_kretprobe
= "KRETPROBE";
147 const char * const config_event_context_pid
= "PID";
148 const char * const config_event_context_procname
= "PROCNAME";
149 const char * const config_event_context_prio
= "PRIO";
150 const char * const config_event_context_nice
= "NICE";
151 const char * const config_event_context_vpid
= "VPID";
152 const char * const config_event_context_tid
= "TID";
153 const char * const config_event_context_vtid
= "VTID";
154 const char * const config_event_context_ppid
= "PPID";
155 const char * const config_event_context_vppid
= "VPPID";
156 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
157 const char * const config_event_context_hostname
= "HOSTNAME";
158 const char * const config_event_context_ip
= "IP";
159 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
161 struct consumer_output
{
168 static int config_entry_handler_filter(struct handler_filter_args
*args
,
169 const char *section
, const char *name
, const char *value
)
172 struct config_entry entry
= { section
, name
, value
};
176 if (!section
|| !name
|| !value
) {
182 if (strcmp(args
->section
, section
)) {
187 ret
= args
->handler(&entry
, args
->user_data
);
193 int config_get_section_entries(const char *override_path
, const char *section
,
194 config_entry_handler_cb handler
, void *user_data
)
198 FILE *config_file
= NULL
;
199 struct handler_filter_args filter
= { section
, handler
, user_data
};
201 /* First, try system-wide conf. file. */
202 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
204 config_file
= fopen(path
, "r");
206 DBG("Loading daemon conf file at %s", path
);
208 * Return value is not very important here since error or not, we
209 * continue and try the next possible conf. file.
211 (void) ini_parse_file(config_file
,
212 (ini_entry_handler
) config_entry_handler_filter
,
217 /* Second is the user local configuration. */
218 path
= utils_get_home_dir();
220 char fullpath
[PATH_MAX
];
222 ret
= snprintf(fullpath
, sizeof(fullpath
),
223 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
225 PERROR("snprintf user conf. path");
229 config_file
= fopen(fullpath
, "r");
231 DBG("Loading daemon user conf file at %s", path
);
233 * Return value is not very important here since error or not, we
234 * continue and try the next possible conf. file.
236 (void) ini_parse_file(config_file
,
237 (ini_entry_handler
) config_entry_handler_filter
,
243 /* Final path is the one that the user might have provided. */
245 config_file
= fopen(override_path
, "r");
247 DBG("Loading daemon command line conf file at %s", override_path
);
248 (void) ini_parse_file(config_file
,
249 (ini_entry_handler
) config_entry_handler_filter
,
253 ERR("Failed to open daemon configuration file at %s",
260 /* Everything went well. */
268 int config_parse_value(const char *value
)
271 char *endptr
, *lower_str
;
281 v
= strtoul(value
, &endptr
, 10);
282 if (endptr
!= value
) {
287 lower_str
= zmalloc(len
+ 1);
294 for (i
= 0; i
< len
; i
++) {
295 lower_str
[i
] = tolower(value
[i
]);
298 if (!strcmp(lower_str
, config_str_yes
) ||
299 !strcmp(lower_str
, config_str_true
) ||
300 !strcmp(lower_str
, config_str_on
)) {
302 } else if (!strcmp(lower_str
, config_str_no
) ||
303 !strcmp(lower_str
, config_str_false
) ||
304 !strcmp(lower_str
, config_str_off
)) {
316 * Returns a xmlChar string which must be released using xmlFree().
318 static xmlChar
*encode_string(const char *in_str
)
320 xmlChar
*out_str
= NULL
;
321 xmlCharEncodingHandlerPtr handler
;
322 int out_len
, ret
, in_len
;
326 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
328 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
332 in_len
= strlen(in_str
);
334 * Add 1 byte for the NULL terminted character. The factor 4 here is
335 * used because UTF-8 characters can take up to 4 bytes.
337 out_len
= (in_len
* 4) + 1;
338 out_str
= xmlMalloc(out_len
);
343 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
350 /* out_len is now the size of out_str */
351 out_str
[out_len
] = '\0';
357 struct config_writer
*config_writer_create(int fd_output
, int indent
)
360 struct config_writer
*writer
;
361 xmlOutputBufferPtr buffer
;
363 writer
= zmalloc(sizeof(struct config_writer
));
365 PERROR("zmalloc config_writer_create");
369 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
374 writer
->writer
= xmlNewTextWriter(buffer
);
375 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
376 config_xml_encoding
, NULL
);
381 ret
= xmlTextWriterSetIndentString(writer
->writer
,
382 BAD_CAST config_xml_indent_string
);
387 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
395 config_writer_destroy(writer
);
400 int config_writer_destroy(struct config_writer
*writer
)
409 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
410 WARN("Could not close XML document");
414 if (writer
->writer
) {
415 xmlFreeTextWriter(writer
->writer
);
424 int config_writer_open_element(struct config_writer
*writer
,
425 const char *element_name
)
428 xmlChar
*encoded_element_name
;
430 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
435 encoded_element_name
= encode_string(element_name
);
436 if (!encoded_element_name
) {
441 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
442 xmlFree(encoded_element_name
);
444 return ret
> 0 ? 0 : ret
;
448 int config_writer_close_element(struct config_writer
*writer
)
452 if (!writer
|| !writer
->writer
) {
457 ret
= xmlTextWriterEndElement(writer
->writer
);
459 return ret
> 0 ? 0 : ret
;
463 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
464 const char *element_name
, uint64_t value
)
467 xmlChar
*encoded_element_name
;
469 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
474 encoded_element_name
= encode_string(element_name
);
475 if (!encoded_element_name
) {
480 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
481 encoded_element_name
, "%" PRIu64
, value
);
482 xmlFree(encoded_element_name
);
484 return ret
> 0 ? 0 : ret
;
488 int config_writer_write_element_signed_int(struct config_writer
*writer
,
489 const char *element_name
, int64_t value
)
492 xmlChar
*encoded_element_name
;
494 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
499 encoded_element_name
= encode_string(element_name
);
500 if (!encoded_element_name
) {
505 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
506 encoded_element_name
, "%" PRIi64
, value
);
507 xmlFree(encoded_element_name
);
509 return ret
> 0 ? 0 : ret
;
513 int config_writer_write_element_bool(struct config_writer
*writer
,
514 const char *element_name
, int value
)
516 return config_writer_write_element_string(writer
, element_name
,
517 value
? config_xml_true
: config_xml_false
);
521 int config_writer_write_element_string(struct config_writer
*writer
,
522 const char *element_name
, const char *value
)
525 xmlChar
*encoded_element_name
= NULL
;
526 xmlChar
*encoded_value
= NULL
;
528 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
534 encoded_element_name
= encode_string(element_name
);
535 if (!encoded_element_name
) {
540 encoded_value
= encode_string(value
);
541 if (!encoded_value
) {
546 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
549 xmlFree(encoded_element_name
);
550 xmlFree(encoded_value
);
551 return ret
> 0 ? 0 : ret
;
555 void xml_error_handler(void *ctx
, const char *format
, ...)
561 va_start(args
, format
);
562 ret
= vasprintf(&errMsg
, format
, args
);
565 ERR("String allocation failed in xml error handler");
569 fprintf(stderr
, "XML Error: %s", errMsg
);
574 void fini_session_config_validation_ctx(
575 struct session_config_validation_ctx
*ctx
)
577 if (ctx
->parser_ctx
) {
578 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
582 xmlSchemaFree(ctx
->schema
);
585 if (ctx
->schema_validation_ctx
) {
586 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
589 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
593 char *get_session_config_xsd_path()
596 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
597 size_t base_path_len
;
601 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
604 base_path_len
= strlen(base_path
);
605 max_path_len
= base_path_len
+
606 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
607 xsd_path
= zmalloc(max_path_len
);
612 strncpy(xsd_path
, base_path
, max_path_len
);
613 if (xsd_path
[base_path_len
- 1] != '/') {
614 xsd_path
[base_path_len
++] = '/';
617 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
618 max_path_len
- base_path_len
);
624 int init_session_config_validation_ctx(
625 struct session_config_validation_ctx
*ctx
)
628 char *xsd_path
= get_session_config_xsd_path();
631 ret
= -LTTNG_ERR_NOMEM
;
635 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
636 if (!ctx
->parser_ctx
) {
637 ERR("XSD parser context creation failed");
638 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
641 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
642 xml_error_handler
, NULL
);
644 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
646 ERR("XSD parsing failed");
647 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
651 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
652 if (!ctx
->schema_validation_ctx
) {
653 ERR("XSD validation context creation failed");
654 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
658 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
659 xml_error_handler
, NULL
);
664 fini_session_config_validation_ctx(ctx
);
672 int parse_uint(xmlChar
*str
, uint64_t *val
)
682 *val
= strtoull((const char *) str
, &endptr
, 10);
683 if (!endptr
|| *endptr
) {
694 int parse_int(xmlChar
*str
, int64_t *val
)
704 *val
= strtoll((const char *) str
, &endptr
, 10);
705 if (!endptr
|| *endptr
) {
716 int parse_bool(xmlChar
*str
, int *val
)
725 if (!strcmp((const char *) str
, config_xml_true
)) {
727 } else if (!strcmp((const char *) str
, config_xml_false
)) {
730 WARN("Invalid boolean value encoutered (%s).",
739 int get_domain_type(xmlChar
*domain
)
747 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
748 ret
= LTTNG_DOMAIN_KERNEL
;
749 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
750 ret
= LTTNG_DOMAIN_UST
;
751 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
752 ret
= LTTNG_DOMAIN_JUL
;
753 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
754 ret
= LTTNG_DOMAIN_LOG4J
;
765 int get_buffer_type(xmlChar
*buffer_type
)
773 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
774 ret
= LTTNG_BUFFER_GLOBAL
;
775 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
776 ret
= LTTNG_BUFFER_PER_UID
;
777 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
778 ret
= LTTNG_BUFFER_PER_PID
;
789 int get_overwrite_mode(xmlChar
*overwrite_mode
)
793 if (!overwrite_mode
) {
797 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
799 } else if (!strcmp((char *) overwrite_mode
,
800 config_overwrite_mode_discard
)) {
812 int get_output_type(xmlChar
*output_type
)
820 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
821 ret
= LTTNG_EVENT_MMAP
;
822 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
823 ret
= LTTNG_EVENT_SPLICE
;
834 int get_event_type(xmlChar
*event_type
)
842 if (!strcmp((char *) event_type
, config_event_type_all
)) {
843 ret
= LTTNG_EVENT_ALL
;
844 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
845 ret
= LTTNG_EVENT_TRACEPOINT
;
846 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
847 ret
= LTTNG_EVENT_PROBE
;
848 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
849 ret
= LTTNG_EVENT_FUNCTION
;
850 } else if (!strcmp((char *) event_type
,
851 config_event_type_function_entry
)) {
852 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
853 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
854 ret
= LTTNG_EVENT_NOOP
;
855 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
856 ret
= LTTNG_EVENT_SYSCALL
;
867 int get_loglevel_type(xmlChar
*loglevel_type
)
871 if (!loglevel_type
) {
875 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
876 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
877 } else if (!strcmp((char *) loglevel_type
,
878 config_loglevel_type_range
)) {
879 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
880 } else if (!strcmp((char *) loglevel_type
,
881 config_loglevel_type_single
)) {
882 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
893 * Return the context type or -1 on error.
896 int get_context_type(xmlChar
*context_type
)
904 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
905 ret
= LTTNG_EVENT_CONTEXT_PID
;
906 } else if (!strcmp((char *) context_type
,
907 config_event_context_procname
)) {
908 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
909 } else if (!strcmp((char *) context_type
,
910 config_event_context_prio
)) {
911 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
912 } else if (!strcmp((char *) context_type
,
913 config_event_context_nice
)) {
914 ret
= LTTNG_EVENT_CONTEXT_NICE
;
915 } else if (!strcmp((char *) context_type
,
916 config_event_context_vpid
)) {
917 ret
= LTTNG_EVENT_CONTEXT_VPID
;
918 } else if (!strcmp((char *) context_type
,
919 config_event_context_tid
)) {
920 ret
= LTTNG_EVENT_CONTEXT_TID
;
921 } else if (!strcmp((char *) context_type
,
922 config_event_context_vtid
)) {
923 ret
= LTTNG_EVENT_CONTEXT_VTID
;
924 } else if (!strcmp((char *) context_type
,
925 config_event_context_ppid
)) {
926 ret
= LTTNG_EVENT_CONTEXT_PPID
;
927 } else if (!strcmp((char *) context_type
,
928 config_event_context_vppid
)) {
929 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
930 } else if (!strcmp((char *) context_type
,
931 config_event_context_pthread_id
)) {
932 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
933 } else if (!strcmp((char *) context_type
,
934 config_event_context_hostname
)) {
935 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
936 } else if (!strcmp((char *) context_type
,
937 config_event_context_ip
)) {
938 ret
= LTTNG_EVENT_CONTEXT_IP
;
949 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
954 for (node
= xmlFirstElementChild(domain_node
); node
;
955 node
= xmlNextElementSibling(node
)) {
956 if (!strcmp((const char *) node
->name
, config_element_type
)) {
958 xmlChar
*node_content
= xmlNodeGetContent(node
);
960 ret
= -LTTNG_ERR_NOMEM
;
964 ret
= get_domain_type(node_content
);
967 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
972 } else if (!strcmp((const char *) node
->name
,
973 config_element_buffer_type
)) {
975 xmlChar
*node_content
= xmlNodeGetContent(node
);
977 ret
= -LTTNG_ERR_NOMEM
;
981 ret
= get_buffer_type(node_content
);
984 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
988 domain
->buf_type
= ret
;
997 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1002 for (node
= xmlFirstElementChild(net_output_node
); node
;
1003 node
= xmlNextElementSibling(node
)) {
1004 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1006 *control_uri
= (char *) xmlNodeGetContent(node
);
1007 if (!*control_uri
) {
1012 *data_uri
= (char *) xmlNodeGetContent(node
);
1019 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1023 int process_consumer_output(xmlNodePtr consumer_output_node
,
1024 struct consumer_output
*output
)
1031 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1032 node
= xmlNextElementSibling(node
)) {
1033 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1034 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1038 ret
= -LTTNG_ERR_NOMEM
;
1042 ret
= parse_bool(enabled_str
, &output
->enabled
);
1048 xmlNodePtr output_type_node
;
1051 output_type_node
= xmlFirstElementChild(node
);
1052 if (!output_type_node
) {
1053 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1057 if (!strcmp((const char *) output_type_node
->name
,
1058 config_element_path
)) {
1060 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1061 if (!output
->path
) {
1062 ret
= -LTTNG_ERR_NOMEM
;
1067 ret
= get_net_output_uris(output_type_node
,
1068 &output
->control_uri
, &output
->data_uri
);
1080 free(output
->control_uri
);
1081 free(output
->data_uri
);
1082 memset(output
, 0, sizeof(struct consumer_output
));
1088 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1089 const char *control_uri
, const char *data_uri
)
1092 struct lttng_handle
*handle
;
1093 const char *uri
= NULL
;
1098 handle
= lttng_create_handle(name
, domain
);
1100 ret
= -LTTNG_ERR_NOMEM
;
1104 if (!control_uri
|| !data_uri
) {
1105 uri
= control_uri
? control_uri
: data_uri
;
1110 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1111 lttng_destroy_handle(handle
);
1117 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1120 xmlNodePtr node
= NULL
;
1121 xmlNodePtr snapshot_output_list_node
;
1122 xmlNodePtr snapshot_output_node
;
1124 assert(session_name
);
1126 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1135 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1137 /* Parse and create snapshot outputs */
1139 for (snapshot_output_node
=
1140 xmlFirstElementChild(snapshot_output_list_node
);
1141 snapshot_output_node
; snapshot_output_node
=
1142 xmlNextElementSibling(snapshot_output_node
)) {
1144 uint64_t max_size
= UINT64_MAX
;
1145 struct consumer_output output
= { 0 };
1146 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1148 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1149 node
= xmlNextElementSibling(node
)) {
1150 if (!strcmp((const char *) node
->name
,
1151 config_element_name
)) {
1153 name
= (char *) xmlNodeGetContent(node
);
1155 ret
= -LTTNG_ERR_NOMEM
;
1156 goto error_snapshot_output
;
1158 } else if (!strcmp((const char *) node
->name
,
1159 config_element_max_size
)) {
1160 xmlChar
*content
= xmlNodeGetContent(node
);
1164 ret
= -LTTNG_ERR_NOMEM
;
1165 goto error_snapshot_output
;
1167 ret
= parse_uint(content
, &max_size
);
1170 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1171 goto error_snapshot_output
;
1174 /* consumer_output */
1175 ret
= process_consumer_output(node
, &output
);
1177 goto error_snapshot_output
;
1182 snapshot_output
= lttng_snapshot_output_create();
1183 if (!snapshot_output
) {
1184 ret
= -LTTNG_ERR_NOMEM
;
1185 goto error_snapshot_output
;
1188 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1190 goto error_snapshot_output
;
1193 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1195 goto error_snapshot_output
;
1199 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1202 goto error_snapshot_output
;
1205 if (output
.control_uri
) {
1206 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1209 goto error_snapshot_output
;
1213 if (output
.data_uri
) {
1214 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1217 goto error_snapshot_output
;
1222 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1223 error_snapshot_output
:
1226 free(output
.control_uri
);
1227 free(output
.data_uri
);
1228 lttng_snapshot_output_destroy(snapshot_output
);
1238 int create_session(const char *name
,
1239 struct lttng_domain
*kernel_domain
,
1240 struct lttng_domain
*ust_domain
,
1241 struct lttng_domain
*jul_domain
,
1242 struct lttng_domain
*log4j_domain
,
1243 xmlNodePtr output_node
,
1244 uint64_t live_timer_interval
)
1247 struct consumer_output output
= { 0 };
1248 xmlNodePtr consumer_output_node
;
1253 consumer_output_node
= xmlFirstElementChild(output_node
);
1254 if (!consumer_output_node
) {
1255 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1259 if (strcmp((const char *) consumer_output_node
->name
,
1260 config_element_consumer_output
)) {
1261 WARN("Invalid output type, expected %s node",
1262 config_element_consumer_output
);
1263 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1267 ret
= process_consumer_output(consumer_output_node
, &output
);
1273 if (live_timer_interval
!= UINT64_MAX
&&
1274 !output
.control_uri
&& !output
.data_uri
) {
1275 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1279 if (output
.control_uri
|| output
.data_uri
) {
1281 struct lttng_domain
*domain
;
1282 struct lttng_domain
*domains
[] =
1283 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1285 /* network destination */
1286 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1288 * URLs are provided for sure since the test above make sure that
1289 * with a live timer the data and control URIs are provided. So,
1290 * NULL is passed here and will be set right after.
1292 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1294 ret
= lttng_create_session(name
, NULL
);
1300 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1301 domain
= domains
[i
];
1306 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1313 /* either local output or no output */
1314 ret
= lttng_create_session(name
, output
.path
);
1321 free(output
.control_uri
);
1322 free(output
.data_uri
);
1326 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1327 struct lttng_event_probe_attr
*attr
)
1331 assert(probe_attribute_node
);
1334 if (!strcmp((const char *) probe_attribute_node
->name
,
1335 config_element_address
)) {
1340 content
= xmlNodeGetContent(probe_attribute_node
);
1342 ret
= -LTTNG_ERR_NOMEM
;
1346 ret
= parse_uint(content
, &addr
);
1349 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1354 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1355 config_element_offset
)) {
1357 uint64_t offset
= 0;
1360 content
= xmlNodeGetContent(probe_attribute_node
);
1362 ret
= -LTTNG_ERR_NOMEM
;
1366 ret
= parse_uint(content
, &offset
);
1369 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1373 attr
->offset
= offset
;
1374 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1375 config_element_symbol_name
)) {
1380 content
= xmlNodeGetContent(probe_attribute_node
);
1382 ret
= -LTTNG_ERR_NOMEM
;
1386 name_len
= strlen((char *) content
);
1387 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1388 WARN("symbol_name too long.");
1389 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1394 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1403 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1404 const char *channel_name
)
1408 struct lttng_event event
;
1409 char **exclusions
= NULL
;
1410 unsigned long exclusion_count
= 0;
1411 char *filter_expression
= NULL
;
1415 assert(channel_name
);
1417 memset(&event
, 0, sizeof(event
));
1419 /* Initialize default log level which varies by domain */
1420 switch (handle
->domain
.type
)
1422 case LTTNG_DOMAIN_JUL
:
1423 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1425 case LTTNG_DOMAIN_LOG4J
:
1426 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1428 case LTTNG_DOMAIN_UST
:
1429 case LTTNG_DOMAIN_KERNEL
:
1430 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1436 for (node
= xmlFirstElementChild(event_node
); node
;
1437 node
= xmlNextElementSibling(node
)) {
1438 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1443 content
= xmlNodeGetContent(node
);
1445 ret
= -LTTNG_ERR_NOMEM
;
1449 name_len
= strlen((char *) content
);
1450 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1451 WARN("Channel name too long.");
1452 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1457 strncpy(event
.name
, (const char *) content
, name_len
);
1459 } else if (!strcmp((const char *) node
->name
,
1460 config_element_enabled
)) {
1461 xmlChar
*content
= xmlNodeGetContent(node
);
1465 ret
= -LTTNG_ERR_NOMEM
;
1469 ret
= parse_bool(content
, &event
.enabled
);
1472 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1475 } else if (!strcmp((const char *) node
->name
,
1476 config_element_type
)) {
1477 xmlChar
*content
= xmlNodeGetContent(node
);
1481 ret
= -LTTNG_ERR_NOMEM
;
1485 ret
= get_event_type(content
);
1488 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1493 } else if (!strcmp((const char *) node
->name
,
1494 config_element_loglevel_type
)) {
1495 xmlChar
*content
= xmlNodeGetContent(node
);
1499 ret
= -LTTNG_ERR_NOMEM
;
1503 ret
= get_loglevel_type(content
);
1506 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1510 event
.loglevel_type
= ret
;
1511 } else if (!strcmp((const char *) node
->name
,
1512 config_element_loglevel
)) {
1514 int64_t loglevel
= 0;
1517 content
= xmlNodeGetContent(node
);
1519 ret
= -LTTNG_ERR_NOMEM
;
1523 ret
= parse_int(content
, &loglevel
);
1526 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1530 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1531 WARN("loglevel out of range.");
1532 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1536 event
.loglevel
= loglevel
;
1537 } else if (!strcmp((const char *) node
->name
,
1538 config_element_filter
)) {
1540 xmlNodeGetContent(node
);
1544 ret
= -LTTNG_ERR_NOMEM
;
1548 free(filter_expression
);
1549 filter_expression
= strdup((char *) content
);
1551 if (!filter_expression
) {
1552 ret
= -LTTNG_ERR_NOMEM
;
1555 } else if (!strcmp((const char *) node
->name
,
1556 config_element_exclusions
)) {
1557 xmlNodePtr exclusion_node
;
1558 int exclusion_index
= 0;
1563 * Exclusions has already been initialized,
1566 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1570 exclusion_count
= xmlChildElementCount(node
);
1571 if (!exclusion_count
) {
1575 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1577 exclusion_count
= 0;
1578 ret
= -LTTNG_ERR_NOMEM
;
1582 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1583 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1585 xmlNodeGetContent(exclusion_node
);
1588 ret
= -LTTNG_ERR_NOMEM
;
1592 exclusions
[exclusion_index
] = strdup((const char *) content
);
1594 if (!exclusions
[exclusion_index
]) {
1595 ret
= -LTTNG_ERR_NOMEM
;
1601 event
.exclusion
= 1;
1602 } else if (!strcmp((const char *) node
->name
,
1603 config_element_attributes
)) {
1604 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1607 if (!attribute_node
) {
1608 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1612 if (!strcmp((const char *) node
->name
,
1613 config_element_probe_attributes
)) {
1614 xmlNodePtr probe_attribute_node
;
1616 /* probe_attributes */
1617 for (probe_attribute_node
=
1618 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1619 probe_attribute_node
= xmlNextElementSibling(
1620 probe_attribute_node
)) {
1622 ret
= process_probe_attribute_node(probe_attribute_node
,
1631 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1633 /* function_attributes */
1634 content
= xmlNodeGetContent(symbol_node
);
1636 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1640 sym_len
= strlen((char *) content
);
1641 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1642 WARN("Function name too long.");
1643 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1648 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1655 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1656 filter_expression
, exclusion_count
, exclusions
);
1661 if (!event
.enabled
) {
1663 * Note that we should use lttng_disable_event_ext() (2.6+) to
1664 * eliminate the risk of clashing on events of the same
1665 * name (with different event types and loglevels).
1667 * Unfortunately, lttng_disable_event_ext() only performs a
1668 * match on the name and event type and errors out if any other
1669 * event attribute is not set to its default value.
1671 * This will disable all events that match this name.
1673 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1676 for (i
= 0; i
< exclusion_count
; i
++) {
1677 free(exclusions
[i
]);
1681 free(filter_expression
);
1686 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1687 const char *channel_name
)
1692 assert(events_node
);
1694 assert(channel_name
);
1696 for (node
= xmlFirstElementChild(events_node
); node
;
1697 node
= xmlNextElementSibling(node
)) {
1698 ret
= process_event_node(node
, handle
, channel_name
);
1708 int process_channel_attr_node(xmlNodePtr attr_node
,
1709 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1710 xmlNodePtr
*events_node
)
1716 assert(contexts_node
);
1717 assert(events_node
);
1719 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1724 content
= xmlNodeGetContent(attr_node
);
1726 ret
= -LTTNG_ERR_NOMEM
;
1730 name_len
= strlen((char *) content
);
1731 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1732 WARN("Channel name too long.");
1733 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1738 strncpy(channel
->name
, (const char *) content
, name_len
);
1740 } else if (!strcmp((const char *) attr_node
->name
,
1741 config_element_enabled
)) {
1746 content
= xmlNodeGetContent(attr_node
);
1748 ret
= -LTTNG_ERR_NOMEM
;
1752 ret
= parse_bool(content
, &enabled
);
1755 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1759 channel
->enabled
= enabled
;
1760 } else if (!strcmp((const char *) attr_node
->name
,
1761 config_element_overwrite_mode
)) {
1764 /* overwrite_mode */
1765 content
= xmlNodeGetContent(attr_node
);
1767 ret
= -LTTNG_ERR_NOMEM
;
1771 ret
= get_overwrite_mode(content
);
1774 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1778 channel
->attr
.overwrite
= ret
;
1779 } else if (!strcmp((const char *) attr_node
->name
,
1780 config_element_subbuf_size
)) {
1783 /* subbuffer_size */
1784 content
= xmlNodeGetContent(attr_node
);
1786 ret
= -LTTNG_ERR_NOMEM
;
1790 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1793 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1796 } else if (!strcmp((const char *) attr_node
->name
,
1797 config_element_num_subbuf
)) {
1800 /* subbuffer_count */
1801 content
= xmlNodeGetContent(attr_node
);
1803 ret
= -LTTNG_ERR_NOMEM
;
1807 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1810 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1813 } else if (!strcmp((const char *) attr_node
->name
,
1814 config_element_switch_timer_interval
)) {
1816 uint64_t switch_timer_interval
= 0;
1818 /* switch_timer_interval */
1819 content
= xmlNodeGetContent(attr_node
);
1821 ret
= -LTTNG_ERR_NOMEM
;
1825 ret
= parse_uint(content
, &switch_timer_interval
);
1828 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1832 if (switch_timer_interval
> UINT_MAX
) {
1833 WARN("switch_timer_interval out of range.");
1834 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1838 channel
->attr
.switch_timer_interval
=
1839 switch_timer_interval
;
1840 } else if (!strcmp((const char *) attr_node
->name
,
1841 config_element_read_timer_interval
)) {
1843 uint64_t read_timer_interval
= 0;
1845 /* read_timer_interval */
1846 content
= xmlNodeGetContent(attr_node
);
1848 ret
= -LTTNG_ERR_NOMEM
;
1852 ret
= parse_uint(content
, &read_timer_interval
);
1855 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1859 if (read_timer_interval
> UINT_MAX
) {
1860 WARN("read_timer_interval out of range.");
1861 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1865 channel
->attr
.read_timer_interval
=
1866 read_timer_interval
;
1867 } else if (!strcmp((const char *) attr_node
->name
,
1868 config_element_output_type
)) {
1872 content
= xmlNodeGetContent(attr_node
);
1874 ret
= -LTTNG_ERR_NOMEM
;
1878 ret
= get_output_type(content
);
1881 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1885 channel
->attr
.output
= ret
;
1886 } else if (!strcmp((const char *) attr_node
->name
,
1887 config_element_tracefile_size
)) {
1890 /* tracefile_size */
1891 content
= xmlNodeGetContent(attr_node
);
1893 ret
= -LTTNG_ERR_NOMEM
;
1897 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1900 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1903 } else if (!strcmp((const char *) attr_node
->name
,
1904 config_element_tracefile_count
)) {
1907 /* tracefile_count */
1908 content
= xmlNodeGetContent(attr_node
);
1910 ret
= -LTTNG_ERR_NOMEM
;
1914 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1917 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1920 } else if (!strcmp((const char *) attr_node
->name
,
1921 config_element_live_timer_interval
)) {
1923 uint64_t live_timer_interval
= 0;
1925 /* live_timer_interval */
1926 content
= xmlNodeGetContent(attr_node
);
1928 ret
= -LTTNG_ERR_NOMEM
;
1932 ret
= parse_uint(content
, &live_timer_interval
);
1935 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1939 if (live_timer_interval
> UINT_MAX
) {
1940 WARN("live_timer_interval out of range.");
1941 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1945 channel
->attr
.live_timer_interval
=
1946 live_timer_interval
;
1947 } else if (!strcmp((const char *) attr_node
->name
,
1948 config_element_events
)) {
1950 *events_node
= attr_node
;
1953 *contexts_node
= attr_node
;
1961 int process_context_node(xmlNodePtr context_node
,
1962 struct lttng_handle
*handle
, const char *channel_name
)
1965 struct lttng_event_context context
;
1966 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1969 assert(channel_name
);
1971 if (!context_child_node
) {
1972 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1976 memset(&context
, 0, sizeof(context
));
1978 if (!strcmp((const char *) context_child_node
->name
,
1979 config_element_type
)) {
1981 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1983 ret
= -LTTNG_ERR_NOMEM
;
1987 ret
= get_context_type(content
);
1990 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1996 xmlNodePtr perf_attr_node
;
1999 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2000 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2001 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2002 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2003 perf_attr_node
; perf_attr_node
=
2004 xmlNextElementSibling(perf_attr_node
)) {
2005 if (!strcmp((const char *) perf_attr_node
->name
,
2006 config_element_type
)) {
2011 content
= xmlNodeGetContent(perf_attr_node
);
2013 ret
= -LTTNG_ERR_NOMEM
;
2017 ret
= parse_uint(content
, &type
);
2020 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2024 if (type
> UINT32_MAX
) {
2025 WARN("perf context type out of range.");
2026 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2030 context
.u
.perf_counter
.type
= type
;
2031 } else if (!strcmp((const char *) perf_attr_node
->name
,
2032 config_element_config
)) {
2034 uint64_t config
= 0;
2037 content
= xmlNodeGetContent(perf_attr_node
);
2039 ret
= -LTTNG_ERR_NOMEM
;
2043 ret
= parse_uint(content
, &config
);
2046 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2050 context
.u
.perf_counter
.config
= config
;
2051 } else if (!strcmp((const char *) perf_attr_node
->name
,
2052 config_element_name
)) {
2057 content
= xmlNodeGetContent(perf_attr_node
);
2059 ret
= -LTTNG_ERR_NOMEM
;
2063 name_len
= strlen((char *) content
);
2064 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2065 WARN("perf context name too long.");
2066 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2071 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2078 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2084 int process_contexts_node(xmlNodePtr contexts_node
,
2085 struct lttng_handle
*handle
, const char *channel_name
)
2088 xmlNodePtr context_node
;
2090 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2091 context_node
= xmlNextElementSibling(context_node
)) {
2092 ret
= process_context_node(context_node
, handle
, channel_name
);
2102 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2105 struct lttng_domain domain
= { 0 };
2106 struct lttng_handle
*handle
= NULL
;
2107 xmlNodePtr channels_node
= NULL
;
2110 assert(session_name
);
2112 ret
= init_domain(domain_node
, &domain
);
2117 handle
= lttng_create_handle(session_name
, &domain
);
2119 ret
= -LTTNG_ERR_NOMEM
;
2123 /* get the channels node */
2124 for (node
= xmlFirstElementChild(domain_node
); node
;
2125 node
= xmlNextElementSibling(node
)) {
2126 if (!strcmp((const char *) node
->name
,
2127 config_element_channels
)) {
2128 channels_node
= node
;
2133 if (!channels_node
) {
2137 /* create all channels */
2138 for (node
= xmlFirstElementChild(channels_node
); node
;
2139 node
= xmlNextElementSibling(node
)) {
2140 struct lttng_channel channel
;
2141 xmlNodePtr contexts_node
= NULL
;
2142 xmlNodePtr events_node
= NULL
;
2143 xmlNodePtr channel_attr_node
;
2145 memset(&channel
, 0, sizeof(channel
));
2146 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2148 for (channel_attr_node
= xmlFirstElementChild(node
);
2149 channel_attr_node
; channel_attr_node
=
2150 xmlNextElementSibling(channel_attr_node
)) {
2151 ret
= process_channel_attr_node(channel_attr_node
,
2152 &channel
, &contexts_node
, &events_node
);
2158 ret
= lttng_enable_channel(handle
, &channel
);
2163 ret
= process_events_node(events_node
, handle
, channel
.name
);
2168 ret
= process_contexts_node(contexts_node
, handle
,
2175 lttng_destroy_handle(handle
);
2180 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2183 int ret
, started
= -1, snapshot_mode
= -1;
2184 uint64_t live_timer_interval
= UINT64_MAX
;
2185 xmlChar
*name
= NULL
;
2186 xmlNodePtr domains_node
= NULL
;
2187 xmlNodePtr output_node
= NULL
;
2189 struct lttng_domain
*kernel_domain
= NULL
;
2190 struct lttng_domain
*ust_domain
= NULL
;
2191 struct lttng_domain
*jul_domain
= NULL
;
2192 struct lttng_domain
*log4j_domain
= NULL
;
2194 for (node
= xmlFirstElementChild(session_node
); node
;
2195 node
= xmlNextElementSibling(node
)) {
2196 if (!name
&& !strcmp((const char *) node
->name
,
2197 config_element_name
)) {
2199 xmlChar
*node_content
= xmlNodeGetContent(node
);
2200 if (!node_content
) {
2201 ret
= -LTTNG_ERR_NOMEM
;
2205 name
= node_content
;
2206 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2207 config_element_domains
)) {
2209 domains_node
= node
;
2210 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2211 config_element_started
)) {
2213 xmlChar
*node_content
= xmlNodeGetContent(node
);
2214 if (!node_content
) {
2215 ret
= -LTTNG_ERR_NOMEM
;
2219 ret
= parse_bool(node_content
, &started
);
2222 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2225 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2226 config_element_output
)) {
2230 /* attributes, snapshot_mode or live_timer_interval */
2231 xmlNodePtr attributes_child
=
2232 xmlFirstElementChild(node
);
2234 if (!strcmp((const char *) attributes_child
->name
,
2235 config_element_snapshot_mode
)) {
2237 xmlChar
*snapshot_mode_content
=
2238 xmlNodeGetContent(attributes_child
);
2239 if (!snapshot_mode_content
) {
2240 ret
= -LTTNG_ERR_NOMEM
;
2244 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2245 free(snapshot_mode_content
);
2247 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2251 /* live_timer_interval */
2252 xmlChar
*timer_interval_content
=
2253 xmlNodeGetContent(attributes_child
);
2254 if (!timer_interval_content
) {
2255 ret
= -LTTNG_ERR_NOMEM
;
2259 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2260 free(timer_interval_content
);
2262 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2270 /* Mandatory attribute, as defined in the session XSD */
2271 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2275 if (session_name
&& strcmp((char *) name
, session_name
)) {
2276 /* This is not the session we are looking for */
2277 ret
= -LTTNG_ERR_NO_SESSION
;
2281 /* Init domains to create the session handles */
2282 for (node
= xmlFirstElementChild(domains_node
); node
;
2283 node
= xmlNextElementSibling(node
)) {
2284 struct lttng_domain
*domain
;
2286 domain
= zmalloc(sizeof(*domain
));
2288 ret
= -LTTNG_ERR_NOMEM
;
2292 ret
= init_domain(node
, domain
);
2294 goto domain_init_error
;
2297 switch (domain
->type
) {
2298 case LTTNG_DOMAIN_KERNEL
:
2299 if (kernel_domain
) {
2300 /* Same domain seen twice, invalid! */
2301 goto domain_init_error
;
2303 kernel_domain
= domain
;
2305 case LTTNG_DOMAIN_UST
:
2307 /* Same domain seen twice, invalid! */
2308 goto domain_init_error
;
2310 ust_domain
= domain
;
2312 case LTTNG_DOMAIN_JUL
:
2314 /* Same domain seen twice, invalid! */
2315 goto domain_init_error
;
2317 jul_domain
= domain
;
2319 case LTTNG_DOMAIN_LOG4J
:
2321 /* Same domain seen twice, invalid! */
2322 goto domain_init_error
;
2324 log4j_domain
= domain
;
2327 WARN("Invalid domain type");
2328 goto domain_init_error
;
2333 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2338 /* Destroy session if it exists */
2339 ret
= lttng_destroy_session((const char *) name
);
2340 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2341 ERR("Failed to destroy existing session.");
2346 /* Create session type depending on output type */
2347 if (snapshot_mode
&& snapshot_mode
!= -1) {
2348 ret
= create_snapshot_session((const char *) name
, output_node
);
2349 } else if (live_timer_interval
&&
2350 live_timer_interval
!= UINT64_MAX
) {
2351 ret
= create_session((const char *) name
, kernel_domain
,
2352 ust_domain
, jul_domain
, log4j_domain
,
2353 output_node
, live_timer_interval
);
2355 /* regular session */
2356 ret
= create_session((const char *) name
, kernel_domain
,
2357 ust_domain
, jul_domain
, log4j_domain
,
2358 output_node
, UINT64_MAX
);
2364 for (node
= xmlFirstElementChild(domains_node
); node
;
2365 node
= xmlNextElementSibling(node
)) {
2366 ret
= process_domain_node(node
, (const char *) name
);
2373 ret
= lttng_start_tracing((const char *) name
);
2381 ERR("Failed to load session %s: %s", (const char *) name
,
2382 lttng_strerror(ret
));
2383 lttng_destroy_session((const char *) name
);
2387 free(kernel_domain
);
2396 * Return 1 if the given path is readable by the current UID or 0 if not.
2397 * Return -1 if the path is EPERM.
2399 static int validate_file_read_creds(const char *path
)
2405 /* Can we read the file. */
2406 ret
= access(path
, R_OK
);
2410 if (errno
== EACCES
) {
2421 int load_session_from_file(const char *path
, const char *session_name
,
2422 struct session_config_validation_ctx
*validation_ctx
, int override
)
2424 int ret
, session_found
= !session_name
;
2425 xmlDocPtr doc
= NULL
;
2426 xmlNodePtr sessions_node
;
2427 xmlNodePtr session_node
;
2430 assert(validation_ctx
);
2432 ret
= validate_file_read_creds(path
);
2435 ret
= -LTTNG_ERR_EPERM
;
2437 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2442 doc
= xmlParseFile(path
);
2444 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2448 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2450 ERR("Session configuration file validation failed");
2451 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2455 sessions_node
= xmlDocGetRootElement(doc
);
2456 if (!sessions_node
) {
2460 for (session_node
= xmlFirstElementChild(sessions_node
);
2461 session_node
; session_node
=
2462 xmlNextElementSibling(session_node
)) {
2463 ret
= process_session_node(session_node
,
2464 session_name
, override
);
2465 if (session_name
&& ret
== 0) {
2466 /* Target session found and loaded */
2474 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2479 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2481 struct dirent
*alloc_dirent(const char *path
)
2485 struct dirent
*entry
;
2487 name_max
= pathconf(path
, _PC_NAME_MAX
);
2488 if (name_max
== -1) {
2489 name_max
= PATH_MAX
;
2491 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2492 entry
= zmalloc(len
);
2497 int load_session_from_path(const char *path
, const char *session_name
,
2498 struct session_config_validation_ctx
*validation_ctx
, int override
)
2500 int ret
, session_found
= !session_name
;
2501 DIR *directory
= NULL
;
2504 assert(validation_ctx
);
2506 directory
= opendir(path
);
2510 /* Try the file loading. */
2513 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2516 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2521 struct dirent
*entry
;
2522 struct dirent
*result
;
2523 char *file_path
= NULL
;
2524 size_t path_len
= strlen(path
);
2526 if (path_len
>= PATH_MAX
) {
2527 ret
= -LTTNG_ERR_INVALID
;
2531 entry
= alloc_dirent(path
);
2533 ret
= -LTTNG_ERR_NOMEM
;
2537 file_path
= zmalloc(PATH_MAX
);
2539 ret
= -LTTNG_ERR_NOMEM
;
2544 strncpy(file_path
, path
, path_len
);
2545 if (file_path
[path_len
- 1] != '/') {
2546 file_path
[path_len
++] = '/';
2550 /* Search for *.lttng files */
2551 while (!readdir_r(directory
, entry
, &result
) && result
) {
2552 size_t file_name_len
= strlen(result
->d_name
);
2554 if (file_name_len
<=
2555 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2559 if (path_len
+ file_name_len
>= PATH_MAX
) {
2563 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2564 result
->d_name
+ file_name_len
- sizeof(
2565 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2569 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2570 file_path
[path_len
+ file_name_len
] = '\0';
2572 ret
= load_session_from_file(file_path
, session_name
,
2573 validation_ctx
, override
);
2574 if (session_name
&& !ret
) {
2583 ret
= load_session_from_file(path
, session_name
,
2584 validation_ctx
, override
);
2594 if (closedir(directory
)) {
2599 if (session_found
) {
2607 * Validate that the given path's credentials and the current process have the
2608 * same UID. If so, return 1 else return 0 if it does NOT match.
2610 static int validate_path_creds(const char *path
)
2612 int ret
, uid
= getuid();
2621 ret
= stat(path
, &buf
);
2623 if (errno
!= ENOENT
) {
2626 ret
= -LTTNG_ERR_INVALID
;
2630 if (buf
.st_uid
!= uid
) {
2641 int config_load_session(const char *path
, const char *session_name
,
2642 int override
, unsigned int autoload
)
2645 bool session_loaded
= false;
2646 const char *path_ptr
= NULL
;
2647 struct session_config_validation_ctx validation_ctx
= { 0 };
2649 ret
= init_session_config_validation_ctx(&validation_ctx
);
2656 const char *sys_path
;
2659 home_path
= utils_get_home_dir();
2661 char path
[PATH_MAX
];
2664 * Try user session configuration path. Ignore error here so we can
2665 * continue loading the system wide sessions.
2668 ret
= snprintf(path
, sizeof(path
),
2669 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2670 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2672 PERROR("snprintf session autoload home config path");
2677 * Credentials are only validated for the autoload in order to
2678 * avoid any user session daemon to try to load kernel sessions
2679 * automatically and failing all the times.
2681 ret
= validate_path_creds(path
);
2686 ret
= snprintf(path
, sizeof(path
),
2687 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2689 PERROR("snprintf session home config path");
2695 ret
= load_session_from_path(path_ptr
, session_name
,
2696 &validation_ctx
, override
);
2697 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2701 * Continue even if the session was found since we have to try
2702 * the system wide sessions.
2704 session_loaded
= true;
2708 /* Reset path pointer for the system wide dir. */
2711 /* Try system wide configuration directory. */
2713 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2714 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2715 ret
= validate_path_creds(sys_path
);
2717 path_ptr
= sys_path
;
2720 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2721 path_ptr
= sys_path
;
2725 ret
= load_session_from_path(path_ptr
, session_name
,
2726 &validation_ctx
, override
);
2728 session_loaded
= true;
2732 ret
= access(path
, F_OK
);
2737 ret
= -LTTNG_ERR_INVALID
;
2738 WARN("Session configuration path does not exist.");
2741 ret
= -LTTNG_ERR_EPERM
;
2744 ret
= -LTTNG_ERR_UNK
;
2750 ret
= load_session_from_path(path
, session_name
,
2751 &validation_ctx
, override
);
2754 fini_session_config_validation_ctx(&validation_ctx
);
2755 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2757 * Don't report an error if no sessions are found when called
2758 * without a session_name or a search path.
2763 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2764 /* A matching session was found in one of the search paths. */