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 memset(&event
, 0, sizeof(event
));
1553 /* Initialize default log level which varies by domain */
1554 switch (handle
->domain
.type
)
1556 case LTTNG_DOMAIN_JUL
:
1557 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1559 case LTTNG_DOMAIN_LOG4J
:
1560 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1562 case LTTNG_DOMAIN_PYTHON
:
1563 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1565 case LTTNG_DOMAIN_UST
:
1566 case LTTNG_DOMAIN_KERNEL
:
1567 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1573 for (node
= xmlFirstElementChild(event_node
); node
;
1574 node
= xmlNextElementSibling(node
)) {
1575 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1579 content
= xmlNodeGetContent(node
);
1581 ret
= -LTTNG_ERR_NOMEM
;
1585 ret
= lttng_strncpy(event
.name
,
1586 (const char *) content
,
1587 LTTNG_SYMBOL_NAME_LEN
);
1589 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1590 (const char *) content
,
1591 strlen((const char *) content
),
1592 LTTNG_SYMBOL_NAME_LEN
);
1593 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1598 } else if (!strcmp((const char *) node
->name
,
1599 config_element_enabled
)) {
1600 xmlChar
*content
= xmlNodeGetContent(node
);
1604 ret
= -LTTNG_ERR_NOMEM
;
1608 ret
= parse_bool(content
, &event
.enabled
);
1611 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1614 } else if (!strcmp((const char *) node
->name
,
1615 config_element_type
)) {
1616 xmlChar
*content
= xmlNodeGetContent(node
);
1620 ret
= -LTTNG_ERR_NOMEM
;
1624 ret
= get_event_type(content
);
1627 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1632 } else if (!strcmp((const char *) node
->name
,
1633 config_element_loglevel_type
)) {
1634 xmlChar
*content
= xmlNodeGetContent(node
);
1638 ret
= -LTTNG_ERR_NOMEM
;
1642 ret
= get_loglevel_type(content
);
1645 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1649 event
.loglevel_type
= ret
;
1650 } else if (!strcmp((const char *) node
->name
,
1651 config_element_loglevel
)) {
1653 int64_t loglevel
= 0;
1656 content
= xmlNodeGetContent(node
);
1658 ret
= -LTTNG_ERR_NOMEM
;
1662 ret
= parse_int(content
, &loglevel
);
1665 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1669 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1670 WARN("loglevel out of range.");
1671 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1675 event
.loglevel
= loglevel
;
1676 } else if (!strcmp((const char *) node
->name
,
1677 config_element_filter
)) {
1679 xmlNodeGetContent(node
);
1683 ret
= -LTTNG_ERR_NOMEM
;
1687 free(filter_expression
);
1688 filter_expression
= strdup((char *) content
);
1690 if (!filter_expression
) {
1691 ret
= -LTTNG_ERR_NOMEM
;
1694 } else if (!strcmp((const char *) node
->name
,
1695 config_element_exclusions
)) {
1696 xmlNodePtr exclusion_node
;
1697 int exclusion_index
= 0;
1702 * Exclusions has already been initialized,
1705 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1709 exclusion_count
= xmlChildElementCount(node
);
1710 if (!exclusion_count
) {
1714 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1716 exclusion_count
= 0;
1717 ret
= -LTTNG_ERR_NOMEM
;
1721 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1722 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1724 xmlNodeGetContent(exclusion_node
);
1727 ret
= -LTTNG_ERR_NOMEM
;
1731 exclusions
[exclusion_index
] = strdup((const char *) content
);
1733 if (!exclusions
[exclusion_index
]) {
1734 ret
= -LTTNG_ERR_NOMEM
;
1740 event
.exclusion
= 1;
1741 } else if (!strcmp((const char *) node
->name
,
1742 config_element_attributes
)) {
1743 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1746 if (!attribute_node
) {
1747 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1751 if (!strcmp((const char *) node
->name
,
1752 config_element_probe_attributes
)) {
1753 xmlNodePtr probe_attribute_node
;
1755 /* probe_attributes */
1756 for (probe_attribute_node
=
1757 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1758 probe_attribute_node
= xmlNextElementSibling(
1759 probe_attribute_node
)) {
1761 ret
= process_probe_attribute_node(probe_attribute_node
,
1770 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1772 /* function_attributes */
1773 content
= xmlNodeGetContent(symbol_node
);
1775 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1779 sym_len
= strlen((char *) content
);
1780 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1781 WARN("Function name too long.");
1782 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1787 ret
= lttng_strncpy(
1788 event
.attr
.ftrace
.symbol_name
,
1789 (char *) content
, sym_len
);
1791 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1800 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1801 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1802 filter_expression
, exclusion_count
, exclusions
);
1804 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1805 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1811 for (i
= 0; i
< exclusion_count
; i
++) {
1812 free(exclusions
[i
]);
1816 free(filter_expression
);
1821 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1822 const char *channel_name
)
1825 struct lttng_event event
;
1828 assert(events_node
);
1830 assert(channel_name
);
1832 for (node
= xmlFirstElementChild(events_node
); node
;
1833 node
= xmlNextElementSibling(node
)) {
1834 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1841 * Disable all events to enable only the necessary events.
1842 * Limitations regarding lttng_disable_events and tuple descriptor
1843 * force this approach.
1845 memset(&event
, 0, sizeof(event
));
1846 event
.loglevel
= -1;
1847 event
.type
= LTTNG_EVENT_ALL
;
1848 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1853 for (node
= xmlFirstElementChild(events_node
); node
;
1854 node
= xmlNextElementSibling(node
)) {
1855 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1866 int process_channel_attr_node(xmlNodePtr attr_node
,
1867 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1868 xmlNodePtr
*events_node
)
1874 assert(contexts_node
);
1875 assert(events_node
);
1877 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1881 content
= xmlNodeGetContent(attr_node
);
1883 ret
= -LTTNG_ERR_NOMEM
;
1887 ret
= lttng_strncpy(channel
->name
,
1888 (const char *) content
,
1889 LTTNG_SYMBOL_NAME_LEN
);
1891 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1892 (const char *) content
,
1893 strlen((const char *) content
),
1894 LTTNG_SYMBOL_NAME_LEN
);
1895 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1900 } else if (!strcmp((const char *) attr_node
->name
,
1901 config_element_enabled
)) {
1906 content
= xmlNodeGetContent(attr_node
);
1908 ret
= -LTTNG_ERR_NOMEM
;
1912 ret
= parse_bool(content
, &enabled
);
1915 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1919 channel
->enabled
= enabled
;
1920 } else if (!strcmp((const char *) attr_node
->name
,
1921 config_element_overwrite_mode
)) {
1924 /* overwrite_mode */
1925 content
= xmlNodeGetContent(attr_node
);
1927 ret
= -LTTNG_ERR_NOMEM
;
1931 ret
= get_overwrite_mode(content
);
1934 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1938 channel
->attr
.overwrite
= ret
;
1939 } else if (!strcmp((const char *) attr_node
->name
,
1940 config_element_subbuf_size
)) {
1943 /* subbuffer_size */
1944 content
= xmlNodeGetContent(attr_node
);
1946 ret
= -LTTNG_ERR_NOMEM
;
1950 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1953 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1956 } else if (!strcmp((const char *) attr_node
->name
,
1957 config_element_num_subbuf
)) {
1960 /* subbuffer_count */
1961 content
= xmlNodeGetContent(attr_node
);
1963 ret
= -LTTNG_ERR_NOMEM
;
1967 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1970 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1973 } else if (!strcmp((const char *) attr_node
->name
,
1974 config_element_switch_timer_interval
)) {
1976 uint64_t switch_timer_interval
= 0;
1978 /* switch_timer_interval */
1979 content
= xmlNodeGetContent(attr_node
);
1981 ret
= -LTTNG_ERR_NOMEM
;
1985 ret
= parse_uint(content
, &switch_timer_interval
);
1988 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1992 if (switch_timer_interval
> UINT_MAX
) {
1993 WARN("switch_timer_interval out of range.");
1994 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1998 channel
->attr
.switch_timer_interval
=
1999 switch_timer_interval
;
2000 } else if (!strcmp((const char *) attr_node
->name
,
2001 config_element_read_timer_interval
)) {
2003 uint64_t read_timer_interval
= 0;
2005 /* read_timer_interval */
2006 content
= xmlNodeGetContent(attr_node
);
2008 ret
= -LTTNG_ERR_NOMEM
;
2012 ret
= parse_uint(content
, &read_timer_interval
);
2015 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2019 if (read_timer_interval
> UINT_MAX
) {
2020 WARN("read_timer_interval out of range.");
2021 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2025 channel
->attr
.read_timer_interval
=
2026 read_timer_interval
;
2027 } else if (!strcmp((const char *) attr_node
->name
,
2028 config_element_output_type
)) {
2032 content
= xmlNodeGetContent(attr_node
);
2034 ret
= -LTTNG_ERR_NOMEM
;
2038 ret
= get_output_type(content
);
2041 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2045 channel
->attr
.output
= ret
;
2046 } else if (!strcmp((const char *) attr_node
->name
,
2047 config_element_tracefile_size
)) {
2050 /* tracefile_size */
2051 content
= xmlNodeGetContent(attr_node
);
2053 ret
= -LTTNG_ERR_NOMEM
;
2057 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2060 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2063 } else if (!strcmp((const char *) attr_node
->name
,
2064 config_element_tracefile_count
)) {
2067 /* tracefile_count */
2068 content
= xmlNodeGetContent(attr_node
);
2070 ret
= -LTTNG_ERR_NOMEM
;
2074 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2077 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2080 } else if (!strcmp((const char *) attr_node
->name
,
2081 config_element_live_timer_interval
)) {
2083 uint64_t live_timer_interval
= 0;
2085 /* live_timer_interval */
2086 content
= xmlNodeGetContent(attr_node
);
2088 ret
= -LTTNG_ERR_NOMEM
;
2092 ret
= parse_uint(content
, &live_timer_interval
);
2095 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2099 if (live_timer_interval
> UINT_MAX
) {
2100 WARN("live_timer_interval out of range.");
2101 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2105 channel
->attr
.live_timer_interval
=
2106 live_timer_interval
;
2107 } else if (!strcmp((const char *) attr_node
->name
,
2108 config_element_monitor_timer_interval
)) {
2110 uint64_t monitor_timer_interval
= 0;
2112 /* monitor_timer_interval */
2113 content
= xmlNodeGetContent(attr_node
);
2115 ret
= -LTTNG_ERR_NOMEM
;
2119 ret
= parse_uint(content
, &monitor_timer_interval
);
2122 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2126 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2127 monitor_timer_interval
);
2129 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2132 } else if (!strcmp((const char *) attr_node
->name
,
2133 config_element_blocking_timeout
)) {
2135 int64_t blocking_timeout
= 0;
2137 /* blocking_timeout */
2138 content
= xmlNodeGetContent(attr_node
);
2140 ret
= -LTTNG_ERR_NOMEM
;
2144 ret
= parse_int(content
, &blocking_timeout
);
2147 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2151 ret
= lttng_channel_set_blocking_timeout(channel
,
2154 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2157 } else if (!strcmp((const char *) attr_node
->name
,
2158 config_element_events
)) {
2160 *events_node
= attr_node
;
2163 *contexts_node
= attr_node
;
2171 int process_context_node(xmlNodePtr context_node
,
2172 struct lttng_handle
*handle
, const char *channel_name
)
2175 struct lttng_event_context context
;
2176 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2179 assert(channel_name
);
2181 if (!context_child_node
) {
2182 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2186 memset(&context
, 0, sizeof(context
));
2188 if (!strcmp((const char *) context_child_node
->name
,
2189 config_element_type
)) {
2191 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2194 ret
= -LTTNG_ERR_NOMEM
;
2198 ret
= get_context_type(content
);
2201 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2206 } else if (!strcmp((const char *) context_child_node
->name
,
2207 config_element_context_perf
)) {
2209 xmlNodePtr perf_attr_node
;
2211 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2212 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2213 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2214 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2215 perf_attr_node
; perf_attr_node
=
2216 xmlNextElementSibling(perf_attr_node
)) {
2217 if (!strcmp((const char *) perf_attr_node
->name
,
2218 config_element_type
)) {
2223 content
= xmlNodeGetContent(perf_attr_node
);
2225 ret
= -LTTNG_ERR_NOMEM
;
2229 ret
= parse_uint(content
, &type
);
2232 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2236 if (type
> UINT32_MAX
) {
2237 WARN("perf context type out of range.");
2238 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2242 context
.u
.perf_counter
.type
= type
;
2243 } else if (!strcmp((const char *) perf_attr_node
->name
,
2244 config_element_config
)) {
2246 uint64_t config
= 0;
2249 content
= xmlNodeGetContent(perf_attr_node
);
2251 ret
= -LTTNG_ERR_NOMEM
;
2255 ret
= parse_uint(content
, &config
);
2258 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2262 context
.u
.perf_counter
.config
= config
;
2263 } else if (!strcmp((const char *) perf_attr_node
->name
,
2264 config_element_name
)) {
2268 content
= xmlNodeGetContent(perf_attr_node
);
2270 ret
= -LTTNG_ERR_NOMEM
;
2274 ret
= lttng_strncpy(context
.u
.perf_counter
.name
,
2275 (const char *) content
,
2276 LTTNG_SYMBOL_NAME_LEN
);
2278 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2279 (const char *) content
,
2280 strlen((const char *) content
),
2281 LTTNG_SYMBOL_NAME_LEN
);
2282 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2289 } else if (!strcmp((const char *) context_child_node
->name
,
2290 config_element_context_app
)) {
2291 /* application context */
2292 xmlNodePtr app_ctx_node
;
2294 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2295 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2296 app_ctx_node
; app_ctx_node
=
2297 xmlNextElementSibling(app_ctx_node
)) {
2299 char **target
= strcmp(
2300 (const char *) app_ctx_node
->name
,
2301 config_element_context_app_provider_name
) == 0 ?
2302 &context
.u
.app_ctx
.provider_name
:
2303 &context
.u
.app_ctx
.ctx_name
;
2305 content
= xmlNodeGetContent(app_ctx_node
);
2307 ret
= -LTTNG_ERR_NOMEM
;
2311 *target
= (char *) content
;
2314 /* Unrecognized context type */
2315 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2319 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2320 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2321 free(context
.u
.app_ctx
.provider_name
);
2322 free(context
.u
.app_ctx
.ctx_name
);
2329 int process_contexts_node(xmlNodePtr contexts_node
,
2330 struct lttng_handle
*handle
, const char *channel_name
)
2333 xmlNodePtr context_node
;
2335 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2336 context_node
= xmlNextElementSibling(context_node
)) {
2337 ret
= process_context_node(context_node
, handle
, channel_name
);
2347 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2348 struct lttng_handle
*handle
)
2351 xmlNodePtr targets_node
= NULL
;
2355 assert(pid_tracker_node
);
2356 /* get the targets node */
2357 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2358 node
= xmlNextElementSibling(node
)) {
2359 if (!strcmp((const char *) node
->name
,
2360 config_element_targets
)) {
2361 targets_node
= node
;
2366 if (!targets_node
) {
2367 ret
= LTTNG_ERR_INVALID
;
2371 /* Go through all pid_target node */
2372 child
= xmlChildElementCount(targets_node
);
2374 /* The session is explicitly set to target nothing. */
2375 ret
= lttng_untrack_pid(handle
, -1);
2380 for (node
= xmlFirstElementChild(targets_node
); node
;
2381 node
= xmlNextElementSibling(node
)) {
2382 xmlNodePtr pid_target_node
= node
;
2384 /* get pid node and track it */
2385 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2386 node
= xmlNextElementSibling(node
)) {
2387 if (!strcmp((const char *) node
->name
,
2388 config_element_pid
)) {
2390 xmlChar
*content
= NULL
;
2392 content
= xmlNodeGetContent(node
);
2394 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2398 ret
= parse_int(content
, &pid
);
2401 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2405 ret
= lttng_track_pid(handle
, (int) pid
);
2411 node
= pid_target_node
;
2419 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2422 struct lttng_domain domain
= { 0 };
2423 struct lttng_handle
*handle
= NULL
;
2424 struct lttng_channel
*channel
= NULL
;
2425 xmlNodePtr channels_node
= NULL
;
2426 xmlNodePtr trackers_node
= NULL
;
2427 xmlNodePtr pid_tracker_node
= NULL
;
2430 assert(session_name
);
2432 ret
= init_domain(domain_node
, &domain
);
2437 handle
= lttng_create_handle(session_name
, &domain
);
2439 ret
= -LTTNG_ERR_NOMEM
;
2443 /* get the channels node */
2444 for (node
= xmlFirstElementChild(domain_node
); node
;
2445 node
= xmlNextElementSibling(node
)) {
2446 if (!strcmp((const char *) node
->name
,
2447 config_element_channels
)) {
2448 channels_node
= node
;
2453 if (!channels_node
) {
2457 /* create all channels */
2458 for (node
= xmlFirstElementChild(channels_node
); node
;
2459 node
= xmlNextElementSibling(node
)) {
2460 const enum lttng_domain_type original_domain
= domain
.type
;
2461 xmlNodePtr contexts_node
= NULL
;
2462 xmlNodePtr events_node
= NULL
;
2463 xmlNodePtr channel_attr_node
;
2466 * Channels of the "agent" types cannot be created directly.
2467 * They are meant to be created implicitly through the
2468 * activation of events in their domain. However, a user
2469 * can override the default channel configuration attributes
2470 * by creating the underlying UST channel _before_ enabling
2471 * an agent domain event.
2473 * Hence, the channel's type is substituted before the creation
2474 * and restored by the time the events are created.
2476 switch (domain
.type
) {
2477 case LTTNG_DOMAIN_JUL
:
2478 case LTTNG_DOMAIN_LOG4J
:
2479 case LTTNG_DOMAIN_PYTHON
:
2480 domain
.type
= LTTNG_DOMAIN_UST
;
2485 channel
= lttng_channel_create(&domain
);
2491 for (channel_attr_node
= xmlFirstElementChild(node
);
2492 channel_attr_node
; channel_attr_node
=
2493 xmlNextElementSibling(channel_attr_node
)) {
2494 ret
= process_channel_attr_node(channel_attr_node
,
2495 channel
, &contexts_node
, &events_node
);
2501 ret
= lttng_enable_channel(handle
, channel
);
2506 /* Restore the original channel domain. */
2507 domain
.type
= original_domain
;
2509 ret
= process_events_node(events_node
, handle
, channel
->name
);
2514 ret
= process_contexts_node(contexts_node
, handle
,
2520 lttng_channel_destroy(channel
);
2524 /* get the trackers node */
2525 for (node
= xmlFirstElementChild(domain_node
); node
;
2526 node
= xmlNextElementSibling(node
)) {
2527 if (!strcmp((const char *) node
->name
,
2528 config_element_trackers
)) {
2529 trackers_node
= node
;
2534 if (!trackers_node
) {
2538 for (node
= xmlFirstElementChild(trackers_node
); node
;
2539 node
= xmlNextElementSibling(node
)) {
2540 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2541 pid_tracker_node
= node
;
2542 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2549 if (!pid_tracker_node
) {
2550 lttng_track_pid(handle
, -1);
2554 lttng_channel_destroy(channel
);
2555 lttng_destroy_handle(handle
);
2560 int add_periodic_rotation(const char *name
, uint64_t time_us
)
2563 enum lttng_rotation_status status
;
2564 struct lttng_rotation_schedule
*periodic
=
2565 lttng_rotation_schedule_periodic_create();
2568 ret
= -LTTNG_ERR_NOMEM
;
2572 status
= lttng_rotation_schedule_periodic_set_period(periodic
,
2574 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2575 ret
= -LTTNG_ERR_INVALID
;
2579 status
= lttng_session_add_rotation_schedule(name
, periodic
);
2581 case LTTNG_ROTATION_STATUS_OK
:
2584 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2585 case LTTNG_ROTATION_STATUS_INVALID
:
2586 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2589 ret
= -LTTNG_ERR_UNK
;
2593 lttng_rotation_schedule_destroy(periodic
);
2598 int add_size_rotation(const char *name
, uint64_t size_bytes
)
2601 enum lttng_rotation_status status
;
2602 struct lttng_rotation_schedule
*size
=
2603 lttng_rotation_schedule_size_threshold_create();
2606 ret
= -LTTNG_ERR_NOMEM
;
2610 status
= lttng_rotation_schedule_size_threshold_set_threshold(size
,
2612 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
2613 ret
= -LTTNG_ERR_INVALID
;
2617 status
= lttng_session_add_rotation_schedule(name
, size
);
2619 case LTTNG_ROTATION_STATUS_OK
:
2622 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
2623 case LTTNG_ROTATION_STATUS_INVALID
:
2624 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2627 ret
= -LTTNG_ERR_UNK
;
2631 lttng_rotation_schedule_destroy(size
);
2636 int process_session_rotation_schedules_node(
2637 xmlNodePtr schedules_node
,
2638 uint64_t *rotation_timer_interval
,
2639 uint64_t *rotation_size
)
2644 for (child
= xmlFirstElementChild(schedules_node
);
2646 child
= xmlNextElementSibling(child
)) {
2647 if (!strcmp((const char *) child
->name
,
2648 config_element_rotation_schedule_periodic
)) {
2650 xmlNodePtr time_us_node
;
2652 /* periodic rotation schedule */
2653 time_us_node
= xmlFirstElementChild(child
);
2654 if (!time_us_node
||
2655 strcmp((const char *) time_us_node
->name
,
2656 config_element_rotation_schedule_periodic_time_us
)) {
2657 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2662 content
= xmlNodeGetContent(time_us_node
);
2664 ret
= -LTTNG_ERR_NOMEM
;
2667 ret
= parse_uint(content
, rotation_timer_interval
);
2670 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2673 } else if (!strcmp((const char *) child
->name
,
2674 config_element_rotation_schedule_size_threshold
)) {
2676 xmlNodePtr bytes_node
;
2678 /* size_threshold rotation schedule */
2679 bytes_node
= xmlFirstElementChild(child
);
2681 strcmp((const char *) bytes_node
->name
,
2682 config_element_rotation_schedule_size_threshold_bytes
)) {
2683 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2688 content
= xmlNodeGetContent(bytes_node
);
2690 ret
= -LTTNG_ERR_NOMEM
;
2693 ret
= parse_uint(content
, rotation_size
);
2696 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2707 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2709 const struct config_load_session_override_attr
*overrides
)
2711 int ret
, started
= -1, snapshot_mode
= -1;
2712 uint64_t live_timer_interval
= UINT64_MAX
,
2713 rotation_timer_interval
= 0,
2715 xmlChar
*name
= NULL
;
2716 xmlChar
*shm_path
= NULL
;
2717 xmlNodePtr domains_node
= NULL
;
2718 xmlNodePtr output_node
= NULL
;
2720 xmlNodePtr attributes_child
;
2721 struct lttng_domain
*kernel_domain
= NULL
;
2722 struct lttng_domain
*ust_domain
= NULL
;
2723 struct lttng_domain
*jul_domain
= NULL
;
2724 struct lttng_domain
*log4j_domain
= NULL
;
2725 struct lttng_domain
*python_domain
= NULL
;
2727 for (node
= xmlFirstElementChild(session_node
); node
;
2728 node
= xmlNextElementSibling(node
)) {
2729 if (!name
&& !strcmp((const char *) node
->name
,
2730 config_element_name
)) {
2732 xmlChar
*node_content
= xmlNodeGetContent(node
);
2733 if (!node_content
) {
2734 ret
= -LTTNG_ERR_NOMEM
;
2738 name
= node_content
;
2739 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2740 config_element_domains
)) {
2742 domains_node
= node
;
2743 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2744 config_element_started
)) {
2746 xmlChar
*node_content
= xmlNodeGetContent(node
);
2747 if (!node_content
) {
2748 ret
= -LTTNG_ERR_NOMEM
;
2752 ret
= parse_bool(node_content
, &started
);
2755 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2758 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2759 config_element_output
)) {
2762 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2763 config_element_shared_memory_path
)) {
2764 /* shared memory path */
2765 xmlChar
*node_content
= xmlNodeGetContent(node
);
2766 if (!node_content
) {
2767 ret
= -LTTNG_ERR_NOMEM
;
2771 shm_path
= node_content
;
2774 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2775 * rotation_timer_interval.
2777 for (attributes_child
= xmlFirstElementChild(node
); attributes_child
;
2778 attributes_child
= xmlNextElementSibling(attributes_child
)) {
2779 if (!strcmp((const char *) attributes_child
->name
,
2780 config_element_snapshot_mode
)) {
2782 xmlChar
*snapshot_mode_content
=
2783 xmlNodeGetContent(attributes_child
);
2784 if (!snapshot_mode_content
) {
2785 ret
= -LTTNG_ERR_NOMEM
;
2789 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2790 free(snapshot_mode_content
);
2792 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2795 } else if (!strcmp((const char *) attributes_child
->name
,
2796 config_element_live_timer_interval
)) {
2797 /* live_timer_interval */
2798 xmlChar
*timer_interval_content
=
2799 xmlNodeGetContent(attributes_child
);
2800 if (!timer_interval_content
) {
2801 ret
= -LTTNG_ERR_NOMEM
;
2805 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2806 free(timer_interval_content
);
2808 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2811 } else if (!strcmp((const char *) attributes_child
->name
,
2812 config_element_rotation_schedules
)) {
2813 ret
= process_session_rotation_schedules_node(
2815 &rotation_timer_interval
,
2818 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2828 /* Mandatory attribute, as defined in the session XSD */
2829 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2833 if (session_name
&& strcmp((char *) name
, session_name
)) {
2834 /* This is not the session we are looking for */
2835 ret
= -LTTNG_ERR_NO_SESSION
;
2839 /* Init domains to create the session handles */
2840 for (node
= xmlFirstElementChild(domains_node
); node
;
2841 node
= xmlNextElementSibling(node
)) {
2842 struct lttng_domain
*domain
;
2844 domain
= zmalloc(sizeof(*domain
));
2846 ret
= -LTTNG_ERR_NOMEM
;
2850 ret
= init_domain(node
, domain
);
2852 goto domain_init_error
;
2855 switch (domain
->type
) {
2856 case LTTNG_DOMAIN_KERNEL
:
2857 if (kernel_domain
) {
2858 /* Same domain seen twice, invalid! */
2859 goto domain_init_error
;
2861 kernel_domain
= domain
;
2863 case LTTNG_DOMAIN_UST
:
2865 /* Same domain seen twice, invalid! */
2866 goto domain_init_error
;
2868 ust_domain
= domain
;
2870 case LTTNG_DOMAIN_JUL
:
2872 /* Same domain seen twice, invalid! */
2873 goto domain_init_error
;
2875 jul_domain
= domain
;
2877 case LTTNG_DOMAIN_LOG4J
:
2879 /* Same domain seen twice, invalid! */
2880 goto domain_init_error
;
2882 log4j_domain
= domain
;
2884 case LTTNG_DOMAIN_PYTHON
:
2885 if (python_domain
) {
2886 /* Same domain seen twice, invalid! */
2887 goto domain_init_error
;
2889 python_domain
= domain
;
2892 WARN("Invalid domain type");
2893 goto domain_init_error
;
2898 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2902 /* Apply overrides */
2904 if (overrides
->session_name
) {
2905 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2906 if (!name_override
) {
2907 ret
= -LTTNG_ERR_NOMEM
;
2911 /* Overrides the session name to the provided name */
2913 name
= name_override
;
2918 /* Destroy session if it exists */
2919 ret
= lttng_destroy_session((const char *) name
);
2920 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2921 ERR("Failed to destroy existing session.");
2926 /* Create session type depending on output type */
2927 if (snapshot_mode
&& snapshot_mode
!= -1) {
2928 ret
= create_snapshot_session((const char *) name
, output_node
,
2930 } else if (live_timer_interval
&&
2931 live_timer_interval
!= UINT64_MAX
) {
2932 ret
= create_session((const char *) name
,
2933 output_node
, live_timer_interval
, overrides
);
2935 /* regular session */
2936 ret
= create_session((const char *) name
,
2937 output_node
, UINT64_MAX
, overrides
);
2944 ret
= lttng_set_session_shm_path((const char *) name
,
2945 (const char *) shm_path
);
2951 for (node
= xmlFirstElementChild(domains_node
); node
;
2952 node
= xmlNextElementSibling(node
)) {
2953 ret
= process_domain_node(node
, (const char *) name
);
2959 if (rotation_timer_interval
) {
2960 ret
= add_periodic_rotation((const char *) name
,
2961 rotation_timer_interval
);
2966 if (rotation_size
) {
2967 ret
= add_size_rotation((const char *) name
,
2975 ret
= lttng_start_tracing((const char *) name
);
2983 ERR("Failed to load session %s: %s", (const char *) name
,
2984 lttng_strerror(ret
));
2985 lttng_destroy_session((const char *) name
);
2989 free(kernel_domain
);
2993 free(python_domain
);
3000 * Return 1 if the given path is readable by the current UID or 0 if not.
3001 * Return -1 if the path is EPERM.
3003 static int validate_file_read_creds(const char *path
)
3009 /* Can we read the file. */
3010 ret
= access(path
, R_OK
);
3014 if (errno
== EACCES
) {
3025 int load_session_from_file(const char *path
, const char *session_name
,
3026 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3027 const struct config_load_session_override_attr
*overrides
)
3029 int ret
, session_found
= !session_name
;
3030 xmlDocPtr doc
= NULL
;
3031 xmlNodePtr sessions_node
;
3032 xmlNodePtr session_node
;
3035 assert(validation_ctx
);
3037 ret
= validate_file_read_creds(path
);
3040 ret
= -LTTNG_ERR_EPERM
;
3042 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3047 doc
= xmlParseFile(path
);
3049 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3053 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
3055 ERR("Session configuration file validation failed");
3056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
3060 sessions_node
= xmlDocGetRootElement(doc
);
3061 if (!sessions_node
) {
3065 for (session_node
= xmlFirstElementChild(sessions_node
);
3066 session_node
; session_node
=
3067 xmlNextElementSibling(session_node
)) {
3068 ret
= process_session_node(session_node
,
3069 session_name
, overwrite
, overrides
);
3070 if (session_name
&& ret
== 0) {
3071 /* Target session found and loaded */
3079 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
3085 int load_session_from_path(const char *path
, const char *session_name
,
3086 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
3087 const struct config_load_session_override_attr
*overrides
)
3089 int ret
, session_found
= !session_name
;
3090 DIR *directory
= NULL
;
3091 struct lttng_dynamic_buffer file_path
;
3095 assert(validation_ctx
);
3096 path_len
= strlen(path
);
3097 lttng_dynamic_buffer_init(&file_path
);
3098 if (path_len
>= LTTNG_PATH_MAX
) {
3099 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
3100 path
, path_len
, LTTNG_PATH_MAX
);
3101 ret
= -LTTNG_ERR_INVALID
;
3105 directory
= opendir(path
);
3109 /* Try the file loading. */
3112 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3115 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3120 size_t file_path_root_len
;
3122 ret
= lttng_dynamic_buffer_set_capacity(&file_path
,
3125 ret
= -LTTNG_ERR_NOMEM
;
3129 ret
= lttng_dynamic_buffer_append(&file_path
, path
, path_len
);
3131 ret
= -LTTNG_ERR_NOMEM
;
3135 if (file_path
.data
[file_path
.size
- 1] != '/') {
3136 ret
= lttng_dynamic_buffer_append(&file_path
, "/", 1);
3138 ret
= -LTTNG_ERR_NOMEM
;
3142 file_path_root_len
= file_path
.size
;
3144 /* Search for *.lttng files */
3146 size_t file_name_len
;
3147 struct dirent
*result
;
3150 * When the end of the directory stream is reached, NULL
3151 * is returned and errno is kept unchanged. When an
3152 * error occurs, NULL is returned and errno is set
3153 * accordingly. To distinguish between the two, set
3154 * errno to zero before calling readdir().
3156 * On success, readdir() returns a pointer to a dirent
3157 * structure. This structure may be statically
3158 * allocated, do not attempt to free(3) it.
3161 result
= readdir(directory
);
3163 /* Reached end of dir stream or error out. */
3166 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path
);
3167 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3173 file_name_len
= strlen(result
->d_name
);
3175 if (file_name_len
<=
3176 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3180 if (file_path
.size
+ file_name_len
>= LTTNG_PATH_MAX
) {
3181 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3183 /* +1 to account for NULL terminator. */
3184 file_path
.size
+ file_name_len
+ 1,
3189 /* Does the file end with .lttng? */
3190 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3191 result
->d_name
+ file_name_len
- sizeof(
3192 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3196 ret
= lttng_dynamic_buffer_append(&file_path
, result
->d_name
,
3199 ret
= -LTTNG_ERR_NOMEM
;
3203 ret
= load_session_from_file(file_path
.data
, session_name
,
3204 validation_ctx
, overwrite
, overrides
);
3205 if (session_name
&& !ret
) {
3210 * Reset the buffer's size to the location of the
3211 * path's trailing '/'.
3213 ret
= lttng_dynamic_buffer_set_size(&file_path
,
3214 file_path_root_len
);
3216 ret
= -LTTNG_ERR_UNK
;
3222 ret
= load_session_from_file(path
, session_name
,
3223 validation_ctx
, overwrite
, overrides
);
3233 if (closedir(directory
)) {
3237 if (session_found
&& !ret
) {
3240 lttng_dynamic_buffer_reset(&file_path
);
3245 * Validate that the given path's credentials and the current process have the
3246 * same UID. If so, return 1 else return 0 if it does NOT match.
3248 static int validate_path_creds(const char *path
)
3250 int ret
, uid
= getuid();
3259 ret
= stat(path
, &buf
);
3261 if (errno
!= ENOENT
) {
3267 if (buf
.st_uid
!= uid
) {
3278 int config_load_session(const char *path
, const char *session_name
,
3279 int overwrite
, unsigned int autoload
,
3280 const struct config_load_session_override_attr
*overrides
)
3283 bool session_loaded
= false;
3284 const char *path_ptr
= NULL
;
3285 struct session_config_validation_ctx validation_ctx
= { 0 };
3287 ret
= init_session_config_validation_ctx(&validation_ctx
);
3294 const char *sys_path
;
3297 home_path
= utils_get_home_dir();
3299 char path
[PATH_MAX
];
3302 * Try user session configuration path. Ignore error here so we can
3303 * continue loading the system wide sessions.
3306 ret
= snprintf(path
, sizeof(path
),
3307 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3308 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3310 PERROR("snprintf session autoload home config path");
3315 * Credentials are only validated for the autoload in order to
3316 * avoid any user session daemon to try to load kernel sessions
3317 * automatically and failing all the times.
3319 ret
= validate_path_creds(path
);
3324 ret
= snprintf(path
, sizeof(path
),
3325 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3327 PERROR("snprintf session home config path");
3333 ret
= load_session_from_path(path_ptr
, session_name
,
3334 &validation_ctx
, overwrite
, overrides
);
3335 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3339 * Continue even if the session was found since we have to try
3340 * the system wide sessions.
3342 session_loaded
= true;
3346 /* Reset path pointer for the system wide dir. */
3349 /* Try system wide configuration directory. */
3351 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3352 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3353 ret
= validate_path_creds(sys_path
);
3355 path_ptr
= sys_path
;
3358 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3359 path_ptr
= sys_path
;
3363 ret
= load_session_from_path(path_ptr
, session_name
,
3364 &validation_ctx
, overwrite
, overrides
);
3366 session_loaded
= true;
3370 ret
= access(path
, F_OK
);
3375 ret
= -LTTNG_ERR_INVALID
;
3376 WARN("Session configuration path does not exist.");
3379 ret
= -LTTNG_ERR_EPERM
;
3382 ret
= -LTTNG_ERR_UNK
;
3388 ret
= load_session_from_path(path
, session_name
,
3389 &validation_ctx
, overwrite
, overrides
);
3392 fini_session_config_validation_ctx(&validation_ctx
);
3393 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3395 * Don't report an error if no sessions are found when called
3396 * without a session_name or a search path.
3401 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3402 /* A matching session was found in one of the search paths. */
3409 void __attribute__((destructor
)) session_config_exit(void)