2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <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>
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";
118 const char * const config_domain_type_kernel
= "KERNEL";
119 const char * const config_domain_type_ust
= "UST";
120 const char * const config_domain_type_jul
= "JUL";
121 const char * const config_domain_type_log4j
= "LOG4J";
123 const char * const config_buffer_type_per_pid
= "PER_PID";
124 const char * const config_buffer_type_per_uid
= "PER_UID";
125 const char * const config_buffer_type_global
= "GLOBAL";
127 const char * const config_overwrite_mode_discard
= "DISCARD";
128 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
130 const char * const config_output_type_splice
= "SPLICE";
131 const char * const config_output_type_mmap
= "MMAP";
133 const char * const config_loglevel_type_all
= "ALL";
134 const char * const config_loglevel_type_range
= "RANGE";
135 const char * const config_loglevel_type_single
= "SINGLE";
137 const char * const config_event_type_all
= "ALL";
138 const char * const config_event_type_tracepoint
= "TRACEPOINT";
139 const char * const config_event_type_probe
= "PROBE";
140 const char * const config_event_type_function
= "FUNCTION";
141 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
142 const char * const config_event_type_noop
= "NOOP";
143 const char * const config_event_type_syscall
= "SYSCALL";
144 const char * const config_event_type_kprobe
= "KPROBE";
145 const char * const config_event_type_kretprobe
= "KRETPROBE";
147 const char * const config_event_context_pid
= "PID";
148 const char * const config_event_context_procname
= "PROCNAME";
149 const char * const config_event_context_prio
= "PRIO";
150 const char * const config_event_context_nice
= "NICE";
151 const char * const config_event_context_vpid
= "VPID";
152 const char * const config_event_context_tid
= "TID";
153 const char * const config_event_context_vtid
= "VTID";
154 const char * const config_event_context_ppid
= "PPID";
155 const char * const config_event_context_vppid
= "VPPID";
156 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
157 const char * const config_event_context_hostname
= "HOSTNAME";
158 const char * const config_event_context_ip
= "IP";
159 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
161 enum process_event_node_phase
{
166 struct consumer_output
{
173 static int config_entry_handler_filter(struct handler_filter_args
*args
,
174 const char *section
, const char *name
, const char *value
)
177 struct config_entry entry
= { section
, name
, value
};
181 if (!section
|| !name
|| !value
) {
187 if (strcmp(args
->section
, section
)) {
192 ret
= args
->handler(&entry
, args
->user_data
);
198 int config_get_section_entries(const char *override_path
, const char *section
,
199 config_entry_handler_cb handler
, void *user_data
)
203 FILE *config_file
= NULL
;
204 struct handler_filter_args filter
= { section
, handler
, user_data
};
206 /* First, try system-wide conf. file. */
207 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
209 config_file
= fopen(path
, "r");
211 DBG("Loading daemon conf file at %s", path
);
213 * Return value is not very important here since error or not, we
214 * continue and try the next possible conf. file.
216 (void) ini_parse_file(config_file
,
217 (ini_entry_handler
) config_entry_handler_filter
,
222 /* Second is the user local configuration. */
223 path
= utils_get_home_dir();
225 char fullpath
[PATH_MAX
];
227 ret
= snprintf(fullpath
, sizeof(fullpath
),
228 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
230 PERROR("snprintf user conf. path");
234 config_file
= fopen(fullpath
, "r");
236 DBG("Loading daemon user conf file at %s", path
);
238 * Return value is not very important here since error or not, we
239 * continue and try the next possible conf. file.
241 (void) ini_parse_file(config_file
,
242 (ini_entry_handler
) config_entry_handler_filter
,
248 /* Final path is the one that the user might have provided. */
250 config_file
= fopen(override_path
, "r");
252 DBG("Loading daemon command line conf file at %s", override_path
);
253 (void) ini_parse_file(config_file
,
254 (ini_entry_handler
) config_entry_handler_filter
,
258 ERR("Failed to open daemon configuration file at %s",
265 /* Everything went well. */
273 int config_parse_value(const char *value
)
276 char *endptr
, *lower_str
;
286 v
= strtoul(value
, &endptr
, 10);
287 if (endptr
!= value
) {
292 lower_str
= zmalloc(len
+ 1);
299 for (i
= 0; i
< len
; i
++) {
300 lower_str
[i
] = tolower(value
[i
]);
303 if (!strcmp(lower_str
, config_str_yes
) ||
304 !strcmp(lower_str
, config_str_true
) ||
305 !strcmp(lower_str
, config_str_on
)) {
307 } else if (!strcmp(lower_str
, config_str_no
) ||
308 !strcmp(lower_str
, config_str_false
) ||
309 !strcmp(lower_str
, config_str_off
)) {
321 * Returns a xmlChar string which must be released using xmlFree().
323 static xmlChar
*encode_string(const char *in_str
)
325 xmlChar
*out_str
= NULL
;
326 xmlCharEncodingHandlerPtr handler
;
327 int out_len
, ret
, in_len
;
331 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
333 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
337 in_len
= strlen(in_str
);
339 * Add 1 byte for the NULL terminted character. The factor 4 here is
340 * used because UTF-8 characters can take up to 4 bytes.
342 out_len
= (in_len
* 4) + 1;
343 out_str
= xmlMalloc(out_len
);
348 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
355 /* out_len is now the size of out_str */
356 out_str
[out_len
] = '\0';
362 struct config_writer
*config_writer_create(int fd_output
, int indent
)
365 struct config_writer
*writer
;
366 xmlOutputBufferPtr buffer
;
368 writer
= zmalloc(sizeof(struct config_writer
));
370 PERROR("zmalloc config_writer_create");
374 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
379 writer
->writer
= xmlNewTextWriter(buffer
);
380 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
381 config_xml_encoding
, NULL
);
386 ret
= xmlTextWriterSetIndentString(writer
->writer
,
387 BAD_CAST config_xml_indent_string
);
392 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
400 config_writer_destroy(writer
);
405 int config_writer_destroy(struct config_writer
*writer
)
414 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
415 WARN("Could not close XML document");
419 if (writer
->writer
) {
420 xmlFreeTextWriter(writer
->writer
);
429 int config_writer_open_element(struct config_writer
*writer
,
430 const char *element_name
)
433 xmlChar
*encoded_element_name
;
435 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
440 encoded_element_name
= encode_string(element_name
);
441 if (!encoded_element_name
) {
446 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
447 xmlFree(encoded_element_name
);
449 return ret
> 0 ? 0 : ret
;
453 int config_writer_close_element(struct config_writer
*writer
)
457 if (!writer
|| !writer
->writer
) {
462 ret
= xmlTextWriterEndElement(writer
->writer
);
464 return ret
> 0 ? 0 : ret
;
468 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
469 const char *element_name
, uint64_t value
)
472 xmlChar
*encoded_element_name
;
474 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
479 encoded_element_name
= encode_string(element_name
);
480 if (!encoded_element_name
) {
485 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
486 encoded_element_name
, "%" PRIu64
, value
);
487 xmlFree(encoded_element_name
);
489 return ret
> 0 ? 0 : ret
;
493 int config_writer_write_element_signed_int(struct config_writer
*writer
,
494 const char *element_name
, int64_t value
)
497 xmlChar
*encoded_element_name
;
499 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
504 encoded_element_name
= encode_string(element_name
);
505 if (!encoded_element_name
) {
510 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
511 encoded_element_name
, "%" PRIi64
, value
);
512 xmlFree(encoded_element_name
);
514 return ret
> 0 ? 0 : ret
;
518 int config_writer_write_element_bool(struct config_writer
*writer
,
519 const char *element_name
, int value
)
521 return config_writer_write_element_string(writer
, element_name
,
522 value
? config_xml_true
: config_xml_false
);
526 int config_writer_write_element_string(struct config_writer
*writer
,
527 const char *element_name
, const char *value
)
530 xmlChar
*encoded_element_name
= NULL
;
531 xmlChar
*encoded_value
= NULL
;
533 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
539 encoded_element_name
= encode_string(element_name
);
540 if (!encoded_element_name
) {
545 encoded_value
= encode_string(value
);
546 if (!encoded_value
) {
551 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
554 xmlFree(encoded_element_name
);
555 xmlFree(encoded_value
);
556 return ret
> 0 ? 0 : ret
;
560 void xml_error_handler(void *ctx
, const char *format
, ...)
566 va_start(args
, format
);
567 ret
= vasprintf(&errMsg
, format
, args
);
570 ERR("String allocation failed in xml error handler");
574 fprintf(stderr
, "XML Error: %s", errMsg
);
579 void fini_session_config_validation_ctx(
580 struct session_config_validation_ctx
*ctx
)
582 if (ctx
->parser_ctx
) {
583 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
587 xmlSchemaFree(ctx
->schema
);
590 if (ctx
->schema_validation_ctx
) {
591 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
594 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
598 char *get_session_config_xsd_path()
601 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
602 size_t base_path_len
;
606 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
609 base_path_len
= strlen(base_path
);
610 max_path_len
= base_path_len
+
611 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
612 xsd_path
= zmalloc(max_path_len
);
617 strncpy(xsd_path
, base_path
, max_path_len
);
618 if (xsd_path
[base_path_len
- 1] != '/') {
619 xsd_path
[base_path_len
++] = '/';
622 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
623 max_path_len
- base_path_len
);
629 int init_session_config_validation_ctx(
630 struct session_config_validation_ctx
*ctx
)
633 char *xsd_path
= get_session_config_xsd_path();
636 ret
= -LTTNG_ERR_NOMEM
;
640 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
641 if (!ctx
->parser_ctx
) {
642 ERR("XSD parser context creation failed");
643 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
646 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
647 xml_error_handler
, NULL
);
649 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
651 ERR("XSD parsing failed");
652 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
656 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
657 if (!ctx
->schema_validation_ctx
) {
658 ERR("XSD validation context creation failed");
659 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
663 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
664 xml_error_handler
, NULL
);
669 fini_session_config_validation_ctx(ctx
);
677 int parse_uint(xmlChar
*str
, uint64_t *val
)
687 *val
= strtoull((const char *) str
, &endptr
, 10);
688 if (!endptr
|| *endptr
) {
699 int parse_int(xmlChar
*str
, int64_t *val
)
709 *val
= strtoll((const char *) str
, &endptr
, 10);
710 if (!endptr
|| *endptr
) {
721 int parse_bool(xmlChar
*str
, int *val
)
730 if (!strcmp((const char *) str
, config_xml_true
)) {
732 } else if (!strcmp((const char *) str
, config_xml_false
)) {
735 WARN("Invalid boolean value encoutered (%s).",
744 int get_domain_type(xmlChar
*domain
)
752 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
753 ret
= LTTNG_DOMAIN_KERNEL
;
754 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
755 ret
= LTTNG_DOMAIN_UST
;
756 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
757 ret
= LTTNG_DOMAIN_JUL
;
758 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
759 ret
= LTTNG_DOMAIN_LOG4J
;
770 int get_buffer_type(xmlChar
*buffer_type
)
778 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
779 ret
= LTTNG_BUFFER_GLOBAL
;
780 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
781 ret
= LTTNG_BUFFER_PER_UID
;
782 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
783 ret
= LTTNG_BUFFER_PER_PID
;
794 int get_overwrite_mode(xmlChar
*overwrite_mode
)
798 if (!overwrite_mode
) {
802 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
804 } else if (!strcmp((char *) overwrite_mode
,
805 config_overwrite_mode_discard
)) {
817 int get_output_type(xmlChar
*output_type
)
825 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
826 ret
= LTTNG_EVENT_MMAP
;
827 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
828 ret
= LTTNG_EVENT_SPLICE
;
839 int get_event_type(xmlChar
*event_type
)
847 if (!strcmp((char *) event_type
, config_event_type_all
)) {
848 ret
= LTTNG_EVENT_ALL
;
849 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
850 ret
= LTTNG_EVENT_TRACEPOINT
;
851 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
852 ret
= LTTNG_EVENT_PROBE
;
853 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
854 ret
= LTTNG_EVENT_FUNCTION
;
855 } else if (!strcmp((char *) event_type
,
856 config_event_type_function_entry
)) {
857 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
858 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
859 ret
= LTTNG_EVENT_NOOP
;
860 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
861 ret
= LTTNG_EVENT_SYSCALL
;
872 int get_loglevel_type(xmlChar
*loglevel_type
)
876 if (!loglevel_type
) {
880 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
881 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
882 } else if (!strcmp((char *) loglevel_type
,
883 config_loglevel_type_range
)) {
884 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
885 } else if (!strcmp((char *) loglevel_type
,
886 config_loglevel_type_single
)) {
887 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
898 * Return the context type or -1 on error.
901 int get_context_type(xmlChar
*context_type
)
909 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
910 ret
= LTTNG_EVENT_CONTEXT_PID
;
911 } else if (!strcmp((char *) context_type
,
912 config_event_context_procname
)) {
913 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
914 } else if (!strcmp((char *) context_type
,
915 config_event_context_prio
)) {
916 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
917 } else if (!strcmp((char *) context_type
,
918 config_event_context_nice
)) {
919 ret
= LTTNG_EVENT_CONTEXT_NICE
;
920 } else if (!strcmp((char *) context_type
,
921 config_event_context_vpid
)) {
922 ret
= LTTNG_EVENT_CONTEXT_VPID
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_tid
)) {
925 ret
= LTTNG_EVENT_CONTEXT_TID
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_vtid
)) {
928 ret
= LTTNG_EVENT_CONTEXT_VTID
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_ppid
)) {
931 ret
= LTTNG_EVENT_CONTEXT_PPID
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_vppid
)) {
934 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
935 } else if (!strcmp((char *) context_type
,
936 config_event_context_pthread_id
)) {
937 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
938 } else if (!strcmp((char *) context_type
,
939 config_event_context_hostname
)) {
940 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
941 } else if (!strcmp((char *) context_type
,
942 config_event_context_ip
)) {
943 ret
= LTTNG_EVENT_CONTEXT_IP
;
954 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
959 for (node
= xmlFirstElementChild(domain_node
); node
;
960 node
= xmlNextElementSibling(node
)) {
961 if (!strcmp((const char *) node
->name
, config_element_type
)) {
963 xmlChar
*node_content
= xmlNodeGetContent(node
);
965 ret
= -LTTNG_ERR_NOMEM
;
969 ret
= get_domain_type(node_content
);
972 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
977 } else if (!strcmp((const char *) node
->name
,
978 config_element_buffer_type
)) {
980 xmlChar
*node_content
= xmlNodeGetContent(node
);
982 ret
= -LTTNG_ERR_NOMEM
;
986 ret
= get_buffer_type(node_content
);
989 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
993 domain
->buf_type
= ret
;
1002 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1007 for (node
= xmlFirstElementChild(net_output_node
); node
;
1008 node
= xmlNextElementSibling(node
)) {
1009 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1011 *control_uri
= (char *) xmlNodeGetContent(node
);
1012 if (!*control_uri
) {
1017 *data_uri
= (char *) xmlNodeGetContent(node
);
1024 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1028 int process_consumer_output(xmlNodePtr consumer_output_node
,
1029 struct consumer_output
*output
)
1036 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1037 node
= xmlNextElementSibling(node
)) {
1038 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1039 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1043 ret
= -LTTNG_ERR_NOMEM
;
1047 ret
= parse_bool(enabled_str
, &output
->enabled
);
1053 xmlNodePtr output_type_node
;
1056 output_type_node
= xmlFirstElementChild(node
);
1057 if (!output_type_node
) {
1058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1062 if (!strcmp((const char *) output_type_node
->name
,
1063 config_element_path
)) {
1065 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1066 if (!output
->path
) {
1067 ret
= -LTTNG_ERR_NOMEM
;
1072 ret
= get_net_output_uris(output_type_node
,
1073 &output
->control_uri
, &output
->data_uri
);
1085 free(output
->control_uri
);
1086 free(output
->data_uri
);
1087 memset(output
, 0, sizeof(struct consumer_output
));
1093 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1094 const char *control_uri
, const char *data_uri
)
1097 struct lttng_handle
*handle
;
1098 const char *uri
= NULL
;
1103 handle
= lttng_create_handle(name
, domain
);
1105 ret
= -LTTNG_ERR_NOMEM
;
1109 if (!control_uri
|| !data_uri
) {
1110 uri
= control_uri
? control_uri
: data_uri
;
1115 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1116 lttng_destroy_handle(handle
);
1122 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1125 xmlNodePtr node
= NULL
;
1126 xmlNodePtr snapshot_output_list_node
;
1127 xmlNodePtr snapshot_output_node
;
1129 assert(session_name
);
1131 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1140 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1142 /* Parse and create snapshot outputs */
1144 for (snapshot_output_node
=
1145 xmlFirstElementChild(snapshot_output_list_node
);
1146 snapshot_output_node
; snapshot_output_node
=
1147 xmlNextElementSibling(snapshot_output_node
)) {
1149 uint64_t max_size
= UINT64_MAX
;
1150 struct consumer_output output
= { 0 };
1151 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1153 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1154 node
= xmlNextElementSibling(node
)) {
1155 if (!strcmp((const char *) node
->name
,
1156 config_element_name
)) {
1158 name
= (char *) xmlNodeGetContent(node
);
1160 ret
= -LTTNG_ERR_NOMEM
;
1161 goto error_snapshot_output
;
1163 } else if (!strcmp((const char *) node
->name
,
1164 config_element_max_size
)) {
1165 xmlChar
*content
= xmlNodeGetContent(node
);
1169 ret
= -LTTNG_ERR_NOMEM
;
1170 goto error_snapshot_output
;
1172 ret
= parse_uint(content
, &max_size
);
1175 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1176 goto error_snapshot_output
;
1179 /* consumer_output */
1180 ret
= process_consumer_output(node
, &output
);
1182 goto error_snapshot_output
;
1187 snapshot_output
= lttng_snapshot_output_create();
1188 if (!snapshot_output
) {
1189 ret
= -LTTNG_ERR_NOMEM
;
1190 goto error_snapshot_output
;
1193 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1195 goto error_snapshot_output
;
1198 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1200 goto error_snapshot_output
;
1204 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1207 goto error_snapshot_output
;
1210 if (output
.control_uri
) {
1211 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1214 goto error_snapshot_output
;
1218 if (output
.data_uri
) {
1219 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1222 goto error_snapshot_output
;
1227 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1228 error_snapshot_output
:
1231 free(output
.control_uri
);
1232 free(output
.data_uri
);
1233 lttng_snapshot_output_destroy(snapshot_output
);
1243 int create_session(const char *name
,
1244 struct lttng_domain
*kernel_domain
,
1245 struct lttng_domain
*ust_domain
,
1246 struct lttng_domain
*jul_domain
,
1247 struct lttng_domain
*log4j_domain
,
1248 xmlNodePtr output_node
,
1249 uint64_t live_timer_interval
)
1252 struct consumer_output output
= { 0 };
1253 xmlNodePtr consumer_output_node
;
1258 consumer_output_node
= xmlFirstElementChild(output_node
);
1259 if (!consumer_output_node
) {
1260 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1264 if (strcmp((const char *) consumer_output_node
->name
,
1265 config_element_consumer_output
)) {
1266 WARN("Invalid output type, expected %s node",
1267 config_element_consumer_output
);
1268 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1272 ret
= process_consumer_output(consumer_output_node
, &output
);
1278 if (live_timer_interval
!= UINT64_MAX
&&
1279 !output
.control_uri
&& !output
.data_uri
) {
1280 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1284 if (output
.control_uri
|| output
.data_uri
) {
1286 struct lttng_domain
*domain
;
1287 struct lttng_domain
*domains
[] =
1288 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1290 /* network destination */
1291 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1293 * URLs are provided for sure since the test above make sure that
1294 * with a live timer the data and control URIs are provided. So,
1295 * NULL is passed here and will be set right after.
1297 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1299 ret
= lttng_create_session(name
, NULL
);
1305 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1306 domain
= domains
[i
];
1311 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1318 /* either local output or no output */
1319 ret
= lttng_create_session(name
, output
.path
);
1326 free(output
.control_uri
);
1327 free(output
.data_uri
);
1331 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1332 struct lttng_event_probe_attr
*attr
)
1336 assert(probe_attribute_node
);
1339 if (!strcmp((const char *) probe_attribute_node
->name
,
1340 config_element_address
)) {
1345 content
= xmlNodeGetContent(probe_attribute_node
);
1347 ret
= -LTTNG_ERR_NOMEM
;
1351 ret
= parse_uint(content
, &addr
);
1354 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1359 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1360 config_element_offset
)) {
1362 uint64_t offset
= 0;
1365 content
= xmlNodeGetContent(probe_attribute_node
);
1367 ret
= -LTTNG_ERR_NOMEM
;
1371 ret
= parse_uint(content
, &offset
);
1374 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1378 attr
->offset
= offset
;
1379 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1380 config_element_symbol_name
)) {
1385 content
= xmlNodeGetContent(probe_attribute_node
);
1387 ret
= -LTTNG_ERR_NOMEM
;
1391 name_len
= strlen((char *) content
);
1392 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1393 WARN("symbol_name too long.");
1394 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1399 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1408 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1409 const char *channel_name
, const enum process_event_node_phase phase
)
1413 struct lttng_event event
;
1414 char **exclusions
= NULL
;
1415 unsigned long exclusion_count
= 0;
1416 char *filter_expression
= NULL
;
1420 assert(channel_name
);
1422 memset(&event
, 0, sizeof(event
));
1424 /* Initialize default log level which varies by domain */
1425 switch (handle
->domain
.type
)
1427 case LTTNG_DOMAIN_JUL
:
1428 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1430 case LTTNG_DOMAIN_LOG4J
:
1431 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1433 case LTTNG_DOMAIN_UST
:
1434 case LTTNG_DOMAIN_KERNEL
:
1435 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1441 for (node
= xmlFirstElementChild(event_node
); node
;
1442 node
= xmlNextElementSibling(node
)) {
1443 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1448 content
= xmlNodeGetContent(node
);
1450 ret
= -LTTNG_ERR_NOMEM
;
1454 name_len
= strlen((char *) content
);
1455 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1456 WARN("Channel name too long.");
1457 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1462 strncpy(event
.name
, (const char *) content
, name_len
);
1464 } else if (!strcmp((const char *) node
->name
,
1465 config_element_enabled
)) {
1466 xmlChar
*content
= xmlNodeGetContent(node
);
1470 ret
= -LTTNG_ERR_NOMEM
;
1474 ret
= parse_bool(content
, &event
.enabled
);
1477 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1480 } else if (!strcmp((const char *) node
->name
,
1481 config_element_type
)) {
1482 xmlChar
*content
= xmlNodeGetContent(node
);
1486 ret
= -LTTNG_ERR_NOMEM
;
1490 ret
= get_event_type(content
);
1493 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1498 } else if (!strcmp((const char *) node
->name
,
1499 config_element_loglevel_type
)) {
1500 xmlChar
*content
= xmlNodeGetContent(node
);
1504 ret
= -LTTNG_ERR_NOMEM
;
1508 ret
= get_loglevel_type(content
);
1511 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1515 event
.loglevel_type
= ret
;
1516 } else if (!strcmp((const char *) node
->name
,
1517 config_element_loglevel
)) {
1519 int64_t loglevel
= 0;
1522 content
= xmlNodeGetContent(node
);
1524 ret
= -LTTNG_ERR_NOMEM
;
1528 ret
= parse_int(content
, &loglevel
);
1531 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1535 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1536 WARN("loglevel out of range.");
1537 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1541 event
.loglevel
= loglevel
;
1542 } else if (!strcmp((const char *) node
->name
,
1543 config_element_filter
)) {
1545 xmlNodeGetContent(node
);
1549 ret
= -LTTNG_ERR_NOMEM
;
1553 free(filter_expression
);
1554 filter_expression
= strdup((char *) content
);
1556 if (!filter_expression
) {
1557 ret
= -LTTNG_ERR_NOMEM
;
1560 } else if (!strcmp((const char *) node
->name
,
1561 config_element_exclusions
)) {
1562 xmlNodePtr exclusion_node
;
1563 int exclusion_index
= 0;
1568 * Exclusions has already been initialized,
1571 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1575 exclusion_count
= xmlChildElementCount(node
);
1576 if (!exclusion_count
) {
1580 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1582 exclusion_count
= 0;
1583 ret
= -LTTNG_ERR_NOMEM
;
1587 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1588 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1590 xmlNodeGetContent(exclusion_node
);
1593 ret
= -LTTNG_ERR_NOMEM
;
1597 exclusions
[exclusion_index
] = strdup((const char *) content
);
1599 if (!exclusions
[exclusion_index
]) {
1600 ret
= -LTTNG_ERR_NOMEM
;
1606 event
.exclusion
= 1;
1607 } else if (!strcmp((const char *) node
->name
,
1608 config_element_attributes
)) {
1609 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1612 if (!attribute_node
) {
1613 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1617 if (!strcmp((const char *) node
->name
,
1618 config_element_probe_attributes
)) {
1619 xmlNodePtr probe_attribute_node
;
1621 /* probe_attributes */
1622 for (probe_attribute_node
=
1623 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1624 probe_attribute_node
= xmlNextElementSibling(
1625 probe_attribute_node
)) {
1627 ret
= process_probe_attribute_node(probe_attribute_node
,
1636 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1638 /* function_attributes */
1639 content
= xmlNodeGetContent(symbol_node
);
1641 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1645 sym_len
= strlen((char *) content
);
1646 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1647 WARN("Function name too long.");
1648 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1653 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1660 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1661 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1662 filter_expression
, exclusion_count
, exclusions
);
1664 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1665 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1670 for (i
= 0; i
< exclusion_count
; i
++) {
1671 free(exclusions
[i
]);
1675 free(filter_expression
);
1680 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1681 const char *channel_name
)
1684 struct lttng_event event
;
1687 assert(events_node
);
1689 assert(channel_name
);
1691 for (node
= xmlFirstElementChild(events_node
); node
;
1692 node
= xmlNextElementSibling(node
)) {
1693 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1700 * Disable all events to enable only the necessary events.
1701 * Limitations regarding lttng_disable_events and tuple descriptor
1702 * force this approach.
1704 memset(&event
, 0, sizeof(event
));
1705 event
.loglevel
= -1;
1706 event
.type
= LTTNG_EVENT_ALL
;
1707 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1712 for (node
= xmlFirstElementChild(events_node
); node
;
1713 node
= xmlNextElementSibling(node
)) {
1714 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1725 int process_channel_attr_node(xmlNodePtr attr_node
,
1726 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1727 xmlNodePtr
*events_node
)
1733 assert(contexts_node
);
1734 assert(events_node
);
1736 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1741 content
= xmlNodeGetContent(attr_node
);
1743 ret
= -LTTNG_ERR_NOMEM
;
1747 name_len
= strlen((char *) content
);
1748 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1749 WARN("Channel name too long.");
1750 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1755 strncpy(channel
->name
, (const char *) content
, name_len
);
1757 } else if (!strcmp((const char *) attr_node
->name
,
1758 config_element_enabled
)) {
1763 content
= xmlNodeGetContent(attr_node
);
1765 ret
= -LTTNG_ERR_NOMEM
;
1769 ret
= parse_bool(content
, &enabled
);
1772 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1776 channel
->enabled
= enabled
;
1777 } else if (!strcmp((const char *) attr_node
->name
,
1778 config_element_overwrite_mode
)) {
1781 /* overwrite_mode */
1782 content
= xmlNodeGetContent(attr_node
);
1784 ret
= -LTTNG_ERR_NOMEM
;
1788 ret
= get_overwrite_mode(content
);
1791 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1795 channel
->attr
.overwrite
= ret
;
1796 } else if (!strcmp((const char *) attr_node
->name
,
1797 config_element_subbuf_size
)) {
1800 /* subbuffer_size */
1801 content
= xmlNodeGetContent(attr_node
);
1803 ret
= -LTTNG_ERR_NOMEM
;
1807 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1810 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1813 } else if (!strcmp((const char *) attr_node
->name
,
1814 config_element_num_subbuf
)) {
1817 /* subbuffer_count */
1818 content
= xmlNodeGetContent(attr_node
);
1820 ret
= -LTTNG_ERR_NOMEM
;
1824 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1827 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1830 } else if (!strcmp((const char *) attr_node
->name
,
1831 config_element_switch_timer_interval
)) {
1833 uint64_t switch_timer_interval
= 0;
1835 /* switch_timer_interval */
1836 content
= xmlNodeGetContent(attr_node
);
1838 ret
= -LTTNG_ERR_NOMEM
;
1842 ret
= parse_uint(content
, &switch_timer_interval
);
1845 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1849 if (switch_timer_interval
> UINT_MAX
) {
1850 WARN("switch_timer_interval out of range.");
1851 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1855 channel
->attr
.switch_timer_interval
=
1856 switch_timer_interval
;
1857 } else if (!strcmp((const char *) attr_node
->name
,
1858 config_element_read_timer_interval
)) {
1860 uint64_t read_timer_interval
= 0;
1862 /* read_timer_interval */
1863 content
= xmlNodeGetContent(attr_node
);
1865 ret
= -LTTNG_ERR_NOMEM
;
1869 ret
= parse_uint(content
, &read_timer_interval
);
1872 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1876 if (read_timer_interval
> UINT_MAX
) {
1877 WARN("read_timer_interval out of range.");
1878 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1882 channel
->attr
.read_timer_interval
=
1883 read_timer_interval
;
1884 } else if (!strcmp((const char *) attr_node
->name
,
1885 config_element_output_type
)) {
1889 content
= xmlNodeGetContent(attr_node
);
1891 ret
= -LTTNG_ERR_NOMEM
;
1895 ret
= get_output_type(content
);
1898 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1902 channel
->attr
.output
= ret
;
1903 } else if (!strcmp((const char *) attr_node
->name
,
1904 config_element_tracefile_size
)) {
1907 /* tracefile_size */
1908 content
= xmlNodeGetContent(attr_node
);
1910 ret
= -LTTNG_ERR_NOMEM
;
1914 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1917 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1920 } else if (!strcmp((const char *) attr_node
->name
,
1921 config_element_tracefile_count
)) {
1924 /* tracefile_count */
1925 content
= xmlNodeGetContent(attr_node
);
1927 ret
= -LTTNG_ERR_NOMEM
;
1931 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1934 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1937 } else if (!strcmp((const char *) attr_node
->name
,
1938 config_element_live_timer_interval
)) {
1940 uint64_t live_timer_interval
= 0;
1942 /* live_timer_interval */
1943 content
= xmlNodeGetContent(attr_node
);
1945 ret
= -LTTNG_ERR_NOMEM
;
1949 ret
= parse_uint(content
, &live_timer_interval
);
1952 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1956 if (live_timer_interval
> UINT_MAX
) {
1957 WARN("live_timer_interval out of range.");
1958 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1962 channel
->attr
.live_timer_interval
=
1963 live_timer_interval
;
1964 } else if (!strcmp((const char *) attr_node
->name
,
1965 config_element_events
)) {
1967 *events_node
= attr_node
;
1970 *contexts_node
= attr_node
;
1978 int process_context_node(xmlNodePtr context_node
,
1979 struct lttng_handle
*handle
, const char *channel_name
)
1982 struct lttng_event_context context
;
1983 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1986 assert(channel_name
);
1988 if (!context_child_node
) {
1989 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1993 memset(&context
, 0, sizeof(context
));
1995 if (!strcmp((const char *) context_child_node
->name
,
1996 config_element_type
)) {
1998 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2000 ret
= -LTTNG_ERR_NOMEM
;
2004 ret
= get_context_type(content
);
2007 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2013 xmlNodePtr perf_attr_node
;
2016 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2017 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2018 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2019 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2020 perf_attr_node
; perf_attr_node
=
2021 xmlNextElementSibling(perf_attr_node
)) {
2022 if (!strcmp((const char *) perf_attr_node
->name
,
2023 config_element_type
)) {
2028 content
= xmlNodeGetContent(perf_attr_node
);
2030 ret
= -LTTNG_ERR_NOMEM
;
2034 ret
= parse_uint(content
, &type
);
2037 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2041 if (type
> UINT32_MAX
) {
2042 WARN("perf context type out of range.");
2043 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2047 context
.u
.perf_counter
.type
= type
;
2048 } else if (!strcmp((const char *) perf_attr_node
->name
,
2049 config_element_config
)) {
2051 uint64_t config
= 0;
2054 content
= xmlNodeGetContent(perf_attr_node
);
2056 ret
= -LTTNG_ERR_NOMEM
;
2060 ret
= parse_uint(content
, &config
);
2063 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2067 context
.u
.perf_counter
.config
= config
;
2068 } else if (!strcmp((const char *) perf_attr_node
->name
,
2069 config_element_name
)) {
2074 content
= xmlNodeGetContent(perf_attr_node
);
2076 ret
= -LTTNG_ERR_NOMEM
;
2080 name_len
= strlen((char *) content
);
2081 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2082 WARN("perf context name too long.");
2083 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2088 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2095 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2101 int process_contexts_node(xmlNodePtr contexts_node
,
2102 struct lttng_handle
*handle
, const char *channel_name
)
2105 xmlNodePtr context_node
;
2107 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2108 context_node
= xmlNextElementSibling(context_node
)) {
2109 ret
= process_context_node(context_node
, handle
, channel_name
);
2119 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2122 struct lttng_domain domain
= { 0 };
2123 struct lttng_handle
*handle
= NULL
;
2124 xmlNodePtr channels_node
= NULL
;
2127 assert(session_name
);
2129 ret
= init_domain(domain_node
, &domain
);
2134 handle
= lttng_create_handle(session_name
, &domain
);
2136 ret
= -LTTNG_ERR_NOMEM
;
2140 /* get the channels node */
2141 for (node
= xmlFirstElementChild(domain_node
); node
;
2142 node
= xmlNextElementSibling(node
)) {
2143 if (!strcmp((const char *) node
->name
,
2144 config_element_channels
)) {
2145 channels_node
= node
;
2150 if (!channels_node
) {
2154 /* create all channels */
2155 for (node
= xmlFirstElementChild(channels_node
); node
;
2156 node
= xmlNextElementSibling(node
)) {
2157 struct lttng_channel channel
;
2158 xmlNodePtr contexts_node
= NULL
;
2159 xmlNodePtr events_node
= NULL
;
2160 xmlNodePtr channel_attr_node
;
2162 memset(&channel
, 0, sizeof(channel
));
2163 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2165 for (channel_attr_node
= xmlFirstElementChild(node
);
2166 channel_attr_node
; channel_attr_node
=
2167 xmlNextElementSibling(channel_attr_node
)) {
2168 ret
= process_channel_attr_node(channel_attr_node
,
2169 &channel
, &contexts_node
, &events_node
);
2175 ret
= lttng_enable_channel(handle
, &channel
);
2180 ret
= process_events_node(events_node
, handle
, channel
.name
);
2185 ret
= process_contexts_node(contexts_node
, handle
,
2192 lttng_destroy_handle(handle
);
2197 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2200 int ret
, started
= -1, snapshot_mode
= -1;
2201 uint64_t live_timer_interval
= UINT64_MAX
;
2202 xmlChar
*name
= NULL
;
2203 xmlNodePtr domains_node
= NULL
;
2204 xmlNodePtr output_node
= NULL
;
2206 struct lttng_domain
*kernel_domain
= NULL
;
2207 struct lttng_domain
*ust_domain
= NULL
;
2208 struct lttng_domain
*jul_domain
= NULL
;
2209 struct lttng_domain
*log4j_domain
= NULL
;
2211 for (node
= xmlFirstElementChild(session_node
); node
;
2212 node
= xmlNextElementSibling(node
)) {
2213 if (!name
&& !strcmp((const char *) node
->name
,
2214 config_element_name
)) {
2216 xmlChar
*node_content
= xmlNodeGetContent(node
);
2217 if (!node_content
) {
2218 ret
= -LTTNG_ERR_NOMEM
;
2222 name
= node_content
;
2223 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2224 config_element_domains
)) {
2226 domains_node
= node
;
2227 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2228 config_element_started
)) {
2230 xmlChar
*node_content
= xmlNodeGetContent(node
);
2231 if (!node_content
) {
2232 ret
= -LTTNG_ERR_NOMEM
;
2236 ret
= parse_bool(node_content
, &started
);
2239 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2242 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2243 config_element_output
)) {
2247 /* attributes, snapshot_mode or live_timer_interval */
2248 xmlNodePtr attributes_child
=
2249 xmlFirstElementChild(node
);
2251 if (!strcmp((const char *) attributes_child
->name
,
2252 config_element_snapshot_mode
)) {
2254 xmlChar
*snapshot_mode_content
=
2255 xmlNodeGetContent(attributes_child
);
2256 if (!snapshot_mode_content
) {
2257 ret
= -LTTNG_ERR_NOMEM
;
2261 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2262 free(snapshot_mode_content
);
2264 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2268 /* live_timer_interval */
2269 xmlChar
*timer_interval_content
=
2270 xmlNodeGetContent(attributes_child
);
2271 if (!timer_interval_content
) {
2272 ret
= -LTTNG_ERR_NOMEM
;
2276 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2277 free(timer_interval_content
);
2279 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2287 /* Mandatory attribute, as defined in the session XSD */
2288 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2292 if (session_name
&& strcmp((char *) name
, session_name
)) {
2293 /* This is not the session we are looking for */
2294 ret
= -LTTNG_ERR_NO_SESSION
;
2298 /* Init domains to create the session handles */
2299 for (node
= xmlFirstElementChild(domains_node
); node
;
2300 node
= xmlNextElementSibling(node
)) {
2301 struct lttng_domain
*domain
;
2303 domain
= zmalloc(sizeof(*domain
));
2305 ret
= -LTTNG_ERR_NOMEM
;
2309 ret
= init_domain(node
, domain
);
2311 goto domain_init_error
;
2314 switch (domain
->type
) {
2315 case LTTNG_DOMAIN_KERNEL
:
2316 if (kernel_domain
) {
2317 /* Same domain seen twice, invalid! */
2318 goto domain_init_error
;
2320 kernel_domain
= domain
;
2322 case LTTNG_DOMAIN_UST
:
2324 /* Same domain seen twice, invalid! */
2325 goto domain_init_error
;
2327 ust_domain
= domain
;
2329 case LTTNG_DOMAIN_JUL
:
2331 /* Same domain seen twice, invalid! */
2332 goto domain_init_error
;
2334 jul_domain
= domain
;
2336 case LTTNG_DOMAIN_LOG4J
:
2338 /* Same domain seen twice, invalid! */
2339 goto domain_init_error
;
2341 log4j_domain
= domain
;
2344 WARN("Invalid domain type");
2345 goto domain_init_error
;
2350 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2355 /* Destroy session if it exists */
2356 ret
= lttng_destroy_session((const char *) name
);
2357 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2358 ERR("Failed to destroy existing session.");
2363 /* Create session type depending on output type */
2364 if (snapshot_mode
&& snapshot_mode
!= -1) {
2365 ret
= create_snapshot_session((const char *) name
, output_node
);
2366 } else if (live_timer_interval
&&
2367 live_timer_interval
!= UINT64_MAX
) {
2368 ret
= create_session((const char *) name
, kernel_domain
,
2369 ust_domain
, jul_domain
, log4j_domain
,
2370 output_node
, live_timer_interval
);
2372 /* regular session */
2373 ret
= create_session((const char *) name
, kernel_domain
,
2374 ust_domain
, jul_domain
, log4j_domain
,
2375 output_node
, UINT64_MAX
);
2381 for (node
= xmlFirstElementChild(domains_node
); node
;
2382 node
= xmlNextElementSibling(node
)) {
2383 ret
= process_domain_node(node
, (const char *) name
);
2390 ret
= lttng_start_tracing((const char *) name
);
2398 ERR("Failed to load session %s: %s", (const char *) name
,
2399 lttng_strerror(ret
));
2400 lttng_destroy_session((const char *) name
);
2404 free(kernel_domain
);
2413 * Return 1 if the given path is readable by the current UID or 0 if not.
2414 * Return -1 if the path is EPERM.
2416 static int validate_file_read_creds(const char *path
)
2422 /* Can we read the file. */
2423 ret
= access(path
, R_OK
);
2427 if (errno
== EACCES
) {
2438 int load_session_from_file(const char *path
, const char *session_name
,
2439 struct session_config_validation_ctx
*validation_ctx
, int override
)
2441 int ret
, session_found
= !session_name
;
2442 xmlDocPtr doc
= NULL
;
2443 xmlNodePtr sessions_node
;
2444 xmlNodePtr session_node
;
2447 assert(validation_ctx
);
2449 ret
= validate_file_read_creds(path
);
2452 ret
= -LTTNG_ERR_EPERM
;
2454 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2459 doc
= xmlParseFile(path
);
2461 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2465 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2467 ERR("Session configuration file validation failed");
2468 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2472 sessions_node
= xmlDocGetRootElement(doc
);
2473 if (!sessions_node
) {
2477 for (session_node
= xmlFirstElementChild(sessions_node
);
2478 session_node
; session_node
=
2479 xmlNextElementSibling(session_node
)) {
2480 ret
= process_session_node(session_node
,
2481 session_name
, override
);
2482 if (session_name
&& ret
== 0) {
2483 /* Target session found and loaded */
2491 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2496 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2498 struct dirent
*alloc_dirent(const char *path
)
2502 struct dirent
*entry
;
2504 name_max
= pathconf(path
, _PC_NAME_MAX
);
2505 if (name_max
== -1) {
2506 name_max
= PATH_MAX
;
2508 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2509 entry
= zmalloc(len
);
2514 int load_session_from_path(const char *path
, const char *session_name
,
2515 struct session_config_validation_ctx
*validation_ctx
, int override
)
2517 int ret
, session_found
= !session_name
;
2518 DIR *directory
= NULL
;
2521 assert(validation_ctx
);
2523 directory
= opendir(path
);
2527 /* Try the file loading. */
2530 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2533 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2538 struct dirent
*entry
;
2539 struct dirent
*result
;
2540 char *file_path
= NULL
;
2541 size_t path_len
= strlen(path
);
2543 if (path_len
>= PATH_MAX
) {
2544 ret
= -LTTNG_ERR_INVALID
;
2548 entry
= alloc_dirent(path
);
2550 ret
= -LTTNG_ERR_NOMEM
;
2554 file_path
= zmalloc(PATH_MAX
);
2556 ret
= -LTTNG_ERR_NOMEM
;
2561 strncpy(file_path
, path
, path_len
);
2562 if (file_path
[path_len
- 1] != '/') {
2563 file_path
[path_len
++] = '/';
2567 /* Search for *.lttng files */
2568 while (!readdir_r(directory
, entry
, &result
) && result
) {
2569 size_t file_name_len
= strlen(result
->d_name
);
2571 if (file_name_len
<=
2572 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2576 if (path_len
+ file_name_len
>= PATH_MAX
) {
2580 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2581 result
->d_name
+ file_name_len
- sizeof(
2582 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2586 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2587 file_path
[path_len
+ file_name_len
] = '\0';
2589 ret
= load_session_from_file(file_path
, session_name
,
2590 validation_ctx
, override
);
2591 if (session_name
&& !ret
) {
2600 ret
= load_session_from_file(path
, session_name
,
2601 validation_ctx
, override
);
2611 if (closedir(directory
)) {
2616 if (session_found
) {
2624 * Validate that the given path's credentials and the current process have the
2625 * same UID. If so, return 1 else return 0 if it does NOT match.
2627 static int validate_path_creds(const char *path
)
2629 int ret
, uid
= getuid();
2638 ret
= stat(path
, &buf
);
2640 if (errno
!= ENOENT
) {
2643 ret
= -LTTNG_ERR_INVALID
;
2647 if (buf
.st_uid
!= uid
) {
2658 int config_load_session(const char *path
, const char *session_name
,
2659 int override
, unsigned int autoload
)
2662 bool session_loaded
= false;
2663 const char *path_ptr
= NULL
;
2664 struct session_config_validation_ctx validation_ctx
= { 0 };
2666 ret
= init_session_config_validation_ctx(&validation_ctx
);
2673 const char *sys_path
;
2676 home_path
= utils_get_home_dir();
2678 char path
[PATH_MAX
];
2681 * Try user session configuration path. Ignore error here so we can
2682 * continue loading the system wide sessions.
2685 ret
= snprintf(path
, sizeof(path
),
2686 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2687 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2689 PERROR("snprintf session autoload home config path");
2694 * Credentials are only validated for the autoload in order to
2695 * avoid any user session daemon to try to load kernel sessions
2696 * automatically and failing all the times.
2698 ret
= validate_path_creds(path
);
2703 ret
= snprintf(path
, sizeof(path
),
2704 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2706 PERROR("snprintf session home config path");
2712 ret
= load_session_from_path(path_ptr
, session_name
,
2713 &validation_ctx
, override
);
2714 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2718 * Continue even if the session was found since we have to try
2719 * the system wide sessions.
2721 session_loaded
= true;
2725 /* Reset path pointer for the system wide dir. */
2728 /* Try system wide configuration directory. */
2730 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2731 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2732 ret
= validate_path_creds(sys_path
);
2734 path_ptr
= sys_path
;
2737 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2738 path_ptr
= sys_path
;
2742 ret
= load_session_from_path(path_ptr
, session_name
,
2743 &validation_ctx
, override
);
2745 session_loaded
= true;
2749 ret
= access(path
, F_OK
);
2754 ret
= -LTTNG_ERR_INVALID
;
2755 WARN("Session configuration path does not exist.");
2758 ret
= -LTTNG_ERR_EPERM
;
2761 ret
= -LTTNG_ERR_UNK
;
2767 ret
= load_session_from_path(path
, session_name
,
2768 &validation_ctx
, override
);
2771 fini_session_config_validation_ctx(&validation_ctx
);
2772 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2774 * Don't report an error if no sessions are found when called
2775 * without a session_name or a search path.
2780 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2781 /* A matching session was found in one of the search paths. */