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_schedules
= "rotation_schedules";
137 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_periodic
= "periodic";
138 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_periodic_time_us
= "time_us";
139 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_size_threshold
= "size_threshold";
140 LTTNG_HIDDEN
const char * const config_element_rotation_schedule_size_threshold_bytes
= "bytes";
142 const char * const config_domain_type_kernel
= "KERNEL";
143 const char * const config_domain_type_ust
= "UST";
144 const char * const config_domain_type_jul
= "JUL";
145 const char * const config_domain_type_log4j
= "LOG4J";
146 const char * const config_domain_type_python
= "PYTHON";
148 const char * const config_buffer_type_per_pid
= "PER_PID";
149 const char * const config_buffer_type_per_uid
= "PER_UID";
150 const char * const config_buffer_type_global
= "GLOBAL";
152 const char * const config_overwrite_mode_discard
= "DISCARD";
153 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
155 const char * const config_output_type_splice
= "SPLICE";
156 const char * const config_output_type_mmap
= "MMAP";
158 const char * const config_loglevel_type_all
= "ALL";
159 const char * const config_loglevel_type_range
= "RANGE";
160 const char * const config_loglevel_type_single
= "SINGLE";
162 const char * const config_event_type_all
= "ALL";
163 const char * const config_event_type_tracepoint
= "TRACEPOINT";
164 const char * const config_event_type_probe
= "PROBE";
165 const char * const config_event_type_function
= "FUNCTION";
166 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
167 const char * const config_event_type_noop
= "NOOP";
168 const char * const config_event_type_syscall
= "SYSCALL";
169 const char * const config_event_type_kprobe
= "KPROBE";
170 const char * const config_event_type_kretprobe
= "KRETPROBE";
172 const char * const config_event_context_pid
= "PID";
173 const char * const config_event_context_procname
= "PROCNAME";
174 const char * const config_event_context_prio
= "PRIO";
175 const char * const config_event_context_nice
= "NICE";
176 const char * const config_event_context_vpid
= "VPID";
177 const char * const config_event_context_tid
= "TID";
178 const char * const config_event_context_vtid
= "VTID";
179 const char * const config_event_context_ppid
= "PPID";
180 const char * const config_event_context_vppid
= "VPPID";
181 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
182 const char * const config_event_context_hostname
= "HOSTNAME";
183 const char * const config_event_context_ip
= "IP";
184 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
185 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
186 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
187 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
188 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
189 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
190 LTTNG_HIDDEN
const char * const config_event_context_callstack_user
= "CALLSTACK_USER";
191 LTTNG_HIDDEN
const char * const config_event_context_callstack_kernel
= "CALLSTACK_KERNEL";
193 /* Deprecated symbols */
194 const char * const config_element_perf
;
196 enum process_event_node_phase
{
201 struct consumer_output
{
208 static int config_entry_handler_filter(struct handler_filter_args
*args
,
209 const char *section
, const char *name
, const char *value
)
212 struct config_entry entry
= { section
, name
, value
};
216 if (!section
|| !name
|| !value
) {
222 if (strcmp(args
->section
, section
)) {
227 ret
= args
->handler(&entry
, args
->user_data
);
233 int config_get_section_entries(const char *override_path
, const char *section
,
234 config_entry_handler_cb handler
, void *user_data
)
238 FILE *config_file
= NULL
;
239 struct handler_filter_args filter
= { section
, handler
, user_data
};
241 /* First, try system-wide conf. file. */
242 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
244 config_file
= fopen(path
, "r");
246 DBG("Loading daemon conf file at %s", path
);
248 * Return value is not very important here since error or not, we
249 * continue and try the next possible conf. file.
251 (void) ini_parse_file(config_file
,
252 (ini_entry_handler
) config_entry_handler_filter
,
257 /* Second is the user local configuration. */
258 path
= utils_get_home_dir();
260 char fullpath
[PATH_MAX
];
262 ret
= snprintf(fullpath
, sizeof(fullpath
),
263 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
265 PERROR("snprintf user conf. path");
269 config_file
= fopen(fullpath
, "r");
271 DBG("Loading daemon user conf file at %s", path
);
273 * Return value is not very important here since error or not, we
274 * continue and try the next possible conf. file.
276 (void) ini_parse_file(config_file
,
277 (ini_entry_handler
) config_entry_handler_filter
,
283 /* Final path is the one that the user might have provided. */
285 config_file
= fopen(override_path
, "r");
287 DBG("Loading daemon command line conf file at %s", override_path
);
288 (void) ini_parse_file(config_file
,
289 (ini_entry_handler
) config_entry_handler_filter
,
293 ERR("Failed to open daemon configuration file at %s",
300 /* Everything went well. */
308 int config_parse_value(const char *value
)
311 char *endptr
, *lower_str
;
321 v
= strtoul(value
, &endptr
, 10);
322 if (endptr
!= value
) {
327 lower_str
= zmalloc(len
+ 1);
334 for (i
= 0; i
< len
; i
++) {
335 lower_str
[i
] = tolower(value
[i
]);
338 if (!strcmp(lower_str
, config_str_yes
) ||
339 !strcmp(lower_str
, config_str_true
) ||
340 !strcmp(lower_str
, config_str_on
)) {
342 } else if (!strcmp(lower_str
, config_str_no
) ||
343 !strcmp(lower_str
, config_str_false
) ||
344 !strcmp(lower_str
, config_str_off
)) {
356 * Returns a xmlChar string which must be released using xmlFree().
358 static xmlChar
*encode_string(const char *in_str
)
360 xmlChar
*out_str
= NULL
;
361 xmlCharEncodingHandlerPtr handler
;
362 int out_len
, ret
, in_len
;
366 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
368 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
372 in_len
= strlen(in_str
);
374 * Add 1 byte for the NULL terminted character. The factor 4 here is
375 * used because UTF-8 characters can take up to 4 bytes.
377 out_len
= (in_len
* 4) + 1;
378 out_str
= xmlMalloc(out_len
);
383 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
390 /* out_len is now the size of out_str */
391 out_str
[out_len
] = '\0';
397 struct config_writer
*config_writer_create(int fd_output
, int indent
)
400 struct config_writer
*writer
;
401 xmlOutputBufferPtr buffer
;
403 writer
= zmalloc(sizeof(struct config_writer
));
405 PERROR("zmalloc config_writer_create");
409 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
414 writer
->writer
= xmlNewTextWriter(buffer
);
415 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
416 config_xml_encoding
, NULL
);
421 ret
= xmlTextWriterSetIndentString(writer
->writer
,
422 BAD_CAST config_xml_indent_string
);
427 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
435 config_writer_destroy(writer
);
440 int config_writer_destroy(struct config_writer
*writer
)
449 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
450 WARN("Could not close XML document");
454 if (writer
->writer
) {
455 xmlFreeTextWriter(writer
->writer
);
464 int config_writer_open_element(struct config_writer
*writer
,
465 const char *element_name
)
468 xmlChar
*encoded_element_name
;
470 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
475 encoded_element_name
= encode_string(element_name
);
476 if (!encoded_element_name
) {
481 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
482 xmlFree(encoded_element_name
);
484 return ret
>= 0 ? 0 : ret
;
488 int config_writer_write_attribute(struct config_writer
*writer
,
489 const char *name
, const char *value
)
492 xmlChar
*encoded_name
= NULL
;
493 xmlChar
*encoded_value
= NULL
;
495 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
500 encoded_name
= encode_string(name
);
506 encoded_value
= encode_string(value
);
507 if (!encoded_value
) {
512 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
515 xmlFree(encoded_name
);
516 xmlFree(encoded_value
);
517 return ret
>= 0 ? 0 : ret
;
521 int config_writer_close_element(struct config_writer
*writer
)
525 if (!writer
|| !writer
->writer
) {
530 ret
= xmlTextWriterEndElement(writer
->writer
);
532 return ret
>= 0 ? 0 : ret
;
536 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
537 const char *element_name
, uint64_t value
)
540 xmlChar
*encoded_element_name
;
542 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
547 encoded_element_name
= encode_string(element_name
);
548 if (!encoded_element_name
) {
553 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
554 encoded_element_name
, "%" PRIu64
, value
);
555 xmlFree(encoded_element_name
);
557 return ret
>= 0 ? 0 : ret
;
561 int config_writer_write_element_signed_int(struct config_writer
*writer
,
562 const char *element_name
, int64_t value
)
565 xmlChar
*encoded_element_name
;
567 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
572 encoded_element_name
= encode_string(element_name
);
573 if (!encoded_element_name
) {
578 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
579 encoded_element_name
, "%" PRIi64
, value
);
580 xmlFree(encoded_element_name
);
582 return ret
>= 0 ? 0 : ret
;
586 int config_writer_write_element_bool(struct config_writer
*writer
,
587 const char *element_name
, int value
)
589 return config_writer_write_element_string(writer
, element_name
,
590 value
? config_xml_true
: config_xml_false
);
594 int config_writer_write_element_string(struct config_writer
*writer
,
595 const char *element_name
, const char *value
)
598 xmlChar
*encoded_element_name
= NULL
;
599 xmlChar
*encoded_value
= NULL
;
601 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
607 encoded_element_name
= encode_string(element_name
);
608 if (!encoded_element_name
) {
613 encoded_value
= encode_string(value
);
614 if (!encoded_value
) {
619 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
622 xmlFree(encoded_element_name
);
623 xmlFree(encoded_value
);
624 return ret
>= 0 ? 0 : ret
;
628 void xml_error_handler(void *ctx
, const char *format
, ...)
634 va_start(args
, format
);
635 ret
= vasprintf(&errMsg
, format
, args
);
638 ERR("String allocation failed in xml error handler");
642 fprintf(stderr
, "XML Error: %s", errMsg
);
647 void fini_session_config_validation_ctx(
648 struct session_config_validation_ctx
*ctx
)
650 if (ctx
->parser_ctx
) {
651 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
655 xmlSchemaFree(ctx
->schema
);
658 if (ctx
->schema_validation_ctx
) {
659 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
662 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
666 char *get_session_config_xsd_path()
669 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
670 size_t base_path_len
;
674 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
677 base_path_len
= strlen(base_path
);
678 max_path_len
= base_path_len
+
679 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
680 xsd_path
= zmalloc(max_path_len
);
685 strcpy(xsd_path
, base_path
);
686 if (xsd_path
[base_path_len
- 1] != '/') {
687 xsd_path
[base_path_len
++] = '/';
690 strcpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
);
696 int init_session_config_validation_ctx(
697 struct session_config_validation_ctx
*ctx
)
700 char *xsd_path
= get_session_config_xsd_path();
703 ret
= -LTTNG_ERR_NOMEM
;
707 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
708 if (!ctx
->parser_ctx
) {
709 ERR("XSD parser context creation failed");
710 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
713 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
714 xml_error_handler
, NULL
);
716 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
718 ERR("XSD parsing failed");
719 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
723 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
724 if (!ctx
->schema_validation_ctx
) {
725 ERR("XSD validation context creation failed");
726 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
730 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
731 xml_error_handler
, NULL
);
736 fini_session_config_validation_ctx(ctx
);
744 int parse_uint(xmlChar
*str
, uint64_t *val
)
754 *val
= strtoull((const char *) str
, &endptr
, 10);
755 if (!endptr
|| *endptr
) {
766 int parse_int(xmlChar
*str
, int64_t *val
)
776 *val
= strtoll((const char *) str
, &endptr
, 10);
777 if (!endptr
|| *endptr
) {
788 int parse_bool(xmlChar
*str
, int *val
)
797 if (!strcmp((const char *) str
, config_xml_true
)) {
799 } else if (!strcmp((const char *) str
, config_xml_false
)) {
802 WARN("Invalid boolean value encoutered (%s).",
811 int get_domain_type(xmlChar
*domain
)
819 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
820 ret
= LTTNG_DOMAIN_KERNEL
;
821 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
822 ret
= LTTNG_DOMAIN_UST
;
823 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
824 ret
= LTTNG_DOMAIN_JUL
;
825 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
826 ret
= LTTNG_DOMAIN_LOG4J
;
827 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
828 ret
= LTTNG_DOMAIN_PYTHON
;
839 int get_buffer_type(xmlChar
*buffer_type
)
847 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
848 ret
= LTTNG_BUFFER_GLOBAL
;
849 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
850 ret
= LTTNG_BUFFER_PER_UID
;
851 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
852 ret
= LTTNG_BUFFER_PER_PID
;
863 int get_overwrite_mode(xmlChar
*overwrite_mode
)
867 if (!overwrite_mode
) {
871 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
873 } else if (!strcmp((char *) overwrite_mode
,
874 config_overwrite_mode_discard
)) {
886 int get_output_type(xmlChar
*output_type
)
894 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
895 ret
= LTTNG_EVENT_MMAP
;
896 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
897 ret
= LTTNG_EVENT_SPLICE
;
908 int get_event_type(xmlChar
*event_type
)
916 if (!strcmp((char *) event_type
, config_event_type_all
)) {
917 ret
= LTTNG_EVENT_ALL
;
918 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
919 ret
= LTTNG_EVENT_TRACEPOINT
;
920 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
921 ret
= LTTNG_EVENT_PROBE
;
922 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
923 ret
= LTTNG_EVENT_FUNCTION
;
924 } else if (!strcmp((char *) event_type
,
925 config_event_type_function_entry
)) {
926 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
927 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
928 ret
= LTTNG_EVENT_NOOP
;
929 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
930 ret
= LTTNG_EVENT_SYSCALL
;
941 int get_loglevel_type(xmlChar
*loglevel_type
)
945 if (!loglevel_type
) {
949 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
950 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
951 } else if (!strcmp((char *) loglevel_type
,
952 config_loglevel_type_range
)) {
953 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
954 } else if (!strcmp((char *) loglevel_type
,
955 config_loglevel_type_single
)) {
956 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
967 * Return the context type or -1 on error.
970 int get_context_type(xmlChar
*context_type
)
978 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
979 ret
= LTTNG_EVENT_CONTEXT_PID
;
980 } else if (!strcmp((char *) context_type
,
981 config_event_context_procname
)) {
982 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
983 } else if (!strcmp((char *) context_type
,
984 config_event_context_prio
)) {
985 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
986 } else if (!strcmp((char *) context_type
,
987 config_event_context_nice
)) {
988 ret
= LTTNG_EVENT_CONTEXT_NICE
;
989 } else if (!strcmp((char *) context_type
,
990 config_event_context_vpid
)) {
991 ret
= LTTNG_EVENT_CONTEXT_VPID
;
992 } else if (!strcmp((char *) context_type
,
993 config_event_context_tid
)) {
994 ret
= LTTNG_EVENT_CONTEXT_TID
;
995 } else if (!strcmp((char *) context_type
,
996 config_event_context_vtid
)) {
997 ret
= LTTNG_EVENT_CONTEXT_VTID
;
998 } else if (!strcmp((char *) context_type
,
999 config_event_context_ppid
)) {
1000 ret
= LTTNG_EVENT_CONTEXT_PPID
;
1001 } else if (!strcmp((char *) context_type
,
1002 config_event_context_vppid
)) {
1003 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1004 } else if (!strcmp((char *) context_type
,
1005 config_event_context_pthread_id
)) {
1006 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1007 } else if (!strcmp((char *) context_type
,
1008 config_event_context_hostname
)) {
1009 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1010 } else if (!strcmp((char *) context_type
,
1011 config_event_context_ip
)) {
1012 ret
= LTTNG_EVENT_CONTEXT_IP
;
1013 } else if (!strcmp((char *) context_type
,
1014 config_event_context_interruptible
)) {
1015 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1016 } else if (!strcmp((char *) context_type
,
1017 config_event_context_preemptible
)) {
1018 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1019 } else if (!strcmp((char *) context_type
,
1020 config_event_context_need_reschedule
)) {
1021 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1022 } else if (!strcmp((char *) context_type
,
1023 config_event_context_migratable
)) {
1024 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1025 } else if (!strcmp((char *) context_type
,
1026 config_event_context_callstack_user
)) {
1027 ret
= LTTNG_EVENT_CONTEXT_CALLSTACK_USER
;
1028 } else if (!strcmp((char *) context_type
,
1029 config_event_context_callstack_kernel
)) {
1030 ret
= LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL
;
1041 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1046 for (node
= xmlFirstElementChild(domain_node
); node
;
1047 node
= xmlNextElementSibling(node
)) {
1048 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1050 xmlChar
*node_content
= xmlNodeGetContent(node
);
1051 if (!node_content
) {
1052 ret
= -LTTNG_ERR_NOMEM
;
1056 ret
= get_domain_type(node_content
);
1059 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1064 } else if (!strcmp((const char *) node
->name
,
1065 config_element_buffer_type
)) {
1067 xmlChar
*node_content
= xmlNodeGetContent(node
);
1068 if (!node_content
) {
1069 ret
= -LTTNG_ERR_NOMEM
;
1073 ret
= get_buffer_type(node_content
);
1076 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1080 domain
->buf_type
= ret
;
1089 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1094 for (node
= xmlFirstElementChild(net_output_node
); node
;
1095 node
= xmlNextElementSibling(node
)) {
1096 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1098 *control_uri
= (char *) xmlNodeGetContent(node
);
1099 if (!*control_uri
) {
1104 *data_uri
= (char *) xmlNodeGetContent(node
);
1111 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1115 int process_consumer_output(xmlNodePtr consumer_output_node
,
1116 struct consumer_output
*output
)
1123 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1124 node
= xmlNextElementSibling(node
)) {
1125 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1126 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1130 ret
= -LTTNG_ERR_NOMEM
;
1134 ret
= parse_bool(enabled_str
, &output
->enabled
);
1140 xmlNodePtr output_type_node
;
1143 output_type_node
= xmlFirstElementChild(node
);
1144 if (!output_type_node
) {
1145 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1149 if (!strcmp((const char *) output_type_node
->name
,
1150 config_element_path
)) {
1152 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1153 if (!output
->path
) {
1154 ret
= -LTTNG_ERR_NOMEM
;
1159 ret
= get_net_output_uris(output_type_node
,
1160 &output
->control_uri
, &output
->data_uri
);
1172 free(output
->control_uri
);
1173 free(output
->data_uri
);
1174 memset(output
, 0, sizeof(struct consumer_output
));
1180 int create_session_net_output(const char *name
, const char *control_uri
,
1181 const char *data_uri
)
1184 struct lttng_handle
*handle
;
1185 const char *uri
= NULL
;
1189 handle
= lttng_create_handle(name
, NULL
);
1191 ret
= -LTTNG_ERR_NOMEM
;
1195 if (!control_uri
|| !data_uri
) {
1196 uri
= control_uri
? control_uri
: data_uri
;
1201 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1202 lttng_destroy_handle(handle
);
1208 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
,
1209 const struct config_load_session_override_attr
*overrides
)
1212 xmlNodePtr node
= NULL
;
1213 xmlNodePtr snapshot_output_list_node
;
1214 xmlNodePtr snapshot_output_node
;
1216 assert(session_name
);
1218 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1227 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1229 /* Parse and create snapshot outputs */
1231 for (snapshot_output_node
=
1232 xmlFirstElementChild(snapshot_output_list_node
);
1233 snapshot_output_node
; snapshot_output_node
=
1234 xmlNextElementSibling(snapshot_output_node
)) {
1236 uint64_t max_size
= UINT64_MAX
;
1237 struct consumer_output output
= { 0 };
1238 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1239 const char *control_uri
= NULL
;
1240 const char *data_uri
= NULL
;
1241 const char *path
= NULL
;
1243 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1244 node
= xmlNextElementSibling(node
)) {
1245 if (!strcmp((const char *) node
->name
,
1246 config_element_name
)) {
1248 name
= (char *) xmlNodeGetContent(node
);
1250 ret
= -LTTNG_ERR_NOMEM
;
1251 goto error_snapshot_output
;
1253 } else if (!strcmp((const char *) node
->name
,
1254 config_element_max_size
)) {
1255 xmlChar
*content
= xmlNodeGetContent(node
);
1259 ret
= -LTTNG_ERR_NOMEM
;
1260 goto error_snapshot_output
;
1262 ret
= parse_uint(content
, &max_size
);
1265 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1266 goto error_snapshot_output
;
1269 /* consumer_output */
1270 ret
= process_consumer_output(node
, &output
);
1272 goto error_snapshot_output
;
1277 control_uri
= output
.control_uri
;
1278 data_uri
= output
.data_uri
;
1282 if (overrides
->path_url
) {
1283 path
= overrides
->path_url
;
1284 /* Control/data_uri are null */
1288 if (overrides
->ctrl_url
) {
1289 control_uri
= overrides
->ctrl_url
;
1293 if (overrides
->data_url
) {
1294 data_uri
= overrides
->data_url
;
1301 snapshot_output
= lttng_snapshot_output_create();
1302 if (!snapshot_output
) {
1303 ret
= -LTTNG_ERR_NOMEM
;
1304 goto error_snapshot_output
;
1307 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1309 goto error_snapshot_output
;
1312 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1314 goto error_snapshot_output
;
1318 ret
= lttng_snapshot_output_set_ctrl_url(path
,
1321 goto error_snapshot_output
;
1325 ret
= lttng_snapshot_output_set_ctrl_url(control_uri
,
1328 goto error_snapshot_output
;
1333 ret
= lttng_snapshot_output_set_data_url(data_uri
,
1336 goto error_snapshot_output
;
1341 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1342 error_snapshot_output
:
1345 free(output
.control_uri
);
1346 free(output
.data_uri
);
1347 lttng_snapshot_output_destroy(snapshot_output
);
1357 int create_session(const char *name
,
1358 xmlNodePtr output_node
,
1359 uint64_t live_timer_interval
,
1360 const struct config_load_session_override_attr
*overrides
)
1363 struct consumer_output output
= { 0 };
1364 xmlNodePtr consumer_output_node
;
1365 const char *control_uri
= NULL
;
1366 const char *data_uri
= NULL
;
1367 const char *path
= NULL
;
1372 consumer_output_node
= xmlFirstElementChild(output_node
);
1373 if (!consumer_output_node
) {
1374 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1378 if (strcmp((const char *) consumer_output_node
->name
,
1379 config_element_consumer_output
)) {
1380 WARN("Invalid output type, expected %s node",
1381 config_element_consumer_output
);
1382 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1386 ret
= process_consumer_output(consumer_output_node
, &output
);
1392 control_uri
= output
.control_uri
;
1393 data_uri
= output
.data_uri
;
1396 /* Check for override and apply them */
1398 if (overrides
->path_url
) {
1399 path
= overrides
->path_url
;
1400 /* control/data_uri are null */;
1404 if (overrides
->ctrl_url
) {
1405 control_uri
= overrides
->ctrl_url
;
1409 if (overrides
->data_url
) {
1410 data_uri
= overrides
->data_url
;
1418 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1419 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1423 if (control_uri
|| data_uri
) {
1424 /* network destination */
1425 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1427 * URLs are provided for sure since the test above make sure that
1428 * with a live timer the data and control URIs are provided. So,
1429 * NULL is passed here and will be set right after.
1431 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1433 ret
= lttng_create_session(name
, NULL
);
1439 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1445 /* either local output or no output */
1446 ret
= lttng_create_session(name
, path
);
1453 free(output
.control_uri
);
1454 free(output
.data_uri
);
1458 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1459 struct lttng_event_probe_attr
*attr
)
1463 assert(probe_attribute_node
);
1466 if (!strcmp((const char *) probe_attribute_node
->name
,
1467 config_element_address
)) {
1472 content
= xmlNodeGetContent(probe_attribute_node
);
1474 ret
= -LTTNG_ERR_NOMEM
;
1478 ret
= parse_uint(content
, &addr
);
1481 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1486 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1487 config_element_offset
)) {
1489 uint64_t offset
= 0;
1492 content
= xmlNodeGetContent(probe_attribute_node
);
1494 ret
= -LTTNG_ERR_NOMEM
;
1498 ret
= parse_uint(content
, &offset
);
1501 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1505 attr
->offset
= offset
;
1506 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1507 config_element_symbol_name
)) {
1511 content
= xmlNodeGetContent(probe_attribute_node
);
1513 ret
= -LTTNG_ERR_NOMEM
;
1517 ret
= lttng_strncpy(attr
->symbol_name
,
1518 (const char *) content
,
1519 LTTNG_SYMBOL_NAME_LEN
);
1521 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1522 (const char *) content
,
1523 strlen((const char *) content
),
1524 LTTNG_SYMBOL_NAME_LEN
);
1525 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1537 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1538 const char *channel_name
, const enum process_event_node_phase phase
)
1542 struct lttng_event
*event
;
1543 char **exclusions
= NULL
;
1544 unsigned long exclusion_count
= 0;
1545 char *filter_expression
= NULL
;
1549 assert(channel_name
);
1551 event
= lttng_event_create();
1553 ret
= -LTTNG_ERR_NOMEM
;
1557 /* Initialize default log level which varies by domain */
1558 switch (handle
->domain
.type
)
1560 case LTTNG_DOMAIN_JUL
:
1561 event
->loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1563 case LTTNG_DOMAIN_LOG4J
:
1564 event
->loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1566 case LTTNG_DOMAIN_PYTHON
:
1567 event
->loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1569 case LTTNG_DOMAIN_UST
:
1570 case LTTNG_DOMAIN_KERNEL
:
1571 event
->loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1577 for (node
= xmlFirstElementChild(event_node
); node
;
1578 node
= xmlNextElementSibling(node
)) {
1579 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1583 content
= xmlNodeGetContent(node
);
1585 ret
= -LTTNG_ERR_NOMEM
;
1589 ret
= lttng_strncpy(event
->name
,
1590 (const char *) content
,
1591 LTTNG_SYMBOL_NAME_LEN
);
1593 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1594 (const char *) content
,
1595 strlen((const char *) content
),
1596 LTTNG_SYMBOL_NAME_LEN
);
1597 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1602 } else if (!strcmp((const char *) node
->name
,
1603 config_element_enabled
)) {
1604 xmlChar
*content
= xmlNodeGetContent(node
);
1608 ret
= -LTTNG_ERR_NOMEM
;
1612 ret
= parse_bool(content
, &event
->enabled
);
1615 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1618 } else if (!strcmp((const char *) node
->name
,
1619 config_element_type
)) {
1620 xmlChar
*content
= xmlNodeGetContent(node
);
1624 ret
= -LTTNG_ERR_NOMEM
;
1628 ret
= get_event_type(content
);
1631 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1636 } else if (!strcmp((const char *) node
->name
,
1637 config_element_loglevel_type
)) {
1638 xmlChar
*content
= xmlNodeGetContent(node
);
1642 ret
= -LTTNG_ERR_NOMEM
;
1646 ret
= get_loglevel_type(content
);
1649 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1653 event
->loglevel_type
= ret
;
1654 } else if (!strcmp((const char *) node
->name
,
1655 config_element_loglevel
)) {
1657 int64_t loglevel
= 0;
1660 content
= xmlNodeGetContent(node
);
1662 ret
= -LTTNG_ERR_NOMEM
;
1666 ret
= parse_int(content
, &loglevel
);
1669 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1673 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1674 WARN("loglevel out of range.");
1675 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1679 event
->loglevel
= loglevel
;
1680 } else if (!strcmp((const char *) node
->name
,
1681 config_element_filter
)) {
1683 xmlNodeGetContent(node
);
1687 ret
= -LTTNG_ERR_NOMEM
;
1691 free(filter_expression
);
1692 filter_expression
= strdup((char *) content
);
1694 if (!filter_expression
) {
1695 ret
= -LTTNG_ERR_NOMEM
;
1698 } else if (!strcmp((const char *) node
->name
,
1699 config_element_exclusions
)) {
1700 xmlNodePtr exclusion_node
;
1701 int exclusion_index
= 0;
1706 * Exclusions has already been initialized,
1709 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1713 exclusion_count
= xmlChildElementCount(node
);
1714 if (!exclusion_count
) {
1718 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1720 exclusion_count
= 0;
1721 ret
= -LTTNG_ERR_NOMEM
;
1725 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1726 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1728 xmlNodeGetContent(exclusion_node
);
1731 ret
= -LTTNG_ERR_NOMEM
;
1735 exclusions
[exclusion_index
] = strdup((const char *) content
);
1737 if (!exclusions
[exclusion_index
]) {
1738 ret
= -LTTNG_ERR_NOMEM
;
1744 event
->exclusion
= 1;
1745 } else if (!strcmp((const char *) node
->name
,
1746 config_element_attributes
)) {
1747 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1750 if (!attribute_node
) {
1751 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1755 if (!strcmp((const char *) node
->name
,
1756 config_element_probe_attributes
)) {
1757 xmlNodePtr probe_attribute_node
;
1759 /* probe_attributes */
1760 for (probe_attribute_node
=
1761 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1762 probe_attribute_node
= xmlNextElementSibling(
1763 probe_attribute_node
)) {
1765 ret
= process_probe_attribute_node(probe_attribute_node
,
1766 &event
->attr
.probe
);
1774 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1776 /* function_attributes */
1777 content
= xmlNodeGetContent(symbol_node
);
1779 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1783 sym_len
= strlen((char *) content
);
1784 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1785 WARN("Function name too long.");
1786 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1791 ret
= lttng_strncpy(
1792 event
->attr
.ftrace
.symbol_name
,
1793 (char *) content
, sym_len
);
1795 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1804 if ((event
->enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1805 ret
= lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1806 filter_expression
, exclusion_count
, exclusions
);
1808 WARN("Enabling event (name:%s) on load failed.", event
->name
);
1809 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1815 for (i
= 0; i
< exclusion_count
; i
++) {
1816 free(exclusions
[i
]);
1820 free(filter_expression
);
1825 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1826 const char *channel_name
)
1829 struct lttng_event event
;
1832 assert(events_node
);
1834 assert(channel_name
);
1836 for (node
= xmlFirstElementChild(events_node
); node
;
1837 node
= xmlNextElementSibling(node
)) {
1838 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1845 * Disable all events to enable only the necessary events.
1846 * Limitations regarding lttng_disable_events and tuple descriptor
1847 * force this approach.
1849 memset(&event
, 0, sizeof(event
));
1850 event
.loglevel
= -1;
1851 event
.type
= LTTNG_EVENT_ALL
;
1852 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1857 for (node
= xmlFirstElementChild(events_node
); node
;
1858 node
= xmlNextElementSibling(node
)) {
1859 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1870 int process_channel_attr_node(xmlNodePtr attr_node
,
1871 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1872 xmlNodePtr
*events_node
)
1878 assert(contexts_node
);
1879 assert(events_node
);
1881 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1885 content
= xmlNodeGetContent(attr_node
);
1887 ret
= -LTTNG_ERR_NOMEM
;
1891 ret
= lttng_strncpy(channel
->name
,
1892 (const char *) content
,
1893 LTTNG_SYMBOL_NAME_LEN
);
1895 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1896 (const char *) content
,
1897 strlen((const char *) content
),
1898 LTTNG_SYMBOL_NAME_LEN
);
1899 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1904 } else if (!strcmp((const char *) attr_node
->name
,
1905 config_element_enabled
)) {
1910 content
= xmlNodeGetContent(attr_node
);
1912 ret
= -LTTNG_ERR_NOMEM
;
1916 ret
= parse_bool(content
, &enabled
);
1919 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1923 channel
->enabled
= enabled
;
1924 } else if (!strcmp((const char *) attr_node
->name
,
1925 config_element_overwrite_mode
)) {
1928 /* overwrite_mode */
1929 content
= xmlNodeGetContent(attr_node
);
1931 ret
= -LTTNG_ERR_NOMEM
;
1935 ret
= get_overwrite_mode(content
);
1938 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1942 channel
->attr
.overwrite
= ret
;
1943 } else if (!strcmp((const char *) attr_node
->name
,
1944 config_element_subbuf_size
)) {
1947 /* subbuffer_size */
1948 content
= xmlNodeGetContent(attr_node
);
1950 ret
= -LTTNG_ERR_NOMEM
;
1954 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1957 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1960 } else if (!strcmp((const char *) attr_node
->name
,
1961 config_element_num_subbuf
)) {
1964 /* subbuffer_count */
1965 content
= xmlNodeGetContent(attr_node
);
1967 ret
= -LTTNG_ERR_NOMEM
;
1971 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1974 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1977 } else if (!strcmp((const char *) attr_node
->name
,
1978 config_element_switch_timer_interval
)) {
1980 uint64_t switch_timer_interval
= 0;
1982 /* switch_timer_interval */
1983 content
= xmlNodeGetContent(attr_node
);
1985 ret
= -LTTNG_ERR_NOMEM
;
1989 ret
= parse_uint(content
, &switch_timer_interval
);
1992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1996 if (switch_timer_interval
> UINT_MAX
) {
1997 WARN("switch_timer_interval out of range.");
1998 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2002 channel
->attr
.switch_timer_interval
=
2003 switch_timer_interval
;
2004 } else if (!strcmp((const char *) attr_node
->name
,
2005 config_element_read_timer_interval
)) {
2007 uint64_t read_timer_interval
= 0;
2009 /* read_timer_interval */
2010 content
= xmlNodeGetContent(attr_node
);
2012 ret
= -LTTNG_ERR_NOMEM
;
2016 ret
= parse_uint(content
, &read_timer_interval
);
2019 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2023 if (read_timer_interval
> UINT_MAX
) {
2024 WARN("read_timer_interval out of range.");
2025 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2029 channel
->attr
.read_timer_interval
=
2030 read_timer_interval
;
2031 } else if (!strcmp((const char *) attr_node
->name
,
2032 config_element_output_type
)) {
2036 content
= xmlNodeGetContent(attr_node
);
2038 ret
= -LTTNG_ERR_NOMEM
;
2042 ret
= get_output_type(content
);
2045 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2049 channel
->attr
.output
= ret
;
2050 } else if (!strcmp((const char *) attr_node
->name
,
2051 config_element_tracefile_size
)) {
2054 /* tracefile_size */
2055 content
= xmlNodeGetContent(attr_node
);
2057 ret
= -LTTNG_ERR_NOMEM
;
2061 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2064 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2067 } else if (!strcmp((const char *) attr_node
->name
,
2068 config_element_tracefile_count
)) {
2071 /* tracefile_count */
2072 content
= xmlNodeGetContent(attr_node
);
2074 ret
= -LTTNG_ERR_NOMEM
;
2078 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2081 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2084 } else if (!strcmp((const char *) attr_node
->name
,
2085 config_element_live_timer_interval
)) {
2087 uint64_t live_timer_interval
= 0;
2089 /* live_timer_interval */
2090 content
= xmlNodeGetContent(attr_node
);
2092 ret
= -LTTNG_ERR_NOMEM
;
2096 ret
= parse_uint(content
, &live_timer_interval
);
2099 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2103 if (live_timer_interval
> UINT_MAX
) {
2104 WARN("live_timer_interval out of range.");
2105 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2109 channel
->attr
.live_timer_interval
=
2110 live_timer_interval
;
2111 } else if (!strcmp((const char *) attr_node
->name
,
2112 config_element_monitor_timer_interval
)) {
2114 uint64_t monitor_timer_interval
= 0;
2116 /* monitor_timer_interval */
2117 content
= xmlNodeGetContent(attr_node
);
2119 ret
= -LTTNG_ERR_NOMEM
;
2123 ret
= parse_uint(content
, &monitor_timer_interval
);
2126 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2130 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2131 monitor_timer_interval
);
2133 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2136 } else if (!strcmp((const char *) attr_node
->name
,
2137 config_element_blocking_timeout
)) {
2139 int64_t blocking_timeout
= 0;
2141 /* blocking_timeout */
2142 content
= xmlNodeGetContent(attr_node
);
2144 ret
= -LTTNG_ERR_NOMEM
;
2148 ret
= parse_int(content
, &blocking_timeout
);
2151 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2155 ret
= lttng_channel_set_blocking_timeout(channel
,
2158 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2161 } else if (!strcmp((const char *) attr_node
->name
,
2162 config_element_events
)) {
2164 *events_node
= attr_node
;
2167 *contexts_node
= attr_node
;
2175 int process_context_node(xmlNodePtr context_node
,
2176 struct lttng_handle
*handle
, const char *channel_name
)
2179 struct lttng_event_context context
;
2180 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2183 assert(channel_name
);
2185 if (!context_child_node
) {
2186 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2190 memset(&context
, 0, sizeof(context
));
2192 if (!strcmp((const char *) context_child_node
->name
,
2193 config_element_type
)) {
2195 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2198 ret
= -LTTNG_ERR_NOMEM
;
2202 ret
= get_context_type(content
);
2205 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2210 } else if (!strcmp((const char *) context_child_node
->name
,
2211 config_element_context_perf
)) {
2213 xmlNodePtr perf_attr_node
;
2215 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2216 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2217 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2218 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2219 perf_attr_node
; perf_attr_node
=
2220 xmlNextElementSibling(perf_attr_node
)) {
2221 if (!strcmp((const char *) perf_attr_node
->name
,
2222 config_element_type
)) {
2227 content
= xmlNodeGetContent(perf_attr_node
);
2229 ret
= -LTTNG_ERR_NOMEM
;
2233 ret
= parse_uint(content
, &type
);
2236 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2240 if (type
> UINT32_MAX
) {
2241 WARN("perf context type out of range.");
2242 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2246 context
.u
.perf_counter
.type
= type
;
2247 } else if (!strcmp((const char *) perf_attr_node
->name
,
2248 config_element_config
)) {
2250 uint64_t config
= 0;
2253 content
= xmlNodeGetContent(perf_attr_node
);
2255 ret
= -LTTNG_ERR_NOMEM
;
2259 ret
= parse_uint(content
, &config
);
2262 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2266 context
.u
.perf_counter
.config
= config
;
2267 } else if (!strcmp((const char *) perf_attr_node
->name
,
2268 config_element_name
)) {
2272 content
= xmlNodeGetContent(perf_attr_node
);
2274 ret
= -LTTNG_ERR_NOMEM
;
2278 ret
= lttng_strncpy(context
.u
.perf_counter
.name
,
2279 (const char *) content
,
2280 LTTNG_SYMBOL_NAME_LEN
);
2282 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2283 (const char *) content
,
2284 strlen((const char *) content
),
2285 LTTNG_SYMBOL_NAME_LEN
);
2286 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2293 } else if (!strcmp((const char *) context_child_node
->name
,
2294 config_element_context_app
)) {
2295 /* application context */
2296 xmlNodePtr app_ctx_node
;
2298 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2299 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2300 app_ctx_node
; app_ctx_node
=
2301 xmlNextElementSibling(app_ctx_node
)) {
2303 char **target
= strcmp(
2304 (const char *) app_ctx_node
->name
,
2305 config_element_context_app_provider_name
) == 0 ?
2306 &context
.u
.app_ctx
.provider_name
:
2307 &context
.u
.app_ctx
.ctx_name
;
2309 content
= xmlNodeGetContent(app_ctx_node
);
2311 ret
= -LTTNG_ERR_NOMEM
;
2315 *target
= (char *) content
;
2318 /* Unrecognized context type */
2319 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2323 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2324 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2325 free(context
.u
.app_ctx
.provider_name
);
2326 free(context
.u
.app_ctx
.ctx_name
);
2333 int process_contexts_node(xmlNodePtr contexts_node
,
2334 struct lttng_handle
*handle
, const char *channel_name
)
2337 xmlNodePtr context_node
;
2339 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2340 context_node
= xmlNextElementSibling(context_node
)) {
2341 ret
= process_context_node(context_node
, handle
, channel_name
);
2351 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2352 struct lttng_handle
*handle
)
2355 xmlNodePtr targets_node
= NULL
;
2359 assert(pid_tracker_node
);
2360 /* get the targets node */
2361 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2362 node
= xmlNextElementSibling(node
)) {
2363 if (!strcmp((const char *) node
->name
,
2364 config_element_targets
)) {
2365 targets_node
= node
;
2370 if (!targets_node
) {
2371 ret
= LTTNG_ERR_INVALID
;
2375 /* Go through all pid_target node */
2376 child
= xmlChildElementCount(targets_node
);
2378 /* The session is explicitly set to target nothing. */
2379 ret
= lttng_untrack_pid(handle
, -1);
2384 for (node
= xmlFirstElementChild(targets_node
); node
;
2385 node
= xmlNextElementSibling(node
)) {
2386 xmlNodePtr pid_target_node
= node
;
2388 /* get pid node and track it */
2389 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2390 node
= xmlNextElementSibling(node
)) {
2391 if (!strcmp((const char *) node
->name
,
2392 config_element_pid
)) {
2394 xmlChar
*content
= NULL
;
2396 content
= xmlNodeGetContent(node
);
2398 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2402 ret
= parse_int(content
, &pid
);
2405 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2409 ret
= lttng_track_pid(handle
, (int) pid
);
2415 node
= pid_target_node
;
2423 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2426 struct lttng_domain domain
= { 0 };
2427 struct lttng_handle
*handle
= NULL
;
2428 struct lttng_channel
*channel
= NULL
;
2429 xmlNodePtr channels_node
= NULL
;
2430 xmlNodePtr trackers_node
= NULL
;
2431 xmlNodePtr pid_tracker_node
= NULL
;
2434 assert(session_name
);
2436 ret
= init_domain(domain_node
, &domain
);
2441 handle
= lttng_create_handle(session_name
, &domain
);
2443 ret
= -LTTNG_ERR_NOMEM
;
2447 /* get the channels node */
2448 for (node
= xmlFirstElementChild(domain_node
); node
;
2449 node
= xmlNextElementSibling(node
)) {
2450 if (!strcmp((const char *) node
->name
,
2451 config_element_channels
)) {
2452 channels_node
= node
;
2457 if (!channels_node
) {
2461 /* create all channels */
2462 for (node
= xmlFirstElementChild(channels_node
); node
;
2463 node
= xmlNextElementSibling(node
)) {
2464 const enum lttng_domain_type original_domain
= domain
.type
;
2465 xmlNodePtr contexts_node
= NULL
;
2466 xmlNodePtr events_node
= NULL
;
2467 xmlNodePtr channel_attr_node
;
2470 * Channels of the "agent" types cannot be created directly.
2471 * They are meant to be created implicitly through the
2472 * activation of events in their domain. However, a user
2473 * can override the default channel configuration attributes
2474 * by creating the underlying UST channel _before_ enabling
2475 * an agent domain event.
2477 * Hence, the channel's type is substituted before the creation
2478 * and restored by the time the events are created.
2480 switch (domain
.type
) {
2481 case LTTNG_DOMAIN_JUL
:
2482 case LTTNG_DOMAIN_LOG4J
:
2483 case LTTNG_DOMAIN_PYTHON
:
2484 domain
.type
= LTTNG_DOMAIN_UST
;
2489 channel
= lttng_channel_create(&domain
);
2495 for (channel_attr_node
= xmlFirstElementChild(node
);
2496 channel_attr_node
; channel_attr_node
=
2497 xmlNextElementSibling(channel_attr_node
)) {
2498 ret
= process_channel_attr_node(channel_attr_node
,
2499 channel
, &contexts_node
, &events_node
);
2505 ret
= lttng_enable_channel(handle
, channel
);
2510 /* Restore the original channel domain. */
2511 domain
.type
= original_domain
;
2513 ret
= process_events_node(events_node
, handle
, channel
->name
);
2518 ret
= process_contexts_node(contexts_node
, handle
,
2524 lttng_channel_destroy(channel
);
2528 /* get the trackers node */
2529 for (node
= xmlFirstElementChild(domain_node
); node
;
2530 node
= xmlNextElementSibling(node
)) {
2531 if (!strcmp((const char *) node
->name
,
2532 config_element_trackers
)) {
2533 trackers_node
= node
;
2538 if (!trackers_node
) {
2542 for (node
= xmlFirstElementChild(trackers_node
); node
;
2543 node
= xmlNextElementSibling(node
)) {
2544 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2545 pid_tracker_node
= node
;
2546 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2553 if (!pid_tracker_node
) {
2554 lttng_track_pid(handle
, -1);
2558 lttng_channel_destroy(channel
);
2559 lttng_destroy_handle(handle
);
2564 int add_periodic_rotation(const char *name
, uint64_t time_us
)
2567 enum lttng_rotation_status status
;
2568 struct lttng_rotation_schedule
*periodic
=
2569 lttng_rotation_schedule_periodic_create();
2572 ret
= -LTTNG_ERR_NOMEM
;
2576 status
= lttng_rotation_schedule_periodic_set_period(periodic
,
2578 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2579 ret
= -LTTNG_ERR_INVALID
;
2583 status
= lttng_session_add_rotation_schedule(name
, periodic
);
2585 case LTTNG_ROTATION_STATUS_OK
:
2588 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2589 case LTTNG_ROTATION_STATUS_INVALID
:
2590 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2593 ret
= -LTTNG_ERR_UNK
;
2597 lttng_rotation_schedule_destroy(periodic
);
2602 int add_size_rotation(const char *name
, uint64_t size_bytes
)
2605 enum lttng_rotation_status status
;
2606 struct lttng_rotation_schedule
*size
=
2607 lttng_rotation_schedule_size_threshold_create();
2610 ret
= -LTTNG_ERR_NOMEM
;
2614 status
= lttng_rotation_schedule_size_threshold_set_threshold(size
,
2616 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2617 ret
= -LTTNG_ERR_INVALID
;
2621 status
= lttng_session_add_rotation_schedule(name
, size
);
2623 case LTTNG_ROTATION_STATUS_OK
:
2626 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2627 case LTTNG_ROTATION_STATUS_INVALID
:
2628 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2631 ret
= -LTTNG_ERR_UNK
;
2635 lttng_rotation_schedule_destroy(size
);
2640 int process_session_rotation_schedules_node(
2641 xmlNodePtr schedules_node
,
2642 uint64_t *rotation_timer_interval
,
2643 uint64_t *rotation_size
)
2648 for (child
= xmlFirstElementChild(schedules_node
);
2650 child
= xmlNextElementSibling(child
)) {
2651 if (!strcmp((const char *) child
->name
,
2652 config_element_rotation_schedule_periodic
)) {
2654 xmlNodePtr time_us_node
;
2656 /* periodic rotation schedule */
2657 time_us_node
= xmlFirstElementChild(child
);
2658 if (!time_us_node
||
2659 strcmp((const char *) time_us_node
->name
,
2660 config_element_rotation_schedule_periodic_time_us
)) {
2661 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2666 content
= xmlNodeGetContent(time_us_node
);
2668 ret
= -LTTNG_ERR_NOMEM
;
2671 ret
= parse_uint(content
, rotation_timer_interval
);
2674 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2677 } else if (!strcmp((const char *) child
->name
,
2678 config_element_rotation_schedule_size_threshold
)) {
2680 xmlNodePtr bytes_node
;
2682 /* size_threshold rotation schedule */
2683 bytes_node
= xmlFirstElementChild(child
);
2685 strcmp((const char *) bytes_node
->name
,
2686 config_element_rotation_schedule_size_threshold_bytes
)) {
2687 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2692 content
= xmlNodeGetContent(bytes_node
);
2694 ret
= -LTTNG_ERR_NOMEM
;
2697 ret
= parse_uint(content
, rotation_size
);
2700 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2711 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2713 const struct config_load_session_override_attr
*overrides
)
2715 int ret
, started
= -1, snapshot_mode
= -1;
2716 uint64_t live_timer_interval
= UINT64_MAX
,
2717 rotation_timer_interval
= 0,
2719 xmlChar
*name
= NULL
;
2720 xmlChar
*shm_path
= NULL
;
2721 xmlNodePtr domains_node
= NULL
;
2722 xmlNodePtr output_node
= NULL
;
2724 xmlNodePtr attributes_child
;
2725 struct lttng_domain
*kernel_domain
= NULL
;
2726 struct lttng_domain
*ust_domain
= NULL
;
2727 struct lttng_domain
*jul_domain
= NULL
;
2728 struct lttng_domain
*log4j_domain
= NULL
;
2729 struct lttng_domain
*python_domain
= NULL
;
2731 for (node
= xmlFirstElementChild(session_node
); node
;
2732 node
= xmlNextElementSibling(node
)) {
2733 if (!name
&& !strcmp((const char *) node
->name
,
2734 config_element_name
)) {
2736 xmlChar
*node_content
= xmlNodeGetContent(node
);
2737 if (!node_content
) {
2738 ret
= -LTTNG_ERR_NOMEM
;
2742 name
= node_content
;
2743 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2744 config_element_domains
)) {
2746 domains_node
= node
;
2747 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2748 config_element_started
)) {
2750 xmlChar
*node_content
= xmlNodeGetContent(node
);
2751 if (!node_content
) {
2752 ret
= -LTTNG_ERR_NOMEM
;
2756 ret
= parse_bool(node_content
, &started
);
2759 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2762 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2763 config_element_output
)) {
2766 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2767 config_element_shared_memory_path
)) {
2768 /* shared memory path */
2769 xmlChar
*node_content
= xmlNodeGetContent(node
);
2770 if (!node_content
) {
2771 ret
= -LTTNG_ERR_NOMEM
;
2775 shm_path
= node_content
;
2778 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2779 * rotation_timer_interval.
2781 for (attributes_child
= xmlFirstElementChild(node
); attributes_child
;
2782 attributes_child
= xmlNextElementSibling(attributes_child
)) {
2783 if (!strcmp((const char *) attributes_child
->name
,
2784 config_element_snapshot_mode
)) {
2786 xmlChar
*snapshot_mode_content
=
2787 xmlNodeGetContent(attributes_child
);
2788 if (!snapshot_mode_content
) {
2789 ret
= -LTTNG_ERR_NOMEM
;
2793 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2794 free(snapshot_mode_content
);
2796 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2799 } else if (!strcmp((const char *) attributes_child
->name
,
2800 config_element_live_timer_interval
)) {
2801 /* live_timer_interval */
2802 xmlChar
*timer_interval_content
=
2803 xmlNodeGetContent(attributes_child
);
2804 if (!timer_interval_content
) {
2805 ret
= -LTTNG_ERR_NOMEM
;
2809 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2810 free(timer_interval_content
);
2812 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2815 } else if (!strcmp((const char *) attributes_child
->name
,
2816 config_element_rotation_schedules
)) {
2817 ret
= process_session_rotation_schedules_node(
2819 &rotation_timer_interval
,
2822 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2832 /* Mandatory attribute, as defined in the session XSD */
2833 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2837 if (session_name
&& strcmp((char *) name
, session_name
)) {
2838 /* This is not the session we are looking for */
2839 ret
= -LTTNG_ERR_NO_SESSION
;
2843 /* Init domains to create the session handles */
2844 for (node
= xmlFirstElementChild(domains_node
); node
;
2845 node
= xmlNextElementSibling(node
)) {
2846 struct lttng_domain
*domain
;
2848 domain
= zmalloc(sizeof(*domain
));
2850 ret
= -LTTNG_ERR_NOMEM
;
2854 ret
= init_domain(node
, domain
);
2856 goto domain_init_error
;
2859 switch (domain
->type
) {
2860 case LTTNG_DOMAIN_KERNEL
:
2861 if (kernel_domain
) {
2862 /* Same domain seen twice, invalid! */
2863 goto domain_init_error
;
2865 kernel_domain
= domain
;
2867 case LTTNG_DOMAIN_UST
:
2869 /* Same domain seen twice, invalid! */
2870 goto domain_init_error
;
2872 ust_domain
= domain
;
2874 case LTTNG_DOMAIN_JUL
:
2876 /* Same domain seen twice, invalid! */
2877 goto domain_init_error
;
2879 jul_domain
= domain
;
2881 case LTTNG_DOMAIN_LOG4J
:
2883 /* Same domain seen twice, invalid! */
2884 goto domain_init_error
;
2886 log4j_domain
= domain
;
2888 case LTTNG_DOMAIN_PYTHON
:
2889 if (python_domain
) {
2890 /* Same domain seen twice, invalid! */
2891 goto domain_init_error
;
2893 python_domain
= domain
;
2896 WARN("Invalid domain type");
2897 goto domain_init_error
;
2902 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2906 /* Apply overrides */
2908 if (overrides
->session_name
) {
2909 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2910 if (!name_override
) {
2911 ret
= -LTTNG_ERR_NOMEM
;
2915 /* Overrides the session name to the provided name */
2917 name
= name_override
;
2922 /* Destroy session if it exists */
2923 ret
= lttng_destroy_session((const char *) name
);
2924 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2925 ERR("Failed to destroy existing session.");
2930 /* Create session type depending on output type */
2931 if (snapshot_mode
&& snapshot_mode
!= -1) {
2932 ret
= create_snapshot_session((const char *) name
, output_node
,
2934 } else if (live_timer_interval
&&
2935 live_timer_interval
!= UINT64_MAX
) {
2936 ret
= create_session((const char *) name
,
2937 output_node
, live_timer_interval
, overrides
);
2939 /* regular session */
2940 ret
= create_session((const char *) name
,
2941 output_node
, UINT64_MAX
, overrides
);
2948 ret
= lttng_set_session_shm_path((const char *) name
,
2949 (const char *) shm_path
);
2955 for (node
= xmlFirstElementChild(domains_node
); node
;
2956 node
= xmlNextElementSibling(node
)) {
2957 ret
= process_domain_node(node
, (const char *) name
);
2963 if (rotation_timer_interval
) {
2964 ret
= add_periodic_rotation((const char *) name
,
2965 rotation_timer_interval
);
2970 if (rotation_size
) {
2971 ret
= add_size_rotation((const char *) name
,
2979 ret
= lttng_start_tracing((const char *) name
);
2987 ERR("Failed to load session %s: %s", (const char *) name
,
2988 lttng_strerror(ret
));
2989 lttng_destroy_session((const char *) name
);
2993 free(kernel_domain
);
2997 free(python_domain
);
3004 * Return 1 if the given path is readable by the current UID or 0 if not.
3005 * Return -1 if the path is EPERM.
3007 static int validate_file_read_creds(const char *path
)
3013 /* Can we read the file. */
3014 ret
= access(path
, R_OK
);
3018 if (errno
== EACCES
) {
3029 int load_session_from_file(const char *path
, const char *session_name
,
3030 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3031 const struct config_load_session_override_attr
*overrides
)
3033 int ret
, session_found
= !session_name
;
3034 xmlDocPtr doc
= NULL
;
3035 xmlNodePtr sessions_node
;
3036 xmlNodePtr session_node
;
3039 assert(validation_ctx
);
3041 ret
= validate_file_read_creds(path
);
3044 ret
= -LTTNG_ERR_EPERM
;
3046 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3051 doc
= xmlParseFile(path
);
3053 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3057 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
3059 ERR("Session configuration file validation failed");
3060 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3064 sessions_node
= xmlDocGetRootElement(doc
);
3065 if (!sessions_node
) {
3069 for (session_node
= xmlFirstElementChild(sessions_node
);
3070 session_node
; session_node
=
3071 xmlNextElementSibling(session_node
)) {
3072 ret
= process_session_node(session_node
,
3073 session_name
, overwrite
, overrides
);
3074 if (session_name
&& ret
== 0) {
3075 /* Target session found and loaded */
3083 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
3089 int load_session_from_path(const char *path
, const char *session_name
,
3090 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3091 const struct config_load_session_override_attr
*overrides
)
3093 int ret
, session_found
= !session_name
;
3094 DIR *directory
= NULL
;
3095 struct lttng_dynamic_buffer file_path
;
3099 assert(validation_ctx
);
3100 path_len
= strlen(path
);
3101 lttng_dynamic_buffer_init(&file_path
);
3102 if (path_len
>= LTTNG_PATH_MAX
) {
3103 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
3104 path
, path_len
, LTTNG_PATH_MAX
);
3105 ret
= -LTTNG_ERR_INVALID
;
3109 directory
= opendir(path
);
3113 /* Try the file loading. */
3116 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3119 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3124 size_t file_path_root_len
;
3126 ret
= lttng_dynamic_buffer_set_capacity(&file_path
,
3129 ret
= -LTTNG_ERR_NOMEM
;
3133 ret
= lttng_dynamic_buffer_append(&file_path
, path
, path_len
);
3135 ret
= -LTTNG_ERR_NOMEM
;
3139 if (file_path
.data
[file_path
.size
- 1] != '/') {
3140 ret
= lttng_dynamic_buffer_append(&file_path
, "/", 1);
3142 ret
= -LTTNG_ERR_NOMEM
;
3146 file_path_root_len
= file_path
.size
;
3148 /* Search for *.lttng files */
3150 size_t file_name_len
;
3151 struct dirent
*result
;
3154 * When the end of the directory stream is reached, NULL
3155 * is returned and errno is kept unchanged. When an
3156 * error occurs, NULL is returned and errno is set
3157 * accordingly. To distinguish between the two, set
3158 * errno to zero before calling readdir().
3160 * On success, readdir() returns a pointer to a dirent
3161 * structure. This structure may be statically
3162 * allocated, do not attempt to free(3) it.
3165 result
= readdir(directory
);
3167 /* Reached end of dir stream or error out. */
3170 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path
);
3171 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3177 file_name_len
= strlen(result
->d_name
);
3179 if (file_name_len
<=
3180 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3184 if (file_path
.size
+ file_name_len
>= LTTNG_PATH_MAX
) {
3185 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3187 /* +1 to account for NULL terminator. */
3188 file_path
.size
+ file_name_len
+ 1,
3193 /* Does the file end with .lttng? */
3194 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3195 result
->d_name
+ file_name_len
- sizeof(
3196 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3200 ret
= lttng_dynamic_buffer_append(&file_path
, result
->d_name
,
3203 ret
= -LTTNG_ERR_NOMEM
;
3207 ret
= load_session_from_file(file_path
.data
, session_name
,
3208 validation_ctx
, overwrite
, overrides
);
3209 if (session_name
&& !ret
) {
3214 * Reset the buffer's size to the location of the
3215 * path's trailing '/'.
3217 ret
= lttng_dynamic_buffer_set_size(&file_path
,
3218 file_path_root_len
);
3220 ret
= -LTTNG_ERR_UNK
;
3226 ret
= load_session_from_file(path
, session_name
,
3227 validation_ctx
, overwrite
, overrides
);
3237 if (closedir(directory
)) {
3241 if (session_found
&& !ret
) {
3244 lttng_dynamic_buffer_reset(&file_path
);
3249 * Validate that the given path's credentials and the current process have the
3250 * same UID. If so, return 1 else return 0 if it does NOT match.
3252 static int validate_path_creds(const char *path
)
3254 int ret
, uid
= getuid();
3263 ret
= stat(path
, &buf
);
3265 if (errno
!= ENOENT
) {
3271 if (buf
.st_uid
!= uid
) {
3282 int config_load_session(const char *path
, const char *session_name
,
3283 int overwrite
, unsigned int autoload
,
3284 const struct config_load_session_override_attr
*overrides
)
3287 bool session_loaded
= false;
3288 const char *path_ptr
= NULL
;
3289 struct session_config_validation_ctx validation_ctx
= { 0 };
3291 ret
= init_session_config_validation_ctx(&validation_ctx
);
3298 const char *sys_path
;
3301 home_path
= utils_get_home_dir();
3303 char path
[PATH_MAX
];
3306 * Try user session configuration path. Ignore error here so we can
3307 * continue loading the system wide sessions.
3310 ret
= snprintf(path
, sizeof(path
),
3311 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3312 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3314 PERROR("snprintf session autoload home config path");
3319 * Credentials are only validated for the autoload in order to
3320 * avoid any user session daemon to try to load kernel sessions
3321 * automatically and failing all the times.
3323 ret
= validate_path_creds(path
);
3328 ret
= snprintf(path
, sizeof(path
),
3329 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3331 PERROR("snprintf session home config path");
3337 ret
= load_session_from_path(path_ptr
, session_name
,
3338 &validation_ctx
, overwrite
, overrides
);
3339 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3343 * Continue even if the session was found since we have to try
3344 * the system wide sessions.
3346 session_loaded
= true;
3350 /* Reset path pointer for the system wide dir. */
3353 /* Try system wide configuration directory. */
3355 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3356 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3357 ret
= validate_path_creds(sys_path
);
3359 path_ptr
= sys_path
;
3362 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3363 path_ptr
= sys_path
;
3367 ret
= load_session_from_path(path_ptr
, session_name
,
3368 &validation_ctx
, overwrite
, overrides
);
3370 session_loaded
= true;
3374 ret
= access(path
, F_OK
);
3379 ret
= -LTTNG_ERR_INVALID
;
3380 WARN("Session configuration path does not exist.");
3383 ret
= -LTTNG_ERR_EPERM
;
3386 ret
= -LTTNG_ERR_UNK
;
3392 ret
= load_session_from_path(path
, session_name
,
3393 &validation_ctx
, overwrite
, overrides
);
3396 fini_session_config_validation_ctx(&validation_ctx
);
3397 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3399 * Don't report an error if no sessions are found when called
3400 * without a session_name or a search path.
3405 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3406 /* A matching session was found in one of the search paths. */
3413 void __attribute__((destructor
)) session_config_exit(void)