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/compat/getenv.h>
36 #include <lttng/lttng-error.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlschemas.h>
40 #include <libxml/tree.h>
41 #include <lttng/lttng.h>
42 #include <lttng/snapshot.h>
44 #include "session-config.h"
45 #include "config-internal.h"
47 struct handler_filter_args
{
49 config_entry_handler_cb handler
;
53 struct session_config_validation_ctx
{
54 xmlSchemaParserCtxtPtr parser_ctx
;
56 xmlSchemaValidCtxtPtr schema_validation_ctx
;
59 const char * const config_str_yes
= "yes";
60 const char * const config_str_true
= "true";
61 const char * const config_str_on
= "on";
62 const char * const config_str_no
= "no";
63 const char * const config_str_false
= "false";
64 const char * const config_str_off
= "off";
65 const char * const config_xml_encoding
= "UTF-8";
66 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
67 const char * const config_xml_indent_string
= "\t";
68 const char * const config_xml_true
= "true";
69 const char * const config_xml_false
= "false";
71 const char * const config_element_channel
= "channel";
72 const char * const config_element_channels
= "channels";
73 const char * const config_element_domain
= "domain";
74 const char * const config_element_domains
= "domains";
75 const char * const config_element_event
= "event";
76 const char * const config_element_events
= "events";
77 const char * const config_element_context
= "context";
78 const char * const config_element_contexts
= "contexts";
79 const char * const config_element_attributes
= "attributes";
80 const char * const config_element_exclusion
= "exclusion";
81 const char * const config_element_exclusions
= "exclusions";
82 const char * const config_element_function_attributes
= "function_attributes";
83 const char * const config_element_probe_attributes
= "probe_attributes";
84 const char * const config_element_symbol_name
= "symbol_name";
85 const char * const config_element_address
= "address";
86 const char * const config_element_offset
= "offset";
87 const char * const config_element_name
= "name";
88 const char * const config_element_enabled
= "enabled";
89 const char * const config_element_overwrite_mode
= "overwrite_mode";
90 const char * const config_element_subbuf_size
= "subbuffer_size";
91 const char * const config_element_num_subbuf
= "subbuffer_count";
92 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
93 const char * const config_element_read_timer_interval
= "read_timer_interval";
94 const char * const config_element_output
= "output";
95 const char * const config_element_output_type
= "output_type";
96 const char * const config_element_tracefile_size
= "tracefile_size";
97 const char * const config_element_tracefile_count
= "tracefile_count";
98 const char * const config_element_live_timer_interval
= "live_timer_interval";
99 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
100 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
101 const char * const config_element_type
= "type";
102 const char * const config_element_buffer_type
= "buffer_type";
103 const char * const config_element_session
= "session";
104 const char * const config_element_sessions
= "sessions";
105 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
106 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
107 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
108 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
109 const char * const config_element_config
= "config";
110 const char * const config_element_started
= "started";
111 const char * const config_element_snapshot_mode
= "snapshot_mode";
112 const char * const config_element_loglevel
= "loglevel";
113 const char * const config_element_loglevel_type
= "loglevel_type";
114 const char * const config_element_filter
= "filter";
115 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
116 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
117 const char * const config_element_consumer_output
= "consumer_output";
118 const char * const config_element_destination
= "destination";
119 const char * const config_element_path
= "path";
120 const char * const config_element_net_output
= "net_output";
121 const char * const config_element_control_uri
= "control_uri";
122 const char * const config_element_data_uri
= "data_uri";
123 const char * const config_element_max_size
= "max_size";
124 const char * const config_element_pid
= "pid";
125 const char * const config_element_pids
= "pids";
126 const char * const config_element_shared_memory_path
= "shared_memory_path";
127 const char * const config_element_pid_tracker
= "pid_tracker";
128 const char * const config_element_trackers
= "trackers";
129 const char * const config_element_targets
= "targets";
130 const char * const config_element_target_pid
= "pid_target";
132 const char * const config_domain_type_kernel
= "KERNEL";
133 const char * const config_domain_type_ust
= "UST";
134 const char * const config_domain_type_jul
= "JUL";
135 const char * const config_domain_type_log4j
= "LOG4J";
136 const char * const config_domain_type_python
= "PYTHON";
138 const char * const config_buffer_type_per_pid
= "PER_PID";
139 const char * const config_buffer_type_per_uid
= "PER_UID";
140 const char * const config_buffer_type_global
= "GLOBAL";
142 const char * const config_overwrite_mode_discard
= "DISCARD";
143 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
145 const char * const config_output_type_splice
= "SPLICE";
146 const char * const config_output_type_mmap
= "MMAP";
148 const char * const config_loglevel_type_all
= "ALL";
149 const char * const config_loglevel_type_range
= "RANGE";
150 const char * const config_loglevel_type_single
= "SINGLE";
152 const char * const config_event_type_all
= "ALL";
153 const char * const config_event_type_tracepoint
= "TRACEPOINT";
154 const char * const config_event_type_probe
= "PROBE";
155 const char * const config_event_type_function
= "FUNCTION";
156 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
157 const char * const config_event_type_noop
= "NOOP";
158 const char * const config_event_type_syscall
= "SYSCALL";
159 const char * const config_event_type_kprobe
= "KPROBE";
160 const char * const config_event_type_kretprobe
= "KRETPROBE";
162 const char * const config_event_context_pid
= "PID";
163 const char * const config_event_context_procname
= "PROCNAME";
164 const char * const config_event_context_prio
= "PRIO";
165 const char * const config_event_context_nice
= "NICE";
166 const char * const config_event_context_vpid
= "VPID";
167 const char * const config_event_context_tid
= "TID";
168 const char * const config_event_context_vtid
= "VTID";
169 const char * const config_event_context_ppid
= "PPID";
170 const char * const config_event_context_vppid
= "VPPID";
171 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
172 const char * const config_event_context_hostname
= "HOSTNAME";
173 const char * const config_event_context_ip
= "IP";
174 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
175 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
176 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
177 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
178 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
179 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
181 enum process_event_node_phase
{
186 struct consumer_output
{
193 static int config_entry_handler_filter(struct handler_filter_args
*args
,
194 const char *section
, const char *name
, const char *value
)
197 struct config_entry entry
= { section
, name
, value
};
201 if (!section
|| !name
|| !value
) {
207 if (strcmp(args
->section
, section
)) {
212 ret
= args
->handler(&entry
, args
->user_data
);
218 int config_get_section_entries(const char *override_path
, const char *section
,
219 config_entry_handler_cb handler
, void *user_data
)
223 FILE *config_file
= NULL
;
224 struct handler_filter_args filter
= { section
, handler
, user_data
};
226 /* First, try system-wide conf. file. */
227 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
229 config_file
= fopen(path
, "r");
231 DBG("Loading daemon conf file at %s", path
);
233 * Return value is not very important here since error or not, we
234 * continue and try the next possible conf. file.
236 (void) ini_parse_file(config_file
,
237 (ini_entry_handler
) config_entry_handler_filter
,
242 /* Second is the user local configuration. */
243 path
= utils_get_home_dir();
245 char fullpath
[PATH_MAX
];
247 ret
= snprintf(fullpath
, sizeof(fullpath
),
248 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
250 PERROR("snprintf user conf. path");
254 config_file
= fopen(fullpath
, "r");
256 DBG("Loading daemon user conf file at %s", path
);
258 * Return value is not very important here since error or not, we
259 * continue and try the next possible conf. file.
261 (void) ini_parse_file(config_file
,
262 (ini_entry_handler
) config_entry_handler_filter
,
268 /* Final path is the one that the user might have provided. */
270 config_file
= fopen(override_path
, "r");
272 DBG("Loading daemon command line conf file at %s", override_path
);
273 (void) ini_parse_file(config_file
,
274 (ini_entry_handler
) config_entry_handler_filter
,
278 ERR("Failed to open daemon configuration file at %s",
285 /* Everything went well. */
293 int config_parse_value(const char *value
)
296 char *endptr
, *lower_str
;
306 v
= strtoul(value
, &endptr
, 10);
307 if (endptr
!= value
) {
312 lower_str
= zmalloc(len
+ 1);
319 for (i
= 0; i
< len
; i
++) {
320 lower_str
[i
] = tolower(value
[i
]);
323 if (!strcmp(lower_str
, config_str_yes
) ||
324 !strcmp(lower_str
, config_str_true
) ||
325 !strcmp(lower_str
, config_str_on
)) {
327 } else if (!strcmp(lower_str
, config_str_no
) ||
328 !strcmp(lower_str
, config_str_false
) ||
329 !strcmp(lower_str
, config_str_off
)) {
341 * Returns a xmlChar string which must be released using xmlFree().
343 static xmlChar
*encode_string(const char *in_str
)
345 xmlChar
*out_str
= NULL
;
346 xmlCharEncodingHandlerPtr handler
;
347 int out_len
, ret
, in_len
;
351 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
353 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
357 in_len
= strlen(in_str
);
359 * Add 1 byte for the NULL terminted character. The factor 4 here is
360 * used because UTF-8 characters can take up to 4 bytes.
362 out_len
= (in_len
* 4) + 1;
363 out_str
= xmlMalloc(out_len
);
368 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
375 /* out_len is now the size of out_str */
376 out_str
[out_len
] = '\0';
382 struct config_writer
*config_writer_create(int fd_output
, int indent
)
385 struct config_writer
*writer
;
386 xmlOutputBufferPtr buffer
;
388 writer
= zmalloc(sizeof(struct config_writer
));
390 PERROR("zmalloc config_writer_create");
394 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
399 writer
->writer
= xmlNewTextWriter(buffer
);
400 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
401 config_xml_encoding
, NULL
);
406 ret
= xmlTextWriterSetIndentString(writer
->writer
,
407 BAD_CAST config_xml_indent_string
);
412 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
420 config_writer_destroy(writer
);
425 int config_writer_destroy(struct config_writer
*writer
)
434 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
435 WARN("Could not close XML document");
439 if (writer
->writer
) {
440 xmlFreeTextWriter(writer
->writer
);
449 int config_writer_open_element(struct config_writer
*writer
,
450 const char *element_name
)
453 xmlChar
*encoded_element_name
;
455 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
460 encoded_element_name
= encode_string(element_name
);
461 if (!encoded_element_name
) {
466 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
467 xmlFree(encoded_element_name
);
469 return ret
>= 0 ? 0 : ret
;
473 int config_writer_write_attribute(struct config_writer
*writer
,
474 const char *name
, const char *value
)
477 xmlChar
*encoded_name
= NULL
;
478 xmlChar
*encoded_value
= NULL
;
480 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
485 encoded_name
= encode_string(name
);
491 encoded_value
= encode_string(value
);
492 if (!encoded_value
) {
497 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
500 xmlFree(encoded_name
);
501 xmlFree(encoded_value
);
502 return ret
>= 0 ? 0 : ret
;
506 int config_writer_close_element(struct config_writer
*writer
)
510 if (!writer
|| !writer
->writer
) {
515 ret
= xmlTextWriterEndElement(writer
->writer
);
517 return ret
>= 0 ? 0 : ret
;
521 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
522 const char *element_name
, uint64_t value
)
525 xmlChar
*encoded_element_name
;
527 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
532 encoded_element_name
= encode_string(element_name
);
533 if (!encoded_element_name
) {
538 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
539 encoded_element_name
, "%" PRIu64
, value
);
540 xmlFree(encoded_element_name
);
542 return ret
>= 0 ? 0 : ret
;
546 int config_writer_write_element_signed_int(struct config_writer
*writer
,
547 const char *element_name
, int64_t value
)
550 xmlChar
*encoded_element_name
;
552 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
557 encoded_element_name
= encode_string(element_name
);
558 if (!encoded_element_name
) {
563 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
564 encoded_element_name
, "%" PRIi64
, value
);
565 xmlFree(encoded_element_name
);
567 return ret
>= 0 ? 0 : ret
;
571 int config_writer_write_element_bool(struct config_writer
*writer
,
572 const char *element_name
, int value
)
574 return config_writer_write_element_string(writer
, element_name
,
575 value
? config_xml_true
: config_xml_false
);
579 int config_writer_write_element_string(struct config_writer
*writer
,
580 const char *element_name
, const char *value
)
583 xmlChar
*encoded_element_name
= NULL
;
584 xmlChar
*encoded_value
= NULL
;
586 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
592 encoded_element_name
= encode_string(element_name
);
593 if (!encoded_element_name
) {
598 encoded_value
= encode_string(value
);
599 if (!encoded_value
) {
604 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
607 xmlFree(encoded_element_name
);
608 xmlFree(encoded_value
);
609 return ret
>= 0 ? 0 : ret
;
613 void xml_error_handler(void *ctx
, const char *format
, ...)
619 va_start(args
, format
);
620 ret
= vasprintf(&errMsg
, format
, args
);
623 ERR("String allocation failed in xml error handler");
627 fprintf(stderr
, "XML Error: %s", errMsg
);
632 void fini_session_config_validation_ctx(
633 struct session_config_validation_ctx
*ctx
)
635 if (ctx
->parser_ctx
) {
636 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
640 xmlSchemaFree(ctx
->schema
);
643 if (ctx
->schema_validation_ctx
) {
644 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
647 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
651 char *get_session_config_xsd_path()
654 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
655 size_t base_path_len
;
659 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
662 base_path_len
= strlen(base_path
);
663 max_path_len
= base_path_len
+
664 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
665 xsd_path
= zmalloc(max_path_len
);
670 strncpy(xsd_path
, base_path
, max_path_len
);
671 if (xsd_path
[base_path_len
- 1] != '/') {
672 xsd_path
[base_path_len
++] = '/';
675 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
676 max_path_len
- base_path_len
);
682 int init_session_config_validation_ctx(
683 struct session_config_validation_ctx
*ctx
)
686 char *xsd_path
= get_session_config_xsd_path();
689 ret
= -LTTNG_ERR_NOMEM
;
693 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
694 if (!ctx
->parser_ctx
) {
695 ERR("XSD parser context creation failed");
696 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
699 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
700 xml_error_handler
, NULL
);
702 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
704 ERR("XSD parsing failed");
705 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
709 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
710 if (!ctx
->schema_validation_ctx
) {
711 ERR("XSD validation context creation failed");
712 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
716 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
717 xml_error_handler
, NULL
);
722 fini_session_config_validation_ctx(ctx
);
730 int parse_uint(xmlChar
*str
, uint64_t *val
)
740 *val
= strtoull((const char *) str
, &endptr
, 10);
741 if (!endptr
|| *endptr
) {
752 int parse_int(xmlChar
*str
, int64_t *val
)
762 *val
= strtoll((const char *) str
, &endptr
, 10);
763 if (!endptr
|| *endptr
) {
774 int parse_bool(xmlChar
*str
, int *val
)
783 if (!strcmp((const char *) str
, config_xml_true
)) {
785 } else if (!strcmp((const char *) str
, config_xml_false
)) {
788 WARN("Invalid boolean value encoutered (%s).",
797 int get_domain_type(xmlChar
*domain
)
805 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
806 ret
= LTTNG_DOMAIN_KERNEL
;
807 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
808 ret
= LTTNG_DOMAIN_UST
;
809 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
810 ret
= LTTNG_DOMAIN_JUL
;
811 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
812 ret
= LTTNG_DOMAIN_LOG4J
;
813 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
814 ret
= LTTNG_DOMAIN_PYTHON
;
825 int get_buffer_type(xmlChar
*buffer_type
)
833 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
834 ret
= LTTNG_BUFFER_GLOBAL
;
835 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
836 ret
= LTTNG_BUFFER_PER_UID
;
837 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
838 ret
= LTTNG_BUFFER_PER_PID
;
849 int get_overwrite_mode(xmlChar
*overwrite_mode
)
853 if (!overwrite_mode
) {
857 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
859 } else if (!strcmp((char *) overwrite_mode
,
860 config_overwrite_mode_discard
)) {
872 int get_output_type(xmlChar
*output_type
)
880 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
881 ret
= LTTNG_EVENT_MMAP
;
882 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
883 ret
= LTTNG_EVENT_SPLICE
;
894 int get_event_type(xmlChar
*event_type
)
902 if (!strcmp((char *) event_type
, config_event_type_all
)) {
903 ret
= LTTNG_EVENT_ALL
;
904 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
905 ret
= LTTNG_EVENT_TRACEPOINT
;
906 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
907 ret
= LTTNG_EVENT_PROBE
;
908 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
909 ret
= LTTNG_EVENT_FUNCTION
;
910 } else if (!strcmp((char *) event_type
,
911 config_event_type_function_entry
)) {
912 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
913 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
914 ret
= LTTNG_EVENT_NOOP
;
915 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
916 ret
= LTTNG_EVENT_SYSCALL
;
927 int get_loglevel_type(xmlChar
*loglevel_type
)
931 if (!loglevel_type
) {
935 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
936 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
937 } else if (!strcmp((char *) loglevel_type
,
938 config_loglevel_type_range
)) {
939 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
940 } else if (!strcmp((char *) loglevel_type
,
941 config_loglevel_type_single
)) {
942 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
953 * Return the context type or -1 on error.
956 int get_context_type(xmlChar
*context_type
)
964 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
965 ret
= LTTNG_EVENT_CONTEXT_PID
;
966 } else if (!strcmp((char *) context_type
,
967 config_event_context_procname
)) {
968 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
969 } else if (!strcmp((char *) context_type
,
970 config_event_context_prio
)) {
971 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
972 } else if (!strcmp((char *) context_type
,
973 config_event_context_nice
)) {
974 ret
= LTTNG_EVENT_CONTEXT_NICE
;
975 } else if (!strcmp((char *) context_type
,
976 config_event_context_vpid
)) {
977 ret
= LTTNG_EVENT_CONTEXT_VPID
;
978 } else if (!strcmp((char *) context_type
,
979 config_event_context_tid
)) {
980 ret
= LTTNG_EVENT_CONTEXT_TID
;
981 } else if (!strcmp((char *) context_type
,
982 config_event_context_vtid
)) {
983 ret
= LTTNG_EVENT_CONTEXT_VTID
;
984 } else if (!strcmp((char *) context_type
,
985 config_event_context_ppid
)) {
986 ret
= LTTNG_EVENT_CONTEXT_PPID
;
987 } else if (!strcmp((char *) context_type
,
988 config_event_context_vppid
)) {
989 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
990 } else if (!strcmp((char *) context_type
,
991 config_event_context_pthread_id
)) {
992 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
993 } else if (!strcmp((char *) context_type
,
994 config_event_context_hostname
)) {
995 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
996 } else if (!strcmp((char *) context_type
,
997 config_event_context_ip
)) {
998 ret
= LTTNG_EVENT_CONTEXT_IP
;
999 } else if (!strcmp((char *) context_type
,
1000 config_event_context_interruptible
)) {
1001 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1002 } else if (!strcmp((char *) context_type
,
1003 config_event_context_preemptible
)) {
1004 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1005 } else if (!strcmp((char *) context_type
,
1006 config_event_context_need_reschedule
)) {
1007 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1008 } else if (!strcmp((char *) context_type
,
1009 config_event_context_migratable
)) {
1010 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1021 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1026 for (node
= xmlFirstElementChild(domain_node
); node
;
1027 node
= xmlNextElementSibling(node
)) {
1028 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1030 xmlChar
*node_content
= xmlNodeGetContent(node
);
1031 if (!node_content
) {
1032 ret
= -LTTNG_ERR_NOMEM
;
1036 ret
= get_domain_type(node_content
);
1039 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1044 } else if (!strcmp((const char *) node
->name
,
1045 config_element_buffer_type
)) {
1047 xmlChar
*node_content
= xmlNodeGetContent(node
);
1048 if (!node_content
) {
1049 ret
= -LTTNG_ERR_NOMEM
;
1053 ret
= get_buffer_type(node_content
);
1056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1060 domain
->buf_type
= ret
;
1069 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1074 for (node
= xmlFirstElementChild(net_output_node
); node
;
1075 node
= xmlNextElementSibling(node
)) {
1076 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1078 *control_uri
= (char *) xmlNodeGetContent(node
);
1079 if (!*control_uri
) {
1084 *data_uri
= (char *) xmlNodeGetContent(node
);
1091 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1095 int process_consumer_output(xmlNodePtr consumer_output_node
,
1096 struct consumer_output
*output
)
1103 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1104 node
= xmlNextElementSibling(node
)) {
1105 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1106 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1110 ret
= -LTTNG_ERR_NOMEM
;
1114 ret
= parse_bool(enabled_str
, &output
->enabled
);
1120 xmlNodePtr output_type_node
;
1123 output_type_node
= xmlFirstElementChild(node
);
1124 if (!output_type_node
) {
1125 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1129 if (!strcmp((const char *) output_type_node
->name
,
1130 config_element_path
)) {
1132 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1133 if (!output
->path
) {
1134 ret
= -LTTNG_ERR_NOMEM
;
1139 ret
= get_net_output_uris(output_type_node
,
1140 &output
->control_uri
, &output
->data_uri
);
1152 free(output
->control_uri
);
1153 free(output
->data_uri
);
1154 memset(output
, 0, sizeof(struct consumer_output
));
1160 int create_session_net_output(const char *name
, const char *control_uri
,
1161 const char *data_uri
)
1164 struct lttng_handle
*handle
;
1165 const char *uri
= NULL
;
1169 handle
= lttng_create_handle(name
, NULL
);
1171 ret
= -LTTNG_ERR_NOMEM
;
1175 if (!control_uri
|| !data_uri
) {
1176 uri
= control_uri
? control_uri
: data_uri
;
1181 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1182 lttng_destroy_handle(handle
);
1188 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1191 xmlNodePtr node
= NULL
;
1192 xmlNodePtr snapshot_output_list_node
;
1193 xmlNodePtr snapshot_output_node
;
1195 assert(session_name
);
1197 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1206 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1208 /* Parse and create snapshot outputs */
1210 for (snapshot_output_node
=
1211 xmlFirstElementChild(snapshot_output_list_node
);
1212 snapshot_output_node
; snapshot_output_node
=
1213 xmlNextElementSibling(snapshot_output_node
)) {
1215 uint64_t max_size
= UINT64_MAX
;
1216 struct consumer_output output
= { 0 };
1217 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1219 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1220 node
= xmlNextElementSibling(node
)) {
1221 if (!strcmp((const char *) node
->name
,
1222 config_element_name
)) {
1224 name
= (char *) xmlNodeGetContent(node
);
1226 ret
= -LTTNG_ERR_NOMEM
;
1227 goto error_snapshot_output
;
1229 } else if (!strcmp((const char *) node
->name
,
1230 config_element_max_size
)) {
1231 xmlChar
*content
= xmlNodeGetContent(node
);
1235 ret
= -LTTNG_ERR_NOMEM
;
1236 goto error_snapshot_output
;
1238 ret
= parse_uint(content
, &max_size
);
1241 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1242 goto error_snapshot_output
;
1245 /* consumer_output */
1246 ret
= process_consumer_output(node
, &output
);
1248 goto error_snapshot_output
;
1253 snapshot_output
= lttng_snapshot_output_create();
1254 if (!snapshot_output
) {
1255 ret
= -LTTNG_ERR_NOMEM
;
1256 goto error_snapshot_output
;
1259 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1261 goto error_snapshot_output
;
1264 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1266 goto error_snapshot_output
;
1270 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1273 goto error_snapshot_output
;
1276 if (output
.control_uri
) {
1277 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1280 goto error_snapshot_output
;
1284 if (output
.data_uri
) {
1285 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1288 goto error_snapshot_output
;
1293 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1294 error_snapshot_output
:
1297 free(output
.control_uri
);
1298 free(output
.data_uri
);
1299 lttng_snapshot_output_destroy(snapshot_output
);
1309 int create_session(const char *name
,
1310 struct lttng_domain
*kernel_domain
,
1311 struct lttng_domain
*ust_domain
,
1312 struct lttng_domain
*jul_domain
,
1313 struct lttng_domain
*log4j_domain
,
1314 xmlNodePtr output_node
,
1315 uint64_t live_timer_interval
)
1318 struct consumer_output output
= { 0 };
1319 xmlNodePtr consumer_output_node
;
1324 consumer_output_node
= xmlFirstElementChild(output_node
);
1325 if (!consumer_output_node
) {
1326 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1330 if (strcmp((const char *) consumer_output_node
->name
,
1331 config_element_consumer_output
)) {
1332 WARN("Invalid output type, expected %s node",
1333 config_element_consumer_output
);
1334 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1338 ret
= process_consumer_output(consumer_output_node
, &output
);
1344 if (live_timer_interval
!= UINT64_MAX
&&
1345 !output
.control_uri
&& !output
.data_uri
) {
1346 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1350 if (output
.control_uri
|| output
.data_uri
) {
1351 /* network destination */
1352 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1354 * URLs are provided for sure since the test above make sure that
1355 * with a live timer the data and control URIs are provided. So,
1356 * NULL is passed here and will be set right after.
1358 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1360 ret
= lttng_create_session(name
, NULL
);
1366 ret
= create_session_net_output(name
, output
.control_uri
,
1373 /* either local output or no output */
1374 ret
= lttng_create_session(name
, output
.path
);
1381 free(output
.control_uri
);
1382 free(output
.data_uri
);
1386 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1387 struct lttng_event_probe_attr
*attr
)
1391 assert(probe_attribute_node
);
1394 if (!strcmp((const char *) probe_attribute_node
->name
,
1395 config_element_address
)) {
1400 content
= xmlNodeGetContent(probe_attribute_node
);
1402 ret
= -LTTNG_ERR_NOMEM
;
1406 ret
= parse_uint(content
, &addr
);
1409 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1414 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1415 config_element_offset
)) {
1417 uint64_t offset
= 0;
1420 content
= xmlNodeGetContent(probe_attribute_node
);
1422 ret
= -LTTNG_ERR_NOMEM
;
1426 ret
= parse_uint(content
, &offset
);
1429 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1433 attr
->offset
= offset
;
1434 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1435 config_element_symbol_name
)) {
1440 content
= xmlNodeGetContent(probe_attribute_node
);
1442 ret
= -LTTNG_ERR_NOMEM
;
1446 name_len
= strlen((char *) content
);
1447 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1448 WARN("symbol_name too long.");
1449 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1454 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1463 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1464 const char *channel_name
, const enum process_event_node_phase phase
)
1468 struct lttng_event event
;
1469 char **exclusions
= NULL
;
1470 unsigned long exclusion_count
= 0;
1471 char *filter_expression
= NULL
;
1475 assert(channel_name
);
1477 memset(&event
, 0, sizeof(event
));
1479 /* Initialize default log level which varies by domain */
1480 switch (handle
->domain
.type
)
1482 case LTTNG_DOMAIN_JUL
:
1483 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1485 case LTTNG_DOMAIN_LOG4J
:
1486 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1488 case LTTNG_DOMAIN_PYTHON
:
1489 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1491 case LTTNG_DOMAIN_UST
:
1492 case LTTNG_DOMAIN_KERNEL
:
1493 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1499 for (node
= xmlFirstElementChild(event_node
); node
;
1500 node
= xmlNextElementSibling(node
)) {
1501 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1506 content
= xmlNodeGetContent(node
);
1508 ret
= -LTTNG_ERR_NOMEM
;
1512 name_len
= strlen((char *) content
);
1513 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1514 WARN("Channel name too long.");
1515 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1520 strncpy(event
.name
, (const char *) content
, name_len
);
1522 } else if (!strcmp((const char *) node
->name
,
1523 config_element_enabled
)) {
1524 xmlChar
*content
= xmlNodeGetContent(node
);
1528 ret
= -LTTNG_ERR_NOMEM
;
1532 ret
= parse_bool(content
, &event
.enabled
);
1535 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1538 } else if (!strcmp((const char *) node
->name
,
1539 config_element_type
)) {
1540 xmlChar
*content
= xmlNodeGetContent(node
);
1544 ret
= -LTTNG_ERR_NOMEM
;
1548 ret
= get_event_type(content
);
1551 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1556 } else if (!strcmp((const char *) node
->name
,
1557 config_element_loglevel_type
)) {
1558 xmlChar
*content
= xmlNodeGetContent(node
);
1562 ret
= -LTTNG_ERR_NOMEM
;
1566 ret
= get_loglevel_type(content
);
1569 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1573 event
.loglevel_type
= ret
;
1574 } else if (!strcmp((const char *) node
->name
,
1575 config_element_loglevel
)) {
1577 int64_t loglevel
= 0;
1580 content
= xmlNodeGetContent(node
);
1582 ret
= -LTTNG_ERR_NOMEM
;
1586 ret
= parse_int(content
, &loglevel
);
1589 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1593 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1594 WARN("loglevel out of range.");
1595 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1599 event
.loglevel
= loglevel
;
1600 } else if (!strcmp((const char *) node
->name
,
1601 config_element_filter
)) {
1603 xmlNodeGetContent(node
);
1607 ret
= -LTTNG_ERR_NOMEM
;
1611 filter_expression
= strdup((char *) content
);
1613 if (!filter_expression
) {
1614 ret
= -LTTNG_ERR_NOMEM
;
1617 } else if (!strcmp((const char *) node
->name
,
1618 config_element_exclusions
)) {
1619 xmlNodePtr exclusion_node
;
1620 int exclusion_index
= 0;
1625 * Exclusions has already been initialized,
1628 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1632 exclusion_count
= xmlChildElementCount(node
);
1633 if (!exclusion_count
) {
1637 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1639 exclusion_count
= 0;
1640 ret
= -LTTNG_ERR_NOMEM
;
1644 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1645 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1647 xmlNodeGetContent(exclusion_node
);
1650 ret
= -LTTNG_ERR_NOMEM
;
1654 exclusions
[exclusion_index
] = strdup((const char *) content
);
1656 if (!exclusions
[exclusion_index
]) {
1657 ret
= -LTTNG_ERR_NOMEM
;
1663 event
.exclusion
= 1;
1664 } else if (!strcmp((const char *) node
->name
,
1665 config_element_attributes
)) {
1666 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1669 if (!attribute_node
) {
1670 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1674 if (!strcmp((const char *) node
->name
,
1675 config_element_probe_attributes
)) {
1676 xmlNodePtr probe_attribute_node
;
1678 /* probe_attributes */
1679 for (probe_attribute_node
=
1680 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1681 probe_attribute_node
= xmlNextElementSibling(
1682 probe_attribute_node
)) {
1684 ret
= process_probe_attribute_node(probe_attribute_node
,
1693 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1695 /* function_attributes */
1696 content
= xmlNodeGetContent(symbol_node
);
1698 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1702 sym_len
= strlen((char *) content
);
1703 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1704 WARN("Function name too long.");
1705 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1710 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1717 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1718 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1719 filter_expression
, exclusion_count
, exclusions
);
1721 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1722 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1727 for (i
= 0; i
< exclusion_count
; i
++) {
1728 free(exclusions
[i
]);
1732 free(filter_expression
);
1737 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1738 const char *channel_name
)
1741 struct lttng_event event
;
1744 assert(events_node
);
1746 assert(channel_name
);
1748 for (node
= xmlFirstElementChild(events_node
); node
;
1749 node
= xmlNextElementSibling(node
)) {
1750 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1757 * Disable all events to enable only the necessary events.
1758 * Limitations regarding lttng_disable_events and tuple descriptor
1759 * force this approach.
1761 memset(&event
, 0, sizeof(event
));
1762 event
.loglevel
= -1;
1763 event
.type
= LTTNG_EVENT_ALL
;
1764 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1769 for (node
= xmlFirstElementChild(events_node
); node
;
1770 node
= xmlNextElementSibling(node
)) {
1771 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1782 int process_channel_attr_node(xmlNodePtr attr_node
,
1783 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1784 xmlNodePtr
*events_node
)
1790 assert(contexts_node
);
1791 assert(events_node
);
1793 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1798 content
= xmlNodeGetContent(attr_node
);
1800 ret
= -LTTNG_ERR_NOMEM
;
1804 name_len
= strlen((char *) content
);
1805 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1806 WARN("Channel name too long.");
1807 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1812 strncpy(channel
->name
, (const char *) content
, name_len
);
1814 } else if (!strcmp((const char *) attr_node
->name
,
1815 config_element_enabled
)) {
1820 content
= xmlNodeGetContent(attr_node
);
1822 ret
= -LTTNG_ERR_NOMEM
;
1826 ret
= parse_bool(content
, &enabled
);
1829 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1833 channel
->enabled
= enabled
;
1834 } else if (!strcmp((const char *) attr_node
->name
,
1835 config_element_overwrite_mode
)) {
1838 /* overwrite_mode */
1839 content
= xmlNodeGetContent(attr_node
);
1841 ret
= -LTTNG_ERR_NOMEM
;
1845 ret
= get_overwrite_mode(content
);
1848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 channel
->attr
.overwrite
= ret
;
1853 } else if (!strcmp((const char *) attr_node
->name
,
1854 config_element_subbuf_size
)) {
1857 /* subbuffer_size */
1858 content
= xmlNodeGetContent(attr_node
);
1860 ret
= -LTTNG_ERR_NOMEM
;
1864 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1867 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1870 } else if (!strcmp((const char *) attr_node
->name
,
1871 config_element_num_subbuf
)) {
1874 /* subbuffer_count */
1875 content
= xmlNodeGetContent(attr_node
);
1877 ret
= -LTTNG_ERR_NOMEM
;
1881 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1884 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1887 } else if (!strcmp((const char *) attr_node
->name
,
1888 config_element_switch_timer_interval
)) {
1890 uint64_t switch_timer_interval
= 0;
1892 /* switch_timer_interval */
1893 content
= xmlNodeGetContent(attr_node
);
1895 ret
= -LTTNG_ERR_NOMEM
;
1899 ret
= parse_uint(content
, &switch_timer_interval
);
1902 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1906 if (switch_timer_interval
> UINT_MAX
) {
1907 WARN("switch_timer_interval out of range.");
1908 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1912 channel
->attr
.switch_timer_interval
=
1913 switch_timer_interval
;
1914 } else if (!strcmp((const char *) attr_node
->name
,
1915 config_element_read_timer_interval
)) {
1917 uint64_t read_timer_interval
= 0;
1919 /* read_timer_interval */
1920 content
= xmlNodeGetContent(attr_node
);
1922 ret
= -LTTNG_ERR_NOMEM
;
1926 ret
= parse_uint(content
, &read_timer_interval
);
1929 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1933 if (read_timer_interval
> UINT_MAX
) {
1934 WARN("read_timer_interval out of range.");
1935 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1939 channel
->attr
.read_timer_interval
=
1940 read_timer_interval
;
1941 } else if (!strcmp((const char *) attr_node
->name
,
1942 config_element_output_type
)) {
1946 content
= xmlNodeGetContent(attr_node
);
1948 ret
= -LTTNG_ERR_NOMEM
;
1952 ret
= get_output_type(content
);
1955 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1959 channel
->attr
.output
= ret
;
1960 } else if (!strcmp((const char *) attr_node
->name
,
1961 config_element_tracefile_size
)) {
1964 /* tracefile_size */
1965 content
= xmlNodeGetContent(attr_node
);
1967 ret
= -LTTNG_ERR_NOMEM
;
1971 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1974 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1977 } else if (!strcmp((const char *) attr_node
->name
,
1978 config_element_tracefile_count
)) {
1981 /* tracefile_count */
1982 content
= xmlNodeGetContent(attr_node
);
1984 ret
= -LTTNG_ERR_NOMEM
;
1988 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1991 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1994 } else if (!strcmp((const char *) attr_node
->name
,
1995 config_element_live_timer_interval
)) {
1997 uint64_t live_timer_interval
= 0;
1999 /* live_timer_interval */
2000 content
= xmlNodeGetContent(attr_node
);
2002 ret
= -LTTNG_ERR_NOMEM
;
2006 ret
= parse_uint(content
, &live_timer_interval
);
2009 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2013 if (live_timer_interval
> UINT_MAX
) {
2014 WARN("live_timer_interval out of range.");
2015 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2019 channel
->attr
.live_timer_interval
=
2020 live_timer_interval
;
2021 } else if (!strcmp((const char *) attr_node
->name
,
2022 config_element_events
)) {
2024 *events_node
= attr_node
;
2027 *contexts_node
= attr_node
;
2035 int process_context_node(xmlNodePtr context_node
,
2036 struct lttng_handle
*handle
, const char *channel_name
)
2039 struct lttng_event_context context
;
2040 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2043 assert(channel_name
);
2045 if (!context_child_node
) {
2046 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2050 memset(&context
, 0, sizeof(context
));
2052 if (!strcmp((const char *) context_child_node
->name
,
2053 config_element_type
)) {
2055 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2058 ret
= -LTTNG_ERR_NOMEM
;
2062 ret
= get_context_type(content
);
2065 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2070 } else if (!strcmp((const char *) context_child_node
->name
,
2071 config_element_context_perf
)) {
2073 xmlNodePtr perf_attr_node
;
2075 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2076 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2077 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2078 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2079 perf_attr_node
; perf_attr_node
=
2080 xmlNextElementSibling(perf_attr_node
)) {
2081 if (!strcmp((const char *) perf_attr_node
->name
,
2082 config_element_type
)) {
2087 content
= xmlNodeGetContent(perf_attr_node
);
2089 ret
= -LTTNG_ERR_NOMEM
;
2093 ret
= parse_uint(content
, &type
);
2096 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2100 if (type
> UINT32_MAX
) {
2101 WARN("perf context type out of range.");
2102 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2106 context
.u
.perf_counter
.type
= type
;
2107 } else if (!strcmp((const char *) perf_attr_node
->name
,
2108 config_element_config
)) {
2110 uint64_t config
= 0;
2113 content
= xmlNodeGetContent(perf_attr_node
);
2115 ret
= -LTTNG_ERR_NOMEM
;
2119 ret
= parse_uint(content
, &config
);
2122 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2126 context
.u
.perf_counter
.config
= config
;
2127 } else if (!strcmp((const char *) perf_attr_node
->name
,
2128 config_element_name
)) {
2133 content
= xmlNodeGetContent(perf_attr_node
);
2135 ret
= -LTTNG_ERR_NOMEM
;
2139 name_len
= strlen((char *) content
);
2140 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2141 WARN("perf context name too long.");
2142 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2147 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2152 } else if (!strcmp((const char *) context_child_node
->name
,
2153 config_element_context_app
)) {
2154 /* application context */
2155 xmlNodePtr app_ctx_node
;
2157 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2158 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2159 app_ctx_node
; app_ctx_node
=
2160 xmlNextElementSibling(app_ctx_node
)) {
2162 char **target
= strcmp(
2163 (const char *) app_ctx_node
->name
,
2164 config_element_context_app_provider_name
) == 0 ?
2165 &context
.u
.app_ctx
.provider_name
:
2166 &context
.u
.app_ctx
.ctx_name
;
2168 content
= xmlNodeGetContent(app_ctx_node
);
2170 ret
= -LTTNG_ERR_NOMEM
;
2174 *target
= (char *) content
;
2177 /* Unrecognized context type */
2178 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2182 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2183 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2184 free(context
.u
.app_ctx
.provider_name
);
2185 free(context
.u
.app_ctx
.ctx_name
);
2192 int process_contexts_node(xmlNodePtr contexts_node
,
2193 struct lttng_handle
*handle
, const char *channel_name
)
2196 xmlNodePtr context_node
;
2198 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2199 context_node
= xmlNextElementSibling(context_node
)) {
2200 ret
= process_context_node(context_node
, handle
, channel_name
);
2210 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2211 struct lttng_handle
*handle
)
2214 xmlNodePtr targets_node
= NULL
;
2218 assert(pid_tracker_node
);
2219 /* get the targets node */
2220 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2221 node
= xmlNextElementSibling(node
)) {
2222 if (!strcmp((const char *) node
->name
,
2223 config_element_targets
)) {
2224 targets_node
= node
;
2229 if (!targets_node
) {
2230 ret
= LTTNG_ERR_INVALID
;
2234 /* Go through all pid_target node */
2235 child
= xmlChildElementCount(targets_node
);
2237 /* The session is explicitly set to target nothing. */
2238 ret
= lttng_untrack_pid(handle
, -1);
2243 for (node
= xmlFirstElementChild(targets_node
); node
;
2244 node
= xmlNextElementSibling(node
)) {
2245 xmlNodePtr pid_target_node
= node
;
2247 /* get pid node and track it */
2248 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2249 node
= xmlNextElementSibling(node
)) {
2250 if (!strcmp((const char *) node
->name
,
2251 config_element_pid
)) {
2253 xmlChar
*content
= NULL
;
2255 content
= xmlNodeGetContent(node
);
2257 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2261 ret
= parse_int(content
, &pid
);
2264 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2268 ret
= lttng_track_pid(handle
, (int) pid
);
2274 node
= pid_target_node
;
2283 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2286 struct lttng_domain domain
= { 0 };
2287 struct lttng_handle
*handle
= NULL
;
2288 xmlNodePtr channels_node
= NULL
;
2289 xmlNodePtr trackers_node
= NULL
;
2290 xmlNodePtr pid_tracker_node
= NULL
;
2293 assert(session_name
);
2295 ret
= init_domain(domain_node
, &domain
);
2300 handle
= lttng_create_handle(session_name
, &domain
);
2302 ret
= -LTTNG_ERR_NOMEM
;
2306 /* get the channels node */
2307 for (node
= xmlFirstElementChild(domain_node
); node
;
2308 node
= xmlNextElementSibling(node
)) {
2309 if (!strcmp((const char *) node
->name
,
2310 config_element_channels
)) {
2311 channels_node
= node
;
2316 if (!channels_node
) {
2320 /* create all channels */
2321 for (node
= xmlFirstElementChild(channels_node
); node
;
2322 node
= xmlNextElementSibling(node
)) {
2323 struct lttng_channel channel
;
2324 xmlNodePtr contexts_node
= NULL
;
2325 xmlNodePtr events_node
= NULL
;
2326 xmlNodePtr channel_attr_node
;
2328 memset(&channel
, 0, sizeof(channel
));
2329 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2331 for (channel_attr_node
= xmlFirstElementChild(node
);
2332 channel_attr_node
; channel_attr_node
=
2333 xmlNextElementSibling(channel_attr_node
)) {
2334 ret
= process_channel_attr_node(channel_attr_node
,
2335 &channel
, &contexts_node
, &events_node
);
2341 ret
= lttng_enable_channel(handle
, &channel
);
2346 ret
= process_events_node(events_node
, handle
, channel
.name
);
2351 ret
= process_contexts_node(contexts_node
, handle
,
2358 /* get the trackers node */
2359 for (node
= xmlFirstElementChild(domain_node
); node
;
2360 node
= xmlNextElementSibling(node
)) {
2361 if (!strcmp((const char *) node
->name
,
2362 config_element_trackers
)) {
2363 trackers_node
= node
;
2368 if (!trackers_node
) {
2372 for (node
= xmlFirstElementChild(trackers_node
); node
;
2373 node
= xmlNextElementSibling(node
)) {
2374 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2375 pid_tracker_node
= node
;
2376 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2383 if (!pid_tracker_node
) {
2384 lttng_track_pid(handle
, -1);
2388 lttng_destroy_handle(handle
);
2393 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2396 int ret
, started
= -1, snapshot_mode
= -1;
2397 uint64_t live_timer_interval
= UINT64_MAX
;
2398 xmlChar
*name
= NULL
;
2399 xmlChar
*shm_path
= NULL
;
2400 xmlNodePtr domains_node
= NULL
;
2401 xmlNodePtr output_node
= NULL
;
2403 struct lttng_domain
*kernel_domain
= NULL
;
2404 struct lttng_domain
*ust_domain
= NULL
;
2405 struct lttng_domain
*jul_domain
= NULL
;
2406 struct lttng_domain
*log4j_domain
= NULL
;
2407 struct lttng_domain
*python_domain
= NULL
;
2409 for (node
= xmlFirstElementChild(session_node
); node
;
2410 node
= xmlNextElementSibling(node
)) {
2411 if (!name
&& !strcmp((const char *) node
->name
,
2412 config_element_name
)) {
2414 xmlChar
*node_content
= xmlNodeGetContent(node
);
2415 if (!node_content
) {
2416 ret
= -LTTNG_ERR_NOMEM
;
2420 name
= node_content
;
2421 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2422 config_element_domains
)) {
2424 domains_node
= node
;
2425 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2426 config_element_started
)) {
2428 xmlChar
*node_content
= xmlNodeGetContent(node
);
2429 if (!node_content
) {
2430 ret
= -LTTNG_ERR_NOMEM
;
2434 ret
= parse_bool(node_content
, &started
);
2437 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2440 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2441 config_element_output
)) {
2444 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2445 config_element_shared_memory_path
)) {
2446 /* shared memory path */
2447 xmlChar
*node_content
= xmlNodeGetContent(node
);
2448 if (!node_content
) {
2449 ret
= -LTTNG_ERR_NOMEM
;
2453 shm_path
= node_content
;
2455 /* attributes, snapshot_mode or live_timer_interval */
2456 xmlNodePtr attributes_child
=
2457 xmlFirstElementChild(node
);
2459 if (!strcmp((const char *) attributes_child
->name
,
2460 config_element_snapshot_mode
)) {
2462 xmlChar
*snapshot_mode_content
=
2463 xmlNodeGetContent(attributes_child
);
2464 if (!snapshot_mode_content
) {
2465 ret
= -LTTNG_ERR_NOMEM
;
2469 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2470 free(snapshot_mode_content
);
2472 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2476 /* live_timer_interval */
2477 xmlChar
*timer_interval_content
=
2478 xmlNodeGetContent(attributes_child
);
2479 if (!timer_interval_content
) {
2480 ret
= -LTTNG_ERR_NOMEM
;
2484 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2485 free(timer_interval_content
);
2487 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2495 /* Mandatory attribute, as defined in the session XSD */
2496 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2500 if (session_name
&& strcmp((char *) name
, session_name
)) {
2501 /* This is not the session we are looking for */
2502 ret
= -LTTNG_ERR_NO_SESSION
;
2506 /* Init domains to create the session handles */
2507 for (node
= xmlFirstElementChild(domains_node
); node
;
2508 node
= xmlNextElementSibling(node
)) {
2509 struct lttng_domain
*domain
;
2511 domain
= zmalloc(sizeof(*domain
));
2513 ret
= -LTTNG_ERR_NOMEM
;
2517 ret
= init_domain(node
, domain
);
2519 goto domain_init_error
;
2522 switch (domain
->type
) {
2523 case LTTNG_DOMAIN_KERNEL
:
2524 if (kernel_domain
) {
2525 /* Same domain seen twice, invalid! */
2526 goto domain_init_error
;
2528 kernel_domain
= domain
;
2530 case LTTNG_DOMAIN_UST
:
2532 /* Same domain seen twice, invalid! */
2533 goto domain_init_error
;
2535 ust_domain
= domain
;
2537 case LTTNG_DOMAIN_JUL
:
2539 /* Same domain seen twice, invalid! */
2540 goto domain_init_error
;
2542 jul_domain
= domain
;
2544 case LTTNG_DOMAIN_LOG4J
:
2546 /* Same domain seen twice, invalid! */
2547 goto domain_init_error
;
2549 log4j_domain
= domain
;
2551 case LTTNG_DOMAIN_PYTHON
:
2552 if (python_domain
) {
2553 /* Same domain seen twice, invalid! */
2554 goto domain_init_error
;
2556 python_domain
= domain
;
2559 WARN("Invalid domain type");
2560 goto domain_init_error
;
2565 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2570 /* Destroy session if it exists */
2571 ret
= lttng_destroy_session((const char *) name
);
2572 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2573 ERR("Failed to destroy existing session.");
2578 /* Create session type depending on output type */
2579 if (snapshot_mode
&& snapshot_mode
!= -1) {
2580 ret
= create_snapshot_session((const char *) name
, output_node
);
2581 } else if (live_timer_interval
&&
2582 live_timer_interval
!= UINT64_MAX
) {
2583 ret
= create_session((const char *) name
, kernel_domain
,
2584 ust_domain
, jul_domain
, log4j_domain
,
2585 output_node
, live_timer_interval
);
2587 /* regular session */
2588 ret
= create_session((const char *) name
, kernel_domain
,
2589 ust_domain
, jul_domain
, log4j_domain
,
2590 output_node
, UINT64_MAX
);
2597 ret
= lttng_set_session_shm_path((const char *) name
,
2598 (const char *) shm_path
);
2604 for (node
= xmlFirstElementChild(domains_node
); node
;
2605 node
= xmlNextElementSibling(node
)) {
2606 ret
= process_domain_node(node
, (const char *) name
);
2613 ret
= lttng_start_tracing((const char *) name
);
2621 ERR("Failed to load session %s: %s", (const char *) name
,
2622 lttng_strerror(ret
));
2623 lttng_destroy_session((const char *) name
);
2627 free(kernel_domain
);
2631 free(python_domain
);
2638 * Return 1 if the given path is readable by the current UID or 0 if not.
2639 * Return -1 if the path is EPERM.
2641 static int validate_file_read_creds(const char *path
)
2647 /* Can we read the file. */
2648 ret
= access(path
, R_OK
);
2652 if (errno
== EACCES
) {
2663 int load_session_from_file(const char *path
, const char *session_name
,
2664 struct session_config_validation_ctx
*validation_ctx
, int override
)
2666 int ret
, session_found
= !session_name
;
2667 xmlDocPtr doc
= NULL
;
2668 xmlNodePtr sessions_node
;
2669 xmlNodePtr session_node
;
2672 assert(validation_ctx
);
2674 ret
= validate_file_read_creds(path
);
2677 ret
= -LTTNG_ERR_EPERM
;
2679 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2684 doc
= xmlParseFile(path
);
2686 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2690 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2692 ERR("Session configuration file validation failed");
2693 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2697 sessions_node
= xmlDocGetRootElement(doc
);
2698 if (!sessions_node
) {
2702 for (session_node
= xmlFirstElementChild(sessions_node
);
2703 session_node
; session_node
=
2704 xmlNextElementSibling(session_node
)) {
2705 ret
= process_session_node(session_node
,
2706 session_name
, override
);
2707 if (session_name
&& ret
== 0) {
2708 /* Target session found and loaded */
2716 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2721 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2723 struct dirent
*alloc_dirent(const char *path
)
2727 struct dirent
*entry
;
2729 name_max
= pathconf(path
, _PC_NAME_MAX
);
2730 if (name_max
== -1) {
2731 name_max
= PATH_MAX
;
2733 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2734 entry
= zmalloc(len
);
2739 int load_session_from_path(const char *path
, const char *session_name
,
2740 struct session_config_validation_ctx
*validation_ctx
, int override
)
2742 int ret
, session_found
= !session_name
;
2743 DIR *directory
= NULL
;
2746 assert(validation_ctx
);
2748 directory
= opendir(path
);
2752 /* Try the file loading. */
2755 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2758 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2763 struct dirent
*entry
;
2764 struct dirent
*result
;
2765 char *file_path
= NULL
;
2766 size_t path_len
= strlen(path
);
2768 if (path_len
>= PATH_MAX
) {
2769 ret
= -LTTNG_ERR_INVALID
;
2773 entry
= alloc_dirent(path
);
2775 ret
= -LTTNG_ERR_NOMEM
;
2779 file_path
= zmalloc(PATH_MAX
);
2781 ret
= -LTTNG_ERR_NOMEM
;
2786 strncpy(file_path
, path
, path_len
);
2787 if (file_path
[path_len
- 1] != '/') {
2788 file_path
[path_len
++] = '/';
2792 /* Search for *.lttng files */
2793 while (!readdir_r(directory
, entry
, &result
) && result
) {
2794 size_t file_name_len
= strlen(result
->d_name
);
2796 if (file_name_len
<=
2797 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2801 if (path_len
+ file_name_len
>= PATH_MAX
) {
2805 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2806 result
->d_name
+ file_name_len
- sizeof(
2807 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2811 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2812 file_path
[path_len
+ file_name_len
] = '\0';
2814 ret
= load_session_from_file(file_path
, session_name
,
2815 validation_ctx
, override
);
2816 if (session_name
&& !ret
) {
2825 ret
= load_session_from_file(path
, session_name
,
2826 validation_ctx
, override
);
2836 if (closedir(directory
)) {
2841 if (session_found
) {
2849 * Validate that the given path's credentials and the current process have the
2850 * same UID. If so, return 1 else return 0 if it does NOT match.
2852 static int validate_path_creds(const char *path
)
2854 int ret
, uid
= getuid();
2863 ret
= stat(path
, &buf
);
2865 if (errno
!= ENOENT
) {
2868 ret
= -LTTNG_ERR_INVALID
;
2872 if (buf
.st_uid
!= uid
) {
2883 int config_load_session(const char *path
, const char *session_name
,
2884 int override
, unsigned int autoload
)
2887 bool session_loaded
= false;
2888 const char *path_ptr
= NULL
;
2889 struct session_config_validation_ctx validation_ctx
= { 0 };
2891 ret
= init_session_config_validation_ctx(&validation_ctx
);
2898 const char *sys_path
;
2901 home_path
= utils_get_home_dir();
2903 char path
[PATH_MAX
];
2906 * Try user session configuration path. Ignore error here so we can
2907 * continue loading the system wide sessions.
2910 ret
= snprintf(path
, sizeof(path
),
2911 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2912 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2914 PERROR("snprintf session autoload home config path");
2919 * Credentials are only validated for the autoload in order to
2920 * avoid any user session daemon to try to load kernel sessions
2921 * automatically and failing all the times.
2923 ret
= validate_path_creds(path
);
2928 ret
= snprintf(path
, sizeof(path
),
2929 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2931 PERROR("snprintf session home config path");
2937 ret
= load_session_from_path(path_ptr
, session_name
,
2938 &validation_ctx
, override
);
2939 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2943 * Continue even if the session was found since we have to try
2944 * the system wide sessions.
2946 session_loaded
= true;
2950 /* Reset path pointer for the system wide dir. */
2953 /* Try system wide configuration directory. */
2955 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2956 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2957 ret
= validate_path_creds(sys_path
);
2959 path_ptr
= sys_path
;
2962 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2963 path_ptr
= sys_path
;
2967 ret
= load_session_from_path(path_ptr
, session_name
,
2968 &validation_ctx
, override
);
2970 session_loaded
= true;
2974 ret
= access(path
, F_OK
);
2979 ret
= -LTTNG_ERR_INVALID
;
2980 WARN("Session configuration path does not exist.");
2983 ret
= -LTTNG_ERR_EPERM
;
2986 ret
= -LTTNG_ERR_UNK
;
2992 ret
= load_session_from_path(path
, session_name
,
2993 &validation_ctx
, override
);
2996 fini_session_config_validation_ctx(&validation_ctx
);
2997 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2999 * Don't report an error if no sessions are found when called
3000 * without a session_name or a search path.
3005 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3006 /* A matching session was found in one of the search paths. */