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_filter_expression
= "filter_expression";
113 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
114 const char * const config_element_consumer_output
= "consumer_output";
115 const char * const config_element_destination
= "destination";
116 const char * const config_element_path
= "path";
117 const char * const config_element_net_output
= "net_output";
118 const char * const config_element_control_uri
= "control_uri";
119 const char * const config_element_data_uri
= "data_uri";
120 const char * const config_element_max_size
= "max_size";
121 const char * const config_element_pid
= "pid";
122 const char * const config_element_pids
= "pids";
123 const char * const config_element_shared_memory_path
= "shared_memory_path";
124 const char * const config_element_pid_tracker
= "pid_tracker";
125 const char * const config_element_trackers
= "trackers";
126 const char * const config_element_targets
= "targets";
127 const char * const config_element_target_pid
= "pid_target";
129 const char * const config_domain_type_kernel
= "KERNEL";
130 const char * const config_domain_type_ust
= "UST";
131 const char * const config_domain_type_jul
= "JUL";
132 const char * const config_domain_type_log4j
= "LOG4J";
133 const char * const config_domain_type_python
= "PYTHON";
135 const char * const config_buffer_type_per_pid
= "PER_PID";
136 const char * const config_buffer_type_per_uid
= "PER_UID";
137 const char * const config_buffer_type_global
= "GLOBAL";
139 const char * const config_overwrite_mode_discard
= "DISCARD";
140 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
142 const char * const config_output_type_splice
= "SPLICE";
143 const char * const config_output_type_mmap
= "MMAP";
145 const char * const config_loglevel_type_all
= "ALL";
146 const char * const config_loglevel_type_range
= "RANGE";
147 const char * const config_loglevel_type_single
= "SINGLE";
149 const char * const config_event_type_all
= "ALL";
150 const char * const config_event_type_tracepoint
= "TRACEPOINT";
151 const char * const config_event_type_probe
= "PROBE";
152 const char * const config_event_type_function
= "FUNCTION";
153 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
154 const char * const config_event_type_noop
= "NOOP";
155 const char * const config_event_type_syscall
= "SYSCALL";
156 const char * const config_event_type_kprobe
= "KPROBE";
157 const char * const config_event_type_kretprobe
= "KRETPROBE";
159 const char * const config_event_context_pid
= "PID";
160 const char * const config_event_context_procname
= "PROCNAME";
161 const char * const config_event_context_prio
= "PRIO";
162 const char * const config_event_context_nice
= "NICE";
163 const char * const config_event_context_vpid
= "VPID";
164 const char * const config_event_context_tid
= "TID";
165 const char * const config_event_context_vtid
= "VTID";
166 const char * const config_event_context_ppid
= "PPID";
167 const char * const config_event_context_vppid
= "VPPID";
168 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
169 const char * const config_event_context_hostname
= "HOSTNAME";
170 const char * const config_event_context_ip
= "IP";
171 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
172 const char * const config_event_context_app
= "APP";
175 struct consumer_output
{
182 static int config_entry_handler_filter(struct handler_filter_args
*args
,
183 const char *section
, const char *name
, const char *value
)
186 struct config_entry entry
= { section
, name
, value
};
190 if (!section
|| !name
|| !value
) {
196 if (strcmp(args
->section
, section
)) {
201 ret
= args
->handler(&entry
, args
->user_data
);
207 int config_get_section_entries(const char *override_path
, const char *section
,
208 config_entry_handler_cb handler
, void *user_data
)
212 FILE *config_file
= NULL
;
213 struct handler_filter_args filter
= { section
, handler
, user_data
};
215 /* First, try system-wide conf. file. */
216 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
218 config_file
= fopen(path
, "r");
220 DBG("Loading daemon conf file at %s", path
);
222 * Return value is not very important here since error or not, we
223 * continue and try the next possible conf. file.
225 (void) ini_parse_file(config_file
,
226 (ini_entry_handler
) config_entry_handler_filter
,
231 /* Second is the user local configuration. */
232 path
= utils_get_home_dir();
234 char fullpath
[PATH_MAX
];
236 ret
= snprintf(fullpath
, sizeof(fullpath
),
237 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
239 PERROR("snprintf user conf. path");
243 config_file
= fopen(fullpath
, "r");
245 DBG("Loading daemon user conf file at %s", path
);
247 * Return value is not very important here since error or not, we
248 * continue and try the next possible conf. file.
250 (void) ini_parse_file(config_file
,
251 (ini_entry_handler
) config_entry_handler_filter
,
257 /* Final path is the one that the user might have provided. */
259 config_file
= fopen(override_path
, "r");
261 DBG("Loading daemon command line conf file at %s", override_path
);
262 (void) ini_parse_file(config_file
,
263 (ini_entry_handler
) config_entry_handler_filter
,
267 ERR("Failed to open daemon configuration file at %s",
274 /* Everything went well. */
282 int config_parse_value(const char *value
)
285 char *endptr
, *lower_str
;
295 v
= strtoul(value
, &endptr
, 10);
296 if (endptr
!= value
) {
301 lower_str
= zmalloc(len
+ 1);
308 for (i
= 0; i
< len
; i
++) {
309 lower_str
[i
] = tolower(value
[i
]);
312 if (!strcmp(lower_str
, config_str_yes
) ||
313 !strcmp(lower_str
, config_str_true
) ||
314 !strcmp(lower_str
, config_str_on
)) {
316 } else if (!strcmp(lower_str
, config_str_no
) ||
317 !strcmp(lower_str
, config_str_false
) ||
318 !strcmp(lower_str
, config_str_off
)) {
330 * Returns a xmlChar string which must be released using xmlFree().
332 static xmlChar
*encode_string(const char *in_str
)
334 xmlChar
*out_str
= NULL
;
335 xmlCharEncodingHandlerPtr handler
;
336 int out_len
, ret
, in_len
;
340 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
342 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
346 in_len
= strlen(in_str
);
348 * Add 1 byte for the NULL terminted character. The factor 4 here is
349 * used because UTF-8 characters can take up to 4 bytes.
351 out_len
= (in_len
* 4) + 1;
352 out_str
= xmlMalloc(out_len
);
357 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
364 /* out_len is now the size of out_str */
365 out_str
[out_len
] = '\0';
371 struct config_writer
*config_writer_create(int fd_output
, int indent
)
374 struct config_writer
*writer
;
375 xmlOutputBufferPtr buffer
;
377 writer
= zmalloc(sizeof(struct config_writer
));
379 PERROR("zmalloc config_writer_create");
383 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
388 writer
->writer
= xmlNewTextWriter(buffer
);
389 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
390 config_xml_encoding
, NULL
);
395 ret
= xmlTextWriterSetIndentString(writer
->writer
,
396 BAD_CAST config_xml_indent_string
);
401 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
409 config_writer_destroy(writer
);
414 int config_writer_destroy(struct config_writer
*writer
)
423 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
424 WARN("Could not close XML document");
428 if (writer
->writer
) {
429 xmlFreeTextWriter(writer
->writer
);
438 int config_writer_open_element(struct config_writer
*writer
,
439 const char *element_name
)
442 xmlChar
*encoded_element_name
;
444 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
449 encoded_element_name
= encode_string(element_name
);
450 if (!encoded_element_name
) {
455 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
456 xmlFree(encoded_element_name
);
458 return ret
> 0 ? 0 : ret
;
462 int config_writer_close_element(struct config_writer
*writer
)
466 if (!writer
|| !writer
->writer
) {
471 ret
= xmlTextWriterEndElement(writer
->writer
);
473 return ret
> 0 ? 0 : ret
;
477 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
478 const char *element_name
, uint64_t value
)
481 xmlChar
*encoded_element_name
;
483 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
488 encoded_element_name
= encode_string(element_name
);
489 if (!encoded_element_name
) {
494 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
495 encoded_element_name
, "%" PRIu64
, value
);
496 xmlFree(encoded_element_name
);
498 return ret
> 0 ? 0 : ret
;
502 int config_writer_write_element_signed_int(struct config_writer
*writer
,
503 const char *element_name
, int64_t value
)
506 xmlChar
*encoded_element_name
;
508 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
513 encoded_element_name
= encode_string(element_name
);
514 if (!encoded_element_name
) {
519 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
520 encoded_element_name
, "%" PRIi64
, value
);
521 xmlFree(encoded_element_name
);
523 return ret
> 0 ? 0 : ret
;
527 int config_writer_write_element_bool(struct config_writer
*writer
,
528 const char *element_name
, int value
)
530 return config_writer_write_element_string(writer
, element_name
,
531 value
? config_xml_true
: config_xml_false
);
535 int config_writer_write_element_string(struct config_writer
*writer
,
536 const char *element_name
, const char *value
)
539 xmlChar
*encoded_element_name
= NULL
;
540 xmlChar
*encoded_value
= NULL
;
542 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
548 encoded_element_name
= encode_string(element_name
);
549 if (!encoded_element_name
) {
554 encoded_value
= encode_string(value
);
555 if (!encoded_value
) {
560 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
563 xmlFree(encoded_element_name
);
564 xmlFree(encoded_value
);
565 return ret
> 0 ? 0 : ret
;
569 void xml_error_handler(void *ctx
, const char *format
, ...)
575 va_start(args
, format
);
576 ret
= vasprintf(&errMsg
, format
, args
);
579 ERR("String allocation failed in xml error handler");
583 fprintf(stderr
, "XML Error: %s", errMsg
);
588 void fini_session_config_validation_ctx(
589 struct session_config_validation_ctx
*ctx
)
591 if (ctx
->parser_ctx
) {
592 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
596 xmlSchemaFree(ctx
->schema
);
599 if (ctx
->schema_validation_ctx
) {
600 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
603 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
607 char *get_session_config_xsd_path()
610 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
611 size_t base_path_len
;
615 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
618 base_path_len
= strlen(base_path
);
619 max_path_len
= base_path_len
+
620 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
621 xsd_path
= zmalloc(max_path_len
);
626 strncpy(xsd_path
, base_path
, max_path_len
);
627 if (xsd_path
[base_path_len
- 1] != '/') {
628 xsd_path
[base_path_len
++] = '/';
631 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
632 max_path_len
- base_path_len
);
638 int init_session_config_validation_ctx(
639 struct session_config_validation_ctx
*ctx
)
642 char *xsd_path
= get_session_config_xsd_path();
645 ret
= -LTTNG_ERR_NOMEM
;
649 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
650 if (!ctx
->parser_ctx
) {
651 ERR("XSD parser context creation failed");
652 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
655 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
656 xml_error_handler
, NULL
);
658 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
660 ERR("XSD parsing failed");
661 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
665 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
666 if (!ctx
->schema_validation_ctx
) {
667 ERR("XSD validation context creation failed");
668 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
672 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
673 xml_error_handler
, NULL
);
678 fini_session_config_validation_ctx(ctx
);
686 int parse_uint(xmlChar
*str
, uint64_t *val
)
696 *val
= strtoull((const char *) str
, &endptr
, 10);
697 if (!endptr
|| *endptr
) {
708 int parse_int(xmlChar
*str
, int64_t *val
)
718 *val
= strtoll((const char *) str
, &endptr
, 10);
719 if (!endptr
|| *endptr
) {
730 int parse_bool(xmlChar
*str
, int *val
)
739 if (!strcmp((const char *) str
, config_xml_true
)) {
741 } else if (!strcmp((const char *) str
, config_xml_false
)) {
744 WARN("Invalid boolean value encoutered (%s).",
753 int get_domain_type(xmlChar
*domain
)
761 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
762 ret
= LTTNG_DOMAIN_KERNEL
;
763 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
764 ret
= LTTNG_DOMAIN_UST
;
765 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
766 ret
= LTTNG_DOMAIN_JUL
;
767 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
768 ret
= LTTNG_DOMAIN_LOG4J
;
769 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
770 ret
= LTTNG_DOMAIN_PYTHON
;
781 int get_buffer_type(xmlChar
*buffer_type
)
789 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
790 ret
= LTTNG_BUFFER_GLOBAL
;
791 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
792 ret
= LTTNG_BUFFER_PER_UID
;
793 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
794 ret
= LTTNG_BUFFER_PER_PID
;
805 int get_overwrite_mode(xmlChar
*overwrite_mode
)
809 if (!overwrite_mode
) {
813 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
815 } else if (!strcmp((char *) overwrite_mode
,
816 config_overwrite_mode_discard
)) {
828 int get_output_type(xmlChar
*output_type
)
836 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
837 ret
= LTTNG_EVENT_MMAP
;
838 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
839 ret
= LTTNG_EVENT_SPLICE
;
850 int get_event_type(xmlChar
*event_type
)
858 if (!strcmp((char *) event_type
, config_event_type_all
)) {
859 ret
= LTTNG_EVENT_ALL
;
860 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
861 ret
= LTTNG_EVENT_TRACEPOINT
;
862 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
863 ret
= LTTNG_EVENT_PROBE
;
864 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
865 ret
= LTTNG_EVENT_FUNCTION
;
866 } else if (!strcmp((char *) event_type
,
867 config_event_type_function_entry
)) {
868 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
869 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
870 ret
= LTTNG_EVENT_NOOP
;
871 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
872 ret
= LTTNG_EVENT_SYSCALL
;
883 int get_loglevel_type(xmlChar
*loglevel_type
)
887 if (!loglevel_type
) {
891 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
892 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
893 } else if (!strcmp((char *) loglevel_type
,
894 config_loglevel_type_range
)) {
895 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
896 } else if (!strcmp((char *) loglevel_type
,
897 config_loglevel_type_single
)) {
898 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
909 * Return the context type or -1 on error.
912 int get_context_type(xmlChar
*context_type
)
920 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
921 ret
= LTTNG_EVENT_CONTEXT_PID
;
922 } else if (!strcmp((char *) context_type
,
923 config_event_context_procname
)) {
924 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
925 } else if (!strcmp((char *) context_type
,
926 config_event_context_prio
)) {
927 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
928 } else if (!strcmp((char *) context_type
,
929 config_event_context_nice
)) {
930 ret
= LTTNG_EVENT_CONTEXT_NICE
;
931 } else if (!strcmp((char *) context_type
,
932 config_event_context_vpid
)) {
933 ret
= LTTNG_EVENT_CONTEXT_VPID
;
934 } else if (!strcmp((char *) context_type
,
935 config_event_context_tid
)) {
936 ret
= LTTNG_EVENT_CONTEXT_TID
;
937 } else if (!strcmp((char *) context_type
,
938 config_event_context_vtid
)) {
939 ret
= LTTNG_EVENT_CONTEXT_VTID
;
940 } else if (!strcmp((char *) context_type
,
941 config_event_context_ppid
)) {
942 ret
= LTTNG_EVENT_CONTEXT_PPID
;
943 } else if (!strcmp((char *) context_type
,
944 config_event_context_vppid
)) {
945 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
946 } else if (!strcmp((char *) context_type
,
947 config_event_context_pthread_id
)) {
948 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
949 } else if (!strcmp((char *) context_type
,
950 config_event_context_hostname
)) {
951 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
952 } else if (!strcmp((char *) context_type
,
953 config_event_context_ip
)) {
954 ret
= LTTNG_EVENT_CONTEXT_IP
;
965 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
970 for (node
= xmlFirstElementChild(domain_node
); node
;
971 node
= xmlNextElementSibling(node
)) {
972 if (!strcmp((const char *) node
->name
, config_element_type
)) {
974 xmlChar
*node_content
= xmlNodeGetContent(node
);
976 ret
= -LTTNG_ERR_NOMEM
;
980 ret
= get_domain_type(node_content
);
983 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
988 } else if (!strcmp((const char *) node
->name
,
989 config_element_buffer_type
)) {
991 xmlChar
*node_content
= xmlNodeGetContent(node
);
993 ret
= -LTTNG_ERR_NOMEM
;
997 ret
= get_buffer_type(node_content
);
1000 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1004 domain
->buf_type
= ret
;
1013 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1018 for (node
= xmlFirstElementChild(net_output_node
); node
;
1019 node
= xmlNextElementSibling(node
)) {
1020 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1022 *control_uri
= (char *) xmlNodeGetContent(node
);
1023 if (!*control_uri
) {
1028 *data_uri
= (char *) xmlNodeGetContent(node
);
1035 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1039 int process_consumer_output(xmlNodePtr consumer_output_node
,
1040 struct consumer_output
*output
)
1047 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1048 node
= xmlNextElementSibling(node
)) {
1049 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1050 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1054 ret
= -LTTNG_ERR_NOMEM
;
1058 ret
= parse_bool(enabled_str
, &output
->enabled
);
1064 xmlNodePtr output_type_node
;
1067 output_type_node
= xmlFirstElementChild(node
);
1068 if (!output_type_node
) {
1069 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1073 if (!strcmp((const char *) output_type_node
->name
,
1074 config_element_path
)) {
1076 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1077 if (!output
->path
) {
1078 ret
= -LTTNG_ERR_NOMEM
;
1083 ret
= get_net_output_uris(output_type_node
,
1084 &output
->control_uri
, &output
->data_uri
);
1096 free(output
->control_uri
);
1097 free(output
->data_uri
);
1098 memset(output
, 0, sizeof(struct consumer_output
));
1104 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1105 const char *control_uri
, const char *data_uri
)
1108 struct lttng_handle
*handle
;
1109 const char *uri
= NULL
;
1114 handle
= lttng_create_handle(name
, domain
);
1116 ret
= -LTTNG_ERR_NOMEM
;
1120 if (!control_uri
|| !data_uri
) {
1121 uri
= control_uri
? control_uri
: data_uri
;
1126 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1127 lttng_destroy_handle(handle
);
1133 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1136 xmlNodePtr node
= NULL
;
1137 xmlNodePtr snapshot_output_list_node
;
1138 xmlNodePtr snapshot_output_node
;
1140 assert(session_name
);
1142 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1151 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1153 /* Parse and create snapshot outputs */
1155 for (snapshot_output_node
=
1156 xmlFirstElementChild(snapshot_output_list_node
);
1157 snapshot_output_node
; snapshot_output_node
=
1158 xmlNextElementSibling(snapshot_output_node
)) {
1160 uint64_t max_size
= UINT64_MAX
;
1161 struct consumer_output output
= { 0 };
1162 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1164 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1165 node
= xmlNextElementSibling(node
)) {
1166 if (!strcmp((const char *) node
->name
,
1167 config_element_name
)) {
1169 name
= (char *) xmlNodeGetContent(node
);
1171 ret
= -LTTNG_ERR_NOMEM
;
1172 goto error_snapshot_output
;
1174 } else if (!strcmp((const char *) node
->name
,
1175 config_element_max_size
)) {
1176 xmlChar
*content
= xmlNodeGetContent(node
);
1180 ret
= -LTTNG_ERR_NOMEM
;
1181 goto error_snapshot_output
;
1183 ret
= parse_uint(content
, &max_size
);
1186 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1187 goto error_snapshot_output
;
1190 /* consumer_output */
1191 ret
= process_consumer_output(node
, &output
);
1193 goto error_snapshot_output
;
1198 snapshot_output
= lttng_snapshot_output_create();
1199 if (!snapshot_output
) {
1200 ret
= -LTTNG_ERR_NOMEM
;
1201 goto error_snapshot_output
;
1204 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1206 goto error_snapshot_output
;
1209 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1211 goto error_snapshot_output
;
1215 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1218 goto error_snapshot_output
;
1221 if (output
.control_uri
) {
1222 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1225 goto error_snapshot_output
;
1229 if (output
.data_uri
) {
1230 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1233 goto error_snapshot_output
;
1238 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1239 error_snapshot_output
:
1242 free(output
.control_uri
);
1243 free(output
.data_uri
);
1244 lttng_snapshot_output_destroy(snapshot_output
);
1254 int create_session(const char *name
,
1255 struct lttng_domain
*kernel_domain
,
1256 struct lttng_domain
*ust_domain
,
1257 struct lttng_domain
*jul_domain
,
1258 struct lttng_domain
*log4j_domain
,
1259 xmlNodePtr output_node
,
1260 uint64_t live_timer_interval
)
1263 struct consumer_output output
= { 0 };
1264 xmlNodePtr consumer_output_node
;
1269 consumer_output_node
= xmlFirstElementChild(output_node
);
1270 if (!consumer_output_node
) {
1271 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1275 if (strcmp((const char *) consumer_output_node
->name
,
1276 config_element_consumer_output
)) {
1277 WARN("Invalid output type, expected %s node",
1278 config_element_consumer_output
);
1279 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1283 ret
= process_consumer_output(consumer_output_node
, &output
);
1289 if (live_timer_interval
!= UINT64_MAX
&&
1290 !output
.control_uri
&& !output
.data_uri
) {
1291 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1295 if (output
.control_uri
|| output
.data_uri
) {
1297 struct lttng_domain
*domain
;
1298 struct lttng_domain
*domains
[] =
1299 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1301 /* network destination */
1302 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1304 * URLs are provided for sure since the test above make sure that
1305 * with a live timer the data and control URIs are provided. So,
1306 * NULL is passed here and will be set right after.
1308 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1310 ret
= lttng_create_session(name
, NULL
);
1316 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1317 domain
= domains
[i
];
1322 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1329 /* either local output or no output */
1330 ret
= lttng_create_session(name
, output
.path
);
1337 free(output
.control_uri
);
1338 free(output
.data_uri
);
1342 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1343 struct lttng_event_probe_attr
*attr
)
1347 assert(probe_attribute_node
);
1350 if (!strcmp((const char *) probe_attribute_node
->name
,
1351 config_element_address
)) {
1356 content
= xmlNodeGetContent(probe_attribute_node
);
1358 ret
= -LTTNG_ERR_NOMEM
;
1362 ret
= parse_uint(content
, &addr
);
1365 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1370 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1371 config_element_offset
)) {
1373 uint64_t offset
= 0;
1376 content
= xmlNodeGetContent(probe_attribute_node
);
1378 ret
= -LTTNG_ERR_NOMEM
;
1382 ret
= parse_uint(content
, &offset
);
1385 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1389 attr
->offset
= offset
;
1390 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1391 config_element_symbol_name
)) {
1396 content
= xmlNodeGetContent(probe_attribute_node
);
1398 ret
= -LTTNG_ERR_NOMEM
;
1402 name_len
= strlen((char *) content
);
1403 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1404 WARN("symbol_name too long.");
1405 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1410 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1419 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1420 const char *channel_name
)
1424 struct lttng_event event
;
1425 char **exclusions
= NULL
;
1426 unsigned long exclusion_count
= 0;
1427 char *filter_expression
= NULL
;
1431 assert(channel_name
);
1433 memset(&event
, 0, sizeof(event
));
1435 /* Initialize default log level which varies by domain */
1436 switch (handle
->domain
.type
)
1438 case LTTNG_DOMAIN_JUL
:
1439 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1441 case LTTNG_DOMAIN_LOG4J
:
1442 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1444 case LTTNG_DOMAIN_PYTHON
:
1445 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1447 case LTTNG_DOMAIN_UST
:
1448 case LTTNG_DOMAIN_KERNEL
:
1449 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1455 for (node
= xmlFirstElementChild(event_node
); node
;
1456 node
= xmlNextElementSibling(node
)) {
1457 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1462 content
= xmlNodeGetContent(node
);
1464 ret
= -LTTNG_ERR_NOMEM
;
1468 name_len
= strlen((char *) content
);
1469 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1470 WARN("Channel name too long.");
1471 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1476 strncpy(event
.name
, (const char *) content
, name_len
);
1478 } else if (!strcmp((const char *) node
->name
,
1479 config_element_enabled
)) {
1480 xmlChar
*content
= xmlNodeGetContent(node
);
1484 ret
= -LTTNG_ERR_NOMEM
;
1488 ret
= parse_bool(content
, &event
.enabled
);
1491 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1494 } else if (!strcmp((const char *) node
->name
,
1495 config_element_type
)) {
1496 xmlChar
*content
= xmlNodeGetContent(node
);
1500 ret
= -LTTNG_ERR_NOMEM
;
1504 ret
= get_event_type(content
);
1507 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1512 } else if (!strcmp((const char *) node
->name
,
1513 config_element_loglevel_type
)) {
1514 xmlChar
*content
= xmlNodeGetContent(node
);
1518 ret
= -LTTNG_ERR_NOMEM
;
1522 ret
= get_loglevel_type(content
);
1525 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1529 event
.loglevel_type
= ret
;
1530 } else if (!strcmp((const char *) node
->name
,
1531 config_element_loglevel
)) {
1533 int64_t loglevel
= 0;
1536 content
= xmlNodeGetContent(node
);
1538 ret
= -LTTNG_ERR_NOMEM
;
1542 ret
= parse_int(content
, &loglevel
);
1545 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1549 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1550 WARN("loglevel out of range.");
1551 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1555 event
.loglevel
= loglevel
;
1556 } else if (!strcmp((const char *) node
->name
,
1557 config_element_filter
)) {
1559 xmlNodeGetContent(node
);
1563 ret
= -LTTNG_ERR_NOMEM
;
1567 filter_expression
= strdup((char *) content
);
1569 if (!filter_expression
) {
1570 ret
= -LTTNG_ERR_NOMEM
;
1573 } else if (!strcmp((const char *) node
->name
,
1574 config_element_exclusions
)) {
1575 xmlNodePtr exclusion_node
;
1576 int exclusion_index
= 0;
1581 * Exclusions has already been initialized,
1584 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1588 exclusion_count
= xmlChildElementCount(node
);
1589 if (!exclusion_count
) {
1593 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1595 exclusion_count
= 0;
1596 ret
= -LTTNG_ERR_NOMEM
;
1600 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1601 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1603 xmlNodeGetContent(exclusion_node
);
1606 ret
= -LTTNG_ERR_NOMEM
;
1610 exclusions
[exclusion_index
] = strdup((const char *) content
);
1612 if (!exclusions
[exclusion_index
]) {
1613 ret
= -LTTNG_ERR_NOMEM
;
1619 event
.exclusion
= 1;
1620 } else if (!strcmp((const char *) node
->name
,
1621 config_element_attributes
)) {
1622 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1625 if (!attribute_node
) {
1626 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1630 if (!strcmp((const char *) node
->name
,
1631 config_element_probe_attributes
)) {
1632 xmlNodePtr probe_attribute_node
;
1634 /* probe_attributes */
1635 for (probe_attribute_node
=
1636 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1637 probe_attribute_node
= xmlNextElementSibling(
1638 probe_attribute_node
)) {
1640 ret
= process_probe_attribute_node(probe_attribute_node
,
1649 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1651 /* function_attributes */
1652 content
= xmlNodeGetContent(symbol_node
);
1654 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1658 sym_len
= strlen((char *) content
);
1659 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1660 WARN("Function name too long.");
1661 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1666 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1673 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1674 filter_expression
, exclusion_count
, exclusions
);
1679 if (!event
.enabled
) {
1681 * Note that we should use lttng_disable_event_ext() (2.6+) to
1682 * eliminate the risk of clashing on events of the same
1683 * name (with different event types and loglevels).
1685 * Unfortunately, lttng_disable_event_ext() only performs a
1686 * match on the name and event type and errors out if any other
1687 * event attribute is not set to its default value.
1689 * This will disable all events that match this name.
1691 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1694 for (i
= 0; i
< exclusion_count
; i
++) {
1695 free(exclusions
[i
]);
1699 free(filter_expression
);
1704 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1705 const char *channel_name
)
1710 assert(events_node
);
1712 assert(channel_name
);
1714 for (node
= xmlFirstElementChild(events_node
); node
;
1715 node
= xmlNextElementSibling(node
)) {
1716 ret
= process_event_node(node
, handle
, channel_name
);
1726 int process_channel_attr_node(xmlNodePtr attr_node
,
1727 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1728 xmlNodePtr
*events_node
)
1734 assert(contexts_node
);
1735 assert(events_node
);
1737 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1742 content
= xmlNodeGetContent(attr_node
);
1744 ret
= -LTTNG_ERR_NOMEM
;
1748 name_len
= strlen((char *) content
);
1749 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1750 WARN("Channel name too long.");
1751 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1756 strncpy(channel
->name
, (const char *) content
, name_len
);
1758 } else if (!strcmp((const char *) attr_node
->name
,
1759 config_element_enabled
)) {
1764 content
= xmlNodeGetContent(attr_node
);
1766 ret
= -LTTNG_ERR_NOMEM
;
1770 ret
= parse_bool(content
, &enabled
);
1773 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1777 channel
->enabled
= enabled
;
1778 } else if (!strcmp((const char *) attr_node
->name
,
1779 config_element_overwrite_mode
)) {
1782 /* overwrite_mode */
1783 content
= xmlNodeGetContent(attr_node
);
1785 ret
= -LTTNG_ERR_NOMEM
;
1789 ret
= get_overwrite_mode(content
);
1792 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1796 channel
->attr
.overwrite
= ret
;
1797 } else if (!strcmp((const char *) attr_node
->name
,
1798 config_element_subbuf_size
)) {
1801 /* subbuffer_size */
1802 content
= xmlNodeGetContent(attr_node
);
1804 ret
= -LTTNG_ERR_NOMEM
;
1808 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1811 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1814 } else if (!strcmp((const char *) attr_node
->name
,
1815 config_element_num_subbuf
)) {
1818 /* subbuffer_count */
1819 content
= xmlNodeGetContent(attr_node
);
1821 ret
= -LTTNG_ERR_NOMEM
;
1825 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1828 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1831 } else if (!strcmp((const char *) attr_node
->name
,
1832 config_element_switch_timer_interval
)) {
1834 uint64_t switch_timer_interval
= 0;
1836 /* switch_timer_interval */
1837 content
= xmlNodeGetContent(attr_node
);
1839 ret
= -LTTNG_ERR_NOMEM
;
1843 ret
= parse_uint(content
, &switch_timer_interval
);
1846 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1850 if (switch_timer_interval
> UINT_MAX
) {
1851 WARN("switch_timer_interval out of range.");
1852 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1856 channel
->attr
.switch_timer_interval
=
1857 switch_timer_interval
;
1858 } else if (!strcmp((const char *) attr_node
->name
,
1859 config_element_read_timer_interval
)) {
1861 uint64_t read_timer_interval
= 0;
1863 /* read_timer_interval */
1864 content
= xmlNodeGetContent(attr_node
);
1866 ret
= -LTTNG_ERR_NOMEM
;
1870 ret
= parse_uint(content
, &read_timer_interval
);
1873 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1877 if (read_timer_interval
> UINT_MAX
) {
1878 WARN("read_timer_interval out of range.");
1879 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1883 channel
->attr
.read_timer_interval
=
1884 read_timer_interval
;
1885 } else if (!strcmp((const char *) attr_node
->name
,
1886 config_element_output_type
)) {
1890 content
= xmlNodeGetContent(attr_node
);
1892 ret
= -LTTNG_ERR_NOMEM
;
1896 ret
= get_output_type(content
);
1899 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1903 channel
->attr
.output
= ret
;
1904 } else if (!strcmp((const char *) attr_node
->name
,
1905 config_element_tracefile_size
)) {
1908 /* tracefile_size */
1909 content
= xmlNodeGetContent(attr_node
);
1911 ret
= -LTTNG_ERR_NOMEM
;
1915 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1918 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1921 } else if (!strcmp((const char *) attr_node
->name
,
1922 config_element_tracefile_count
)) {
1925 /* tracefile_count */
1926 content
= xmlNodeGetContent(attr_node
);
1928 ret
= -LTTNG_ERR_NOMEM
;
1932 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1935 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1938 } else if (!strcmp((const char *) attr_node
->name
,
1939 config_element_live_timer_interval
)) {
1941 uint64_t live_timer_interval
= 0;
1943 /* live_timer_interval */
1944 content
= xmlNodeGetContent(attr_node
);
1946 ret
= -LTTNG_ERR_NOMEM
;
1950 ret
= parse_uint(content
, &live_timer_interval
);
1953 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1957 if (live_timer_interval
> UINT_MAX
) {
1958 WARN("live_timer_interval out of range.");
1959 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1963 channel
->attr
.live_timer_interval
=
1964 live_timer_interval
;
1965 } else if (!strcmp((const char *) attr_node
->name
,
1966 config_element_events
)) {
1968 *events_node
= attr_node
;
1971 *contexts_node
= attr_node
;
1979 int process_context_node(xmlNodePtr context_node
,
1980 struct lttng_handle
*handle
, const char *channel_name
)
1983 struct lttng_event_context context
;
1984 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1987 assert(channel_name
);
1989 if (!context_child_node
) {
1990 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1994 memset(&context
, 0, sizeof(context
));
1996 if (!strcmp((const char *) context_child_node
->name
,
1997 config_element_type
)) {
1999 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2002 ret
= -LTTNG_ERR_NOMEM
;
2006 ret
= get_context_type(content
);
2009 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2014 } else if (!strcmp((const char *) context_child_node
->name
,
2015 config_element_context_perf
)) {
2017 xmlNodePtr perf_attr_node
;
2019 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2020 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2021 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2022 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2023 perf_attr_node
; perf_attr_node
=
2024 xmlNextElementSibling(perf_attr_node
)) {
2025 if (!strcmp((const char *) perf_attr_node
->name
,
2026 config_element_type
)) {
2031 content
= xmlNodeGetContent(perf_attr_node
);
2033 ret
= -LTTNG_ERR_NOMEM
;
2037 ret
= parse_uint(content
, &type
);
2040 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2044 if (type
> UINT32_MAX
) {
2045 WARN("perf context type out of range.");
2046 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2050 context
.u
.perf_counter
.type
= type
;
2051 } else if (!strcmp((const char *) perf_attr_node
->name
,
2052 config_element_config
)) {
2054 uint64_t config
= 0;
2057 content
= xmlNodeGetContent(perf_attr_node
);
2059 ret
= -LTTNG_ERR_NOMEM
;
2063 ret
= parse_uint(content
, &config
);
2066 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2070 context
.u
.perf_counter
.config
= config
;
2071 } else if (!strcmp((const char *) perf_attr_node
->name
,
2072 config_element_name
)) {
2077 content
= xmlNodeGetContent(perf_attr_node
);
2079 ret
= -LTTNG_ERR_NOMEM
;
2083 name_len
= strlen((char *) content
);
2084 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2085 WARN("perf context name too long.");
2086 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2091 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2096 } else if (!strcmp((const char *) context_child_node
->name
,
2097 config_element_context_app
)) {
2098 /* application context */
2099 xmlNodePtr app_ctx_node
;
2101 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2102 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2103 app_ctx_node
; app_ctx_node
=
2104 xmlNextElementSibling(app_ctx_node
)) {
2106 char **target
= strcmp(
2107 (const char *) app_ctx_node
->name
,
2108 config_element_context_app_provider_name
) == 0 ?
2109 &context
.u
.app_ctx
.provider_name
:
2110 &context
.u
.app_ctx
.ctx_name
;
2112 content
= xmlNodeGetContent(app_ctx_node
);
2114 ret
= -LTTNG_ERR_NOMEM
;
2118 *target
= (char *) content
;
2121 /* Unrecognized context type */
2122 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2126 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2127 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2128 free(context
.u
.app_ctx
.provider_name
);
2129 free(context
.u
.app_ctx
.ctx_name
);
2136 int process_contexts_node(xmlNodePtr contexts_node
,
2137 struct lttng_handle
*handle
, const char *channel_name
)
2140 xmlNodePtr context_node
;
2142 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2143 context_node
= xmlNextElementSibling(context_node
)) {
2144 ret
= process_context_node(context_node
, handle
, channel_name
);
2154 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2155 struct lttng_handle
*handle
)
2158 xmlNodePtr targets_node
= NULL
;
2162 assert(pid_tracker_node
);
2163 /* get the targets node */
2164 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2165 node
= xmlNextElementSibling(node
)) {
2166 if (!strcmp((const char *) node
->name
,
2167 config_element_targets
)) {
2168 targets_node
= node
;
2173 if (!targets_node
) {
2174 ret
= LTTNG_ERR_INVALID
;
2178 /* Go through all pid_target node */
2179 child
= xmlChildElementCount(targets_node
);
2181 /* The session is explicitly set to target nothing. */
2182 ret
= lttng_untrack_pid(handle
, -1);
2187 for (node
= xmlFirstElementChild(targets_node
); node
;
2188 node
= xmlNextElementSibling(node
)) {
2189 xmlNodePtr pid_target_node
= node
;
2191 /* get pid node and track it */
2192 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2193 node
= xmlNextElementSibling(node
)) {
2194 if (!strcmp((const char *) node
->name
,
2195 config_element_pid
)) {
2197 xmlChar
*content
= NULL
;
2199 content
= xmlNodeGetContent(node
);
2201 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2205 ret
= parse_int(content
, &pid
);
2208 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2212 ret
= lttng_track_pid(handle
, (int) pid
);
2218 node
= pid_target_node
;
2227 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2230 struct lttng_domain domain
= { 0 };
2231 struct lttng_handle
*handle
= NULL
;
2232 xmlNodePtr channels_node
= NULL
;
2233 xmlNodePtr trackers_node
= NULL
;
2234 xmlNodePtr pid_tracker_node
= NULL
;
2237 assert(session_name
);
2239 ret
= init_domain(domain_node
, &domain
);
2244 handle
= lttng_create_handle(session_name
, &domain
);
2246 ret
= -LTTNG_ERR_NOMEM
;
2250 /* get the channels node */
2251 for (node
= xmlFirstElementChild(domain_node
); node
;
2252 node
= xmlNextElementSibling(node
)) {
2253 if (!strcmp((const char *) node
->name
,
2254 config_element_channels
)) {
2255 channels_node
= node
;
2260 if (!channels_node
) {
2264 /* create all channels */
2265 for (node
= xmlFirstElementChild(channels_node
); node
;
2266 node
= xmlNextElementSibling(node
)) {
2267 struct lttng_channel channel
;
2268 xmlNodePtr contexts_node
= NULL
;
2269 xmlNodePtr events_node
= NULL
;
2270 xmlNodePtr channel_attr_node
;
2272 memset(&channel
, 0, sizeof(channel
));
2273 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2275 for (channel_attr_node
= xmlFirstElementChild(node
);
2276 channel_attr_node
; channel_attr_node
=
2277 xmlNextElementSibling(channel_attr_node
)) {
2278 ret
= process_channel_attr_node(channel_attr_node
,
2279 &channel
, &contexts_node
, &events_node
);
2285 ret
= lttng_enable_channel(handle
, &channel
);
2290 ret
= process_events_node(events_node
, handle
, channel
.name
);
2295 ret
= process_contexts_node(contexts_node
, handle
,
2302 /* get the trackers node */
2303 for (node
= xmlFirstElementChild(domain_node
); node
;
2304 node
= xmlNextElementSibling(node
)) {
2305 if (!strcmp((const char *) node
->name
,
2306 config_element_trackers
)) {
2307 trackers_node
= node
;
2312 if (!trackers_node
) {
2316 for (node
= xmlFirstElementChild(trackers_node
); node
;
2317 node
= xmlNextElementSibling(node
)) {
2318 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2319 pid_tracker_node
= node
;
2320 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2327 if (!pid_tracker_node
) {
2328 lttng_track_pid(handle
, -1);
2332 lttng_destroy_handle(handle
);
2337 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2340 int ret
, started
= -1, snapshot_mode
= -1;
2341 uint64_t live_timer_interval
= UINT64_MAX
;
2342 xmlChar
*name
= NULL
;
2343 xmlChar
*shm_path
= NULL
;
2344 xmlNodePtr domains_node
= NULL
;
2345 xmlNodePtr output_node
= NULL
;
2347 struct lttng_domain
*kernel_domain
= NULL
;
2348 struct lttng_domain
*ust_domain
= NULL
;
2349 struct lttng_domain
*jul_domain
= NULL
;
2350 struct lttng_domain
*log4j_domain
= NULL
;
2351 struct lttng_domain
*python_domain
= NULL
;
2353 for (node
= xmlFirstElementChild(session_node
); node
;
2354 node
= xmlNextElementSibling(node
)) {
2355 if (!name
&& !strcmp((const char *) node
->name
,
2356 config_element_name
)) {
2358 xmlChar
*node_content
= xmlNodeGetContent(node
);
2359 if (!node_content
) {
2360 ret
= -LTTNG_ERR_NOMEM
;
2364 name
= node_content
;
2365 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2366 config_element_domains
)) {
2368 domains_node
= node
;
2369 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2370 config_element_started
)) {
2372 xmlChar
*node_content
= xmlNodeGetContent(node
);
2373 if (!node_content
) {
2374 ret
= -LTTNG_ERR_NOMEM
;
2378 ret
= parse_bool(node_content
, &started
);
2381 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2384 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2385 config_element_output
)) {
2388 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2389 config_element_shared_memory_path
)) {
2390 /* shared memory path */
2391 xmlChar
*node_content
= xmlNodeGetContent(node
);
2392 if (!node_content
) {
2393 ret
= -LTTNG_ERR_NOMEM
;
2397 shm_path
= node_content
;
2399 /* attributes, snapshot_mode or live_timer_interval */
2400 xmlNodePtr attributes_child
=
2401 xmlFirstElementChild(node
);
2403 if (!strcmp((const char *) attributes_child
->name
,
2404 config_element_snapshot_mode
)) {
2406 xmlChar
*snapshot_mode_content
=
2407 xmlNodeGetContent(attributes_child
);
2408 if (!snapshot_mode_content
) {
2409 ret
= -LTTNG_ERR_NOMEM
;
2413 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2414 free(snapshot_mode_content
);
2416 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2420 /* live_timer_interval */
2421 xmlChar
*timer_interval_content
=
2422 xmlNodeGetContent(attributes_child
);
2423 if (!timer_interval_content
) {
2424 ret
= -LTTNG_ERR_NOMEM
;
2428 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2429 free(timer_interval_content
);
2431 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2439 /* Mandatory attribute, as defined in the session XSD */
2440 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2444 if (session_name
&& strcmp((char *) name
, session_name
)) {
2445 /* This is not the session we are looking for */
2446 ret
= -LTTNG_ERR_NO_SESSION
;
2450 /* Init domains to create the session handles */
2451 for (node
= xmlFirstElementChild(domains_node
); node
;
2452 node
= xmlNextElementSibling(node
)) {
2453 struct lttng_domain
*domain
;
2455 domain
= zmalloc(sizeof(*domain
));
2457 ret
= -LTTNG_ERR_NOMEM
;
2461 ret
= init_domain(node
, domain
);
2463 goto domain_init_error
;
2466 switch (domain
->type
) {
2467 case LTTNG_DOMAIN_KERNEL
:
2468 if (kernel_domain
) {
2469 /* Same domain seen twice, invalid! */
2470 goto domain_init_error
;
2472 kernel_domain
= domain
;
2474 case LTTNG_DOMAIN_UST
:
2476 /* Same domain seen twice, invalid! */
2477 goto domain_init_error
;
2479 ust_domain
= domain
;
2481 case LTTNG_DOMAIN_JUL
:
2483 /* Same domain seen twice, invalid! */
2484 goto domain_init_error
;
2486 jul_domain
= domain
;
2488 case LTTNG_DOMAIN_LOG4J
:
2490 /* Same domain seen twice, invalid! */
2491 goto domain_init_error
;
2493 log4j_domain
= domain
;
2495 case LTTNG_DOMAIN_PYTHON
:
2496 if (python_domain
) {
2497 /* Same domain seen twice, invalid! */
2498 goto domain_init_error
;
2500 python_domain
= domain
;
2503 WARN("Invalid domain type");
2504 goto domain_init_error
;
2509 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2514 /* Destroy session if it exists */
2515 ret
= lttng_destroy_session((const char *) name
);
2516 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2517 ERR("Failed to destroy existing session.");
2522 /* Create session type depending on output type */
2523 if (snapshot_mode
&& snapshot_mode
!= -1) {
2524 ret
= create_snapshot_session((const char *) name
, output_node
);
2525 } else if (live_timer_interval
&&
2526 live_timer_interval
!= UINT64_MAX
) {
2527 ret
= create_session((const char *) name
, kernel_domain
,
2528 ust_domain
, jul_domain
, log4j_domain
,
2529 output_node
, live_timer_interval
);
2531 /* regular session */
2532 ret
= create_session((const char *) name
, kernel_domain
,
2533 ust_domain
, jul_domain
, log4j_domain
,
2534 output_node
, UINT64_MAX
);
2541 ret
= lttng_set_session_shm_path((const char *) name
,
2542 (const char *) shm_path
);
2548 for (node
= xmlFirstElementChild(domains_node
); node
;
2549 node
= xmlNextElementSibling(node
)) {
2550 ret
= process_domain_node(node
, (const char *) name
);
2557 ret
= lttng_start_tracing((const char *) name
);
2565 ERR("Failed to load session %s: %s", (const char *) name
,
2566 lttng_strerror(ret
));
2567 lttng_destroy_session((const char *) name
);
2571 free(kernel_domain
);
2575 free(python_domain
);
2582 * Return 1 if the given path is readable by the current UID or 0 if not.
2583 * Return -1 if the path is EPERM.
2585 static int validate_file_read_creds(const char *path
)
2591 /* Can we read the file. */
2592 ret
= access(path
, R_OK
);
2596 if (errno
== EACCES
) {
2607 int load_session_from_file(const char *path
, const char *session_name
,
2608 struct session_config_validation_ctx
*validation_ctx
, int override
)
2610 int ret
, session_found
= !session_name
;
2611 xmlDocPtr doc
= NULL
;
2612 xmlNodePtr sessions_node
;
2613 xmlNodePtr session_node
;
2616 assert(validation_ctx
);
2618 ret
= validate_file_read_creds(path
);
2621 ret
= -LTTNG_ERR_EPERM
;
2623 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2628 doc
= xmlParseFile(path
);
2630 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2634 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2636 ERR("Session configuration file validation failed");
2637 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2641 sessions_node
= xmlDocGetRootElement(doc
);
2642 if (!sessions_node
) {
2646 for (session_node
= xmlFirstElementChild(sessions_node
);
2647 session_node
; session_node
=
2648 xmlNextElementSibling(session_node
)) {
2649 ret
= process_session_node(session_node
,
2650 session_name
, override
);
2651 if (session_name
&& ret
== 0) {
2652 /* Target session found and loaded */
2660 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2665 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2667 struct dirent
*alloc_dirent(const char *path
)
2671 struct dirent
*entry
;
2673 name_max
= pathconf(path
, _PC_NAME_MAX
);
2674 if (name_max
== -1) {
2675 name_max
= PATH_MAX
;
2677 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2678 entry
= zmalloc(len
);
2683 int load_session_from_path(const char *path
, const char *session_name
,
2684 struct session_config_validation_ctx
*validation_ctx
, int override
)
2686 int ret
, session_found
= !session_name
;
2687 DIR *directory
= NULL
;
2690 assert(validation_ctx
);
2692 directory
= opendir(path
);
2696 /* Try the file loading. */
2699 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2702 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2707 struct dirent
*entry
;
2708 struct dirent
*result
;
2709 char *file_path
= NULL
;
2710 size_t path_len
= strlen(path
);
2712 if (path_len
>= PATH_MAX
) {
2713 ret
= -LTTNG_ERR_INVALID
;
2717 entry
= alloc_dirent(path
);
2719 ret
= -LTTNG_ERR_NOMEM
;
2723 file_path
= zmalloc(PATH_MAX
);
2725 ret
= -LTTNG_ERR_NOMEM
;
2730 strncpy(file_path
, path
, path_len
);
2731 if (file_path
[path_len
- 1] != '/') {
2732 file_path
[path_len
++] = '/';
2736 /* Search for *.lttng files */
2737 while (!readdir_r(directory
, entry
, &result
) && result
) {
2738 size_t file_name_len
= strlen(result
->d_name
);
2740 if (file_name_len
<=
2741 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2745 if (path_len
+ file_name_len
>= PATH_MAX
) {
2749 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2750 result
->d_name
+ file_name_len
- sizeof(
2751 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2755 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2756 file_path
[path_len
+ file_name_len
] = '\0';
2758 ret
= load_session_from_file(file_path
, session_name
,
2759 validation_ctx
, override
);
2760 if (session_name
&& !ret
) {
2769 ret
= load_session_from_file(path
, session_name
,
2770 validation_ctx
, override
);
2780 if (closedir(directory
)) {
2785 if (!session_found
) {
2786 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2793 * Validate that the given path's credentials and the current process have the
2794 * same UID. If so, return 1 else return 0 if it does NOT match.
2796 static int validate_path_creds(const char *path
)
2798 int ret
, uid
= getuid();
2807 ret
= stat(path
, &buf
);
2809 if (errno
!= ENOENT
) {
2812 ret
= -LTTNG_ERR_INVALID
;
2816 if (buf
.st_uid
!= uid
) {
2827 int config_load_session(const char *path
, const char *session_name
,
2828 int override
, unsigned int autoload
)
2831 const char *path_ptr
= NULL
;
2832 struct session_config_validation_ctx validation_ctx
= { 0 };
2834 ret
= init_session_config_validation_ctx(&validation_ctx
);
2841 const char *sys_path
;
2844 home_path
= utils_get_home_dir();
2846 char path
[PATH_MAX
];
2849 * Try user session configuration path. Ignore error here so we can
2850 * continue loading the system wide sessions.
2853 ret
= snprintf(path
, sizeof(path
),
2854 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2855 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2857 PERROR("snprintf session autoload home config path");
2862 * Credentials are only validated for the autoload in order to
2863 * avoid any user session daemon to try to load kernel sessions
2864 * automatically and failing all the times.
2866 ret
= validate_path_creds(path
);
2871 ret
= snprintf(path
, sizeof(path
),
2872 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2874 PERROR("snprintf session home config path");
2880 ret
= load_session_from_path(path_ptr
, session_name
,
2881 &validation_ctx
, override
);
2882 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2886 * Continue even if the session was found since we have to try
2887 * the system wide sessions.
2892 /* Reset path pointer for the system wide dir. */
2895 /* Try system wide configuration directory. */
2897 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2898 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2899 ret
= validate_path_creds(sys_path
);
2901 path_ptr
= sys_path
;
2904 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2905 path_ptr
= sys_path
;
2909 ret
= load_session_from_path(path_ptr
, session_name
,
2910 &validation_ctx
, override
);
2913 ret
= access(path
, F_OK
);
2918 ret
= -LTTNG_ERR_INVALID
;
2919 WARN("Session configuration path does not exist.");
2922 ret
= -LTTNG_ERR_EPERM
;
2925 ret
= -LTTNG_ERR_UNK
;
2931 ret
= load_session_from_path(path
, session_name
,
2932 &validation_ctx
, override
);
2935 fini_session_config_validation_ctx(&validation_ctx
);
2936 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2938 * Don't report an error if no sessions are found when called
2939 * without a session_name or a search path.