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_perf
= "perf";
103 const char * const config_element_config
= "config";
104 const char * const config_element_started
= "started";
105 const char * const config_element_snapshot_mode
= "snapshot_mode";
106 const char * const config_element_loglevel
= "loglevel";
107 const char * const config_element_loglevel_type
= "loglevel_type";
108 const char * const config_element_filter
= "filter";
109 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
110 const char * const config_element_consumer_output
= "consumer_output";
111 const char * const config_element_destination
= "destination";
112 const char * const config_element_path
= "path";
113 const char * const config_element_net_output
= "net_output";
114 const char * const config_element_control_uri
= "control_uri";
115 const char * const config_element_data_uri
= "data_uri";
116 const char * const config_element_max_size
= "max_size";
117 const char * const config_element_pid
= "pid";
118 const char * const config_element_pids
= "pids";
119 const char * const config_element_shared_memory_path
= "shared_memory_path";
120 const char * const config_element_pid_tracker
= "pid_tracker";
121 const char * const config_element_trackers
= "trackers";
122 const char * const config_element_targets
= "targets";
123 const char * const config_element_target_pid
= "pid_target";
125 const char * const config_domain_type_kernel
= "KERNEL";
126 const char * const config_domain_type_ust
= "UST";
127 const char * const config_domain_type_jul
= "JUL";
128 const char * const config_domain_type_log4j
= "LOG4J";
129 const char * const config_domain_type_python
= "PYTHON";
131 const char * const config_buffer_type_per_pid
= "PER_PID";
132 const char * const config_buffer_type_per_uid
= "PER_UID";
133 const char * const config_buffer_type_global
= "GLOBAL";
135 const char * const config_overwrite_mode_discard
= "DISCARD";
136 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
138 const char * const config_output_type_splice
= "SPLICE";
139 const char * const config_output_type_mmap
= "MMAP";
141 const char * const config_loglevel_type_all
= "ALL";
142 const char * const config_loglevel_type_range
= "RANGE";
143 const char * const config_loglevel_type_single
= "SINGLE";
145 const char * const config_event_type_all
= "ALL";
146 const char * const config_event_type_tracepoint
= "TRACEPOINT";
147 const char * const config_event_type_probe
= "PROBE";
148 const char * const config_event_type_function
= "FUNCTION";
149 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
150 const char * const config_event_type_noop
= "NOOP";
151 const char * const config_event_type_syscall
= "SYSCALL";
152 const char * const config_event_type_kprobe
= "KPROBE";
153 const char * const config_event_type_kretprobe
= "KRETPROBE";
155 const char * const config_event_context_pid
= "PID";
156 const char * const config_event_context_procname
= "PROCNAME";
157 const char * const config_event_context_prio
= "PRIO";
158 const char * const config_event_context_nice
= "NICE";
159 const char * const config_event_context_vpid
= "VPID";
160 const char * const config_event_context_tid
= "TID";
161 const char * const config_event_context_vtid
= "VTID";
162 const char * const config_event_context_ppid
= "PPID";
163 const char * const config_event_context_vppid
= "VPPID";
164 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
165 const char * const config_event_context_hostname
= "HOSTNAME";
166 const char * const config_event_context_ip
= "IP";
167 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
169 struct consumer_output
{
176 static int config_entry_handler_filter(struct handler_filter_args
*args
,
177 const char *section
, const char *name
, const char *value
)
180 struct config_entry entry
= { section
, name
, value
};
184 if (!section
|| !name
|| !value
) {
190 if (strcmp(args
->section
, section
)) {
195 ret
= args
->handler(&entry
, args
->user_data
);
201 int config_get_section_entries(const char *override_path
, const char *section
,
202 config_entry_handler_cb handler
, void *user_data
)
206 FILE *config_file
= NULL
;
207 struct handler_filter_args filter
= { section
, handler
, user_data
};
209 /* First, try system-wide conf. file. */
210 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
212 config_file
= fopen(path
, "r");
214 DBG("Loading daemon conf file at %s", path
);
216 * Return value is not very important here since error or not, we
217 * continue and try the next possible conf. file.
219 (void) ini_parse_file(config_file
,
220 (ini_entry_handler
) config_entry_handler_filter
,
225 /* Second is the user local configuration. */
226 path
= utils_get_home_dir();
228 char fullpath
[PATH_MAX
];
230 ret
= snprintf(fullpath
, sizeof(fullpath
),
231 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
233 PERROR("snprintf user conf. path");
237 config_file
= fopen(fullpath
, "r");
239 DBG("Loading daemon user conf file at %s", path
);
241 * Return value is not very important here since error or not, we
242 * continue and try the next possible conf. file.
244 (void) ini_parse_file(config_file
,
245 (ini_entry_handler
) config_entry_handler_filter
,
251 /* Final path is the one that the user might have provided. */
253 config_file
= fopen(override_path
, "r");
255 DBG("Loading daemon command line conf file at %s", override_path
);
256 (void) ini_parse_file(config_file
,
257 (ini_entry_handler
) config_entry_handler_filter
,
261 ERR("Failed to open daemon configuration file at %s",
268 /* Everything went well. */
276 int config_parse_value(const char *value
)
279 char *endptr
, *lower_str
;
289 v
= strtoul(value
, &endptr
, 10);
290 if (endptr
!= value
) {
295 lower_str
= zmalloc(len
+ 1);
302 for (i
= 0; i
< len
; i
++) {
303 lower_str
[i
] = tolower(value
[i
]);
306 if (!strcmp(lower_str
, config_str_yes
) ||
307 !strcmp(lower_str
, config_str_true
) ||
308 !strcmp(lower_str
, config_str_on
)) {
310 } else if (!strcmp(lower_str
, config_str_no
) ||
311 !strcmp(lower_str
, config_str_false
) ||
312 !strcmp(lower_str
, config_str_off
)) {
324 * Returns a xmlChar string which must be released using xmlFree().
326 static xmlChar
*encode_string(const char *in_str
)
328 xmlChar
*out_str
= NULL
;
329 xmlCharEncodingHandlerPtr handler
;
330 int out_len
, ret
, in_len
;
334 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
336 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
340 in_len
= strlen(in_str
);
342 * Add 1 byte for the NULL terminted character. The factor 4 here is
343 * used because UTF-8 characters can take up to 4 bytes.
345 out_len
= (in_len
* 4) + 1;
346 out_str
= xmlMalloc(out_len
);
351 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
358 /* out_len is now the size of out_str */
359 out_str
[out_len
] = '\0';
365 struct config_writer
*config_writer_create(int fd_output
, int indent
)
368 struct config_writer
*writer
;
369 xmlOutputBufferPtr buffer
;
371 writer
= zmalloc(sizeof(struct config_writer
));
373 PERROR("zmalloc config_writer_create");
377 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
382 writer
->writer
= xmlNewTextWriter(buffer
);
383 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
384 config_xml_encoding
, NULL
);
389 ret
= xmlTextWriterSetIndentString(writer
->writer
,
390 BAD_CAST config_xml_indent_string
);
395 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
403 config_writer_destroy(writer
);
408 int config_writer_destroy(struct config_writer
*writer
)
417 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
418 WARN("Could not close XML document");
422 if (writer
->writer
) {
423 xmlFreeTextWriter(writer
->writer
);
432 int config_writer_open_element(struct config_writer
*writer
,
433 const char *element_name
)
436 xmlChar
*encoded_element_name
;
438 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
443 encoded_element_name
= encode_string(element_name
);
444 if (!encoded_element_name
) {
449 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
450 xmlFree(encoded_element_name
);
452 return ret
> 0 ? 0 : ret
;
456 int config_writer_close_element(struct config_writer
*writer
)
460 if (!writer
|| !writer
->writer
) {
465 ret
= xmlTextWriterEndElement(writer
->writer
);
467 return ret
> 0 ? 0 : ret
;
471 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
472 const char *element_name
, uint64_t value
)
475 xmlChar
*encoded_element_name
;
477 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
482 encoded_element_name
= encode_string(element_name
);
483 if (!encoded_element_name
) {
488 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
489 encoded_element_name
, "%" PRIu64
, value
);
490 xmlFree(encoded_element_name
);
492 return ret
> 0 ? 0 : ret
;
496 int config_writer_write_element_signed_int(struct config_writer
*writer
,
497 const char *element_name
, int64_t value
)
500 xmlChar
*encoded_element_name
;
502 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
507 encoded_element_name
= encode_string(element_name
);
508 if (!encoded_element_name
) {
513 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
514 encoded_element_name
, "%" PRIi64
, value
);
515 xmlFree(encoded_element_name
);
517 return ret
> 0 ? 0 : ret
;
521 int config_writer_write_element_bool(struct config_writer
*writer
,
522 const char *element_name
, int value
)
524 return config_writer_write_element_string(writer
, element_name
,
525 value
? config_xml_true
: config_xml_false
);
529 int config_writer_write_element_string(struct config_writer
*writer
,
530 const char *element_name
, const char *value
)
533 xmlChar
*encoded_element_name
= NULL
;
534 xmlChar
*encoded_value
= NULL
;
536 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
542 encoded_element_name
= encode_string(element_name
);
543 if (!encoded_element_name
) {
548 encoded_value
= encode_string(value
);
549 if (!encoded_value
) {
554 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
557 xmlFree(encoded_element_name
);
558 xmlFree(encoded_value
);
559 return ret
> 0 ? 0 : ret
;
563 void xml_error_handler(void *ctx
, const char *format
, ...)
569 va_start(args
, format
);
570 ret
= vasprintf(&errMsg
, format
, args
);
573 ERR("String allocation failed in xml error handler");
577 fprintf(stderr
, "XML Error: %s", errMsg
);
582 void fini_session_config_validation_ctx(
583 struct session_config_validation_ctx
*ctx
)
585 if (ctx
->parser_ctx
) {
586 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
590 xmlSchemaFree(ctx
->schema
);
593 if (ctx
->schema_validation_ctx
) {
594 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
597 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
601 char *get_session_config_xsd_path()
604 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
605 size_t base_path_len
;
609 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
612 base_path_len
= strlen(base_path
);
613 max_path_len
= base_path_len
+
614 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
615 xsd_path
= zmalloc(max_path_len
);
620 strncpy(xsd_path
, base_path
, max_path_len
);
621 if (xsd_path
[base_path_len
- 1] != '/') {
622 xsd_path
[base_path_len
++] = '/';
625 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
626 max_path_len
- base_path_len
);
632 int init_session_config_validation_ctx(
633 struct session_config_validation_ctx
*ctx
)
636 char *xsd_path
= get_session_config_xsd_path();
639 ret
= -LTTNG_ERR_NOMEM
;
643 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
644 if (!ctx
->parser_ctx
) {
645 ERR("XSD parser context creation failed");
646 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
649 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
650 xml_error_handler
, NULL
);
652 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
654 ERR("XSD parsing failed");
655 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
659 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
660 if (!ctx
->schema_validation_ctx
) {
661 ERR("XSD validation context creation failed");
662 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
666 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
667 xml_error_handler
, NULL
);
672 fini_session_config_validation_ctx(ctx
);
680 int parse_uint(xmlChar
*str
, uint64_t *val
)
690 *val
= strtoull((const char *) str
, &endptr
, 10);
691 if (!endptr
|| *endptr
) {
702 int parse_int(xmlChar
*str
, int64_t *val
)
712 *val
= strtoll((const char *) str
, &endptr
, 10);
713 if (!endptr
|| *endptr
) {
724 int parse_bool(xmlChar
*str
, int *val
)
733 if (!strcmp((const char *) str
, config_xml_true
)) {
735 } else if (!strcmp((const char *) str
, config_xml_false
)) {
738 WARN("Invalid boolean value encoutered (%s).",
747 int get_domain_type(xmlChar
*domain
)
755 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
756 ret
= LTTNG_DOMAIN_KERNEL
;
757 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
758 ret
= LTTNG_DOMAIN_UST
;
759 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
760 ret
= LTTNG_DOMAIN_JUL
;
761 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
762 ret
= LTTNG_DOMAIN_LOG4J
;
763 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
764 ret
= LTTNG_DOMAIN_PYTHON
;
775 int get_buffer_type(xmlChar
*buffer_type
)
783 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
784 ret
= LTTNG_BUFFER_GLOBAL
;
785 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
786 ret
= LTTNG_BUFFER_PER_UID
;
787 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
788 ret
= LTTNG_BUFFER_PER_PID
;
799 int get_overwrite_mode(xmlChar
*overwrite_mode
)
803 if (!overwrite_mode
) {
807 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
809 } else if (!strcmp((char *) overwrite_mode
,
810 config_overwrite_mode_discard
)) {
822 int get_output_type(xmlChar
*output_type
)
830 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
831 ret
= LTTNG_EVENT_MMAP
;
832 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
833 ret
= LTTNG_EVENT_SPLICE
;
844 int get_event_type(xmlChar
*event_type
)
852 if (!strcmp((char *) event_type
, config_event_type_all
)) {
853 ret
= LTTNG_EVENT_ALL
;
854 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
855 ret
= LTTNG_EVENT_TRACEPOINT
;
856 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
857 ret
= LTTNG_EVENT_PROBE
;
858 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
859 ret
= LTTNG_EVENT_FUNCTION
;
860 } else if (!strcmp((char *) event_type
,
861 config_event_type_function_entry
)) {
862 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
863 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
864 ret
= LTTNG_EVENT_NOOP
;
865 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
866 ret
= LTTNG_EVENT_SYSCALL
;
877 int get_loglevel_type(xmlChar
*loglevel_type
)
881 if (!loglevel_type
) {
885 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
886 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
887 } else if (!strcmp((char *) loglevel_type
,
888 config_loglevel_type_range
)) {
889 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
890 } else if (!strcmp((char *) loglevel_type
,
891 config_loglevel_type_single
)) {
892 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
903 * Return the context type or -1 on error.
906 int get_context_type(xmlChar
*context_type
)
914 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
915 ret
= LTTNG_EVENT_CONTEXT_PID
;
916 } else if (!strcmp((char *) context_type
,
917 config_event_context_procname
)) {
918 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
919 } else if (!strcmp((char *) context_type
,
920 config_event_context_prio
)) {
921 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
922 } else if (!strcmp((char *) context_type
,
923 config_event_context_nice
)) {
924 ret
= LTTNG_EVENT_CONTEXT_NICE
;
925 } else if (!strcmp((char *) context_type
,
926 config_event_context_vpid
)) {
927 ret
= LTTNG_EVENT_CONTEXT_VPID
;
928 } else if (!strcmp((char *) context_type
,
929 config_event_context_tid
)) {
930 ret
= LTTNG_EVENT_CONTEXT_TID
;
931 } else if (!strcmp((char *) context_type
,
932 config_event_context_vtid
)) {
933 ret
= LTTNG_EVENT_CONTEXT_VTID
;
934 } else if (!strcmp((char *) context_type
,
935 config_event_context_ppid
)) {
936 ret
= LTTNG_EVENT_CONTEXT_PPID
;
937 } else if (!strcmp((char *) context_type
,
938 config_event_context_vppid
)) {
939 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
940 } else if (!strcmp((char *) context_type
,
941 config_event_context_pthread_id
)) {
942 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
943 } else if (!strcmp((char *) context_type
,
944 config_event_context_hostname
)) {
945 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
946 } else if (!strcmp((char *) context_type
,
947 config_event_context_ip
)) {
948 ret
= LTTNG_EVENT_CONTEXT_IP
;
959 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
964 for (node
= xmlFirstElementChild(domain_node
); node
;
965 node
= xmlNextElementSibling(node
)) {
966 if (!strcmp((const char *) node
->name
, config_element_type
)) {
968 xmlChar
*node_content
= xmlNodeGetContent(node
);
970 ret
= -LTTNG_ERR_NOMEM
;
974 ret
= get_domain_type(node_content
);
977 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
982 } else if (!strcmp((const char *) node
->name
,
983 config_element_buffer_type
)) {
985 xmlChar
*node_content
= xmlNodeGetContent(node
);
987 ret
= -LTTNG_ERR_NOMEM
;
991 ret
= get_buffer_type(node_content
);
994 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
998 domain
->buf_type
= ret
;
1007 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1012 for (node
= xmlFirstElementChild(net_output_node
); node
;
1013 node
= xmlNextElementSibling(node
)) {
1014 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1016 *control_uri
= (char *) xmlNodeGetContent(node
);
1017 if (!*control_uri
) {
1022 *data_uri
= (char *) xmlNodeGetContent(node
);
1029 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1033 int process_consumer_output(xmlNodePtr consumer_output_node
,
1034 struct consumer_output
*output
)
1041 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1042 node
= xmlNextElementSibling(node
)) {
1043 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1044 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1048 ret
= -LTTNG_ERR_NOMEM
;
1052 ret
= parse_bool(enabled_str
, &output
->enabled
);
1058 xmlNodePtr output_type_node
;
1061 output_type_node
= xmlFirstElementChild(node
);
1062 if (!output_type_node
) {
1063 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1067 if (!strcmp((const char *) output_type_node
->name
,
1068 config_element_path
)) {
1070 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1071 if (!output
->path
) {
1072 ret
= -LTTNG_ERR_NOMEM
;
1077 ret
= get_net_output_uris(output_type_node
,
1078 &output
->control_uri
, &output
->data_uri
);
1090 free(output
->control_uri
);
1091 free(output
->data_uri
);
1092 memset(output
, 0, sizeof(struct consumer_output
));
1098 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1099 const char *control_uri
, const char *data_uri
)
1102 struct lttng_handle
*handle
;
1103 const char *uri
= NULL
;
1108 handle
= lttng_create_handle(name
, domain
);
1110 ret
= -LTTNG_ERR_NOMEM
;
1114 if (!control_uri
|| !data_uri
) {
1115 uri
= control_uri
? control_uri
: data_uri
;
1120 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1121 lttng_destroy_handle(handle
);
1127 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1130 xmlNodePtr node
= NULL
;
1131 xmlNodePtr snapshot_output_list_node
;
1132 xmlNodePtr snapshot_output_node
;
1134 assert(session_name
);
1136 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1145 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1147 /* Parse and create snapshot outputs */
1149 for (snapshot_output_node
=
1150 xmlFirstElementChild(snapshot_output_list_node
);
1151 snapshot_output_node
; snapshot_output_node
=
1152 xmlNextElementSibling(snapshot_output_node
)) {
1154 uint64_t max_size
= UINT64_MAX
;
1155 struct consumer_output output
= { 0 };
1156 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1158 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1159 node
= xmlNextElementSibling(node
)) {
1160 if (!strcmp((const char *) node
->name
,
1161 config_element_name
)) {
1163 name
= (char *) xmlNodeGetContent(node
);
1165 ret
= -LTTNG_ERR_NOMEM
;
1166 goto error_snapshot_output
;
1168 } else if (!strcmp((const char *) node
->name
,
1169 config_element_max_size
)) {
1170 xmlChar
*content
= xmlNodeGetContent(node
);
1174 ret
= -LTTNG_ERR_NOMEM
;
1175 goto error_snapshot_output
;
1177 ret
= parse_uint(content
, &max_size
);
1180 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1181 goto error_snapshot_output
;
1184 /* consumer_output */
1185 ret
= process_consumer_output(node
, &output
);
1187 goto error_snapshot_output
;
1192 snapshot_output
= lttng_snapshot_output_create();
1193 if (!snapshot_output
) {
1194 ret
= -LTTNG_ERR_NOMEM
;
1195 goto error_snapshot_output
;
1198 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1200 goto error_snapshot_output
;
1203 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1205 goto error_snapshot_output
;
1209 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1212 goto error_snapshot_output
;
1215 if (output
.control_uri
) {
1216 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1219 goto error_snapshot_output
;
1223 if (output
.data_uri
) {
1224 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1227 goto error_snapshot_output
;
1232 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1233 error_snapshot_output
:
1236 free(output
.control_uri
);
1237 free(output
.data_uri
);
1238 lttng_snapshot_output_destroy(snapshot_output
);
1248 int create_session(const char *name
,
1249 struct lttng_domain
*kernel_domain
,
1250 struct lttng_domain
*ust_domain
,
1251 struct lttng_domain
*jul_domain
,
1252 struct lttng_domain
*log4j_domain
,
1253 xmlNodePtr output_node
,
1254 uint64_t live_timer_interval
)
1257 struct consumer_output output
= { 0 };
1258 xmlNodePtr consumer_output_node
;
1263 consumer_output_node
= xmlFirstElementChild(output_node
);
1264 if (!consumer_output_node
) {
1265 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1269 if (strcmp((const char *) consumer_output_node
->name
,
1270 config_element_consumer_output
)) {
1271 WARN("Invalid output type, expected %s node",
1272 config_element_consumer_output
);
1273 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1277 ret
= process_consumer_output(consumer_output_node
, &output
);
1283 if (live_timer_interval
!= UINT64_MAX
&&
1284 !output
.control_uri
&& !output
.data_uri
) {
1285 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1289 if (output
.control_uri
|| output
.data_uri
) {
1291 struct lttng_domain
*domain
;
1292 struct lttng_domain
*domains
[] =
1293 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1295 /* network destination */
1296 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1298 * URLs are provided for sure since the test above make sure that
1299 * with a live timer the data and control URIs are provided. So,
1300 * NULL is passed here and will be set right after.
1302 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1304 ret
= lttng_create_session(name
, NULL
);
1310 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1311 domain
= domains
[i
];
1316 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1323 /* either local output or no output */
1324 ret
= lttng_create_session(name
, output
.path
);
1331 free(output
.control_uri
);
1332 free(output
.data_uri
);
1336 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1337 struct lttng_event_probe_attr
*attr
)
1341 assert(probe_attribute_node
);
1344 if (!strcmp((const char *) probe_attribute_node
->name
,
1345 config_element_address
)) {
1350 content
= xmlNodeGetContent(probe_attribute_node
);
1352 ret
= -LTTNG_ERR_NOMEM
;
1356 ret
= parse_uint(content
, &addr
);
1359 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1364 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1365 config_element_offset
)) {
1367 uint64_t offset
= 0;
1370 content
= xmlNodeGetContent(probe_attribute_node
);
1372 ret
= -LTTNG_ERR_NOMEM
;
1376 ret
= parse_uint(content
, &offset
);
1379 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1383 attr
->offset
= offset
;
1384 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1385 config_element_symbol_name
)) {
1390 content
= xmlNodeGetContent(probe_attribute_node
);
1392 ret
= -LTTNG_ERR_NOMEM
;
1396 name_len
= strlen((char *) content
);
1397 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1398 WARN("symbol_name too long.");
1399 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1404 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1413 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1414 const char *channel_name
)
1418 struct lttng_event event
;
1419 char **exclusions
= NULL
;
1420 unsigned long exclusion_count
= 0;
1421 char *filter_expression
= NULL
;
1425 assert(channel_name
);
1427 memset(&event
, 0, sizeof(event
));
1429 /* Initialize default log level which varies by domain */
1430 switch (handle
->domain
.type
)
1432 case LTTNG_DOMAIN_JUL
:
1433 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1435 case LTTNG_DOMAIN_LOG4J
:
1436 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1438 case LTTNG_DOMAIN_PYTHON
:
1439 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1441 case LTTNG_DOMAIN_UST
:
1442 case LTTNG_DOMAIN_KERNEL
:
1443 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1449 for (node
= xmlFirstElementChild(event_node
); node
;
1450 node
= xmlNextElementSibling(node
)) {
1451 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1456 content
= xmlNodeGetContent(node
);
1458 ret
= -LTTNG_ERR_NOMEM
;
1462 name_len
= strlen((char *) content
);
1463 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1464 WARN("Channel name too long.");
1465 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1470 strncpy(event
.name
, (const char *) content
, name_len
);
1472 } else if (!strcmp((const char *) node
->name
,
1473 config_element_enabled
)) {
1474 xmlChar
*content
= xmlNodeGetContent(node
);
1478 ret
= -LTTNG_ERR_NOMEM
;
1482 ret
= parse_bool(content
, &event
.enabled
);
1485 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1488 } else if (!strcmp((const char *) node
->name
,
1489 config_element_type
)) {
1490 xmlChar
*content
= xmlNodeGetContent(node
);
1494 ret
= -LTTNG_ERR_NOMEM
;
1498 ret
= get_event_type(content
);
1501 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1506 } else if (!strcmp((const char *) node
->name
,
1507 config_element_loglevel_type
)) {
1508 xmlChar
*content
= xmlNodeGetContent(node
);
1512 ret
= -LTTNG_ERR_NOMEM
;
1516 ret
= get_loglevel_type(content
);
1519 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1523 event
.loglevel_type
= ret
;
1524 } else if (!strcmp((const char *) node
->name
,
1525 config_element_loglevel
)) {
1527 int64_t loglevel
= 0;
1530 content
= xmlNodeGetContent(node
);
1532 ret
= -LTTNG_ERR_NOMEM
;
1536 ret
= parse_int(content
, &loglevel
);
1539 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1543 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1544 WARN("loglevel out of range.");
1545 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1549 event
.loglevel
= loglevel
;
1550 } else if (!strcmp((const char *) node
->name
,
1551 config_element_filter
)) {
1553 xmlNodeGetContent(node
);
1557 ret
= -LTTNG_ERR_NOMEM
;
1561 filter_expression
= strdup((char *) content
);
1563 if (!filter_expression
) {
1564 ret
= -LTTNG_ERR_NOMEM
;
1567 } else if (!strcmp((const char *) node
->name
,
1568 config_element_exclusions
)) {
1569 xmlNodePtr exclusion_node
;
1570 int exclusion_index
= 0;
1575 * Exclusions has already been initialized,
1578 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1582 exclusion_count
= xmlChildElementCount(node
);
1583 if (!exclusion_count
) {
1587 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1589 exclusion_count
= 0;
1590 ret
= -LTTNG_ERR_NOMEM
;
1594 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1595 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1597 xmlNodeGetContent(exclusion_node
);
1600 ret
= -LTTNG_ERR_NOMEM
;
1604 exclusions
[exclusion_index
] = strdup((const char *) content
);
1606 if (!exclusions
[exclusion_index
]) {
1607 ret
= -LTTNG_ERR_NOMEM
;
1613 event
.exclusion
= 1;
1614 } else if (!strcmp((const char *) node
->name
,
1615 config_element_attributes
)) {
1616 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1619 if (!attribute_node
) {
1620 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1624 if (!strcmp((const char *) node
->name
,
1625 config_element_probe_attributes
)) {
1626 xmlNodePtr probe_attribute_node
;
1628 /* probe_attributes */
1629 for (probe_attribute_node
=
1630 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1631 probe_attribute_node
= xmlNextElementSibling(
1632 probe_attribute_node
)) {
1634 ret
= process_probe_attribute_node(probe_attribute_node
,
1643 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1645 /* function_attributes */
1646 content
= xmlNodeGetContent(symbol_node
);
1648 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1652 sym_len
= strlen((char *) content
);
1653 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1654 WARN("Function name too long.");
1655 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1660 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1667 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1668 filter_expression
, exclusion_count
, exclusions
);
1673 if (!event
.enabled
) {
1675 * Note that we should use lttng_disable_event_ext() (2.6+) to
1676 * eliminate the risk of clashing on events of the same
1677 * name (with different event types and loglevels).
1679 * Unfortunately, lttng_disable_event_ext() only performs a
1680 * match on the name and event type and errors out if any other
1681 * event attribute is not set to its default value.
1683 * This will disable all events that match this name.
1685 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1688 for (i
= 0; i
< exclusion_count
; i
++) {
1689 free(exclusions
[i
]);
1693 free(filter_expression
);
1698 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1699 const char *channel_name
)
1704 assert(events_node
);
1706 assert(channel_name
);
1708 for (node
= xmlFirstElementChild(events_node
); node
;
1709 node
= xmlNextElementSibling(node
)) {
1710 ret
= process_event_node(node
, handle
, channel_name
);
1720 int process_channel_attr_node(xmlNodePtr attr_node
,
1721 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1722 xmlNodePtr
*events_node
)
1728 assert(contexts_node
);
1729 assert(events_node
);
1731 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1736 content
= xmlNodeGetContent(attr_node
);
1738 ret
= -LTTNG_ERR_NOMEM
;
1742 name_len
= strlen((char *) content
);
1743 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1744 WARN("Channel name too long.");
1745 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1750 strncpy(channel
->name
, (const char *) content
, name_len
);
1752 } else if (!strcmp((const char *) attr_node
->name
,
1753 config_element_enabled
)) {
1758 content
= xmlNodeGetContent(attr_node
);
1760 ret
= -LTTNG_ERR_NOMEM
;
1764 ret
= parse_bool(content
, &enabled
);
1767 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1771 channel
->enabled
= enabled
;
1772 } else if (!strcmp((const char *) attr_node
->name
,
1773 config_element_overwrite_mode
)) {
1776 /* overwrite_mode */
1777 content
= xmlNodeGetContent(attr_node
);
1779 ret
= -LTTNG_ERR_NOMEM
;
1783 ret
= get_overwrite_mode(content
);
1786 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1790 channel
->attr
.overwrite
= ret
;
1791 } else if (!strcmp((const char *) attr_node
->name
,
1792 config_element_subbuf_size
)) {
1795 /* subbuffer_size */
1796 content
= xmlNodeGetContent(attr_node
);
1798 ret
= -LTTNG_ERR_NOMEM
;
1802 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1805 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1808 } else if (!strcmp((const char *) attr_node
->name
,
1809 config_element_num_subbuf
)) {
1812 /* subbuffer_count */
1813 content
= xmlNodeGetContent(attr_node
);
1815 ret
= -LTTNG_ERR_NOMEM
;
1819 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1822 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 } else if (!strcmp((const char *) attr_node
->name
,
1826 config_element_switch_timer_interval
)) {
1828 uint64_t switch_timer_interval
= 0;
1830 /* switch_timer_interval */
1831 content
= xmlNodeGetContent(attr_node
);
1833 ret
= -LTTNG_ERR_NOMEM
;
1837 ret
= parse_uint(content
, &switch_timer_interval
);
1840 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1844 if (switch_timer_interval
> UINT_MAX
) {
1845 WARN("switch_timer_interval out of range.");
1846 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1850 channel
->attr
.switch_timer_interval
=
1851 switch_timer_interval
;
1852 } else if (!strcmp((const char *) attr_node
->name
,
1853 config_element_read_timer_interval
)) {
1855 uint64_t read_timer_interval
= 0;
1857 /* read_timer_interval */
1858 content
= xmlNodeGetContent(attr_node
);
1860 ret
= -LTTNG_ERR_NOMEM
;
1864 ret
= parse_uint(content
, &read_timer_interval
);
1867 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1871 if (read_timer_interval
> UINT_MAX
) {
1872 WARN("read_timer_interval out of range.");
1873 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1877 channel
->attr
.read_timer_interval
=
1878 read_timer_interval
;
1879 } else if (!strcmp((const char *) attr_node
->name
,
1880 config_element_output_type
)) {
1884 content
= xmlNodeGetContent(attr_node
);
1886 ret
= -LTTNG_ERR_NOMEM
;
1890 ret
= get_output_type(content
);
1893 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1897 channel
->attr
.output
= ret
;
1898 } else if (!strcmp((const char *) attr_node
->name
,
1899 config_element_tracefile_size
)) {
1902 /* tracefile_size */
1903 content
= xmlNodeGetContent(attr_node
);
1905 ret
= -LTTNG_ERR_NOMEM
;
1909 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1912 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1915 } else if (!strcmp((const char *) attr_node
->name
,
1916 config_element_tracefile_count
)) {
1919 /* tracefile_count */
1920 content
= xmlNodeGetContent(attr_node
);
1922 ret
= -LTTNG_ERR_NOMEM
;
1926 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1929 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1932 } else if (!strcmp((const char *) attr_node
->name
,
1933 config_element_live_timer_interval
)) {
1935 uint64_t live_timer_interval
= 0;
1937 /* live_timer_interval */
1938 content
= xmlNodeGetContent(attr_node
);
1940 ret
= -LTTNG_ERR_NOMEM
;
1944 ret
= parse_uint(content
, &live_timer_interval
);
1947 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1951 if (live_timer_interval
> UINT_MAX
) {
1952 WARN("live_timer_interval out of range.");
1953 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1957 channel
->attr
.live_timer_interval
=
1958 live_timer_interval
;
1959 } else if (!strcmp((const char *) attr_node
->name
,
1960 config_element_events
)) {
1962 *events_node
= attr_node
;
1965 *contexts_node
= attr_node
;
1973 int process_context_node(xmlNodePtr context_node
,
1974 struct lttng_handle
*handle
, const char *channel_name
)
1977 struct lttng_event_context context
;
1978 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1981 assert(channel_name
);
1983 if (!context_child_node
) {
1984 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1988 memset(&context
, 0, sizeof(context
));
1990 if (!strcmp((const char *) context_child_node
->name
,
1991 config_element_type
)) {
1993 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1995 ret
= -LTTNG_ERR_NOMEM
;
1999 ret
= get_context_type(content
);
2002 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2008 xmlNodePtr perf_attr_node
;
2011 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2012 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2013 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2014 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2015 perf_attr_node
; perf_attr_node
=
2016 xmlNextElementSibling(perf_attr_node
)) {
2017 if (!strcmp((const char *) perf_attr_node
->name
,
2018 config_element_type
)) {
2023 content
= xmlNodeGetContent(perf_attr_node
);
2025 ret
= -LTTNG_ERR_NOMEM
;
2029 ret
= parse_uint(content
, &type
);
2032 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2036 if (type
> UINT32_MAX
) {
2037 WARN("perf context type out of range.");
2038 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2042 context
.u
.perf_counter
.type
= type
;
2043 } else if (!strcmp((const char *) perf_attr_node
->name
,
2044 config_element_config
)) {
2046 uint64_t config
= 0;
2049 content
= xmlNodeGetContent(perf_attr_node
);
2051 ret
= -LTTNG_ERR_NOMEM
;
2055 ret
= parse_uint(content
, &config
);
2058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2062 context
.u
.perf_counter
.config
= config
;
2063 } else if (!strcmp((const char *) perf_attr_node
->name
,
2064 config_element_name
)) {
2069 content
= xmlNodeGetContent(perf_attr_node
);
2071 ret
= -LTTNG_ERR_NOMEM
;
2075 name_len
= strlen((char *) content
);
2076 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2077 WARN("perf context name too long.");
2078 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2083 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2090 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2096 int process_contexts_node(xmlNodePtr contexts_node
,
2097 struct lttng_handle
*handle
, const char *channel_name
)
2100 xmlNodePtr context_node
;
2102 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2103 context_node
= xmlNextElementSibling(context_node
)) {
2104 ret
= process_context_node(context_node
, handle
, channel_name
);
2114 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2115 struct lttng_handle
*handle
)
2118 xmlNodePtr targets_node
= NULL
;
2122 assert(pid_tracker_node
);
2123 /* get the targets node */
2124 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2125 node
= xmlNextElementSibling(node
)) {
2126 if (!strcmp((const char *) node
->name
,
2127 config_element_targets
)) {
2128 targets_node
= node
;
2133 if (!targets_node
) {
2134 ret
= LTTNG_ERR_INVALID
;
2138 /* Go through all pid_target node */
2139 child
= xmlChildElementCount(targets_node
);
2141 /* The session is explicitly set to target nothing. */
2142 ret
= lttng_untrack_pid(handle
, -1);
2147 for (node
= xmlFirstElementChild(targets_node
); node
;
2148 node
= xmlNextElementSibling(node
)) {
2149 xmlNodePtr pid_target_node
= node
;
2151 /* get pid node and track it */
2152 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2153 node
= xmlNextElementSibling(node
)) {
2154 if (!strcmp((const char *) node
->name
,
2155 config_element_pid
)) {
2157 xmlChar
*content
= NULL
;
2159 content
= xmlNodeGetContent(node
);
2161 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2165 ret
= parse_int(content
, &pid
);
2168 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2172 ret
= lttng_track_pid(handle
, (int) pid
);
2178 node
= pid_target_node
;
2187 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2190 struct lttng_domain domain
= { 0 };
2191 struct lttng_handle
*handle
= NULL
;
2192 xmlNodePtr channels_node
= NULL
;
2193 xmlNodePtr trackers_node
= NULL
;
2194 xmlNodePtr pid_tracker_node
= NULL
;
2197 assert(session_name
);
2199 ret
= init_domain(domain_node
, &domain
);
2204 handle
= lttng_create_handle(session_name
, &domain
);
2206 ret
= -LTTNG_ERR_NOMEM
;
2210 /* get the channels node */
2211 for (node
= xmlFirstElementChild(domain_node
); node
;
2212 node
= xmlNextElementSibling(node
)) {
2213 if (!strcmp((const char *) node
->name
,
2214 config_element_channels
)) {
2215 channels_node
= node
;
2220 if (!channels_node
) {
2224 /* create all channels */
2225 for (node
= xmlFirstElementChild(channels_node
); node
;
2226 node
= xmlNextElementSibling(node
)) {
2227 struct lttng_channel channel
;
2228 xmlNodePtr contexts_node
= NULL
;
2229 xmlNodePtr events_node
= NULL
;
2230 xmlNodePtr channel_attr_node
;
2232 memset(&channel
, 0, sizeof(channel
));
2233 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2235 for (channel_attr_node
= xmlFirstElementChild(node
);
2236 channel_attr_node
; channel_attr_node
=
2237 xmlNextElementSibling(channel_attr_node
)) {
2238 ret
= process_channel_attr_node(channel_attr_node
,
2239 &channel
, &contexts_node
, &events_node
);
2245 ret
= lttng_enable_channel(handle
, &channel
);
2250 ret
= process_events_node(events_node
, handle
, channel
.name
);
2255 ret
= process_contexts_node(contexts_node
, handle
,
2262 /* get the trackers node */
2263 for (node
= xmlFirstElementChild(domain_node
); node
;
2264 node
= xmlNextElementSibling(node
)) {
2265 if (!strcmp((const char *) node
->name
,
2266 config_element_trackers
)) {
2267 trackers_node
= node
;
2272 if (!trackers_node
) {
2276 for (node
= xmlFirstElementChild(trackers_node
); node
;
2277 node
= xmlNextElementSibling(node
)) {
2278 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2279 pid_tracker_node
= node
;
2280 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2287 if (!pid_tracker_node
) {
2288 lttng_track_pid(handle
, -1);
2292 lttng_destroy_handle(handle
);
2297 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2300 int ret
, started
= -1, snapshot_mode
= -1;
2301 uint64_t live_timer_interval
= UINT64_MAX
;
2302 xmlChar
*name
= NULL
;
2303 xmlChar
*shm_path
= NULL
;
2304 xmlNodePtr domains_node
= NULL
;
2305 xmlNodePtr output_node
= NULL
;
2307 struct lttng_domain
*kernel_domain
= NULL
;
2308 struct lttng_domain
*ust_domain
= NULL
;
2309 struct lttng_domain
*jul_domain
= NULL
;
2310 struct lttng_domain
*log4j_domain
= NULL
;
2311 struct lttng_domain
*python_domain
= NULL
;
2313 for (node
= xmlFirstElementChild(session_node
); node
;
2314 node
= xmlNextElementSibling(node
)) {
2315 if (!name
&& !strcmp((const char *) node
->name
,
2316 config_element_name
)) {
2318 xmlChar
*node_content
= xmlNodeGetContent(node
);
2319 if (!node_content
) {
2320 ret
= -LTTNG_ERR_NOMEM
;
2324 name
= node_content
;
2325 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2326 config_element_domains
)) {
2328 domains_node
= node
;
2329 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2330 config_element_started
)) {
2332 xmlChar
*node_content
= xmlNodeGetContent(node
);
2333 if (!node_content
) {
2334 ret
= -LTTNG_ERR_NOMEM
;
2338 ret
= parse_bool(node_content
, &started
);
2341 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2344 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2345 config_element_output
)) {
2348 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2349 config_element_shared_memory_path
)) {
2350 /* shared memory path */
2351 xmlChar
*node_content
= xmlNodeGetContent(node
);
2352 if (!node_content
) {
2353 ret
= -LTTNG_ERR_NOMEM
;
2357 shm_path
= node_content
;
2359 /* attributes, snapshot_mode or live_timer_interval */
2360 xmlNodePtr attributes_child
=
2361 xmlFirstElementChild(node
);
2363 if (!strcmp((const char *) attributes_child
->name
,
2364 config_element_snapshot_mode
)) {
2366 xmlChar
*snapshot_mode_content
=
2367 xmlNodeGetContent(attributes_child
);
2368 if (!snapshot_mode_content
) {
2369 ret
= -LTTNG_ERR_NOMEM
;
2373 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2374 free(snapshot_mode_content
);
2376 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2380 /* live_timer_interval */
2381 xmlChar
*timer_interval_content
=
2382 xmlNodeGetContent(attributes_child
);
2383 if (!timer_interval_content
) {
2384 ret
= -LTTNG_ERR_NOMEM
;
2388 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2389 free(timer_interval_content
);
2391 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2399 /* Mandatory attribute, as defined in the session XSD */
2400 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2404 if (session_name
&& strcmp((char *) name
, session_name
)) {
2405 /* This is not the session we are looking for */
2406 ret
= -LTTNG_ERR_NO_SESSION
;
2410 /* Init domains to create the session handles */
2411 for (node
= xmlFirstElementChild(domains_node
); node
;
2412 node
= xmlNextElementSibling(node
)) {
2413 struct lttng_domain
*domain
;
2415 domain
= zmalloc(sizeof(*domain
));
2417 ret
= -LTTNG_ERR_NOMEM
;
2421 ret
= init_domain(node
, domain
);
2423 goto domain_init_error
;
2426 switch (domain
->type
) {
2427 case LTTNG_DOMAIN_KERNEL
:
2428 if (kernel_domain
) {
2429 /* Same domain seen twice, invalid! */
2430 goto domain_init_error
;
2432 kernel_domain
= domain
;
2434 case LTTNG_DOMAIN_UST
:
2436 /* Same domain seen twice, invalid! */
2437 goto domain_init_error
;
2439 ust_domain
= domain
;
2441 case LTTNG_DOMAIN_JUL
:
2443 /* Same domain seen twice, invalid! */
2444 goto domain_init_error
;
2446 jul_domain
= domain
;
2448 case LTTNG_DOMAIN_LOG4J
:
2450 /* Same domain seen twice, invalid! */
2451 goto domain_init_error
;
2453 log4j_domain
= domain
;
2455 case LTTNG_DOMAIN_PYTHON
:
2456 if (python_domain
) {
2457 /* Same domain seen twice, invalid! */
2458 goto domain_init_error
;
2460 python_domain
= domain
;
2463 WARN("Invalid domain type");
2464 goto domain_init_error
;
2469 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2474 /* Destroy session if it exists */
2475 ret
= lttng_destroy_session((const char *) name
);
2476 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2477 ERR("Failed to destroy existing session.");
2482 /* Create session type depending on output type */
2483 if (snapshot_mode
&& snapshot_mode
!= -1) {
2484 ret
= create_snapshot_session((const char *) name
, output_node
);
2485 } else if (live_timer_interval
&&
2486 live_timer_interval
!= UINT64_MAX
) {
2487 ret
= create_session((const char *) name
, kernel_domain
,
2488 ust_domain
, jul_domain
, log4j_domain
,
2489 output_node
, live_timer_interval
);
2491 /* regular session */
2492 ret
= create_session((const char *) name
, kernel_domain
,
2493 ust_domain
, jul_domain
, log4j_domain
,
2494 output_node
, UINT64_MAX
);
2501 ret
= lttng_set_session_shm_path((const char *) name
,
2502 (const char *) shm_path
);
2508 for (node
= xmlFirstElementChild(domains_node
); node
;
2509 node
= xmlNextElementSibling(node
)) {
2510 ret
= process_domain_node(node
, (const char *) name
);
2517 ret
= lttng_start_tracing((const char *) name
);
2525 ERR("Failed to load session %s: %s", (const char *) name
,
2526 lttng_strerror(ret
));
2527 lttng_destroy_session((const char *) name
);
2531 free(kernel_domain
);
2535 free(python_domain
);
2542 * Return 1 if the given path is readable by the current UID or 0 if not.
2543 * Return -1 if the path is EPERM.
2545 static int validate_file_read_creds(const char *path
)
2551 /* Can we read the file. */
2552 ret
= access(path
, R_OK
);
2556 if (errno
== EACCES
) {
2567 int load_session_from_file(const char *path
, const char *session_name
,
2568 struct session_config_validation_ctx
*validation_ctx
, int override
)
2570 int ret
, session_found
= !session_name
;
2571 xmlDocPtr doc
= NULL
;
2572 xmlNodePtr sessions_node
;
2573 xmlNodePtr session_node
;
2576 assert(validation_ctx
);
2578 ret
= validate_file_read_creds(path
);
2581 ret
= -LTTNG_ERR_EPERM
;
2583 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2588 doc
= xmlParseFile(path
);
2590 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2594 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2596 ERR("Session configuration file validation failed");
2597 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2601 sessions_node
= xmlDocGetRootElement(doc
);
2602 if (!sessions_node
) {
2606 for (session_node
= xmlFirstElementChild(sessions_node
);
2607 session_node
; session_node
=
2608 xmlNextElementSibling(session_node
)) {
2609 ret
= process_session_node(session_node
,
2610 session_name
, override
);
2611 if (session_name
&& ret
== 0) {
2612 /* Target session found and loaded */
2620 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2625 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2627 struct dirent
*alloc_dirent(const char *path
)
2631 struct dirent
*entry
;
2633 name_max
= pathconf(path
, _PC_NAME_MAX
);
2634 if (name_max
== -1) {
2635 name_max
= PATH_MAX
;
2637 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2638 entry
= zmalloc(len
);
2643 int load_session_from_path(const char *path
, const char *session_name
,
2644 struct session_config_validation_ctx
*validation_ctx
, int override
)
2646 int ret
, session_found
= !session_name
;
2647 DIR *directory
= NULL
;
2650 assert(validation_ctx
);
2652 directory
= opendir(path
);
2656 /* Try the file loading. */
2659 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2662 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2667 struct dirent
*entry
;
2668 struct dirent
*result
;
2669 char *file_path
= NULL
;
2670 size_t path_len
= strlen(path
);
2672 if (path_len
>= PATH_MAX
) {
2673 ret
= -LTTNG_ERR_INVALID
;
2677 entry
= alloc_dirent(path
);
2679 ret
= -LTTNG_ERR_NOMEM
;
2683 file_path
= zmalloc(PATH_MAX
);
2685 ret
= -LTTNG_ERR_NOMEM
;
2690 strncpy(file_path
, path
, path_len
);
2691 if (file_path
[path_len
- 1] != '/') {
2692 file_path
[path_len
++] = '/';
2696 /* Search for *.lttng files */
2697 while (!readdir_r(directory
, entry
, &result
) && result
) {
2698 size_t file_name_len
= strlen(result
->d_name
);
2700 if (file_name_len
<=
2701 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2705 if (path_len
+ file_name_len
>= PATH_MAX
) {
2709 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2710 result
->d_name
+ file_name_len
- sizeof(
2711 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2715 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2716 file_path
[path_len
+ file_name_len
] = '\0';
2718 ret
= load_session_from_file(file_path
, session_name
,
2719 validation_ctx
, override
);
2720 if (session_name
&& !ret
) {
2729 ret
= load_session_from_file(path
, session_name
,
2730 validation_ctx
, override
);
2740 if (closedir(directory
)) {
2745 if (!session_found
) {
2746 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2753 * Validate that the given path's credentials and the current process have the
2754 * same UID. If so, return 1 else return 0 if it does NOT match.
2756 static int validate_path_creds(const char *path
)
2758 int ret
, uid
= getuid();
2767 ret
= stat(path
, &buf
);
2769 if (errno
!= ENOENT
) {
2772 ret
= -LTTNG_ERR_INVALID
;
2776 if (buf
.st_uid
!= uid
) {
2787 int config_load_session(const char *path
, const char *session_name
,
2788 int override
, unsigned int autoload
)
2791 const char *path_ptr
= NULL
;
2792 struct session_config_validation_ctx validation_ctx
= { 0 };
2794 ret
= init_session_config_validation_ctx(&validation_ctx
);
2801 const char *sys_path
;
2804 home_path
= utils_get_home_dir();
2806 char path
[PATH_MAX
];
2809 * Try user session configuration path. Ignore error here so we can
2810 * continue loading the system wide sessions.
2813 ret
= snprintf(path
, sizeof(path
),
2814 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2815 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2817 PERROR("snprintf session autoload home config path");
2822 * Credentials are only validated for the autoload in order to
2823 * avoid any user session daemon to try to load kernel sessions
2824 * automatically and failing all the times.
2826 ret
= validate_path_creds(path
);
2831 ret
= snprintf(path
, sizeof(path
),
2832 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2834 PERROR("snprintf session home config path");
2840 ret
= load_session_from_path(path_ptr
, session_name
,
2841 &validation_ctx
, override
);
2842 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2846 * Continue even if the session was found since we have to try
2847 * the system wide sessions.
2852 /* Reset path pointer for the system wide dir. */
2855 /* Try system wide configuration directory. */
2857 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2858 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2859 ret
= validate_path_creds(sys_path
);
2861 path_ptr
= sys_path
;
2864 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2865 path_ptr
= sys_path
;
2869 ret
= load_session_from_path(path_ptr
, session_name
,
2870 &validation_ctx
, override
);
2873 ret
= access(path
, F_OK
);
2878 ret
= -LTTNG_ERR_INVALID
;
2879 WARN("Session configuration path does not exist.");
2882 ret
= -LTTNG_ERR_EPERM
;
2885 ret
= -LTTNG_ERR_UNK
;
2891 ret
= load_session_from_path(path
, session_name
,
2892 &validation_ctx
, override
);
2895 fini_session_config_validation_ctx(&validation_ctx
);
2896 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2898 * Don't report an error if no sessions are found when called
2899 * without a session_name or a search path.