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.
28 #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 <common/compat/getenv.h>
36 #include <lttng/lttng-error.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlschemas.h>
40 #include <libxml/tree.h>
41 #include <lttng/lttng.h>
42 #include <lttng/snapshot.h>
45 #include "config-internal.h"
47 struct handler_filter_args
{
49 config_entry_handler_cb handler
;
53 struct session_config_validation_ctx
{
54 xmlSchemaParserCtxtPtr parser_ctx
;
56 xmlSchemaValidCtxtPtr schema_validation_ctx
;
59 const char * const config_str_yes
= "yes";
60 const char * const config_str_true
= "true";
61 const char * const config_str_on
= "on";
62 const char * const config_str_no
= "no";
63 const char * const config_str_false
= "false";
64 const char * const config_str_off
= "off";
65 const char * const config_xml_encoding
= "UTF-8";
66 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
67 const char * const config_xml_indent_string
= "\t";
68 const char * const config_xml_true
= "true";
69 const char * const config_xml_false
= "false";
71 const char * const config_element_channel
= "channel";
72 const char * const config_element_channels
= "channels";
73 const char * const config_element_domain
= "domain";
74 const char * const config_element_domains
= "domains";
75 const char * const config_element_event
= "event";
76 const char * const config_element_events
= "events";
77 const char * const config_element_context
= "context";
78 const char * const config_element_contexts
= "contexts";
79 const char * const config_element_attributes
= "attributes";
80 const char * const config_element_exclusion
= "exclusion";
81 const char * const config_element_exclusions
= "exclusions";
82 const char * const config_element_function_attributes
= "function_attributes";
83 const char * const config_element_probe_attributes
= "probe_attributes";
84 const char * const config_element_symbol_name
= "symbol_name";
85 const char * const config_element_address
= "address";
86 const char * const config_element_offset
= "offset";
87 const char * const config_element_name
= "name";
88 const char * const config_element_enabled
= "enabled";
89 const char * const config_element_overwrite_mode
= "overwrite_mode";
90 const char * const config_element_subbuf_size
= "subbuffer_size";
91 const char * const config_element_num_subbuf
= "subbuffer_count";
92 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
93 const char * const config_element_read_timer_interval
= "read_timer_interval";
94 const char * const config_element_output
= "output";
95 const char * const config_element_output_type
= "output_type";
96 const char * const config_element_tracefile_size
= "tracefile_size";
97 const char * const config_element_tracefile_count
= "tracefile_count";
98 const char * const config_element_live_timer_interval
= "live_timer_interval";
99 const char * const config_element_type
= "type";
100 const char * const config_element_buffer_type
= "buffer_type";
101 const char * const config_element_session
= "session";
102 const char * const config_element_sessions
= "sessions";
103 const char * const config_element_perf
= "perf";
104 const char * const config_element_config
= "config";
105 const char * const config_element_started
= "started";
106 const char * const config_element_snapshot_mode
= "snapshot_mode";
107 const char * const config_element_loglevel
= "loglevel";
108 const char * const config_element_loglevel_type
= "loglevel_type";
109 const char * const config_element_filter
= "filter";
110 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
111 const char * const config_element_consumer_output
= "consumer_output";
112 const char * const config_element_destination
= "destination";
113 const char * const config_element_path
= "path";
114 const char * const config_element_net_output
= "net_output";
115 const char * const config_element_control_uri
= "control_uri";
116 const char * const config_element_data_uri
= "data_uri";
117 const char * const config_element_max_size
= "max_size";
118 const char * const config_element_pid
= "pid";
119 const char * const config_element_pids
= "pids";
121 const char * const config_domain_type_kernel
= "KERNEL";
122 const char * const config_domain_type_ust
= "UST";
123 const char * const config_domain_type_jul
= "JUL";
124 const char * const config_domain_type_log4j
= "LOG4J";
125 const char * const config_domain_type_python
= "PYTHON";
127 const char * const config_buffer_type_per_pid
= "PER_PID";
128 const char * const config_buffer_type_per_uid
= "PER_UID";
129 const char * const config_buffer_type_global
= "GLOBAL";
131 const char * const config_overwrite_mode_discard
= "DISCARD";
132 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
134 const char * const config_output_type_splice
= "SPLICE";
135 const char * const config_output_type_mmap
= "MMAP";
137 const char * const config_loglevel_type_all
= "ALL";
138 const char * const config_loglevel_type_range
= "RANGE";
139 const char * const config_loglevel_type_single
= "SINGLE";
141 const char * const config_event_type_all
= "ALL";
142 const char * const config_event_type_tracepoint
= "TRACEPOINT";
143 const char * const config_event_type_probe
= "PROBE";
144 const char * const config_event_type_function
= "FUNCTION";
145 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
146 const char * const config_event_type_noop
= "NOOP";
147 const char * const config_event_type_syscall
= "SYSCALL";
148 const char * const config_event_type_kprobe
= "KPROBE";
149 const char * const config_event_type_kretprobe
= "KRETPROBE";
151 const char * const config_event_context_pid
= "PID";
152 const char * const config_event_context_procname
= "PROCNAME";
153 const char * const config_event_context_prio
= "PRIO";
154 const char * const config_event_context_nice
= "NICE";
155 const char * const config_event_context_vpid
= "VPID";
156 const char * const config_event_context_tid
= "TID";
157 const char * const config_event_context_vtid
= "VTID";
158 const char * const config_event_context_ppid
= "PPID";
159 const char * const config_event_context_vppid
= "VPPID";
160 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
161 const char * const config_event_context_hostname
= "HOSTNAME";
162 const char * const config_event_context_ip
= "IP";
163 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
165 struct consumer_output
{
172 static int config_entry_handler_filter(struct handler_filter_args
*args
,
173 const char *section
, const char *name
, const char *value
)
176 struct config_entry entry
= { section
, name
, value
};
180 if (!section
|| !name
|| !value
) {
186 if (strcmp(args
->section
, section
)) {
191 ret
= args
->handler(&entry
, args
->user_data
);
197 int config_get_section_entries(const char *override_path
, const char *section
,
198 config_entry_handler_cb handler
, void *user_data
)
202 FILE *config_file
= NULL
;
203 struct handler_filter_args filter
= { section
, handler
, user_data
};
205 /* First, try system-wide conf. file. */
206 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
208 config_file
= fopen(path
, "r");
210 DBG("Loading daemon conf file at %s", path
);
212 * Return value is not very important here since error or not, we
213 * continue and try the next possible conf. file.
215 (void) ini_parse_file(config_file
,
216 (ini_entry_handler
) config_entry_handler_filter
,
221 /* Second is the user local configuration. */
222 path
= utils_get_home_dir();
224 char fullpath
[PATH_MAX
];
226 ret
= snprintf(fullpath
, sizeof(fullpath
),
227 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
229 PERROR("snprintf user conf. path");
233 config_file
= fopen(fullpath
, "r");
235 DBG("Loading daemon user conf file at %s", path
);
237 * Return value is not very important here since error or not, we
238 * continue and try the next possible conf. file.
240 (void) ini_parse_file(config_file
,
241 (ini_entry_handler
) config_entry_handler_filter
,
247 /* Final path is the one that the user might have provided. */
249 config_file
= fopen(override_path
, "r");
251 DBG("Loading daemon command line conf file at %s", override_path
);
252 (void) ini_parse_file(config_file
,
253 (ini_entry_handler
) config_entry_handler_filter
,
257 ERR("Failed to open daemon configuration file at %s",
264 /* Everything went well. */
272 int config_parse_value(const char *value
)
275 char *endptr
, *lower_str
;
285 v
= strtoul(value
, &endptr
, 10);
286 if (endptr
!= value
) {
291 lower_str
= zmalloc(len
+ 1);
298 for (i
= 0; i
< len
; i
++) {
299 lower_str
[i
] = tolower(value
[i
]);
302 if (!strcmp(lower_str
, config_str_yes
) ||
303 !strcmp(lower_str
, config_str_true
) ||
304 !strcmp(lower_str
, config_str_on
)) {
306 } else if (!strcmp(lower_str
, config_str_no
) ||
307 !strcmp(lower_str
, config_str_false
) ||
308 !strcmp(lower_str
, config_str_off
)) {
320 * Returns a xmlChar string which must be released using xmlFree().
322 static xmlChar
*encode_string(const char *in_str
)
324 xmlChar
*out_str
= NULL
;
325 xmlCharEncodingHandlerPtr handler
;
326 int out_len
, ret
, in_len
;
330 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
332 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
336 in_len
= strlen(in_str
);
338 * Add 1 byte for the NULL terminted character. The factor 2 here is
339 * because UTF-8 can be on two bytes so this fits the worst case for each
342 out_len
= (in_len
* 2) + 1;
343 out_str
= xmlMalloc(out_len
);
348 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
355 /* out_len is now the size of out_str */
356 out_str
[out_len
] = '\0';
362 struct config_writer
*config_writer_create(int fd_output
, int indent
)
365 struct config_writer
*writer
;
366 xmlOutputBufferPtr buffer
;
368 writer
= zmalloc(sizeof(struct config_writer
));
370 PERROR("zmalloc config_writer_create");
374 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
379 writer
->writer
= xmlNewTextWriter(buffer
);
380 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
381 config_xml_encoding
, NULL
);
386 ret
= xmlTextWriterSetIndentString(writer
->writer
,
387 BAD_CAST config_xml_indent_string
);
392 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
400 config_writer_destroy(writer
);
405 int config_writer_destroy(struct config_writer
*writer
)
414 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
415 WARN("Could not close XML document");
419 if (writer
->writer
) {
420 xmlFreeTextWriter(writer
->writer
);
429 int config_writer_open_element(struct config_writer
*writer
,
430 const char *element_name
)
433 xmlChar
*encoded_element_name
;
435 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
440 encoded_element_name
= encode_string(element_name
);
441 if (!encoded_element_name
) {
446 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
447 xmlFree(encoded_element_name
);
449 return ret
> 0 ? 0 : ret
;
453 int config_writer_close_element(struct config_writer
*writer
)
457 if (!writer
|| !writer
->writer
) {
462 ret
= xmlTextWriterEndElement(writer
->writer
);
464 return ret
> 0 ? 0 : ret
;
468 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
469 const char *element_name
, uint64_t value
)
472 xmlChar
*encoded_element_name
;
474 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
479 encoded_element_name
= encode_string(element_name
);
480 if (!encoded_element_name
) {
485 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
486 encoded_element_name
, "%" PRIu64
, value
);
487 xmlFree(encoded_element_name
);
489 return ret
> 0 ? 0 : ret
;
493 int config_writer_write_element_signed_int(struct config_writer
*writer
,
494 const char *element_name
, int64_t value
)
497 xmlChar
*encoded_element_name
;
499 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
504 encoded_element_name
= encode_string(element_name
);
505 if (!encoded_element_name
) {
510 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
511 encoded_element_name
, "%" PRIi64
, value
);
512 xmlFree(encoded_element_name
);
514 return ret
> 0 ? 0 : ret
;
518 int config_writer_write_element_bool(struct config_writer
*writer
,
519 const char *element_name
, int value
)
521 return config_writer_write_element_string(writer
, element_name
,
522 value
? config_xml_true
: config_xml_false
);
526 int config_writer_write_element_string(struct config_writer
*writer
,
527 const char *element_name
, const char *value
)
530 xmlChar
*encoded_element_name
= NULL
;
531 xmlChar
*encoded_value
= NULL
;
533 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
539 encoded_element_name
= encode_string(element_name
);
540 if (!encoded_element_name
) {
545 encoded_value
= encode_string(value
);
546 if (!encoded_value
) {
551 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
554 xmlFree(encoded_element_name
);
555 xmlFree(encoded_value
);
556 return ret
> 0 ? 0 : ret
;
560 void xml_error_handler(void *ctx
, const char *format
, ...)
566 va_start(args
, format
);
567 ret
= vasprintf(&errMsg
, format
, args
);
570 ERR("String allocation failed in xml error handler");
574 fprintf(stderr
, "XML Error: %s", errMsg
);
579 void fini_session_config_validation_ctx(
580 struct session_config_validation_ctx
*ctx
)
582 if (ctx
->parser_ctx
) {
583 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
587 xmlSchemaFree(ctx
->schema
);
590 if (ctx
->schema_validation_ctx
) {
591 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
594 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
598 char *get_session_config_xsd_path()
601 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
602 size_t base_path_len
;
606 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
609 base_path_len
= strlen(base_path
);
610 max_path_len
= base_path_len
+
611 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
612 xsd_path
= zmalloc(max_path_len
);
617 strncpy(xsd_path
, base_path
, max_path_len
);
618 if (xsd_path
[base_path_len
- 1] != '/') {
619 xsd_path
[base_path_len
++] = '/';
622 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
623 max_path_len
- base_path_len
);
629 int init_session_config_validation_ctx(
630 struct session_config_validation_ctx
*ctx
)
633 char *xsd_path
= get_session_config_xsd_path();
636 ret
= -LTTNG_ERR_NOMEM
;
640 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
641 if (!ctx
->parser_ctx
) {
642 ERR("XSD parser context creation failed");
643 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
646 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
647 xml_error_handler
, NULL
);
649 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
651 ERR("XSD parsing failed");
652 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
656 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
657 if (!ctx
->schema_validation_ctx
) {
658 ERR("XSD validation context creation failed");
659 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
663 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
664 xml_error_handler
, NULL
);
669 fini_session_config_validation_ctx(ctx
);
677 int parse_uint(xmlChar
*str
, uint64_t *val
)
687 *val
= strtoull((const char *) str
, &endptr
, 10);
688 if (!endptr
|| *endptr
) {
699 int parse_int(xmlChar
*str
, int64_t *val
)
709 *val
= strtoll((const char *) str
, &endptr
, 10);
710 if (!endptr
|| *endptr
) {
721 int parse_bool(xmlChar
*str
, int *val
)
730 if (!strcmp((const char *) str
, config_xml_true
)) {
732 } else if (!strcmp((const char *) str
, config_xml_false
)) {
735 WARN("Invalid boolean value encoutered (%s).",
744 int get_domain_type(xmlChar
*domain
)
752 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
753 ret
= LTTNG_DOMAIN_KERNEL
;
754 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
755 ret
= LTTNG_DOMAIN_UST
;
756 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
757 ret
= LTTNG_DOMAIN_JUL
;
758 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
759 ret
= LTTNG_DOMAIN_LOG4J
;
760 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
761 ret
= LTTNG_DOMAIN_PYTHON
;
772 int get_buffer_type(xmlChar
*buffer_type
)
780 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
781 ret
= LTTNG_BUFFER_GLOBAL
;
782 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
783 ret
= LTTNG_BUFFER_PER_UID
;
784 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
785 ret
= LTTNG_BUFFER_PER_PID
;
796 int get_overwrite_mode(xmlChar
*overwrite_mode
)
800 if (!overwrite_mode
) {
804 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
806 } else if (!strcmp((char *) overwrite_mode
,
807 config_overwrite_mode_discard
)) {
819 int get_output_type(xmlChar
*output_type
)
827 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
828 ret
= LTTNG_EVENT_MMAP
;
829 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
830 ret
= LTTNG_EVENT_SPLICE
;
841 int get_event_type(xmlChar
*event_type
)
849 if (!strcmp((char *) event_type
, config_event_type_all
)) {
850 ret
= LTTNG_EVENT_ALL
;
851 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
852 ret
= LTTNG_EVENT_TRACEPOINT
;
853 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
854 ret
= LTTNG_EVENT_PROBE
;
855 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
856 ret
= LTTNG_EVENT_FUNCTION
;
857 } else if (!strcmp((char *) event_type
,
858 config_event_type_function_entry
)) {
859 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
860 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
861 ret
= LTTNG_EVENT_NOOP
;
862 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
863 ret
= LTTNG_EVENT_SYSCALL
;
874 int get_loglevel_type(xmlChar
*loglevel_type
)
878 if (!loglevel_type
) {
882 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
883 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
884 } else if (!strcmp((char *) loglevel_type
,
885 config_loglevel_type_range
)) {
886 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
887 } else if (!strcmp((char *) loglevel_type
,
888 config_loglevel_type_single
)) {
889 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
900 * Return the context type or -1 on error.
903 int get_context_type(xmlChar
*context_type
)
911 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
912 ret
= LTTNG_EVENT_CONTEXT_PID
;
913 } else if (!strcmp((char *) context_type
,
914 config_event_context_procname
)) {
915 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
916 } else if (!strcmp((char *) context_type
,
917 config_event_context_prio
)) {
918 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
919 } else if (!strcmp((char *) context_type
,
920 config_event_context_nice
)) {
921 ret
= LTTNG_EVENT_CONTEXT_NICE
;
922 } else if (!strcmp((char *) context_type
,
923 config_event_context_vpid
)) {
924 ret
= LTTNG_EVENT_CONTEXT_VPID
;
925 } else if (!strcmp((char *) context_type
,
926 config_event_context_tid
)) {
927 ret
= LTTNG_EVENT_CONTEXT_TID
;
928 } else if (!strcmp((char *) context_type
,
929 config_event_context_vtid
)) {
930 ret
= LTTNG_EVENT_CONTEXT_VTID
;
931 } else if (!strcmp((char *) context_type
,
932 config_event_context_ppid
)) {
933 ret
= LTTNG_EVENT_CONTEXT_PPID
;
934 } else if (!strcmp((char *) context_type
,
935 config_event_context_vppid
)) {
936 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
937 } else if (!strcmp((char *) context_type
,
938 config_event_context_pthread_id
)) {
939 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
940 } else if (!strcmp((char *) context_type
,
941 config_event_context_hostname
)) {
942 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
943 } else if (!strcmp((char *) context_type
,
944 config_event_context_ip
)) {
945 ret
= LTTNG_EVENT_CONTEXT_IP
;
956 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
961 for (node
= xmlFirstElementChild(domain_node
); node
;
962 node
= xmlNextElementSibling(node
)) {
963 if (!strcmp((const char *) node
->name
, config_element_type
)) {
965 xmlChar
*node_content
= xmlNodeGetContent(node
);
967 ret
= -LTTNG_ERR_NOMEM
;
971 ret
= get_domain_type(node_content
);
974 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
979 } else if (!strcmp((const char *) node
->name
,
980 config_element_buffer_type
)) {
982 xmlChar
*node_content
= xmlNodeGetContent(node
);
984 ret
= -LTTNG_ERR_NOMEM
;
988 ret
= get_buffer_type(node_content
);
991 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
995 domain
->buf_type
= ret
;
1004 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1009 for (node
= xmlFirstElementChild(net_output_node
); node
;
1010 node
= xmlNextElementSibling(node
)) {
1011 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1013 *control_uri
= (char *) xmlNodeGetContent(node
);
1014 if (!*control_uri
) {
1019 *data_uri
= (char *) xmlNodeGetContent(node
);
1026 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1030 int process_consumer_output(xmlNodePtr consumer_output_node
,
1031 struct consumer_output
*output
)
1038 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1039 node
= xmlNextElementSibling(node
)) {
1040 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1041 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1045 ret
= -LTTNG_ERR_NOMEM
;
1049 ret
= parse_bool(enabled_str
, &output
->enabled
);
1055 xmlNodePtr output_type_node
;
1058 output_type_node
= xmlFirstElementChild(node
);
1059 if (!output_type_node
) {
1060 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1064 if (!strcmp((const char *) output_type_node
->name
,
1065 config_element_path
)) {
1067 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1068 if (!output
->path
) {
1069 ret
= -LTTNG_ERR_NOMEM
;
1074 ret
= get_net_output_uris(output_type_node
,
1075 &output
->control_uri
, &output
->data_uri
);
1087 free(output
->control_uri
);
1088 free(output
->data_uri
);
1089 memset(output
, 0, sizeof(struct consumer_output
));
1095 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1096 const char *control_uri
, const char *data_uri
)
1099 struct lttng_handle
*handle
;
1100 const char *uri
= NULL
;
1105 handle
= lttng_create_handle(name
, domain
);
1107 ret
= -LTTNG_ERR_NOMEM
;
1111 if (!control_uri
|| !data_uri
) {
1112 uri
= control_uri
? control_uri
: data_uri
;
1117 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1118 lttng_destroy_handle(handle
);
1124 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1127 xmlNodePtr node
= NULL
;
1128 xmlNodePtr snapshot_output_list_node
;
1129 xmlNodePtr snapshot_output_node
;
1131 assert(session_name
);
1133 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1142 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1144 /* Parse and create snapshot outputs */
1146 for (snapshot_output_node
=
1147 xmlFirstElementChild(snapshot_output_list_node
);
1148 snapshot_output_node
; snapshot_output_node
=
1149 xmlNextElementSibling(snapshot_output_node
)) {
1151 uint64_t max_size
= UINT64_MAX
;
1152 struct consumer_output output
= { 0 };
1153 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1155 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1156 node
= xmlNextElementSibling(node
)) {
1157 if (!strcmp((const char *) node
->name
,
1158 config_element_name
)) {
1160 name
= (char *) xmlNodeGetContent(node
);
1162 ret
= -LTTNG_ERR_NOMEM
;
1163 goto error_snapshot_output
;
1165 } else if (!strcmp((const char *) node
->name
,
1166 config_element_max_size
)) {
1167 xmlChar
*content
= xmlNodeGetContent(node
);
1171 ret
= -LTTNG_ERR_NOMEM
;
1172 goto error_snapshot_output
;
1174 ret
= parse_uint(content
, &max_size
);
1177 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1178 goto error_snapshot_output
;
1181 /* consumer_output */
1182 ret
= process_consumer_output(node
, &output
);
1184 goto error_snapshot_output
;
1189 snapshot_output
= lttng_snapshot_output_create();
1190 if (!snapshot_output
) {
1191 ret
= -LTTNG_ERR_NOMEM
;
1192 goto error_snapshot_output
;
1195 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1197 goto error_snapshot_output
;
1200 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1202 goto error_snapshot_output
;
1206 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1209 goto error_snapshot_output
;
1212 if (output
.control_uri
) {
1213 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1216 goto error_snapshot_output
;
1220 if (output
.data_uri
) {
1221 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1224 goto error_snapshot_output
;
1229 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1230 error_snapshot_output
:
1233 free(output
.control_uri
);
1234 free(output
.data_uri
);
1235 lttng_snapshot_output_destroy(snapshot_output
);
1245 int create_session(const char *name
,
1246 struct lttng_domain
*kernel_domain
,
1247 struct lttng_domain
*ust_domain
,
1248 struct lttng_domain
*jul_domain
,
1249 struct lttng_domain
*log4j_domain
,
1250 xmlNodePtr output_node
,
1251 uint64_t live_timer_interval
)
1254 struct consumer_output output
= { 0 };
1255 xmlNodePtr consumer_output_node
;
1260 consumer_output_node
= xmlFirstElementChild(output_node
);
1261 if (!consumer_output_node
) {
1262 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1266 if (strcmp((const char *) consumer_output_node
->name
,
1267 config_element_consumer_output
)) {
1268 WARN("Invalid output type, expected %s node",
1269 config_element_consumer_output
);
1270 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1274 ret
= process_consumer_output(consumer_output_node
, &output
);
1280 if (live_timer_interval
!= UINT64_MAX
&&
1281 !output
.control_uri
&& !output
.data_uri
) {
1282 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1286 if (output
.control_uri
|| output
.data_uri
) {
1288 struct lttng_domain
*domain
;
1289 struct lttng_domain
*domains
[] =
1290 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1292 /* network destination */
1293 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1295 * URLs are provided for sure since the test above make sure that
1296 * with a live timer the data and control URIs are provided. So,
1297 * NULL is passed here and will be set right after.
1299 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1301 ret
= lttng_create_session(name
, NULL
);
1307 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1308 domain
= domains
[i
];
1313 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1320 /* either local output or no output */
1321 ret
= lttng_create_session(name
, output
.path
);
1328 free(output
.control_uri
);
1329 free(output
.data_uri
);
1333 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1334 struct lttng_event_probe_attr
*attr
)
1338 assert(probe_attribute_node
);
1341 if (!strcmp((const char *) probe_attribute_node
->name
,
1342 config_element_address
)) {
1347 content
= xmlNodeGetContent(probe_attribute_node
);
1349 ret
= -LTTNG_ERR_NOMEM
;
1353 ret
= parse_uint(content
, &addr
);
1356 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1361 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1362 config_element_offset
)) {
1364 uint64_t offset
= 0;
1367 content
= xmlNodeGetContent(probe_attribute_node
);
1369 ret
= -LTTNG_ERR_NOMEM
;
1373 ret
= parse_uint(content
, &offset
);
1376 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1380 attr
->offset
= offset
;
1381 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1382 config_element_symbol_name
)) {
1387 content
= xmlNodeGetContent(probe_attribute_node
);
1389 ret
= -LTTNG_ERR_NOMEM
;
1393 name_len
= strlen((char *) content
);
1394 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1395 WARN("symbol_name too long.");
1396 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1401 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1410 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1411 const char *channel_name
)
1415 struct lttng_event event
;
1416 char **exclusions
= NULL
;
1417 unsigned long exclusion_count
= 0;
1418 char *filter_expression
= NULL
;
1422 assert(channel_name
);
1424 memset(&event
, 0, sizeof(event
));
1426 for (node
= xmlFirstElementChild(event_node
); node
;
1427 node
= xmlNextElementSibling(node
)) {
1428 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1433 content
= xmlNodeGetContent(node
);
1435 ret
= -LTTNG_ERR_NOMEM
;
1439 name_len
= strlen((char *) content
);
1440 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1441 WARN("Channel name too long.");
1442 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1447 strncpy(event
.name
, (const char *) content
, name_len
);
1449 } else if (!strcmp((const char *) node
->name
,
1450 config_element_enabled
)) {
1451 xmlChar
*content
= xmlNodeGetContent(node
);
1455 ret
= -LTTNG_ERR_NOMEM
;
1459 ret
= parse_bool(content
, &event
.enabled
);
1462 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1465 } else if (!strcmp((const char *) node
->name
,
1466 config_element_type
)) {
1467 xmlChar
*content
= xmlNodeGetContent(node
);
1471 ret
= -LTTNG_ERR_NOMEM
;
1475 ret
= get_event_type(content
);
1478 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1483 } else if (!strcmp((const char *) node
->name
,
1484 config_element_loglevel_type
)) {
1485 xmlChar
*content
= xmlNodeGetContent(node
);
1489 ret
= -LTTNG_ERR_NOMEM
;
1493 ret
= get_loglevel_type(content
);
1496 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1500 event
.loglevel_type
= ret
;
1501 } else if (!strcmp((const char *) node
->name
,
1502 config_element_loglevel
)) {
1504 int64_t loglevel
= 0;
1507 content
= xmlNodeGetContent(node
);
1509 ret
= -LTTNG_ERR_NOMEM
;
1513 ret
= parse_int(content
, &loglevel
);
1516 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1520 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1521 WARN("loglevel out of range.");
1522 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1526 event
.loglevel
= loglevel
;
1527 } else if (!strcmp((const char *) node
->name
,
1528 config_element_filter
)) {
1530 xmlNodeGetContent(node
);
1534 ret
= -LTTNG_ERR_NOMEM
;
1538 filter_expression
= strdup((char *) content
);
1540 if (!filter_expression
) {
1541 ret
= -LTTNG_ERR_NOMEM
;
1544 } else if (!strcmp((const char *) node
->name
,
1545 config_element_exclusions
)) {
1546 xmlNodePtr exclusion_node
;
1547 int exclusion_index
= 0;
1552 * Exclusions has already been initialized,
1555 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1559 exclusion_count
= xmlChildElementCount(node
);
1560 if (!exclusion_count
) {
1564 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1566 exclusion_count
= 0;
1567 ret
= -LTTNG_ERR_NOMEM
;
1571 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1572 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1574 xmlNodeGetContent(exclusion_node
);
1577 ret
= -LTTNG_ERR_NOMEM
;
1581 exclusions
[exclusion_index
] = strdup((const char *) content
);
1583 if (!exclusions
[exclusion_index
]) {
1584 ret
= -LTTNG_ERR_NOMEM
;
1590 event
.exclusion
= 1;
1591 } else if (!strcmp((const char *) node
->name
,
1592 config_element_attributes
)) {
1593 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1596 if (!attribute_node
) {
1597 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1601 if (!strcmp((const char *) node
->name
,
1602 config_element_probe_attributes
)) {
1603 xmlNodePtr probe_attribute_node
;
1605 /* probe_attributes */
1606 for (probe_attribute_node
=
1607 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1608 probe_attribute_node
= xmlNextElementSibling(
1609 probe_attribute_node
)) {
1611 ret
= process_probe_attribute_node(probe_attribute_node
,
1620 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1622 /* function_attributes */
1623 content
= xmlNodeGetContent(symbol_node
);
1625 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1629 sym_len
= strlen((char *) content
);
1630 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1631 WARN("Function name too long.");
1632 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1637 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1644 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1645 filter_expression
, exclusion_count
, exclusions
);
1650 if (!event
.enabled
) {
1652 * Note that we should use lttng_disable_event_ext() (2.6+) to
1653 * eliminate the risk of clashing on events of the same
1654 * name (with different event types and loglevels).
1656 * Unfortunately, lttng_disable_event_ext() only performs a
1657 * match on the name and event type and errors out if any other
1658 * event attribute is not set to its default value.
1660 * This will disable all events that match this name.
1662 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1665 for (i
= 0; i
< exclusion_count
; i
++) {
1666 free(exclusions
[i
]);
1670 free(filter_expression
);
1675 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1676 const char *channel_name
)
1681 assert(events_node
);
1683 assert(channel_name
);
1685 for (node
= xmlFirstElementChild(events_node
); node
;
1686 node
= xmlNextElementSibling(node
)) {
1687 ret
= process_event_node(node
, handle
, channel_name
);
1697 int process_channel_attr_node(xmlNodePtr attr_node
,
1698 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1699 xmlNodePtr
*events_node
)
1705 assert(contexts_node
);
1706 assert(events_node
);
1708 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1713 content
= xmlNodeGetContent(attr_node
);
1715 ret
= -LTTNG_ERR_NOMEM
;
1719 name_len
= strlen((char *) content
);
1720 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1721 WARN("Channel name too long.");
1722 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1727 strncpy(channel
->name
, (const char *) content
, name_len
);
1729 } else if (!strcmp((const char *) attr_node
->name
,
1730 config_element_enabled
)) {
1735 content
= xmlNodeGetContent(attr_node
);
1737 ret
= -LTTNG_ERR_NOMEM
;
1741 ret
= parse_bool(content
, &enabled
);
1744 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1748 channel
->enabled
= enabled
;
1749 } else if (!strcmp((const char *) attr_node
->name
,
1750 config_element_overwrite_mode
)) {
1753 /* overwrite_mode */
1754 content
= xmlNodeGetContent(attr_node
);
1756 ret
= -LTTNG_ERR_NOMEM
;
1760 ret
= get_overwrite_mode(content
);
1763 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1767 channel
->attr
.overwrite
= ret
;
1768 } else if (!strcmp((const char *) attr_node
->name
,
1769 config_element_subbuf_size
)) {
1772 /* subbuffer_size */
1773 content
= xmlNodeGetContent(attr_node
);
1775 ret
= -LTTNG_ERR_NOMEM
;
1779 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1782 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1785 } else if (!strcmp((const char *) attr_node
->name
,
1786 config_element_num_subbuf
)) {
1789 /* subbuffer_count */
1790 content
= xmlNodeGetContent(attr_node
);
1792 ret
= -LTTNG_ERR_NOMEM
;
1796 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1799 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1802 } else if (!strcmp((const char *) attr_node
->name
,
1803 config_element_switch_timer_interval
)) {
1805 uint64_t switch_timer_interval
= 0;
1807 /* switch_timer_interval */
1808 content
= xmlNodeGetContent(attr_node
);
1810 ret
= -LTTNG_ERR_NOMEM
;
1814 ret
= parse_uint(content
, &switch_timer_interval
);
1817 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1821 if (switch_timer_interval
> UINT_MAX
) {
1822 WARN("switch_timer_interval out of range.");
1823 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1827 channel
->attr
.switch_timer_interval
=
1828 switch_timer_interval
;
1829 } else if (!strcmp((const char *) attr_node
->name
,
1830 config_element_read_timer_interval
)) {
1832 uint64_t read_timer_interval
= 0;
1834 /* read_timer_interval */
1835 content
= xmlNodeGetContent(attr_node
);
1837 ret
= -LTTNG_ERR_NOMEM
;
1841 ret
= parse_uint(content
, &read_timer_interval
);
1844 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1848 if (read_timer_interval
> UINT_MAX
) {
1849 WARN("read_timer_interval out of range.");
1850 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1854 channel
->attr
.read_timer_interval
=
1855 read_timer_interval
;
1856 } else if (!strcmp((const char *) attr_node
->name
,
1857 config_element_output_type
)) {
1861 content
= xmlNodeGetContent(attr_node
);
1863 ret
= -LTTNG_ERR_NOMEM
;
1867 ret
= get_output_type(content
);
1870 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1874 channel
->attr
.output
= ret
;
1875 } else if (!strcmp((const char *) attr_node
->name
,
1876 config_element_tracefile_size
)) {
1879 /* tracefile_size */
1880 content
= xmlNodeGetContent(attr_node
);
1882 ret
= -LTTNG_ERR_NOMEM
;
1886 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1889 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1892 } else if (!strcmp((const char *) attr_node
->name
,
1893 config_element_tracefile_count
)) {
1896 /* tracefile_count */
1897 content
= xmlNodeGetContent(attr_node
);
1899 ret
= -LTTNG_ERR_NOMEM
;
1903 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1906 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1909 } else if (!strcmp((const char *) attr_node
->name
,
1910 config_element_live_timer_interval
)) {
1912 uint64_t live_timer_interval
= 0;
1914 /* live_timer_interval */
1915 content
= xmlNodeGetContent(attr_node
);
1917 ret
= -LTTNG_ERR_NOMEM
;
1921 ret
= parse_uint(content
, &live_timer_interval
);
1924 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1928 if (live_timer_interval
> UINT_MAX
) {
1929 WARN("live_timer_interval out of range.");
1930 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1934 channel
->attr
.live_timer_interval
=
1935 live_timer_interval
;
1936 } else if (!strcmp((const char *) attr_node
->name
,
1937 config_element_events
)) {
1939 *events_node
= attr_node
;
1942 *contexts_node
= attr_node
;
1950 int process_context_node(xmlNodePtr context_node
,
1951 struct lttng_handle
*handle
, const char *channel_name
)
1954 struct lttng_event_context context
;
1955 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1958 assert(channel_name
);
1960 if (!context_child_node
) {
1961 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1965 memset(&context
, 0, sizeof(context
));
1967 if (!strcmp((const char *) context_child_node
->name
,
1968 config_element_type
)) {
1970 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1972 ret
= -LTTNG_ERR_NOMEM
;
1976 ret
= get_context_type(content
);
1979 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1985 xmlNodePtr perf_attr_node
;
1988 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1989 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1990 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1991 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1992 perf_attr_node
; perf_attr_node
=
1993 xmlNextElementSibling(perf_attr_node
)) {
1994 if (!strcmp((const char *) perf_attr_node
->name
,
1995 config_element_type
)) {
2000 content
= xmlNodeGetContent(perf_attr_node
);
2002 ret
= -LTTNG_ERR_NOMEM
;
2006 ret
= parse_uint(content
, &type
);
2009 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2013 if (type
> UINT32_MAX
) {
2014 WARN("perf context type out of range.");
2015 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2019 context
.u
.perf_counter
.type
= type
;
2020 } else if (!strcmp((const char *) perf_attr_node
->name
,
2021 config_element_config
)) {
2023 uint64_t config
= 0;
2026 content
= xmlNodeGetContent(perf_attr_node
);
2028 ret
= -LTTNG_ERR_NOMEM
;
2032 ret
= parse_uint(content
, &config
);
2035 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2039 context
.u
.perf_counter
.config
= config
;
2040 } else if (!strcmp((const char *) perf_attr_node
->name
,
2041 config_element_name
)) {
2046 content
= xmlNodeGetContent(perf_attr_node
);
2048 ret
= -LTTNG_ERR_NOMEM
;
2052 name_len
= strlen((char *) content
);
2053 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2054 WARN("perf context name too long.");
2055 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2060 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2067 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2073 int process_contexts_node(xmlNodePtr contexts_node
,
2074 struct lttng_handle
*handle
, const char *channel_name
)
2077 xmlNodePtr context_node
;
2079 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2080 context_node
= xmlNextElementSibling(context_node
)) {
2081 ret
= process_context_node(context_node
, handle
, channel_name
);
2091 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2094 struct lttng_domain domain
= { 0 };
2095 struct lttng_handle
*handle
= NULL
;
2096 xmlNodePtr channels_node
= NULL
;
2099 assert(session_name
);
2101 ret
= init_domain(domain_node
, &domain
);
2106 handle
= lttng_create_handle(session_name
, &domain
);
2108 ret
= -LTTNG_ERR_NOMEM
;
2112 /* get the channels node */
2113 for (node
= xmlFirstElementChild(domain_node
); node
;
2114 node
= xmlNextElementSibling(node
)) {
2115 if (!strcmp((const char *) node
->name
,
2116 config_element_channels
)) {
2117 channels_node
= node
;
2122 if (!channels_node
) {
2126 /* create all channels */
2127 for (node
= xmlFirstElementChild(channels_node
); node
;
2128 node
= xmlNextElementSibling(node
)) {
2129 struct lttng_channel channel
;
2130 xmlNodePtr contexts_node
= NULL
;
2131 xmlNodePtr events_node
= NULL
;
2132 xmlNodePtr channel_attr_node
;
2134 memset(&channel
, 0, sizeof(channel
));
2135 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2137 for (channel_attr_node
= xmlFirstElementChild(node
);
2138 channel_attr_node
; channel_attr_node
=
2139 xmlNextElementSibling(channel_attr_node
)) {
2140 ret
= process_channel_attr_node(channel_attr_node
,
2141 &channel
, &contexts_node
, &events_node
);
2147 ret
= lttng_enable_channel(handle
, &channel
);
2152 ret
= process_events_node(events_node
, handle
, channel
.name
);
2157 ret
= process_contexts_node(contexts_node
, handle
,
2164 lttng_destroy_handle(handle
);
2169 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2172 int ret
, started
= -1, snapshot_mode
= -1;
2173 uint64_t live_timer_interval
= UINT64_MAX
;
2175 xmlNodePtr domains_node
= NULL
;
2176 xmlNodePtr output_node
= NULL
;
2178 struct lttng_domain
*kernel_domain
= NULL
;
2179 struct lttng_domain
*ust_domain
= NULL
;
2180 struct lttng_domain
*jul_domain
= NULL
;
2181 struct lttng_domain
*log4j_domain
= NULL
;
2182 struct lttng_domain
*python_domain
= NULL
;
2184 for (node
= xmlFirstElementChild(session_node
); node
;
2185 node
= xmlNextElementSibling(node
)) {
2186 if (!name
&& !strcmp((const char *) node
->name
,
2187 config_element_name
)) {
2189 xmlChar
*node_content
= xmlNodeGetContent(node
);
2190 if (!node_content
) {
2191 ret
= -LTTNG_ERR_NOMEM
;
2195 name
= (char *) node_content
;
2196 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2197 config_element_domains
)) {
2199 domains_node
= node
;
2200 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2201 config_element_started
)) {
2203 xmlChar
*node_content
= xmlNodeGetContent(node
);
2204 if (!node_content
) {
2205 ret
= -LTTNG_ERR_NOMEM
;
2209 ret
= parse_bool(node_content
, &started
);
2212 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2215 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2216 config_element_output
)) {
2220 /* attributes, snapshot_mode or live_timer_interval */
2221 xmlNodePtr attributes_child
=
2222 xmlFirstElementChild(node
);
2224 if (!strcmp((const char *) attributes_child
->name
,
2225 config_element_snapshot_mode
)) {
2227 xmlChar
*snapshot_mode_content
=
2228 xmlNodeGetContent(attributes_child
);
2229 if (!snapshot_mode_content
) {
2230 ret
= -LTTNG_ERR_NOMEM
;
2234 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2235 free(snapshot_mode_content
);
2237 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2241 /* live_timer_interval */
2242 xmlChar
*timer_interval_content
=
2243 xmlNodeGetContent(attributes_child
);
2244 if (!timer_interval_content
) {
2245 ret
= -LTTNG_ERR_NOMEM
;
2249 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2250 free(timer_interval_content
);
2252 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2260 /* Mandatory attribute, as defined in the session XSD */
2261 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2265 if (session_name
&& strcmp(name
, session_name
)) {
2266 /* This is not the session we are looking for */
2267 ret
= -LTTNG_ERR_NO_SESSION
;
2271 /* Init domains to create the session handles */
2272 for (node
= xmlFirstElementChild(domains_node
); node
;
2273 node
= xmlNextElementSibling(node
)) {
2274 struct lttng_domain
*domain
;
2276 domain
= zmalloc(sizeof(*domain
));
2278 ret
= -LTTNG_ERR_NOMEM
;
2282 ret
= init_domain(node
, domain
);
2284 goto domain_init_error
;
2287 switch (domain
->type
) {
2288 case LTTNG_DOMAIN_KERNEL
:
2289 if (kernel_domain
) {
2290 /* Same domain seen twice, invalid! */
2291 goto domain_init_error
;
2293 kernel_domain
= domain
;
2295 case LTTNG_DOMAIN_UST
:
2297 /* Same domain seen twice, invalid! */
2298 goto domain_init_error
;
2300 ust_domain
= domain
;
2302 case LTTNG_DOMAIN_JUL
:
2304 /* Same domain seen twice, invalid! */
2305 goto domain_init_error
;
2307 jul_domain
= domain
;
2309 case LTTNG_DOMAIN_LOG4J
:
2311 /* Same domain seen twice, invalid! */
2312 goto domain_init_error
;
2314 log4j_domain
= domain
;
2316 case LTTNG_DOMAIN_PYTHON
:
2317 if (python_domain
) {
2318 /* Same domain seen twice, invalid! */
2319 goto domain_init_error
;
2321 python_domain
= domain
;
2324 WARN("Invalid domain type");
2325 goto domain_init_error
;
2330 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2335 /* Destroy session if it exists */
2336 ret
= lttng_destroy_session(name
);
2337 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2338 ERR("Failed to destroy existing session.");
2343 /* Create session type depending on output type */
2344 if (snapshot_mode
&& snapshot_mode
!= -1) {
2345 ret
= create_snapshot_session(name
, output_node
);
2346 } else if (live_timer_interval
&&
2347 live_timer_interval
!= UINT64_MAX
) {
2348 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2349 log4j_domain
, output_node
, live_timer_interval
);
2351 /* regular session */
2352 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2353 log4j_domain
, output_node
, UINT64_MAX
);
2359 for (node
= xmlFirstElementChild(domains_node
); node
;
2360 node
= xmlNextElementSibling(node
)) {
2361 ret
= process_domain_node(node
, name
);
2368 ret
= lttng_start_tracing(name
);
2376 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2377 lttng_destroy_session(name
);
2381 free(kernel_domain
);
2385 free(python_domain
);
2391 * Return 1 if the given path is readable by the current UID or 0 if not.
2392 * Return -1 if the path is EPERM.
2394 static int validate_file_read_creds(const char *path
)
2400 /* Can we read the file. */
2401 ret
= access(path
, R_OK
);
2405 if (errno
== EACCES
) {
2416 int load_session_from_file(const char *path
, const char *session_name
,
2417 struct session_config_validation_ctx
*validation_ctx
, int override
)
2419 int ret
, session_found
= !session_name
;
2420 xmlDocPtr doc
= NULL
;
2421 xmlNodePtr sessions_node
;
2422 xmlNodePtr session_node
;
2425 assert(validation_ctx
);
2427 ret
= validate_file_read_creds(path
);
2430 ret
= -LTTNG_ERR_EPERM
;
2432 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2437 doc
= xmlParseFile(path
);
2439 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2443 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2445 ERR("Session configuration file validation failed");
2446 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2450 sessions_node
= xmlDocGetRootElement(doc
);
2451 if (!sessions_node
) {
2455 for (session_node
= xmlFirstElementChild(sessions_node
);
2456 session_node
; session_node
=
2457 xmlNextElementSibling(session_node
)) {
2458 ret
= process_session_node(session_node
,
2459 session_name
, override
);
2460 if (session_name
&& ret
== 0) {
2461 /* Target session found and loaded */
2469 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2475 int load_session_from_path(const char *path
, const char *session_name
,
2476 struct session_config_validation_ctx
*validation_ctx
, int override
)
2478 int ret
, session_found
= !session_name
;
2479 DIR *directory
= NULL
;
2482 assert(validation_ctx
);
2484 directory
= opendir(path
);
2488 /* Try the file loading. */
2491 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2494 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2499 struct dirent
*entry
;
2500 struct dirent
*result
;
2501 char *file_path
= NULL
;
2502 size_t path_len
= strlen(path
);
2504 if (path_len
>= PATH_MAX
) {
2505 ret
= -LTTNG_ERR_INVALID
;
2509 entry
= zmalloc(sizeof(*entry
));
2511 ret
= -LTTNG_ERR_NOMEM
;
2515 file_path
= zmalloc(PATH_MAX
);
2517 ret
= -LTTNG_ERR_NOMEM
;
2522 strncpy(file_path
, path
, path_len
);
2523 if (file_path
[path_len
- 1] != '/') {
2524 file_path
[path_len
++] = '/';
2528 /* Search for *.lttng files */
2529 while (!readdir_r(directory
, entry
, &result
) && result
) {
2530 size_t file_name_len
= strlen(result
->d_name
);
2532 if (file_name_len
<=
2533 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2537 if (path_len
+ file_name_len
>= PATH_MAX
) {
2541 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2542 result
->d_name
+ file_name_len
- sizeof(
2543 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2547 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2548 file_path
[path_len
+ file_name_len
] = '\0';
2550 ret
= load_session_from_file(file_path
, session_name
,
2551 validation_ctx
, override
);
2552 if (session_name
&& !ret
) {
2561 ret
= load_session_from_file(path
, session_name
,
2562 validation_ctx
, override
);
2572 if (closedir(directory
)) {
2577 if (!session_found
) {
2578 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2585 * Validate that the given path's credentials and the current process have the
2586 * same UID. If so, return 1 else return 0 if it does NOT match.
2588 static int validate_path_creds(const char *path
)
2590 int ret
, uid
= getuid();
2599 ret
= stat(path
, &buf
);
2601 if (errno
!= ENOENT
) {
2604 ret
= -LTTNG_ERR_INVALID
;
2608 if (buf
.st_uid
!= uid
) {
2619 int config_load_session(const char *path
, const char *session_name
,
2620 int override
, unsigned int autoload
)
2623 const char *path_ptr
= NULL
;
2624 struct session_config_validation_ctx validation_ctx
= { 0 };
2626 ret
= init_session_config_validation_ctx(&validation_ctx
);
2633 const char *sys_path
;
2636 home_path
= utils_get_home_dir();
2638 char path
[PATH_MAX
];
2641 * Try user session configuration path. Ignore error here so we can
2642 * continue loading the system wide sessions.
2645 ret
= snprintf(path
, sizeof(path
),
2646 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2647 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2649 PERROR("snprintf session autoload home config path");
2654 * Credentials are only validated for the autoload in order to
2655 * avoid any user session daemon to try to load kernel sessions
2656 * automatically and failing all the times.
2658 ret
= validate_path_creds(path
);
2663 ret
= snprintf(path
, sizeof(path
),
2664 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2666 PERROR("snprintf session home config path");
2672 ret
= load_session_from_path(path_ptr
, session_name
,
2673 &validation_ctx
, override
);
2674 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2678 * Continue even if the session was found since we have to try
2679 * the system wide sessions.
2684 /* Reset path pointer for the system wide dir. */
2687 /* Try system wide configuration directory. */
2689 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2690 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2691 ret
= validate_path_creds(sys_path
);
2693 path_ptr
= sys_path
;
2696 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2697 path_ptr
= sys_path
;
2701 ret
= load_session_from_path(path_ptr
, session_name
,
2702 &validation_ctx
, override
);
2705 ret
= access(path
, F_OK
);
2710 ret
= -LTTNG_ERR_INVALID
;
2711 WARN("Session configuration path does not exist.");
2714 ret
= -LTTNG_ERR_EPERM
;
2717 ret
= -LTTNG_ERR_UNK
;
2723 ret
= load_session_from_path(path
, session_name
,
2724 &validation_ctx
, override
);
2727 fini_session_config_validation_ctx(&validation_ctx
);
2728 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2730 * Don't report an error if no sessions are found when called
2731 * without a session_name or a search path.