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 const char * const config_element_type
= "type";
99 const char * const config_element_buffer_type
= "buffer_type";
100 const char * const config_element_session
= "session";
101 const char * const config_element_sessions
= "sessions";
102 const char * const config_element_context_perf
= "perf";
103 const char * const config_element_context_app
= "app";
104 const char * const config_element_context_app_provider_name
= "provider_name";
105 const char * const config_element_context_app_ctx_name
= "ctx_name";
106 const char * const config_element_config
= "config";
107 const char * const config_element_started
= "started";
108 const char * const config_element_snapshot_mode
= "snapshot_mode";
109 const char * const config_element_loglevel
= "loglevel";
110 const char * const config_element_loglevel_type
= "loglevel_type";
111 const char * const config_element_filter
= "filter";
112 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
113 const char * const config_element_consumer_output
= "consumer_output";
114 const char * const config_element_destination
= "destination";
115 const char * const config_element_path
= "path";
116 const char * const config_element_net_output
= "net_output";
117 const char * const config_element_control_uri
= "control_uri";
118 const char * const config_element_data_uri
= "data_uri";
119 const char * const config_element_max_size
= "max_size";
120 const char * const config_element_pid
= "pid";
121 const char * const config_element_pids
= "pids";
122 const char * const config_element_shared_memory_path
= "shared_memory_path";
123 const char * const config_element_pid_tracker
= "pid_tracker";
124 const char * const config_element_trackers
= "trackers";
125 const char * const config_element_targets
= "targets";
126 const char * const config_element_target_pid
= "pid_target";
128 const char * const config_domain_type_kernel
= "KERNEL";
129 const char * const config_domain_type_ust
= "UST";
130 const char * const config_domain_type_jul
= "JUL";
131 const char * const config_domain_type_log4j
= "LOG4J";
132 const char * const config_domain_type_python
= "PYTHON";
134 const char * const config_buffer_type_per_pid
= "PER_PID";
135 const char * const config_buffer_type_per_uid
= "PER_UID";
136 const char * const config_buffer_type_global
= "GLOBAL";
138 const char * const config_overwrite_mode_discard
= "DISCARD";
139 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
141 const char * const config_output_type_splice
= "SPLICE";
142 const char * const config_output_type_mmap
= "MMAP";
144 const char * const config_loglevel_type_all
= "ALL";
145 const char * const config_loglevel_type_range
= "RANGE";
146 const char * const config_loglevel_type_single
= "SINGLE";
148 const char * const config_event_type_all
= "ALL";
149 const char * const config_event_type_tracepoint
= "TRACEPOINT";
150 const char * const config_event_type_probe
= "PROBE";
151 const char * const config_event_type_function
= "FUNCTION";
152 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
153 const char * const config_event_type_noop
= "NOOP";
154 const char * const config_event_type_syscall
= "SYSCALL";
155 const char * const config_event_type_kprobe
= "KPROBE";
156 const char * const config_event_type_kretprobe
= "KRETPROBE";
158 const char * const config_event_context_pid
= "PID";
159 const char * const config_event_context_procname
= "PROCNAME";
160 const char * const config_event_context_prio
= "PRIO";
161 const char * const config_event_context_nice
= "NICE";
162 const char * const config_event_context_vpid
= "VPID";
163 const char * const config_event_context_tid
= "TID";
164 const char * const config_event_context_vtid
= "VTID";
165 const char * const config_event_context_ppid
= "PPID";
166 const char * const config_event_context_vppid
= "VPPID";
167 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
168 const char * const config_event_context_hostname
= "HOSTNAME";
169 const char * const config_event_context_ip
= "IP";
170 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
171 const char * const config_event_context_app
= "APP";
174 struct consumer_output
{
181 static int config_entry_handler_filter(struct handler_filter_args
*args
,
182 const char *section
, const char *name
, const char *value
)
185 struct config_entry entry
= { section
, name
, value
};
189 if (!section
|| !name
|| !value
) {
195 if (strcmp(args
->section
, section
)) {
200 ret
= args
->handler(&entry
, args
->user_data
);
206 int config_get_section_entries(const char *override_path
, const char *section
,
207 config_entry_handler_cb handler
, void *user_data
)
211 FILE *config_file
= NULL
;
212 struct handler_filter_args filter
= { section
, handler
, user_data
};
214 /* First, try system-wide conf. file. */
215 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
217 config_file
= fopen(path
, "r");
219 DBG("Loading daemon conf file at %s", path
);
221 * Return value is not very important here since error or not, we
222 * continue and try the next possible conf. file.
224 (void) ini_parse_file(config_file
,
225 (ini_entry_handler
) config_entry_handler_filter
,
230 /* Second is the user local configuration. */
231 path
= utils_get_home_dir();
233 char fullpath
[PATH_MAX
];
235 ret
= snprintf(fullpath
, sizeof(fullpath
),
236 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
238 PERROR("snprintf user conf. path");
242 config_file
= fopen(fullpath
, "r");
244 DBG("Loading daemon user conf file at %s", path
);
246 * Return value is not very important here since error or not, we
247 * continue and try the next possible conf. file.
249 (void) ini_parse_file(config_file
,
250 (ini_entry_handler
) config_entry_handler_filter
,
256 /* Final path is the one that the user might have provided. */
258 config_file
= fopen(override_path
, "r");
260 DBG("Loading daemon command line conf file at %s", override_path
);
261 (void) ini_parse_file(config_file
,
262 (ini_entry_handler
) config_entry_handler_filter
,
266 ERR("Failed to open daemon configuration file at %s",
273 /* Everything went well. */
281 int config_parse_value(const char *value
)
284 char *endptr
, *lower_str
;
294 v
= strtoul(value
, &endptr
, 10);
295 if (endptr
!= value
) {
300 lower_str
= zmalloc(len
+ 1);
307 for (i
= 0; i
< len
; i
++) {
308 lower_str
[i
] = tolower(value
[i
]);
311 if (!strcmp(lower_str
, config_str_yes
) ||
312 !strcmp(lower_str
, config_str_true
) ||
313 !strcmp(lower_str
, config_str_on
)) {
315 } else if (!strcmp(lower_str
, config_str_no
) ||
316 !strcmp(lower_str
, config_str_false
) ||
317 !strcmp(lower_str
, config_str_off
)) {
329 * Returns a xmlChar string which must be released using xmlFree().
331 static xmlChar
*encode_string(const char *in_str
)
333 xmlChar
*out_str
= NULL
;
334 xmlCharEncodingHandlerPtr handler
;
335 int out_len
, ret
, in_len
;
339 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
341 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
345 in_len
= strlen(in_str
);
347 * Add 1 byte for the NULL terminted character. The factor 4 here is
348 * used because UTF-8 characters can take up to 4 bytes.
350 out_len
= (in_len
* 4) + 1;
351 out_str
= xmlMalloc(out_len
);
356 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
363 /* out_len is now the size of out_str */
364 out_str
[out_len
] = '\0';
370 struct config_writer
*config_writer_create(int fd_output
, int indent
)
373 struct config_writer
*writer
;
374 xmlOutputBufferPtr buffer
;
376 writer
= zmalloc(sizeof(struct config_writer
));
378 PERROR("zmalloc config_writer_create");
382 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
387 writer
->writer
= xmlNewTextWriter(buffer
);
388 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
389 config_xml_encoding
, NULL
);
394 ret
= xmlTextWriterSetIndentString(writer
->writer
,
395 BAD_CAST config_xml_indent_string
);
400 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
408 config_writer_destroy(writer
);
413 int config_writer_destroy(struct config_writer
*writer
)
422 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
423 WARN("Could not close XML document");
427 if (writer
->writer
) {
428 xmlFreeTextWriter(writer
->writer
);
437 int config_writer_open_element(struct config_writer
*writer
,
438 const char *element_name
)
441 xmlChar
*encoded_element_name
;
443 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
448 encoded_element_name
= encode_string(element_name
);
449 if (!encoded_element_name
) {
454 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
455 xmlFree(encoded_element_name
);
457 return ret
> 0 ? 0 : ret
;
461 int config_writer_close_element(struct config_writer
*writer
)
465 if (!writer
|| !writer
->writer
) {
470 ret
= xmlTextWriterEndElement(writer
->writer
);
472 return ret
> 0 ? 0 : ret
;
476 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
477 const char *element_name
, uint64_t value
)
480 xmlChar
*encoded_element_name
;
482 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
487 encoded_element_name
= encode_string(element_name
);
488 if (!encoded_element_name
) {
493 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
494 encoded_element_name
, "%" PRIu64
, value
);
495 xmlFree(encoded_element_name
);
497 return ret
> 0 ? 0 : ret
;
501 int config_writer_write_element_signed_int(struct config_writer
*writer
,
502 const char *element_name
, int64_t value
)
505 xmlChar
*encoded_element_name
;
507 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
512 encoded_element_name
= encode_string(element_name
);
513 if (!encoded_element_name
) {
518 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
519 encoded_element_name
, "%" PRIi64
, value
);
520 xmlFree(encoded_element_name
);
522 return ret
> 0 ? 0 : ret
;
526 int config_writer_write_element_bool(struct config_writer
*writer
,
527 const char *element_name
, int value
)
529 return config_writer_write_element_string(writer
, element_name
,
530 value
? config_xml_true
: config_xml_false
);
534 int config_writer_write_element_string(struct config_writer
*writer
,
535 const char *element_name
, const char *value
)
538 xmlChar
*encoded_element_name
= NULL
;
539 xmlChar
*encoded_value
= NULL
;
541 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
547 encoded_element_name
= encode_string(element_name
);
548 if (!encoded_element_name
) {
553 encoded_value
= encode_string(value
);
554 if (!encoded_value
) {
559 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
562 xmlFree(encoded_element_name
);
563 xmlFree(encoded_value
);
564 return ret
> 0 ? 0 : ret
;
568 void xml_error_handler(void *ctx
, const char *format
, ...)
574 va_start(args
, format
);
575 ret
= vasprintf(&errMsg
, format
, args
);
578 ERR("String allocation failed in xml error handler");
582 fprintf(stderr
, "XML Error: %s", errMsg
);
587 void fini_session_config_validation_ctx(
588 struct session_config_validation_ctx
*ctx
)
590 if (ctx
->parser_ctx
) {
591 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
595 xmlSchemaFree(ctx
->schema
);
598 if (ctx
->schema_validation_ctx
) {
599 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
602 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
606 char *get_session_config_xsd_path()
609 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
610 size_t base_path_len
;
614 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
617 base_path_len
= strlen(base_path
);
618 max_path_len
= base_path_len
+
619 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
620 xsd_path
= zmalloc(max_path_len
);
625 strncpy(xsd_path
, base_path
, max_path_len
);
626 if (xsd_path
[base_path_len
- 1] != '/') {
627 xsd_path
[base_path_len
++] = '/';
630 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
631 max_path_len
- base_path_len
);
637 int init_session_config_validation_ctx(
638 struct session_config_validation_ctx
*ctx
)
641 char *xsd_path
= get_session_config_xsd_path();
644 ret
= -LTTNG_ERR_NOMEM
;
648 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
649 if (!ctx
->parser_ctx
) {
650 ERR("XSD parser context creation failed");
651 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
654 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
655 xml_error_handler
, NULL
);
657 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
659 ERR("XSD parsing failed");
660 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
664 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
665 if (!ctx
->schema_validation_ctx
) {
666 ERR("XSD validation context creation failed");
667 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
671 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
672 xml_error_handler
, NULL
);
677 fini_session_config_validation_ctx(ctx
);
685 int parse_uint(xmlChar
*str
, uint64_t *val
)
695 *val
= strtoull((const char *) str
, &endptr
, 10);
696 if (!endptr
|| *endptr
) {
707 int parse_int(xmlChar
*str
, int64_t *val
)
717 *val
= strtoll((const char *) str
, &endptr
, 10);
718 if (!endptr
|| *endptr
) {
729 int parse_bool(xmlChar
*str
, int *val
)
738 if (!strcmp((const char *) str
, config_xml_true
)) {
740 } else if (!strcmp((const char *) str
, config_xml_false
)) {
743 WARN("Invalid boolean value encoutered (%s).",
752 int get_domain_type(xmlChar
*domain
)
760 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
761 ret
= LTTNG_DOMAIN_KERNEL
;
762 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
763 ret
= LTTNG_DOMAIN_UST
;
764 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
765 ret
= LTTNG_DOMAIN_JUL
;
766 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
767 ret
= LTTNG_DOMAIN_LOG4J
;
768 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
769 ret
= LTTNG_DOMAIN_PYTHON
;
780 int get_buffer_type(xmlChar
*buffer_type
)
788 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
789 ret
= LTTNG_BUFFER_GLOBAL
;
790 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
791 ret
= LTTNG_BUFFER_PER_UID
;
792 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
793 ret
= LTTNG_BUFFER_PER_PID
;
804 int get_overwrite_mode(xmlChar
*overwrite_mode
)
808 if (!overwrite_mode
) {
812 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
814 } else if (!strcmp((char *) overwrite_mode
,
815 config_overwrite_mode_discard
)) {
827 int get_output_type(xmlChar
*output_type
)
835 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
836 ret
= LTTNG_EVENT_MMAP
;
837 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
838 ret
= LTTNG_EVENT_SPLICE
;
849 int get_event_type(xmlChar
*event_type
)
857 if (!strcmp((char *) event_type
, config_event_type_all
)) {
858 ret
= LTTNG_EVENT_ALL
;
859 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
860 ret
= LTTNG_EVENT_TRACEPOINT
;
861 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
862 ret
= LTTNG_EVENT_PROBE
;
863 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
864 ret
= LTTNG_EVENT_FUNCTION
;
865 } else if (!strcmp((char *) event_type
,
866 config_event_type_function_entry
)) {
867 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
868 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
869 ret
= LTTNG_EVENT_NOOP
;
870 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
871 ret
= LTTNG_EVENT_SYSCALL
;
882 int get_loglevel_type(xmlChar
*loglevel_type
)
886 if (!loglevel_type
) {
890 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
891 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
892 } else if (!strcmp((char *) loglevel_type
,
893 config_loglevel_type_range
)) {
894 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
895 } else if (!strcmp((char *) loglevel_type
,
896 config_loglevel_type_single
)) {
897 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
908 * Return the context type or -1 on error.
911 int get_context_type(xmlChar
*context_type
)
919 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
920 ret
= LTTNG_EVENT_CONTEXT_PID
;
921 } else if (!strcmp((char *) context_type
,
922 config_event_context_procname
)) {
923 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
924 } else if (!strcmp((char *) context_type
,
925 config_event_context_prio
)) {
926 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
927 } else if (!strcmp((char *) context_type
,
928 config_event_context_nice
)) {
929 ret
= LTTNG_EVENT_CONTEXT_NICE
;
930 } else if (!strcmp((char *) context_type
,
931 config_event_context_vpid
)) {
932 ret
= LTTNG_EVENT_CONTEXT_VPID
;
933 } else if (!strcmp((char *) context_type
,
934 config_event_context_tid
)) {
935 ret
= LTTNG_EVENT_CONTEXT_TID
;
936 } else if (!strcmp((char *) context_type
,
937 config_event_context_vtid
)) {
938 ret
= LTTNG_EVENT_CONTEXT_VTID
;
939 } else if (!strcmp((char *) context_type
,
940 config_event_context_ppid
)) {
941 ret
= LTTNG_EVENT_CONTEXT_PPID
;
942 } else if (!strcmp((char *) context_type
,
943 config_event_context_vppid
)) {
944 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
945 } else if (!strcmp((char *) context_type
,
946 config_event_context_pthread_id
)) {
947 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
948 } else if (!strcmp((char *) context_type
,
949 config_event_context_hostname
)) {
950 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
951 } else if (!strcmp((char *) context_type
,
952 config_event_context_ip
)) {
953 ret
= LTTNG_EVENT_CONTEXT_IP
;
964 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
969 for (node
= xmlFirstElementChild(domain_node
); node
;
970 node
= xmlNextElementSibling(node
)) {
971 if (!strcmp((const char *) node
->name
, config_element_type
)) {
973 xmlChar
*node_content
= xmlNodeGetContent(node
);
975 ret
= -LTTNG_ERR_NOMEM
;
979 ret
= get_domain_type(node_content
);
982 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
987 } else if (!strcmp((const char *) node
->name
,
988 config_element_buffer_type
)) {
990 xmlChar
*node_content
= xmlNodeGetContent(node
);
992 ret
= -LTTNG_ERR_NOMEM
;
996 ret
= get_buffer_type(node_content
);
999 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1003 domain
->buf_type
= ret
;
1012 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1017 for (node
= xmlFirstElementChild(net_output_node
); node
;
1018 node
= xmlNextElementSibling(node
)) {
1019 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1021 *control_uri
= (char *) xmlNodeGetContent(node
);
1022 if (!*control_uri
) {
1027 *data_uri
= (char *) xmlNodeGetContent(node
);
1034 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1038 int process_consumer_output(xmlNodePtr consumer_output_node
,
1039 struct consumer_output
*output
)
1046 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1047 node
= xmlNextElementSibling(node
)) {
1048 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1049 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1053 ret
= -LTTNG_ERR_NOMEM
;
1057 ret
= parse_bool(enabled_str
, &output
->enabled
);
1063 xmlNodePtr output_type_node
;
1066 output_type_node
= xmlFirstElementChild(node
);
1067 if (!output_type_node
) {
1068 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1072 if (!strcmp((const char *) output_type_node
->name
,
1073 config_element_path
)) {
1075 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1076 if (!output
->path
) {
1077 ret
= -LTTNG_ERR_NOMEM
;
1082 ret
= get_net_output_uris(output_type_node
,
1083 &output
->control_uri
, &output
->data_uri
);
1095 free(output
->control_uri
);
1096 free(output
->data_uri
);
1097 memset(output
, 0, sizeof(struct consumer_output
));
1103 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1104 const char *control_uri
, const char *data_uri
)
1107 struct lttng_handle
*handle
;
1108 const char *uri
= NULL
;
1113 handle
= lttng_create_handle(name
, domain
);
1115 ret
= -LTTNG_ERR_NOMEM
;
1119 if (!control_uri
|| !data_uri
) {
1120 uri
= control_uri
? control_uri
: data_uri
;
1125 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1126 lttng_destroy_handle(handle
);
1132 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1135 xmlNodePtr node
= NULL
;
1136 xmlNodePtr snapshot_output_list_node
;
1137 xmlNodePtr snapshot_output_node
;
1139 assert(session_name
);
1141 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1150 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1152 /* Parse and create snapshot outputs */
1154 for (snapshot_output_node
=
1155 xmlFirstElementChild(snapshot_output_list_node
);
1156 snapshot_output_node
; snapshot_output_node
=
1157 xmlNextElementSibling(snapshot_output_node
)) {
1159 uint64_t max_size
= UINT64_MAX
;
1160 struct consumer_output output
= { 0 };
1161 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1163 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1164 node
= xmlNextElementSibling(node
)) {
1165 if (!strcmp((const char *) node
->name
,
1166 config_element_name
)) {
1168 name
= (char *) xmlNodeGetContent(node
);
1170 ret
= -LTTNG_ERR_NOMEM
;
1171 goto error_snapshot_output
;
1173 } else if (!strcmp((const char *) node
->name
,
1174 config_element_max_size
)) {
1175 xmlChar
*content
= xmlNodeGetContent(node
);
1179 ret
= -LTTNG_ERR_NOMEM
;
1180 goto error_snapshot_output
;
1182 ret
= parse_uint(content
, &max_size
);
1185 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1186 goto error_snapshot_output
;
1189 /* consumer_output */
1190 ret
= process_consumer_output(node
, &output
);
1192 goto error_snapshot_output
;
1197 snapshot_output
= lttng_snapshot_output_create();
1198 if (!snapshot_output
) {
1199 ret
= -LTTNG_ERR_NOMEM
;
1200 goto error_snapshot_output
;
1203 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1205 goto error_snapshot_output
;
1208 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1210 goto error_snapshot_output
;
1214 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1217 goto error_snapshot_output
;
1220 if (output
.control_uri
) {
1221 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1224 goto error_snapshot_output
;
1228 if (output
.data_uri
) {
1229 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1232 goto error_snapshot_output
;
1237 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1238 error_snapshot_output
:
1241 free(output
.control_uri
);
1242 free(output
.data_uri
);
1243 lttng_snapshot_output_destroy(snapshot_output
);
1253 int create_session(const char *name
,
1254 struct lttng_domain
*kernel_domain
,
1255 struct lttng_domain
*ust_domain
,
1256 struct lttng_domain
*jul_domain
,
1257 struct lttng_domain
*log4j_domain
,
1258 xmlNodePtr output_node
,
1259 uint64_t live_timer_interval
)
1262 struct consumer_output output
= { 0 };
1263 xmlNodePtr consumer_output_node
;
1268 consumer_output_node
= xmlFirstElementChild(output_node
);
1269 if (!consumer_output_node
) {
1270 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1274 if (strcmp((const char *) consumer_output_node
->name
,
1275 config_element_consumer_output
)) {
1276 WARN("Invalid output type, expected %s node",
1277 config_element_consumer_output
);
1278 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1282 ret
= process_consumer_output(consumer_output_node
, &output
);
1288 if (live_timer_interval
!= UINT64_MAX
&&
1289 !output
.control_uri
&& !output
.data_uri
) {
1290 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1294 if (output
.control_uri
|| output
.data_uri
) {
1296 struct lttng_domain
*domain
;
1297 struct lttng_domain
*domains
[] =
1298 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1300 /* network destination */
1301 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1303 * URLs are provided for sure since the test above make sure that
1304 * with a live timer the data and control URIs are provided. So,
1305 * NULL is passed here and will be set right after.
1307 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1309 ret
= lttng_create_session(name
, NULL
);
1315 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1316 domain
= domains
[i
];
1321 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1328 /* either local output or no output */
1329 ret
= lttng_create_session(name
, output
.path
);
1336 free(output
.control_uri
);
1337 free(output
.data_uri
);
1341 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1342 struct lttng_event_probe_attr
*attr
)
1346 assert(probe_attribute_node
);
1349 if (!strcmp((const char *) probe_attribute_node
->name
,
1350 config_element_address
)) {
1355 content
= xmlNodeGetContent(probe_attribute_node
);
1357 ret
= -LTTNG_ERR_NOMEM
;
1361 ret
= parse_uint(content
, &addr
);
1364 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1369 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1370 config_element_offset
)) {
1372 uint64_t offset
= 0;
1375 content
= xmlNodeGetContent(probe_attribute_node
);
1377 ret
= -LTTNG_ERR_NOMEM
;
1381 ret
= parse_uint(content
, &offset
);
1384 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1388 attr
->offset
= offset
;
1389 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1390 config_element_symbol_name
)) {
1395 content
= xmlNodeGetContent(probe_attribute_node
);
1397 ret
= -LTTNG_ERR_NOMEM
;
1401 name_len
= strlen((char *) content
);
1402 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1403 WARN("symbol_name too long.");
1404 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1409 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1418 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1419 const char *channel_name
)
1423 struct lttng_event event
;
1424 char **exclusions
= NULL
;
1425 unsigned long exclusion_count
= 0;
1426 char *filter_expression
= NULL
;
1430 assert(channel_name
);
1432 memset(&event
, 0, sizeof(event
));
1434 /* Initialize default log level which varies by domain */
1435 switch (handle
->domain
.type
)
1437 case LTTNG_DOMAIN_JUL
:
1438 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1440 case LTTNG_DOMAIN_LOG4J
:
1441 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1443 case LTTNG_DOMAIN_PYTHON
:
1444 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1446 case LTTNG_DOMAIN_UST
:
1447 case LTTNG_DOMAIN_KERNEL
:
1448 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1454 for (node
= xmlFirstElementChild(event_node
); node
;
1455 node
= xmlNextElementSibling(node
)) {
1456 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1461 content
= xmlNodeGetContent(node
);
1463 ret
= -LTTNG_ERR_NOMEM
;
1467 name_len
= strlen((char *) content
);
1468 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1469 WARN("Channel name too long.");
1470 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1475 strncpy(event
.name
, (const char *) content
, name_len
);
1477 } else if (!strcmp((const char *) node
->name
,
1478 config_element_enabled
)) {
1479 xmlChar
*content
= xmlNodeGetContent(node
);
1483 ret
= -LTTNG_ERR_NOMEM
;
1487 ret
= parse_bool(content
, &event
.enabled
);
1490 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1493 } else if (!strcmp((const char *) node
->name
,
1494 config_element_type
)) {
1495 xmlChar
*content
= xmlNodeGetContent(node
);
1499 ret
= -LTTNG_ERR_NOMEM
;
1503 ret
= get_event_type(content
);
1506 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1511 } else if (!strcmp((const char *) node
->name
,
1512 config_element_loglevel_type
)) {
1513 xmlChar
*content
= xmlNodeGetContent(node
);
1517 ret
= -LTTNG_ERR_NOMEM
;
1521 ret
= get_loglevel_type(content
);
1524 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1528 event
.loglevel_type
= ret
;
1529 } else if (!strcmp((const char *) node
->name
,
1530 config_element_loglevel
)) {
1532 int64_t loglevel
= 0;
1535 content
= xmlNodeGetContent(node
);
1537 ret
= -LTTNG_ERR_NOMEM
;
1541 ret
= parse_int(content
, &loglevel
);
1544 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1548 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1549 WARN("loglevel out of range.");
1550 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1554 event
.loglevel
= loglevel
;
1555 } else if (!strcmp((const char *) node
->name
,
1556 config_element_filter
)) {
1558 xmlNodeGetContent(node
);
1562 ret
= -LTTNG_ERR_NOMEM
;
1566 filter_expression
= strdup((char *) content
);
1568 if (!filter_expression
) {
1569 ret
= -LTTNG_ERR_NOMEM
;
1572 } else if (!strcmp((const char *) node
->name
,
1573 config_element_exclusions
)) {
1574 xmlNodePtr exclusion_node
;
1575 int exclusion_index
= 0;
1580 * Exclusions has already been initialized,
1583 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1587 exclusion_count
= xmlChildElementCount(node
);
1588 if (!exclusion_count
) {
1592 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1594 exclusion_count
= 0;
1595 ret
= -LTTNG_ERR_NOMEM
;
1599 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1600 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1602 xmlNodeGetContent(exclusion_node
);
1605 ret
= -LTTNG_ERR_NOMEM
;
1609 exclusions
[exclusion_index
] = strdup((const char *) content
);
1611 if (!exclusions
[exclusion_index
]) {
1612 ret
= -LTTNG_ERR_NOMEM
;
1618 event
.exclusion
= 1;
1619 } else if (!strcmp((const char *) node
->name
,
1620 config_element_attributes
)) {
1621 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1624 if (!attribute_node
) {
1625 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1629 if (!strcmp((const char *) node
->name
,
1630 config_element_probe_attributes
)) {
1631 xmlNodePtr probe_attribute_node
;
1633 /* probe_attributes */
1634 for (probe_attribute_node
=
1635 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1636 probe_attribute_node
= xmlNextElementSibling(
1637 probe_attribute_node
)) {
1639 ret
= process_probe_attribute_node(probe_attribute_node
,
1648 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1650 /* function_attributes */
1651 content
= xmlNodeGetContent(symbol_node
);
1653 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1657 sym_len
= strlen((char *) content
);
1658 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1659 WARN("Function name too long.");
1660 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1665 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1672 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1673 filter_expression
, exclusion_count
, exclusions
);
1678 if (!event
.enabled
) {
1680 * Note that we should use lttng_disable_event_ext() (2.6+) to
1681 * eliminate the risk of clashing on events of the same
1682 * name (with different event types and loglevels).
1684 * Unfortunately, lttng_disable_event_ext() only performs a
1685 * match on the name and event type and errors out if any other
1686 * event attribute is not set to its default value.
1688 * This will disable all events that match this name.
1690 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1693 for (i
= 0; i
< exclusion_count
; i
++) {
1694 free(exclusions
[i
]);
1698 free(filter_expression
);
1703 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1704 const char *channel_name
)
1709 assert(events_node
);
1711 assert(channel_name
);
1713 for (node
= xmlFirstElementChild(events_node
); node
;
1714 node
= xmlNextElementSibling(node
)) {
1715 ret
= process_event_node(node
, handle
, channel_name
);
1725 int process_channel_attr_node(xmlNodePtr attr_node
,
1726 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1727 xmlNodePtr
*events_node
)
1733 assert(contexts_node
);
1734 assert(events_node
);
1736 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1741 content
= xmlNodeGetContent(attr_node
);
1743 ret
= -LTTNG_ERR_NOMEM
;
1747 name_len
= strlen((char *) content
);
1748 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1749 WARN("Channel name too long.");
1750 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1755 strncpy(channel
->name
, (const char *) content
, name_len
);
1757 } else if (!strcmp((const char *) attr_node
->name
,
1758 config_element_enabled
)) {
1763 content
= xmlNodeGetContent(attr_node
);
1765 ret
= -LTTNG_ERR_NOMEM
;
1769 ret
= parse_bool(content
, &enabled
);
1772 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1776 channel
->enabled
= enabled
;
1777 } else if (!strcmp((const char *) attr_node
->name
,
1778 config_element_overwrite_mode
)) {
1781 /* overwrite_mode */
1782 content
= xmlNodeGetContent(attr_node
);
1784 ret
= -LTTNG_ERR_NOMEM
;
1788 ret
= get_overwrite_mode(content
);
1791 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1795 channel
->attr
.overwrite
= ret
;
1796 } else if (!strcmp((const char *) attr_node
->name
,
1797 config_element_subbuf_size
)) {
1800 /* subbuffer_size */
1801 content
= xmlNodeGetContent(attr_node
);
1803 ret
= -LTTNG_ERR_NOMEM
;
1807 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1810 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1813 } else if (!strcmp((const char *) attr_node
->name
,
1814 config_element_num_subbuf
)) {
1817 /* subbuffer_count */
1818 content
= xmlNodeGetContent(attr_node
);
1820 ret
= -LTTNG_ERR_NOMEM
;
1824 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1827 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1830 } else if (!strcmp((const char *) attr_node
->name
,
1831 config_element_switch_timer_interval
)) {
1833 uint64_t switch_timer_interval
= 0;
1835 /* switch_timer_interval */
1836 content
= xmlNodeGetContent(attr_node
);
1838 ret
= -LTTNG_ERR_NOMEM
;
1842 ret
= parse_uint(content
, &switch_timer_interval
);
1845 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1849 if (switch_timer_interval
> UINT_MAX
) {
1850 WARN("switch_timer_interval out of range.");
1851 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1855 channel
->attr
.switch_timer_interval
=
1856 switch_timer_interval
;
1857 } else if (!strcmp((const char *) attr_node
->name
,
1858 config_element_read_timer_interval
)) {
1860 uint64_t read_timer_interval
= 0;
1862 /* read_timer_interval */
1863 content
= xmlNodeGetContent(attr_node
);
1865 ret
= -LTTNG_ERR_NOMEM
;
1869 ret
= parse_uint(content
, &read_timer_interval
);
1872 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1876 if (read_timer_interval
> UINT_MAX
) {
1877 WARN("read_timer_interval out of range.");
1878 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1882 channel
->attr
.read_timer_interval
=
1883 read_timer_interval
;
1884 } else if (!strcmp((const char *) attr_node
->name
,
1885 config_element_output_type
)) {
1889 content
= xmlNodeGetContent(attr_node
);
1891 ret
= -LTTNG_ERR_NOMEM
;
1895 ret
= get_output_type(content
);
1898 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1902 channel
->attr
.output
= ret
;
1903 } else if (!strcmp((const char *) attr_node
->name
,
1904 config_element_tracefile_size
)) {
1907 /* tracefile_size */
1908 content
= xmlNodeGetContent(attr_node
);
1910 ret
= -LTTNG_ERR_NOMEM
;
1914 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1917 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1920 } else if (!strcmp((const char *) attr_node
->name
,
1921 config_element_tracefile_count
)) {
1924 /* tracefile_count */
1925 content
= xmlNodeGetContent(attr_node
);
1927 ret
= -LTTNG_ERR_NOMEM
;
1931 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1934 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1937 } else if (!strcmp((const char *) attr_node
->name
,
1938 config_element_live_timer_interval
)) {
1940 uint64_t live_timer_interval
= 0;
1942 /* live_timer_interval */
1943 content
= xmlNodeGetContent(attr_node
);
1945 ret
= -LTTNG_ERR_NOMEM
;
1949 ret
= parse_uint(content
, &live_timer_interval
);
1952 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1956 if (live_timer_interval
> UINT_MAX
) {
1957 WARN("live_timer_interval out of range.");
1958 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1962 channel
->attr
.live_timer_interval
=
1963 live_timer_interval
;
1964 } else if (!strcmp((const char *) attr_node
->name
,
1965 config_element_events
)) {
1967 *events_node
= attr_node
;
1970 *contexts_node
= attr_node
;
1978 int process_context_node(xmlNodePtr context_node
,
1979 struct lttng_handle
*handle
, const char *channel_name
)
1982 struct lttng_event_context context
;
1983 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1986 assert(channel_name
);
1988 if (!context_child_node
) {
1989 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1993 memset(&context
, 0, sizeof(context
));
1995 if (!strcmp((const char *) context_child_node
->name
,
1996 config_element_type
)) {
1998 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2001 ret
= -LTTNG_ERR_NOMEM
;
2005 ret
= get_context_type(content
);
2008 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2013 } else if (!strcmp((const char *) context_child_node
->name
,
2014 config_element_context_perf
)) {
2016 xmlNodePtr perf_attr_node
;
2018 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2019 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2020 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2021 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2022 perf_attr_node
; perf_attr_node
=
2023 xmlNextElementSibling(perf_attr_node
)) {
2024 if (!strcmp((const char *) perf_attr_node
->name
,
2025 config_element_type
)) {
2030 content
= xmlNodeGetContent(perf_attr_node
);
2032 ret
= -LTTNG_ERR_NOMEM
;
2036 ret
= parse_uint(content
, &type
);
2039 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2043 if (type
> UINT32_MAX
) {
2044 WARN("perf context type out of range.");
2045 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2049 context
.u
.perf_counter
.type
= type
;
2050 } else if (!strcmp((const char *) perf_attr_node
->name
,
2051 config_element_config
)) {
2053 uint64_t config
= 0;
2056 content
= xmlNodeGetContent(perf_attr_node
);
2058 ret
= -LTTNG_ERR_NOMEM
;
2062 ret
= parse_uint(content
, &config
);
2065 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2069 context
.u
.perf_counter
.config
= config
;
2070 } else if (!strcmp((const char *) perf_attr_node
->name
,
2071 config_element_name
)) {
2076 content
= xmlNodeGetContent(perf_attr_node
);
2078 ret
= -LTTNG_ERR_NOMEM
;
2082 name_len
= strlen((char *) content
);
2083 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2084 WARN("perf context name too long.");
2085 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2090 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2095 } else if (!strcmp((const char *) context_child_node
->name
,
2096 config_element_context_app
)) {
2097 /* application context */
2098 xmlNodePtr app_ctx_node
;
2100 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2101 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2102 app_ctx_node
; app_ctx_node
=
2103 xmlNextElementSibling(app_ctx_node
)) {
2105 char **target
= strcmp(
2106 (const char *) app_ctx_node
->name
,
2107 config_element_context_app_provider_name
) == 0 ?
2108 &context
.u
.app_ctx
.provider_name
:
2109 &context
.u
.app_ctx
.ctx_name
;
2111 content
= xmlNodeGetContent(app_ctx_node
);
2113 ret
= -LTTNG_ERR_NOMEM
;
2117 *target
= (char *) content
;
2120 /* Unrecognized context type */
2121 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2125 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2126 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2127 free(context
.u
.app_ctx
.provider_name
);
2128 free(context
.u
.app_ctx
.ctx_name
);
2135 int process_contexts_node(xmlNodePtr contexts_node
,
2136 struct lttng_handle
*handle
, const char *channel_name
)
2139 xmlNodePtr context_node
;
2141 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2142 context_node
= xmlNextElementSibling(context_node
)) {
2143 ret
= process_context_node(context_node
, handle
, channel_name
);
2153 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2154 struct lttng_handle
*handle
)
2157 xmlNodePtr targets_node
= NULL
;
2161 assert(pid_tracker_node
);
2162 /* get the targets node */
2163 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2164 node
= xmlNextElementSibling(node
)) {
2165 if (!strcmp((const char *) node
->name
,
2166 config_element_targets
)) {
2167 targets_node
= node
;
2172 if (!targets_node
) {
2173 ret
= LTTNG_ERR_INVALID
;
2177 /* Go through all pid_target node */
2178 child
= xmlChildElementCount(targets_node
);
2180 /* The session is explicitly set to target nothing. */
2181 ret
= lttng_untrack_pid(handle
, -1);
2186 for (node
= xmlFirstElementChild(targets_node
); node
;
2187 node
= xmlNextElementSibling(node
)) {
2188 xmlNodePtr pid_target_node
= node
;
2190 /* get pid node and track it */
2191 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2192 node
= xmlNextElementSibling(node
)) {
2193 if (!strcmp((const char *) node
->name
,
2194 config_element_pid
)) {
2196 xmlChar
*content
= NULL
;
2198 content
= xmlNodeGetContent(node
);
2200 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2204 ret
= parse_int(content
, &pid
);
2207 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2211 ret
= lttng_track_pid(handle
, (int) pid
);
2217 node
= pid_target_node
;
2226 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2229 struct lttng_domain domain
= { 0 };
2230 struct lttng_handle
*handle
= NULL
;
2231 xmlNodePtr channels_node
= NULL
;
2232 xmlNodePtr trackers_node
= NULL
;
2233 xmlNodePtr pid_tracker_node
= NULL
;
2236 assert(session_name
);
2238 ret
= init_domain(domain_node
, &domain
);
2243 handle
= lttng_create_handle(session_name
, &domain
);
2245 ret
= -LTTNG_ERR_NOMEM
;
2249 /* get the channels node */
2250 for (node
= xmlFirstElementChild(domain_node
); node
;
2251 node
= xmlNextElementSibling(node
)) {
2252 if (!strcmp((const char *) node
->name
,
2253 config_element_channels
)) {
2254 channels_node
= node
;
2259 if (!channels_node
) {
2263 /* create all channels */
2264 for (node
= xmlFirstElementChild(channels_node
); node
;
2265 node
= xmlNextElementSibling(node
)) {
2266 struct lttng_channel channel
;
2267 xmlNodePtr contexts_node
= NULL
;
2268 xmlNodePtr events_node
= NULL
;
2269 xmlNodePtr channel_attr_node
;
2271 memset(&channel
, 0, sizeof(channel
));
2272 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2274 for (channel_attr_node
= xmlFirstElementChild(node
);
2275 channel_attr_node
; channel_attr_node
=
2276 xmlNextElementSibling(channel_attr_node
)) {
2277 ret
= process_channel_attr_node(channel_attr_node
,
2278 &channel
, &contexts_node
, &events_node
);
2284 ret
= lttng_enable_channel(handle
, &channel
);
2289 ret
= process_events_node(events_node
, handle
, channel
.name
);
2294 ret
= process_contexts_node(contexts_node
, handle
,
2301 /* get the trackers node */
2302 for (node
= xmlFirstElementChild(domain_node
); node
;
2303 node
= xmlNextElementSibling(node
)) {
2304 if (!strcmp((const char *) node
->name
,
2305 config_element_trackers
)) {
2306 trackers_node
= node
;
2311 if (!trackers_node
) {
2315 for (node
= xmlFirstElementChild(trackers_node
); node
;
2316 node
= xmlNextElementSibling(node
)) {
2317 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2318 pid_tracker_node
= node
;
2319 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2326 if (!pid_tracker_node
) {
2327 lttng_track_pid(handle
, -1);
2331 lttng_destroy_handle(handle
);
2336 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2339 int ret
, started
= -1, snapshot_mode
= -1;
2340 uint64_t live_timer_interval
= UINT64_MAX
;
2341 xmlChar
*name
= NULL
;
2342 xmlChar
*shm_path
= NULL
;
2343 xmlNodePtr domains_node
= NULL
;
2344 xmlNodePtr output_node
= NULL
;
2346 struct lttng_domain
*kernel_domain
= NULL
;
2347 struct lttng_domain
*ust_domain
= NULL
;
2348 struct lttng_domain
*jul_domain
= NULL
;
2349 struct lttng_domain
*log4j_domain
= NULL
;
2350 struct lttng_domain
*python_domain
= NULL
;
2352 for (node
= xmlFirstElementChild(session_node
); node
;
2353 node
= xmlNextElementSibling(node
)) {
2354 if (!name
&& !strcmp((const char *) node
->name
,
2355 config_element_name
)) {
2357 xmlChar
*node_content
= xmlNodeGetContent(node
);
2358 if (!node_content
) {
2359 ret
= -LTTNG_ERR_NOMEM
;
2363 name
= node_content
;
2364 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2365 config_element_domains
)) {
2367 domains_node
= node
;
2368 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2369 config_element_started
)) {
2371 xmlChar
*node_content
= xmlNodeGetContent(node
);
2372 if (!node_content
) {
2373 ret
= -LTTNG_ERR_NOMEM
;
2377 ret
= parse_bool(node_content
, &started
);
2380 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2383 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2384 config_element_output
)) {
2387 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2388 config_element_shared_memory_path
)) {
2389 /* shared memory path */
2390 xmlChar
*node_content
= xmlNodeGetContent(node
);
2391 if (!node_content
) {
2392 ret
= -LTTNG_ERR_NOMEM
;
2396 shm_path
= node_content
;
2398 /* attributes, snapshot_mode or live_timer_interval */
2399 xmlNodePtr attributes_child
=
2400 xmlFirstElementChild(node
);
2402 if (!strcmp((const char *) attributes_child
->name
,
2403 config_element_snapshot_mode
)) {
2405 xmlChar
*snapshot_mode_content
=
2406 xmlNodeGetContent(attributes_child
);
2407 if (!snapshot_mode_content
) {
2408 ret
= -LTTNG_ERR_NOMEM
;
2412 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2413 free(snapshot_mode_content
);
2415 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2419 /* live_timer_interval */
2420 xmlChar
*timer_interval_content
=
2421 xmlNodeGetContent(attributes_child
);
2422 if (!timer_interval_content
) {
2423 ret
= -LTTNG_ERR_NOMEM
;
2427 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2428 free(timer_interval_content
);
2430 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2438 /* Mandatory attribute, as defined in the session XSD */
2439 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2443 if (session_name
&& strcmp((char *) name
, session_name
)) {
2444 /* This is not the session we are looking for */
2445 ret
= -LTTNG_ERR_NO_SESSION
;
2449 /* Init domains to create the session handles */
2450 for (node
= xmlFirstElementChild(domains_node
); node
;
2451 node
= xmlNextElementSibling(node
)) {
2452 struct lttng_domain
*domain
;
2454 domain
= zmalloc(sizeof(*domain
));
2456 ret
= -LTTNG_ERR_NOMEM
;
2460 ret
= init_domain(node
, domain
);
2462 goto domain_init_error
;
2465 switch (domain
->type
) {
2466 case LTTNG_DOMAIN_KERNEL
:
2467 if (kernel_domain
) {
2468 /* Same domain seen twice, invalid! */
2469 goto domain_init_error
;
2471 kernel_domain
= domain
;
2473 case LTTNG_DOMAIN_UST
:
2475 /* Same domain seen twice, invalid! */
2476 goto domain_init_error
;
2478 ust_domain
= domain
;
2480 case LTTNG_DOMAIN_JUL
:
2482 /* Same domain seen twice, invalid! */
2483 goto domain_init_error
;
2485 jul_domain
= domain
;
2487 case LTTNG_DOMAIN_LOG4J
:
2489 /* Same domain seen twice, invalid! */
2490 goto domain_init_error
;
2492 log4j_domain
= domain
;
2494 case LTTNG_DOMAIN_PYTHON
:
2495 if (python_domain
) {
2496 /* Same domain seen twice, invalid! */
2497 goto domain_init_error
;
2499 python_domain
= domain
;
2502 WARN("Invalid domain type");
2503 goto domain_init_error
;
2508 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2513 /* Destroy session if it exists */
2514 ret
= lttng_destroy_session((const char *) name
);
2515 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2516 ERR("Failed to destroy existing session.");
2521 /* Create session type depending on output type */
2522 if (snapshot_mode
&& snapshot_mode
!= -1) {
2523 ret
= create_snapshot_session((const char *) name
, output_node
);
2524 } else if (live_timer_interval
&&
2525 live_timer_interval
!= UINT64_MAX
) {
2526 ret
= create_session((const char *) name
, kernel_domain
,
2527 ust_domain
, jul_domain
, log4j_domain
,
2528 output_node
, live_timer_interval
);
2530 /* regular session */
2531 ret
= create_session((const char *) name
, kernel_domain
,
2532 ust_domain
, jul_domain
, log4j_domain
,
2533 output_node
, UINT64_MAX
);
2540 ret
= lttng_set_session_shm_path((const char *) name
,
2541 (const char *) shm_path
);
2547 for (node
= xmlFirstElementChild(domains_node
); node
;
2548 node
= xmlNextElementSibling(node
)) {
2549 ret
= process_domain_node(node
, (const char *) name
);
2556 ret
= lttng_start_tracing((const char *) name
);
2564 ERR("Failed to load session %s: %s", (const char *) name
,
2565 lttng_strerror(ret
));
2566 lttng_destroy_session((const char *) name
);
2570 free(kernel_domain
);
2574 free(python_domain
);
2581 * Return 1 if the given path is readable by the current UID or 0 if not.
2582 * Return -1 if the path is EPERM.
2584 static int validate_file_read_creds(const char *path
)
2590 /* Can we read the file. */
2591 ret
= access(path
, R_OK
);
2595 if (errno
== EACCES
) {
2606 int load_session_from_file(const char *path
, const char *session_name
,
2607 struct session_config_validation_ctx
*validation_ctx
, int override
)
2609 int ret
, session_found
= !session_name
;
2610 xmlDocPtr doc
= NULL
;
2611 xmlNodePtr sessions_node
;
2612 xmlNodePtr session_node
;
2615 assert(validation_ctx
);
2617 ret
= validate_file_read_creds(path
);
2620 ret
= -LTTNG_ERR_EPERM
;
2622 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2627 doc
= xmlParseFile(path
);
2629 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2633 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2635 ERR("Session configuration file validation failed");
2636 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2640 sessions_node
= xmlDocGetRootElement(doc
);
2641 if (!sessions_node
) {
2645 for (session_node
= xmlFirstElementChild(sessions_node
);
2646 session_node
; session_node
=
2647 xmlNextElementSibling(session_node
)) {
2648 ret
= process_session_node(session_node
,
2649 session_name
, override
);
2650 if (session_name
&& ret
== 0) {
2651 /* Target session found and loaded */
2659 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2664 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2666 struct dirent
*alloc_dirent(const char *path
)
2670 struct dirent
*entry
;
2672 name_max
= pathconf(path
, _PC_NAME_MAX
);
2673 if (name_max
== -1) {
2674 name_max
= PATH_MAX
;
2676 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2677 entry
= zmalloc(len
);
2682 int load_session_from_path(const char *path
, const char *session_name
,
2683 struct session_config_validation_ctx
*validation_ctx
, int override
)
2685 int ret
, session_found
= !session_name
;
2686 DIR *directory
= NULL
;
2689 assert(validation_ctx
);
2691 directory
= opendir(path
);
2695 /* Try the file loading. */
2698 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2701 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2706 struct dirent
*entry
;
2707 struct dirent
*result
;
2708 char *file_path
= NULL
;
2709 size_t path_len
= strlen(path
);
2711 if (path_len
>= PATH_MAX
) {
2712 ret
= -LTTNG_ERR_INVALID
;
2716 entry
= alloc_dirent(path
);
2718 ret
= -LTTNG_ERR_NOMEM
;
2722 file_path
= zmalloc(PATH_MAX
);
2724 ret
= -LTTNG_ERR_NOMEM
;
2729 strncpy(file_path
, path
, path_len
);
2730 if (file_path
[path_len
- 1] != '/') {
2731 file_path
[path_len
++] = '/';
2735 /* Search for *.lttng files */
2736 while (!readdir_r(directory
, entry
, &result
) && result
) {
2737 size_t file_name_len
= strlen(result
->d_name
);
2739 if (file_name_len
<=
2740 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2744 if (path_len
+ file_name_len
>= PATH_MAX
) {
2748 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2749 result
->d_name
+ file_name_len
- sizeof(
2750 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2754 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2755 file_path
[path_len
+ file_name_len
] = '\0';
2757 ret
= load_session_from_file(file_path
, session_name
,
2758 validation_ctx
, override
);
2759 if (session_name
&& !ret
) {
2768 ret
= load_session_from_file(path
, session_name
,
2769 validation_ctx
, override
);
2779 if (closedir(directory
)) {
2784 if (!session_found
) {
2785 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2792 * Validate that the given path's credentials and the current process have the
2793 * same UID. If so, return 1 else return 0 if it does NOT match.
2795 static int validate_path_creds(const char *path
)
2797 int ret
, uid
= getuid();
2806 ret
= stat(path
, &buf
);
2808 if (errno
!= ENOENT
) {
2811 ret
= -LTTNG_ERR_INVALID
;
2815 if (buf
.st_uid
!= uid
) {
2826 int config_load_session(const char *path
, const char *session_name
,
2827 int override
, unsigned int autoload
)
2830 const char *path_ptr
= NULL
;
2831 struct session_config_validation_ctx validation_ctx
= { 0 };
2833 ret
= init_session_config_validation_ctx(&validation_ctx
);
2840 const char *sys_path
;
2843 home_path
= utils_get_home_dir();
2845 char path
[PATH_MAX
];
2848 * Try user session configuration path. Ignore error here so we can
2849 * continue loading the system wide sessions.
2852 ret
= snprintf(path
, sizeof(path
),
2853 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2854 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2856 PERROR("snprintf session autoload home config path");
2861 * Credentials are only validated for the autoload in order to
2862 * avoid any user session daemon to try to load kernel sessions
2863 * automatically and failing all the times.
2865 ret
= validate_path_creds(path
);
2870 ret
= snprintf(path
, sizeof(path
),
2871 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2873 PERROR("snprintf session home config path");
2879 ret
= load_session_from_path(path_ptr
, session_name
,
2880 &validation_ctx
, override
);
2881 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2885 * Continue even if the session was found since we have to try
2886 * the system wide sessions.
2891 /* Reset path pointer for the system wide dir. */
2894 /* Try system wide configuration directory. */
2896 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2897 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2898 ret
= validate_path_creds(sys_path
);
2900 path_ptr
= sys_path
;
2903 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2904 path_ptr
= sys_path
;
2908 ret
= load_session_from_path(path_ptr
, session_name
,
2909 &validation_ctx
, override
);
2912 ret
= access(path
, F_OK
);
2917 ret
= -LTTNG_ERR_INVALID
;
2918 WARN("Session configuration path does not exist.");
2921 ret
= -LTTNG_ERR_EPERM
;
2924 ret
= -LTTNG_ERR_UNK
;
2930 ret
= load_session_from_path(path
, session_name
,
2931 &validation_ctx
, override
);
2934 fini_session_config_validation_ctx(&validation_ctx
);
2935 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2937 * Don't report an error if no sessions are found when called
2938 * without a session_name or a search path.