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>
32 #include <common/defaults.h>
33 #include <common/error.h>
34 #include <common/macros.h>
35 #include <common/utils.h>
36 #include <common/compat/getenv.h>
37 #include <lttng/lttng-error.h>
38 #include <libxml/parser.h>
39 #include <libxml/valid.h>
40 #include <libxml/xmlschemas.h>
41 #include <libxml/tree.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.h>
46 #include "config-internal.h"
48 struct handler_filter_args
{
50 config_entry_handler_cb handler
;
54 struct session_config_validation_ctx
{
55 xmlSchemaParserCtxtPtr parser_ctx
;
57 xmlSchemaValidCtxtPtr schema_validation_ctx
;
60 const char * const config_str_yes
= "yes";
61 const char * const config_str_true
= "true";
62 const char * const config_str_on
= "on";
63 const char * const config_str_no
= "no";
64 const char * const config_str_false
= "false";
65 const char * const config_str_off
= "off";
66 const char * const config_xml_encoding
= "UTF-8";
67 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
68 const char * const config_xml_indent_string
= "\t";
69 const char * const config_xml_true
= "true";
70 const char * const config_xml_false
= "false";
72 const char * const config_element_channel
= "channel";
73 const char * const config_element_channels
= "channels";
74 const char * const config_element_domain
= "domain";
75 const char * const config_element_domains
= "domains";
76 const char * const config_element_event
= "event";
77 const char * const config_element_events
= "events";
78 const char * const config_element_context
= "context";
79 const char * const config_element_contexts
= "contexts";
80 const char * const config_element_attributes
= "attributes";
81 const char * const config_element_exclusion
= "exclusion";
82 const char * const config_element_exclusions
= "exclusions";
83 const char * const config_element_function_attributes
= "function_attributes";
84 const char * const config_element_probe_attributes
= "probe_attributes";
85 const char * const config_element_symbol_name
= "symbol_name";
86 const char * const config_element_address
= "address";
87 const char * const config_element_offset
= "offset";
88 const char * const config_element_name
= "name";
89 const char * const config_element_enabled
= "enabled";
90 const char * const config_element_overwrite_mode
= "overwrite_mode";
91 const char * const config_element_subbuf_size
= "subbuffer_size";
92 const char * const config_element_num_subbuf
= "subbuffer_count";
93 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
94 const char * const config_element_read_timer_interval
= "read_timer_interval";
95 const char * const config_element_output
= "output";
96 const char * const config_element_output_type
= "output_type";
97 const char * const config_element_tracefile_size
= "tracefile_size";
98 const char * const config_element_tracefile_count
= "tracefile_count";
99 const char * const config_element_live_timer_interval
= "live_timer_interval";
100 const char * const config_element_type
= "type";
101 const char * const config_element_buffer_type
= "buffer_type";
102 const char * const config_element_session
= "session";
103 const char * const config_element_sessions
= "sessions";
104 const char * const config_element_perf
= "perf";
105 const char * const config_element_config
= "config";
106 const char * const config_element_started
= "started";
107 const char * const config_element_snapshot_mode
= "snapshot_mode";
108 const char * const config_element_loglevel
= "loglevel";
109 const char * const config_element_loglevel_type
= "loglevel_type";
110 const char * const config_element_filter
= "filter";
111 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
112 const char * const config_element_consumer_output
= "consumer_output";
113 const char * const config_element_destination
= "destination";
114 const char * const config_element_path
= "path";
115 const char * const config_element_net_output
= "net_output";
116 const char * const config_element_control_uri
= "control_uri";
117 const char * const config_element_data_uri
= "data_uri";
118 const char * const config_element_max_size
= "max_size";
119 const char * const config_element_pid
= "pid";
120 const char * const config_element_pids
= "pids";
121 const char * const config_element_shared_memory_path
= "shared_memory_path";
122 const char * const config_element_pid_tracker
= "pid_tracker";
123 const char * const config_element_trackers
= "trackers";
124 const char * const config_element_targets
= "targets";
125 const char * const config_element_target_pid
= "pid_target";
127 const char * const config_domain_type_kernel
= "KERNEL";
128 const char * const config_domain_type_ust
= "UST";
129 const char * const config_domain_type_jul
= "JUL";
130 const char * const config_domain_type_log4j
= "LOG4J";
131 const char * const config_domain_type_python
= "PYTHON";
133 const char * const config_buffer_type_per_pid
= "PER_PID";
134 const char * const config_buffer_type_per_uid
= "PER_UID";
135 const char * const config_buffer_type_global
= "GLOBAL";
137 const char * const config_overwrite_mode_discard
= "DISCARD";
138 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
140 const char * const config_output_type_splice
= "SPLICE";
141 const char * const config_output_type_mmap
= "MMAP";
143 const char * const config_loglevel_type_all
= "ALL";
144 const char * const config_loglevel_type_range
= "RANGE";
145 const char * const config_loglevel_type_single
= "SINGLE";
147 const char * const config_event_type_all
= "ALL";
148 const char * const config_event_type_tracepoint
= "TRACEPOINT";
149 const char * const config_event_type_probe
= "PROBE";
150 const char * const config_event_type_function
= "FUNCTION";
151 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
152 const char * const config_event_type_noop
= "NOOP";
153 const char * const config_event_type_syscall
= "SYSCALL";
154 const char * const config_event_type_kprobe
= "KPROBE";
155 const char * const config_event_type_kretprobe
= "KRETPROBE";
157 const char * const config_event_context_pid
= "PID";
158 const char * const config_event_context_procname
= "PROCNAME";
159 const char * const config_event_context_prio
= "PRIO";
160 const char * const config_event_context_nice
= "NICE";
161 const char * const config_event_context_vpid
= "VPID";
162 const char * const config_event_context_tid
= "TID";
163 const char * const config_event_context_vtid
= "VTID";
164 const char * const config_event_context_ppid
= "PPID";
165 const char * const config_event_context_vppid
= "VPPID";
166 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
167 const char * const config_event_context_hostname
= "HOSTNAME";
168 const char * const config_event_context_ip
= "IP";
169 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
171 struct consumer_output
{
178 static int config_entry_handler_filter(struct handler_filter_args
*args
,
179 const char *section
, const char *name
, const char *value
)
182 struct config_entry entry
= { section
, name
, value
};
186 if (!section
|| !name
|| !value
) {
192 if (strcmp(args
->section
, section
)) {
197 ret
= args
->handler(&entry
, args
->user_data
);
203 int config_get_section_entries(const char *override_path
, const char *section
,
204 config_entry_handler_cb handler
, void *user_data
)
208 FILE *config_file
= NULL
;
209 struct handler_filter_args filter
= { section
, handler
, user_data
};
211 /* First, try system-wide conf. file. */
212 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
214 config_file
= fopen(path
, "r");
216 DBG("Loading daemon conf file at %s", path
);
218 * Return value is not very important here since error or not, we
219 * continue and try the next possible conf. file.
221 (void) ini_parse_file(config_file
,
222 (ini_entry_handler
) config_entry_handler_filter
,
227 /* Second is the user local configuration. */
228 path
= utils_get_home_dir();
230 char fullpath
[PATH_MAX
];
232 ret
= snprintf(fullpath
, sizeof(fullpath
),
233 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
235 PERROR("snprintf user conf. path");
239 config_file
= fopen(fullpath
, "r");
241 DBG("Loading daemon user conf file at %s", path
);
243 * Return value is not very important here since error or not, we
244 * continue and try the next possible conf. file.
246 (void) ini_parse_file(config_file
,
247 (ini_entry_handler
) config_entry_handler_filter
,
253 /* Final path is the one that the user might have provided. */
255 config_file
= fopen(override_path
, "r");
257 DBG("Loading daemon command line conf file at %s", override_path
);
258 (void) ini_parse_file(config_file
,
259 (ini_entry_handler
) config_entry_handler_filter
,
263 ERR("Failed to open daemon configuration file at %s",
270 /* Everything went well. */
278 int config_parse_value(const char *value
)
281 char *endptr
, *lower_str
;
291 v
= strtoul(value
, &endptr
, 10);
292 if (endptr
!= value
) {
297 lower_str
= zmalloc(len
+ 1);
304 for (i
= 0; i
< len
; i
++) {
305 lower_str
[i
] = tolower(value
[i
]);
308 if (!strcmp(lower_str
, config_str_yes
) ||
309 !strcmp(lower_str
, config_str_true
) ||
310 !strcmp(lower_str
, config_str_on
)) {
312 } else if (!strcmp(lower_str
, config_str_no
) ||
313 !strcmp(lower_str
, config_str_false
) ||
314 !strcmp(lower_str
, config_str_off
)) {
326 * Returns a xmlChar string which must be released using xmlFree().
328 static xmlChar
*encode_string(const char *in_str
)
330 xmlChar
*out_str
= NULL
;
331 xmlCharEncodingHandlerPtr handler
;
332 int out_len
, ret
, in_len
;
336 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
338 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
342 in_len
= strlen(in_str
);
344 * Add 1 byte for the NULL terminted character. The factor 4 here is
345 * used because UTF-8 characters can take up to 4 bytes.
347 out_len
= (in_len
* 4) + 1;
348 out_str
= xmlMalloc(out_len
);
353 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
360 /* out_len is now the size of out_str */
361 out_str
[out_len
] = '\0';
367 struct config_writer
*config_writer_create(int fd_output
, int indent
)
370 struct config_writer
*writer
;
371 xmlOutputBufferPtr buffer
;
373 writer
= zmalloc(sizeof(struct config_writer
));
375 PERROR("zmalloc config_writer_create");
379 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
384 writer
->writer
= xmlNewTextWriter(buffer
);
385 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
386 config_xml_encoding
, NULL
);
391 ret
= xmlTextWriterSetIndentString(writer
->writer
,
392 BAD_CAST config_xml_indent_string
);
397 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
405 config_writer_destroy(writer
);
410 int config_writer_destroy(struct config_writer
*writer
)
419 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
420 WARN("Could not close XML document");
424 if (writer
->writer
) {
425 xmlFreeTextWriter(writer
->writer
);
434 int config_writer_open_element(struct config_writer
*writer
,
435 const char *element_name
)
438 xmlChar
*encoded_element_name
;
440 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
445 encoded_element_name
= encode_string(element_name
);
446 if (!encoded_element_name
) {
451 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
452 xmlFree(encoded_element_name
);
454 return ret
> 0 ? 0 : ret
;
458 int config_writer_close_element(struct config_writer
*writer
)
462 if (!writer
|| !writer
->writer
) {
467 ret
= xmlTextWriterEndElement(writer
->writer
);
469 return ret
> 0 ? 0 : ret
;
473 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
474 const char *element_name
, uint64_t value
)
477 xmlChar
*encoded_element_name
;
479 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
484 encoded_element_name
= encode_string(element_name
);
485 if (!encoded_element_name
) {
490 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
491 encoded_element_name
, "%" PRIu64
, value
);
492 xmlFree(encoded_element_name
);
494 return ret
> 0 ? 0 : ret
;
498 int config_writer_write_element_signed_int(struct config_writer
*writer
,
499 const char *element_name
, int64_t value
)
502 xmlChar
*encoded_element_name
;
504 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
509 encoded_element_name
= encode_string(element_name
);
510 if (!encoded_element_name
) {
515 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
516 encoded_element_name
, "%" PRIi64
, value
);
517 xmlFree(encoded_element_name
);
519 return ret
> 0 ? 0 : ret
;
523 int config_writer_write_element_bool(struct config_writer
*writer
,
524 const char *element_name
, int value
)
526 return config_writer_write_element_string(writer
, element_name
,
527 value
? config_xml_true
: config_xml_false
);
531 int config_writer_write_element_string(struct config_writer
*writer
,
532 const char *element_name
, const char *value
)
535 xmlChar
*encoded_element_name
= NULL
;
536 xmlChar
*encoded_value
= NULL
;
538 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
544 encoded_element_name
= encode_string(element_name
);
545 if (!encoded_element_name
) {
550 encoded_value
= encode_string(value
);
551 if (!encoded_value
) {
556 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
559 xmlFree(encoded_element_name
);
560 xmlFree(encoded_value
);
561 return ret
> 0 ? 0 : ret
;
565 void xml_error_handler(void *ctx
, const char *format
, ...)
571 va_start(args
, format
);
572 ret
= vasprintf(&errMsg
, format
, args
);
575 ERR("String allocation failed in xml error handler");
579 fprintf(stderr
, "XML Error: %s", errMsg
);
584 void fini_session_config_validation_ctx(
585 struct session_config_validation_ctx
*ctx
)
587 if (ctx
->parser_ctx
) {
588 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
592 xmlSchemaFree(ctx
->schema
);
595 if (ctx
->schema_validation_ctx
) {
596 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
599 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
603 char *get_session_config_xsd_path()
606 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
607 size_t base_path_len
;
611 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
614 base_path_len
= strlen(base_path
);
615 max_path_len
= base_path_len
+
616 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
617 xsd_path
= zmalloc(max_path_len
);
622 strncpy(xsd_path
, base_path
, max_path_len
);
623 if (xsd_path
[base_path_len
- 1] != '/') {
624 xsd_path
[base_path_len
++] = '/';
627 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
628 max_path_len
- base_path_len
);
634 int init_session_config_validation_ctx(
635 struct session_config_validation_ctx
*ctx
)
638 char *xsd_path
= get_session_config_xsd_path();
641 ret
= -LTTNG_ERR_NOMEM
;
645 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
646 if (!ctx
->parser_ctx
) {
647 ERR("XSD parser context creation failed");
648 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
651 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
652 xml_error_handler
, NULL
);
654 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
656 ERR("XSD parsing failed");
657 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
661 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
662 if (!ctx
->schema_validation_ctx
) {
663 ERR("XSD validation context creation failed");
664 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
668 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
669 xml_error_handler
, NULL
);
674 fini_session_config_validation_ctx(ctx
);
682 int parse_uint(xmlChar
*str
, uint64_t *val
)
692 *val
= strtoull((const char *) str
, &endptr
, 10);
693 if (!endptr
|| *endptr
) {
704 int parse_int(xmlChar
*str
, int64_t *val
)
714 *val
= strtoll((const char *) str
, &endptr
, 10);
715 if (!endptr
|| *endptr
) {
726 int parse_bool(xmlChar
*str
, int *val
)
735 if (!strcmp((const char *) str
, config_xml_true
)) {
737 } else if (!strcmp((const char *) str
, config_xml_false
)) {
740 WARN("Invalid boolean value encoutered (%s).",
749 int get_domain_type(xmlChar
*domain
)
757 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
758 ret
= LTTNG_DOMAIN_KERNEL
;
759 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
760 ret
= LTTNG_DOMAIN_UST
;
761 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
762 ret
= LTTNG_DOMAIN_JUL
;
763 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
764 ret
= LTTNG_DOMAIN_LOG4J
;
765 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
766 ret
= LTTNG_DOMAIN_PYTHON
;
777 int get_buffer_type(xmlChar
*buffer_type
)
785 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
786 ret
= LTTNG_BUFFER_GLOBAL
;
787 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
788 ret
= LTTNG_BUFFER_PER_UID
;
789 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
790 ret
= LTTNG_BUFFER_PER_PID
;
801 int get_overwrite_mode(xmlChar
*overwrite_mode
)
805 if (!overwrite_mode
) {
809 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
811 } else if (!strcmp((char *) overwrite_mode
,
812 config_overwrite_mode_discard
)) {
824 int get_output_type(xmlChar
*output_type
)
832 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
833 ret
= LTTNG_EVENT_MMAP
;
834 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
835 ret
= LTTNG_EVENT_SPLICE
;
846 int get_event_type(xmlChar
*event_type
)
854 if (!strcmp((char *) event_type
, config_event_type_all
)) {
855 ret
= LTTNG_EVENT_ALL
;
856 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
857 ret
= LTTNG_EVENT_TRACEPOINT
;
858 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
859 ret
= LTTNG_EVENT_PROBE
;
860 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
861 ret
= LTTNG_EVENT_FUNCTION
;
862 } else if (!strcmp((char *) event_type
,
863 config_event_type_function_entry
)) {
864 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
865 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
866 ret
= LTTNG_EVENT_NOOP
;
867 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
868 ret
= LTTNG_EVENT_SYSCALL
;
879 int get_loglevel_type(xmlChar
*loglevel_type
)
883 if (!loglevel_type
) {
887 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
888 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
889 } else if (!strcmp((char *) loglevel_type
,
890 config_loglevel_type_range
)) {
891 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
892 } else if (!strcmp((char *) loglevel_type
,
893 config_loglevel_type_single
)) {
894 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
905 * Return the context type or -1 on error.
908 int get_context_type(xmlChar
*context_type
)
916 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
917 ret
= LTTNG_EVENT_CONTEXT_PID
;
918 } else if (!strcmp((char *) context_type
,
919 config_event_context_procname
)) {
920 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
921 } else if (!strcmp((char *) context_type
,
922 config_event_context_prio
)) {
923 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
924 } else if (!strcmp((char *) context_type
,
925 config_event_context_nice
)) {
926 ret
= LTTNG_EVENT_CONTEXT_NICE
;
927 } else if (!strcmp((char *) context_type
,
928 config_event_context_vpid
)) {
929 ret
= LTTNG_EVENT_CONTEXT_VPID
;
930 } else if (!strcmp((char *) context_type
,
931 config_event_context_tid
)) {
932 ret
= LTTNG_EVENT_CONTEXT_TID
;
933 } else if (!strcmp((char *) context_type
,
934 config_event_context_vtid
)) {
935 ret
= LTTNG_EVENT_CONTEXT_VTID
;
936 } else if (!strcmp((char *) context_type
,
937 config_event_context_ppid
)) {
938 ret
= LTTNG_EVENT_CONTEXT_PPID
;
939 } else if (!strcmp((char *) context_type
,
940 config_event_context_vppid
)) {
941 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
942 } else if (!strcmp((char *) context_type
,
943 config_event_context_pthread_id
)) {
944 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
945 } else if (!strcmp((char *) context_type
,
946 config_event_context_hostname
)) {
947 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
948 } else if (!strcmp((char *) context_type
,
949 config_event_context_ip
)) {
950 ret
= LTTNG_EVENT_CONTEXT_IP
;
961 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
966 for (node
= xmlFirstElementChild(domain_node
); node
;
967 node
= xmlNextElementSibling(node
)) {
968 if (!strcmp((const char *) node
->name
, config_element_type
)) {
970 xmlChar
*node_content
= xmlNodeGetContent(node
);
972 ret
= -LTTNG_ERR_NOMEM
;
976 ret
= get_domain_type(node_content
);
979 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
984 } else if (!strcmp((const char *) node
->name
,
985 config_element_buffer_type
)) {
987 xmlChar
*node_content
= xmlNodeGetContent(node
);
989 ret
= -LTTNG_ERR_NOMEM
;
993 ret
= get_buffer_type(node_content
);
996 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1000 domain
->buf_type
= ret
;
1009 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1014 for (node
= xmlFirstElementChild(net_output_node
); node
;
1015 node
= xmlNextElementSibling(node
)) {
1016 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1018 *control_uri
= (char *) xmlNodeGetContent(node
);
1019 if (!*control_uri
) {
1024 *data_uri
= (char *) xmlNodeGetContent(node
);
1031 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1035 int process_consumer_output(xmlNodePtr consumer_output_node
,
1036 struct consumer_output
*output
)
1043 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1044 node
= xmlNextElementSibling(node
)) {
1045 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1046 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1050 ret
= -LTTNG_ERR_NOMEM
;
1054 ret
= parse_bool(enabled_str
, &output
->enabled
);
1060 xmlNodePtr output_type_node
;
1063 output_type_node
= xmlFirstElementChild(node
);
1064 if (!output_type_node
) {
1065 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1069 if (!strcmp((const char *) output_type_node
->name
,
1070 config_element_path
)) {
1072 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1073 if (!output
->path
) {
1074 ret
= -LTTNG_ERR_NOMEM
;
1079 ret
= get_net_output_uris(output_type_node
,
1080 &output
->control_uri
, &output
->data_uri
);
1092 free(output
->control_uri
);
1093 free(output
->data_uri
);
1094 memset(output
, 0, sizeof(struct consumer_output
));
1100 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1101 const char *control_uri
, const char *data_uri
)
1104 struct lttng_handle
*handle
;
1105 const char *uri
= NULL
;
1110 handle
= lttng_create_handle(name
, domain
);
1112 ret
= -LTTNG_ERR_NOMEM
;
1116 if (!control_uri
|| !data_uri
) {
1117 uri
= control_uri
? control_uri
: data_uri
;
1122 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1123 lttng_destroy_handle(handle
);
1129 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1132 xmlNodePtr node
= NULL
;
1133 xmlNodePtr snapshot_output_list_node
;
1134 xmlNodePtr snapshot_output_node
;
1136 assert(session_name
);
1138 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1147 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1149 /* Parse and create snapshot outputs */
1151 for (snapshot_output_node
=
1152 xmlFirstElementChild(snapshot_output_list_node
);
1153 snapshot_output_node
; snapshot_output_node
=
1154 xmlNextElementSibling(snapshot_output_node
)) {
1156 uint64_t max_size
= UINT64_MAX
;
1157 struct consumer_output output
= { 0 };
1158 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1160 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1161 node
= xmlNextElementSibling(node
)) {
1162 if (!strcmp((const char *) node
->name
,
1163 config_element_name
)) {
1165 name
= (char *) xmlNodeGetContent(node
);
1167 ret
= -LTTNG_ERR_NOMEM
;
1168 goto error_snapshot_output
;
1170 } else if (!strcmp((const char *) node
->name
,
1171 config_element_max_size
)) {
1172 xmlChar
*content
= xmlNodeGetContent(node
);
1176 ret
= -LTTNG_ERR_NOMEM
;
1177 goto error_snapshot_output
;
1179 ret
= parse_uint(content
, &max_size
);
1182 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1183 goto error_snapshot_output
;
1186 /* consumer_output */
1187 ret
= process_consumer_output(node
, &output
);
1189 goto error_snapshot_output
;
1194 snapshot_output
= lttng_snapshot_output_create();
1195 if (!snapshot_output
) {
1196 ret
= -LTTNG_ERR_NOMEM
;
1197 goto error_snapshot_output
;
1200 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1202 goto error_snapshot_output
;
1205 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1207 goto error_snapshot_output
;
1211 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1214 goto error_snapshot_output
;
1217 if (output
.control_uri
) {
1218 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1221 goto error_snapshot_output
;
1225 if (output
.data_uri
) {
1226 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1229 goto error_snapshot_output
;
1234 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1235 error_snapshot_output
:
1238 free(output
.control_uri
);
1239 free(output
.data_uri
);
1240 lttng_snapshot_output_destroy(snapshot_output
);
1250 int create_session(const char *name
,
1251 struct lttng_domain
*kernel_domain
,
1252 struct lttng_domain
*ust_domain
,
1253 struct lttng_domain
*jul_domain
,
1254 struct lttng_domain
*log4j_domain
,
1255 xmlNodePtr output_node
,
1256 uint64_t live_timer_interval
)
1259 struct consumer_output output
= { 0 };
1260 xmlNodePtr consumer_output_node
;
1265 consumer_output_node
= xmlFirstElementChild(output_node
);
1266 if (!consumer_output_node
) {
1267 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1271 if (strcmp((const char *) consumer_output_node
->name
,
1272 config_element_consumer_output
)) {
1273 WARN("Invalid output type, expected %s node",
1274 config_element_consumer_output
);
1275 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1279 ret
= process_consumer_output(consumer_output_node
, &output
);
1285 if (live_timer_interval
!= UINT64_MAX
&&
1286 !output
.control_uri
&& !output
.data_uri
) {
1287 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1291 if (output
.control_uri
|| output
.data_uri
) {
1293 struct lttng_domain
*domain
;
1294 struct lttng_domain
*domains
[] =
1295 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1297 /* network destination */
1298 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1300 * URLs are provided for sure since the test above make sure that
1301 * with a live timer the data and control URIs are provided. So,
1302 * NULL is passed here and will be set right after.
1304 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1306 ret
= lttng_create_session(name
, NULL
);
1312 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1313 domain
= domains
[i
];
1318 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1325 /* either local output or no output */
1326 ret
= lttng_create_session(name
, output
.path
);
1333 free(output
.control_uri
);
1334 free(output
.data_uri
);
1338 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1339 struct lttng_event_probe_attr
*attr
)
1343 assert(probe_attribute_node
);
1346 if (!strcmp((const char *) probe_attribute_node
->name
,
1347 config_element_address
)) {
1352 content
= xmlNodeGetContent(probe_attribute_node
);
1354 ret
= -LTTNG_ERR_NOMEM
;
1358 ret
= parse_uint(content
, &addr
);
1361 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1366 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1367 config_element_offset
)) {
1369 uint64_t offset
= 0;
1372 content
= xmlNodeGetContent(probe_attribute_node
);
1374 ret
= -LTTNG_ERR_NOMEM
;
1378 ret
= parse_uint(content
, &offset
);
1381 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1385 attr
->offset
= offset
;
1386 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1387 config_element_symbol_name
)) {
1392 content
= xmlNodeGetContent(probe_attribute_node
);
1394 ret
= -LTTNG_ERR_NOMEM
;
1398 name_len
= strlen((char *) content
);
1399 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1400 WARN("symbol_name too long.");
1401 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1406 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1415 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1416 const char *channel_name
)
1420 struct lttng_event event
;
1421 char **exclusions
= NULL
;
1422 unsigned long exclusion_count
= 0;
1423 char *filter_expression
= NULL
;
1427 assert(channel_name
);
1429 memset(&event
, 0, sizeof(event
));
1431 /* Initialize default log level which varies by domain */
1432 switch (handle
->domain
.type
)
1434 case LTTNG_DOMAIN_JUL
:
1435 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1437 case LTTNG_DOMAIN_LOG4J
:
1438 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1440 case LTTNG_DOMAIN_PYTHON
:
1441 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1443 case LTTNG_DOMAIN_UST
:
1444 case LTTNG_DOMAIN_KERNEL
:
1445 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1451 for (node
= xmlFirstElementChild(event_node
); node
;
1452 node
= xmlNextElementSibling(node
)) {
1453 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1458 content
= xmlNodeGetContent(node
);
1460 ret
= -LTTNG_ERR_NOMEM
;
1464 name_len
= strlen((char *) content
);
1465 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1466 WARN("Channel name too long.");
1467 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1472 strncpy(event
.name
, (const char *) content
, name_len
);
1474 } else if (!strcmp((const char *) node
->name
,
1475 config_element_enabled
)) {
1476 xmlChar
*content
= xmlNodeGetContent(node
);
1480 ret
= -LTTNG_ERR_NOMEM
;
1484 ret
= parse_bool(content
, &event
.enabled
);
1487 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1490 } else if (!strcmp((const char *) node
->name
,
1491 config_element_type
)) {
1492 xmlChar
*content
= xmlNodeGetContent(node
);
1496 ret
= -LTTNG_ERR_NOMEM
;
1500 ret
= get_event_type(content
);
1503 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1508 } else if (!strcmp((const char *) node
->name
,
1509 config_element_loglevel_type
)) {
1510 xmlChar
*content
= xmlNodeGetContent(node
);
1514 ret
= -LTTNG_ERR_NOMEM
;
1518 ret
= get_loglevel_type(content
);
1521 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1525 event
.loglevel_type
= ret
;
1526 } else if (!strcmp((const char *) node
->name
,
1527 config_element_loglevel
)) {
1529 int64_t loglevel
= 0;
1532 content
= xmlNodeGetContent(node
);
1534 ret
= -LTTNG_ERR_NOMEM
;
1538 ret
= parse_int(content
, &loglevel
);
1541 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1545 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1546 WARN("loglevel out of range.");
1547 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1551 event
.loglevel
= loglevel
;
1552 } else if (!strcmp((const char *) node
->name
,
1553 config_element_filter
)) {
1555 xmlNodeGetContent(node
);
1559 ret
= -LTTNG_ERR_NOMEM
;
1563 filter_expression
= strdup((char *) content
);
1565 if (!filter_expression
) {
1566 ret
= -LTTNG_ERR_NOMEM
;
1569 } else if (!strcmp((const char *) node
->name
,
1570 config_element_exclusions
)) {
1571 xmlNodePtr exclusion_node
;
1572 int exclusion_index
= 0;
1577 * Exclusions has already been initialized,
1580 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1584 exclusion_count
= xmlChildElementCount(node
);
1585 if (!exclusion_count
) {
1589 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1591 exclusion_count
= 0;
1592 ret
= -LTTNG_ERR_NOMEM
;
1596 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1597 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1599 xmlNodeGetContent(exclusion_node
);
1602 ret
= -LTTNG_ERR_NOMEM
;
1606 exclusions
[exclusion_index
] = strdup((const char *) content
);
1608 if (!exclusions
[exclusion_index
]) {
1609 ret
= -LTTNG_ERR_NOMEM
;
1615 event
.exclusion
= 1;
1616 } else if (!strcmp((const char *) node
->name
,
1617 config_element_attributes
)) {
1618 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1621 if (!attribute_node
) {
1622 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1626 if (!strcmp((const char *) node
->name
,
1627 config_element_probe_attributes
)) {
1628 xmlNodePtr probe_attribute_node
;
1630 /* probe_attributes */
1631 for (probe_attribute_node
=
1632 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1633 probe_attribute_node
= xmlNextElementSibling(
1634 probe_attribute_node
)) {
1636 ret
= process_probe_attribute_node(probe_attribute_node
,
1645 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1647 /* function_attributes */
1648 content
= xmlNodeGetContent(symbol_node
);
1650 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1654 sym_len
= strlen((char *) content
);
1655 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1656 WARN("Function name too long.");
1657 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1662 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1669 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1670 filter_expression
, exclusion_count
, exclusions
);
1675 if (!event
.enabled
) {
1677 * Note that we should use lttng_disable_event_ext() (2.6+) to
1678 * eliminate the risk of clashing on events of the same
1679 * name (with different event types and loglevels).
1681 * Unfortunately, lttng_disable_event_ext() only performs a
1682 * match on the name and event type and errors out if any other
1683 * event attribute is not set to its default value.
1685 * This will disable all events that match this name.
1687 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1690 for (i
= 0; i
< exclusion_count
; i
++) {
1691 free(exclusions
[i
]);
1695 free(filter_expression
);
1700 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1701 const char *channel_name
)
1706 assert(events_node
);
1708 assert(channel_name
);
1710 for (node
= xmlFirstElementChild(events_node
); node
;
1711 node
= xmlNextElementSibling(node
)) {
1712 ret
= process_event_node(node
, handle
, channel_name
);
1722 int process_channel_attr_node(xmlNodePtr attr_node
,
1723 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1724 xmlNodePtr
*events_node
)
1730 assert(contexts_node
);
1731 assert(events_node
);
1733 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1738 content
= xmlNodeGetContent(attr_node
);
1740 ret
= -LTTNG_ERR_NOMEM
;
1744 name_len
= strlen((char *) content
);
1745 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1746 WARN("Channel name too long.");
1747 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1752 strncpy(channel
->name
, (const char *) content
, name_len
);
1754 } else if (!strcmp((const char *) attr_node
->name
,
1755 config_element_enabled
)) {
1760 content
= xmlNodeGetContent(attr_node
);
1762 ret
= -LTTNG_ERR_NOMEM
;
1766 ret
= parse_bool(content
, &enabled
);
1769 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1773 channel
->enabled
= enabled
;
1774 } else if (!strcmp((const char *) attr_node
->name
,
1775 config_element_overwrite_mode
)) {
1778 /* overwrite_mode */
1779 content
= xmlNodeGetContent(attr_node
);
1781 ret
= -LTTNG_ERR_NOMEM
;
1785 ret
= get_overwrite_mode(content
);
1788 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1792 channel
->attr
.overwrite
= ret
;
1793 } else if (!strcmp((const char *) attr_node
->name
,
1794 config_element_subbuf_size
)) {
1797 /* subbuffer_size */
1798 content
= xmlNodeGetContent(attr_node
);
1800 ret
= -LTTNG_ERR_NOMEM
;
1804 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1807 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1810 } else if (!strcmp((const char *) attr_node
->name
,
1811 config_element_num_subbuf
)) {
1814 /* subbuffer_count */
1815 content
= xmlNodeGetContent(attr_node
);
1817 ret
= -LTTNG_ERR_NOMEM
;
1821 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1824 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1827 } else if (!strcmp((const char *) attr_node
->name
,
1828 config_element_switch_timer_interval
)) {
1830 uint64_t switch_timer_interval
= 0;
1832 /* switch_timer_interval */
1833 content
= xmlNodeGetContent(attr_node
);
1835 ret
= -LTTNG_ERR_NOMEM
;
1839 ret
= parse_uint(content
, &switch_timer_interval
);
1842 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1846 if (switch_timer_interval
> UINT_MAX
) {
1847 WARN("switch_timer_interval out of range.");
1848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 channel
->attr
.switch_timer_interval
=
1853 switch_timer_interval
;
1854 } else if (!strcmp((const char *) attr_node
->name
,
1855 config_element_read_timer_interval
)) {
1857 uint64_t read_timer_interval
= 0;
1859 /* read_timer_interval */
1860 content
= xmlNodeGetContent(attr_node
);
1862 ret
= -LTTNG_ERR_NOMEM
;
1866 ret
= parse_uint(content
, &read_timer_interval
);
1869 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1873 if (read_timer_interval
> UINT_MAX
) {
1874 WARN("read_timer_interval out of range.");
1875 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1879 channel
->attr
.read_timer_interval
=
1880 read_timer_interval
;
1881 } else if (!strcmp((const char *) attr_node
->name
,
1882 config_element_output_type
)) {
1886 content
= xmlNodeGetContent(attr_node
);
1888 ret
= -LTTNG_ERR_NOMEM
;
1892 ret
= get_output_type(content
);
1895 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1899 channel
->attr
.output
= ret
;
1900 } else if (!strcmp((const char *) attr_node
->name
,
1901 config_element_tracefile_size
)) {
1904 /* tracefile_size */
1905 content
= xmlNodeGetContent(attr_node
);
1907 ret
= -LTTNG_ERR_NOMEM
;
1911 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1914 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1917 } else if (!strcmp((const char *) attr_node
->name
,
1918 config_element_tracefile_count
)) {
1921 /* tracefile_count */
1922 content
= xmlNodeGetContent(attr_node
);
1924 ret
= -LTTNG_ERR_NOMEM
;
1928 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1931 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1934 } else if (!strcmp((const char *) attr_node
->name
,
1935 config_element_live_timer_interval
)) {
1937 uint64_t live_timer_interval
= 0;
1939 /* live_timer_interval */
1940 content
= xmlNodeGetContent(attr_node
);
1942 ret
= -LTTNG_ERR_NOMEM
;
1946 ret
= parse_uint(content
, &live_timer_interval
);
1949 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1953 if (live_timer_interval
> UINT_MAX
) {
1954 WARN("live_timer_interval out of range.");
1955 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1959 channel
->attr
.live_timer_interval
=
1960 live_timer_interval
;
1961 } else if (!strcmp((const char *) attr_node
->name
,
1962 config_element_events
)) {
1964 *events_node
= attr_node
;
1967 *contexts_node
= attr_node
;
1975 int process_context_node(xmlNodePtr context_node
,
1976 struct lttng_handle
*handle
, const char *channel_name
)
1979 struct lttng_event_context context
;
1980 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1983 assert(channel_name
);
1985 if (!context_child_node
) {
1986 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1990 memset(&context
, 0, sizeof(context
));
1992 if (!strcmp((const char *) context_child_node
->name
,
1993 config_element_type
)) {
1995 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1997 ret
= -LTTNG_ERR_NOMEM
;
2001 ret
= get_context_type(content
);
2004 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2010 xmlNodePtr perf_attr_node
;
2013 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2014 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2015 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2016 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2017 perf_attr_node
; perf_attr_node
=
2018 xmlNextElementSibling(perf_attr_node
)) {
2019 if (!strcmp((const char *) perf_attr_node
->name
,
2020 config_element_type
)) {
2025 content
= xmlNodeGetContent(perf_attr_node
);
2027 ret
= -LTTNG_ERR_NOMEM
;
2031 ret
= parse_uint(content
, &type
);
2034 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2038 if (type
> UINT32_MAX
) {
2039 WARN("perf context type out of range.");
2040 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2044 context
.u
.perf_counter
.type
= type
;
2045 } else if (!strcmp((const char *) perf_attr_node
->name
,
2046 config_element_config
)) {
2048 uint64_t config
= 0;
2051 content
= xmlNodeGetContent(perf_attr_node
);
2053 ret
= -LTTNG_ERR_NOMEM
;
2057 ret
= parse_uint(content
, &config
);
2060 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2064 context
.u
.perf_counter
.config
= config
;
2065 } else if (!strcmp((const char *) perf_attr_node
->name
,
2066 config_element_name
)) {
2071 content
= xmlNodeGetContent(perf_attr_node
);
2073 ret
= -LTTNG_ERR_NOMEM
;
2077 name_len
= strlen((char *) content
);
2078 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2079 WARN("perf context name too long.");
2080 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2085 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2092 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2098 int process_contexts_node(xmlNodePtr contexts_node
,
2099 struct lttng_handle
*handle
, const char *channel_name
)
2102 xmlNodePtr context_node
;
2104 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2105 context_node
= xmlNextElementSibling(context_node
)) {
2106 ret
= process_context_node(context_node
, handle
, channel_name
);
2116 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2117 struct lttng_handle
*handle
)
2120 xmlNodePtr targets_node
= NULL
;
2124 assert(pid_tracker_node
);
2125 /* get the targets node */
2126 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2127 node
= xmlNextElementSibling(node
)) {
2128 if (!strcmp((const char *) node
->name
,
2129 config_element_targets
)) {
2130 targets_node
= node
;
2135 if (!targets_node
) {
2136 ret
= LTTNG_ERR_INVALID
;
2140 /* Go through all pid_target node */
2141 child
= xmlChildElementCount(targets_node
);
2143 /* The session is explicitly set to target nothing. */
2144 ret
= lttng_untrack_pid(handle
, -1);
2149 for (node
= xmlFirstElementChild(targets_node
); node
;
2150 node
= xmlNextElementSibling(node
)) {
2151 xmlNodePtr pid_target_node
= node
;
2153 /* get pid node and track it */
2154 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2155 node
= xmlNextElementSibling(node
)) {
2156 if (!strcmp((const char *) node
->name
,
2157 config_element_pid
)) {
2159 xmlChar
*content
= NULL
;
2161 content
= xmlNodeGetContent(node
);
2163 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2167 ret
= parse_int(content
, &pid
);
2170 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2174 ret
= lttng_track_pid(handle
, (int) pid
);
2180 node
= pid_target_node
;
2189 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2192 struct lttng_domain domain
= { 0 };
2193 struct lttng_handle
*handle
= NULL
;
2194 xmlNodePtr channels_node
= NULL
;
2195 xmlNodePtr trackers_node
= NULL
;
2196 xmlNodePtr pid_tracker_node
= NULL
;
2199 assert(session_name
);
2201 ret
= init_domain(domain_node
, &domain
);
2206 handle
= lttng_create_handle(session_name
, &domain
);
2208 ret
= -LTTNG_ERR_NOMEM
;
2212 /* get the channels node */
2213 for (node
= xmlFirstElementChild(domain_node
); node
;
2214 node
= xmlNextElementSibling(node
)) {
2215 if (!strcmp((const char *) node
->name
,
2216 config_element_channels
)) {
2217 channels_node
= node
;
2222 if (!channels_node
) {
2226 /* create all channels */
2227 for (node
= xmlFirstElementChild(channels_node
); node
;
2228 node
= xmlNextElementSibling(node
)) {
2229 struct lttng_channel channel
;
2230 xmlNodePtr contexts_node
= NULL
;
2231 xmlNodePtr events_node
= NULL
;
2232 xmlNodePtr channel_attr_node
;
2234 memset(&channel
, 0, sizeof(channel
));
2235 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2237 for (channel_attr_node
= xmlFirstElementChild(node
);
2238 channel_attr_node
; channel_attr_node
=
2239 xmlNextElementSibling(channel_attr_node
)) {
2240 ret
= process_channel_attr_node(channel_attr_node
,
2241 &channel
, &contexts_node
, &events_node
);
2247 ret
= lttng_enable_channel(handle
, &channel
);
2252 ret
= process_events_node(events_node
, handle
, channel
.name
);
2257 ret
= process_contexts_node(contexts_node
, handle
,
2264 /* get the trackers node */
2265 for (node
= xmlFirstElementChild(domain_node
); node
;
2266 node
= xmlNextElementSibling(node
)) {
2267 if (!strcmp((const char *) node
->name
,
2268 config_element_trackers
)) {
2269 trackers_node
= node
;
2274 if (!trackers_node
) {
2278 for (node
= xmlFirstElementChild(trackers_node
); node
;
2279 node
= xmlNextElementSibling(node
)) {
2280 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2281 pid_tracker_node
= node
;
2282 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2289 if (!pid_tracker_node
) {
2290 lttng_track_pid(handle
, -1);
2294 lttng_destroy_handle(handle
);
2299 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2302 int ret
, started
= -1, snapshot_mode
= -1;
2303 uint64_t live_timer_interval
= UINT64_MAX
;
2304 xmlChar
*name
= NULL
;
2305 xmlChar
*shm_path
= NULL
;
2306 xmlNodePtr domains_node
= NULL
;
2307 xmlNodePtr output_node
= NULL
;
2309 struct lttng_domain
*kernel_domain
= NULL
;
2310 struct lttng_domain
*ust_domain
= NULL
;
2311 struct lttng_domain
*jul_domain
= NULL
;
2312 struct lttng_domain
*log4j_domain
= NULL
;
2313 struct lttng_domain
*python_domain
= NULL
;
2315 for (node
= xmlFirstElementChild(session_node
); node
;
2316 node
= xmlNextElementSibling(node
)) {
2317 if (!name
&& !strcmp((const char *) node
->name
,
2318 config_element_name
)) {
2320 xmlChar
*node_content
= xmlNodeGetContent(node
);
2321 if (!node_content
) {
2322 ret
= -LTTNG_ERR_NOMEM
;
2326 name
= node_content
;
2327 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2328 config_element_domains
)) {
2330 domains_node
= node
;
2331 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2332 config_element_started
)) {
2334 xmlChar
*node_content
= xmlNodeGetContent(node
);
2335 if (!node_content
) {
2336 ret
= -LTTNG_ERR_NOMEM
;
2340 ret
= parse_bool(node_content
, &started
);
2343 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2346 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2347 config_element_output
)) {
2350 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2351 config_element_shared_memory_path
)) {
2352 /* shared memory path */
2353 xmlChar
*node_content
= xmlNodeGetContent(node
);
2354 if (!node_content
) {
2355 ret
= -LTTNG_ERR_NOMEM
;
2359 shm_path
= node_content
;
2361 /* attributes, snapshot_mode or live_timer_interval */
2362 xmlNodePtr attributes_child
=
2363 xmlFirstElementChild(node
);
2365 if (!strcmp((const char *) attributes_child
->name
,
2366 config_element_snapshot_mode
)) {
2368 xmlChar
*snapshot_mode_content
=
2369 xmlNodeGetContent(attributes_child
);
2370 if (!snapshot_mode_content
) {
2371 ret
= -LTTNG_ERR_NOMEM
;
2375 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2376 free(snapshot_mode_content
);
2378 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2382 /* live_timer_interval */
2383 xmlChar
*timer_interval_content
=
2384 xmlNodeGetContent(attributes_child
);
2385 if (!timer_interval_content
) {
2386 ret
= -LTTNG_ERR_NOMEM
;
2390 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2391 free(timer_interval_content
);
2393 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2401 /* Mandatory attribute, as defined in the session XSD */
2402 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2406 if (session_name
&& strcmp((char *) name
, session_name
)) {
2407 /* This is not the session we are looking for */
2408 ret
= -LTTNG_ERR_NO_SESSION
;
2412 /* Init domains to create the session handles */
2413 for (node
= xmlFirstElementChild(domains_node
); node
;
2414 node
= xmlNextElementSibling(node
)) {
2415 struct lttng_domain
*domain
;
2417 domain
= zmalloc(sizeof(*domain
));
2419 ret
= -LTTNG_ERR_NOMEM
;
2423 ret
= init_domain(node
, domain
);
2425 goto domain_init_error
;
2428 switch (domain
->type
) {
2429 case LTTNG_DOMAIN_KERNEL
:
2430 if (kernel_domain
) {
2431 /* Same domain seen twice, invalid! */
2432 goto domain_init_error
;
2434 kernel_domain
= domain
;
2436 case LTTNG_DOMAIN_UST
:
2438 /* Same domain seen twice, invalid! */
2439 goto domain_init_error
;
2441 ust_domain
= domain
;
2443 case LTTNG_DOMAIN_JUL
:
2445 /* Same domain seen twice, invalid! */
2446 goto domain_init_error
;
2448 jul_domain
= domain
;
2450 case LTTNG_DOMAIN_LOG4J
:
2452 /* Same domain seen twice, invalid! */
2453 goto domain_init_error
;
2455 log4j_domain
= domain
;
2457 case LTTNG_DOMAIN_PYTHON
:
2458 if (python_domain
) {
2459 /* Same domain seen twice, invalid! */
2460 goto domain_init_error
;
2462 python_domain
= domain
;
2465 WARN("Invalid domain type");
2466 goto domain_init_error
;
2471 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2476 /* Destroy session if it exists */
2477 ret
= lttng_destroy_session((const char *) name
);
2478 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2479 ERR("Failed to destroy existing session.");
2484 /* Create session type depending on output type */
2485 if (snapshot_mode
&& snapshot_mode
!= -1) {
2486 ret
= create_snapshot_session((const char *) name
, output_node
);
2487 } else if (live_timer_interval
&&
2488 live_timer_interval
!= UINT64_MAX
) {
2489 ret
= create_session((const char *) name
, kernel_domain
,
2490 ust_domain
, jul_domain
, log4j_domain
,
2491 output_node
, live_timer_interval
);
2493 /* regular session */
2494 ret
= create_session((const char *) name
, kernel_domain
,
2495 ust_domain
, jul_domain
, log4j_domain
,
2496 output_node
, UINT64_MAX
);
2503 ret
= lttng_set_session_shm_path((const char *) name
,
2504 (const char *) shm_path
);
2510 for (node
= xmlFirstElementChild(domains_node
); node
;
2511 node
= xmlNextElementSibling(node
)) {
2512 ret
= process_domain_node(node
, (const char *) name
);
2519 ret
= lttng_start_tracing((const char *) name
);
2527 ERR("Failed to load session %s: %s", (const char *) name
,
2528 lttng_strerror(ret
));
2529 lttng_destroy_session((const char *) name
);
2533 free(kernel_domain
);
2537 free(python_domain
);
2544 * Return 1 if the given path is readable by the current UID or 0 if not.
2545 * Return -1 if the path is EPERM.
2547 static int validate_file_read_creds(const char *path
)
2553 /* Can we read the file. */
2554 ret
= access(path
, R_OK
);
2558 if (errno
== EACCES
) {
2569 int load_session_from_file(const char *path
, const char *session_name
,
2570 struct session_config_validation_ctx
*validation_ctx
, int override
)
2572 int ret
, session_found
= !session_name
;
2573 xmlDocPtr doc
= NULL
;
2574 xmlNodePtr sessions_node
;
2575 xmlNodePtr session_node
;
2578 assert(validation_ctx
);
2580 ret
= validate_file_read_creds(path
);
2583 ret
= -LTTNG_ERR_EPERM
;
2585 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2590 doc
= xmlParseFile(path
);
2592 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2596 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2598 ERR("Session configuration file validation failed");
2599 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2603 sessions_node
= xmlDocGetRootElement(doc
);
2604 if (!sessions_node
) {
2608 for (session_node
= xmlFirstElementChild(sessions_node
);
2609 session_node
; session_node
=
2610 xmlNextElementSibling(session_node
)) {
2611 ret
= process_session_node(session_node
,
2612 session_name
, override
);
2613 if (session_name
&& ret
== 0) {
2614 /* Target session found and loaded */
2622 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2627 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2629 struct dirent
*alloc_dirent(const char *path
)
2633 struct dirent
*entry
;
2635 name_max
= pathconf(path
, _PC_NAME_MAX
);
2636 if (name_max
== -1) {
2637 name_max
= PATH_MAX
;
2639 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2640 entry
= zmalloc(len
);
2645 int load_session_from_path(const char *path
, const char *session_name
,
2646 struct session_config_validation_ctx
*validation_ctx
, int override
)
2648 int ret
, session_found
= !session_name
;
2649 DIR *directory
= NULL
;
2652 assert(validation_ctx
);
2654 directory
= opendir(path
);
2658 /* Try the file loading. */
2661 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2664 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2669 struct dirent
*entry
;
2670 struct dirent
*result
;
2671 char *file_path
= NULL
;
2672 size_t path_len
= strlen(path
);
2674 if (path_len
>= PATH_MAX
) {
2675 ret
= -LTTNG_ERR_INVALID
;
2679 entry
= alloc_dirent(path
);
2681 ret
= -LTTNG_ERR_NOMEM
;
2685 file_path
= zmalloc(PATH_MAX
);
2687 ret
= -LTTNG_ERR_NOMEM
;
2692 strncpy(file_path
, path
, path_len
);
2693 if (file_path
[path_len
- 1] != '/') {
2694 file_path
[path_len
++] = '/';
2698 /* Search for *.lttng files */
2699 while (!readdir_r(directory
, entry
, &result
) && result
) {
2700 size_t file_name_len
= strlen(result
->d_name
);
2702 if (file_name_len
<=
2703 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2707 if (path_len
+ file_name_len
>= PATH_MAX
) {
2711 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2712 result
->d_name
+ file_name_len
- sizeof(
2713 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2717 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2718 file_path
[path_len
+ file_name_len
] = '\0';
2720 ret
= load_session_from_file(file_path
, session_name
,
2721 validation_ctx
, override
);
2722 if (session_name
&& !ret
) {
2731 ret
= load_session_from_file(path
, session_name
,
2732 validation_ctx
, override
);
2742 if (closedir(directory
)) {
2747 if (session_found
) {
2755 * Validate that the given path's credentials and the current process have the
2756 * same UID. If so, return 1 else return 0 if it does NOT match.
2758 static int validate_path_creds(const char *path
)
2760 int ret
, uid
= getuid();
2769 ret
= stat(path
, &buf
);
2771 if (errno
!= ENOENT
) {
2774 ret
= -LTTNG_ERR_INVALID
;
2778 if (buf
.st_uid
!= uid
) {
2789 int config_load_session(const char *path
, const char *session_name
,
2790 int override
, unsigned int autoload
)
2793 bool session_loaded
= false;
2794 const char *path_ptr
= NULL
;
2795 struct session_config_validation_ctx validation_ctx
= { 0 };
2797 ret
= init_session_config_validation_ctx(&validation_ctx
);
2804 const char *sys_path
;
2807 home_path
= utils_get_home_dir();
2809 char path
[PATH_MAX
];
2812 * Try user session configuration path. Ignore error here so we can
2813 * continue loading the system wide sessions.
2816 ret
= snprintf(path
, sizeof(path
),
2817 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2818 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2820 PERROR("snprintf session autoload home config path");
2825 * Credentials are only validated for the autoload in order to
2826 * avoid any user session daemon to try to load kernel sessions
2827 * automatically and failing all the times.
2829 ret
= validate_path_creds(path
);
2834 ret
= snprintf(path
, sizeof(path
),
2835 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2837 PERROR("snprintf session home config path");
2843 ret
= load_session_from_path(path_ptr
, session_name
,
2844 &validation_ctx
, override
);
2845 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2849 * Continue even if the session was found since we have to try
2850 * the system wide sessions.
2852 session_loaded
= true;
2856 /* Reset path pointer for the system wide dir. */
2859 /* Try system wide configuration directory. */
2861 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2862 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2863 ret
= validate_path_creds(sys_path
);
2865 path_ptr
= sys_path
;
2868 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2869 path_ptr
= sys_path
;
2873 ret
= load_session_from_path(path_ptr
, session_name
,
2874 &validation_ctx
, override
);
2876 session_loaded
= true;
2880 ret
= access(path
, F_OK
);
2885 ret
= -LTTNG_ERR_INVALID
;
2886 WARN("Session configuration path does not exist.");
2889 ret
= -LTTNG_ERR_EPERM
;
2892 ret
= -LTTNG_ERR_UNK
;
2898 ret
= load_session_from_path(path
, session_name
,
2899 &validation_ctx
, override
);
2902 fini_session_config_validation_ctx(&validation_ctx
);
2903 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2905 * Don't report an error if no sessions are found when called
2906 * without a session_name or a search path.
2911 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2912 /* A matching session was found in one of the search paths. */