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>
46 #include "session-config.h"
47 #include "config-internal.h"
49 struct handler_filter_args
{
51 config_entry_handler_cb handler
;
55 struct session_config_validation_ctx
{
56 xmlSchemaParserCtxtPtr parser_ctx
;
58 xmlSchemaValidCtxtPtr schema_validation_ctx
;
61 const char * const config_str_yes
= "yes";
62 const char * const config_str_true
= "true";
63 const char * const config_str_on
= "on";
64 const char * const config_str_no
= "no";
65 const char * const config_str_false
= "false";
66 const char * const config_str_off
= "off";
67 const char * const config_xml_encoding
= "UTF-8";
68 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
69 const char * const config_xml_indent_string
= "\t";
70 const char * const config_xml_true
= "true";
71 const char * const config_xml_false
= "false";
73 const char * const config_element_channel
= "channel";
74 const char * const config_element_channels
= "channels";
75 const char * const config_element_domain
= "domain";
76 const char * const config_element_domains
= "domains";
77 const char * const config_element_event
= "event";
78 const char * const config_element_events
= "events";
79 const char * const config_element_context
= "context";
80 const char * const config_element_contexts
= "contexts";
81 const char * const config_element_attributes
= "attributes";
82 const char * const config_element_exclusion
= "exclusion";
83 const char * const config_element_exclusions
= "exclusions";
84 const char * const config_element_function_attributes
= "function_attributes";
85 const char * const config_element_probe_attributes
= "probe_attributes";
86 const char * const config_element_symbol_name
= "symbol_name";
87 const char * const config_element_address
= "address";
88 const char * const config_element_offset
= "offset";
89 const char * const config_element_name
= "name";
90 const char * const config_element_enabled
= "enabled";
91 const char * const config_element_overwrite_mode
= "overwrite_mode";
92 const char * const config_element_subbuf_size
= "subbuffer_size";
93 const char * const config_element_num_subbuf
= "subbuffer_count";
94 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
95 const char * const config_element_read_timer_interval
= "read_timer_interval";
96 LTTNG_HIDDEN
const char * const config_element_monitor_timer_interval
= "monitor_timer_interval";
97 LTTNG_HIDDEN
const char * const config_element_blocking_timeout
= "blocking_timeout";
98 const char * const config_element_output
= "output";
99 const char * const config_element_output_type
= "output_type";
100 const char * const config_element_tracefile_size
= "tracefile_size";
101 const char * const config_element_tracefile_count
= "tracefile_count";
102 const char * const config_element_live_timer_interval
= "live_timer_interval";
103 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
104 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
105 const char * const config_element_type
= "type";
106 const char * const config_element_buffer_type
= "buffer_type";
107 const char * const config_element_session
= "session";
108 const char * const config_element_sessions
= "sessions";
109 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
110 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
111 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
112 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
113 const char * const config_element_config
= "config";
114 const char * const config_element_started
= "started";
115 const char * const config_element_snapshot_mode
= "snapshot_mode";
116 const char * const config_element_loglevel
= "loglevel";
117 const char * const config_element_loglevel_type
= "loglevel_type";
118 const char * const config_element_filter
= "filter";
119 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
120 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
121 const char * const config_element_consumer_output
= "consumer_output";
122 const char * const config_element_destination
= "destination";
123 const char * const config_element_path
= "path";
124 const char * const config_element_net_output
= "net_output";
125 const char * const config_element_control_uri
= "control_uri";
126 const char * const config_element_data_uri
= "data_uri";
127 const char * const config_element_max_size
= "max_size";
128 const char * const config_element_pid
= "pid";
129 const char * const config_element_pids
= "pids";
130 const char * const config_element_shared_memory_path
= "shared_memory_path";
131 const char * const config_element_pid_tracker
= "pid_tracker";
132 const char * const config_element_trackers
= "trackers";
133 const char * const config_element_targets
= "targets";
134 const char * const config_element_target_pid
= "pid_target";
136 LTTNG_HIDDEN
const char * const config_element_rotation_timer_interval
= "rotation_schedule_timer_period";
137 LTTNG_HIDDEN
const char * const config_element_rotation_size
= "rotation_schedule_size";
138 LTTNG_HIDDEN
const char * const config_element_rotation_schedule
= "rotation_schedule";
140 const char * const config_domain_type_kernel
= "KERNEL";
141 const char * const config_domain_type_ust
= "UST";
142 const char * const config_domain_type_jul
= "JUL";
143 const char * const config_domain_type_log4j
= "LOG4J";
144 const char * const config_domain_type_python
= "PYTHON";
146 const char * const config_buffer_type_per_pid
= "PER_PID";
147 const char * const config_buffer_type_per_uid
= "PER_UID";
148 const char * const config_buffer_type_global
= "GLOBAL";
150 const char * const config_overwrite_mode_discard
= "DISCARD";
151 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
153 const char * const config_output_type_splice
= "SPLICE";
154 const char * const config_output_type_mmap
= "MMAP";
156 const char * const config_loglevel_type_all
= "ALL";
157 const char * const config_loglevel_type_range
= "RANGE";
158 const char * const config_loglevel_type_single
= "SINGLE";
160 const char * const config_event_type_all
= "ALL";
161 const char * const config_event_type_tracepoint
= "TRACEPOINT";
162 const char * const config_event_type_probe
= "PROBE";
163 const char * const config_event_type_function
= "FUNCTION";
164 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
165 const char * const config_event_type_noop
= "NOOP";
166 const char * const config_event_type_syscall
= "SYSCALL";
167 const char * const config_event_type_kprobe
= "KPROBE";
168 const char * const config_event_type_kretprobe
= "KRETPROBE";
170 const char * const config_event_context_pid
= "PID";
171 const char * const config_event_context_procname
= "PROCNAME";
172 const char * const config_event_context_prio
= "PRIO";
173 const char * const config_event_context_nice
= "NICE";
174 const char * const config_event_context_vpid
= "VPID";
175 const char * const config_event_context_tid
= "TID";
176 const char * const config_event_context_vtid
= "VTID";
177 const char * const config_event_context_ppid
= "PPID";
178 const char * const config_event_context_vppid
= "VPPID";
179 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
180 const char * const config_event_context_hostname
= "HOSTNAME";
181 const char * const config_event_context_ip
= "IP";
182 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
183 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
184 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
185 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
186 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
187 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
188 LTTNG_HIDDEN
const char * const config_event_context_callstack_user
= "CALLSTACK_USER";
189 LTTNG_HIDDEN
const char * const config_event_context_callstack_kernel
= "CALLSTACK_KERNEL";
191 /* Deprecated symbols */
192 const char * const config_element_perf
;
194 enum process_event_node_phase
{
199 struct consumer_output
{
206 static int config_entry_handler_filter(struct handler_filter_args
*args
,
207 const char *section
, const char *name
, const char *value
)
210 struct config_entry entry
= { section
, name
, value
};
214 if (!section
|| !name
|| !value
) {
220 if (strcmp(args
->section
, section
)) {
225 ret
= args
->handler(&entry
, args
->user_data
);
231 int config_get_section_entries(const char *override_path
, const char *section
,
232 config_entry_handler_cb handler
, void *user_data
)
236 FILE *config_file
= NULL
;
237 struct handler_filter_args filter
= { section
, handler
, user_data
};
239 /* First, try system-wide conf. file. */
240 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
242 config_file
= fopen(path
, "r");
244 DBG("Loading daemon conf file at %s", path
);
246 * Return value is not very important here since error or not, we
247 * continue and try the next possible conf. file.
249 (void) ini_parse_file(config_file
,
250 (ini_entry_handler
) config_entry_handler_filter
,
255 /* Second is the user local configuration. */
256 path
= utils_get_home_dir();
258 char fullpath
[PATH_MAX
];
260 ret
= snprintf(fullpath
, sizeof(fullpath
),
261 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
263 PERROR("snprintf user conf. path");
267 config_file
= fopen(fullpath
, "r");
269 DBG("Loading daemon user conf file at %s", path
);
271 * Return value is not very important here since error or not, we
272 * continue and try the next possible conf. file.
274 (void) ini_parse_file(config_file
,
275 (ini_entry_handler
) config_entry_handler_filter
,
281 /* Final path is the one that the user might have provided. */
283 config_file
= fopen(override_path
, "r");
285 DBG("Loading daemon command line conf file at %s", override_path
);
286 (void) ini_parse_file(config_file
,
287 (ini_entry_handler
) config_entry_handler_filter
,
291 ERR("Failed to open daemon configuration file at %s",
298 /* Everything went well. */
306 int config_parse_value(const char *value
)
309 char *endptr
, *lower_str
;
319 v
= strtoul(value
, &endptr
, 10);
320 if (endptr
!= value
) {
325 lower_str
= zmalloc(len
+ 1);
332 for (i
= 0; i
< len
; i
++) {
333 lower_str
[i
] = tolower(value
[i
]);
336 if (!strcmp(lower_str
, config_str_yes
) ||
337 !strcmp(lower_str
, config_str_true
) ||
338 !strcmp(lower_str
, config_str_on
)) {
340 } else if (!strcmp(lower_str
, config_str_no
) ||
341 !strcmp(lower_str
, config_str_false
) ||
342 !strcmp(lower_str
, config_str_off
)) {
354 * Returns a xmlChar string which must be released using xmlFree().
356 static xmlChar
*encode_string(const char *in_str
)
358 xmlChar
*out_str
= NULL
;
359 xmlCharEncodingHandlerPtr handler
;
360 int out_len
, ret
, in_len
;
364 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
366 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
370 in_len
= strlen(in_str
);
372 * Add 1 byte for the NULL terminted character. The factor 4 here is
373 * used because UTF-8 characters can take up to 4 bytes.
375 out_len
= (in_len
* 4) + 1;
376 out_str
= xmlMalloc(out_len
);
381 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
388 /* out_len is now the size of out_str */
389 out_str
[out_len
] = '\0';
395 struct config_writer
*config_writer_create(int fd_output
, int indent
)
398 struct config_writer
*writer
;
399 xmlOutputBufferPtr buffer
;
401 writer
= zmalloc(sizeof(struct config_writer
));
403 PERROR("zmalloc config_writer_create");
407 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
412 writer
->writer
= xmlNewTextWriter(buffer
);
413 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
414 config_xml_encoding
, NULL
);
419 ret
= xmlTextWriterSetIndentString(writer
->writer
,
420 BAD_CAST config_xml_indent_string
);
425 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
433 config_writer_destroy(writer
);
438 int config_writer_destroy(struct config_writer
*writer
)
447 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
448 WARN("Could not close XML document");
452 if (writer
->writer
) {
453 xmlFreeTextWriter(writer
->writer
);
462 int config_writer_open_element(struct config_writer
*writer
,
463 const char *element_name
)
466 xmlChar
*encoded_element_name
;
468 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
473 encoded_element_name
= encode_string(element_name
);
474 if (!encoded_element_name
) {
479 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
480 xmlFree(encoded_element_name
);
482 return ret
>= 0 ? 0 : ret
;
486 int config_writer_write_attribute(struct config_writer
*writer
,
487 const char *name
, const char *value
)
490 xmlChar
*encoded_name
= NULL
;
491 xmlChar
*encoded_value
= NULL
;
493 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
498 encoded_name
= encode_string(name
);
504 encoded_value
= encode_string(value
);
505 if (!encoded_value
) {
510 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
513 xmlFree(encoded_name
);
514 xmlFree(encoded_value
);
515 return ret
>= 0 ? 0 : ret
;
519 int config_writer_close_element(struct config_writer
*writer
)
523 if (!writer
|| !writer
->writer
) {
528 ret
= xmlTextWriterEndElement(writer
->writer
);
530 return ret
>= 0 ? 0 : ret
;
534 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
535 const char *element_name
, uint64_t value
)
538 xmlChar
*encoded_element_name
;
540 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
545 encoded_element_name
= encode_string(element_name
);
546 if (!encoded_element_name
) {
551 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
552 encoded_element_name
, "%" PRIu64
, value
);
553 xmlFree(encoded_element_name
);
555 return ret
>= 0 ? 0 : ret
;
559 int config_writer_write_element_signed_int(struct config_writer
*writer
,
560 const char *element_name
, int64_t value
)
563 xmlChar
*encoded_element_name
;
565 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
570 encoded_element_name
= encode_string(element_name
);
571 if (!encoded_element_name
) {
576 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
577 encoded_element_name
, "%" PRIi64
, value
);
578 xmlFree(encoded_element_name
);
580 return ret
>= 0 ? 0 : ret
;
584 int config_writer_write_element_bool(struct config_writer
*writer
,
585 const char *element_name
, int value
)
587 return config_writer_write_element_string(writer
, element_name
,
588 value
? config_xml_true
: config_xml_false
);
592 int config_writer_write_element_string(struct config_writer
*writer
,
593 const char *element_name
, const char *value
)
596 xmlChar
*encoded_element_name
= NULL
;
597 xmlChar
*encoded_value
= NULL
;
599 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
605 encoded_element_name
= encode_string(element_name
);
606 if (!encoded_element_name
) {
611 encoded_value
= encode_string(value
);
612 if (!encoded_value
) {
617 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
620 xmlFree(encoded_element_name
);
621 xmlFree(encoded_value
);
622 return ret
>= 0 ? 0 : ret
;
626 void xml_error_handler(void *ctx
, const char *format
, ...)
632 va_start(args
, format
);
633 ret
= vasprintf(&errMsg
, format
, args
);
636 ERR("String allocation failed in xml error handler");
640 fprintf(stderr
, "XML Error: %s", errMsg
);
645 void fini_session_config_validation_ctx(
646 struct session_config_validation_ctx
*ctx
)
648 if (ctx
->parser_ctx
) {
649 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
653 xmlSchemaFree(ctx
->schema
);
656 if (ctx
->schema_validation_ctx
) {
657 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
660 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
664 char *get_session_config_xsd_path()
667 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
668 size_t base_path_len
;
672 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
675 base_path_len
= strlen(base_path
);
676 max_path_len
= base_path_len
+
677 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
678 xsd_path
= zmalloc(max_path_len
);
683 strcpy(xsd_path
, base_path
);
684 if (xsd_path
[base_path_len
- 1] != '/') {
685 xsd_path
[base_path_len
++] = '/';
688 strcpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
);
694 int init_session_config_validation_ctx(
695 struct session_config_validation_ctx
*ctx
)
698 char *xsd_path
= get_session_config_xsd_path();
701 ret
= -LTTNG_ERR_NOMEM
;
705 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
706 if (!ctx
->parser_ctx
) {
707 ERR("XSD parser context creation failed");
708 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
711 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
712 xml_error_handler
, NULL
);
714 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
716 ERR("XSD parsing failed");
717 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
721 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
722 if (!ctx
->schema_validation_ctx
) {
723 ERR("XSD validation context creation failed");
724 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
728 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
729 xml_error_handler
, NULL
);
734 fini_session_config_validation_ctx(ctx
);
742 int parse_uint(xmlChar
*str
, uint64_t *val
)
752 *val
= strtoull((const char *) str
, &endptr
, 10);
753 if (!endptr
|| *endptr
) {
764 int parse_int(xmlChar
*str
, int64_t *val
)
774 *val
= strtoll((const char *) str
, &endptr
, 10);
775 if (!endptr
|| *endptr
) {
786 int parse_bool(xmlChar
*str
, int *val
)
795 if (!strcmp((const char *) str
, config_xml_true
)) {
797 } else if (!strcmp((const char *) str
, config_xml_false
)) {
800 WARN("Invalid boolean value encoutered (%s).",
809 int get_domain_type(xmlChar
*domain
)
817 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
818 ret
= LTTNG_DOMAIN_KERNEL
;
819 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
820 ret
= LTTNG_DOMAIN_UST
;
821 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
822 ret
= LTTNG_DOMAIN_JUL
;
823 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
824 ret
= LTTNG_DOMAIN_LOG4J
;
825 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
826 ret
= LTTNG_DOMAIN_PYTHON
;
837 int get_buffer_type(xmlChar
*buffer_type
)
845 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
846 ret
= LTTNG_BUFFER_GLOBAL
;
847 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
848 ret
= LTTNG_BUFFER_PER_UID
;
849 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
850 ret
= LTTNG_BUFFER_PER_PID
;
861 int get_overwrite_mode(xmlChar
*overwrite_mode
)
865 if (!overwrite_mode
) {
869 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
871 } else if (!strcmp((char *) overwrite_mode
,
872 config_overwrite_mode_discard
)) {
884 int get_output_type(xmlChar
*output_type
)
892 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
893 ret
= LTTNG_EVENT_MMAP
;
894 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
895 ret
= LTTNG_EVENT_SPLICE
;
906 int get_event_type(xmlChar
*event_type
)
914 if (!strcmp((char *) event_type
, config_event_type_all
)) {
915 ret
= LTTNG_EVENT_ALL
;
916 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
917 ret
= LTTNG_EVENT_TRACEPOINT
;
918 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
919 ret
= LTTNG_EVENT_PROBE
;
920 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
921 ret
= LTTNG_EVENT_FUNCTION
;
922 } else if (!strcmp((char *) event_type
,
923 config_event_type_function_entry
)) {
924 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
925 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
926 ret
= LTTNG_EVENT_NOOP
;
927 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
928 ret
= LTTNG_EVENT_SYSCALL
;
939 int get_loglevel_type(xmlChar
*loglevel_type
)
943 if (!loglevel_type
) {
947 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
948 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
949 } else if (!strcmp((char *) loglevel_type
,
950 config_loglevel_type_range
)) {
951 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
952 } else if (!strcmp((char *) loglevel_type
,
953 config_loglevel_type_single
)) {
954 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
965 * Return the context type or -1 on error.
968 int get_context_type(xmlChar
*context_type
)
976 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
977 ret
= LTTNG_EVENT_CONTEXT_PID
;
978 } else if (!strcmp((char *) context_type
,
979 config_event_context_procname
)) {
980 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
981 } else if (!strcmp((char *) context_type
,
982 config_event_context_prio
)) {
983 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
984 } else if (!strcmp((char *) context_type
,
985 config_event_context_nice
)) {
986 ret
= LTTNG_EVENT_CONTEXT_NICE
;
987 } else if (!strcmp((char *) context_type
,
988 config_event_context_vpid
)) {
989 ret
= LTTNG_EVENT_CONTEXT_VPID
;
990 } else if (!strcmp((char *) context_type
,
991 config_event_context_tid
)) {
992 ret
= LTTNG_EVENT_CONTEXT_TID
;
993 } else if (!strcmp((char *) context_type
,
994 config_event_context_vtid
)) {
995 ret
= LTTNG_EVENT_CONTEXT_VTID
;
996 } else if (!strcmp((char *) context_type
,
997 config_event_context_ppid
)) {
998 ret
= LTTNG_EVENT_CONTEXT_PPID
;
999 } else if (!strcmp((char *) context_type
,
1000 config_event_context_vppid
)) {
1001 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1002 } else if (!strcmp((char *) context_type
,
1003 config_event_context_pthread_id
)) {
1004 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1005 } else if (!strcmp((char *) context_type
,
1006 config_event_context_hostname
)) {
1007 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1008 } else if (!strcmp((char *) context_type
,
1009 config_event_context_ip
)) {
1010 ret
= LTTNG_EVENT_CONTEXT_IP
;
1011 } else if (!strcmp((char *) context_type
,
1012 config_event_context_interruptible
)) {
1013 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1014 } else if (!strcmp((char *) context_type
,
1015 config_event_context_preemptible
)) {
1016 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1017 } else if (!strcmp((char *) context_type
,
1018 config_event_context_need_reschedule
)) {
1019 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1020 } else if (!strcmp((char *) context_type
,
1021 config_event_context_migratable
)) {
1022 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1023 } else if (!strcmp((char *) context_type
,
1024 config_event_context_callstack_user
)) {
1025 ret
= LTTNG_EVENT_CONTEXT_CALLSTACK_USER
;
1026 } else if (!strcmp((char *) context_type
,
1027 config_event_context_callstack_kernel
)) {
1028 ret
= LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL
;
1039 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1044 for (node
= xmlFirstElementChild(domain_node
); node
;
1045 node
= xmlNextElementSibling(node
)) {
1046 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1048 xmlChar
*node_content
= xmlNodeGetContent(node
);
1049 if (!node_content
) {
1050 ret
= -LTTNG_ERR_NOMEM
;
1054 ret
= get_domain_type(node_content
);
1057 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1062 } else if (!strcmp((const char *) node
->name
,
1063 config_element_buffer_type
)) {
1065 xmlChar
*node_content
= xmlNodeGetContent(node
);
1066 if (!node_content
) {
1067 ret
= -LTTNG_ERR_NOMEM
;
1071 ret
= get_buffer_type(node_content
);
1074 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1078 domain
->buf_type
= ret
;
1087 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1092 for (node
= xmlFirstElementChild(net_output_node
); node
;
1093 node
= xmlNextElementSibling(node
)) {
1094 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1096 *control_uri
= (char *) xmlNodeGetContent(node
);
1097 if (!*control_uri
) {
1102 *data_uri
= (char *) xmlNodeGetContent(node
);
1109 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1113 int process_consumer_output(xmlNodePtr consumer_output_node
,
1114 struct consumer_output
*output
)
1121 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1122 node
= xmlNextElementSibling(node
)) {
1123 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1124 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1128 ret
= -LTTNG_ERR_NOMEM
;
1132 ret
= parse_bool(enabled_str
, &output
->enabled
);
1138 xmlNodePtr output_type_node
;
1141 output_type_node
= xmlFirstElementChild(node
);
1142 if (!output_type_node
) {
1143 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1147 if (!strcmp((const char *) output_type_node
->name
,
1148 config_element_path
)) {
1150 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1151 if (!output
->path
) {
1152 ret
= -LTTNG_ERR_NOMEM
;
1157 ret
= get_net_output_uris(output_type_node
,
1158 &output
->control_uri
, &output
->data_uri
);
1170 free(output
->control_uri
);
1171 free(output
->data_uri
);
1172 memset(output
, 0, sizeof(struct consumer_output
));
1178 int create_session_net_output(const char *name
, const char *control_uri
,
1179 const char *data_uri
)
1182 struct lttng_handle
*handle
;
1183 const char *uri
= NULL
;
1187 handle
= lttng_create_handle(name
, NULL
);
1189 ret
= -LTTNG_ERR_NOMEM
;
1193 if (!control_uri
|| !data_uri
) {
1194 uri
= control_uri
? control_uri
: data_uri
;
1199 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1200 lttng_destroy_handle(handle
);
1206 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
,
1207 const struct config_load_session_override_attr
*overrides
)
1210 xmlNodePtr node
= NULL
;
1211 xmlNodePtr snapshot_output_list_node
;
1212 xmlNodePtr snapshot_output_node
;
1214 assert(session_name
);
1216 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1225 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1227 /* Parse and create snapshot outputs */
1229 for (snapshot_output_node
=
1230 xmlFirstElementChild(snapshot_output_list_node
);
1231 snapshot_output_node
; snapshot_output_node
=
1232 xmlNextElementSibling(snapshot_output_node
)) {
1234 uint64_t max_size
= UINT64_MAX
;
1235 struct consumer_output output
= { 0 };
1236 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1237 const char *control_uri
= NULL
;
1238 const char *data_uri
= NULL
;
1239 const char *path
= NULL
;
1241 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1242 node
= xmlNextElementSibling(node
)) {
1243 if (!strcmp((const char *) node
->name
,
1244 config_element_name
)) {
1246 name
= (char *) xmlNodeGetContent(node
);
1248 ret
= -LTTNG_ERR_NOMEM
;
1249 goto error_snapshot_output
;
1251 } else if (!strcmp((const char *) node
->name
,
1252 config_element_max_size
)) {
1253 xmlChar
*content
= xmlNodeGetContent(node
);
1257 ret
= -LTTNG_ERR_NOMEM
;
1258 goto error_snapshot_output
;
1260 ret
= parse_uint(content
, &max_size
);
1263 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1264 goto error_snapshot_output
;
1267 /* consumer_output */
1268 ret
= process_consumer_output(node
, &output
);
1270 goto error_snapshot_output
;
1275 control_uri
= output
.control_uri
;
1276 data_uri
= output
.data_uri
;
1280 if (overrides
->path_url
) {
1281 path
= overrides
->path_url
;
1282 /* Control/data_uri are null */
1286 if (overrides
->ctrl_url
) {
1287 control_uri
= overrides
->ctrl_url
;
1291 if (overrides
->data_url
) {
1292 data_uri
= overrides
->data_url
;
1299 snapshot_output
= lttng_snapshot_output_create();
1300 if (!snapshot_output
) {
1301 ret
= -LTTNG_ERR_NOMEM
;
1302 goto error_snapshot_output
;
1305 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1307 goto error_snapshot_output
;
1310 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1312 goto error_snapshot_output
;
1316 ret
= lttng_snapshot_output_set_ctrl_url(path
,
1319 goto error_snapshot_output
;
1323 ret
= lttng_snapshot_output_set_ctrl_url(control_uri
,
1326 goto error_snapshot_output
;
1331 ret
= lttng_snapshot_output_set_data_url(data_uri
,
1334 goto error_snapshot_output
;
1339 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1340 error_snapshot_output
:
1343 free(output
.control_uri
);
1344 free(output
.data_uri
);
1345 lttng_snapshot_output_destroy(snapshot_output
);
1355 int create_session(const char *name
,
1356 xmlNodePtr output_node
,
1357 uint64_t live_timer_interval
,
1358 const struct config_load_session_override_attr
*overrides
)
1361 struct consumer_output output
= { 0 };
1362 xmlNodePtr consumer_output_node
;
1363 const char *control_uri
= NULL
;
1364 const char *data_uri
= NULL
;
1365 const char *path
= NULL
;
1370 consumer_output_node
= xmlFirstElementChild(output_node
);
1371 if (!consumer_output_node
) {
1372 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1376 if (strcmp((const char *) consumer_output_node
->name
,
1377 config_element_consumer_output
)) {
1378 WARN("Invalid output type, expected %s node",
1379 config_element_consumer_output
);
1380 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1384 ret
= process_consumer_output(consumer_output_node
, &output
);
1390 control_uri
= output
.control_uri
;
1391 data_uri
= output
.data_uri
;
1394 /* Check for override and apply them */
1396 if (overrides
->path_url
) {
1397 path
= overrides
->path_url
;
1398 /* control/data_uri are null */;
1402 if (overrides
->ctrl_url
) {
1403 control_uri
= overrides
->ctrl_url
;
1407 if (overrides
->data_url
) {
1408 data_uri
= overrides
->data_url
;
1416 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1417 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1421 if (control_uri
|| data_uri
) {
1422 /* network destination */
1423 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1425 * URLs are provided for sure since the test above make sure that
1426 * with a live timer the data and control URIs are provided. So,
1427 * NULL is passed here and will be set right after.
1429 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1431 ret
= lttng_create_session(name
, NULL
);
1437 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1443 /* either local output or no output */
1444 ret
= lttng_create_session(name
, path
);
1451 free(output
.control_uri
);
1452 free(output
.data_uri
);
1456 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1457 struct lttng_event_probe_attr
*attr
)
1461 assert(probe_attribute_node
);
1464 if (!strcmp((const char *) probe_attribute_node
->name
,
1465 config_element_address
)) {
1470 content
= xmlNodeGetContent(probe_attribute_node
);
1472 ret
= -LTTNG_ERR_NOMEM
;
1476 ret
= parse_uint(content
, &addr
);
1479 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1484 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1485 config_element_offset
)) {
1487 uint64_t offset
= 0;
1490 content
= xmlNodeGetContent(probe_attribute_node
);
1492 ret
= -LTTNG_ERR_NOMEM
;
1496 ret
= parse_uint(content
, &offset
);
1499 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1503 attr
->offset
= offset
;
1504 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1505 config_element_symbol_name
)) {
1509 content
= xmlNodeGetContent(probe_attribute_node
);
1511 ret
= -LTTNG_ERR_NOMEM
;
1515 ret
= lttng_strncpy(attr
->symbol_name
,
1516 (const char *) content
,
1517 LTTNG_SYMBOL_NAME_LEN
);
1519 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1520 (const char *) content
,
1521 strlen((const char *) content
),
1522 LTTNG_SYMBOL_NAME_LEN
);
1523 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1535 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1536 const char *channel_name
, const enum process_event_node_phase phase
)
1540 struct lttng_event event
;
1541 char **exclusions
= NULL
;
1542 unsigned long exclusion_count
= 0;
1543 char *filter_expression
= NULL
;
1547 assert(channel_name
);
1549 memset(&event
, 0, sizeof(event
));
1551 /* Initialize default log level which varies by domain */
1552 switch (handle
->domain
.type
)
1554 case LTTNG_DOMAIN_JUL
:
1555 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1557 case LTTNG_DOMAIN_LOG4J
:
1558 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1560 case LTTNG_DOMAIN_PYTHON
:
1561 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1563 case LTTNG_DOMAIN_UST
:
1564 case LTTNG_DOMAIN_KERNEL
:
1565 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1571 for (node
= xmlFirstElementChild(event_node
); node
;
1572 node
= xmlNextElementSibling(node
)) {
1573 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1577 content
= xmlNodeGetContent(node
);
1579 ret
= -LTTNG_ERR_NOMEM
;
1583 ret
= lttng_strncpy(event
.name
,
1584 (const char *) content
,
1585 LTTNG_SYMBOL_NAME_LEN
);
1587 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1588 (const char *) content
,
1589 strlen((const char *) content
),
1590 LTTNG_SYMBOL_NAME_LEN
);
1591 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1596 } else if (!strcmp((const char *) node
->name
,
1597 config_element_enabled
)) {
1598 xmlChar
*content
= xmlNodeGetContent(node
);
1602 ret
= -LTTNG_ERR_NOMEM
;
1606 ret
= parse_bool(content
, &event
.enabled
);
1609 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1612 } else if (!strcmp((const char *) node
->name
,
1613 config_element_type
)) {
1614 xmlChar
*content
= xmlNodeGetContent(node
);
1618 ret
= -LTTNG_ERR_NOMEM
;
1622 ret
= get_event_type(content
);
1625 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1630 } else if (!strcmp((const char *) node
->name
,
1631 config_element_loglevel_type
)) {
1632 xmlChar
*content
= xmlNodeGetContent(node
);
1636 ret
= -LTTNG_ERR_NOMEM
;
1640 ret
= get_loglevel_type(content
);
1643 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1647 event
.loglevel_type
= ret
;
1648 } else if (!strcmp((const char *) node
->name
,
1649 config_element_loglevel
)) {
1651 int64_t loglevel
= 0;
1654 content
= xmlNodeGetContent(node
);
1656 ret
= -LTTNG_ERR_NOMEM
;
1660 ret
= parse_int(content
, &loglevel
);
1663 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1667 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1668 WARN("loglevel out of range.");
1669 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1673 event
.loglevel
= loglevel
;
1674 } else if (!strcmp((const char *) node
->name
,
1675 config_element_filter
)) {
1677 xmlNodeGetContent(node
);
1681 ret
= -LTTNG_ERR_NOMEM
;
1685 free(filter_expression
);
1686 filter_expression
= strdup((char *) content
);
1688 if (!filter_expression
) {
1689 ret
= -LTTNG_ERR_NOMEM
;
1692 } else if (!strcmp((const char *) node
->name
,
1693 config_element_exclusions
)) {
1694 xmlNodePtr exclusion_node
;
1695 int exclusion_index
= 0;
1700 * Exclusions has already been initialized,
1703 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1707 exclusion_count
= xmlChildElementCount(node
);
1708 if (!exclusion_count
) {
1712 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1714 exclusion_count
= 0;
1715 ret
= -LTTNG_ERR_NOMEM
;
1719 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1720 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1722 xmlNodeGetContent(exclusion_node
);
1725 ret
= -LTTNG_ERR_NOMEM
;
1729 exclusions
[exclusion_index
] = strdup((const char *) content
);
1731 if (!exclusions
[exclusion_index
]) {
1732 ret
= -LTTNG_ERR_NOMEM
;
1738 event
.exclusion
= 1;
1739 } else if (!strcmp((const char *) node
->name
,
1740 config_element_attributes
)) {
1741 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1744 if (!attribute_node
) {
1745 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1749 if (!strcmp((const char *) node
->name
,
1750 config_element_probe_attributes
)) {
1751 xmlNodePtr probe_attribute_node
;
1753 /* probe_attributes */
1754 for (probe_attribute_node
=
1755 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1756 probe_attribute_node
= xmlNextElementSibling(
1757 probe_attribute_node
)) {
1759 ret
= process_probe_attribute_node(probe_attribute_node
,
1768 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1770 /* function_attributes */
1771 content
= xmlNodeGetContent(symbol_node
);
1773 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1777 sym_len
= strlen((char *) content
);
1778 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1779 WARN("Function name too long.");
1780 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1785 ret
= lttng_strncpy(
1786 event
.attr
.ftrace
.symbol_name
,
1787 (char *) content
, sym_len
);
1789 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1798 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1799 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1800 filter_expression
, exclusion_count
, exclusions
);
1802 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1803 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1809 for (i
= 0; i
< exclusion_count
; i
++) {
1810 free(exclusions
[i
]);
1814 free(filter_expression
);
1819 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1820 const char *channel_name
)
1823 struct lttng_event event
;
1826 assert(events_node
);
1828 assert(channel_name
);
1830 for (node
= xmlFirstElementChild(events_node
); node
;
1831 node
= xmlNextElementSibling(node
)) {
1832 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1839 * Disable all events to enable only the necessary events.
1840 * Limitations regarding lttng_disable_events and tuple descriptor
1841 * force this approach.
1843 memset(&event
, 0, sizeof(event
));
1844 event
.loglevel
= -1;
1845 event
.type
= LTTNG_EVENT_ALL
;
1846 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1851 for (node
= xmlFirstElementChild(events_node
); node
;
1852 node
= xmlNextElementSibling(node
)) {
1853 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1864 int process_channel_attr_node(xmlNodePtr attr_node
,
1865 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1866 xmlNodePtr
*events_node
)
1872 assert(contexts_node
);
1873 assert(events_node
);
1875 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1879 content
= xmlNodeGetContent(attr_node
);
1881 ret
= -LTTNG_ERR_NOMEM
;
1885 ret
= lttng_strncpy(channel
->name
,
1886 (const char *) content
,
1887 LTTNG_SYMBOL_NAME_LEN
);
1889 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1890 (const char *) content
,
1891 strlen((const char *) content
),
1892 LTTNG_SYMBOL_NAME_LEN
);
1893 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1898 } else if (!strcmp((const char *) attr_node
->name
,
1899 config_element_enabled
)) {
1904 content
= xmlNodeGetContent(attr_node
);
1906 ret
= -LTTNG_ERR_NOMEM
;
1910 ret
= parse_bool(content
, &enabled
);
1913 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1917 channel
->enabled
= enabled
;
1918 } else if (!strcmp((const char *) attr_node
->name
,
1919 config_element_overwrite_mode
)) {
1922 /* overwrite_mode */
1923 content
= xmlNodeGetContent(attr_node
);
1925 ret
= -LTTNG_ERR_NOMEM
;
1929 ret
= get_overwrite_mode(content
);
1932 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1936 channel
->attr
.overwrite
= ret
;
1937 } else if (!strcmp((const char *) attr_node
->name
,
1938 config_element_subbuf_size
)) {
1941 /* subbuffer_size */
1942 content
= xmlNodeGetContent(attr_node
);
1944 ret
= -LTTNG_ERR_NOMEM
;
1948 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1951 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1954 } else if (!strcmp((const char *) attr_node
->name
,
1955 config_element_num_subbuf
)) {
1958 /* subbuffer_count */
1959 content
= xmlNodeGetContent(attr_node
);
1961 ret
= -LTTNG_ERR_NOMEM
;
1965 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1968 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1971 } else if (!strcmp((const char *) attr_node
->name
,
1972 config_element_switch_timer_interval
)) {
1974 uint64_t switch_timer_interval
= 0;
1976 /* switch_timer_interval */
1977 content
= xmlNodeGetContent(attr_node
);
1979 ret
= -LTTNG_ERR_NOMEM
;
1983 ret
= parse_uint(content
, &switch_timer_interval
);
1986 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1990 if (switch_timer_interval
> UINT_MAX
) {
1991 WARN("switch_timer_interval out of range.");
1992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1996 channel
->attr
.switch_timer_interval
=
1997 switch_timer_interval
;
1998 } else if (!strcmp((const char *) attr_node
->name
,
1999 config_element_read_timer_interval
)) {
2001 uint64_t read_timer_interval
= 0;
2003 /* read_timer_interval */
2004 content
= xmlNodeGetContent(attr_node
);
2006 ret
= -LTTNG_ERR_NOMEM
;
2010 ret
= parse_uint(content
, &read_timer_interval
);
2013 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2017 if (read_timer_interval
> UINT_MAX
) {
2018 WARN("read_timer_interval out of range.");
2019 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2023 channel
->attr
.read_timer_interval
=
2024 read_timer_interval
;
2025 } else if (!strcmp((const char *) attr_node
->name
,
2026 config_element_output_type
)) {
2030 content
= xmlNodeGetContent(attr_node
);
2032 ret
= -LTTNG_ERR_NOMEM
;
2036 ret
= get_output_type(content
);
2039 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2043 channel
->attr
.output
= ret
;
2044 } else if (!strcmp((const char *) attr_node
->name
,
2045 config_element_tracefile_size
)) {
2048 /* tracefile_size */
2049 content
= xmlNodeGetContent(attr_node
);
2051 ret
= -LTTNG_ERR_NOMEM
;
2055 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2061 } else if (!strcmp((const char *) attr_node
->name
,
2062 config_element_tracefile_count
)) {
2065 /* tracefile_count */
2066 content
= xmlNodeGetContent(attr_node
);
2068 ret
= -LTTNG_ERR_NOMEM
;
2072 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2075 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2078 } else if (!strcmp((const char *) attr_node
->name
,
2079 config_element_live_timer_interval
)) {
2081 uint64_t live_timer_interval
= 0;
2083 /* live_timer_interval */
2084 content
= xmlNodeGetContent(attr_node
);
2086 ret
= -LTTNG_ERR_NOMEM
;
2090 ret
= parse_uint(content
, &live_timer_interval
);
2093 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2097 if (live_timer_interval
> UINT_MAX
) {
2098 WARN("live_timer_interval out of range.");
2099 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2103 channel
->attr
.live_timer_interval
=
2104 live_timer_interval
;
2105 } else if (!strcmp((const char *) attr_node
->name
,
2106 config_element_monitor_timer_interval
)) {
2108 uint64_t monitor_timer_interval
= 0;
2110 /* monitor_timer_interval */
2111 content
= xmlNodeGetContent(attr_node
);
2113 ret
= -LTTNG_ERR_NOMEM
;
2117 ret
= parse_uint(content
, &monitor_timer_interval
);
2120 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2124 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2125 monitor_timer_interval
);
2127 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2130 } else if (!strcmp((const char *) attr_node
->name
,
2131 config_element_blocking_timeout
)) {
2133 int64_t blocking_timeout
= 0;
2135 /* blocking_timeout */
2136 content
= xmlNodeGetContent(attr_node
);
2138 ret
= -LTTNG_ERR_NOMEM
;
2142 ret
= parse_int(content
, &blocking_timeout
);
2145 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2149 ret
= lttng_channel_set_blocking_timeout(channel
,
2152 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2155 } else if (!strcmp((const char *) attr_node
->name
,
2156 config_element_events
)) {
2158 *events_node
= attr_node
;
2161 *contexts_node
= attr_node
;
2169 int process_context_node(xmlNodePtr context_node
,
2170 struct lttng_handle
*handle
, const char *channel_name
)
2173 struct lttng_event_context context
;
2174 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2177 assert(channel_name
);
2179 if (!context_child_node
) {
2180 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2184 memset(&context
, 0, sizeof(context
));
2186 if (!strcmp((const char *) context_child_node
->name
,
2187 config_element_type
)) {
2189 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2192 ret
= -LTTNG_ERR_NOMEM
;
2196 ret
= get_context_type(content
);
2199 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2204 } else if (!strcmp((const char *) context_child_node
->name
,
2205 config_element_context_perf
)) {
2207 xmlNodePtr perf_attr_node
;
2209 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2210 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2211 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2212 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2213 perf_attr_node
; perf_attr_node
=
2214 xmlNextElementSibling(perf_attr_node
)) {
2215 if (!strcmp((const char *) perf_attr_node
->name
,
2216 config_element_type
)) {
2221 content
= xmlNodeGetContent(perf_attr_node
);
2223 ret
= -LTTNG_ERR_NOMEM
;
2227 ret
= parse_uint(content
, &type
);
2230 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2234 if (type
> UINT32_MAX
) {
2235 WARN("perf context type out of range.");
2236 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2240 context
.u
.perf_counter
.type
= type
;
2241 } else if (!strcmp((const char *) perf_attr_node
->name
,
2242 config_element_config
)) {
2244 uint64_t config
= 0;
2247 content
= xmlNodeGetContent(perf_attr_node
);
2249 ret
= -LTTNG_ERR_NOMEM
;
2253 ret
= parse_uint(content
, &config
);
2256 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2260 context
.u
.perf_counter
.config
= config
;
2261 } else if (!strcmp((const char *) perf_attr_node
->name
,
2262 config_element_name
)) {
2266 content
= xmlNodeGetContent(perf_attr_node
);
2268 ret
= -LTTNG_ERR_NOMEM
;
2272 ret
= lttng_strncpy(context
.u
.perf_counter
.name
,
2273 (const char *) content
,
2274 LTTNG_SYMBOL_NAME_LEN
);
2276 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2277 (const char *) content
,
2278 strlen((const char *) content
),
2279 LTTNG_SYMBOL_NAME_LEN
);
2280 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2287 } else if (!strcmp((const char *) context_child_node
->name
,
2288 config_element_context_app
)) {
2289 /* application context */
2290 xmlNodePtr app_ctx_node
;
2292 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2293 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2294 app_ctx_node
; app_ctx_node
=
2295 xmlNextElementSibling(app_ctx_node
)) {
2297 char **target
= strcmp(
2298 (const char *) app_ctx_node
->name
,
2299 config_element_context_app_provider_name
) == 0 ?
2300 &context
.u
.app_ctx
.provider_name
:
2301 &context
.u
.app_ctx
.ctx_name
;
2303 content
= xmlNodeGetContent(app_ctx_node
);
2305 ret
= -LTTNG_ERR_NOMEM
;
2309 *target
= (char *) content
;
2312 /* Unrecognized context type */
2313 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2317 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2318 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2319 free(context
.u
.app_ctx
.provider_name
);
2320 free(context
.u
.app_ctx
.ctx_name
);
2327 int process_contexts_node(xmlNodePtr contexts_node
,
2328 struct lttng_handle
*handle
, const char *channel_name
)
2331 xmlNodePtr context_node
;
2333 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2334 context_node
= xmlNextElementSibling(context_node
)) {
2335 ret
= process_context_node(context_node
, handle
, channel_name
);
2345 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2346 struct lttng_handle
*handle
)
2349 xmlNodePtr targets_node
= NULL
;
2353 assert(pid_tracker_node
);
2354 /* get the targets node */
2355 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2356 node
= xmlNextElementSibling(node
)) {
2357 if (!strcmp((const char *) node
->name
,
2358 config_element_targets
)) {
2359 targets_node
= node
;
2364 if (!targets_node
) {
2365 ret
= LTTNG_ERR_INVALID
;
2369 /* Go through all pid_target node */
2370 child
= xmlChildElementCount(targets_node
);
2372 /* The session is explicitly set to target nothing. */
2373 ret
= lttng_untrack_pid(handle
, -1);
2378 for (node
= xmlFirstElementChild(targets_node
); node
;
2379 node
= xmlNextElementSibling(node
)) {
2380 xmlNodePtr pid_target_node
= node
;
2382 /* get pid node and track it */
2383 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2384 node
= xmlNextElementSibling(node
)) {
2385 if (!strcmp((const char *) node
->name
,
2386 config_element_pid
)) {
2388 xmlChar
*content
= NULL
;
2390 content
= xmlNodeGetContent(node
);
2392 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2396 ret
= parse_int(content
, &pid
);
2399 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2403 ret
= lttng_track_pid(handle
, (int) pid
);
2409 node
= pid_target_node
;
2417 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2420 struct lttng_domain domain
= { 0 };
2421 struct lttng_handle
*handle
= NULL
;
2422 struct lttng_channel
*channel
= NULL
;
2423 xmlNodePtr channels_node
= NULL
;
2424 xmlNodePtr trackers_node
= NULL
;
2425 xmlNodePtr pid_tracker_node
= NULL
;
2428 assert(session_name
);
2430 ret
= init_domain(domain_node
, &domain
);
2435 handle
= lttng_create_handle(session_name
, &domain
);
2437 ret
= -LTTNG_ERR_NOMEM
;
2441 /* get the channels node */
2442 for (node
= xmlFirstElementChild(domain_node
); node
;
2443 node
= xmlNextElementSibling(node
)) {
2444 if (!strcmp((const char *) node
->name
,
2445 config_element_channels
)) {
2446 channels_node
= node
;
2451 if (!channels_node
) {
2455 /* create all channels */
2456 for (node
= xmlFirstElementChild(channels_node
); node
;
2457 node
= xmlNextElementSibling(node
)) {
2458 const enum lttng_domain_type original_domain
= domain
.type
;
2459 xmlNodePtr contexts_node
= NULL
;
2460 xmlNodePtr events_node
= NULL
;
2461 xmlNodePtr channel_attr_node
;
2464 * Channels of the "agent" types cannot be created directly.
2465 * They are meant to be created implicitly through the
2466 * activation of events in their domain. However, a user
2467 * can override the default channel configuration attributes
2468 * by creating the underlying UST channel _before_ enabling
2469 * an agent domain event.
2471 * Hence, the channel's type is substituted before the creation
2472 * and restored by the time the events are created.
2474 switch (domain
.type
) {
2475 case LTTNG_DOMAIN_JUL
:
2476 case LTTNG_DOMAIN_LOG4J
:
2477 case LTTNG_DOMAIN_PYTHON
:
2478 domain
.type
= LTTNG_DOMAIN_UST
;
2483 channel
= lttng_channel_create(&domain
);
2489 for (channel_attr_node
= xmlFirstElementChild(node
);
2490 channel_attr_node
; channel_attr_node
=
2491 xmlNextElementSibling(channel_attr_node
)) {
2492 ret
= process_channel_attr_node(channel_attr_node
,
2493 channel
, &contexts_node
, &events_node
);
2499 ret
= lttng_enable_channel(handle
, channel
);
2504 /* Restore the original channel domain. */
2505 domain
.type
= original_domain
;
2507 ret
= process_events_node(events_node
, handle
, channel
->name
);
2512 ret
= process_contexts_node(contexts_node
, handle
,
2518 lttng_channel_destroy(channel
);
2522 /* get the trackers node */
2523 for (node
= xmlFirstElementChild(domain_node
); node
;
2524 node
= xmlNextElementSibling(node
)) {
2525 if (!strcmp((const char *) node
->name
,
2526 config_element_trackers
)) {
2527 trackers_node
= node
;
2532 if (!trackers_node
) {
2536 for (node
= xmlFirstElementChild(trackers_node
); node
;
2537 node
= xmlNextElementSibling(node
)) {
2538 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2539 pid_tracker_node
= node
;
2540 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2547 if (!pid_tracker_node
) {
2548 lttng_track_pid(handle
, -1);
2552 lttng_channel_destroy(channel
);
2553 lttng_destroy_handle(handle
);
2558 int add_periodic_rotation(const char *name
, uint64_t time_us
)
2561 enum lttng_rotation_status status
;
2562 struct lttng_rotation_schedule
*periodic
=
2563 lttng_rotation_schedule_periodic_create();
2566 ret
= -LTTNG_ERR_NOMEM
;
2570 status
= lttng_rotation_schedule_periodic_set_period(periodic
,
2572 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2573 ret
= -LTTNG_ERR_INVALID
;
2577 status
= lttng_session_add_rotation_schedule(name
, periodic
);
2579 case LTTNG_ROTATION_STATUS_OK
:
2582 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2583 case LTTNG_ROTATION_STATUS_INVALID
:
2584 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2587 ret
= -LTTNG_ERR_UNK
;
2591 lttng_rotation_schedule_destroy(periodic
);
2596 int add_size_rotation(const char *name
, uint64_t size_bytes
)
2599 enum lttng_rotation_status status
;
2600 struct lttng_rotation_schedule
*size
=
2601 lttng_rotation_schedule_size_threshold_create();
2604 ret
= -LTTNG_ERR_NOMEM
;
2608 status
= lttng_rotation_schedule_size_threshold_set_threshold(size
,
2610 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2611 ret
= -LTTNG_ERR_INVALID
;
2615 status
= lttng_session_add_rotation_schedule(name
, size
);
2617 case LTTNG_ROTATION_STATUS_OK
:
2620 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2621 case LTTNG_ROTATION_STATUS_INVALID
:
2622 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2625 ret
= -LTTNG_ERR_UNK
;
2629 lttng_rotation_schedule_destroy(size
);
2634 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2636 const struct config_load_session_override_attr
*overrides
)
2638 int ret
, started
= -1, snapshot_mode
= -1;
2639 uint64_t live_timer_interval
= UINT64_MAX
,
2640 rotation_timer_interval
= 0,
2642 xmlChar
*name
= NULL
;
2643 xmlChar
*shm_path
= NULL
;
2644 xmlNodePtr domains_node
= NULL
;
2645 xmlNodePtr output_node
= NULL
;
2647 xmlNodePtr attributes_child
;
2648 struct lttng_domain
*kernel_domain
= NULL
;
2649 struct lttng_domain
*ust_domain
= NULL
;
2650 struct lttng_domain
*jul_domain
= NULL
;
2651 struct lttng_domain
*log4j_domain
= NULL
;
2652 struct lttng_domain
*python_domain
= NULL
;
2654 for (node
= xmlFirstElementChild(session_node
); node
;
2655 node
= xmlNextElementSibling(node
)) {
2656 if (!name
&& !strcmp((const char *) node
->name
,
2657 config_element_name
)) {
2659 xmlChar
*node_content
= xmlNodeGetContent(node
);
2660 if (!node_content
) {
2661 ret
= -LTTNG_ERR_NOMEM
;
2665 name
= node_content
;
2666 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2667 config_element_domains
)) {
2669 domains_node
= node
;
2670 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2671 config_element_started
)) {
2673 xmlChar
*node_content
= xmlNodeGetContent(node
);
2674 if (!node_content
) {
2675 ret
= -LTTNG_ERR_NOMEM
;
2679 ret
= parse_bool(node_content
, &started
);
2682 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2685 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2686 config_element_output
)) {
2689 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2690 config_element_shared_memory_path
)) {
2691 /* shared memory path */
2692 xmlChar
*node_content
= xmlNodeGetContent(node
);
2693 if (!node_content
) {
2694 ret
= -LTTNG_ERR_NOMEM
;
2698 shm_path
= node_content
;
2701 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2702 * rotation_timer_interval.
2704 for (attributes_child
= xmlFirstElementChild(node
); attributes_child
;
2705 attributes_child
= xmlNextElementSibling(attributes_child
)) {
2706 if (!strcmp((const char *) attributes_child
->name
,
2707 config_element_snapshot_mode
)) {
2709 xmlChar
*snapshot_mode_content
=
2710 xmlNodeGetContent(attributes_child
);
2711 if (!snapshot_mode_content
) {
2712 ret
= -LTTNG_ERR_NOMEM
;
2716 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2717 free(snapshot_mode_content
);
2719 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2722 } else if (!strcmp((const char *) attributes_child
->name
,
2723 config_element_live_timer_interval
)) {
2724 /* live_timer_interval */
2725 xmlChar
*timer_interval_content
=
2726 xmlNodeGetContent(attributes_child
);
2727 if (!timer_interval_content
) {
2728 ret
= -LTTNG_ERR_NOMEM
;
2732 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2733 free(timer_interval_content
);
2735 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2739 if (!strcmp((const char *) attributes_child
->name
,
2740 config_element_rotation_timer_interval
)) {
2741 /* rotation_timer_interval */
2742 xmlChar
*timer_interval_content
=
2743 xmlNodeGetContent(attributes_child
);
2744 if (!timer_interval_content
) {
2745 ret
= -LTTNG_ERR_NOMEM
;
2749 ret
= parse_uint(timer_interval_content
, &rotation_timer_interval
);
2750 free(timer_interval_content
);
2752 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2756 if (!strcmp((const char *) attributes_child
->name
,
2757 config_element_rotation_size
)) {
2759 xmlChar
*rotation_size_content
=
2760 xmlNodeGetContent(attributes_child
);
2761 if (!rotation_size_content
) {
2762 ret
= -LTTNG_ERR_NOMEM
;
2766 ret
= parse_uint(rotation_size_content
, &rotation_size
);
2767 free(rotation_size_content
);
2769 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2778 /* Mandatory attribute, as defined in the session XSD */
2779 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2783 if (session_name
&& strcmp((char *) name
, session_name
)) {
2784 /* This is not the session we are looking for */
2785 ret
= -LTTNG_ERR_NO_SESSION
;
2789 /* Init domains to create the session handles */
2790 for (node
= xmlFirstElementChild(domains_node
); node
;
2791 node
= xmlNextElementSibling(node
)) {
2792 struct lttng_domain
*domain
;
2794 domain
= zmalloc(sizeof(*domain
));
2796 ret
= -LTTNG_ERR_NOMEM
;
2800 ret
= init_domain(node
, domain
);
2802 goto domain_init_error
;
2805 switch (domain
->type
) {
2806 case LTTNG_DOMAIN_KERNEL
:
2807 if (kernel_domain
) {
2808 /* Same domain seen twice, invalid! */
2809 goto domain_init_error
;
2811 kernel_domain
= domain
;
2813 case LTTNG_DOMAIN_UST
:
2815 /* Same domain seen twice, invalid! */
2816 goto domain_init_error
;
2818 ust_domain
= domain
;
2820 case LTTNG_DOMAIN_JUL
:
2822 /* Same domain seen twice, invalid! */
2823 goto domain_init_error
;
2825 jul_domain
= domain
;
2827 case LTTNG_DOMAIN_LOG4J
:
2829 /* Same domain seen twice, invalid! */
2830 goto domain_init_error
;
2832 log4j_domain
= domain
;
2834 case LTTNG_DOMAIN_PYTHON
:
2835 if (python_domain
) {
2836 /* Same domain seen twice, invalid! */
2837 goto domain_init_error
;
2839 python_domain
= domain
;
2842 WARN("Invalid domain type");
2843 goto domain_init_error
;
2848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2852 /* Apply overrides */
2854 if (overrides
->session_name
) {
2855 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2856 if (!name_override
) {
2857 ret
= -LTTNG_ERR_NOMEM
;
2861 /* Overrides the session name to the provided name */
2863 name
= name_override
;
2868 /* Destroy session if it exists */
2869 ret
= lttng_destroy_session((const char *) name
);
2870 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2871 ERR("Failed to destroy existing session.");
2876 /* Create session type depending on output type */
2877 if (snapshot_mode
&& snapshot_mode
!= -1) {
2878 ret
= create_snapshot_session((const char *) name
, output_node
,
2880 } else if (live_timer_interval
&&
2881 live_timer_interval
!= UINT64_MAX
) {
2882 ret
= create_session((const char *) name
,
2883 output_node
, live_timer_interval
, overrides
);
2885 /* regular session */
2886 ret
= create_session((const char *) name
,
2887 output_node
, UINT64_MAX
, overrides
);
2894 ret
= lttng_set_session_shm_path((const char *) name
,
2895 (const char *) shm_path
);
2901 for (node
= xmlFirstElementChild(domains_node
); node
;
2902 node
= xmlNextElementSibling(node
)) {
2903 ret
= process_domain_node(node
, (const char *) name
);
2909 if (rotation_timer_interval
) {
2910 ret
= add_periodic_rotation((const char *) name
,
2911 rotation_timer_interval
);
2916 if (rotation_size
) {
2917 ret
= add_size_rotation((const char *) name
,
2925 ret
= lttng_start_tracing((const char *) name
);
2933 ERR("Failed to load session %s: %s", (const char *) name
,
2934 lttng_strerror(ret
));
2935 lttng_destroy_session((const char *) name
);
2939 free(kernel_domain
);
2943 free(python_domain
);
2950 * Return 1 if the given path is readable by the current UID or 0 if not.
2951 * Return -1 if the path is EPERM.
2953 static int validate_file_read_creds(const char *path
)
2959 /* Can we read the file. */
2960 ret
= access(path
, R_OK
);
2964 if (errno
== EACCES
) {
2975 int load_session_from_file(const char *path
, const char *session_name
,
2976 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2977 const struct config_load_session_override_attr
*overrides
)
2979 int ret
, session_found
= !session_name
;
2980 xmlDocPtr doc
= NULL
;
2981 xmlNodePtr sessions_node
;
2982 xmlNodePtr session_node
;
2985 assert(validation_ctx
);
2987 ret
= validate_file_read_creds(path
);
2990 ret
= -LTTNG_ERR_EPERM
;
2992 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2997 doc
= xmlParseFile(path
);
2999 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3003 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
3005 ERR("Session configuration file validation failed");
3006 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3010 sessions_node
= xmlDocGetRootElement(doc
);
3011 if (!sessions_node
) {
3015 for (session_node
= xmlFirstElementChild(sessions_node
);
3016 session_node
; session_node
=
3017 xmlNextElementSibling(session_node
)) {
3018 ret
= process_session_node(session_node
,
3019 session_name
, overwrite
, overrides
);
3020 if (session_name
&& ret
== 0) {
3021 /* Target session found and loaded */
3029 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
3035 int load_session_from_path(const char *path
, const char *session_name
,
3036 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3037 const struct config_load_session_override_attr
*overrides
)
3039 int ret
, session_found
= !session_name
;
3040 DIR *directory
= NULL
;
3041 struct lttng_dynamic_buffer file_path
;
3045 assert(validation_ctx
);
3046 path_len
= strlen(path
);
3047 lttng_dynamic_buffer_init(&file_path
);
3048 if (path_len
>= LTTNG_PATH_MAX
) {
3049 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
3050 path
, path_len
, LTTNG_PATH_MAX
);
3051 ret
= -LTTNG_ERR_INVALID
;
3055 directory
= opendir(path
);
3059 /* Try the file loading. */
3062 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3065 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3070 size_t file_path_root_len
;
3072 ret
= lttng_dynamic_buffer_set_capacity(&file_path
,
3075 ret
= -LTTNG_ERR_NOMEM
;
3079 ret
= lttng_dynamic_buffer_append(&file_path
, path
, path_len
);
3081 ret
= -LTTNG_ERR_NOMEM
;
3085 if (file_path
.data
[file_path
.size
- 1] != '/') {
3086 ret
= lttng_dynamic_buffer_append(&file_path
, "/", 1);
3088 ret
= -LTTNG_ERR_NOMEM
;
3092 file_path_root_len
= file_path
.size
;
3094 /* Search for *.lttng files */
3096 size_t file_name_len
;
3097 struct dirent
*result
;
3100 * When the end of the directory stream is reached, NULL
3101 * is returned and errno is kept unchanged. When an
3102 * error occurs, NULL is returned and errno is set
3103 * accordingly. To distinguish between the two, set
3104 * errno to zero before calling readdir().
3106 * On success, readdir() returns a pointer to a dirent
3107 * structure. This structure may be statically
3108 * allocated, do not attempt to free(3) it.
3111 result
= readdir(directory
);
3113 /* Reached end of dir stream or error out. */
3116 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path
);
3117 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3123 file_name_len
= strlen(result
->d_name
);
3125 if (file_name_len
<=
3126 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3130 if (file_path
.size
+ file_name_len
>= LTTNG_PATH_MAX
) {
3131 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3133 /* +1 to account for NULL terminator. */
3134 file_path
.size
+ file_name_len
+ 1,
3139 /* Does the file end with .lttng? */
3140 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3141 result
->d_name
+ file_name_len
- sizeof(
3142 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3146 ret
= lttng_dynamic_buffer_append(&file_path
, result
->d_name
,
3149 ret
= -LTTNG_ERR_NOMEM
;
3153 ret
= load_session_from_file(file_path
.data
, session_name
,
3154 validation_ctx
, overwrite
, overrides
);
3155 if (session_name
&& !ret
) {
3160 * Reset the buffer's size to the location of the
3161 * path's trailing '/'.
3163 ret
= lttng_dynamic_buffer_set_size(&file_path
,
3164 file_path_root_len
);
3166 ret
= -LTTNG_ERR_UNK
;
3172 ret
= load_session_from_file(path
, session_name
,
3173 validation_ctx
, overwrite
, overrides
);
3183 if (closedir(directory
)) {
3187 if (session_found
&& !ret
) {
3190 lttng_dynamic_buffer_reset(&file_path
);
3195 * Validate that the given path's credentials and the current process have the
3196 * same UID. If so, return 1 else return 0 if it does NOT match.
3198 static int validate_path_creds(const char *path
)
3200 int ret
, uid
= getuid();
3209 ret
= stat(path
, &buf
);
3211 if (errno
!= ENOENT
) {
3217 if (buf
.st_uid
!= uid
) {
3228 int config_load_session(const char *path
, const char *session_name
,
3229 int overwrite
, unsigned int autoload
,
3230 const struct config_load_session_override_attr
*overrides
)
3233 bool session_loaded
= false;
3234 const char *path_ptr
= NULL
;
3235 struct session_config_validation_ctx validation_ctx
= { 0 };
3237 ret
= init_session_config_validation_ctx(&validation_ctx
);
3244 const char *sys_path
;
3247 home_path
= utils_get_home_dir();
3249 char path
[PATH_MAX
];
3252 * Try user session configuration path. Ignore error here so we can
3253 * continue loading the system wide sessions.
3256 ret
= snprintf(path
, sizeof(path
),
3257 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3258 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3260 PERROR("snprintf session autoload home config path");
3265 * Credentials are only validated for the autoload in order to
3266 * avoid any user session daemon to try to load kernel sessions
3267 * automatically and failing all the times.
3269 ret
= validate_path_creds(path
);
3274 ret
= snprintf(path
, sizeof(path
),
3275 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3277 PERROR("snprintf session home config path");
3283 ret
= load_session_from_path(path_ptr
, session_name
,
3284 &validation_ctx
, overwrite
, overrides
);
3285 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3289 * Continue even if the session was found since we have to try
3290 * the system wide sessions.
3292 session_loaded
= true;
3296 /* Reset path pointer for the system wide dir. */
3299 /* Try system wide configuration directory. */
3301 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3302 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3303 ret
= validate_path_creds(sys_path
);
3305 path_ptr
= sys_path
;
3308 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3309 path_ptr
= sys_path
;
3313 ret
= load_session_from_path(path_ptr
, session_name
,
3314 &validation_ctx
, overwrite
, overrides
);
3316 session_loaded
= true;
3320 ret
= access(path
, F_OK
);
3325 ret
= -LTTNG_ERR_INVALID
;
3326 WARN("Session configuration path does not exist.");
3329 ret
= -LTTNG_ERR_EPERM
;
3332 ret
= -LTTNG_ERR_UNK
;
3338 ret
= load_session_from_path(path
, session_name
,
3339 &validation_ctx
, overwrite
, overrides
);
3342 fini_session_config_validation_ctx(&validation_ctx
);
3343 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3345 * Don't report an error if no sessions are found when called
3346 * without a session_name or a search path.
3351 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3352 /* A matching session was found in one of the search paths. */
3359 void __attribute__((destructor
)) session_config_exit(void)