2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
30 #include <common/defaults.h>
31 #include <common/error.h>
32 #include <common/macros.h>
33 #include <common/utils.h>
34 #include <common/compat/getenv.h>
35 #include <lttng/lttng-error.h>
36 #include <libxml/parser.h>
37 #include <libxml/valid.h>
38 #include <libxml/xmlschemas.h>
39 #include <libxml/tree.h>
40 #include <lttng/lttng.h>
41 #include <lttng/snapshot.h>
43 #include "session-config.h"
44 #include "config-internal.h"
46 struct handler_filter_args
{
48 config_entry_handler_cb handler
;
52 struct session_config_validation_ctx
{
53 xmlSchemaParserCtxtPtr parser_ctx
;
55 xmlSchemaValidCtxtPtr schema_validation_ctx
;
58 const char * const config_str_yes
= "yes";
59 const char * const config_str_true
= "true";
60 const char * const config_str_on
= "on";
61 const char * const config_str_no
= "no";
62 const char * const config_str_false
= "false";
63 const char * const config_str_off
= "off";
64 const char * const config_xml_encoding
= "UTF-8";
65 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
66 const char * const config_xml_indent_string
= "\t";
67 const char * const config_xml_true
= "true";
68 const char * const config_xml_false
= "false";
70 const char * const config_element_channel
= "channel";
71 const char * const config_element_channels
= "channels";
72 const char * const config_element_domain
= "domain";
73 const char * const config_element_domains
= "domains";
74 const char * const config_element_event
= "event";
75 const char * const config_element_events
= "events";
76 const char * const config_element_context
= "context";
77 const char * const config_element_contexts
= "contexts";
78 const char * const config_element_attributes
= "attributes";
79 const char * const config_element_exclusion
= "exclusion";
80 const char * const config_element_exclusions
= "exclusions";
81 const char * const config_element_function_attributes
= "function_attributes";
82 const char * const config_element_probe_attributes
= "probe_attributes";
83 const char * const config_element_symbol_name
= "symbol_name";
84 const char * const config_element_address
= "address";
85 const char * const config_element_offset
= "offset";
86 const char * const config_element_name
= "name";
87 const char * const config_element_enabled
= "enabled";
88 const char * const config_element_overwrite_mode
= "overwrite_mode";
89 const char * const config_element_subbuf_size
= "subbuffer_size";
90 const char * const config_element_num_subbuf
= "subbuffer_count";
91 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
92 const char * const config_element_read_timer_interval
= "read_timer_interval";
93 const char * const config_element_output
= "output";
94 const char * const config_element_output_type
= "output_type";
95 const char * const config_element_tracefile_size
= "tracefile_size";
96 const char * const config_element_tracefile_count
= "tracefile_count";
97 const char * const config_element_live_timer_interval
= "live_timer_interval";
98 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
99 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
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 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
105 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
106 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
107 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
108 const char * const config_element_config
= "config";
109 const char * const config_element_started
= "started";
110 const char * const config_element_snapshot_mode
= "snapshot_mode";
111 const char * const config_element_loglevel
= "loglevel";
112 const char * const config_element_loglevel_type
= "loglevel_type";
113 const char * const config_element_filter
= "filter";
114 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
115 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
116 const char * const config_element_consumer_output
= "consumer_output";
117 const char * const config_element_destination
= "destination";
118 const char * const config_element_path
= "path";
119 const char * const config_element_net_output
= "net_output";
120 const char * const config_element_control_uri
= "control_uri";
121 const char * const config_element_data_uri
= "data_uri";
122 const char * const config_element_max_size
= "max_size";
123 const char * const config_element_pid
= "pid";
124 const char * const config_element_pids
= "pids";
125 const char * const config_element_shared_memory_path
= "shared_memory_path";
126 const char * const config_element_pid_tracker
= "pid_tracker";
127 const char * const config_element_trackers
= "trackers";
128 const char * const config_element_targets
= "targets";
129 const char * const config_element_target_pid
= "pid_target";
131 const char * const config_domain_type_kernel
= "KERNEL";
132 const char * const config_domain_type_ust
= "UST";
133 const char * const config_domain_type_jul
= "JUL";
134 const char * const config_domain_type_log4j
= "LOG4J";
135 const char * const config_domain_type_python
= "PYTHON";
137 const char * const config_buffer_type_per_pid
= "PER_PID";
138 const char * const config_buffer_type_per_uid
= "PER_UID";
139 const char * const config_buffer_type_global
= "GLOBAL";
141 const char * const config_overwrite_mode_discard
= "DISCARD";
142 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
144 const char * const config_output_type_splice
= "SPLICE";
145 const char * const config_output_type_mmap
= "MMAP";
147 const char * const config_loglevel_type_all
= "ALL";
148 const char * const config_loglevel_type_range
= "RANGE";
149 const char * const config_loglevel_type_single
= "SINGLE";
151 const char * const config_event_type_all
= "ALL";
152 const char * const config_event_type_tracepoint
= "TRACEPOINT";
153 const char * const config_event_type_probe
= "PROBE";
154 const char * const config_event_type_function
= "FUNCTION";
155 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
156 const char * const config_event_type_noop
= "NOOP";
157 const char * const config_event_type_syscall
= "SYSCALL";
158 const char * const config_event_type_kprobe
= "KPROBE";
159 const char * const config_event_type_kretprobe
= "KRETPROBE";
161 const char * const config_event_context_pid
= "PID";
162 const char * const config_event_context_procname
= "PROCNAME";
163 const char * const config_event_context_prio
= "PRIO";
164 const char * const config_event_context_nice
= "NICE";
165 const char * const config_event_context_vpid
= "VPID";
166 const char * const config_event_context_tid
= "TID";
167 const char * const config_event_context_vtid
= "VTID";
168 const char * const config_event_context_ppid
= "PPID";
169 const char * const config_event_context_vppid
= "VPPID";
170 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
171 const char * const config_event_context_hostname
= "HOSTNAME";
172 const char * const config_event_context_ip
= "IP";
173 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
174 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
175 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
176 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
177 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
178 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
180 enum process_event_node_phase
{
185 struct consumer_output
{
192 static int config_entry_handler_filter(struct handler_filter_args
*args
,
193 const char *section
, const char *name
, const char *value
)
196 struct config_entry entry
= { section
, name
, value
};
200 if (!section
|| !name
|| !value
) {
206 if (strcmp(args
->section
, section
)) {
211 ret
= args
->handler(&entry
, args
->user_data
);
217 int config_get_section_entries(const char *override_path
, const char *section
,
218 config_entry_handler_cb handler
, void *user_data
)
222 FILE *config_file
= NULL
;
223 struct handler_filter_args filter
= { section
, handler
, user_data
};
225 /* First, try system-wide conf. file. */
226 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
228 config_file
= fopen(path
, "r");
230 DBG("Loading daemon conf file at %s", path
);
232 * Return value is not very important here since error or not, we
233 * continue and try the next possible conf. file.
235 (void) ini_parse_file(config_file
,
236 (ini_entry_handler
) config_entry_handler_filter
,
241 /* Second is the user local configuration. */
242 path
= utils_get_home_dir();
244 char fullpath
[PATH_MAX
];
246 ret
= snprintf(fullpath
, sizeof(fullpath
),
247 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
249 PERROR("snprintf user conf. path");
253 config_file
= fopen(fullpath
, "r");
255 DBG("Loading daemon user conf file at %s", path
);
257 * Return value is not very important here since error or not, we
258 * continue and try the next possible conf. file.
260 (void) ini_parse_file(config_file
,
261 (ini_entry_handler
) config_entry_handler_filter
,
267 /* Final path is the one that the user might have provided. */
269 config_file
= fopen(override_path
, "r");
271 DBG("Loading daemon command line conf file at %s", override_path
);
272 (void) ini_parse_file(config_file
,
273 (ini_entry_handler
) config_entry_handler_filter
,
277 ERR("Failed to open daemon configuration file at %s",
284 /* Everything went well. */
292 int config_parse_value(const char *value
)
295 char *endptr
, *lower_str
;
305 v
= strtoul(value
, &endptr
, 10);
306 if (endptr
!= value
) {
311 lower_str
= zmalloc(len
+ 1);
318 for (i
= 0; i
< len
; i
++) {
319 lower_str
[i
] = tolower(value
[i
]);
322 if (!strcmp(lower_str
, config_str_yes
) ||
323 !strcmp(lower_str
, config_str_true
) ||
324 !strcmp(lower_str
, config_str_on
)) {
326 } else if (!strcmp(lower_str
, config_str_no
) ||
327 !strcmp(lower_str
, config_str_false
) ||
328 !strcmp(lower_str
, config_str_off
)) {
340 * Returns a xmlChar string which must be released using xmlFree().
342 static xmlChar
*encode_string(const char *in_str
)
344 xmlChar
*out_str
= NULL
;
345 xmlCharEncodingHandlerPtr handler
;
346 int out_len
, ret
, in_len
;
350 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
352 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
356 in_len
= strlen(in_str
);
358 * Add 1 byte for the NULL terminted character. The factor 4 here is
359 * used because UTF-8 characters can take up to 4 bytes.
361 out_len
= (in_len
* 4) + 1;
362 out_str
= xmlMalloc(out_len
);
367 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
374 /* out_len is now the size of out_str */
375 out_str
[out_len
] = '\0';
381 struct config_writer
*config_writer_create(int fd_output
, int indent
)
384 struct config_writer
*writer
;
385 xmlOutputBufferPtr buffer
;
387 writer
= zmalloc(sizeof(struct config_writer
));
389 PERROR("zmalloc config_writer_create");
393 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
398 writer
->writer
= xmlNewTextWriter(buffer
);
399 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
400 config_xml_encoding
, NULL
);
405 ret
= xmlTextWriterSetIndentString(writer
->writer
,
406 BAD_CAST config_xml_indent_string
);
411 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
419 config_writer_destroy(writer
);
424 int config_writer_destroy(struct config_writer
*writer
)
433 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
434 WARN("Could not close XML document");
438 if (writer
->writer
) {
439 xmlFreeTextWriter(writer
->writer
);
448 int config_writer_open_element(struct config_writer
*writer
,
449 const char *element_name
)
452 xmlChar
*encoded_element_name
;
454 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
459 encoded_element_name
= encode_string(element_name
);
460 if (!encoded_element_name
) {
465 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
466 xmlFree(encoded_element_name
);
468 return ret
>= 0 ? 0 : ret
;
472 int config_writer_write_attribute(struct config_writer
*writer
,
473 const char *name
, const char *value
)
476 xmlChar
*encoded_name
= NULL
;
477 xmlChar
*encoded_value
= NULL
;
479 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
484 encoded_name
= encode_string(name
);
490 encoded_value
= encode_string(value
);
491 if (!encoded_value
) {
496 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
499 xmlFree(encoded_name
);
500 xmlFree(encoded_value
);
501 return ret
>= 0 ? 0 : ret
;
505 int config_writer_close_element(struct config_writer
*writer
)
509 if (!writer
|| !writer
->writer
) {
514 ret
= xmlTextWriterEndElement(writer
->writer
);
516 return ret
>= 0 ? 0 : ret
;
520 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
521 const char *element_name
, uint64_t value
)
524 xmlChar
*encoded_element_name
;
526 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
531 encoded_element_name
= encode_string(element_name
);
532 if (!encoded_element_name
) {
537 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
538 encoded_element_name
, "%" PRIu64
, value
);
539 xmlFree(encoded_element_name
);
541 return ret
>= 0 ? 0 : ret
;
545 int config_writer_write_element_signed_int(struct config_writer
*writer
,
546 const char *element_name
, int64_t value
)
549 xmlChar
*encoded_element_name
;
551 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
556 encoded_element_name
= encode_string(element_name
);
557 if (!encoded_element_name
) {
562 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
563 encoded_element_name
, "%" PRIi64
, value
);
564 xmlFree(encoded_element_name
);
566 return ret
>= 0 ? 0 : ret
;
570 int config_writer_write_element_bool(struct config_writer
*writer
,
571 const char *element_name
, int value
)
573 return config_writer_write_element_string(writer
, element_name
,
574 value
? config_xml_true
: config_xml_false
);
578 int config_writer_write_element_string(struct config_writer
*writer
,
579 const char *element_name
, const char *value
)
582 xmlChar
*encoded_element_name
= NULL
;
583 xmlChar
*encoded_value
= NULL
;
585 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
591 encoded_element_name
= encode_string(element_name
);
592 if (!encoded_element_name
) {
597 encoded_value
= encode_string(value
);
598 if (!encoded_value
) {
603 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
606 xmlFree(encoded_element_name
);
607 xmlFree(encoded_value
);
608 return ret
>= 0 ? 0 : ret
;
612 void xml_error_handler(void *ctx
, const char *format
, ...)
618 va_start(args
, format
);
619 ret
= vasprintf(&errMsg
, format
, args
);
622 ERR("String allocation failed in xml error handler");
626 fprintf(stderr
, "XML Error: %s", errMsg
);
631 void fini_session_config_validation_ctx(
632 struct session_config_validation_ctx
*ctx
)
634 if (ctx
->parser_ctx
) {
635 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
639 xmlSchemaFree(ctx
->schema
);
642 if (ctx
->schema_validation_ctx
) {
643 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
646 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
650 char *get_session_config_xsd_path()
653 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
654 size_t base_path_len
;
658 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
661 base_path_len
= strlen(base_path
);
662 max_path_len
= base_path_len
+
663 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
664 xsd_path
= zmalloc(max_path_len
);
669 strncpy(xsd_path
, base_path
, max_path_len
);
670 if (xsd_path
[base_path_len
- 1] != '/') {
671 xsd_path
[base_path_len
++] = '/';
674 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
675 max_path_len
- base_path_len
);
681 int init_session_config_validation_ctx(
682 struct session_config_validation_ctx
*ctx
)
685 char *xsd_path
= get_session_config_xsd_path();
688 ret
= -LTTNG_ERR_NOMEM
;
692 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
693 if (!ctx
->parser_ctx
) {
694 ERR("XSD parser context creation failed");
695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
698 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
699 xml_error_handler
, NULL
);
701 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
703 ERR("XSD parsing failed");
704 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
708 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
709 if (!ctx
->schema_validation_ctx
) {
710 ERR("XSD validation context creation failed");
711 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
715 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
716 xml_error_handler
, NULL
);
721 fini_session_config_validation_ctx(ctx
);
729 int parse_uint(xmlChar
*str
, uint64_t *val
)
739 *val
= strtoull((const char *) str
, &endptr
, 10);
740 if (!endptr
|| *endptr
) {
751 int parse_int(xmlChar
*str
, int64_t *val
)
761 *val
= strtoll((const char *) str
, &endptr
, 10);
762 if (!endptr
|| *endptr
) {
773 int parse_bool(xmlChar
*str
, int *val
)
782 if (!strcmp((const char *) str
, config_xml_true
)) {
784 } else if (!strcmp((const char *) str
, config_xml_false
)) {
787 WARN("Invalid boolean value encoutered (%s).",
796 int get_domain_type(xmlChar
*domain
)
804 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
805 ret
= LTTNG_DOMAIN_KERNEL
;
806 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
807 ret
= LTTNG_DOMAIN_UST
;
808 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
809 ret
= LTTNG_DOMAIN_JUL
;
810 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
811 ret
= LTTNG_DOMAIN_LOG4J
;
812 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
813 ret
= LTTNG_DOMAIN_PYTHON
;
824 int get_buffer_type(xmlChar
*buffer_type
)
832 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
833 ret
= LTTNG_BUFFER_GLOBAL
;
834 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
835 ret
= LTTNG_BUFFER_PER_UID
;
836 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
837 ret
= LTTNG_BUFFER_PER_PID
;
848 int get_overwrite_mode(xmlChar
*overwrite_mode
)
852 if (!overwrite_mode
) {
856 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
858 } else if (!strcmp((char *) overwrite_mode
,
859 config_overwrite_mode_discard
)) {
871 int get_output_type(xmlChar
*output_type
)
879 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
880 ret
= LTTNG_EVENT_MMAP
;
881 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
882 ret
= LTTNG_EVENT_SPLICE
;
893 int get_event_type(xmlChar
*event_type
)
901 if (!strcmp((char *) event_type
, config_event_type_all
)) {
902 ret
= LTTNG_EVENT_ALL
;
903 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
904 ret
= LTTNG_EVENT_TRACEPOINT
;
905 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
906 ret
= LTTNG_EVENT_PROBE
;
907 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
908 ret
= LTTNG_EVENT_FUNCTION
;
909 } else if (!strcmp((char *) event_type
,
910 config_event_type_function_entry
)) {
911 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
912 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
913 ret
= LTTNG_EVENT_NOOP
;
914 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
915 ret
= LTTNG_EVENT_SYSCALL
;
926 int get_loglevel_type(xmlChar
*loglevel_type
)
930 if (!loglevel_type
) {
934 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
935 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
936 } else if (!strcmp((char *) loglevel_type
,
937 config_loglevel_type_range
)) {
938 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
939 } else if (!strcmp((char *) loglevel_type
,
940 config_loglevel_type_single
)) {
941 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
952 * Return the context type or -1 on error.
955 int get_context_type(xmlChar
*context_type
)
963 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
964 ret
= LTTNG_EVENT_CONTEXT_PID
;
965 } else if (!strcmp((char *) context_type
,
966 config_event_context_procname
)) {
967 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
968 } else if (!strcmp((char *) context_type
,
969 config_event_context_prio
)) {
970 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
971 } else if (!strcmp((char *) context_type
,
972 config_event_context_nice
)) {
973 ret
= LTTNG_EVENT_CONTEXT_NICE
;
974 } else if (!strcmp((char *) context_type
,
975 config_event_context_vpid
)) {
976 ret
= LTTNG_EVENT_CONTEXT_VPID
;
977 } else if (!strcmp((char *) context_type
,
978 config_event_context_tid
)) {
979 ret
= LTTNG_EVENT_CONTEXT_TID
;
980 } else if (!strcmp((char *) context_type
,
981 config_event_context_vtid
)) {
982 ret
= LTTNG_EVENT_CONTEXT_VTID
;
983 } else if (!strcmp((char *) context_type
,
984 config_event_context_ppid
)) {
985 ret
= LTTNG_EVENT_CONTEXT_PPID
;
986 } else if (!strcmp((char *) context_type
,
987 config_event_context_vppid
)) {
988 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
989 } else if (!strcmp((char *) context_type
,
990 config_event_context_pthread_id
)) {
991 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
992 } else if (!strcmp((char *) context_type
,
993 config_event_context_hostname
)) {
994 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
995 } else if (!strcmp((char *) context_type
,
996 config_event_context_ip
)) {
997 ret
= LTTNG_EVENT_CONTEXT_IP
;
998 } else if (!strcmp((char *) context_type
,
999 config_event_context_interruptible
)) {
1000 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1001 } else if (!strcmp((char *) context_type
,
1002 config_event_context_preemptible
)) {
1003 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1004 } else if (!strcmp((char *) context_type
,
1005 config_event_context_need_reschedule
)) {
1006 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1007 } else if (!strcmp((char *) context_type
,
1008 config_event_context_migratable
)) {
1009 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1020 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1025 for (node
= xmlFirstElementChild(domain_node
); node
;
1026 node
= xmlNextElementSibling(node
)) {
1027 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1029 xmlChar
*node_content
= xmlNodeGetContent(node
);
1030 if (!node_content
) {
1031 ret
= -LTTNG_ERR_NOMEM
;
1035 ret
= get_domain_type(node_content
);
1038 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1043 } else if (!strcmp((const char *) node
->name
,
1044 config_element_buffer_type
)) {
1046 xmlChar
*node_content
= xmlNodeGetContent(node
);
1047 if (!node_content
) {
1048 ret
= -LTTNG_ERR_NOMEM
;
1052 ret
= get_buffer_type(node_content
);
1055 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1059 domain
->buf_type
= ret
;
1068 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1073 for (node
= xmlFirstElementChild(net_output_node
); node
;
1074 node
= xmlNextElementSibling(node
)) {
1075 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1077 *control_uri
= (char *) xmlNodeGetContent(node
);
1078 if (!*control_uri
) {
1083 *data_uri
= (char *) xmlNodeGetContent(node
);
1090 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1094 int process_consumer_output(xmlNodePtr consumer_output_node
,
1095 struct consumer_output
*output
)
1102 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1103 node
= xmlNextElementSibling(node
)) {
1104 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1105 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1109 ret
= -LTTNG_ERR_NOMEM
;
1113 ret
= parse_bool(enabled_str
, &output
->enabled
);
1119 xmlNodePtr output_type_node
;
1122 output_type_node
= xmlFirstElementChild(node
);
1123 if (!output_type_node
) {
1124 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1128 if (!strcmp((const char *) output_type_node
->name
,
1129 config_element_path
)) {
1131 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1132 if (!output
->path
) {
1133 ret
= -LTTNG_ERR_NOMEM
;
1138 ret
= get_net_output_uris(output_type_node
,
1139 &output
->control_uri
, &output
->data_uri
);
1151 free(output
->control_uri
);
1152 free(output
->data_uri
);
1153 memset(output
, 0, sizeof(struct consumer_output
));
1159 int create_session_net_output(const char *name
, const char *control_uri
,
1160 const char *data_uri
)
1163 struct lttng_handle
*handle
;
1164 const char *uri
= NULL
;
1168 handle
= lttng_create_handle(name
, NULL
);
1170 ret
= -LTTNG_ERR_NOMEM
;
1174 if (!control_uri
|| !data_uri
) {
1175 uri
= control_uri
? control_uri
: data_uri
;
1180 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1181 lttng_destroy_handle(handle
);
1187 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1190 xmlNodePtr node
= NULL
;
1191 xmlNodePtr snapshot_output_list_node
;
1192 xmlNodePtr snapshot_output_node
;
1194 assert(session_name
);
1196 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1205 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1207 /* Parse and create snapshot outputs */
1209 for (snapshot_output_node
=
1210 xmlFirstElementChild(snapshot_output_list_node
);
1211 snapshot_output_node
; snapshot_output_node
=
1212 xmlNextElementSibling(snapshot_output_node
)) {
1214 uint64_t max_size
= UINT64_MAX
;
1215 struct consumer_output output
= { 0 };
1216 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1218 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1219 node
= xmlNextElementSibling(node
)) {
1220 if (!strcmp((const char *) node
->name
,
1221 config_element_name
)) {
1223 name
= (char *) xmlNodeGetContent(node
);
1225 ret
= -LTTNG_ERR_NOMEM
;
1226 goto error_snapshot_output
;
1228 } else if (!strcmp((const char *) node
->name
,
1229 config_element_max_size
)) {
1230 xmlChar
*content
= xmlNodeGetContent(node
);
1234 ret
= -LTTNG_ERR_NOMEM
;
1235 goto error_snapshot_output
;
1237 ret
= parse_uint(content
, &max_size
);
1240 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1241 goto error_snapshot_output
;
1244 /* consumer_output */
1245 ret
= process_consumer_output(node
, &output
);
1247 goto error_snapshot_output
;
1252 snapshot_output
= lttng_snapshot_output_create();
1253 if (!snapshot_output
) {
1254 ret
= -LTTNG_ERR_NOMEM
;
1255 goto error_snapshot_output
;
1258 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1260 goto error_snapshot_output
;
1263 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1265 goto error_snapshot_output
;
1269 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1272 goto error_snapshot_output
;
1275 if (output
.control_uri
) {
1276 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1279 goto error_snapshot_output
;
1283 if (output
.data_uri
) {
1284 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1287 goto error_snapshot_output
;
1292 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1293 error_snapshot_output
:
1296 free(output
.control_uri
);
1297 free(output
.data_uri
);
1298 lttng_snapshot_output_destroy(snapshot_output
);
1308 int create_session(const char *name
,
1309 struct lttng_domain
*kernel_domain
,
1310 struct lttng_domain
*ust_domain
,
1311 struct lttng_domain
*jul_domain
,
1312 struct lttng_domain
*log4j_domain
,
1313 xmlNodePtr output_node
,
1314 uint64_t live_timer_interval
)
1317 struct consumer_output output
= { 0 };
1318 xmlNodePtr consumer_output_node
;
1323 consumer_output_node
= xmlFirstElementChild(output_node
);
1324 if (!consumer_output_node
) {
1325 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1329 if (strcmp((const char *) consumer_output_node
->name
,
1330 config_element_consumer_output
)) {
1331 WARN("Invalid output type, expected %s node",
1332 config_element_consumer_output
);
1333 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1337 ret
= process_consumer_output(consumer_output_node
, &output
);
1343 if (live_timer_interval
!= UINT64_MAX
&&
1344 !output
.control_uri
&& !output
.data_uri
) {
1345 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1349 if (output
.control_uri
|| output
.data_uri
) {
1350 /* network destination */
1351 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1353 * URLs are provided for sure since the test above make sure that
1354 * with a live timer the data and control URIs are provided. So,
1355 * NULL is passed here and will be set right after.
1357 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1359 ret
= lttng_create_session(name
, NULL
);
1365 ret
= create_session_net_output(name
, output
.control_uri
,
1372 /* either local output or no output */
1373 ret
= lttng_create_session(name
, output
.path
);
1380 free(output
.control_uri
);
1381 free(output
.data_uri
);
1385 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1386 struct lttng_event_probe_attr
*attr
)
1390 assert(probe_attribute_node
);
1393 if (!strcmp((const char *) probe_attribute_node
->name
,
1394 config_element_address
)) {
1399 content
= xmlNodeGetContent(probe_attribute_node
);
1401 ret
= -LTTNG_ERR_NOMEM
;
1405 ret
= parse_uint(content
, &addr
);
1408 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1413 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1414 config_element_offset
)) {
1416 uint64_t offset
= 0;
1419 content
= xmlNodeGetContent(probe_attribute_node
);
1421 ret
= -LTTNG_ERR_NOMEM
;
1425 ret
= parse_uint(content
, &offset
);
1428 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1432 attr
->offset
= offset
;
1433 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1434 config_element_symbol_name
)) {
1439 content
= xmlNodeGetContent(probe_attribute_node
);
1441 ret
= -LTTNG_ERR_NOMEM
;
1445 name_len
= strlen((char *) content
);
1446 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1447 WARN("symbol_name too long.");
1448 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1453 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1462 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1463 const char *channel_name
, const enum process_event_node_phase phase
)
1467 struct lttng_event event
;
1468 char **exclusions
= NULL
;
1469 unsigned long exclusion_count
= 0;
1470 char *filter_expression
= NULL
;
1474 assert(channel_name
);
1476 memset(&event
, 0, sizeof(event
));
1478 /* Initialize default log level which varies by domain */
1479 switch (handle
->domain
.type
)
1481 case LTTNG_DOMAIN_JUL
:
1482 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1484 case LTTNG_DOMAIN_LOG4J
:
1485 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1487 case LTTNG_DOMAIN_PYTHON
:
1488 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1490 case LTTNG_DOMAIN_UST
:
1491 case LTTNG_DOMAIN_KERNEL
:
1492 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1498 for (node
= xmlFirstElementChild(event_node
); node
;
1499 node
= xmlNextElementSibling(node
)) {
1500 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1505 content
= xmlNodeGetContent(node
);
1507 ret
= -LTTNG_ERR_NOMEM
;
1511 name_len
= strlen((char *) content
);
1512 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1513 WARN("Channel name too long.");
1514 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1519 strncpy(event
.name
, (const char *) content
, name_len
);
1521 } else if (!strcmp((const char *) node
->name
,
1522 config_element_enabled
)) {
1523 xmlChar
*content
= xmlNodeGetContent(node
);
1527 ret
= -LTTNG_ERR_NOMEM
;
1531 ret
= parse_bool(content
, &event
.enabled
);
1534 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1537 } else if (!strcmp((const char *) node
->name
,
1538 config_element_type
)) {
1539 xmlChar
*content
= xmlNodeGetContent(node
);
1543 ret
= -LTTNG_ERR_NOMEM
;
1547 ret
= get_event_type(content
);
1550 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1555 } else if (!strcmp((const char *) node
->name
,
1556 config_element_loglevel_type
)) {
1557 xmlChar
*content
= xmlNodeGetContent(node
);
1561 ret
= -LTTNG_ERR_NOMEM
;
1565 ret
= get_loglevel_type(content
);
1568 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1572 event
.loglevel_type
= ret
;
1573 } else if (!strcmp((const char *) node
->name
,
1574 config_element_loglevel
)) {
1576 int64_t loglevel
= 0;
1579 content
= xmlNodeGetContent(node
);
1581 ret
= -LTTNG_ERR_NOMEM
;
1585 ret
= parse_int(content
, &loglevel
);
1588 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1592 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1593 WARN("loglevel out of range.");
1594 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1598 event
.loglevel
= loglevel
;
1599 } else if (!strcmp((const char *) node
->name
,
1600 config_element_filter
)) {
1602 xmlNodeGetContent(node
);
1606 ret
= -LTTNG_ERR_NOMEM
;
1610 filter_expression
= strdup((char *) content
);
1612 if (!filter_expression
) {
1613 ret
= -LTTNG_ERR_NOMEM
;
1616 } else if (!strcmp((const char *) node
->name
,
1617 config_element_exclusions
)) {
1618 xmlNodePtr exclusion_node
;
1619 int exclusion_index
= 0;
1624 * Exclusions has already been initialized,
1627 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1631 exclusion_count
= xmlChildElementCount(node
);
1632 if (!exclusion_count
) {
1636 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1638 exclusion_count
= 0;
1639 ret
= -LTTNG_ERR_NOMEM
;
1643 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1644 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1646 xmlNodeGetContent(exclusion_node
);
1649 ret
= -LTTNG_ERR_NOMEM
;
1653 exclusions
[exclusion_index
] = strdup((const char *) content
);
1655 if (!exclusions
[exclusion_index
]) {
1656 ret
= -LTTNG_ERR_NOMEM
;
1662 event
.exclusion
= 1;
1663 } else if (!strcmp((const char *) node
->name
,
1664 config_element_attributes
)) {
1665 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1668 if (!attribute_node
) {
1669 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1673 if (!strcmp((const char *) node
->name
,
1674 config_element_probe_attributes
)) {
1675 xmlNodePtr probe_attribute_node
;
1677 /* probe_attributes */
1678 for (probe_attribute_node
=
1679 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1680 probe_attribute_node
= xmlNextElementSibling(
1681 probe_attribute_node
)) {
1683 ret
= process_probe_attribute_node(probe_attribute_node
,
1692 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1694 /* function_attributes */
1695 content
= xmlNodeGetContent(symbol_node
);
1697 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1701 sym_len
= strlen((char *) content
);
1702 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1703 WARN("Function name too long.");
1704 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1709 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1716 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1717 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1718 filter_expression
, exclusion_count
, exclusions
);
1720 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1721 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1726 for (i
= 0; i
< exclusion_count
; i
++) {
1727 free(exclusions
[i
]);
1731 free(filter_expression
);
1736 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1737 const char *channel_name
)
1740 struct lttng_event event
;
1743 assert(events_node
);
1745 assert(channel_name
);
1747 for (node
= xmlFirstElementChild(events_node
); node
;
1748 node
= xmlNextElementSibling(node
)) {
1749 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1756 * Disable all events to enable only the necessary events.
1757 * Limitations regarding lttng_disable_events and tuple descriptor
1758 * force this approach.
1760 memset(&event
, 0, sizeof(event
));
1761 event
.loglevel
= -1;
1762 event
.type
= LTTNG_EVENT_ALL
;
1763 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1768 for (node
= xmlFirstElementChild(events_node
); node
;
1769 node
= xmlNextElementSibling(node
)) {
1770 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1781 int process_channel_attr_node(xmlNodePtr attr_node
,
1782 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1783 xmlNodePtr
*events_node
)
1789 assert(contexts_node
);
1790 assert(events_node
);
1792 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1797 content
= xmlNodeGetContent(attr_node
);
1799 ret
= -LTTNG_ERR_NOMEM
;
1803 name_len
= strlen((char *) content
);
1804 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1805 WARN("Channel name too long.");
1806 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1811 strncpy(channel
->name
, (const char *) content
, name_len
);
1813 } else if (!strcmp((const char *) attr_node
->name
,
1814 config_element_enabled
)) {
1819 content
= xmlNodeGetContent(attr_node
);
1821 ret
= -LTTNG_ERR_NOMEM
;
1825 ret
= parse_bool(content
, &enabled
);
1828 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1832 channel
->enabled
= enabled
;
1833 } else if (!strcmp((const char *) attr_node
->name
,
1834 config_element_overwrite_mode
)) {
1837 /* overwrite_mode */
1838 content
= xmlNodeGetContent(attr_node
);
1840 ret
= -LTTNG_ERR_NOMEM
;
1844 ret
= get_overwrite_mode(content
);
1847 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1851 channel
->attr
.overwrite
= ret
;
1852 } else if (!strcmp((const char *) attr_node
->name
,
1853 config_element_subbuf_size
)) {
1856 /* subbuffer_size */
1857 content
= xmlNodeGetContent(attr_node
);
1859 ret
= -LTTNG_ERR_NOMEM
;
1863 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1866 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1869 } else if (!strcmp((const char *) attr_node
->name
,
1870 config_element_num_subbuf
)) {
1873 /* subbuffer_count */
1874 content
= xmlNodeGetContent(attr_node
);
1876 ret
= -LTTNG_ERR_NOMEM
;
1880 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1883 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1886 } else if (!strcmp((const char *) attr_node
->name
,
1887 config_element_switch_timer_interval
)) {
1889 uint64_t switch_timer_interval
= 0;
1891 /* switch_timer_interval */
1892 content
= xmlNodeGetContent(attr_node
);
1894 ret
= -LTTNG_ERR_NOMEM
;
1898 ret
= parse_uint(content
, &switch_timer_interval
);
1901 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1905 if (switch_timer_interval
> UINT_MAX
) {
1906 WARN("switch_timer_interval out of range.");
1907 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1911 channel
->attr
.switch_timer_interval
=
1912 switch_timer_interval
;
1913 } else if (!strcmp((const char *) attr_node
->name
,
1914 config_element_read_timer_interval
)) {
1916 uint64_t read_timer_interval
= 0;
1918 /* read_timer_interval */
1919 content
= xmlNodeGetContent(attr_node
);
1921 ret
= -LTTNG_ERR_NOMEM
;
1925 ret
= parse_uint(content
, &read_timer_interval
);
1928 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1932 if (read_timer_interval
> UINT_MAX
) {
1933 WARN("read_timer_interval out of range.");
1934 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1938 channel
->attr
.read_timer_interval
=
1939 read_timer_interval
;
1940 } else if (!strcmp((const char *) attr_node
->name
,
1941 config_element_output_type
)) {
1945 content
= xmlNodeGetContent(attr_node
);
1947 ret
= -LTTNG_ERR_NOMEM
;
1951 ret
= get_output_type(content
);
1954 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1958 channel
->attr
.output
= ret
;
1959 } else if (!strcmp((const char *) attr_node
->name
,
1960 config_element_tracefile_size
)) {
1963 /* tracefile_size */
1964 content
= xmlNodeGetContent(attr_node
);
1966 ret
= -LTTNG_ERR_NOMEM
;
1970 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1973 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1976 } else if (!strcmp((const char *) attr_node
->name
,
1977 config_element_tracefile_count
)) {
1980 /* tracefile_count */
1981 content
= xmlNodeGetContent(attr_node
);
1983 ret
= -LTTNG_ERR_NOMEM
;
1987 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1990 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1993 } else if (!strcmp((const char *) attr_node
->name
,
1994 config_element_live_timer_interval
)) {
1996 uint64_t live_timer_interval
= 0;
1998 /* live_timer_interval */
1999 content
= xmlNodeGetContent(attr_node
);
2001 ret
= -LTTNG_ERR_NOMEM
;
2005 ret
= parse_uint(content
, &live_timer_interval
);
2008 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2012 if (live_timer_interval
> UINT_MAX
) {
2013 WARN("live_timer_interval out of range.");
2014 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2018 channel
->attr
.live_timer_interval
=
2019 live_timer_interval
;
2020 } else if (!strcmp((const char *) attr_node
->name
,
2021 config_element_events
)) {
2023 *events_node
= attr_node
;
2026 *contexts_node
= attr_node
;
2034 int process_context_node(xmlNodePtr context_node
,
2035 struct lttng_handle
*handle
, const char *channel_name
)
2038 struct lttng_event_context context
;
2039 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2042 assert(channel_name
);
2044 if (!context_child_node
) {
2045 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2049 memset(&context
, 0, sizeof(context
));
2051 if (!strcmp((const char *) context_child_node
->name
,
2052 config_element_type
)) {
2054 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2057 ret
= -LTTNG_ERR_NOMEM
;
2061 ret
= get_context_type(content
);
2064 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2069 } else if (!strcmp((const char *) context_child_node
->name
,
2070 config_element_context_perf
)) {
2072 xmlNodePtr perf_attr_node
;
2074 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2075 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2076 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2077 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2078 perf_attr_node
; perf_attr_node
=
2079 xmlNextElementSibling(perf_attr_node
)) {
2080 if (!strcmp((const char *) perf_attr_node
->name
,
2081 config_element_type
)) {
2086 content
= xmlNodeGetContent(perf_attr_node
);
2088 ret
= -LTTNG_ERR_NOMEM
;
2092 ret
= parse_uint(content
, &type
);
2095 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2099 if (type
> UINT32_MAX
) {
2100 WARN("perf context type out of range.");
2101 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2105 context
.u
.perf_counter
.type
= type
;
2106 } else if (!strcmp((const char *) perf_attr_node
->name
,
2107 config_element_config
)) {
2109 uint64_t config
= 0;
2112 content
= xmlNodeGetContent(perf_attr_node
);
2114 ret
= -LTTNG_ERR_NOMEM
;
2118 ret
= parse_uint(content
, &config
);
2121 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2125 context
.u
.perf_counter
.config
= config
;
2126 } else if (!strcmp((const char *) perf_attr_node
->name
,
2127 config_element_name
)) {
2132 content
= xmlNodeGetContent(perf_attr_node
);
2134 ret
= -LTTNG_ERR_NOMEM
;
2138 name_len
= strlen((char *) content
);
2139 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2140 WARN("perf context name too long.");
2141 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2146 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2151 } else if (!strcmp((const char *) context_child_node
->name
,
2152 config_element_context_app
)) {
2153 /* application context */
2154 xmlNodePtr app_ctx_node
;
2156 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2157 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2158 app_ctx_node
; app_ctx_node
=
2159 xmlNextElementSibling(app_ctx_node
)) {
2161 char **target
= strcmp(
2162 (const char *) app_ctx_node
->name
,
2163 config_element_context_app_provider_name
) == 0 ?
2164 &context
.u
.app_ctx
.provider_name
:
2165 &context
.u
.app_ctx
.ctx_name
;
2167 content
= xmlNodeGetContent(app_ctx_node
);
2169 ret
= -LTTNG_ERR_NOMEM
;
2173 *target
= (char *) content
;
2176 /* Unrecognized context type */
2177 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2181 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2182 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2183 free(context
.u
.app_ctx
.provider_name
);
2184 free(context
.u
.app_ctx
.ctx_name
);
2191 int process_contexts_node(xmlNodePtr contexts_node
,
2192 struct lttng_handle
*handle
, const char *channel_name
)
2195 xmlNodePtr context_node
;
2197 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2198 context_node
= xmlNextElementSibling(context_node
)) {
2199 ret
= process_context_node(context_node
, handle
, channel_name
);
2209 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2210 struct lttng_handle
*handle
)
2213 xmlNodePtr targets_node
= NULL
;
2217 assert(pid_tracker_node
);
2218 /* get the targets node */
2219 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2220 node
= xmlNextElementSibling(node
)) {
2221 if (!strcmp((const char *) node
->name
,
2222 config_element_targets
)) {
2223 targets_node
= node
;
2228 if (!targets_node
) {
2229 ret
= LTTNG_ERR_INVALID
;
2233 /* Go through all pid_target node */
2234 child
= xmlChildElementCount(targets_node
);
2236 /* The session is explicitly set to target nothing. */
2237 ret
= lttng_untrack_pid(handle
, -1);
2242 for (node
= xmlFirstElementChild(targets_node
); node
;
2243 node
= xmlNextElementSibling(node
)) {
2244 xmlNodePtr pid_target_node
= node
;
2246 /* get pid node and track it */
2247 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2248 node
= xmlNextElementSibling(node
)) {
2249 if (!strcmp((const char *) node
->name
,
2250 config_element_pid
)) {
2252 xmlChar
*content
= NULL
;
2254 content
= xmlNodeGetContent(node
);
2256 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2260 ret
= parse_int(content
, &pid
);
2263 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2267 ret
= lttng_track_pid(handle
, (int) pid
);
2273 node
= pid_target_node
;
2282 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2285 struct lttng_domain domain
= { 0 };
2286 struct lttng_handle
*handle
= NULL
;
2287 xmlNodePtr channels_node
= NULL
;
2288 xmlNodePtr trackers_node
= NULL
;
2289 xmlNodePtr pid_tracker_node
= NULL
;
2292 assert(session_name
);
2294 ret
= init_domain(domain_node
, &domain
);
2299 handle
= lttng_create_handle(session_name
, &domain
);
2301 ret
= -LTTNG_ERR_NOMEM
;
2305 /* get the channels node */
2306 for (node
= xmlFirstElementChild(domain_node
); node
;
2307 node
= xmlNextElementSibling(node
)) {
2308 if (!strcmp((const char *) node
->name
,
2309 config_element_channels
)) {
2310 channels_node
= node
;
2315 if (!channels_node
) {
2319 /* create all channels */
2320 for (node
= xmlFirstElementChild(channels_node
); node
;
2321 node
= xmlNextElementSibling(node
)) {
2322 struct lttng_channel channel
;
2323 xmlNodePtr contexts_node
= NULL
;
2324 xmlNodePtr events_node
= NULL
;
2325 xmlNodePtr channel_attr_node
;
2327 memset(&channel
, 0, sizeof(channel
));
2328 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2330 for (channel_attr_node
= xmlFirstElementChild(node
);
2331 channel_attr_node
; channel_attr_node
=
2332 xmlNextElementSibling(channel_attr_node
)) {
2333 ret
= process_channel_attr_node(channel_attr_node
,
2334 &channel
, &contexts_node
, &events_node
);
2340 ret
= lttng_enable_channel(handle
, &channel
);
2345 ret
= process_events_node(events_node
, handle
, channel
.name
);
2350 ret
= process_contexts_node(contexts_node
, handle
,
2357 /* get the trackers node */
2358 for (node
= xmlFirstElementChild(domain_node
); node
;
2359 node
= xmlNextElementSibling(node
)) {
2360 if (!strcmp((const char *) node
->name
,
2361 config_element_trackers
)) {
2362 trackers_node
= node
;
2367 if (!trackers_node
) {
2371 for (node
= xmlFirstElementChild(trackers_node
); node
;
2372 node
= xmlNextElementSibling(node
)) {
2373 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2374 pid_tracker_node
= node
;
2375 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2382 if (!pid_tracker_node
) {
2383 lttng_track_pid(handle
, -1);
2387 lttng_destroy_handle(handle
);
2392 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2395 int ret
, started
= -1, snapshot_mode
= -1;
2396 uint64_t live_timer_interval
= UINT64_MAX
;
2397 xmlChar
*name
= NULL
;
2398 xmlChar
*shm_path
= NULL
;
2399 xmlNodePtr domains_node
= NULL
;
2400 xmlNodePtr output_node
= NULL
;
2402 struct lttng_domain
*kernel_domain
= NULL
;
2403 struct lttng_domain
*ust_domain
= NULL
;
2404 struct lttng_domain
*jul_domain
= NULL
;
2405 struct lttng_domain
*log4j_domain
= NULL
;
2406 struct lttng_domain
*python_domain
= NULL
;
2408 for (node
= xmlFirstElementChild(session_node
); node
;
2409 node
= xmlNextElementSibling(node
)) {
2410 if (!name
&& !strcmp((const char *) node
->name
,
2411 config_element_name
)) {
2413 xmlChar
*node_content
= xmlNodeGetContent(node
);
2414 if (!node_content
) {
2415 ret
= -LTTNG_ERR_NOMEM
;
2419 name
= node_content
;
2420 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2421 config_element_domains
)) {
2423 domains_node
= node
;
2424 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2425 config_element_started
)) {
2427 xmlChar
*node_content
= xmlNodeGetContent(node
);
2428 if (!node_content
) {
2429 ret
= -LTTNG_ERR_NOMEM
;
2433 ret
= parse_bool(node_content
, &started
);
2436 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2439 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2440 config_element_output
)) {
2443 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2444 config_element_shared_memory_path
)) {
2445 /* shared memory path */
2446 xmlChar
*node_content
= xmlNodeGetContent(node
);
2447 if (!node_content
) {
2448 ret
= -LTTNG_ERR_NOMEM
;
2452 shm_path
= node_content
;
2454 /* attributes, snapshot_mode or live_timer_interval */
2455 xmlNodePtr attributes_child
=
2456 xmlFirstElementChild(node
);
2458 if (!strcmp((const char *) attributes_child
->name
,
2459 config_element_snapshot_mode
)) {
2461 xmlChar
*snapshot_mode_content
=
2462 xmlNodeGetContent(attributes_child
);
2463 if (!snapshot_mode_content
) {
2464 ret
= -LTTNG_ERR_NOMEM
;
2468 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2469 free(snapshot_mode_content
);
2471 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2475 /* live_timer_interval */
2476 xmlChar
*timer_interval_content
=
2477 xmlNodeGetContent(attributes_child
);
2478 if (!timer_interval_content
) {
2479 ret
= -LTTNG_ERR_NOMEM
;
2483 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2484 free(timer_interval_content
);
2486 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2494 /* Mandatory attribute, as defined in the session XSD */
2495 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2499 if (session_name
&& strcmp((char *) name
, session_name
)) {
2500 /* This is not the session we are looking for */
2501 ret
= -LTTNG_ERR_NO_SESSION
;
2505 /* Init domains to create the session handles */
2506 for (node
= xmlFirstElementChild(domains_node
); node
;
2507 node
= xmlNextElementSibling(node
)) {
2508 struct lttng_domain
*domain
;
2510 domain
= zmalloc(sizeof(*domain
));
2512 ret
= -LTTNG_ERR_NOMEM
;
2516 ret
= init_domain(node
, domain
);
2518 goto domain_init_error
;
2521 switch (domain
->type
) {
2522 case LTTNG_DOMAIN_KERNEL
:
2523 if (kernel_domain
) {
2524 /* Same domain seen twice, invalid! */
2525 goto domain_init_error
;
2527 kernel_domain
= domain
;
2529 case LTTNG_DOMAIN_UST
:
2531 /* Same domain seen twice, invalid! */
2532 goto domain_init_error
;
2534 ust_domain
= domain
;
2536 case LTTNG_DOMAIN_JUL
:
2538 /* Same domain seen twice, invalid! */
2539 goto domain_init_error
;
2541 jul_domain
= domain
;
2543 case LTTNG_DOMAIN_LOG4J
:
2545 /* Same domain seen twice, invalid! */
2546 goto domain_init_error
;
2548 log4j_domain
= domain
;
2550 case LTTNG_DOMAIN_PYTHON
:
2551 if (python_domain
) {
2552 /* Same domain seen twice, invalid! */
2553 goto domain_init_error
;
2555 python_domain
= domain
;
2558 WARN("Invalid domain type");
2559 goto domain_init_error
;
2564 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2569 /* Destroy session if it exists */
2570 ret
= lttng_destroy_session((const char *) name
);
2571 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2572 ERR("Failed to destroy existing session.");
2577 /* Create session type depending on output type */
2578 if (snapshot_mode
&& snapshot_mode
!= -1) {
2579 ret
= create_snapshot_session((const char *) name
, output_node
);
2580 } else if (live_timer_interval
&&
2581 live_timer_interval
!= UINT64_MAX
) {
2582 ret
= create_session((const char *) name
, kernel_domain
,
2583 ust_domain
, jul_domain
, log4j_domain
,
2584 output_node
, live_timer_interval
);
2586 /* regular session */
2587 ret
= create_session((const char *) name
, kernel_domain
,
2588 ust_domain
, jul_domain
, log4j_domain
,
2589 output_node
, UINT64_MAX
);
2596 ret
= lttng_set_session_shm_path((const char *) name
,
2597 (const char *) shm_path
);
2603 for (node
= xmlFirstElementChild(domains_node
); node
;
2604 node
= xmlNextElementSibling(node
)) {
2605 ret
= process_domain_node(node
, (const char *) name
);
2612 ret
= lttng_start_tracing((const char *) name
);
2620 ERR("Failed to load session %s: %s", (const char *) name
,
2621 lttng_strerror(ret
));
2622 lttng_destroy_session((const char *) name
);
2626 free(kernel_domain
);
2630 free(python_domain
);
2637 * Return 1 if the given path is readable by the current UID or 0 if not.
2638 * Return -1 if the path is EPERM.
2640 static int validate_file_read_creds(const char *path
)
2646 /* Can we read the file. */
2647 ret
= access(path
, R_OK
);
2651 if (errno
== EACCES
) {
2662 int load_session_from_file(const char *path
, const char *session_name
,
2663 struct session_config_validation_ctx
*validation_ctx
, int override
)
2665 int ret
, session_found
= !session_name
;
2666 xmlDocPtr doc
= NULL
;
2667 xmlNodePtr sessions_node
;
2668 xmlNodePtr session_node
;
2671 assert(validation_ctx
);
2673 ret
= validate_file_read_creds(path
);
2676 ret
= -LTTNG_ERR_EPERM
;
2678 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2683 doc
= xmlParseFile(path
);
2685 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2689 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2691 ERR("Session configuration file validation failed");
2692 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2696 sessions_node
= xmlDocGetRootElement(doc
);
2697 if (!sessions_node
) {
2701 for (session_node
= xmlFirstElementChild(sessions_node
);
2702 session_node
; session_node
=
2703 xmlNextElementSibling(session_node
)) {
2704 ret
= process_session_node(session_node
,
2705 session_name
, override
);
2706 if (session_name
&& ret
== 0) {
2707 /* Target session found and loaded */
2715 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2720 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2722 struct dirent
*alloc_dirent(const char *path
)
2726 struct dirent
*entry
;
2728 name_max
= pathconf(path
, _PC_NAME_MAX
);
2729 if (name_max
== -1) {
2730 name_max
= PATH_MAX
;
2732 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2733 entry
= zmalloc(len
);
2738 int load_session_from_path(const char *path
, const char *session_name
,
2739 struct session_config_validation_ctx
*validation_ctx
, int override
)
2741 int ret
, session_found
= !session_name
;
2742 DIR *directory
= NULL
;
2745 assert(validation_ctx
);
2747 directory
= opendir(path
);
2751 /* Try the file loading. */
2754 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2757 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2762 struct dirent
*entry
;
2763 struct dirent
*result
;
2764 char *file_path
= NULL
;
2765 size_t path_len
= strlen(path
);
2767 if (path_len
>= PATH_MAX
) {
2768 ret
= -LTTNG_ERR_INVALID
;
2772 entry
= alloc_dirent(path
);
2774 ret
= -LTTNG_ERR_NOMEM
;
2778 file_path
= zmalloc(PATH_MAX
);
2780 ret
= -LTTNG_ERR_NOMEM
;
2785 strncpy(file_path
, path
, path_len
);
2786 if (file_path
[path_len
- 1] != '/') {
2787 file_path
[path_len
++] = '/';
2791 /* Search for *.lttng files */
2792 while (!readdir_r(directory
, entry
, &result
) && result
) {
2793 size_t file_name_len
= strlen(result
->d_name
);
2795 if (file_name_len
<=
2796 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2800 if (path_len
+ file_name_len
>= PATH_MAX
) {
2804 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2805 result
->d_name
+ file_name_len
- sizeof(
2806 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2810 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2811 file_path
[path_len
+ file_name_len
] = '\0';
2813 ret
= load_session_from_file(file_path
, session_name
,
2814 validation_ctx
, override
);
2815 if (session_name
&& !ret
) {
2824 ret
= load_session_from_file(path
, session_name
,
2825 validation_ctx
, override
);
2835 if (closedir(directory
)) {
2840 if (!session_found
) {
2841 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2848 * Validate that the given path's credentials and the current process have the
2849 * same UID. If so, return 1 else return 0 if it does NOT match.
2851 static int validate_path_creds(const char *path
)
2853 int ret
, uid
= getuid();
2862 ret
= stat(path
, &buf
);
2864 if (errno
!= ENOENT
) {
2867 ret
= -LTTNG_ERR_INVALID
;
2871 if (buf
.st_uid
!= uid
) {
2882 int config_load_session(const char *path
, const char *session_name
,
2883 int override
, unsigned int autoload
)
2886 const char *path_ptr
= NULL
;
2887 struct session_config_validation_ctx validation_ctx
= { 0 };
2889 ret
= init_session_config_validation_ctx(&validation_ctx
);
2896 const char *sys_path
;
2899 home_path
= utils_get_home_dir();
2901 char path
[PATH_MAX
];
2904 * Try user session configuration path. Ignore error here so we can
2905 * continue loading the system wide sessions.
2908 ret
= snprintf(path
, sizeof(path
),
2909 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2910 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2912 PERROR("snprintf session autoload home config path");
2917 * Credentials are only validated for the autoload in order to
2918 * avoid any user session daemon to try to load kernel sessions
2919 * automatically and failing all the times.
2921 ret
= validate_path_creds(path
);
2926 ret
= snprintf(path
, sizeof(path
),
2927 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2929 PERROR("snprintf session home config path");
2935 ret
= load_session_from_path(path_ptr
, session_name
,
2936 &validation_ctx
, override
);
2937 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2941 * Continue even if the session was found since we have to try
2942 * the system wide sessions.
2947 /* Reset path pointer for the system wide dir. */
2950 /* Try system wide configuration directory. */
2952 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2953 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2954 ret
= validate_path_creds(sys_path
);
2956 path_ptr
= sys_path
;
2959 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2960 path_ptr
= sys_path
;
2964 ret
= load_session_from_path(path_ptr
, session_name
,
2965 &validation_ctx
, override
);
2968 ret
= access(path
, F_OK
);
2973 ret
= -LTTNG_ERR_INVALID
;
2974 WARN("Session configuration path does not exist.");
2977 ret
= -LTTNG_ERR_EPERM
;
2980 ret
= -LTTNG_ERR_UNK
;
2986 ret
= load_session_from_path(path
, session_name
,
2987 &validation_ctx
, override
);
2990 fini_session_config_validation_ctx(&validation_ctx
);
2991 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2993 * Don't report an error if no sessions are found when called
2994 * without a session_name or a search path.