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";
120 const char * const config_domain_type_log4j
= "LOG4J";
122 const char * const config_buffer_type_per_pid
= "PER_PID";
123 const char * const config_buffer_type_per_uid
= "PER_UID";
124 const char * const config_buffer_type_global
= "GLOBAL";
126 const char * const config_overwrite_mode_discard
= "DISCARD";
127 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
129 const char * const config_output_type_splice
= "SPLICE";
130 const char * const config_output_type_mmap
= "MMAP";
132 const char * const config_loglevel_type_all
= "ALL";
133 const char * const config_loglevel_type_range
= "RANGE";
134 const char * const config_loglevel_type_single
= "SINGLE";
136 const char * const config_event_type_all
= "ALL";
137 const char * const config_event_type_tracepoint
= "TRACEPOINT";
138 const char * const config_event_type_probe
= "PROBE";
139 const char * const config_event_type_function
= "FUNCTION";
140 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
141 const char * const config_event_type_noop
= "NOOP";
142 const char * const config_event_type_syscall
= "SYSCALL";
143 const char * const config_event_type_kprobe
= "KPROBE";
144 const char * const config_event_type_kretprobe
= "KRETPROBE";
146 const char * const config_event_context_pid
= "PID";
147 const char * const config_event_context_procname
= "PROCNAME";
148 const char * const config_event_context_prio
= "PRIO";
149 const char * const config_event_context_nice
= "NICE";
150 const char * const config_event_context_vpid
= "VPID";
151 const char * const config_event_context_tid
= "TID";
152 const char * const config_event_context_vtid
= "VTID";
153 const char * const config_event_context_ppid
= "PPID";
154 const char * const config_event_context_vppid
= "VPPID";
155 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
156 const char * const config_event_context_hostname
= "HOSTNAME";
157 const char * const config_event_context_ip
= "IP";
158 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
160 struct consumer_output
{
167 static int config_entry_handler_filter(struct handler_filter_args
*args
,
168 const char *section
, const char *name
, const char *value
)
171 struct config_entry entry
= { section
, name
, value
};
175 if (!section
|| !name
|| !value
) {
181 if (strcmp(args
->section
, section
)) {
186 ret
= args
->handler(&entry
, args
->user_data
);
192 int config_get_section_entries(const char *override_path
, const char *section
,
193 config_entry_handler_cb handler
, void *user_data
)
197 FILE *config_file
= NULL
;
198 struct handler_filter_args filter
= { section
, handler
, user_data
};
200 /* First, try system-wide conf. file. */
201 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
203 config_file
= fopen(path
, "r");
205 DBG("Loading daemon conf file at %s", path
);
207 * Return value is not very important here since error or not, we
208 * continue and try the next possible conf. file.
210 (void) ini_parse_file(config_file
,
211 (ini_entry_handler
) config_entry_handler_filter
,
216 /* Second is the user local configuration. */
217 path
= utils_get_home_dir();
219 char fullpath
[PATH_MAX
];
221 ret
= snprintf(fullpath
, sizeof(fullpath
),
222 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
224 PERROR("snprintf user conf. path");
228 config_file
= fopen(fullpath
, "r");
230 DBG("Loading daemon user conf file at %s", path
);
232 * Return value is not very important here since error or not, we
233 * continue and try the next possible conf. file.
235 (void) ini_parse_file(config_file
,
236 (ini_entry_handler
) config_entry_handler_filter
,
242 /* Final path is the one that the user might have provided. */
244 config_file
= fopen(override_path
, "r");
246 DBG("Loading daemon command line conf file at %s", override_path
);
247 (void) ini_parse_file(config_file
,
248 (ini_entry_handler
) config_entry_handler_filter
,
252 ERR("Failed to open daemon configuration file at %s",
259 /* Everything went well. */
267 int config_parse_value(const char *value
)
270 char *endptr
, *lower_str
;
280 v
= strtoul(value
, &endptr
, 10);
281 if (endptr
!= value
) {
286 lower_str
= zmalloc(len
+ 1);
293 for (i
= 0; i
< len
; i
++) {
294 lower_str
[i
] = tolower(value
[i
]);
297 if (!strcmp(lower_str
, config_str_yes
) ||
298 !strcmp(lower_str
, config_str_true
) ||
299 !strcmp(lower_str
, config_str_on
)) {
301 } else if (!strcmp(lower_str
, config_str_no
) ||
302 !strcmp(lower_str
, config_str_false
) ||
303 !strcmp(lower_str
, config_str_off
)) {
315 * Returns a xmlChar string which must be released using xmlFree().
317 static xmlChar
*encode_string(const char *in_str
)
319 xmlChar
*out_str
= NULL
;
320 xmlCharEncodingHandlerPtr handler
;
321 int out_len
, ret
, in_len
;
325 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
327 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
331 in_len
= strlen(in_str
);
333 * Add 1 byte for the NULL terminted character. The factor 4 here is
334 * used because UTF-8 characters can take up to 4 bytes.
336 out_len
= (in_len
* 4) + 1;
337 out_str
= xmlMalloc(out_len
);
342 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
349 /* out_len is now the size of out_str */
350 out_str
[out_len
] = '\0';
356 struct config_writer
*config_writer_create(int fd_output
, int indent
)
359 struct config_writer
*writer
;
360 xmlOutputBufferPtr buffer
;
362 writer
= zmalloc(sizeof(struct config_writer
));
364 PERROR("zmalloc config_writer_create");
368 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
373 writer
->writer
= xmlNewTextWriter(buffer
);
374 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
375 config_xml_encoding
, NULL
);
380 ret
= xmlTextWriterSetIndentString(writer
->writer
,
381 BAD_CAST config_xml_indent_string
);
386 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
394 config_writer_destroy(writer
);
399 int config_writer_destroy(struct config_writer
*writer
)
408 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
409 WARN("Could not close XML document");
413 if (writer
->writer
) {
414 xmlFreeTextWriter(writer
->writer
);
423 int config_writer_open_element(struct config_writer
*writer
,
424 const char *element_name
)
427 xmlChar
*encoded_element_name
;
429 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
434 encoded_element_name
= encode_string(element_name
);
435 if (!encoded_element_name
) {
440 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
441 xmlFree(encoded_element_name
);
443 return ret
> 0 ? 0 : ret
;
447 int config_writer_close_element(struct config_writer
*writer
)
451 if (!writer
|| !writer
->writer
) {
456 ret
= xmlTextWriterEndElement(writer
->writer
);
458 return ret
> 0 ? 0 : ret
;
462 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
463 const char *element_name
, uint64_t value
)
466 xmlChar
*encoded_element_name
;
468 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
473 encoded_element_name
= encode_string(element_name
);
474 if (!encoded_element_name
) {
479 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
480 encoded_element_name
, "%" PRIu64
, value
);
481 xmlFree(encoded_element_name
);
483 return ret
> 0 ? 0 : ret
;
487 int config_writer_write_element_signed_int(struct config_writer
*writer
,
488 const char *element_name
, int64_t value
)
491 xmlChar
*encoded_element_name
;
493 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
498 encoded_element_name
= encode_string(element_name
);
499 if (!encoded_element_name
) {
504 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
505 encoded_element_name
, "%" PRIi64
, value
);
506 xmlFree(encoded_element_name
);
508 return ret
> 0 ? 0 : ret
;
512 int config_writer_write_element_bool(struct config_writer
*writer
,
513 const char *element_name
, int value
)
515 return config_writer_write_element_string(writer
, element_name
,
516 value
? config_xml_true
: config_xml_false
);
520 int config_writer_write_element_string(struct config_writer
*writer
,
521 const char *element_name
, const char *value
)
524 xmlChar
*encoded_element_name
= NULL
;
525 xmlChar
*encoded_value
= NULL
;
527 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
533 encoded_element_name
= encode_string(element_name
);
534 if (!encoded_element_name
) {
539 encoded_value
= encode_string(value
);
540 if (!encoded_value
) {
545 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
548 xmlFree(encoded_element_name
);
549 xmlFree(encoded_value
);
550 return ret
> 0 ? 0 : ret
;
554 void xml_error_handler(void *ctx
, const char *format
, ...)
560 va_start(args
, format
);
561 ret
= vasprintf(&errMsg
, format
, args
);
564 ERR("String allocation failed in xml error handler");
568 fprintf(stderr
, "XML Error: %s", errMsg
);
573 void fini_session_config_validation_ctx(
574 struct session_config_validation_ctx
*ctx
)
576 if (ctx
->parser_ctx
) {
577 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
581 xmlSchemaFree(ctx
->schema
);
584 if (ctx
->schema_validation_ctx
) {
585 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
588 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
592 char *get_session_config_xsd_path()
595 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
596 size_t base_path_len
;
600 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
603 base_path_len
= strlen(base_path
);
604 max_path_len
= base_path_len
+
605 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
606 xsd_path
= zmalloc(max_path_len
);
611 strncpy(xsd_path
, base_path
, max_path_len
);
612 if (xsd_path
[base_path_len
- 1] != '/') {
613 xsd_path
[base_path_len
++] = '/';
616 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
617 max_path_len
- base_path_len
);
623 int init_session_config_validation_ctx(
624 struct session_config_validation_ctx
*ctx
)
627 char *xsd_path
= get_session_config_xsd_path();
630 ret
= -LTTNG_ERR_NOMEM
;
634 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
635 if (!ctx
->parser_ctx
) {
636 ERR("XSD parser context creation failed");
637 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
640 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
641 xml_error_handler
, NULL
);
643 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
645 ERR("XSD parsing failed");
646 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
650 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
651 if (!ctx
->schema_validation_ctx
) {
652 ERR("XSD validation context creation failed");
653 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
657 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
658 xml_error_handler
, NULL
);
663 fini_session_config_validation_ctx(ctx
);
671 int parse_uint(xmlChar
*str
, uint64_t *val
)
681 *val
= strtoull((const char *) str
, &endptr
, 10);
682 if (!endptr
|| *endptr
) {
693 int parse_int(xmlChar
*str
, int64_t *val
)
703 *val
= strtoll((const char *) str
, &endptr
, 10);
704 if (!endptr
|| *endptr
) {
715 int parse_bool(xmlChar
*str
, int *val
)
724 if (!strcmp((const char *) str
, config_xml_true
)) {
726 } else if (!strcmp((const char *) str
, config_xml_false
)) {
729 WARN("Invalid boolean value encoutered (%s).",
738 int get_domain_type(xmlChar
*domain
)
746 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
747 ret
= LTTNG_DOMAIN_KERNEL
;
748 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
749 ret
= LTTNG_DOMAIN_UST
;
750 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
751 ret
= LTTNG_DOMAIN_JUL
;
752 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
753 ret
= LTTNG_DOMAIN_LOG4J
;
764 int get_buffer_type(xmlChar
*buffer_type
)
772 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
773 ret
= LTTNG_BUFFER_GLOBAL
;
774 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
775 ret
= LTTNG_BUFFER_PER_UID
;
776 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
777 ret
= LTTNG_BUFFER_PER_PID
;
788 int get_overwrite_mode(xmlChar
*overwrite_mode
)
792 if (!overwrite_mode
) {
796 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
798 } else if (!strcmp((char *) overwrite_mode
,
799 config_overwrite_mode_discard
)) {
811 int get_output_type(xmlChar
*output_type
)
819 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
820 ret
= LTTNG_EVENT_MMAP
;
821 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
822 ret
= LTTNG_EVENT_SPLICE
;
833 int get_event_type(xmlChar
*event_type
)
841 if (!strcmp((char *) event_type
, config_event_type_all
)) {
842 ret
= LTTNG_EVENT_ALL
;
843 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
844 ret
= LTTNG_EVENT_TRACEPOINT
;
845 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
846 ret
= LTTNG_EVENT_PROBE
;
847 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
848 ret
= LTTNG_EVENT_FUNCTION
;
849 } else if (!strcmp((char *) event_type
,
850 config_event_type_function_entry
)) {
851 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
852 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
853 ret
= LTTNG_EVENT_NOOP
;
854 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
855 ret
= LTTNG_EVENT_SYSCALL
;
866 int get_loglevel_type(xmlChar
*loglevel_type
)
870 if (!loglevel_type
) {
874 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
875 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
876 } else if (!strcmp((char *) loglevel_type
,
877 config_loglevel_type_range
)) {
878 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
879 } else if (!strcmp((char *) loglevel_type
,
880 config_loglevel_type_single
)) {
881 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
892 * Return the context type or -1 on error.
895 int get_context_type(xmlChar
*context_type
)
903 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
904 ret
= LTTNG_EVENT_CONTEXT_PID
;
905 } else if (!strcmp((char *) context_type
,
906 config_event_context_procname
)) {
907 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
908 } else if (!strcmp((char *) context_type
,
909 config_event_context_prio
)) {
910 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
911 } else if (!strcmp((char *) context_type
,
912 config_event_context_nice
)) {
913 ret
= LTTNG_EVENT_CONTEXT_NICE
;
914 } else if (!strcmp((char *) context_type
,
915 config_event_context_vpid
)) {
916 ret
= LTTNG_EVENT_CONTEXT_VPID
;
917 } else if (!strcmp((char *) context_type
,
918 config_event_context_tid
)) {
919 ret
= LTTNG_EVENT_CONTEXT_TID
;
920 } else if (!strcmp((char *) context_type
,
921 config_event_context_vtid
)) {
922 ret
= LTTNG_EVENT_CONTEXT_VTID
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_ppid
)) {
925 ret
= LTTNG_EVENT_CONTEXT_PPID
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_vppid
)) {
928 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_pthread_id
)) {
931 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_hostname
)) {
934 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
935 } else if (!strcmp((char *) context_type
,
936 config_event_context_ip
)) {
937 ret
= LTTNG_EVENT_CONTEXT_IP
;
948 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
953 for (node
= xmlFirstElementChild(domain_node
); node
;
954 node
= xmlNextElementSibling(node
)) {
955 if (!strcmp((const char *) node
->name
, config_element_type
)) {
957 xmlChar
*node_content
= xmlNodeGetContent(node
);
959 ret
= -LTTNG_ERR_NOMEM
;
963 ret
= get_domain_type(node_content
);
966 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
971 } else if (!strcmp((const char *) node
->name
,
972 config_element_buffer_type
)) {
974 xmlChar
*node_content
= xmlNodeGetContent(node
);
976 ret
= -LTTNG_ERR_NOMEM
;
980 ret
= get_buffer_type(node_content
);
983 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
987 domain
->buf_type
= ret
;
996 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1001 for (node
= xmlFirstElementChild(net_output_node
); node
;
1002 node
= xmlNextElementSibling(node
)) {
1003 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1005 *control_uri
= (char *) xmlNodeGetContent(node
);
1006 if (!*control_uri
) {
1011 *data_uri
= (char *) xmlNodeGetContent(node
);
1018 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1022 int process_consumer_output(xmlNodePtr consumer_output_node
,
1023 struct consumer_output
*output
)
1030 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1031 node
= xmlNextElementSibling(node
)) {
1032 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1033 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1037 ret
= -LTTNG_ERR_NOMEM
;
1041 ret
= parse_bool(enabled_str
, &output
->enabled
);
1047 xmlNodePtr output_type_node
;
1050 output_type_node
= xmlFirstElementChild(node
);
1051 if (!output_type_node
) {
1052 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1056 if (!strcmp((const char *) output_type_node
->name
,
1057 config_element_path
)) {
1059 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1060 if (!output
->path
) {
1061 ret
= -LTTNG_ERR_NOMEM
;
1066 ret
= get_net_output_uris(output_type_node
,
1067 &output
->control_uri
, &output
->data_uri
);
1079 free(output
->control_uri
);
1080 free(output
->data_uri
);
1081 memset(output
, 0, sizeof(struct consumer_output
));
1087 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1088 const char *control_uri
, const char *data_uri
)
1091 struct lttng_handle
*handle
;
1092 const char *uri
= NULL
;
1097 handle
= lttng_create_handle(name
, domain
);
1099 ret
= -LTTNG_ERR_NOMEM
;
1103 if (!control_uri
|| !data_uri
) {
1104 uri
= control_uri
? control_uri
: data_uri
;
1109 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1110 lttng_destroy_handle(handle
);
1116 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1119 xmlNodePtr node
= NULL
;
1120 xmlNodePtr snapshot_output_list_node
;
1121 xmlNodePtr snapshot_output_node
;
1123 assert(session_name
);
1125 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1134 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1136 /* Parse and create snapshot outputs */
1138 for (snapshot_output_node
=
1139 xmlFirstElementChild(snapshot_output_list_node
);
1140 snapshot_output_node
; snapshot_output_node
=
1141 xmlNextElementSibling(snapshot_output_node
)) {
1143 uint64_t max_size
= UINT64_MAX
;
1144 struct consumer_output output
= { 0 };
1145 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1147 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1148 node
= xmlNextElementSibling(node
)) {
1149 if (!strcmp((const char *) node
->name
,
1150 config_element_name
)) {
1152 name
= (char *) xmlNodeGetContent(node
);
1154 ret
= -LTTNG_ERR_NOMEM
;
1155 goto error_snapshot_output
;
1157 } else if (!strcmp((const char *) node
->name
,
1158 config_element_max_size
)) {
1159 xmlChar
*content
= xmlNodeGetContent(node
);
1163 ret
= -LTTNG_ERR_NOMEM
;
1164 goto error_snapshot_output
;
1166 ret
= parse_uint(content
, &max_size
);
1169 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1170 goto error_snapshot_output
;
1173 /* consumer_output */
1174 ret
= process_consumer_output(node
, &output
);
1176 goto error_snapshot_output
;
1181 snapshot_output
= lttng_snapshot_output_create();
1182 if (!snapshot_output
) {
1183 ret
= -LTTNG_ERR_NOMEM
;
1184 goto error_snapshot_output
;
1187 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1189 goto error_snapshot_output
;
1192 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1194 goto error_snapshot_output
;
1198 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1201 goto error_snapshot_output
;
1204 if (output
.control_uri
) {
1205 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1208 goto error_snapshot_output
;
1212 if (output
.data_uri
) {
1213 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1216 goto error_snapshot_output
;
1221 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1222 error_snapshot_output
:
1225 free(output
.control_uri
);
1226 free(output
.data_uri
);
1227 lttng_snapshot_output_destroy(snapshot_output
);
1237 int create_session(const char *name
,
1238 struct lttng_domain
*kernel_domain
,
1239 struct lttng_domain
*ust_domain
,
1240 struct lttng_domain
*jul_domain
,
1241 struct lttng_domain
*log4j_domain
,
1242 xmlNodePtr output_node
,
1243 uint64_t live_timer_interval
)
1246 struct consumer_output output
= { 0 };
1247 xmlNodePtr consumer_output_node
;
1252 consumer_output_node
= xmlFirstElementChild(output_node
);
1253 if (!consumer_output_node
) {
1254 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1258 if (strcmp((const char *) consumer_output_node
->name
,
1259 config_element_consumer_output
)) {
1260 WARN("Invalid output type, expected %s node",
1261 config_element_consumer_output
);
1262 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1266 ret
= process_consumer_output(consumer_output_node
, &output
);
1272 if (live_timer_interval
!= UINT64_MAX
&&
1273 !output
.control_uri
&& !output
.data_uri
) {
1274 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1278 if (output
.control_uri
|| output
.data_uri
) {
1280 struct lttng_domain
*domain
;
1281 struct lttng_domain
*domains
[] =
1282 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1284 /* network destination */
1285 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1287 * URLs are provided for sure since the test above make sure that
1288 * with a live timer the data and control URIs are provided. So,
1289 * NULL is passed here and will be set right after.
1291 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1293 ret
= lttng_create_session(name
, NULL
);
1299 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1300 domain
= domains
[i
];
1305 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1312 /* either local output or no output */
1313 ret
= lttng_create_session(name
, output
.path
);
1320 free(output
.control_uri
);
1321 free(output
.data_uri
);
1325 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1326 struct lttng_event_probe_attr
*attr
)
1330 assert(probe_attribute_node
);
1333 if (!strcmp((const char *) probe_attribute_node
->name
,
1334 config_element_address
)) {
1339 content
= xmlNodeGetContent(probe_attribute_node
);
1341 ret
= -LTTNG_ERR_NOMEM
;
1345 ret
= parse_uint(content
, &addr
);
1348 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1353 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1354 config_element_offset
)) {
1356 uint64_t offset
= 0;
1359 content
= xmlNodeGetContent(probe_attribute_node
);
1361 ret
= -LTTNG_ERR_NOMEM
;
1365 ret
= parse_uint(content
, &offset
);
1368 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1372 attr
->offset
= offset
;
1373 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1374 config_element_symbol_name
)) {
1379 content
= xmlNodeGetContent(probe_attribute_node
);
1381 ret
= -LTTNG_ERR_NOMEM
;
1385 name_len
= strlen((char *) content
);
1386 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1387 WARN("symbol_name too long.");
1388 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1393 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1402 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1403 const char *channel_name
)
1407 struct lttng_event event
;
1408 char **exclusions
= NULL
;
1409 unsigned long exclusion_count
= 0;
1410 char *filter_expression
= NULL
;
1414 assert(channel_name
);
1416 memset(&event
, 0, sizeof(event
));
1418 /* Initialize default log level which varies by domain */
1419 switch (handle
->domain
.type
)
1421 case LTTNG_DOMAIN_JUL
:
1422 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1424 case LTTNG_DOMAIN_LOG4J
:
1425 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1427 case LTTNG_DOMAIN_UST
:
1428 case LTTNG_DOMAIN_KERNEL
:
1429 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1435 for (node
= xmlFirstElementChild(event_node
); node
;
1436 node
= xmlNextElementSibling(node
)) {
1437 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1442 content
= xmlNodeGetContent(node
);
1444 ret
= -LTTNG_ERR_NOMEM
;
1448 name_len
= strlen((char *) content
);
1449 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1450 WARN("Channel name too long.");
1451 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1456 strncpy(event
.name
, (const char *) content
, name_len
);
1458 } else if (!strcmp((const char *) node
->name
,
1459 config_element_enabled
)) {
1460 xmlChar
*content
= xmlNodeGetContent(node
);
1464 ret
= -LTTNG_ERR_NOMEM
;
1468 ret
= parse_bool(content
, &event
.enabled
);
1471 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1474 } else if (!strcmp((const char *) node
->name
,
1475 config_element_type
)) {
1476 xmlChar
*content
= xmlNodeGetContent(node
);
1480 ret
= -LTTNG_ERR_NOMEM
;
1484 ret
= get_event_type(content
);
1487 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1492 } else if (!strcmp((const char *) node
->name
,
1493 config_element_loglevel_type
)) {
1494 xmlChar
*content
= xmlNodeGetContent(node
);
1498 ret
= -LTTNG_ERR_NOMEM
;
1502 ret
= get_loglevel_type(content
);
1505 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1509 event
.loglevel_type
= ret
;
1510 } else if (!strcmp((const char *) node
->name
,
1511 config_element_loglevel
)) {
1513 int64_t loglevel
= 0;
1516 content
= xmlNodeGetContent(node
);
1518 ret
= -LTTNG_ERR_NOMEM
;
1522 ret
= parse_int(content
, &loglevel
);
1525 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1529 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1530 WARN("loglevel out of range.");
1531 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1535 event
.loglevel
= loglevel
;
1536 } else if (!strcmp((const char *) node
->name
,
1537 config_element_filter
)) {
1539 xmlNodeGetContent(node
);
1543 ret
= -LTTNG_ERR_NOMEM
;
1547 filter_expression
= strdup((char *) content
);
1549 if (!filter_expression
) {
1550 ret
= -LTTNG_ERR_NOMEM
;
1553 } else if (!strcmp((const char *) node
->name
,
1554 config_element_exclusions
)) {
1555 xmlNodePtr exclusion_node
;
1556 int exclusion_index
= 0;
1561 * Exclusions has already been initialized,
1564 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1568 exclusion_count
= xmlChildElementCount(node
);
1569 if (!exclusion_count
) {
1573 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1575 exclusion_count
= 0;
1576 ret
= -LTTNG_ERR_NOMEM
;
1580 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1581 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1583 xmlNodeGetContent(exclusion_node
);
1586 ret
= -LTTNG_ERR_NOMEM
;
1590 exclusions
[exclusion_index
] = strdup((const char *) content
);
1592 if (!exclusions
[exclusion_index
]) {
1593 ret
= -LTTNG_ERR_NOMEM
;
1599 event
.exclusion
= 1;
1600 } else if (!strcmp((const char *) node
->name
,
1601 config_element_attributes
)) {
1602 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1605 if (!attribute_node
) {
1606 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1610 if (!strcmp((const char *) node
->name
,
1611 config_element_probe_attributes
)) {
1612 xmlNodePtr probe_attribute_node
;
1614 /* probe_attributes */
1615 for (probe_attribute_node
=
1616 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1617 probe_attribute_node
= xmlNextElementSibling(
1618 probe_attribute_node
)) {
1620 ret
= process_probe_attribute_node(probe_attribute_node
,
1629 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1631 /* function_attributes */
1632 content
= xmlNodeGetContent(symbol_node
);
1634 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1638 sym_len
= strlen((char *) content
);
1639 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1640 WARN("Function name too long.");
1641 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1646 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1653 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1654 filter_expression
, exclusion_count
, exclusions
);
1659 if (!event
.enabled
) {
1661 * Note that we should use lttng_disable_event_ext() (2.6+) to
1662 * eliminate the risk of clashing on events of the same
1663 * name (with different event types and loglevels).
1665 * Unfortunately, lttng_disable_event_ext() only performs a
1666 * match on the name and event type and errors out if any other
1667 * event attribute is not set to its default value.
1669 * This will disable all events that match this name.
1671 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1674 for (i
= 0; i
< exclusion_count
; i
++) {
1675 free(exclusions
[i
]);
1679 free(filter_expression
);
1684 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1685 const char *channel_name
)
1690 assert(events_node
);
1692 assert(channel_name
);
1694 for (node
= xmlFirstElementChild(events_node
); node
;
1695 node
= xmlNextElementSibling(node
)) {
1696 ret
= process_event_node(node
, handle
, channel_name
);
1706 int process_channel_attr_node(xmlNodePtr attr_node
,
1707 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1708 xmlNodePtr
*events_node
)
1714 assert(contexts_node
);
1715 assert(events_node
);
1717 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1722 content
= xmlNodeGetContent(attr_node
);
1724 ret
= -LTTNG_ERR_NOMEM
;
1728 name_len
= strlen((char *) content
);
1729 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1730 WARN("Channel name too long.");
1731 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1736 strncpy(channel
->name
, (const char *) content
, name_len
);
1738 } else if (!strcmp((const char *) attr_node
->name
,
1739 config_element_enabled
)) {
1744 content
= xmlNodeGetContent(attr_node
);
1746 ret
= -LTTNG_ERR_NOMEM
;
1750 ret
= parse_bool(content
, &enabled
);
1753 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1757 channel
->enabled
= enabled
;
1758 } else if (!strcmp((const char *) attr_node
->name
,
1759 config_element_overwrite_mode
)) {
1762 /* overwrite_mode */
1763 content
= xmlNodeGetContent(attr_node
);
1765 ret
= -LTTNG_ERR_NOMEM
;
1769 ret
= get_overwrite_mode(content
);
1772 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1776 channel
->attr
.overwrite
= ret
;
1777 } else if (!strcmp((const char *) attr_node
->name
,
1778 config_element_subbuf_size
)) {
1781 /* subbuffer_size */
1782 content
= xmlNodeGetContent(attr_node
);
1784 ret
= -LTTNG_ERR_NOMEM
;
1788 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1791 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1794 } else if (!strcmp((const char *) attr_node
->name
,
1795 config_element_num_subbuf
)) {
1798 /* subbuffer_count */
1799 content
= xmlNodeGetContent(attr_node
);
1801 ret
= -LTTNG_ERR_NOMEM
;
1805 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1808 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1811 } else if (!strcmp((const char *) attr_node
->name
,
1812 config_element_switch_timer_interval
)) {
1814 uint64_t switch_timer_interval
= 0;
1816 /* switch_timer_interval */
1817 content
= xmlNodeGetContent(attr_node
);
1819 ret
= -LTTNG_ERR_NOMEM
;
1823 ret
= parse_uint(content
, &switch_timer_interval
);
1826 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1830 if (switch_timer_interval
> UINT_MAX
) {
1831 WARN("switch_timer_interval out of range.");
1832 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1836 channel
->attr
.switch_timer_interval
=
1837 switch_timer_interval
;
1838 } else if (!strcmp((const char *) attr_node
->name
,
1839 config_element_read_timer_interval
)) {
1841 uint64_t read_timer_interval
= 0;
1843 /* read_timer_interval */
1844 content
= xmlNodeGetContent(attr_node
);
1846 ret
= -LTTNG_ERR_NOMEM
;
1850 ret
= parse_uint(content
, &read_timer_interval
);
1853 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1857 if (read_timer_interval
> UINT_MAX
) {
1858 WARN("read_timer_interval out of range.");
1859 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1863 channel
->attr
.read_timer_interval
=
1864 read_timer_interval
;
1865 } else if (!strcmp((const char *) attr_node
->name
,
1866 config_element_output_type
)) {
1870 content
= xmlNodeGetContent(attr_node
);
1872 ret
= -LTTNG_ERR_NOMEM
;
1876 ret
= get_output_type(content
);
1879 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1883 channel
->attr
.output
= ret
;
1884 } else if (!strcmp((const char *) attr_node
->name
,
1885 config_element_tracefile_size
)) {
1888 /* tracefile_size */
1889 content
= xmlNodeGetContent(attr_node
);
1891 ret
= -LTTNG_ERR_NOMEM
;
1895 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1898 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1901 } else if (!strcmp((const char *) attr_node
->name
,
1902 config_element_tracefile_count
)) {
1905 /* tracefile_count */
1906 content
= xmlNodeGetContent(attr_node
);
1908 ret
= -LTTNG_ERR_NOMEM
;
1912 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1915 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1918 } else if (!strcmp((const char *) attr_node
->name
,
1919 config_element_live_timer_interval
)) {
1921 uint64_t live_timer_interval
= 0;
1923 /* live_timer_interval */
1924 content
= xmlNodeGetContent(attr_node
);
1926 ret
= -LTTNG_ERR_NOMEM
;
1930 ret
= parse_uint(content
, &live_timer_interval
);
1933 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1937 if (live_timer_interval
> UINT_MAX
) {
1938 WARN("live_timer_interval out of range.");
1939 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1943 channel
->attr
.live_timer_interval
=
1944 live_timer_interval
;
1945 } else if (!strcmp((const char *) attr_node
->name
,
1946 config_element_events
)) {
1948 *events_node
= attr_node
;
1951 *contexts_node
= attr_node
;
1959 int process_context_node(xmlNodePtr context_node
,
1960 struct lttng_handle
*handle
, const char *channel_name
)
1963 struct lttng_event_context context
;
1964 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1967 assert(channel_name
);
1969 if (!context_child_node
) {
1970 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1974 memset(&context
, 0, sizeof(context
));
1976 if (!strcmp((const char *) context_child_node
->name
,
1977 config_element_type
)) {
1979 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1981 ret
= -LTTNG_ERR_NOMEM
;
1985 ret
= get_context_type(content
);
1988 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1994 xmlNodePtr perf_attr_node
;
1997 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1998 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1999 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2000 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2001 perf_attr_node
; perf_attr_node
=
2002 xmlNextElementSibling(perf_attr_node
)) {
2003 if (!strcmp((const char *) perf_attr_node
->name
,
2004 config_element_type
)) {
2009 content
= xmlNodeGetContent(perf_attr_node
);
2011 ret
= -LTTNG_ERR_NOMEM
;
2015 ret
= parse_uint(content
, &type
);
2018 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2022 if (type
> UINT32_MAX
) {
2023 WARN("perf context type out of range.");
2024 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2028 context
.u
.perf_counter
.type
= type
;
2029 } else if (!strcmp((const char *) perf_attr_node
->name
,
2030 config_element_config
)) {
2032 uint64_t config
= 0;
2035 content
= xmlNodeGetContent(perf_attr_node
);
2037 ret
= -LTTNG_ERR_NOMEM
;
2041 ret
= parse_uint(content
, &config
);
2044 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2048 context
.u
.perf_counter
.config
= config
;
2049 } else if (!strcmp((const char *) perf_attr_node
->name
,
2050 config_element_name
)) {
2055 content
= xmlNodeGetContent(perf_attr_node
);
2057 ret
= -LTTNG_ERR_NOMEM
;
2061 name_len
= strlen((char *) content
);
2062 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2063 WARN("perf context name too long.");
2064 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2069 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2076 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2082 int process_contexts_node(xmlNodePtr contexts_node
,
2083 struct lttng_handle
*handle
, const char *channel_name
)
2086 xmlNodePtr context_node
;
2088 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2089 context_node
= xmlNextElementSibling(context_node
)) {
2090 ret
= process_context_node(context_node
, handle
, channel_name
);
2100 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2103 struct lttng_domain domain
= { 0 };
2104 struct lttng_handle
*handle
= NULL
;
2105 xmlNodePtr channels_node
= NULL
;
2108 assert(session_name
);
2110 ret
= init_domain(domain_node
, &domain
);
2115 handle
= lttng_create_handle(session_name
, &domain
);
2117 ret
= -LTTNG_ERR_NOMEM
;
2121 /* get the channels node */
2122 for (node
= xmlFirstElementChild(domain_node
); node
;
2123 node
= xmlNextElementSibling(node
)) {
2124 if (!strcmp((const char *) node
->name
,
2125 config_element_channels
)) {
2126 channels_node
= node
;
2131 if (!channels_node
) {
2135 /* create all channels */
2136 for (node
= xmlFirstElementChild(channels_node
); node
;
2137 node
= xmlNextElementSibling(node
)) {
2138 struct lttng_channel channel
;
2139 xmlNodePtr contexts_node
= NULL
;
2140 xmlNodePtr events_node
= NULL
;
2141 xmlNodePtr channel_attr_node
;
2143 memset(&channel
, 0, sizeof(channel
));
2144 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2146 for (channel_attr_node
= xmlFirstElementChild(node
);
2147 channel_attr_node
; channel_attr_node
=
2148 xmlNextElementSibling(channel_attr_node
)) {
2149 ret
= process_channel_attr_node(channel_attr_node
,
2150 &channel
, &contexts_node
, &events_node
);
2156 ret
= lttng_enable_channel(handle
, &channel
);
2161 ret
= process_events_node(events_node
, handle
, channel
.name
);
2166 ret
= process_contexts_node(contexts_node
, handle
,
2173 lttng_destroy_handle(handle
);
2178 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2181 int ret
, started
= -1, snapshot_mode
= -1;
2182 uint64_t live_timer_interval
= UINT64_MAX
;
2183 xmlChar
*name
= NULL
;
2184 xmlNodePtr domains_node
= NULL
;
2185 xmlNodePtr output_node
= NULL
;
2187 struct lttng_domain
*kernel_domain
= NULL
;
2188 struct lttng_domain
*ust_domain
= NULL
;
2189 struct lttng_domain
*jul_domain
= NULL
;
2190 struct lttng_domain
*log4j_domain
= NULL
;
2192 for (node
= xmlFirstElementChild(session_node
); node
;
2193 node
= xmlNextElementSibling(node
)) {
2194 if (!name
&& !strcmp((const char *) node
->name
,
2195 config_element_name
)) {
2197 xmlChar
*node_content
= xmlNodeGetContent(node
);
2198 if (!node_content
) {
2199 ret
= -LTTNG_ERR_NOMEM
;
2203 name
= node_content
;
2204 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2205 config_element_domains
)) {
2207 domains_node
= node
;
2208 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2209 config_element_started
)) {
2211 xmlChar
*node_content
= xmlNodeGetContent(node
);
2212 if (!node_content
) {
2213 ret
= -LTTNG_ERR_NOMEM
;
2217 ret
= parse_bool(node_content
, &started
);
2220 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2223 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2224 config_element_output
)) {
2228 /* attributes, snapshot_mode or live_timer_interval */
2229 xmlNodePtr attributes_child
=
2230 xmlFirstElementChild(node
);
2232 if (!strcmp((const char *) attributes_child
->name
,
2233 config_element_snapshot_mode
)) {
2235 xmlChar
*snapshot_mode_content
=
2236 xmlNodeGetContent(attributes_child
);
2237 if (!snapshot_mode_content
) {
2238 ret
= -LTTNG_ERR_NOMEM
;
2242 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2243 free(snapshot_mode_content
);
2245 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2249 /* live_timer_interval */
2250 xmlChar
*timer_interval_content
=
2251 xmlNodeGetContent(attributes_child
);
2252 if (!timer_interval_content
) {
2253 ret
= -LTTNG_ERR_NOMEM
;
2257 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2258 free(timer_interval_content
);
2260 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2268 /* Mandatory attribute, as defined in the session XSD */
2269 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2273 if (session_name
&& strcmp((char *) name
, session_name
)) {
2274 /* This is not the session we are looking for */
2275 ret
= -LTTNG_ERR_NO_SESSION
;
2279 /* Init domains to create the session handles */
2280 for (node
= xmlFirstElementChild(domains_node
); node
;
2281 node
= xmlNextElementSibling(node
)) {
2282 struct lttng_domain
*domain
;
2284 domain
= zmalloc(sizeof(*domain
));
2286 ret
= -LTTNG_ERR_NOMEM
;
2290 ret
= init_domain(node
, domain
);
2292 goto domain_init_error
;
2295 switch (domain
->type
) {
2296 case LTTNG_DOMAIN_KERNEL
:
2297 if (kernel_domain
) {
2298 /* Same domain seen twice, invalid! */
2299 goto domain_init_error
;
2301 kernel_domain
= domain
;
2303 case LTTNG_DOMAIN_UST
:
2305 /* Same domain seen twice, invalid! */
2306 goto domain_init_error
;
2308 ust_domain
= domain
;
2310 case LTTNG_DOMAIN_JUL
:
2312 /* Same domain seen twice, invalid! */
2313 goto domain_init_error
;
2315 jul_domain
= domain
;
2317 case LTTNG_DOMAIN_LOG4J
:
2319 /* Same domain seen twice, invalid! */
2320 goto domain_init_error
;
2322 log4j_domain
= domain
;
2325 WARN("Invalid domain type");
2326 goto domain_init_error
;
2331 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2336 /* Destroy session if it exists */
2337 ret
= lttng_destroy_session((const char *) name
);
2338 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2339 ERR("Failed to destroy existing session.");
2344 /* Create session type depending on output type */
2345 if (snapshot_mode
&& snapshot_mode
!= -1) {
2346 ret
= create_snapshot_session((const char *) name
, output_node
);
2347 } else if (live_timer_interval
&&
2348 live_timer_interval
!= UINT64_MAX
) {
2349 ret
= create_session((const char *) name
, kernel_domain
,
2350 ust_domain
, jul_domain
, log4j_domain
,
2351 output_node
, live_timer_interval
);
2353 /* regular session */
2354 ret
= create_session((const char *) name
, kernel_domain
,
2355 ust_domain
, jul_domain
, log4j_domain
,
2356 output_node
, UINT64_MAX
);
2362 for (node
= xmlFirstElementChild(domains_node
); node
;
2363 node
= xmlNextElementSibling(node
)) {
2364 ret
= process_domain_node(node
, (const char *) name
);
2371 ret
= lttng_start_tracing((const char *) name
);
2379 ERR("Failed to load session %s: %s", (const char *) name
,
2380 lttng_strerror(ret
));
2381 lttng_destroy_session((const char *) name
);
2385 free(kernel_domain
);
2394 * Return 1 if the given path is readable by the current UID or 0 if not.
2395 * Return -1 if the path is EPERM.
2397 static int validate_file_read_creds(const char *path
)
2403 /* Can we read the file. */
2404 ret
= access(path
, R_OK
);
2408 if (errno
== EACCES
) {
2419 int load_session_from_file(const char *path
, const char *session_name
,
2420 struct session_config_validation_ctx
*validation_ctx
, int override
)
2422 int ret
, session_found
= !session_name
;
2423 xmlDocPtr doc
= NULL
;
2424 xmlNodePtr sessions_node
;
2425 xmlNodePtr session_node
;
2428 assert(validation_ctx
);
2430 ret
= validate_file_read_creds(path
);
2433 ret
= -LTTNG_ERR_EPERM
;
2435 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2440 doc
= xmlParseFile(path
);
2442 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2446 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2448 ERR("Session configuration file validation failed");
2449 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2453 sessions_node
= xmlDocGetRootElement(doc
);
2454 if (!sessions_node
) {
2458 for (session_node
= xmlFirstElementChild(sessions_node
);
2459 session_node
; session_node
=
2460 xmlNextElementSibling(session_node
)) {
2461 ret
= process_session_node(session_node
,
2462 session_name
, override
);
2463 if (session_name
&& ret
== 0) {
2464 /* Target session found and loaded */
2472 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2477 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2479 struct dirent
*alloc_dirent(const char *path
)
2483 struct dirent
*entry
;
2485 name_max
= pathconf(path
, _PC_NAME_MAX
);
2486 if (name_max
== -1) {
2487 name_max
= PATH_MAX
;
2489 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2490 entry
= zmalloc(len
);
2495 int load_session_from_path(const char *path
, const char *session_name
,
2496 struct session_config_validation_ctx
*validation_ctx
, int override
)
2498 int ret
, session_found
= !session_name
;
2499 DIR *directory
= NULL
;
2502 assert(validation_ctx
);
2504 directory
= opendir(path
);
2508 /* Try the file loading. */
2511 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2514 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2519 struct dirent
*entry
;
2520 struct dirent
*result
;
2521 char *file_path
= NULL
;
2522 size_t path_len
= strlen(path
);
2524 if (path_len
>= PATH_MAX
) {
2525 ret
= -LTTNG_ERR_INVALID
;
2529 entry
= alloc_dirent(path
);
2531 ret
= -LTTNG_ERR_NOMEM
;
2535 file_path
= zmalloc(PATH_MAX
);
2537 ret
= -LTTNG_ERR_NOMEM
;
2542 strncpy(file_path
, path
, path_len
);
2543 if (file_path
[path_len
- 1] != '/') {
2544 file_path
[path_len
++] = '/';
2548 /* Search for *.lttng files */
2549 while (!readdir_r(directory
, entry
, &result
) && result
) {
2550 size_t file_name_len
= strlen(result
->d_name
);
2552 if (file_name_len
<=
2553 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2557 if (path_len
+ file_name_len
>= PATH_MAX
) {
2561 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2562 result
->d_name
+ file_name_len
- sizeof(
2563 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2567 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2568 file_path
[path_len
+ file_name_len
] = '\0';
2570 ret
= load_session_from_file(file_path
, session_name
,
2571 validation_ctx
, override
);
2572 if (session_name
&& !ret
) {
2581 ret
= load_session_from_file(path
, session_name
,
2582 validation_ctx
, override
);
2592 if (closedir(directory
)) {
2597 if (!session_found
) {
2598 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2605 * Validate that the given path's credentials and the current process have the
2606 * same UID. If so, return 1 else return 0 if it does NOT match.
2608 static int validate_path_creds(const char *path
)
2610 int ret
, uid
= getuid();
2619 ret
= stat(path
, &buf
);
2621 if (errno
!= ENOENT
) {
2624 ret
= -LTTNG_ERR_INVALID
;
2628 if (buf
.st_uid
!= uid
) {
2639 int config_load_session(const char *path
, const char *session_name
,
2640 int override
, unsigned int autoload
)
2643 const char *path_ptr
= NULL
;
2644 struct session_config_validation_ctx validation_ctx
= { 0 };
2646 ret
= init_session_config_validation_ctx(&validation_ctx
);
2653 const char *sys_path
;
2656 home_path
= utils_get_home_dir();
2658 char path
[PATH_MAX
];
2661 * Try user session configuration path. Ignore error here so we can
2662 * continue loading the system wide sessions.
2665 ret
= snprintf(path
, sizeof(path
),
2666 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2667 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2669 PERROR("snprintf session autoload home config path");
2674 * Credentials are only validated for the autoload in order to
2675 * avoid any user session daemon to try to load kernel sessions
2676 * automatically and failing all the times.
2678 ret
= validate_path_creds(path
);
2683 ret
= snprintf(path
, sizeof(path
),
2684 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2686 PERROR("snprintf session home config path");
2692 ret
= load_session_from_path(path_ptr
, session_name
,
2693 &validation_ctx
, override
);
2694 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2698 * Continue even if the session was found since we have to try
2699 * the system wide sessions.
2704 /* Reset path pointer for the system wide dir. */
2707 /* Try system wide configuration directory. */
2709 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2710 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2711 ret
= validate_path_creds(sys_path
);
2713 path_ptr
= sys_path
;
2716 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2717 path_ptr
= sys_path
;
2721 ret
= load_session_from_path(path_ptr
, session_name
,
2722 &validation_ctx
, override
);
2725 ret
= access(path
, F_OK
);
2730 ret
= -LTTNG_ERR_INVALID
;
2731 WARN("Session configuration path does not exist.");
2734 ret
= -LTTNG_ERR_EPERM
;
2737 ret
= -LTTNG_ERR_UNK
;
2743 ret
= load_session_from_path(path
, session_name
,
2744 &validation_ctx
, override
);
2747 fini_session_config_validation_ctx(&validation_ctx
);
2748 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2750 * Don't report an error if no sessions are found when called
2751 * without a session_name or a search path.