2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <common/dynamic-buffer.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>
44 #include <lttng/rotation.h>
45 #include <lttng/userspace-probe.h>
47 #include "session-config.h"
48 #include "config-internal.h"
50 #define CONFIG_USERSPACE_PROBE_LOOKUP_METHOD_NAME_MAX_LEN 7
52 struct handler_filter_args
{
54 config_entry_handler_cb handler
;
58 struct session_config_validation_ctx
{
59 xmlSchemaParserCtxtPtr parser_ctx
;
61 xmlSchemaValidCtxtPtr schema_validation_ctx
;
64 const char * const config_str_yes
= "yes";
65 const char * const config_str_true
= "true";
66 const char * const config_str_on
= "on";
67 const char * const config_str_no
= "no";
68 const char * const config_str_false
= "false";
69 const char * const config_str_off
= "off";
70 const char * const config_xml_encoding
= "UTF-8";
71 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
72 const char * const config_xml_indent_string
= "\t";
73 const char * const config_xml_true
= "true";
74 const char * const config_xml_false
= "false";
76 const char * const config_element_channel
= "channel";
77 const char * const config_element_channels
= "channels";
78 const char * const config_element_domain
= "domain";
79 const char * const config_element_domains
= "domains";
80 const char * const config_element_event
= "event";
81 const char * const config_element_events
= "events";
82 const char * const config_element_context
= "context";
83 const char * const config_element_contexts
= "contexts";
84 const char * const config_element_attributes
= "attributes";
85 const char * const config_element_exclusion
= "exclusion";
86 const char * const config_element_exclusions
= "exclusions";
87 const char * const config_element_function_attributes
= "function_attributes";
88 const char * const config_element_probe_attributes
= "probe_attributes";
89 const char * const config_element_symbol_name
= "symbol_name";
90 const char * const config_element_address
= "address";
91 const char * const config_element_offset
= "offset";
93 LTTNG_HIDDEN
const char * const config_element_userspace_probe_lookup
= "lookup_method";
94 LTTNG_HIDDEN
const char * const config_element_userspace_probe_lookup_function_default
= "DEFAULT";
95 LTTNG_HIDDEN
const char * const config_element_userspace_probe_lookup_function_elf
= "ELF";
96 LTTNG_HIDDEN
const char * const config_element_userspace_probe_lookup_tracepoint_sdt
= "SDT";
97 LTTNG_HIDDEN
const char * const config_element_userspace_probe_location_binary_path
= "binary_path";
98 LTTNG_HIDDEN
const char * const config_element_userspace_probe_function_attributes
= "userspace_probe_function_attributes";
99 LTTNG_HIDDEN
const char * const config_element_userspace_probe_function_location_function_name
= "function_name";
100 LTTNG_HIDDEN
const char * const config_element_userspace_probe_tracepoint_attributes
= "userspace_probe_tracepoint_attributes";
101 LTTNG_HIDDEN
const char * const config_element_userspace_probe_tracepoint_location_provider_name
= "provider_name";
102 LTTNG_HIDDEN
const char * const config_element_userspace_probe_tracepoint_location_probe_name
= "probe_name";
104 const char * const config_element_name
= "name";
105 const char * const config_element_enabled
= "enabled";
106 const char * const config_element_overwrite_mode
= "overwrite_mode";
107 const char * const config_element_subbuf_size
= "subbuffer_size";
108 const char * const config_element_num_subbuf
= "subbuffer_count";
109 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
110 const char * const config_element_read_timer_interval
= "read_timer_interval";
111 LTTNG_HIDDEN
const char * const config_element_monitor_timer_interval
= "monitor_timer_interval";
112 LTTNG_HIDDEN
const char * const config_element_blocking_timeout
= "blocking_timeout";
113 const char * const config_element_output
= "output";
114 const char * const config_element_output_type
= "output_type";
115 const char * const config_element_tracefile_size
= "tracefile_size";
116 const char * const config_element_tracefile_count
= "tracefile_count";
117 const char * const config_element_live_timer_interval
= "live_timer_interval";
118 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
119 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
120 const char * const config_element_type
= "type";
121 const char * const config_element_buffer_type
= "buffer_type";
122 const char * const config_element_session
= "session";
123 const char * const config_element_sessions
= "sessions";
124 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
125 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
126 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
127 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
128 const char * const config_element_config
= "config";
129 const char * const config_element_started
= "started";
130 const char * const config_element_snapshot_mode
= "snapshot_mode";
131 const char * const config_element_loglevel
= "loglevel";
132 const char * const config_element_loglevel_type
= "loglevel_type";
133 const char * const config_element_filter
= "filter";
134 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
135 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
136 const char * const config_element_consumer_output
= "consumer_output";
137 const char * const config_element_destination
= "destination";
138 const char * const config_element_path
= "path";
139 const char * const config_element_net_output
= "net_output";
140 const char * const config_element_control_uri
= "control_uri";
141 const char * const config_element_data_uri
= "data_uri";
142 const char * const config_element_max_size
= "max_size";
143 const char * const config_element_pid
= "pid";
144 const char * const config_element_pids
= "pids";
145 const char * const config_element_shared_memory_path
= "shared_memory_path";
146 const char * const config_element_pid_tracker
= "pid_tracker";
147 const char * const config_element_trackers
= "trackers";
148 const char * const config_element_targets
= "targets";
149 const char * const config_element_target_pid
= "pid_target";
151 LTTNG_HIDDEN
const char * const config_element_rotation_schedules
= "rotation_schedules";
152 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_periodic
= "periodic";
153 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_periodic_time_us
= "time_us";
154 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_size_threshold
= "size_threshold";
155 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_size_threshold_bytes
= "bytes";
157 const char * const config_domain_type_kernel
= "KERNEL";
158 const char * const config_domain_type_ust
= "UST";
159 const char * const config_domain_type_jul
= "JUL";
160 const char * const config_domain_type_log4j
= "LOG4J";
161 const char * const config_domain_type_python
= "PYTHON";
163 const char * const config_buffer_type_per_pid
= "PER_PID";
164 const char * const config_buffer_type_per_uid
= "PER_UID";
165 const char * const config_buffer_type_global
= "GLOBAL";
167 const char * const config_overwrite_mode_discard
= "DISCARD";
168 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
170 const char * const config_output_type_splice
= "SPLICE";
171 const char * const config_output_type_mmap
= "MMAP";
173 const char * const config_loglevel_type_all
= "ALL";
174 const char * const config_loglevel_type_range
= "RANGE";
175 const char * const config_loglevel_type_single
= "SINGLE";
177 const char * const config_event_type_all
= "ALL";
178 const char * const config_event_type_tracepoint
= "TRACEPOINT";
179 const char * const config_event_type_probe
= "PROBE";
180 const char * const config_event_type_userspace_probe
= "USERSPACE_PROBE";
181 const char * const config_event_type_function
= "FUNCTION";
182 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
183 const char * const config_event_type_noop
= "NOOP";
184 const char * const config_event_type_syscall
= "SYSCALL";
185 const char * const config_event_type_kprobe
= "KPROBE";
186 const char * const config_event_type_kretprobe
= "KRETPROBE";
188 const char * const config_event_context_pid
= "PID";
189 const char * const config_event_context_procname
= "PROCNAME";
190 const char * const config_event_context_prio
= "PRIO";
191 const char * const config_event_context_nice
= "NICE";
192 const char * const config_event_context_vpid
= "VPID";
193 const char * const config_event_context_tid
= "TID";
194 const char * const config_event_context_vtid
= "VTID";
195 const char * const config_event_context_ppid
= "PPID";
196 const char * const config_event_context_vppid
= "VPPID";
197 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
198 const char * const config_event_context_hostname
= "HOSTNAME";
199 const char * const config_event_context_ip
= "IP";
200 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
201 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
202 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
203 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
204 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
205 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
206 LTTNG_HIDDEN
const char * const config_event_context_callstack_user
= "CALLSTACK_USER";
207 LTTNG_HIDDEN
const char * const config_event_context_callstack_kernel
= "CALLSTACK_KERNEL";
209 /* Deprecated symbols */
210 const char * const config_element_perf
;
212 enum process_event_node_phase
{
217 struct consumer_output
{
224 static int config_entry_handler_filter(struct handler_filter_args
*args
,
225 const char *section
, const char *name
, const char *value
)
228 struct config_entry entry
= { section
, name
, value
};
232 if (!section
|| !name
|| !value
) {
238 if (strcmp(args
->section
, section
)) {
243 ret
= args
->handler(&entry
, args
->user_data
);
249 int config_get_section_entries(const char *override_path
, const char *section
,
250 config_entry_handler_cb handler
, void *user_data
)
254 FILE *config_file
= NULL
;
255 struct handler_filter_args filter
= { section
, handler
, user_data
};
257 /* First, try system-wide conf. file. */
258 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
260 config_file
= fopen(path
, "r");
262 DBG("Loading daemon conf file at %s", path
);
264 * Return value is not very important here since error or not, we
265 * continue and try the next possible conf. file.
267 (void) ini_parse_file(config_file
,
268 (ini_entry_handler
) config_entry_handler_filter
,
273 /* Second is the user local configuration. */
274 path
= utils_get_home_dir();
276 char fullpath
[PATH_MAX
];
278 ret
= snprintf(fullpath
, sizeof(fullpath
),
279 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
281 PERROR("snprintf user conf. path");
285 config_file
= fopen(fullpath
, "r");
287 DBG("Loading daemon user conf file at %s", path
);
289 * Return value is not very important here since error or not, we
290 * continue and try the next possible conf. file.
292 (void) ini_parse_file(config_file
,
293 (ini_entry_handler
) config_entry_handler_filter
,
299 /* Final path is the one that the user might have provided. */
301 config_file
= fopen(override_path
, "r");
303 DBG("Loading daemon command line conf file at %s", override_path
);
304 (void) ini_parse_file(config_file
,
305 (ini_entry_handler
) config_entry_handler_filter
,
309 ERR("Failed to open daemon configuration file at %s",
316 /* Everything went well. */
324 int config_parse_value(const char *value
)
327 char *endptr
, *lower_str
;
337 v
= strtoul(value
, &endptr
, 10);
338 if (endptr
!= value
) {
343 lower_str
= zmalloc(len
+ 1);
350 for (i
= 0; i
< len
; i
++) {
351 lower_str
[i
] = tolower(value
[i
]);
354 if (!strcmp(lower_str
, config_str_yes
) ||
355 !strcmp(lower_str
, config_str_true
) ||
356 !strcmp(lower_str
, config_str_on
)) {
358 } else if (!strcmp(lower_str
, config_str_no
) ||
359 !strcmp(lower_str
, config_str_false
) ||
360 !strcmp(lower_str
, config_str_off
)) {
372 * Returns a xmlChar string which must be released using xmlFree().
374 static xmlChar
*encode_string(const char *in_str
)
376 xmlChar
*out_str
= NULL
;
377 xmlCharEncodingHandlerPtr handler
;
378 int out_len
, ret
, in_len
;
382 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
384 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
388 in_len
= strlen(in_str
);
390 * Add 1 byte for the NULL terminted character. The factor 4 here is
391 * used because UTF-8 characters can take up to 4 bytes.
393 out_len
= (in_len
* 4) + 1;
394 out_str
= xmlMalloc(out_len
);
399 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
406 /* out_len is now the size of out_str */
407 out_str
[out_len
] = '\0';
413 struct config_writer
*config_writer_create(int fd_output
, int indent
)
416 struct config_writer
*writer
;
417 xmlOutputBufferPtr buffer
;
419 writer
= zmalloc(sizeof(struct config_writer
));
421 PERROR("zmalloc config_writer_create");
425 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
430 writer
->writer
= xmlNewTextWriter(buffer
);
431 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
432 config_xml_encoding
, NULL
);
437 ret
= xmlTextWriterSetIndentString(writer
->writer
,
438 BAD_CAST config_xml_indent_string
);
443 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
451 config_writer_destroy(writer
);
456 int config_writer_destroy(struct config_writer
*writer
)
465 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
466 WARN("Could not close XML document");
470 if (writer
->writer
) {
471 xmlFreeTextWriter(writer
->writer
);
480 int config_writer_open_element(struct config_writer
*writer
,
481 const char *element_name
)
484 xmlChar
*encoded_element_name
;
486 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
491 encoded_element_name
= encode_string(element_name
);
492 if (!encoded_element_name
) {
497 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
498 xmlFree(encoded_element_name
);
500 return ret
>= 0 ? 0 : ret
;
504 int config_writer_write_attribute(struct config_writer
*writer
,
505 const char *name
, const char *value
)
508 xmlChar
*encoded_name
= NULL
;
509 xmlChar
*encoded_value
= NULL
;
511 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
516 encoded_name
= encode_string(name
);
522 encoded_value
= encode_string(value
);
523 if (!encoded_value
) {
528 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
531 xmlFree(encoded_name
);
532 xmlFree(encoded_value
);
533 return ret
>= 0 ? 0 : ret
;
537 int config_writer_close_element(struct config_writer
*writer
)
541 if (!writer
|| !writer
->writer
) {
546 ret
= xmlTextWriterEndElement(writer
->writer
);
548 return ret
>= 0 ? 0 : ret
;
552 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
553 const char *element_name
, uint64_t value
)
556 xmlChar
*encoded_element_name
;
558 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
563 encoded_element_name
= encode_string(element_name
);
564 if (!encoded_element_name
) {
569 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
570 encoded_element_name
, "%" PRIu64
, value
);
571 xmlFree(encoded_element_name
);
573 return ret
>= 0 ? 0 : ret
;
577 int config_writer_write_element_signed_int(struct config_writer
*writer
,
578 const char *element_name
, int64_t value
)
581 xmlChar
*encoded_element_name
;
583 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
588 encoded_element_name
= encode_string(element_name
);
589 if (!encoded_element_name
) {
594 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
595 encoded_element_name
, "%" PRIi64
, value
);
596 xmlFree(encoded_element_name
);
598 return ret
>= 0 ? 0 : ret
;
602 int config_writer_write_element_bool(struct config_writer
*writer
,
603 const char *element_name
, int value
)
605 return config_writer_write_element_string(writer
, element_name
,
606 value
? config_xml_true
: config_xml_false
);
610 int config_writer_write_element_string(struct config_writer
*writer
,
611 const char *element_name
, const char *value
)
614 xmlChar
*encoded_element_name
= NULL
;
615 xmlChar
*encoded_value
= NULL
;
617 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
623 encoded_element_name
= encode_string(element_name
);
624 if (!encoded_element_name
) {
629 encoded_value
= encode_string(value
);
630 if (!encoded_value
) {
635 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
638 xmlFree(encoded_element_name
);
639 xmlFree(encoded_value
);
640 return ret
>= 0 ? 0 : ret
;
644 void xml_error_handler(void *ctx
, const char *format
, ...)
650 va_start(args
, format
);
651 ret
= vasprintf(&errMsg
, format
, args
);
654 ERR("String allocation failed in xml error handler");
658 fprintf(stderr
, "XML Error: %s", errMsg
);
663 void fini_session_config_validation_ctx(
664 struct session_config_validation_ctx
*ctx
)
666 if (ctx
->parser_ctx
) {
667 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
671 xmlSchemaFree(ctx
->schema
);
674 if (ctx
->schema_validation_ctx
) {
675 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
678 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
682 char *get_session_config_xsd_path()
685 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
686 size_t base_path_len
;
690 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
693 base_path_len
= strlen(base_path
);
694 max_path_len
= base_path_len
+
695 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
696 xsd_path
= zmalloc(max_path_len
);
701 strcpy(xsd_path
, base_path
);
702 if (xsd_path
[base_path_len
- 1] != '/') {
703 xsd_path
[base_path_len
++] = '/';
706 strcpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
);
712 int init_session_config_validation_ctx(
713 struct session_config_validation_ctx
*ctx
)
716 char *xsd_path
= get_session_config_xsd_path();
719 ret
= -LTTNG_ERR_NOMEM
;
723 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
724 if (!ctx
->parser_ctx
) {
725 ERR("XSD parser context creation failed");
726 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
729 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
730 xml_error_handler
, NULL
);
732 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
734 ERR("XSD parsing failed");
735 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
739 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
740 if (!ctx
->schema_validation_ctx
) {
741 ERR("XSD validation context creation failed");
742 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
746 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
747 xml_error_handler
, NULL
);
752 fini_session_config_validation_ctx(ctx
);
760 int parse_uint(xmlChar
*str
, uint64_t *val
)
770 *val
= strtoull((const char *) str
, &endptr
, 10);
771 if (!endptr
|| *endptr
) {
782 int parse_int(xmlChar
*str
, int64_t *val
)
792 *val
= strtoll((const char *) str
, &endptr
, 10);
793 if (!endptr
|| *endptr
) {
804 int parse_bool(xmlChar
*str
, int *val
)
813 if (!strcmp((const char *) str
, config_xml_true
)) {
815 } else if (!strcmp((const char *) str
, config_xml_false
)) {
818 WARN("Invalid boolean value encoutered (%s).",
827 int get_domain_type(xmlChar
*domain
)
835 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
836 ret
= LTTNG_DOMAIN_KERNEL
;
837 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
838 ret
= LTTNG_DOMAIN_UST
;
839 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
840 ret
= LTTNG_DOMAIN_JUL
;
841 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
842 ret
= LTTNG_DOMAIN_LOG4J
;
843 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
844 ret
= LTTNG_DOMAIN_PYTHON
;
855 int get_buffer_type(xmlChar
*buffer_type
)
863 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
864 ret
= LTTNG_BUFFER_GLOBAL
;
865 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
866 ret
= LTTNG_BUFFER_PER_UID
;
867 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
868 ret
= LTTNG_BUFFER_PER_PID
;
879 int get_overwrite_mode(xmlChar
*overwrite_mode
)
883 if (!overwrite_mode
) {
887 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
889 } else if (!strcmp((char *) overwrite_mode
,
890 config_overwrite_mode_discard
)) {
902 int get_output_type(xmlChar
*output_type
)
910 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
911 ret
= LTTNG_EVENT_MMAP
;
912 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
913 ret
= LTTNG_EVENT_SPLICE
;
924 int get_event_type(xmlChar
*event_type
)
932 if (!strcmp((char *) event_type
, config_event_type_all
)) {
933 ret
= LTTNG_EVENT_ALL
;
934 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
935 ret
= LTTNG_EVENT_TRACEPOINT
;
936 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
937 ret
= LTTNG_EVENT_PROBE
;
938 } else if (!strcmp((char *) event_type
,
939 config_event_type_userspace_probe
)) {
940 ret
= LTTNG_EVENT_USERSPACE_PROBE
;
941 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
942 ret
= LTTNG_EVENT_FUNCTION
;
943 } else if (!strcmp((char *) event_type
,
944 config_event_type_function_entry
)) {
945 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
946 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
947 ret
= LTTNG_EVENT_NOOP
;
948 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
949 ret
= LTTNG_EVENT_SYSCALL
;
960 int get_loglevel_type(xmlChar
*loglevel_type
)
964 if (!loglevel_type
) {
968 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
969 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
970 } else if (!strcmp((char *) loglevel_type
,
971 config_loglevel_type_range
)) {
972 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
973 } else if (!strcmp((char *) loglevel_type
,
974 config_loglevel_type_single
)) {
975 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
986 * Return the context type or -1 on error.
989 int get_context_type(xmlChar
*context_type
)
997 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
998 ret
= LTTNG_EVENT_CONTEXT_PID
;
999 } else if (!strcmp((char *) context_type
,
1000 config_event_context_procname
)) {
1001 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1002 } else if (!strcmp((char *) context_type
,
1003 config_event_context_prio
)) {
1004 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
1005 } else if (!strcmp((char *) context_type
,
1006 config_event_context_nice
)) {
1007 ret
= LTTNG_EVENT_CONTEXT_NICE
;
1008 } else if (!strcmp((char *) context_type
,
1009 config_event_context_vpid
)) {
1010 ret
= LTTNG_EVENT_CONTEXT_VPID
;
1011 } else if (!strcmp((char *) context_type
,
1012 config_event_context_tid
)) {
1013 ret
= LTTNG_EVENT_CONTEXT_TID
;
1014 } else if (!strcmp((char *) context_type
,
1015 config_event_context_vtid
)) {
1016 ret
= LTTNG_EVENT_CONTEXT_VTID
;
1017 } else if (!strcmp((char *) context_type
,
1018 config_event_context_ppid
)) {
1019 ret
= LTTNG_EVENT_CONTEXT_PPID
;
1020 } else if (!strcmp((char *) context_type
,
1021 config_event_context_vppid
)) {
1022 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1023 } else if (!strcmp((char *) context_type
,
1024 config_event_context_pthread_id
)) {
1025 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1026 } else if (!strcmp((char *) context_type
,
1027 config_event_context_hostname
)) {
1028 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1029 } else if (!strcmp((char *) context_type
,
1030 config_event_context_ip
)) {
1031 ret
= LTTNG_EVENT_CONTEXT_IP
;
1032 } else if (!strcmp((char *) context_type
,
1033 config_event_context_interruptible
)) {
1034 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1035 } else if (!strcmp((char *) context_type
,
1036 config_event_context_preemptible
)) {
1037 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1038 } else if (!strcmp((char *) context_type
,
1039 config_event_context_need_reschedule
)) {
1040 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1041 } else if (!strcmp((char *) context_type
,
1042 config_event_context_migratable
)) {
1043 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1044 } else if (!strcmp((char *) context_type
,
1045 config_event_context_callstack_user
)) {
1046 ret
= LTTNG_EVENT_CONTEXT_CALLSTACK_USER
;
1047 } else if (!strcmp((char *) context_type
,
1048 config_event_context_callstack_kernel
)) {
1049 ret
= LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL
;
1060 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1065 for (node
= xmlFirstElementChild(domain_node
); node
;
1066 node
= xmlNextElementSibling(node
)) {
1067 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1069 xmlChar
*node_content
= xmlNodeGetContent(node
);
1070 if (!node_content
) {
1071 ret
= -LTTNG_ERR_NOMEM
;
1075 ret
= get_domain_type(node_content
);
1078 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1083 } else if (!strcmp((const char *) node
->name
,
1084 config_element_buffer_type
)) {
1086 xmlChar
*node_content
= xmlNodeGetContent(node
);
1087 if (!node_content
) {
1088 ret
= -LTTNG_ERR_NOMEM
;
1092 ret
= get_buffer_type(node_content
);
1095 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1099 domain
->buf_type
= ret
;
1108 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1113 for (node
= xmlFirstElementChild(net_output_node
); node
;
1114 node
= xmlNextElementSibling(node
)) {
1115 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1117 *control_uri
= (char *) xmlNodeGetContent(node
);
1118 if (!*control_uri
) {
1123 *data_uri
= (char *) xmlNodeGetContent(node
);
1130 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1134 int process_consumer_output(xmlNodePtr consumer_output_node
,
1135 struct consumer_output
*output
)
1142 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1143 node
= xmlNextElementSibling(node
)) {
1144 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1145 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1149 ret
= -LTTNG_ERR_NOMEM
;
1153 ret
= parse_bool(enabled_str
, &output
->enabled
);
1159 xmlNodePtr output_type_node
;
1162 output_type_node
= xmlFirstElementChild(node
);
1163 if (!output_type_node
) {
1164 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1168 if (!strcmp((const char *) output_type_node
->name
,
1169 config_element_path
)) {
1171 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1172 if (!output
->path
) {
1173 ret
= -LTTNG_ERR_NOMEM
;
1178 ret
= get_net_output_uris(output_type_node
,
1179 &output
->control_uri
, &output
->data_uri
);
1191 free(output
->control_uri
);
1192 free(output
->data_uri
);
1193 memset(output
, 0, sizeof(struct consumer_output
));
1199 int create_session_net_output(const char *name
, const char *control_uri
,
1200 const char *data_uri
)
1203 struct lttng_handle
*handle
;
1204 const char *uri
= NULL
;
1208 handle
= lttng_create_handle(name
, NULL
);
1210 ret
= -LTTNG_ERR_NOMEM
;
1214 if (!control_uri
|| !data_uri
) {
1215 uri
= control_uri
? control_uri
: data_uri
;
1220 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1221 lttng_destroy_handle(handle
);
1227 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
,
1228 const struct config_load_session_override_attr
*overrides
)
1231 xmlNodePtr node
= NULL
;
1232 xmlNodePtr snapshot_output_list_node
;
1233 xmlNodePtr snapshot_output_node
;
1235 assert(session_name
);
1237 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1246 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1248 /* Parse and create snapshot outputs */
1250 for (snapshot_output_node
=
1251 xmlFirstElementChild(snapshot_output_list_node
);
1252 snapshot_output_node
; snapshot_output_node
=
1253 xmlNextElementSibling(snapshot_output_node
)) {
1255 uint64_t max_size
= UINT64_MAX
;
1256 struct consumer_output output
= { 0 };
1257 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1258 const char *control_uri
= NULL
;
1259 const char *data_uri
= NULL
;
1260 const char *path
= NULL
;
1262 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1263 node
= xmlNextElementSibling(node
)) {
1264 if (!strcmp((const char *) node
->name
,
1265 config_element_name
)) {
1267 name
= (char *) xmlNodeGetContent(node
);
1269 ret
= -LTTNG_ERR_NOMEM
;
1270 goto error_snapshot_output
;
1272 } else if (!strcmp((const char *) node
->name
,
1273 config_element_max_size
)) {
1274 xmlChar
*content
= xmlNodeGetContent(node
);
1278 ret
= -LTTNG_ERR_NOMEM
;
1279 goto error_snapshot_output
;
1281 ret
= parse_uint(content
, &max_size
);
1284 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1285 goto error_snapshot_output
;
1288 /* consumer_output */
1289 ret
= process_consumer_output(node
, &output
);
1291 goto error_snapshot_output
;
1296 control_uri
= output
.control_uri
;
1297 data_uri
= output
.data_uri
;
1301 if (overrides
->path_url
) {
1302 path
= overrides
->path_url
;
1303 /* Control/data_uri are null */
1307 if (overrides
->ctrl_url
) {
1308 control_uri
= overrides
->ctrl_url
;
1312 if (overrides
->data_url
) {
1313 data_uri
= overrides
->data_url
;
1320 snapshot_output
= lttng_snapshot_output_create();
1321 if (!snapshot_output
) {
1322 ret
= -LTTNG_ERR_NOMEM
;
1323 goto error_snapshot_output
;
1326 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1328 goto error_snapshot_output
;
1331 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1333 goto error_snapshot_output
;
1337 ret
= lttng_snapshot_output_set_ctrl_url(path
,
1340 goto error_snapshot_output
;
1344 ret
= lttng_snapshot_output_set_ctrl_url(control_uri
,
1347 goto error_snapshot_output
;
1352 ret
= lttng_snapshot_output_set_data_url(data_uri
,
1355 goto error_snapshot_output
;
1360 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1361 error_snapshot_output
:
1364 free(output
.control_uri
);
1365 free(output
.data_uri
);
1366 lttng_snapshot_output_destroy(snapshot_output
);
1376 int create_session(const char *name
,
1377 xmlNodePtr output_node
,
1378 uint64_t live_timer_interval
,
1379 const struct config_load_session_override_attr
*overrides
)
1382 struct consumer_output output
= { 0 };
1383 xmlNodePtr consumer_output_node
;
1384 const char *control_uri
= NULL
;
1385 const char *data_uri
= NULL
;
1386 const char *path
= NULL
;
1391 consumer_output_node
= xmlFirstElementChild(output_node
);
1392 if (!consumer_output_node
) {
1393 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1397 if (strcmp((const char *) consumer_output_node
->name
,
1398 config_element_consumer_output
)) {
1399 WARN("Invalid output type, expected %s node",
1400 config_element_consumer_output
);
1401 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1405 ret
= process_consumer_output(consumer_output_node
, &output
);
1411 control_uri
= output
.control_uri
;
1412 data_uri
= output
.data_uri
;
1415 /* Check for override and apply them */
1417 if (overrides
->path_url
) {
1418 path
= overrides
->path_url
;
1419 /* control/data_uri are null */;
1423 if (overrides
->ctrl_url
) {
1424 control_uri
= overrides
->ctrl_url
;
1428 if (overrides
->data_url
) {
1429 data_uri
= overrides
->data_url
;
1437 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1438 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1442 if (control_uri
|| data_uri
) {
1443 /* network destination */
1444 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1446 * URLs are provided for sure since the test above make sure that
1447 * with a live timer the data and control URIs are provided. So,
1448 * NULL is passed here and will be set right after.
1450 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1452 ret
= lttng_create_session(name
, NULL
);
1458 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1464 /* either local output or no output */
1465 ret
= lttng_create_session(name
, path
);
1472 free(output
.control_uri
);
1473 free(output
.data_uri
);
1478 struct lttng_userspace_probe_location
*
1479 process_userspace_probe_function_attribute_node(
1480 xmlNodePtr attribute_node
)
1482 xmlNodePtr function_attribute_node
;
1483 char *function_name
= NULL
, *binary_path
= NULL
;
1484 struct lttng_userspace_probe_location
*location
= NULL
;
1485 struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
1488 * Process userspace probe location function attributes. The order of
1489 * the fields are not guaranteed so we need to iterate over all fields
1490 * and check at the end if everything we need for this location type is
1493 for (function_attribute_node
=
1494 xmlFirstElementChild(attribute_node
);
1495 function_attribute_node
;
1496 function_attribute_node
= xmlNextElementSibling(
1497 function_attribute_node
)) {
1498 /* Handle function name, binary path and lookup method. */
1499 if (!strcmp((const char *) function_attribute_node
->name
,
1500 config_element_userspace_probe_function_location_function_name
)) {
1501 function_name
= (char *) xmlNodeGetContent(function_attribute_node
);
1502 if (!function_name
) {
1505 } else if (!strcmp((const char *) function_attribute_node
->name
,
1506 config_element_userspace_probe_location_binary_path
)) {
1507 binary_path
= (char *) xmlNodeGetContent(function_attribute_node
);
1511 } else if (!strcmp((const char *) function_attribute_node
->name
,
1512 config_element_userspace_probe_lookup
)) {
1513 char *lookup_method_name
;
1515 lookup_method_name
= (char *) xmlNodeGetContent(
1516 function_attribute_node
);
1517 if (!lookup_method_name
) {
1522 * function_default lookup method defaults to
1523 * function_elf lookup method at the moment.
1525 if (!strcmp(lookup_method_name
, config_element_userspace_probe_lookup_function_elf
)
1526 || !strcmp(lookup_method_name
, config_element_userspace_probe_lookup_function_default
)) {
1527 lookup_method
= lttng_userspace_probe_location_lookup_method_function_elf_create();
1528 if (!lookup_method
) {
1529 PERROR("Error creating function default/ELF lookup method");
1532 WARN("Unknown function lookup method");
1535 free(lookup_method_name
);
1536 if (!lookup_method
) {
1543 /* Check if all the necessary fields were found. */
1544 if (binary_path
&& function_name
&& lookup_method
) {
1545 /* Ownership of lookup_method is transferred. */
1547 lttng_userspace_probe_location_function_create(
1548 binary_path
, function_name
,
1550 lookup_method
= NULL
;
1555 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
1557 free(function_name
);
1562 struct lttng_userspace_probe_location
*
1563 process_userspace_probe_tracepoint_attribute_node(
1564 xmlNodePtr attribute_node
)
1566 xmlNodePtr tracepoint_attribute_node
;
1567 char *probe_name
= NULL
, *provider_name
= NULL
, *binary_path
= NULL
;
1568 struct lttng_userspace_probe_location
*location
= NULL
;
1569 struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
1572 * Process userspace probe location tracepoint attributes. The order of
1573 * the fields are not guaranteed so we need to iterate over all fields
1574 * and check at the end if everything we need for this location type is
1577 for (tracepoint_attribute_node
=
1578 xmlFirstElementChild(attribute_node
); tracepoint_attribute_node
;
1579 tracepoint_attribute_node
= xmlNextElementSibling(
1580 tracepoint_attribute_node
)) {
1581 if (!strcmp((const char *) tracepoint_attribute_node
->name
,
1582 config_element_userspace_probe_tracepoint_location_probe_name
)) {
1583 probe_name
= (char *) xmlNodeGetContent(tracepoint_attribute_node
);
1587 } else if (!strcmp((const char *) tracepoint_attribute_node
->name
,
1588 config_element_userspace_probe_tracepoint_location_provider_name
)) {
1589 provider_name
= (char *) xmlNodeGetContent(tracepoint_attribute_node
);
1590 if (!provider_name
) {
1593 } else if (!strcmp((const char *) tracepoint_attribute_node
->name
,
1594 config_element_userspace_probe_location_binary_path
)) {
1595 binary_path
= (char *) xmlNodeGetContent(tracepoint_attribute_node
);
1599 } else if (!strcmp((const char *) tracepoint_attribute_node
->name
,
1600 config_element_userspace_probe_lookup
)) {
1601 char *lookup_method_name
;
1603 lookup_method_name
= (char *) xmlNodeGetContent(
1604 tracepoint_attribute_node
);
1605 if (!lookup_method_name
) {
1609 if (!strcmp(lookup_method_name
,
1610 config_element_userspace_probe_lookup_tracepoint_sdt
)) {
1612 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
1613 if (!lookup_method
) {
1614 PERROR("Error creating tracepoint SDT lookup method");
1617 WARN("Unknown tracepoint lookup method");
1620 free(lookup_method_name
);
1621 if (!lookup_method
) {
1625 WARN("Unknown tracepoint attribute");
1629 /* Check if all the necessary fields were found. */
1630 if (binary_path
&& provider_name
&& probe_name
&& lookup_method
) {
1631 /* Ownership of lookup_method is transferred. */
1633 lttng_userspace_probe_location_tracepoint_create(
1634 binary_path
, provider_name
,
1635 probe_name
, lookup_method
);
1640 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
1642 free(provider_name
);
1648 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1649 struct lttng_event_probe_attr
*attr
)
1653 assert(probe_attribute_node
);
1656 if (!strcmp((const char *) probe_attribute_node
->name
,
1657 config_element_address
)) {
1662 content
= xmlNodeGetContent(probe_attribute_node
);
1664 ret
= -LTTNG_ERR_NOMEM
;
1668 ret
= parse_uint(content
, &addr
);
1671 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1676 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1677 config_element_offset
)) {
1679 uint64_t offset
= 0;
1682 content
= xmlNodeGetContent(probe_attribute_node
);
1684 ret
= -LTTNG_ERR_NOMEM
;
1688 ret
= parse_uint(content
, &offset
);
1691 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1695 attr
->offset
= offset
;
1696 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1697 config_element_symbol_name
)) {
1701 content
= xmlNodeGetContent(probe_attribute_node
);
1703 ret
= -LTTNG_ERR_NOMEM
;
1707 ret
= lttng_strncpy(attr
->symbol_name
,
1708 (const char *) content
,
1709 LTTNG_SYMBOL_NAME_LEN
);
1711 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1712 (const char *) content
,
1713 strlen((const char *) content
),
1714 LTTNG_SYMBOL_NAME_LEN
);
1715 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1727 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1728 const char *channel_name
, const enum process_event_node_phase phase
)
1732 struct lttng_event
*event
;
1733 char **exclusions
= NULL
;
1734 unsigned long exclusion_count
= 0;
1735 char *filter_expression
= NULL
;
1739 assert(channel_name
);
1741 event
= lttng_event_create();
1743 ret
= -LTTNG_ERR_NOMEM
;
1747 /* Initialize default log level which varies by domain */
1748 switch (handle
->domain
.type
)
1750 case LTTNG_DOMAIN_JUL
:
1751 event
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1753 case LTTNG_DOMAIN_LOG4J
:
1754 event
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1756 case LTTNG_DOMAIN_PYTHON
:
1757 event
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1759 case LTTNG_DOMAIN_UST
:
1760 case LTTNG_DOMAIN_KERNEL
:
1761 event
->loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1767 for (node
= xmlFirstElementChild(event_node
); node
;
1768 node
= xmlNextElementSibling(node
)) {
1769 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1773 content
= xmlNodeGetContent(node
);
1775 ret
= -LTTNG_ERR_NOMEM
;
1779 ret
= lttng_strncpy(event
->name
,
1780 (const char *) content
,
1781 LTTNG_SYMBOL_NAME_LEN
);
1783 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1784 (const char *) content
,
1785 strlen((const char *) content
),
1786 LTTNG_SYMBOL_NAME_LEN
);
1787 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1792 } else if (!strcmp((const char *) node
->name
,
1793 config_element_enabled
)) {
1794 xmlChar
*content
= xmlNodeGetContent(node
);
1798 ret
= -LTTNG_ERR_NOMEM
;
1802 ret
= parse_bool(content
, &event
->enabled
);
1805 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1808 } else if (!strcmp((const char *) node
->name
,
1809 config_element_type
)) {
1810 xmlChar
*content
= xmlNodeGetContent(node
);
1814 ret
= -LTTNG_ERR_NOMEM
;
1818 ret
= get_event_type(content
);
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1826 } else if (!strcmp((const char *) node
->name
,
1827 config_element_loglevel_type
)) {
1828 xmlChar
*content
= xmlNodeGetContent(node
);
1832 ret
= -LTTNG_ERR_NOMEM
;
1836 ret
= get_loglevel_type(content
);
1839 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1843 event
->loglevel_type
= ret
;
1844 } else if (!strcmp((const char *) node
->name
,
1845 config_element_loglevel
)) {
1847 int64_t loglevel
= 0;
1850 content
= xmlNodeGetContent(node
);
1852 ret
= -LTTNG_ERR_NOMEM
;
1856 ret
= parse_int(content
, &loglevel
);
1859 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1863 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1864 WARN("loglevel out of range.");
1865 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1869 event
->loglevel
= loglevel
;
1870 } else if (!strcmp((const char *) node
->name
,
1871 config_element_filter
)) {
1873 xmlNodeGetContent(node
);
1877 ret
= -LTTNG_ERR_NOMEM
;
1881 free(filter_expression
);
1882 filter_expression
= strdup((char *) content
);
1884 if (!filter_expression
) {
1885 ret
= -LTTNG_ERR_NOMEM
;
1888 } else if (!strcmp((const char *) node
->name
,
1889 config_element_exclusions
)) {
1890 xmlNodePtr exclusion_node
;
1891 int exclusion_index
= 0;
1896 * Exclusions has already been initialized,
1899 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1903 exclusion_count
= xmlChildElementCount(node
);
1904 if (!exclusion_count
) {
1908 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1910 exclusion_count
= 0;
1911 ret
= -LTTNG_ERR_NOMEM
;
1915 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1916 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1918 xmlNodeGetContent(exclusion_node
);
1921 ret
= -LTTNG_ERR_NOMEM
;
1925 exclusions
[exclusion_index
] = strdup((const char *) content
);
1927 if (!exclusions
[exclusion_index
]) {
1928 ret
= -LTTNG_ERR_NOMEM
;
1934 event
->exclusion
= 1;
1935 } else if (!strcmp((const char *) node
->name
,
1936 config_element_attributes
)) {
1937 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1940 if (!attribute_node
) {
1941 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1945 if (!strcmp((const char *) attribute_node
->name
,
1946 config_element_probe_attributes
)) {
1947 xmlNodePtr probe_attribute_node
;
1949 /* probe_attributes */
1950 for (probe_attribute_node
=
1951 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1952 probe_attribute_node
= xmlNextElementSibling(
1953 probe_attribute_node
)) {
1955 ret
= process_probe_attribute_node(probe_attribute_node
,
1956 &event
->attr
.probe
);
1961 } else if (!strcmp((const char *) attribute_node
->name
,
1962 config_element_function_attributes
)) {
1965 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1967 /* function_attributes */
1968 content
= xmlNodeGetContent(symbol_node
);
1970 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1974 sym_len
= strlen((char *) content
);
1975 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1976 WARN("Function name too long.");
1977 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1982 ret
= lttng_strncpy(
1983 event
->attr
.ftrace
.symbol_name
,
1984 (char *) content
, sym_len
);
1986 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1991 } else if (!strcmp((const char *) attribute_node
->name
,
1992 config_element_userspace_probe_tracepoint_attributes
)) {
1993 struct lttng_userspace_probe_location
*location
;
1995 location
= process_userspace_probe_tracepoint_attribute_node(attribute_node
);
1997 WARN("Error processing userspace probe tracepoint attribute");
1998 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2001 ret
= lttng_event_set_userspace_probe_location(
2004 WARN("Error setting userspace probe location field");
2005 lttng_userspace_probe_location_destroy(
2007 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2010 } else if (!strcmp((const char *) attribute_node
->name
,
2011 config_element_userspace_probe_function_attributes
)) {
2012 struct lttng_userspace_probe_location
*location
;
2015 process_userspace_probe_function_attribute_node(
2018 WARN("Error processing userspace probe function attribute");
2019 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2023 ret
= lttng_event_set_userspace_probe_location(
2026 WARN("Error setting userspace probe location field");
2027 lttng_userspace_probe_location_destroy(
2029 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2033 /* Unknown event attribute. */
2034 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2040 if ((event
->enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
2041 ret
= lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
2042 filter_expression
, exclusion_count
, exclusions
);
2044 WARN("Enabling event (name:%s) on load failed.", event
->name
);
2045 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2051 for (i
= 0; i
< exclusion_count
; i
++) {
2052 free(exclusions
[i
]);
2055 lttng_event_destroy(event
);
2057 free(filter_expression
);
2062 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
2063 const char *channel_name
)
2066 struct lttng_event event
;
2069 assert(events_node
);
2071 assert(channel_name
);
2073 for (node
= xmlFirstElementChild(events_node
); node
;
2074 node
= xmlNextElementSibling(node
)) {
2075 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
2082 * Disable all events to enable only the necessary events.
2083 * Limitations regarding lttng_disable_events and tuple descriptor
2084 * force this approach.
2086 memset(&event
, 0, sizeof(event
));
2087 event
.loglevel
= -1;
2088 event
.type
= LTTNG_EVENT_ALL
;
2089 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
2094 for (node
= xmlFirstElementChild(events_node
); node
;
2095 node
= xmlNextElementSibling(node
)) {
2096 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
2107 int process_channel_attr_node(xmlNodePtr attr_node
,
2108 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
2109 xmlNodePtr
*events_node
)
2115 assert(contexts_node
);
2116 assert(events_node
);
2118 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
2122 content
= xmlNodeGetContent(attr_node
);
2124 ret
= -LTTNG_ERR_NOMEM
;
2128 ret
= lttng_strncpy(channel
->name
,
2129 (const char *) content
,
2130 LTTNG_SYMBOL_NAME_LEN
);
2132 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2133 (const char *) content
,
2134 strlen((const char *) content
),
2135 LTTNG_SYMBOL_NAME_LEN
);
2136 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2141 } else if (!strcmp((const char *) attr_node
->name
,
2142 config_element_enabled
)) {
2147 content
= xmlNodeGetContent(attr_node
);
2149 ret
= -LTTNG_ERR_NOMEM
;
2153 ret
= parse_bool(content
, &enabled
);
2156 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2160 channel
->enabled
= enabled
;
2161 } else if (!strcmp((const char *) attr_node
->name
,
2162 config_element_overwrite_mode
)) {
2165 /* overwrite_mode */
2166 content
= xmlNodeGetContent(attr_node
);
2168 ret
= -LTTNG_ERR_NOMEM
;
2172 ret
= get_overwrite_mode(content
);
2175 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2179 channel
->attr
.overwrite
= ret
;
2180 } else if (!strcmp((const char *) attr_node
->name
,
2181 config_element_subbuf_size
)) {
2184 /* subbuffer_size */
2185 content
= xmlNodeGetContent(attr_node
);
2187 ret
= -LTTNG_ERR_NOMEM
;
2191 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
2194 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2197 } else if (!strcmp((const char *) attr_node
->name
,
2198 config_element_num_subbuf
)) {
2201 /* subbuffer_count */
2202 content
= xmlNodeGetContent(attr_node
);
2204 ret
= -LTTNG_ERR_NOMEM
;
2208 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
2211 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2214 } else if (!strcmp((const char *) attr_node
->name
,
2215 config_element_switch_timer_interval
)) {
2217 uint64_t switch_timer_interval
= 0;
2219 /* switch_timer_interval */
2220 content
= xmlNodeGetContent(attr_node
);
2222 ret
= -LTTNG_ERR_NOMEM
;
2226 ret
= parse_uint(content
, &switch_timer_interval
);
2229 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2233 if (switch_timer_interval
> UINT_MAX
) {
2234 WARN("switch_timer_interval out of range.");
2235 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2239 channel
->attr
.switch_timer_interval
=
2240 switch_timer_interval
;
2241 } else if (!strcmp((const char *) attr_node
->name
,
2242 config_element_read_timer_interval
)) {
2244 uint64_t read_timer_interval
= 0;
2246 /* read_timer_interval */
2247 content
= xmlNodeGetContent(attr_node
);
2249 ret
= -LTTNG_ERR_NOMEM
;
2253 ret
= parse_uint(content
, &read_timer_interval
);
2256 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2260 if (read_timer_interval
> UINT_MAX
) {
2261 WARN("read_timer_interval out of range.");
2262 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2266 channel
->attr
.read_timer_interval
=
2267 read_timer_interval
;
2268 } else if (!strcmp((const char *) attr_node
->name
,
2269 config_element_output_type
)) {
2273 content
= xmlNodeGetContent(attr_node
);
2275 ret
= -LTTNG_ERR_NOMEM
;
2279 ret
= get_output_type(content
);
2282 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2286 channel
->attr
.output
= ret
;
2287 } else if (!strcmp((const char *) attr_node
->name
,
2288 config_element_tracefile_size
)) {
2291 /* tracefile_size */
2292 content
= xmlNodeGetContent(attr_node
);
2294 ret
= -LTTNG_ERR_NOMEM
;
2298 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2301 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2304 } else if (!strcmp((const char *) attr_node
->name
,
2305 config_element_tracefile_count
)) {
2308 /* tracefile_count */
2309 content
= xmlNodeGetContent(attr_node
);
2311 ret
= -LTTNG_ERR_NOMEM
;
2315 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2318 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2321 } else if (!strcmp((const char *) attr_node
->name
,
2322 config_element_live_timer_interval
)) {
2324 uint64_t live_timer_interval
= 0;
2326 /* live_timer_interval */
2327 content
= xmlNodeGetContent(attr_node
);
2329 ret
= -LTTNG_ERR_NOMEM
;
2333 ret
= parse_uint(content
, &live_timer_interval
);
2336 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2340 if (live_timer_interval
> UINT_MAX
) {
2341 WARN("live_timer_interval out of range.");
2342 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2346 channel
->attr
.live_timer_interval
=
2347 live_timer_interval
;
2348 } else if (!strcmp((const char *) attr_node
->name
,
2349 config_element_monitor_timer_interval
)) {
2351 uint64_t monitor_timer_interval
= 0;
2353 /* monitor_timer_interval */
2354 content
= xmlNodeGetContent(attr_node
);
2356 ret
= -LTTNG_ERR_NOMEM
;
2360 ret
= parse_uint(content
, &monitor_timer_interval
);
2363 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2367 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2368 monitor_timer_interval
);
2370 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2373 } else if (!strcmp((const char *) attr_node
->name
,
2374 config_element_blocking_timeout
)) {
2376 int64_t blocking_timeout
= 0;
2378 /* blocking_timeout */
2379 content
= xmlNodeGetContent(attr_node
);
2381 ret
= -LTTNG_ERR_NOMEM
;
2385 ret
= parse_int(content
, &blocking_timeout
);
2388 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2392 ret
= lttng_channel_set_blocking_timeout(channel
,
2395 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2398 } else if (!strcmp((const char *) attr_node
->name
,
2399 config_element_events
)) {
2401 *events_node
= attr_node
;
2404 *contexts_node
= attr_node
;
2412 int process_context_node(xmlNodePtr context_node
,
2413 struct lttng_handle
*handle
, const char *channel_name
)
2416 struct lttng_event_context context
;
2417 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2420 assert(channel_name
);
2422 if (!context_child_node
) {
2423 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2427 memset(&context
, 0, sizeof(context
));
2429 if (!strcmp((const char *) context_child_node
->name
,
2430 config_element_type
)) {
2432 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2435 ret
= -LTTNG_ERR_NOMEM
;
2439 ret
= get_context_type(content
);
2442 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2447 } else if (!strcmp((const char *) context_child_node
->name
,
2448 config_element_context_perf
)) {
2450 xmlNodePtr perf_attr_node
;
2452 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2453 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2454 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2455 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2456 perf_attr_node
; perf_attr_node
=
2457 xmlNextElementSibling(perf_attr_node
)) {
2458 if (!strcmp((const char *) perf_attr_node
->name
,
2459 config_element_type
)) {
2464 content
= xmlNodeGetContent(perf_attr_node
);
2466 ret
= -LTTNG_ERR_NOMEM
;
2470 ret
= parse_uint(content
, &type
);
2473 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2477 if (type
> UINT32_MAX
) {
2478 WARN("perf context type out of range.");
2479 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2483 context
.u
.perf_counter
.type
= type
;
2484 } else if (!strcmp((const char *) perf_attr_node
->name
,
2485 config_element_config
)) {
2487 uint64_t config
= 0;
2490 content
= xmlNodeGetContent(perf_attr_node
);
2492 ret
= -LTTNG_ERR_NOMEM
;
2496 ret
= parse_uint(content
, &config
);
2499 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2503 context
.u
.perf_counter
.config
= config
;
2504 } else if (!strcmp((const char *) perf_attr_node
->name
,
2505 config_element_name
)) {
2509 content
= xmlNodeGetContent(perf_attr_node
);
2511 ret
= -LTTNG_ERR_NOMEM
;
2515 ret
= lttng_strncpy(context
.u
.perf_counter
.name
,
2516 (const char *) content
,
2517 LTTNG_SYMBOL_NAME_LEN
);
2519 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2520 (const char *) content
,
2521 strlen((const char *) content
),
2522 LTTNG_SYMBOL_NAME_LEN
);
2523 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2530 } else if (!strcmp((const char *) context_child_node
->name
,
2531 config_element_context_app
)) {
2532 /* application context */
2533 xmlNodePtr app_ctx_node
;
2535 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2536 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2537 app_ctx_node
; app_ctx_node
=
2538 xmlNextElementSibling(app_ctx_node
)) {
2540 char **target
= strcmp(
2541 (const char *) app_ctx_node
->name
,
2542 config_element_context_app_provider_name
) == 0 ?
2543 &context
.u
.app_ctx
.provider_name
:
2544 &context
.u
.app_ctx
.ctx_name
;
2546 content
= xmlNodeGetContent(app_ctx_node
);
2548 ret
= -LTTNG_ERR_NOMEM
;
2552 *target
= (char *) content
;
2555 /* Unrecognized context type */
2556 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2560 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2561 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2562 free(context
.u
.app_ctx
.provider_name
);
2563 free(context
.u
.app_ctx
.ctx_name
);
2570 int process_contexts_node(xmlNodePtr contexts_node
,
2571 struct lttng_handle
*handle
, const char *channel_name
)
2574 xmlNodePtr context_node
;
2576 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2577 context_node
= xmlNextElementSibling(context_node
)) {
2578 ret
= process_context_node(context_node
, handle
, channel_name
);
2588 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2589 struct lttng_handle
*handle
)
2592 xmlNodePtr targets_node
= NULL
;
2596 assert(pid_tracker_node
);
2597 /* get the targets node */
2598 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2599 node
= xmlNextElementSibling(node
)) {
2600 if (!strcmp((const char *) node
->name
,
2601 config_element_targets
)) {
2602 targets_node
= node
;
2607 if (!targets_node
) {
2608 ret
= LTTNG_ERR_INVALID
;
2612 /* Go through all pid_target node */
2613 child
= xmlChildElementCount(targets_node
);
2615 /* The session is explicitly set to target nothing. */
2616 ret
= lttng_untrack_pid(handle
, -1);
2621 for (node
= xmlFirstElementChild(targets_node
); node
;
2622 node
= xmlNextElementSibling(node
)) {
2623 xmlNodePtr pid_target_node
= node
;
2625 /* get pid node and track it */
2626 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2627 node
= xmlNextElementSibling(node
)) {
2628 if (!strcmp((const char *) node
->name
,
2629 config_element_pid
)) {
2631 xmlChar
*content
= NULL
;
2633 content
= xmlNodeGetContent(node
);
2635 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2639 ret
= parse_int(content
, &pid
);
2642 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2646 ret
= lttng_track_pid(handle
, (int) pid
);
2652 node
= pid_target_node
;
2660 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2663 struct lttng_domain domain
= { 0 };
2664 struct lttng_handle
*handle
= NULL
;
2665 struct lttng_channel
*channel
= NULL
;
2666 xmlNodePtr channels_node
= NULL
;
2667 xmlNodePtr trackers_node
= NULL
;
2668 xmlNodePtr pid_tracker_node
= NULL
;
2671 assert(session_name
);
2673 ret
= init_domain(domain_node
, &domain
);
2678 handle
= lttng_create_handle(session_name
, &domain
);
2680 ret
= -LTTNG_ERR_NOMEM
;
2684 /* get the channels node */
2685 for (node
= xmlFirstElementChild(domain_node
); node
;
2686 node
= xmlNextElementSibling(node
)) {
2687 if (!strcmp((const char *) node
->name
,
2688 config_element_channels
)) {
2689 channels_node
= node
;
2694 if (!channels_node
) {
2698 /* create all channels */
2699 for (node
= xmlFirstElementChild(channels_node
); node
;
2700 node
= xmlNextElementSibling(node
)) {
2701 const enum lttng_domain_type original_domain
= domain
.type
;
2702 xmlNodePtr contexts_node
= NULL
;
2703 xmlNodePtr events_node
= NULL
;
2704 xmlNodePtr channel_attr_node
;
2707 * Channels of the "agent" types cannot be created directly.
2708 * They are meant to be created implicitly through the
2709 * activation of events in their domain. However, a user
2710 * can override the default channel configuration attributes
2711 * by creating the underlying UST channel _before_ enabling
2712 * an agent domain event.
2714 * Hence, the channel's type is substituted before the creation
2715 * and restored by the time the events are created.
2717 switch (domain
.type
) {
2718 case LTTNG_DOMAIN_JUL
:
2719 case LTTNG_DOMAIN_LOG4J
:
2720 case LTTNG_DOMAIN_PYTHON
:
2721 domain
.type
= LTTNG_DOMAIN_UST
;
2726 channel
= lttng_channel_create(&domain
);
2732 for (channel_attr_node
= xmlFirstElementChild(node
);
2733 channel_attr_node
; channel_attr_node
=
2734 xmlNextElementSibling(channel_attr_node
)) {
2735 ret
= process_channel_attr_node(channel_attr_node
,
2736 channel
, &contexts_node
, &events_node
);
2742 ret
= lttng_enable_channel(handle
, channel
);
2747 /* Restore the original channel domain. */
2748 domain
.type
= original_domain
;
2750 ret
= process_events_node(events_node
, handle
, channel
->name
);
2755 ret
= process_contexts_node(contexts_node
, handle
,
2761 lttng_channel_destroy(channel
);
2765 /* get the trackers node */
2766 for (node
= xmlFirstElementChild(domain_node
); node
;
2767 node
= xmlNextElementSibling(node
)) {
2768 if (!strcmp((const char *) node
->name
,
2769 config_element_trackers
)) {
2770 trackers_node
= node
;
2775 if (!trackers_node
) {
2779 for (node
= xmlFirstElementChild(trackers_node
); node
;
2780 node
= xmlNextElementSibling(node
)) {
2781 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2782 pid_tracker_node
= node
;
2783 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2790 if (!pid_tracker_node
) {
2791 lttng_track_pid(handle
, -1);
2795 lttng_channel_destroy(channel
);
2796 lttng_destroy_handle(handle
);
2801 int add_periodic_rotation(const char *name
, uint64_t time_us
)
2804 enum lttng_rotation_status status
;
2805 struct lttng_rotation_schedule
*periodic
=
2806 lttng_rotation_schedule_periodic_create();
2809 ret
= -LTTNG_ERR_NOMEM
;
2813 status
= lttng_rotation_schedule_periodic_set_period(periodic
,
2815 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2816 ret
= -LTTNG_ERR_INVALID
;
2820 status
= lttng_session_add_rotation_schedule(name
, periodic
);
2822 case LTTNG_ROTATION_STATUS_OK
:
2825 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2826 case LTTNG_ROTATION_STATUS_INVALID
:
2827 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2830 ret
= -LTTNG_ERR_UNK
;
2834 lttng_rotation_schedule_destroy(periodic
);
2839 int add_size_rotation(const char *name
, uint64_t size_bytes
)
2842 enum lttng_rotation_status status
;
2843 struct lttng_rotation_schedule
*size
=
2844 lttng_rotation_schedule_size_threshold_create();
2847 ret
= -LTTNG_ERR_NOMEM
;
2851 status
= lttng_rotation_schedule_size_threshold_set_threshold(size
,
2853 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2854 ret
= -LTTNG_ERR_INVALID
;
2858 status
= lttng_session_add_rotation_schedule(name
, size
);
2860 case LTTNG_ROTATION_STATUS_OK
:
2863 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2864 case LTTNG_ROTATION_STATUS_INVALID
:
2865 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2868 ret
= -LTTNG_ERR_UNK
;
2872 lttng_rotation_schedule_destroy(size
);
2877 int process_session_rotation_schedules_node(
2878 xmlNodePtr schedules_node
,
2879 uint64_t *rotation_timer_interval
,
2880 uint64_t *rotation_size
)
2885 for (child
= xmlFirstElementChild(schedules_node
);
2887 child
= xmlNextElementSibling(child
)) {
2888 if (!strcmp((const char *) child
->name
,
2889 config_element_rotation_schedule_periodic
)) {
2891 xmlNodePtr time_us_node
;
2893 /* periodic rotation schedule */
2894 time_us_node
= xmlFirstElementChild(child
);
2895 if (!time_us_node
||
2896 strcmp((const char *) time_us_node
->name
,
2897 config_element_rotation_schedule_periodic_time_us
)) {
2898 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2903 content
= xmlNodeGetContent(time_us_node
);
2905 ret
= -LTTNG_ERR_NOMEM
;
2908 ret
= parse_uint(content
, rotation_timer_interval
);
2911 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2914 } else if (!strcmp((const char *) child
->name
,
2915 config_element_rotation_schedule_size_threshold
)) {
2917 xmlNodePtr bytes_node
;
2919 /* size_threshold rotation schedule */
2920 bytes_node
= xmlFirstElementChild(child
);
2922 strcmp((const char *) bytes_node
->name
,
2923 config_element_rotation_schedule_size_threshold_bytes
)) {
2924 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2929 content
= xmlNodeGetContent(bytes_node
);
2931 ret
= -LTTNG_ERR_NOMEM
;
2934 ret
= parse_uint(content
, rotation_size
);
2937 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2948 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2950 const struct config_load_session_override_attr
*overrides
)
2952 int ret
, started
= -1, snapshot_mode
= -1;
2953 uint64_t live_timer_interval
= UINT64_MAX
,
2954 rotation_timer_interval
= 0,
2956 xmlChar
*name
= NULL
;
2957 xmlChar
*shm_path
= NULL
;
2958 xmlNodePtr domains_node
= NULL
;
2959 xmlNodePtr output_node
= NULL
;
2961 xmlNodePtr attributes_child
;
2962 struct lttng_domain
*kernel_domain
= NULL
;
2963 struct lttng_domain
*ust_domain
= NULL
;
2964 struct lttng_domain
*jul_domain
= NULL
;
2965 struct lttng_domain
*log4j_domain
= NULL
;
2966 struct lttng_domain
*python_domain
= NULL
;
2968 for (node
= xmlFirstElementChild(session_node
); node
;
2969 node
= xmlNextElementSibling(node
)) {
2970 if (!name
&& !strcmp((const char *) node
->name
,
2971 config_element_name
)) {
2973 xmlChar
*node_content
= xmlNodeGetContent(node
);
2974 if (!node_content
) {
2975 ret
= -LTTNG_ERR_NOMEM
;
2979 name
= node_content
;
2980 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2981 config_element_domains
)) {
2983 domains_node
= node
;
2984 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2985 config_element_started
)) {
2987 xmlChar
*node_content
= xmlNodeGetContent(node
);
2988 if (!node_content
) {
2989 ret
= -LTTNG_ERR_NOMEM
;
2993 ret
= parse_bool(node_content
, &started
);
2996 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2999 } else if (!output_node
&& !strcmp((const char *) node
->name
,
3000 config_element_output
)) {
3003 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
3004 config_element_shared_memory_path
)) {
3005 /* shared memory path */
3006 xmlChar
*node_content
= xmlNodeGetContent(node
);
3007 if (!node_content
) {
3008 ret
= -LTTNG_ERR_NOMEM
;
3012 shm_path
= node_content
;
3015 * attributes, snapshot_mode, live_timer_interval, rotation_size,
3016 * rotation_timer_interval.
3018 for (attributes_child
= xmlFirstElementChild(node
); attributes_child
;
3019 attributes_child
= xmlNextElementSibling(attributes_child
)) {
3020 if (!strcmp((const char *) attributes_child
->name
,
3021 config_element_snapshot_mode
)) {
3023 xmlChar
*snapshot_mode_content
=
3024 xmlNodeGetContent(attributes_child
);
3025 if (!snapshot_mode_content
) {
3026 ret
= -LTTNG_ERR_NOMEM
;
3030 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
3031 free(snapshot_mode_content
);
3033 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3036 } else if (!strcmp((const char *) attributes_child
->name
,
3037 config_element_live_timer_interval
)) {
3038 /* live_timer_interval */
3039 xmlChar
*timer_interval_content
=
3040 xmlNodeGetContent(attributes_child
);
3041 if (!timer_interval_content
) {
3042 ret
= -LTTNG_ERR_NOMEM
;
3046 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
3047 free(timer_interval_content
);
3049 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3052 } else if (!strcmp((const char *) attributes_child
->name
,
3053 config_element_rotation_schedules
)) {
3054 ret
= process_session_rotation_schedules_node(
3056 &rotation_timer_interval
,
3059 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3069 /* Mandatory attribute, as defined in the session XSD */
3070 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3074 if (session_name
&& strcmp((char *) name
, session_name
)) {
3075 /* This is not the session we are looking for */
3076 ret
= -LTTNG_ERR_NO_SESSION
;
3080 /* Init domains to create the session handles */
3081 for (node
= xmlFirstElementChild(domains_node
); node
;
3082 node
= xmlNextElementSibling(node
)) {
3083 struct lttng_domain
*domain
;
3085 domain
= zmalloc(sizeof(*domain
));
3087 ret
= -LTTNG_ERR_NOMEM
;
3091 ret
= init_domain(node
, domain
);
3093 goto domain_init_error
;
3096 switch (domain
->type
) {
3097 case LTTNG_DOMAIN_KERNEL
:
3098 if (kernel_domain
) {
3099 /* Same domain seen twice, invalid! */
3100 goto domain_init_error
;
3102 kernel_domain
= domain
;
3104 case LTTNG_DOMAIN_UST
:
3106 /* Same domain seen twice, invalid! */
3107 goto domain_init_error
;
3109 ust_domain
= domain
;
3111 case LTTNG_DOMAIN_JUL
:
3113 /* Same domain seen twice, invalid! */
3114 goto domain_init_error
;
3116 jul_domain
= domain
;
3118 case LTTNG_DOMAIN_LOG4J
:
3120 /* Same domain seen twice, invalid! */
3121 goto domain_init_error
;
3123 log4j_domain
= domain
;
3125 case LTTNG_DOMAIN_PYTHON
:
3126 if (python_domain
) {
3127 /* Same domain seen twice, invalid! */
3128 goto domain_init_error
;
3130 python_domain
= domain
;
3133 WARN("Invalid domain type");
3134 goto domain_init_error
;
3139 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3143 /* Apply overrides */
3145 if (overrides
->session_name
) {
3146 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
3147 if (!name_override
) {
3148 ret
= -LTTNG_ERR_NOMEM
;
3152 /* Overrides the session name to the provided name */
3154 name
= name_override
;
3159 /* Destroy session if it exists */
3160 ret
= lttng_destroy_session((const char *) name
);
3161 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
3162 ERR("Failed to destroy existing session.");
3167 /* Create session type depending on output type */
3168 if (snapshot_mode
&& snapshot_mode
!= -1) {
3169 ret
= create_snapshot_session((const char *) name
, output_node
,
3171 } else if (live_timer_interval
&&
3172 live_timer_interval
!= UINT64_MAX
) {
3173 ret
= create_session((const char *) name
,
3174 output_node
, live_timer_interval
, overrides
);
3176 /* regular session */
3177 ret
= create_session((const char *) name
,
3178 output_node
, UINT64_MAX
, overrides
);
3185 ret
= lttng_set_session_shm_path((const char *) name
,
3186 (const char *) shm_path
);
3192 for (node
= xmlFirstElementChild(domains_node
); node
;
3193 node
= xmlNextElementSibling(node
)) {
3194 ret
= process_domain_node(node
, (const char *) name
);
3200 if (rotation_timer_interval
) {
3201 ret
= add_periodic_rotation((const char *) name
,
3202 rotation_timer_interval
);
3207 if (rotation_size
) {
3208 ret
= add_size_rotation((const char *) name
,
3216 ret
= lttng_start_tracing((const char *) name
);
3224 ERR("Failed to load session %s: %s", (const char *) name
,
3225 lttng_strerror(ret
));
3226 lttng_destroy_session((const char *) name
);
3230 free(kernel_domain
);
3234 free(python_domain
);
3241 * Return 1 if the given path is readable by the current UID or 0 if not.
3242 * Return -1 if the path is EPERM.
3244 static int validate_file_read_creds(const char *path
)
3250 /* Can we read the file. */
3251 ret
= access(path
, R_OK
);
3255 if (errno
== EACCES
) {
3266 int load_session_from_file(const char *path
, const char *session_name
,
3267 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3268 const struct config_load_session_override_attr
*overrides
)
3270 int ret
, session_found
= !session_name
;
3271 xmlDocPtr doc
= NULL
;
3272 xmlNodePtr sessions_node
;
3273 xmlNodePtr session_node
;
3276 assert(validation_ctx
);
3278 ret
= validate_file_read_creds(path
);
3281 ret
= -LTTNG_ERR_EPERM
;
3283 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3288 doc
= xmlParseFile(path
);
3290 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3294 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
3296 ERR("Session configuration file validation failed");
3297 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3301 sessions_node
= xmlDocGetRootElement(doc
);
3302 if (!sessions_node
) {
3306 for (session_node
= xmlFirstElementChild(sessions_node
);
3307 session_node
; session_node
=
3308 xmlNextElementSibling(session_node
)) {
3309 ret
= process_session_node(session_node
,
3310 session_name
, overwrite
, overrides
);
3311 if (session_name
&& ret
== 0) {
3312 /* Target session found and loaded */
3320 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
3326 int load_session_from_path(const char *path
, const char *session_name
,
3327 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3328 const struct config_load_session_override_attr
*overrides
)
3330 int ret
, session_found
= !session_name
;
3331 DIR *directory
= NULL
;
3332 struct lttng_dynamic_buffer file_path
;
3336 assert(validation_ctx
);
3337 path_len
= strlen(path
);
3338 lttng_dynamic_buffer_init(&file_path
);
3339 if (path_len
>= LTTNG_PATH_MAX
) {
3340 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
3341 path
, path_len
, LTTNG_PATH_MAX
);
3342 ret
= -LTTNG_ERR_INVALID
;
3346 directory
= opendir(path
);
3350 /* Try the file loading. */
3353 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3356 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3361 size_t file_path_root_len
;
3363 ret
= lttng_dynamic_buffer_set_capacity(&file_path
,
3366 ret
= -LTTNG_ERR_NOMEM
;
3370 ret
= lttng_dynamic_buffer_append(&file_path
, path
, path_len
);
3372 ret
= -LTTNG_ERR_NOMEM
;
3376 if (file_path
.data
[file_path
.size
- 1] != '/') {
3377 ret
= lttng_dynamic_buffer_append(&file_path
, "/", 1);
3379 ret
= -LTTNG_ERR_NOMEM
;
3383 file_path_root_len
= file_path
.size
;
3385 /* Search for *.lttng files */
3387 size_t file_name_len
;
3388 struct dirent
*result
;
3391 * When the end of the directory stream is reached, NULL
3392 * is returned and errno is kept unchanged. When an
3393 * error occurs, NULL is returned and errno is set
3394 * accordingly. To distinguish between the two, set
3395 * errno to zero before calling readdir().
3397 * On success, readdir() returns a pointer to a dirent
3398 * structure. This structure may be statically
3399 * allocated, do not attempt to free(3) it.
3402 result
= readdir(directory
);
3404 /* Reached end of dir stream or error out. */
3407 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path
);
3408 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3414 file_name_len
= strlen(result
->d_name
);
3416 if (file_name_len
<=
3417 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3421 if (file_path
.size
+ file_name_len
>= LTTNG_PATH_MAX
) {
3422 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3424 /* +1 to account for NULL terminator. */
3425 file_path
.size
+ file_name_len
+ 1,
3430 /* Does the file end with .lttng? */
3431 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3432 result
->d_name
+ file_name_len
- sizeof(
3433 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3437 ret
= lttng_dynamic_buffer_append(&file_path
, result
->d_name
,
3440 ret
= -LTTNG_ERR_NOMEM
;
3444 ret
= load_session_from_file(file_path
.data
, session_name
,
3445 validation_ctx
, overwrite
, overrides
);
3446 if (session_name
&& !ret
) {
3451 * Reset the buffer's size to the location of the
3452 * path's trailing '/'.
3454 ret
= lttng_dynamic_buffer_set_size(&file_path
,
3455 file_path_root_len
);
3457 ret
= -LTTNG_ERR_UNK
;
3463 ret
= load_session_from_file(path
, session_name
,
3464 validation_ctx
, overwrite
, overrides
);
3474 if (closedir(directory
)) {
3478 if (session_found
&& !ret
) {
3481 lttng_dynamic_buffer_reset(&file_path
);
3486 * Validate that the given path's credentials and the current process have the
3487 * same UID. If so, return 1 else return 0 if it does NOT match.
3489 static int validate_path_creds(const char *path
)
3491 int ret
, uid
= getuid();
3500 ret
= stat(path
, &buf
);
3502 if (errno
!= ENOENT
) {
3508 if (buf
.st_uid
!= uid
) {
3519 int config_load_session(const char *path
, const char *session_name
,
3520 int overwrite
, unsigned int autoload
,
3521 const struct config_load_session_override_attr
*overrides
)
3524 bool session_loaded
= false;
3525 const char *path_ptr
= NULL
;
3526 struct session_config_validation_ctx validation_ctx
= { 0 };
3528 ret
= init_session_config_validation_ctx(&validation_ctx
);
3535 const char *sys_path
;
3538 home_path
= utils_get_home_dir();
3540 char path
[PATH_MAX
];
3543 * Try user session configuration path. Ignore error here so we can
3544 * continue loading the system wide sessions.
3547 ret
= snprintf(path
, sizeof(path
),
3548 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3549 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3551 PERROR("snprintf session autoload home config path");
3556 * Credentials are only validated for the autoload in order to
3557 * avoid any user session daemon to try to load kernel sessions
3558 * automatically and failing all the times.
3560 ret
= validate_path_creds(path
);
3565 ret
= snprintf(path
, sizeof(path
),
3566 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3568 PERROR("snprintf session home config path");
3574 ret
= load_session_from_path(path_ptr
, session_name
,
3575 &validation_ctx
, overwrite
, overrides
);
3576 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3580 * Continue even if the session was found since we have to try
3581 * the system wide sessions.
3583 session_loaded
= true;
3587 /* Reset path pointer for the system wide dir. */
3590 /* Try system wide configuration directory. */
3592 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3593 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3594 ret
= validate_path_creds(sys_path
);
3596 path_ptr
= sys_path
;
3599 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3600 path_ptr
= sys_path
;
3604 ret
= load_session_from_path(path_ptr
, session_name
,
3605 &validation_ctx
, overwrite
, overrides
);
3607 session_loaded
= true;
3611 ret
= access(path
, F_OK
);
3616 ret
= -LTTNG_ERR_INVALID
;
3617 WARN("Session configuration path does not exist.");
3620 ret
= -LTTNG_ERR_EPERM
;
3623 ret
= -LTTNG_ERR_UNK
;
3629 ret
= load_session_from_path(path
, session_name
,
3630 &validation_ctx
, overwrite
, overrides
);
3633 fini_session_config_validation_ctx(&validation_ctx
);
3634 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3636 * Don't report an error if no sessions are found when called
3637 * without a session_name or a search path.
3642 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3643 /* A matching session was found in one of the search paths. */
3650 void __attribute__((destructor
)) session_config_exit(void)