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>
30 #include <common/defaults.h>
31 #include <common/error.h>
32 #include <common/macros.h>
33 #include <common/utils.h>
34 #include <common/compat/getenv.h>
35 #include <lttng/lttng-error.h>
36 #include <libxml/parser.h>
37 #include <libxml/valid.h>
38 #include <libxml/xmlschemas.h>
39 #include <libxml/tree.h>
40 #include <lttng/lttng.h>
41 #include <lttng/snapshot.h>
43 #include "session-config.h"
44 #include "config-internal.h"
46 struct handler_filter_args
{
48 config_entry_handler_cb handler
;
52 struct session_config_validation_ctx
{
53 xmlSchemaParserCtxtPtr parser_ctx
;
55 xmlSchemaValidCtxtPtr schema_validation_ctx
;
58 const char * const config_str_yes
= "yes";
59 const char * const config_str_true
= "true";
60 const char * const config_str_on
= "on";
61 const char * const config_str_no
= "no";
62 const char * const config_str_false
= "false";
63 const char * const config_str_off
= "off";
64 const char * const config_xml_encoding
= "UTF-8";
65 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
66 const char * const config_xml_indent_string
= "\t";
67 const char * const config_xml_true
= "true";
68 const char * const config_xml_false
= "false";
70 const char * const config_element_channel
= "channel";
71 const char * const config_element_channels
= "channels";
72 const char * const config_element_domain
= "domain";
73 const char * const config_element_domains
= "domains";
74 const char * const config_element_event
= "event";
75 const char * const config_element_events
= "events";
76 const char * const config_element_context
= "context";
77 const char * const config_element_contexts
= "contexts";
78 const char * const config_element_attributes
= "attributes";
79 const char * const config_element_exclusion
= "exclusion";
80 const char * const config_element_exclusions
= "exclusions";
81 const char * const config_element_function_attributes
= "function_attributes";
82 const char * const config_element_probe_attributes
= "probe_attributes";
83 const char * const config_element_symbol_name
= "symbol_name";
84 const char * const config_element_address
= "address";
85 const char * const config_element_offset
= "offset";
86 const char * const config_element_name
= "name";
87 const char * const config_element_enabled
= "enabled";
88 const char * const config_element_overwrite_mode
= "overwrite_mode";
89 const char * const config_element_subbuf_size
= "subbuffer_size";
90 const char * const config_element_num_subbuf
= "subbuffer_count";
91 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
92 const char * const config_element_read_timer_interval
= "read_timer_interval";
93 const char * const config_element_output
= "output";
94 const char * const config_element_output_type
= "output_type";
95 const char * const config_element_tracefile_size
= "tracefile_size";
96 const char * const config_element_tracefile_count
= "tracefile_count";
97 const char * const config_element_live_timer_interval
= "live_timer_interval";
98 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
99 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
100 const char * const config_element_type
= "type";
101 const char * const config_element_buffer_type
= "buffer_type";
102 const char * const config_element_session
= "session";
103 const char * const config_element_sessions
= "sessions";
104 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
105 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
106 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
107 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
108 const char * const config_element_config
= "config";
109 const char * const config_element_started
= "started";
110 const char * const config_element_snapshot_mode
= "snapshot_mode";
111 const char * const config_element_loglevel
= "loglevel";
112 const char * const config_element_loglevel_type
= "loglevel_type";
113 const char * const config_element_filter
= "filter";
114 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
115 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
116 const char * const config_element_consumer_output
= "consumer_output";
117 const char * const config_element_destination
= "destination";
118 const char * const config_element_path
= "path";
119 const char * const config_element_net_output
= "net_output";
120 const char * const config_element_control_uri
= "control_uri";
121 const char * const config_element_data_uri
= "data_uri";
122 const char * const config_element_max_size
= "max_size";
123 const char * const config_element_pid
= "pid";
124 const char * const config_element_pids
= "pids";
125 const char * const config_element_shared_memory_path
= "shared_memory_path";
126 const char * const config_element_pid_tracker
= "pid_tracker";
127 const char * const config_element_trackers
= "trackers";
128 const char * const config_element_targets
= "targets";
129 const char * const config_element_target_pid
= "pid_target";
131 const char * const config_domain_type_kernel
= "KERNEL";
132 const char * const config_domain_type_ust
= "UST";
133 const char * const config_domain_type_jul
= "JUL";
134 const char * const config_domain_type_log4j
= "LOG4J";
135 const char * const config_domain_type_python
= "PYTHON";
137 const char * const config_buffer_type_per_pid
= "PER_PID";
138 const char * const config_buffer_type_per_uid
= "PER_UID";
139 const char * const config_buffer_type_global
= "GLOBAL";
141 const char * const config_overwrite_mode_discard
= "DISCARD";
142 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
144 const char * const config_output_type_splice
= "SPLICE";
145 const char * const config_output_type_mmap
= "MMAP";
147 const char * const config_loglevel_type_all
= "ALL";
148 const char * const config_loglevel_type_range
= "RANGE";
149 const char * const config_loglevel_type_single
= "SINGLE";
151 const char * const config_event_type_all
= "ALL";
152 const char * const config_event_type_tracepoint
= "TRACEPOINT";
153 const char * const config_event_type_probe
= "PROBE";
154 const char * const config_event_type_function
= "FUNCTION";
155 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
156 const char * const config_event_type_noop
= "NOOP";
157 const char * const config_event_type_syscall
= "SYSCALL";
158 const char * const config_event_type_kprobe
= "KPROBE";
159 const char * const config_event_type_kretprobe
= "KRETPROBE";
161 const char * const config_event_context_pid
= "PID";
162 const char * const config_event_context_procname
= "PROCNAME";
163 const char * const config_event_context_prio
= "PRIO";
164 const char * const config_event_context_nice
= "NICE";
165 const char * const config_event_context_vpid
= "VPID";
166 const char * const config_event_context_tid
= "TID";
167 const char * const config_event_context_vtid
= "VTID";
168 const char * const config_event_context_ppid
= "PPID";
169 const char * const config_event_context_vppid
= "VPPID";
170 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
171 const char * const config_event_context_hostname
= "HOSTNAME";
172 const char * const config_event_context_ip
= "IP";
173 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
174 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
175 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
176 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
177 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
178 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
180 struct consumer_output
{
187 static int config_entry_handler_filter(struct handler_filter_args
*args
,
188 const char *section
, const char *name
, const char *value
)
191 struct config_entry entry
= { section
, name
, value
};
195 if (!section
|| !name
|| !value
) {
201 if (strcmp(args
->section
, section
)) {
206 ret
= args
->handler(&entry
, args
->user_data
);
212 int config_get_section_entries(const char *override_path
, const char *section
,
213 config_entry_handler_cb handler
, void *user_data
)
217 FILE *config_file
= NULL
;
218 struct handler_filter_args filter
= { section
, handler
, user_data
};
220 /* First, try system-wide conf. file. */
221 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
223 config_file
= fopen(path
, "r");
225 DBG("Loading daemon conf file at %s", path
);
227 * Return value is not very important here since error or not, we
228 * continue and try the next possible conf. file.
230 (void) ini_parse_file(config_file
,
231 (ini_entry_handler
) config_entry_handler_filter
,
236 /* Second is the user local configuration. */
237 path
= utils_get_home_dir();
239 char fullpath
[PATH_MAX
];
241 ret
= snprintf(fullpath
, sizeof(fullpath
),
242 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
244 PERROR("snprintf user conf. path");
248 config_file
= fopen(fullpath
, "r");
250 DBG("Loading daemon user conf file at %s", path
);
252 * Return value is not very important here since error or not, we
253 * continue and try the next possible conf. file.
255 (void) ini_parse_file(config_file
,
256 (ini_entry_handler
) config_entry_handler_filter
,
262 /* Final path is the one that the user might have provided. */
264 config_file
= fopen(override_path
, "r");
266 DBG("Loading daemon command line conf file at %s", override_path
);
267 (void) ini_parse_file(config_file
,
268 (ini_entry_handler
) config_entry_handler_filter
,
272 ERR("Failed to open daemon configuration file at %s",
279 /* Everything went well. */
287 int config_parse_value(const char *value
)
290 char *endptr
, *lower_str
;
300 v
= strtoul(value
, &endptr
, 10);
301 if (endptr
!= value
) {
306 lower_str
= zmalloc(len
+ 1);
313 for (i
= 0; i
< len
; i
++) {
314 lower_str
[i
] = tolower(value
[i
]);
317 if (!strcmp(lower_str
, config_str_yes
) ||
318 !strcmp(lower_str
, config_str_true
) ||
319 !strcmp(lower_str
, config_str_on
)) {
321 } else if (!strcmp(lower_str
, config_str_no
) ||
322 !strcmp(lower_str
, config_str_false
) ||
323 !strcmp(lower_str
, config_str_off
)) {
335 * Returns a xmlChar string which must be released using xmlFree().
337 static xmlChar
*encode_string(const char *in_str
)
339 xmlChar
*out_str
= NULL
;
340 xmlCharEncodingHandlerPtr handler
;
341 int out_len
, ret
, in_len
;
345 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
347 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
351 in_len
= strlen(in_str
);
353 * Add 1 byte for the NULL terminted character. The factor 4 here is
354 * used because UTF-8 characters can take up to 4 bytes.
356 out_len
= (in_len
* 4) + 1;
357 out_str
= xmlMalloc(out_len
);
362 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
369 /* out_len is now the size of out_str */
370 out_str
[out_len
] = '\0';
376 struct config_writer
*config_writer_create(int fd_output
, int indent
)
379 struct config_writer
*writer
;
380 xmlOutputBufferPtr buffer
;
382 writer
= zmalloc(sizeof(struct config_writer
));
384 PERROR("zmalloc config_writer_create");
388 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
393 writer
->writer
= xmlNewTextWriter(buffer
);
394 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
395 config_xml_encoding
, NULL
);
400 ret
= xmlTextWriterSetIndentString(writer
->writer
,
401 BAD_CAST config_xml_indent_string
);
406 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
414 config_writer_destroy(writer
);
419 int config_writer_destroy(struct config_writer
*writer
)
428 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
429 WARN("Could not close XML document");
433 if (writer
->writer
) {
434 xmlFreeTextWriter(writer
->writer
);
443 int config_writer_open_element(struct config_writer
*writer
,
444 const char *element_name
)
447 xmlChar
*encoded_element_name
;
449 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
454 encoded_element_name
= encode_string(element_name
);
455 if (!encoded_element_name
) {
460 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
461 xmlFree(encoded_element_name
);
463 return ret
>= 0 ? 0 : ret
;
467 int config_writer_write_attribute(struct config_writer
*writer
,
468 const char *name
, const char *value
)
471 xmlChar
*encoded_name
= NULL
;
472 xmlChar
*encoded_value
= NULL
;
474 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
479 encoded_name
= encode_string(name
);
485 encoded_value
= encode_string(value
);
486 if (!encoded_value
) {
491 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
494 xmlFree(encoded_name
);
495 xmlFree(encoded_value
);
496 return ret
>= 0 ? 0 : ret
;
500 int config_writer_close_element(struct config_writer
*writer
)
504 if (!writer
|| !writer
->writer
) {
509 ret
= xmlTextWriterEndElement(writer
->writer
);
511 return ret
>= 0 ? 0 : ret
;
515 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
516 const char *element_name
, uint64_t value
)
519 xmlChar
*encoded_element_name
;
521 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
526 encoded_element_name
= encode_string(element_name
);
527 if (!encoded_element_name
) {
532 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
533 encoded_element_name
, "%" PRIu64
, value
);
534 xmlFree(encoded_element_name
);
536 return ret
>= 0 ? 0 : ret
;
540 int config_writer_write_element_signed_int(struct config_writer
*writer
,
541 const char *element_name
, int64_t value
)
544 xmlChar
*encoded_element_name
;
546 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
551 encoded_element_name
= encode_string(element_name
);
552 if (!encoded_element_name
) {
557 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
558 encoded_element_name
, "%" PRIi64
, value
);
559 xmlFree(encoded_element_name
);
561 return ret
>= 0 ? 0 : ret
;
565 int config_writer_write_element_bool(struct config_writer
*writer
,
566 const char *element_name
, int value
)
568 return config_writer_write_element_string(writer
, element_name
,
569 value
? config_xml_true
: config_xml_false
);
573 int config_writer_write_element_string(struct config_writer
*writer
,
574 const char *element_name
, const char *value
)
577 xmlChar
*encoded_element_name
= NULL
;
578 xmlChar
*encoded_value
= NULL
;
580 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
586 encoded_element_name
= encode_string(element_name
);
587 if (!encoded_element_name
) {
592 encoded_value
= encode_string(value
);
593 if (!encoded_value
) {
598 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
601 xmlFree(encoded_element_name
);
602 xmlFree(encoded_value
);
603 return ret
>= 0 ? 0 : ret
;
607 void xml_error_handler(void *ctx
, const char *format
, ...)
613 va_start(args
, format
);
614 ret
= vasprintf(&errMsg
, format
, args
);
617 ERR("String allocation failed in xml error handler");
621 fprintf(stderr
, "XML Error: %s", errMsg
);
626 void fini_session_config_validation_ctx(
627 struct session_config_validation_ctx
*ctx
)
629 if (ctx
->parser_ctx
) {
630 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
634 xmlSchemaFree(ctx
->schema
);
637 if (ctx
->schema_validation_ctx
) {
638 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
641 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
645 char *get_session_config_xsd_path()
648 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
649 size_t base_path_len
;
653 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
656 base_path_len
= strlen(base_path
);
657 max_path_len
= base_path_len
+
658 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
659 xsd_path
= zmalloc(max_path_len
);
664 strncpy(xsd_path
, base_path
, max_path_len
);
665 if (xsd_path
[base_path_len
- 1] != '/') {
666 xsd_path
[base_path_len
++] = '/';
669 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
670 max_path_len
- base_path_len
);
676 int init_session_config_validation_ctx(
677 struct session_config_validation_ctx
*ctx
)
680 char *xsd_path
= get_session_config_xsd_path();
683 ret
= -LTTNG_ERR_NOMEM
;
687 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
688 if (!ctx
->parser_ctx
) {
689 ERR("XSD parser context creation failed");
690 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
693 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
694 xml_error_handler
, NULL
);
696 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
698 ERR("XSD parsing failed");
699 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
703 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
704 if (!ctx
->schema_validation_ctx
) {
705 ERR("XSD validation context creation failed");
706 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
710 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
711 xml_error_handler
, NULL
);
716 fini_session_config_validation_ctx(ctx
);
724 int parse_uint(xmlChar
*str
, uint64_t *val
)
734 *val
= strtoull((const char *) str
, &endptr
, 10);
735 if (!endptr
|| *endptr
) {
746 int parse_int(xmlChar
*str
, int64_t *val
)
756 *val
= strtoll((const char *) str
, &endptr
, 10);
757 if (!endptr
|| *endptr
) {
768 int parse_bool(xmlChar
*str
, int *val
)
777 if (!strcmp((const char *) str
, config_xml_true
)) {
779 } else if (!strcmp((const char *) str
, config_xml_false
)) {
782 WARN("Invalid boolean value encoutered (%s).",
791 int get_domain_type(xmlChar
*domain
)
799 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
800 ret
= LTTNG_DOMAIN_KERNEL
;
801 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
802 ret
= LTTNG_DOMAIN_UST
;
803 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
804 ret
= LTTNG_DOMAIN_JUL
;
805 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
806 ret
= LTTNG_DOMAIN_LOG4J
;
807 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
808 ret
= LTTNG_DOMAIN_PYTHON
;
819 int get_buffer_type(xmlChar
*buffer_type
)
827 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
828 ret
= LTTNG_BUFFER_GLOBAL
;
829 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
830 ret
= LTTNG_BUFFER_PER_UID
;
831 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
832 ret
= LTTNG_BUFFER_PER_PID
;
843 int get_overwrite_mode(xmlChar
*overwrite_mode
)
847 if (!overwrite_mode
) {
851 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
853 } else if (!strcmp((char *) overwrite_mode
,
854 config_overwrite_mode_discard
)) {
866 int get_output_type(xmlChar
*output_type
)
874 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
875 ret
= LTTNG_EVENT_MMAP
;
876 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
877 ret
= LTTNG_EVENT_SPLICE
;
888 int get_event_type(xmlChar
*event_type
)
896 if (!strcmp((char *) event_type
, config_event_type_all
)) {
897 ret
= LTTNG_EVENT_ALL
;
898 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
899 ret
= LTTNG_EVENT_TRACEPOINT
;
900 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
901 ret
= LTTNG_EVENT_PROBE
;
902 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
903 ret
= LTTNG_EVENT_FUNCTION
;
904 } else if (!strcmp((char *) event_type
,
905 config_event_type_function_entry
)) {
906 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
907 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
908 ret
= LTTNG_EVENT_NOOP
;
909 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
910 ret
= LTTNG_EVENT_SYSCALL
;
921 int get_loglevel_type(xmlChar
*loglevel_type
)
925 if (!loglevel_type
) {
929 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
930 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
931 } else if (!strcmp((char *) loglevel_type
,
932 config_loglevel_type_range
)) {
933 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
934 } else if (!strcmp((char *) loglevel_type
,
935 config_loglevel_type_single
)) {
936 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
947 * Return the context type or -1 on error.
950 int get_context_type(xmlChar
*context_type
)
958 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
959 ret
= LTTNG_EVENT_CONTEXT_PID
;
960 } else if (!strcmp((char *) context_type
,
961 config_event_context_procname
)) {
962 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
963 } else if (!strcmp((char *) context_type
,
964 config_event_context_prio
)) {
965 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
966 } else if (!strcmp((char *) context_type
,
967 config_event_context_nice
)) {
968 ret
= LTTNG_EVENT_CONTEXT_NICE
;
969 } else if (!strcmp((char *) context_type
,
970 config_event_context_vpid
)) {
971 ret
= LTTNG_EVENT_CONTEXT_VPID
;
972 } else if (!strcmp((char *) context_type
,
973 config_event_context_tid
)) {
974 ret
= LTTNG_EVENT_CONTEXT_TID
;
975 } else if (!strcmp((char *) context_type
,
976 config_event_context_vtid
)) {
977 ret
= LTTNG_EVENT_CONTEXT_VTID
;
978 } else if (!strcmp((char *) context_type
,
979 config_event_context_ppid
)) {
980 ret
= LTTNG_EVENT_CONTEXT_PPID
;
981 } else if (!strcmp((char *) context_type
,
982 config_event_context_vppid
)) {
983 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
984 } else if (!strcmp((char *) context_type
,
985 config_event_context_pthread_id
)) {
986 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
987 } else if (!strcmp((char *) context_type
,
988 config_event_context_hostname
)) {
989 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
990 } else if (!strcmp((char *) context_type
,
991 config_event_context_ip
)) {
992 ret
= LTTNG_EVENT_CONTEXT_IP
;
993 } else if (!strcmp((char *) context_type
,
994 config_event_context_interruptible
)) {
995 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
996 } else if (!strcmp((char *) context_type
,
997 config_event_context_preemptible
)) {
998 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
999 } else if (!strcmp((char *) context_type
,
1000 config_event_context_need_reschedule
)) {
1001 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1002 } else if (!strcmp((char *) context_type
,
1003 config_event_context_migratable
)) {
1004 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1015 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1020 for (node
= xmlFirstElementChild(domain_node
); node
;
1021 node
= xmlNextElementSibling(node
)) {
1022 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1024 xmlChar
*node_content
= xmlNodeGetContent(node
);
1025 if (!node_content
) {
1026 ret
= -LTTNG_ERR_NOMEM
;
1030 ret
= get_domain_type(node_content
);
1033 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1038 } else if (!strcmp((const char *) node
->name
,
1039 config_element_buffer_type
)) {
1041 xmlChar
*node_content
= xmlNodeGetContent(node
);
1042 if (!node_content
) {
1043 ret
= -LTTNG_ERR_NOMEM
;
1047 ret
= get_buffer_type(node_content
);
1050 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1054 domain
->buf_type
= ret
;
1063 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1068 for (node
= xmlFirstElementChild(net_output_node
); node
;
1069 node
= xmlNextElementSibling(node
)) {
1070 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1072 *control_uri
= (char *) xmlNodeGetContent(node
);
1073 if (!*control_uri
) {
1078 *data_uri
= (char *) xmlNodeGetContent(node
);
1085 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1089 int process_consumer_output(xmlNodePtr consumer_output_node
,
1090 struct consumer_output
*output
)
1097 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1098 node
= xmlNextElementSibling(node
)) {
1099 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1100 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1104 ret
= -LTTNG_ERR_NOMEM
;
1108 ret
= parse_bool(enabled_str
, &output
->enabled
);
1114 xmlNodePtr output_type_node
;
1117 output_type_node
= xmlFirstElementChild(node
);
1118 if (!output_type_node
) {
1119 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1123 if (!strcmp((const char *) output_type_node
->name
,
1124 config_element_path
)) {
1126 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1127 if (!output
->path
) {
1128 ret
= -LTTNG_ERR_NOMEM
;
1133 ret
= get_net_output_uris(output_type_node
,
1134 &output
->control_uri
, &output
->data_uri
);
1146 free(output
->control_uri
);
1147 free(output
->data_uri
);
1148 memset(output
, 0, sizeof(struct consumer_output
));
1154 int create_session_net_output(const char *name
, const char *control_uri
,
1155 const char *data_uri
)
1158 struct lttng_handle
*handle
;
1159 const char *uri
= NULL
;
1163 handle
= lttng_create_handle(name
, NULL
);
1165 ret
= -LTTNG_ERR_NOMEM
;
1169 if (!control_uri
|| !data_uri
) {
1170 uri
= control_uri
? control_uri
: data_uri
;
1175 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1176 lttng_destroy_handle(handle
);
1182 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1185 xmlNodePtr node
= NULL
;
1186 xmlNodePtr snapshot_output_list_node
;
1187 xmlNodePtr snapshot_output_node
;
1189 assert(session_name
);
1191 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1200 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1202 /* Parse and create snapshot outputs */
1204 for (snapshot_output_node
=
1205 xmlFirstElementChild(snapshot_output_list_node
);
1206 snapshot_output_node
; snapshot_output_node
=
1207 xmlNextElementSibling(snapshot_output_node
)) {
1209 uint64_t max_size
= UINT64_MAX
;
1210 struct consumer_output output
= { 0 };
1211 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1213 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1214 node
= xmlNextElementSibling(node
)) {
1215 if (!strcmp((const char *) node
->name
,
1216 config_element_name
)) {
1218 name
= (char *) xmlNodeGetContent(node
);
1220 ret
= -LTTNG_ERR_NOMEM
;
1221 goto error_snapshot_output
;
1223 } else if (!strcmp((const char *) node
->name
,
1224 config_element_max_size
)) {
1225 xmlChar
*content
= xmlNodeGetContent(node
);
1229 ret
= -LTTNG_ERR_NOMEM
;
1230 goto error_snapshot_output
;
1232 ret
= parse_uint(content
, &max_size
);
1235 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1236 goto error_snapshot_output
;
1239 /* consumer_output */
1240 ret
= process_consumer_output(node
, &output
);
1242 goto error_snapshot_output
;
1247 snapshot_output
= lttng_snapshot_output_create();
1248 if (!snapshot_output
) {
1249 ret
= -LTTNG_ERR_NOMEM
;
1250 goto error_snapshot_output
;
1253 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1255 goto error_snapshot_output
;
1258 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1260 goto error_snapshot_output
;
1264 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1267 goto error_snapshot_output
;
1270 if (output
.control_uri
) {
1271 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1274 goto error_snapshot_output
;
1278 if (output
.data_uri
) {
1279 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1282 goto error_snapshot_output
;
1287 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1288 error_snapshot_output
:
1291 free(output
.control_uri
);
1292 free(output
.data_uri
);
1293 lttng_snapshot_output_destroy(snapshot_output
);
1303 int create_session(const char *name
,
1304 struct lttng_domain
*kernel_domain
,
1305 struct lttng_domain
*ust_domain
,
1306 struct lttng_domain
*jul_domain
,
1307 struct lttng_domain
*log4j_domain
,
1308 xmlNodePtr output_node
,
1309 uint64_t live_timer_interval
)
1312 struct consumer_output output
= { 0 };
1313 xmlNodePtr consumer_output_node
;
1318 consumer_output_node
= xmlFirstElementChild(output_node
);
1319 if (!consumer_output_node
) {
1320 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1324 if (strcmp((const char *) consumer_output_node
->name
,
1325 config_element_consumer_output
)) {
1326 WARN("Invalid output type, expected %s node",
1327 config_element_consumer_output
);
1328 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1332 ret
= process_consumer_output(consumer_output_node
, &output
);
1338 if (live_timer_interval
!= UINT64_MAX
&&
1339 !output
.control_uri
&& !output
.data_uri
) {
1340 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1344 if (output
.control_uri
|| output
.data_uri
) {
1345 /* network destination */
1346 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1348 * URLs are provided for sure since the test above make sure that
1349 * with a live timer the data and control URIs are provided. So,
1350 * NULL is passed here and will be set right after.
1352 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1354 ret
= lttng_create_session(name
, NULL
);
1360 ret
= create_session_net_output(name
, output
.control_uri
,
1367 /* either local output or no output */
1368 ret
= lttng_create_session(name
, output
.path
);
1375 free(output
.control_uri
);
1376 free(output
.data_uri
);
1380 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1381 struct lttng_event_probe_attr
*attr
)
1385 assert(probe_attribute_node
);
1388 if (!strcmp((const char *) probe_attribute_node
->name
,
1389 config_element_address
)) {
1394 content
= xmlNodeGetContent(probe_attribute_node
);
1396 ret
= -LTTNG_ERR_NOMEM
;
1400 ret
= parse_uint(content
, &addr
);
1403 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1408 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1409 config_element_offset
)) {
1411 uint64_t offset
= 0;
1414 content
= xmlNodeGetContent(probe_attribute_node
);
1416 ret
= -LTTNG_ERR_NOMEM
;
1420 ret
= parse_uint(content
, &offset
);
1423 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1427 attr
->offset
= offset
;
1428 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1429 config_element_symbol_name
)) {
1434 content
= xmlNodeGetContent(probe_attribute_node
);
1436 ret
= -LTTNG_ERR_NOMEM
;
1440 name_len
= strlen((char *) content
);
1441 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1442 WARN("symbol_name too long.");
1443 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1448 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1457 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1458 const char *channel_name
)
1462 struct lttng_event event
;
1463 char **exclusions
= NULL
;
1464 unsigned long exclusion_count
= 0;
1465 char *filter_expression
= NULL
;
1469 assert(channel_name
);
1471 memset(&event
, 0, sizeof(event
));
1473 /* Initialize default log level which varies by domain */
1474 switch (handle
->domain
.type
)
1476 case LTTNG_DOMAIN_JUL
:
1477 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1479 case LTTNG_DOMAIN_LOG4J
:
1480 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1482 case LTTNG_DOMAIN_PYTHON
:
1483 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1485 case LTTNG_DOMAIN_UST
:
1486 case LTTNG_DOMAIN_KERNEL
:
1487 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1493 for (node
= xmlFirstElementChild(event_node
); node
;
1494 node
= xmlNextElementSibling(node
)) {
1495 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1500 content
= xmlNodeGetContent(node
);
1502 ret
= -LTTNG_ERR_NOMEM
;
1506 name_len
= strlen((char *) content
);
1507 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1508 WARN("Channel name too long.");
1509 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1514 strncpy(event
.name
, (const char *) content
, name_len
);
1516 } else if (!strcmp((const char *) node
->name
,
1517 config_element_enabled
)) {
1518 xmlChar
*content
= xmlNodeGetContent(node
);
1522 ret
= -LTTNG_ERR_NOMEM
;
1526 ret
= parse_bool(content
, &event
.enabled
);
1529 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1532 } else if (!strcmp((const char *) node
->name
,
1533 config_element_type
)) {
1534 xmlChar
*content
= xmlNodeGetContent(node
);
1538 ret
= -LTTNG_ERR_NOMEM
;
1542 ret
= get_event_type(content
);
1545 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1550 } else if (!strcmp((const char *) node
->name
,
1551 config_element_loglevel_type
)) {
1552 xmlChar
*content
= xmlNodeGetContent(node
);
1556 ret
= -LTTNG_ERR_NOMEM
;
1560 ret
= get_loglevel_type(content
);
1563 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1567 event
.loglevel_type
= ret
;
1568 } else if (!strcmp((const char *) node
->name
,
1569 config_element_loglevel
)) {
1571 int64_t loglevel
= 0;
1574 content
= xmlNodeGetContent(node
);
1576 ret
= -LTTNG_ERR_NOMEM
;
1580 ret
= parse_int(content
, &loglevel
);
1583 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1587 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1588 WARN("loglevel out of range.");
1589 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1593 event
.loglevel
= loglevel
;
1594 } else if (!strcmp((const char *) node
->name
,
1595 config_element_filter
)) {
1597 xmlNodeGetContent(node
);
1601 ret
= -LTTNG_ERR_NOMEM
;
1605 filter_expression
= strdup((char *) content
);
1607 if (!filter_expression
) {
1608 ret
= -LTTNG_ERR_NOMEM
;
1611 } else if (!strcmp((const char *) node
->name
,
1612 config_element_exclusions
)) {
1613 xmlNodePtr exclusion_node
;
1614 int exclusion_index
= 0;
1619 * Exclusions has already been initialized,
1622 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1626 exclusion_count
= xmlChildElementCount(node
);
1627 if (!exclusion_count
) {
1631 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1633 exclusion_count
= 0;
1634 ret
= -LTTNG_ERR_NOMEM
;
1638 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1639 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1641 xmlNodeGetContent(exclusion_node
);
1644 ret
= -LTTNG_ERR_NOMEM
;
1648 exclusions
[exclusion_index
] = strdup((const char *) content
);
1650 if (!exclusions
[exclusion_index
]) {
1651 ret
= -LTTNG_ERR_NOMEM
;
1657 event
.exclusion
= 1;
1658 } else if (!strcmp((const char *) node
->name
,
1659 config_element_attributes
)) {
1660 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1663 if (!attribute_node
) {
1664 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1668 if (!strcmp((const char *) node
->name
,
1669 config_element_probe_attributes
)) {
1670 xmlNodePtr probe_attribute_node
;
1672 /* probe_attributes */
1673 for (probe_attribute_node
=
1674 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1675 probe_attribute_node
= xmlNextElementSibling(
1676 probe_attribute_node
)) {
1678 ret
= process_probe_attribute_node(probe_attribute_node
,
1687 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1689 /* function_attributes */
1690 content
= xmlNodeGetContent(symbol_node
);
1692 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1696 sym_len
= strlen((char *) content
);
1697 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1698 WARN("Function name too long.");
1699 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1704 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1711 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1712 filter_expression
, exclusion_count
, exclusions
);
1717 if (!event
.enabled
) {
1719 * Note that we should use lttng_disable_event_ext() (2.6+) to
1720 * eliminate the risk of clashing on events of the same
1721 * name (with different event types and loglevels).
1723 * Unfortunately, lttng_disable_event_ext() only performs a
1724 * match on the name and event type and errors out if any other
1725 * event attribute is not set to its default value.
1727 * This will disable all events that match this name.
1729 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1732 for (i
= 0; i
< exclusion_count
; i
++) {
1733 free(exclusions
[i
]);
1737 free(filter_expression
);
1742 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1743 const char *channel_name
)
1748 assert(events_node
);
1750 assert(channel_name
);
1752 for (node
= xmlFirstElementChild(events_node
); node
;
1753 node
= xmlNextElementSibling(node
)) {
1754 ret
= process_event_node(node
, handle
, channel_name
);
1764 int process_channel_attr_node(xmlNodePtr attr_node
,
1765 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1766 xmlNodePtr
*events_node
)
1772 assert(contexts_node
);
1773 assert(events_node
);
1775 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1780 content
= xmlNodeGetContent(attr_node
);
1782 ret
= -LTTNG_ERR_NOMEM
;
1786 name_len
= strlen((char *) content
);
1787 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1788 WARN("Channel name too long.");
1789 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1794 strncpy(channel
->name
, (const char *) content
, name_len
);
1796 } else if (!strcmp((const char *) attr_node
->name
,
1797 config_element_enabled
)) {
1802 content
= xmlNodeGetContent(attr_node
);
1804 ret
= -LTTNG_ERR_NOMEM
;
1808 ret
= parse_bool(content
, &enabled
);
1811 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1815 channel
->enabled
= enabled
;
1816 } else if (!strcmp((const char *) attr_node
->name
,
1817 config_element_overwrite_mode
)) {
1820 /* overwrite_mode */
1821 content
= xmlNodeGetContent(attr_node
);
1823 ret
= -LTTNG_ERR_NOMEM
;
1827 ret
= get_overwrite_mode(content
);
1830 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1834 channel
->attr
.overwrite
= ret
;
1835 } else if (!strcmp((const char *) attr_node
->name
,
1836 config_element_subbuf_size
)) {
1839 /* subbuffer_size */
1840 content
= xmlNodeGetContent(attr_node
);
1842 ret
= -LTTNG_ERR_NOMEM
;
1846 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1849 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 } else if (!strcmp((const char *) attr_node
->name
,
1853 config_element_num_subbuf
)) {
1856 /* subbuffer_count */
1857 content
= xmlNodeGetContent(attr_node
);
1859 ret
= -LTTNG_ERR_NOMEM
;
1863 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1866 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1869 } else if (!strcmp((const char *) attr_node
->name
,
1870 config_element_switch_timer_interval
)) {
1872 uint64_t switch_timer_interval
= 0;
1874 /* switch_timer_interval */
1875 content
= xmlNodeGetContent(attr_node
);
1877 ret
= -LTTNG_ERR_NOMEM
;
1881 ret
= parse_uint(content
, &switch_timer_interval
);
1884 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1888 if (switch_timer_interval
> UINT_MAX
) {
1889 WARN("switch_timer_interval out of range.");
1890 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1894 channel
->attr
.switch_timer_interval
=
1895 switch_timer_interval
;
1896 } else if (!strcmp((const char *) attr_node
->name
,
1897 config_element_read_timer_interval
)) {
1899 uint64_t read_timer_interval
= 0;
1901 /* read_timer_interval */
1902 content
= xmlNodeGetContent(attr_node
);
1904 ret
= -LTTNG_ERR_NOMEM
;
1908 ret
= parse_uint(content
, &read_timer_interval
);
1911 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1915 if (read_timer_interval
> UINT_MAX
) {
1916 WARN("read_timer_interval out of range.");
1917 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1921 channel
->attr
.read_timer_interval
=
1922 read_timer_interval
;
1923 } else if (!strcmp((const char *) attr_node
->name
,
1924 config_element_output_type
)) {
1928 content
= xmlNodeGetContent(attr_node
);
1930 ret
= -LTTNG_ERR_NOMEM
;
1934 ret
= get_output_type(content
);
1937 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1941 channel
->attr
.output
= ret
;
1942 } else if (!strcmp((const char *) attr_node
->name
,
1943 config_element_tracefile_size
)) {
1946 /* tracefile_size */
1947 content
= xmlNodeGetContent(attr_node
);
1949 ret
= -LTTNG_ERR_NOMEM
;
1953 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1956 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1959 } else if (!strcmp((const char *) attr_node
->name
,
1960 config_element_tracefile_count
)) {
1963 /* tracefile_count */
1964 content
= xmlNodeGetContent(attr_node
);
1966 ret
= -LTTNG_ERR_NOMEM
;
1970 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1973 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1976 } else if (!strcmp((const char *) attr_node
->name
,
1977 config_element_live_timer_interval
)) {
1979 uint64_t live_timer_interval
= 0;
1981 /* live_timer_interval */
1982 content
= xmlNodeGetContent(attr_node
);
1984 ret
= -LTTNG_ERR_NOMEM
;
1988 ret
= parse_uint(content
, &live_timer_interval
);
1991 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1995 if (live_timer_interval
> UINT_MAX
) {
1996 WARN("live_timer_interval out of range.");
1997 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2001 channel
->attr
.live_timer_interval
=
2002 live_timer_interval
;
2003 } else if (!strcmp((const char *) attr_node
->name
,
2004 config_element_events
)) {
2006 *events_node
= attr_node
;
2009 *contexts_node
= attr_node
;
2017 int process_context_node(xmlNodePtr context_node
,
2018 struct lttng_handle
*handle
, const char *channel_name
)
2021 struct lttng_event_context context
;
2022 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2025 assert(channel_name
);
2027 if (!context_child_node
) {
2028 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2032 memset(&context
, 0, sizeof(context
));
2034 if (!strcmp((const char *) context_child_node
->name
,
2035 config_element_type
)) {
2037 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2040 ret
= -LTTNG_ERR_NOMEM
;
2044 ret
= get_context_type(content
);
2047 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2052 } else if (!strcmp((const char *) context_child_node
->name
,
2053 config_element_context_perf
)) {
2055 xmlNodePtr perf_attr_node
;
2057 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2058 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2059 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2060 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2061 perf_attr_node
; perf_attr_node
=
2062 xmlNextElementSibling(perf_attr_node
)) {
2063 if (!strcmp((const char *) perf_attr_node
->name
,
2064 config_element_type
)) {
2069 content
= xmlNodeGetContent(perf_attr_node
);
2071 ret
= -LTTNG_ERR_NOMEM
;
2075 ret
= parse_uint(content
, &type
);
2078 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2082 if (type
> UINT32_MAX
) {
2083 WARN("perf context type out of range.");
2084 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2088 context
.u
.perf_counter
.type
= type
;
2089 } else if (!strcmp((const char *) perf_attr_node
->name
,
2090 config_element_config
)) {
2092 uint64_t config
= 0;
2095 content
= xmlNodeGetContent(perf_attr_node
);
2097 ret
= -LTTNG_ERR_NOMEM
;
2101 ret
= parse_uint(content
, &config
);
2104 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2108 context
.u
.perf_counter
.config
= config
;
2109 } else if (!strcmp((const char *) perf_attr_node
->name
,
2110 config_element_name
)) {
2115 content
= xmlNodeGetContent(perf_attr_node
);
2117 ret
= -LTTNG_ERR_NOMEM
;
2121 name_len
= strlen((char *) content
);
2122 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2123 WARN("perf context name too long.");
2124 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2129 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2134 } else if (!strcmp((const char *) context_child_node
->name
,
2135 config_element_context_app
)) {
2136 /* application context */
2137 xmlNodePtr app_ctx_node
;
2139 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2140 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2141 app_ctx_node
; app_ctx_node
=
2142 xmlNextElementSibling(app_ctx_node
)) {
2144 char **target
= strcmp(
2145 (const char *) app_ctx_node
->name
,
2146 config_element_context_app_provider_name
) == 0 ?
2147 &context
.u
.app_ctx
.provider_name
:
2148 &context
.u
.app_ctx
.ctx_name
;
2150 content
= xmlNodeGetContent(app_ctx_node
);
2152 ret
= -LTTNG_ERR_NOMEM
;
2156 *target
= (char *) content
;
2159 /* Unrecognized context type */
2160 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2164 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2165 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2166 free(context
.u
.app_ctx
.provider_name
);
2167 free(context
.u
.app_ctx
.ctx_name
);
2174 int process_contexts_node(xmlNodePtr contexts_node
,
2175 struct lttng_handle
*handle
, const char *channel_name
)
2178 xmlNodePtr context_node
;
2180 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2181 context_node
= xmlNextElementSibling(context_node
)) {
2182 ret
= process_context_node(context_node
, handle
, channel_name
);
2192 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2193 struct lttng_handle
*handle
)
2196 xmlNodePtr targets_node
= NULL
;
2200 assert(pid_tracker_node
);
2201 /* get the targets node */
2202 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2203 node
= xmlNextElementSibling(node
)) {
2204 if (!strcmp((const char *) node
->name
,
2205 config_element_targets
)) {
2206 targets_node
= node
;
2211 if (!targets_node
) {
2212 ret
= LTTNG_ERR_INVALID
;
2216 /* Go through all pid_target node */
2217 child
= xmlChildElementCount(targets_node
);
2219 /* The session is explicitly set to target nothing. */
2220 ret
= lttng_untrack_pid(handle
, -1);
2225 for (node
= xmlFirstElementChild(targets_node
); node
;
2226 node
= xmlNextElementSibling(node
)) {
2227 xmlNodePtr pid_target_node
= node
;
2229 /* get pid node and track it */
2230 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2231 node
= xmlNextElementSibling(node
)) {
2232 if (!strcmp((const char *) node
->name
,
2233 config_element_pid
)) {
2235 xmlChar
*content
= NULL
;
2237 content
= xmlNodeGetContent(node
);
2239 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2243 ret
= parse_int(content
, &pid
);
2246 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2250 ret
= lttng_track_pid(handle
, (int) pid
);
2256 node
= pid_target_node
;
2265 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2268 struct lttng_domain domain
= { 0 };
2269 struct lttng_handle
*handle
= NULL
;
2270 xmlNodePtr channels_node
= NULL
;
2271 xmlNodePtr trackers_node
= NULL
;
2272 xmlNodePtr pid_tracker_node
= NULL
;
2275 assert(session_name
);
2277 ret
= init_domain(domain_node
, &domain
);
2282 handle
= lttng_create_handle(session_name
, &domain
);
2284 ret
= -LTTNG_ERR_NOMEM
;
2288 /* get the channels node */
2289 for (node
= xmlFirstElementChild(domain_node
); node
;
2290 node
= xmlNextElementSibling(node
)) {
2291 if (!strcmp((const char *) node
->name
,
2292 config_element_channels
)) {
2293 channels_node
= node
;
2298 if (!channels_node
) {
2302 /* create all channels */
2303 for (node
= xmlFirstElementChild(channels_node
); node
;
2304 node
= xmlNextElementSibling(node
)) {
2305 struct lttng_channel channel
;
2306 xmlNodePtr contexts_node
= NULL
;
2307 xmlNodePtr events_node
= NULL
;
2308 xmlNodePtr channel_attr_node
;
2310 memset(&channel
, 0, sizeof(channel
));
2311 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2313 for (channel_attr_node
= xmlFirstElementChild(node
);
2314 channel_attr_node
; channel_attr_node
=
2315 xmlNextElementSibling(channel_attr_node
)) {
2316 ret
= process_channel_attr_node(channel_attr_node
,
2317 &channel
, &contexts_node
, &events_node
);
2323 ret
= lttng_enable_channel(handle
, &channel
);
2328 ret
= process_events_node(events_node
, handle
, channel
.name
);
2333 ret
= process_contexts_node(contexts_node
, handle
,
2340 /* get the trackers node */
2341 for (node
= xmlFirstElementChild(domain_node
); node
;
2342 node
= xmlNextElementSibling(node
)) {
2343 if (!strcmp((const char *) node
->name
,
2344 config_element_trackers
)) {
2345 trackers_node
= node
;
2350 if (!trackers_node
) {
2354 for (node
= xmlFirstElementChild(trackers_node
); node
;
2355 node
= xmlNextElementSibling(node
)) {
2356 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2357 pid_tracker_node
= node
;
2358 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2365 if (!pid_tracker_node
) {
2366 lttng_track_pid(handle
, -1);
2370 lttng_destroy_handle(handle
);
2375 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2378 int ret
, started
= -1, snapshot_mode
= -1;
2379 uint64_t live_timer_interval
= UINT64_MAX
;
2380 xmlChar
*name
= NULL
;
2381 xmlChar
*shm_path
= NULL
;
2382 xmlNodePtr domains_node
= NULL
;
2383 xmlNodePtr output_node
= NULL
;
2385 struct lttng_domain
*kernel_domain
= NULL
;
2386 struct lttng_domain
*ust_domain
= NULL
;
2387 struct lttng_domain
*jul_domain
= NULL
;
2388 struct lttng_domain
*log4j_domain
= NULL
;
2389 struct lttng_domain
*python_domain
= NULL
;
2391 for (node
= xmlFirstElementChild(session_node
); node
;
2392 node
= xmlNextElementSibling(node
)) {
2393 if (!name
&& !strcmp((const char *) node
->name
,
2394 config_element_name
)) {
2396 xmlChar
*node_content
= xmlNodeGetContent(node
);
2397 if (!node_content
) {
2398 ret
= -LTTNG_ERR_NOMEM
;
2402 name
= node_content
;
2403 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2404 config_element_domains
)) {
2406 domains_node
= node
;
2407 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2408 config_element_started
)) {
2410 xmlChar
*node_content
= xmlNodeGetContent(node
);
2411 if (!node_content
) {
2412 ret
= -LTTNG_ERR_NOMEM
;
2416 ret
= parse_bool(node_content
, &started
);
2419 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2422 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2423 config_element_output
)) {
2426 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2427 config_element_shared_memory_path
)) {
2428 /* shared memory path */
2429 xmlChar
*node_content
= xmlNodeGetContent(node
);
2430 if (!node_content
) {
2431 ret
= -LTTNG_ERR_NOMEM
;
2435 shm_path
= node_content
;
2437 /* attributes, snapshot_mode or live_timer_interval */
2438 xmlNodePtr attributes_child
=
2439 xmlFirstElementChild(node
);
2441 if (!strcmp((const char *) attributes_child
->name
,
2442 config_element_snapshot_mode
)) {
2444 xmlChar
*snapshot_mode_content
=
2445 xmlNodeGetContent(attributes_child
);
2446 if (!snapshot_mode_content
) {
2447 ret
= -LTTNG_ERR_NOMEM
;
2451 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2452 free(snapshot_mode_content
);
2454 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2458 /* live_timer_interval */
2459 xmlChar
*timer_interval_content
=
2460 xmlNodeGetContent(attributes_child
);
2461 if (!timer_interval_content
) {
2462 ret
= -LTTNG_ERR_NOMEM
;
2466 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2467 free(timer_interval_content
);
2469 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2477 /* Mandatory attribute, as defined in the session XSD */
2478 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2482 if (session_name
&& strcmp((char *) name
, session_name
)) {
2483 /* This is not the session we are looking for */
2484 ret
= -LTTNG_ERR_NO_SESSION
;
2488 /* Init domains to create the session handles */
2489 for (node
= xmlFirstElementChild(domains_node
); node
;
2490 node
= xmlNextElementSibling(node
)) {
2491 struct lttng_domain
*domain
;
2493 domain
= zmalloc(sizeof(*domain
));
2495 ret
= -LTTNG_ERR_NOMEM
;
2499 ret
= init_domain(node
, domain
);
2501 goto domain_init_error
;
2504 switch (domain
->type
) {
2505 case LTTNG_DOMAIN_KERNEL
:
2506 if (kernel_domain
) {
2507 /* Same domain seen twice, invalid! */
2508 goto domain_init_error
;
2510 kernel_domain
= domain
;
2512 case LTTNG_DOMAIN_UST
:
2514 /* Same domain seen twice, invalid! */
2515 goto domain_init_error
;
2517 ust_domain
= domain
;
2519 case LTTNG_DOMAIN_JUL
:
2521 /* Same domain seen twice, invalid! */
2522 goto domain_init_error
;
2524 jul_domain
= domain
;
2526 case LTTNG_DOMAIN_LOG4J
:
2528 /* Same domain seen twice, invalid! */
2529 goto domain_init_error
;
2531 log4j_domain
= domain
;
2533 case LTTNG_DOMAIN_PYTHON
:
2534 if (python_domain
) {
2535 /* Same domain seen twice, invalid! */
2536 goto domain_init_error
;
2538 python_domain
= domain
;
2541 WARN("Invalid domain type");
2542 goto domain_init_error
;
2547 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2552 /* Destroy session if it exists */
2553 ret
= lttng_destroy_session((const char *) name
);
2554 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2555 ERR("Failed to destroy existing session.");
2560 /* Create session type depending on output type */
2561 if (snapshot_mode
&& snapshot_mode
!= -1) {
2562 ret
= create_snapshot_session((const char *) name
, output_node
);
2563 } else if (live_timer_interval
&&
2564 live_timer_interval
!= UINT64_MAX
) {
2565 ret
= create_session((const char *) name
, kernel_domain
,
2566 ust_domain
, jul_domain
, log4j_domain
,
2567 output_node
, live_timer_interval
);
2569 /* regular session */
2570 ret
= create_session((const char *) name
, kernel_domain
,
2571 ust_domain
, jul_domain
, log4j_domain
,
2572 output_node
, UINT64_MAX
);
2579 ret
= lttng_set_session_shm_path((const char *) name
,
2580 (const char *) shm_path
);
2586 for (node
= xmlFirstElementChild(domains_node
); node
;
2587 node
= xmlNextElementSibling(node
)) {
2588 ret
= process_domain_node(node
, (const char *) name
);
2595 ret
= lttng_start_tracing((const char *) name
);
2603 ERR("Failed to load session %s: %s", (const char *) name
,
2604 lttng_strerror(ret
));
2605 lttng_destroy_session((const char *) name
);
2609 free(kernel_domain
);
2613 free(python_domain
);
2620 * Return 1 if the given path is readable by the current UID or 0 if not.
2621 * Return -1 if the path is EPERM.
2623 static int validate_file_read_creds(const char *path
)
2629 /* Can we read the file. */
2630 ret
= access(path
, R_OK
);
2634 if (errno
== EACCES
) {
2645 int load_session_from_file(const char *path
, const char *session_name
,
2646 struct session_config_validation_ctx
*validation_ctx
, int override
)
2648 int ret
, session_found
= !session_name
;
2649 xmlDocPtr doc
= NULL
;
2650 xmlNodePtr sessions_node
;
2651 xmlNodePtr session_node
;
2654 assert(validation_ctx
);
2656 ret
= validate_file_read_creds(path
);
2659 ret
= -LTTNG_ERR_EPERM
;
2661 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2666 doc
= xmlParseFile(path
);
2668 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2672 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2674 ERR("Session configuration file validation failed");
2675 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2679 sessions_node
= xmlDocGetRootElement(doc
);
2680 if (!sessions_node
) {
2684 for (session_node
= xmlFirstElementChild(sessions_node
);
2685 session_node
; session_node
=
2686 xmlNextElementSibling(session_node
)) {
2687 ret
= process_session_node(session_node
,
2688 session_name
, override
);
2689 if (session_name
&& ret
== 0) {
2690 /* Target session found and loaded */
2698 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2703 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2705 struct dirent
*alloc_dirent(const char *path
)
2709 struct dirent
*entry
;
2711 name_max
= pathconf(path
, _PC_NAME_MAX
);
2712 if (name_max
== -1) {
2713 name_max
= PATH_MAX
;
2715 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2716 entry
= zmalloc(len
);
2721 int load_session_from_path(const char *path
, const char *session_name
,
2722 struct session_config_validation_ctx
*validation_ctx
, int override
)
2724 int ret
, session_found
= !session_name
;
2725 DIR *directory
= NULL
;
2728 assert(validation_ctx
);
2730 directory
= opendir(path
);
2734 /* Try the file loading. */
2737 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2740 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2745 struct dirent
*entry
;
2746 struct dirent
*result
;
2747 char *file_path
= NULL
;
2748 size_t path_len
= strlen(path
);
2750 if (path_len
>= PATH_MAX
) {
2751 ret
= -LTTNG_ERR_INVALID
;
2755 entry
= alloc_dirent(path
);
2757 ret
= -LTTNG_ERR_NOMEM
;
2761 file_path
= zmalloc(PATH_MAX
);
2763 ret
= -LTTNG_ERR_NOMEM
;
2768 strncpy(file_path
, path
, path_len
);
2769 if (file_path
[path_len
- 1] != '/') {
2770 file_path
[path_len
++] = '/';
2774 /* Search for *.lttng files */
2775 while (!readdir_r(directory
, entry
, &result
) && result
) {
2776 size_t file_name_len
= strlen(result
->d_name
);
2778 if (file_name_len
<=
2779 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2783 if (path_len
+ file_name_len
>= PATH_MAX
) {
2787 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2788 result
->d_name
+ file_name_len
- sizeof(
2789 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2793 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2794 file_path
[path_len
+ file_name_len
] = '\0';
2796 ret
= load_session_from_file(file_path
, session_name
,
2797 validation_ctx
, override
);
2798 if (session_name
&& !ret
) {
2807 ret
= load_session_from_file(path
, session_name
,
2808 validation_ctx
, override
);
2818 if (closedir(directory
)) {
2823 if (!session_found
) {
2824 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2831 * Validate that the given path's credentials and the current process have the
2832 * same UID. If so, return 1 else return 0 if it does NOT match.
2834 static int validate_path_creds(const char *path
)
2836 int ret
, uid
= getuid();
2845 ret
= stat(path
, &buf
);
2847 if (errno
!= ENOENT
) {
2850 ret
= -LTTNG_ERR_INVALID
;
2854 if (buf
.st_uid
!= uid
) {
2865 int config_load_session(const char *path
, const char *session_name
,
2866 int override
, unsigned int autoload
)
2869 const char *path_ptr
= NULL
;
2870 struct session_config_validation_ctx validation_ctx
= { 0 };
2872 ret
= init_session_config_validation_ctx(&validation_ctx
);
2879 const char *sys_path
;
2882 home_path
= utils_get_home_dir();
2884 char path
[PATH_MAX
];
2887 * Try user session configuration path. Ignore error here so we can
2888 * continue loading the system wide sessions.
2891 ret
= snprintf(path
, sizeof(path
),
2892 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2893 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2895 PERROR("snprintf session autoload home config path");
2900 * Credentials are only validated for the autoload in order to
2901 * avoid any user session daemon to try to load kernel sessions
2902 * automatically and failing all the times.
2904 ret
= validate_path_creds(path
);
2909 ret
= snprintf(path
, sizeof(path
),
2910 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2912 PERROR("snprintf session home config path");
2918 ret
= load_session_from_path(path_ptr
, session_name
,
2919 &validation_ctx
, override
);
2920 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2924 * Continue even if the session was found since we have to try
2925 * the system wide sessions.
2930 /* Reset path pointer for the system wide dir. */
2933 /* Try system wide configuration directory. */
2935 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2936 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2937 ret
= validate_path_creds(sys_path
);
2939 path_ptr
= sys_path
;
2942 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2943 path_ptr
= sys_path
;
2947 ret
= load_session_from_path(path_ptr
, session_name
,
2948 &validation_ctx
, override
);
2951 ret
= access(path
, F_OK
);
2956 ret
= -LTTNG_ERR_INVALID
;
2957 WARN("Session configuration path does not exist.");
2960 ret
= -LTTNG_ERR_EPERM
;
2963 ret
= -LTTNG_ERR_UNK
;
2969 ret
= load_session_from_path(path
, session_name
,
2970 &validation_ctx
, override
);
2973 fini_session_config_validation_ctx(&validation_ctx
);
2974 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2976 * Don't report an error if no sessions are found when called
2977 * without a session_name or a search path.