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.
28 #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 <common/compat/getenv.h>
36 #include <lttng/lttng-error.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlschemas.h>
40 #include <libxml/tree.h>
41 #include <lttng/lttng.h>
42 #include <lttng/snapshot.h>
45 #include "config-internal.h"
47 struct handler_filter_args
{
49 config_entry_handler_cb handler
;
53 struct session_config_validation_ctx
{
54 xmlSchemaParserCtxtPtr parser_ctx
;
56 xmlSchemaValidCtxtPtr schema_validation_ctx
;
59 const char * const config_str_yes
= "yes";
60 const char * const config_str_true
= "true";
61 const char * const config_str_on
= "on";
62 const char * const config_str_no
= "no";
63 const char * const config_str_false
= "false";
64 const char * const config_str_off
= "off";
65 const char * const config_xml_encoding
= "UTF-8";
66 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
67 const char * const config_xml_indent_string
= "\t";
68 const char * const config_xml_true
= "true";
69 const char * const config_xml_false
= "false";
71 const char * const config_element_channel
= "channel";
72 const char * const config_element_channels
= "channels";
73 const char * const config_element_domain
= "domain";
74 const char * const config_element_domains
= "domains";
75 const char * const config_element_event
= "event";
76 const char * const config_element_events
= "events";
77 const char * const config_element_context
= "context";
78 const char * const config_element_contexts
= "contexts";
79 const char * const config_element_attributes
= "attributes";
80 const char * const config_element_exclusion
= "exclusion";
81 const char * const config_element_exclusions
= "exclusions";
82 const char * const config_element_function_attributes
= "function_attributes";
83 const char * const config_element_probe_attributes
= "probe_attributes";
84 const char * const config_element_symbol_name
= "symbol_name";
85 const char * const config_element_address
= "address";
86 const char * const config_element_offset
= "offset";
87 const char * const config_element_name
= "name";
88 const char * const config_element_enabled
= "enabled";
89 const char * const config_element_overwrite_mode
= "overwrite_mode";
90 const char * const config_element_subbuf_size
= "subbuffer_size";
91 const char * const config_element_num_subbuf
= "subbuffer_count";
92 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
93 const char * const config_element_read_timer_interval
= "read_timer_interval";
94 const char * const config_element_output
= "output";
95 const char * const config_element_output_type
= "output_type";
96 const char * const config_element_tracefile_size
= "tracefile_size";
97 const char * const config_element_tracefile_count
= "tracefile_count";
98 const char * const config_element_live_timer_interval
= "live_timer_interval";
99 const char * const config_element_type
= "type";
100 const char * const config_element_buffer_type
= "buffer_type";
101 const char * const config_element_session
= "session";
102 const char * const config_element_sessions
= "sessions";
103 const char * const config_element_perf
= "perf";
104 const char * const config_element_config
= "config";
105 const char * const config_element_started
= "started";
106 const char * const config_element_snapshot_mode
= "snapshot_mode";
107 const char * const config_element_loglevel
= "loglevel";
108 const char * const config_element_loglevel_type
= "loglevel_type";
109 const char * const config_element_filter
= "filter";
110 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
111 const char * const config_element_consumer_output
= "consumer_output";
112 const char * const config_element_destination
= "destination";
113 const char * const config_element_path
= "path";
114 const char * const config_element_net_output
= "net_output";
115 const char * const config_element_control_uri
= "control_uri";
116 const char * const config_element_data_uri
= "data_uri";
117 const char * const config_element_max_size
= "max_size";
118 const char * const config_element_pid
= "pid";
119 const char * const config_element_pids
= "pids";
120 const char * const config_element_shared_memory_path
= "shared_memory_path";
121 const char * const config_element_pid_tracker
= "pid_tracker";
122 const char * const config_element_trackers
= "trackers";
123 const char * const config_element_targets
= "targets";
124 const char * const config_element_target_pid
= "pid_target";
126 const char * const config_domain_type_kernel
= "KERNEL";
127 const char * const config_domain_type_ust
= "UST";
128 const char * const config_domain_type_jul
= "JUL";
129 const char * const config_domain_type_log4j
= "LOG4J";
130 const char * const config_domain_type_python
= "PYTHON";
132 const char * const config_buffer_type_per_pid
= "PER_PID";
133 const char * const config_buffer_type_per_uid
= "PER_UID";
134 const char * const config_buffer_type_global
= "GLOBAL";
136 const char * const config_overwrite_mode_discard
= "DISCARD";
137 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
139 const char * const config_output_type_splice
= "SPLICE";
140 const char * const config_output_type_mmap
= "MMAP";
142 const char * const config_loglevel_type_all
= "ALL";
143 const char * const config_loglevel_type_range
= "RANGE";
144 const char * const config_loglevel_type_single
= "SINGLE";
146 const char * const config_event_type_all
= "ALL";
147 const char * const config_event_type_tracepoint
= "TRACEPOINT";
148 const char * const config_event_type_probe
= "PROBE";
149 const char * const config_event_type_function
= "FUNCTION";
150 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
151 const char * const config_event_type_noop
= "NOOP";
152 const char * const config_event_type_syscall
= "SYSCALL";
153 const char * const config_event_type_kprobe
= "KPROBE";
154 const char * const config_event_type_kretprobe
= "KRETPROBE";
156 const char * const config_event_context_pid
= "PID";
157 const char * const config_event_context_procname
= "PROCNAME";
158 const char * const config_event_context_prio
= "PRIO";
159 const char * const config_event_context_nice
= "NICE";
160 const char * const config_event_context_vpid
= "VPID";
161 const char * const config_event_context_tid
= "TID";
162 const char * const config_event_context_vtid
= "VTID";
163 const char * const config_event_context_ppid
= "PPID";
164 const char * const config_event_context_vppid
= "VPPID";
165 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
166 const char * const config_event_context_hostname
= "HOSTNAME";
167 const char * const config_event_context_ip
= "IP";
168 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
170 struct consumer_output
{
177 static int config_entry_handler_filter(struct handler_filter_args
*args
,
178 const char *section
, const char *name
, const char *value
)
181 struct config_entry entry
= { section
, name
, value
};
185 if (!section
|| !name
|| !value
) {
191 if (strcmp(args
->section
, section
)) {
196 ret
= args
->handler(&entry
, args
->user_data
);
202 int config_get_section_entries(const char *override_path
, const char *section
,
203 config_entry_handler_cb handler
, void *user_data
)
207 FILE *config_file
= NULL
;
208 struct handler_filter_args filter
= { section
, handler
, user_data
};
210 /* First, try system-wide conf. file. */
211 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
213 config_file
= fopen(path
, "r");
215 DBG("Loading daemon conf file at %s", path
);
217 * Return value is not very important here since error or not, we
218 * continue and try the next possible conf. file.
220 (void) ini_parse_file(config_file
,
221 (ini_entry_handler
) config_entry_handler_filter
,
226 /* Second is the user local configuration. */
227 path
= utils_get_home_dir();
229 char fullpath
[PATH_MAX
];
231 ret
= snprintf(fullpath
, sizeof(fullpath
),
232 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
234 PERROR("snprintf user conf. path");
238 config_file
= fopen(fullpath
, "r");
240 DBG("Loading daemon user conf file at %s", path
);
242 * Return value is not very important here since error or not, we
243 * continue and try the next possible conf. file.
245 (void) ini_parse_file(config_file
,
246 (ini_entry_handler
) config_entry_handler_filter
,
252 /* Final path is the one that the user might have provided. */
254 config_file
= fopen(override_path
, "r");
256 DBG("Loading daemon command line conf file at %s", override_path
);
257 (void) ini_parse_file(config_file
,
258 (ini_entry_handler
) config_entry_handler_filter
,
262 ERR("Failed to open daemon configuration file at %s",
269 /* Everything went well. */
277 int config_parse_value(const char *value
)
280 char *endptr
, *lower_str
;
290 v
= strtoul(value
, &endptr
, 10);
291 if (endptr
!= value
) {
296 lower_str
= zmalloc(len
+ 1);
303 for (i
= 0; i
< len
; i
++) {
304 lower_str
[i
] = tolower(value
[i
]);
307 if (!strcmp(lower_str
, config_str_yes
) ||
308 !strcmp(lower_str
, config_str_true
) ||
309 !strcmp(lower_str
, config_str_on
)) {
311 } else if (!strcmp(lower_str
, config_str_no
) ||
312 !strcmp(lower_str
, config_str_false
) ||
313 !strcmp(lower_str
, config_str_off
)) {
325 * Returns a xmlChar string which must be released using xmlFree().
327 static xmlChar
*encode_string(const char *in_str
)
329 xmlChar
*out_str
= NULL
;
330 xmlCharEncodingHandlerPtr handler
;
331 int out_len
, ret
, in_len
;
335 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
337 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
341 in_len
= strlen(in_str
);
343 * Add 1 byte for the NULL terminted character. The factor 4 here is
344 * used because UTF-8 characters can take up to 4 bytes.
346 out_len
= (in_len
* 4) + 1;
347 out_str
= xmlMalloc(out_len
);
352 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
359 /* out_len is now the size of out_str */
360 out_str
[out_len
] = '\0';
366 struct config_writer
*config_writer_create(int fd_output
, int indent
)
369 struct config_writer
*writer
;
370 xmlOutputBufferPtr buffer
;
372 writer
= zmalloc(sizeof(struct config_writer
));
374 PERROR("zmalloc config_writer_create");
378 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
383 writer
->writer
= xmlNewTextWriter(buffer
);
384 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
385 config_xml_encoding
, NULL
);
390 ret
= xmlTextWriterSetIndentString(writer
->writer
,
391 BAD_CAST config_xml_indent_string
);
396 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
404 config_writer_destroy(writer
);
409 int config_writer_destroy(struct config_writer
*writer
)
418 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
419 WARN("Could not close XML document");
423 if (writer
->writer
) {
424 xmlFreeTextWriter(writer
->writer
);
433 int config_writer_open_element(struct config_writer
*writer
,
434 const char *element_name
)
437 xmlChar
*encoded_element_name
;
439 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
444 encoded_element_name
= encode_string(element_name
);
445 if (!encoded_element_name
) {
450 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
451 xmlFree(encoded_element_name
);
453 return ret
> 0 ? 0 : ret
;
457 int config_writer_close_element(struct config_writer
*writer
)
461 if (!writer
|| !writer
->writer
) {
466 ret
= xmlTextWriterEndElement(writer
->writer
);
468 return ret
> 0 ? 0 : ret
;
472 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
473 const char *element_name
, uint64_t value
)
476 xmlChar
*encoded_element_name
;
478 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
483 encoded_element_name
= encode_string(element_name
);
484 if (!encoded_element_name
) {
489 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
490 encoded_element_name
, "%" PRIu64
, value
);
491 xmlFree(encoded_element_name
);
493 return ret
> 0 ? 0 : ret
;
497 int config_writer_write_element_signed_int(struct config_writer
*writer
,
498 const char *element_name
, int64_t value
)
501 xmlChar
*encoded_element_name
;
503 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
508 encoded_element_name
= encode_string(element_name
);
509 if (!encoded_element_name
) {
514 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
515 encoded_element_name
, "%" PRIi64
, value
);
516 xmlFree(encoded_element_name
);
518 return ret
> 0 ? 0 : ret
;
522 int config_writer_write_element_bool(struct config_writer
*writer
,
523 const char *element_name
, int value
)
525 return config_writer_write_element_string(writer
, element_name
,
526 value
? config_xml_true
: config_xml_false
);
530 int config_writer_write_element_string(struct config_writer
*writer
,
531 const char *element_name
, const char *value
)
534 xmlChar
*encoded_element_name
= NULL
;
535 xmlChar
*encoded_value
= NULL
;
537 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
543 encoded_element_name
= encode_string(element_name
);
544 if (!encoded_element_name
) {
549 encoded_value
= encode_string(value
);
550 if (!encoded_value
) {
555 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
558 xmlFree(encoded_element_name
);
559 xmlFree(encoded_value
);
560 return ret
> 0 ? 0 : ret
;
564 void xml_error_handler(void *ctx
, const char *format
, ...)
570 va_start(args
, format
);
571 ret
= vasprintf(&errMsg
, format
, args
);
574 ERR("String allocation failed in xml error handler");
578 fprintf(stderr
, "XML Error: %s", errMsg
);
583 void fini_session_config_validation_ctx(
584 struct session_config_validation_ctx
*ctx
)
586 if (ctx
->parser_ctx
) {
587 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
591 xmlSchemaFree(ctx
->schema
);
594 if (ctx
->schema_validation_ctx
) {
595 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
598 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
602 char *get_session_config_xsd_path()
605 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
606 size_t base_path_len
;
610 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
613 base_path_len
= strlen(base_path
);
614 max_path_len
= base_path_len
+
615 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
616 xsd_path
= zmalloc(max_path_len
);
621 strncpy(xsd_path
, base_path
, max_path_len
);
622 if (xsd_path
[base_path_len
- 1] != '/') {
623 xsd_path
[base_path_len
++] = '/';
626 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
627 max_path_len
- base_path_len
);
633 int init_session_config_validation_ctx(
634 struct session_config_validation_ctx
*ctx
)
637 char *xsd_path
= get_session_config_xsd_path();
640 ret
= -LTTNG_ERR_NOMEM
;
644 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
645 if (!ctx
->parser_ctx
) {
646 ERR("XSD parser context creation failed");
647 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
650 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
651 xml_error_handler
, NULL
);
653 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
655 ERR("XSD parsing failed");
656 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
660 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
661 if (!ctx
->schema_validation_ctx
) {
662 ERR("XSD validation context creation failed");
663 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
667 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
668 xml_error_handler
, NULL
);
673 fini_session_config_validation_ctx(ctx
);
681 int parse_uint(xmlChar
*str
, uint64_t *val
)
691 *val
= strtoull((const char *) str
, &endptr
, 10);
692 if (!endptr
|| *endptr
) {
703 int parse_int(xmlChar
*str
, int64_t *val
)
713 *val
= strtoll((const char *) str
, &endptr
, 10);
714 if (!endptr
|| *endptr
) {
725 int parse_bool(xmlChar
*str
, int *val
)
734 if (!strcmp((const char *) str
, config_xml_true
)) {
736 } else if (!strcmp((const char *) str
, config_xml_false
)) {
739 WARN("Invalid boolean value encoutered (%s).",
748 int get_domain_type(xmlChar
*domain
)
756 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
757 ret
= LTTNG_DOMAIN_KERNEL
;
758 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
759 ret
= LTTNG_DOMAIN_UST
;
760 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
761 ret
= LTTNG_DOMAIN_JUL
;
762 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
763 ret
= LTTNG_DOMAIN_LOG4J
;
764 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
765 ret
= LTTNG_DOMAIN_PYTHON
;
776 int get_buffer_type(xmlChar
*buffer_type
)
784 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
785 ret
= LTTNG_BUFFER_GLOBAL
;
786 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
787 ret
= LTTNG_BUFFER_PER_UID
;
788 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
789 ret
= LTTNG_BUFFER_PER_PID
;
800 int get_overwrite_mode(xmlChar
*overwrite_mode
)
804 if (!overwrite_mode
) {
808 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
810 } else if (!strcmp((char *) overwrite_mode
,
811 config_overwrite_mode_discard
)) {
823 int get_output_type(xmlChar
*output_type
)
831 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
832 ret
= LTTNG_EVENT_MMAP
;
833 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
834 ret
= LTTNG_EVENT_SPLICE
;
845 int get_event_type(xmlChar
*event_type
)
853 if (!strcmp((char *) event_type
, config_event_type_all
)) {
854 ret
= LTTNG_EVENT_ALL
;
855 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
856 ret
= LTTNG_EVENT_TRACEPOINT
;
857 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
858 ret
= LTTNG_EVENT_PROBE
;
859 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
860 ret
= LTTNG_EVENT_FUNCTION
;
861 } else if (!strcmp((char *) event_type
,
862 config_event_type_function_entry
)) {
863 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
864 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
865 ret
= LTTNG_EVENT_NOOP
;
866 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
867 ret
= LTTNG_EVENT_SYSCALL
;
878 int get_loglevel_type(xmlChar
*loglevel_type
)
882 if (!loglevel_type
) {
886 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
887 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
888 } else if (!strcmp((char *) loglevel_type
,
889 config_loglevel_type_range
)) {
890 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
891 } else if (!strcmp((char *) loglevel_type
,
892 config_loglevel_type_single
)) {
893 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
904 * Return the context type or -1 on error.
907 int get_context_type(xmlChar
*context_type
)
915 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
916 ret
= LTTNG_EVENT_CONTEXT_PID
;
917 } else if (!strcmp((char *) context_type
,
918 config_event_context_procname
)) {
919 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
920 } else if (!strcmp((char *) context_type
,
921 config_event_context_prio
)) {
922 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_nice
)) {
925 ret
= LTTNG_EVENT_CONTEXT_NICE
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_vpid
)) {
928 ret
= LTTNG_EVENT_CONTEXT_VPID
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_tid
)) {
931 ret
= LTTNG_EVENT_CONTEXT_TID
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_vtid
)) {
934 ret
= LTTNG_EVENT_CONTEXT_VTID
;
935 } else if (!strcmp((char *) context_type
,
936 config_event_context_ppid
)) {
937 ret
= LTTNG_EVENT_CONTEXT_PPID
;
938 } else if (!strcmp((char *) context_type
,
939 config_event_context_vppid
)) {
940 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
941 } else if (!strcmp((char *) context_type
,
942 config_event_context_pthread_id
)) {
943 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
944 } else if (!strcmp((char *) context_type
,
945 config_event_context_hostname
)) {
946 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
947 } else if (!strcmp((char *) context_type
,
948 config_event_context_ip
)) {
949 ret
= LTTNG_EVENT_CONTEXT_IP
;
960 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
965 for (node
= xmlFirstElementChild(domain_node
); node
;
966 node
= xmlNextElementSibling(node
)) {
967 if (!strcmp((const char *) node
->name
, config_element_type
)) {
969 xmlChar
*node_content
= xmlNodeGetContent(node
);
971 ret
= -LTTNG_ERR_NOMEM
;
975 ret
= get_domain_type(node_content
);
978 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
983 } else if (!strcmp((const char *) node
->name
,
984 config_element_buffer_type
)) {
986 xmlChar
*node_content
= xmlNodeGetContent(node
);
988 ret
= -LTTNG_ERR_NOMEM
;
992 ret
= get_buffer_type(node_content
);
995 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
999 domain
->buf_type
= ret
;
1008 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1013 for (node
= xmlFirstElementChild(net_output_node
); node
;
1014 node
= xmlNextElementSibling(node
)) {
1015 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1017 *control_uri
= (char *) xmlNodeGetContent(node
);
1018 if (!*control_uri
) {
1023 *data_uri
= (char *) xmlNodeGetContent(node
);
1030 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1034 int process_consumer_output(xmlNodePtr consumer_output_node
,
1035 struct consumer_output
*output
)
1042 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1043 node
= xmlNextElementSibling(node
)) {
1044 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1045 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1049 ret
= -LTTNG_ERR_NOMEM
;
1053 ret
= parse_bool(enabled_str
, &output
->enabled
);
1059 xmlNodePtr output_type_node
;
1062 output_type_node
= xmlFirstElementChild(node
);
1063 if (!output_type_node
) {
1064 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1068 if (!strcmp((const char *) output_type_node
->name
,
1069 config_element_path
)) {
1071 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1072 if (!output
->path
) {
1073 ret
= -LTTNG_ERR_NOMEM
;
1078 ret
= get_net_output_uris(output_type_node
,
1079 &output
->control_uri
, &output
->data_uri
);
1091 free(output
->control_uri
);
1092 free(output
->data_uri
);
1093 memset(output
, 0, sizeof(struct consumer_output
));
1099 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1100 const char *control_uri
, const char *data_uri
)
1103 struct lttng_handle
*handle
;
1104 const char *uri
= NULL
;
1109 handle
= lttng_create_handle(name
, domain
);
1111 ret
= -LTTNG_ERR_NOMEM
;
1115 if (!control_uri
|| !data_uri
) {
1116 uri
= control_uri
? control_uri
: data_uri
;
1121 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1122 lttng_destroy_handle(handle
);
1128 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1131 xmlNodePtr node
= NULL
;
1132 xmlNodePtr snapshot_output_list_node
;
1133 xmlNodePtr snapshot_output_node
;
1135 assert(session_name
);
1137 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1146 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1148 /* Parse and create snapshot outputs */
1150 for (snapshot_output_node
=
1151 xmlFirstElementChild(snapshot_output_list_node
);
1152 snapshot_output_node
; snapshot_output_node
=
1153 xmlNextElementSibling(snapshot_output_node
)) {
1155 uint64_t max_size
= UINT64_MAX
;
1156 struct consumer_output output
= { 0 };
1157 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1159 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1160 node
= xmlNextElementSibling(node
)) {
1161 if (!strcmp((const char *) node
->name
,
1162 config_element_name
)) {
1164 name
= (char *) xmlNodeGetContent(node
);
1166 ret
= -LTTNG_ERR_NOMEM
;
1167 goto error_snapshot_output
;
1169 } else if (!strcmp((const char *) node
->name
,
1170 config_element_max_size
)) {
1171 xmlChar
*content
= xmlNodeGetContent(node
);
1175 ret
= -LTTNG_ERR_NOMEM
;
1176 goto error_snapshot_output
;
1178 ret
= parse_uint(content
, &max_size
);
1181 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1182 goto error_snapshot_output
;
1185 /* consumer_output */
1186 ret
= process_consumer_output(node
, &output
);
1188 goto error_snapshot_output
;
1193 snapshot_output
= lttng_snapshot_output_create();
1194 if (!snapshot_output
) {
1195 ret
= -LTTNG_ERR_NOMEM
;
1196 goto error_snapshot_output
;
1199 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1201 goto error_snapshot_output
;
1204 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1206 goto error_snapshot_output
;
1210 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1213 goto error_snapshot_output
;
1216 if (output
.control_uri
) {
1217 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1220 goto error_snapshot_output
;
1224 if (output
.data_uri
) {
1225 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1228 goto error_snapshot_output
;
1233 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1234 error_snapshot_output
:
1237 free(output
.control_uri
);
1238 free(output
.data_uri
);
1239 lttng_snapshot_output_destroy(snapshot_output
);
1249 int create_session(const char *name
,
1250 struct lttng_domain
*kernel_domain
,
1251 struct lttng_domain
*ust_domain
,
1252 struct lttng_domain
*jul_domain
,
1253 struct lttng_domain
*log4j_domain
,
1254 xmlNodePtr output_node
,
1255 uint64_t live_timer_interval
)
1258 struct consumer_output output
= { 0 };
1259 xmlNodePtr consumer_output_node
;
1264 consumer_output_node
= xmlFirstElementChild(output_node
);
1265 if (!consumer_output_node
) {
1266 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1270 if (strcmp((const char *) consumer_output_node
->name
,
1271 config_element_consumer_output
)) {
1272 WARN("Invalid output type, expected %s node",
1273 config_element_consumer_output
);
1274 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1278 ret
= process_consumer_output(consumer_output_node
, &output
);
1284 if (live_timer_interval
!= UINT64_MAX
&&
1285 !output
.control_uri
&& !output
.data_uri
) {
1286 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1290 if (output
.control_uri
|| output
.data_uri
) {
1292 struct lttng_domain
*domain
;
1293 struct lttng_domain
*domains
[] =
1294 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1296 /* network destination */
1297 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1299 * URLs are provided for sure since the test above make sure that
1300 * with a live timer the data and control URIs are provided. So,
1301 * NULL is passed here and will be set right after.
1303 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1305 ret
= lttng_create_session(name
, NULL
);
1311 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1312 domain
= domains
[i
];
1317 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1324 /* either local output or no output */
1325 ret
= lttng_create_session(name
, output
.path
);
1332 free(output
.control_uri
);
1333 free(output
.data_uri
);
1337 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1338 struct lttng_event_probe_attr
*attr
)
1342 assert(probe_attribute_node
);
1345 if (!strcmp((const char *) probe_attribute_node
->name
,
1346 config_element_address
)) {
1351 content
= xmlNodeGetContent(probe_attribute_node
);
1353 ret
= -LTTNG_ERR_NOMEM
;
1357 ret
= parse_uint(content
, &addr
);
1360 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1365 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1366 config_element_offset
)) {
1368 uint64_t offset
= 0;
1371 content
= xmlNodeGetContent(probe_attribute_node
);
1373 ret
= -LTTNG_ERR_NOMEM
;
1377 ret
= parse_uint(content
, &offset
);
1380 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1384 attr
->offset
= offset
;
1385 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1386 config_element_symbol_name
)) {
1391 content
= xmlNodeGetContent(probe_attribute_node
);
1393 ret
= -LTTNG_ERR_NOMEM
;
1397 name_len
= strlen((char *) content
);
1398 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1399 WARN("symbol_name too long.");
1400 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1405 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1414 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1415 const char *channel_name
)
1419 struct lttng_event event
;
1420 char **exclusions
= NULL
;
1421 unsigned long exclusion_count
= 0;
1422 char *filter_expression
= NULL
;
1426 assert(channel_name
);
1428 memset(&event
, 0, sizeof(event
));
1430 /* Initialize default log level which varies by domain */
1431 switch (handle
->domain
.type
)
1433 case LTTNG_DOMAIN_JUL
:
1434 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1436 case LTTNG_DOMAIN_LOG4J
:
1437 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1439 case LTTNG_DOMAIN_PYTHON
:
1440 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1442 case LTTNG_DOMAIN_UST
:
1443 case LTTNG_DOMAIN_KERNEL
:
1444 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1450 for (node
= xmlFirstElementChild(event_node
); node
;
1451 node
= xmlNextElementSibling(node
)) {
1452 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1457 content
= xmlNodeGetContent(node
);
1459 ret
= -LTTNG_ERR_NOMEM
;
1463 name_len
= strlen((char *) content
);
1464 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1465 WARN("Channel name too long.");
1466 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1471 strncpy(event
.name
, (const char *) content
, name_len
);
1473 } else if (!strcmp((const char *) node
->name
,
1474 config_element_enabled
)) {
1475 xmlChar
*content
= xmlNodeGetContent(node
);
1479 ret
= -LTTNG_ERR_NOMEM
;
1483 ret
= parse_bool(content
, &event
.enabled
);
1486 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1489 } else if (!strcmp((const char *) node
->name
,
1490 config_element_type
)) {
1491 xmlChar
*content
= xmlNodeGetContent(node
);
1495 ret
= -LTTNG_ERR_NOMEM
;
1499 ret
= get_event_type(content
);
1502 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1507 } else if (!strcmp((const char *) node
->name
,
1508 config_element_loglevel_type
)) {
1509 xmlChar
*content
= xmlNodeGetContent(node
);
1513 ret
= -LTTNG_ERR_NOMEM
;
1517 ret
= get_loglevel_type(content
);
1520 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1524 event
.loglevel_type
= ret
;
1525 } else if (!strcmp((const char *) node
->name
,
1526 config_element_loglevel
)) {
1528 int64_t loglevel
= 0;
1531 content
= xmlNodeGetContent(node
);
1533 ret
= -LTTNG_ERR_NOMEM
;
1537 ret
= parse_int(content
, &loglevel
);
1540 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1544 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1545 WARN("loglevel out of range.");
1546 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1550 event
.loglevel
= loglevel
;
1551 } else if (!strcmp((const char *) node
->name
,
1552 config_element_filter
)) {
1554 xmlNodeGetContent(node
);
1558 ret
= -LTTNG_ERR_NOMEM
;
1562 filter_expression
= strdup((char *) content
);
1564 if (!filter_expression
) {
1565 ret
= -LTTNG_ERR_NOMEM
;
1568 } else if (!strcmp((const char *) node
->name
,
1569 config_element_exclusions
)) {
1570 xmlNodePtr exclusion_node
;
1571 int exclusion_index
= 0;
1576 * Exclusions has already been initialized,
1579 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1583 exclusion_count
= xmlChildElementCount(node
);
1584 if (!exclusion_count
) {
1588 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1590 exclusion_count
= 0;
1591 ret
= -LTTNG_ERR_NOMEM
;
1595 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1596 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1598 xmlNodeGetContent(exclusion_node
);
1601 ret
= -LTTNG_ERR_NOMEM
;
1605 exclusions
[exclusion_index
] = strdup((const char *) content
);
1607 if (!exclusions
[exclusion_index
]) {
1608 ret
= -LTTNG_ERR_NOMEM
;
1614 event
.exclusion
= 1;
1615 } else if (!strcmp((const char *) node
->name
,
1616 config_element_attributes
)) {
1617 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1620 if (!attribute_node
) {
1621 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1625 if (!strcmp((const char *) node
->name
,
1626 config_element_probe_attributes
)) {
1627 xmlNodePtr probe_attribute_node
;
1629 /* probe_attributes */
1630 for (probe_attribute_node
=
1631 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1632 probe_attribute_node
= xmlNextElementSibling(
1633 probe_attribute_node
)) {
1635 ret
= process_probe_attribute_node(probe_attribute_node
,
1644 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1646 /* function_attributes */
1647 content
= xmlNodeGetContent(symbol_node
);
1649 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1653 sym_len
= strlen((char *) content
);
1654 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1655 WARN("Function name too long.");
1656 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1661 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1668 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1669 filter_expression
, exclusion_count
, exclusions
);
1674 if (!event
.enabled
) {
1676 * Note that we should use lttng_disable_event_ext() (2.6+) to
1677 * eliminate the risk of clashing on events of the same
1678 * name (with different event types and loglevels).
1680 * Unfortunately, lttng_disable_event_ext() only performs a
1681 * match on the name and event type and errors out if any other
1682 * event attribute is not set to its default value.
1684 * This will disable all events that match this name.
1686 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1689 for (i
= 0; i
< exclusion_count
; i
++) {
1690 free(exclusions
[i
]);
1694 free(filter_expression
);
1699 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1700 const char *channel_name
)
1705 assert(events_node
);
1707 assert(channel_name
);
1709 for (node
= xmlFirstElementChild(events_node
); node
;
1710 node
= xmlNextElementSibling(node
)) {
1711 ret
= process_event_node(node
, handle
, channel_name
);
1721 int process_channel_attr_node(xmlNodePtr attr_node
,
1722 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1723 xmlNodePtr
*events_node
)
1729 assert(contexts_node
);
1730 assert(events_node
);
1732 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1737 content
= xmlNodeGetContent(attr_node
);
1739 ret
= -LTTNG_ERR_NOMEM
;
1743 name_len
= strlen((char *) content
);
1744 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1745 WARN("Channel name too long.");
1746 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1751 strncpy(channel
->name
, (const char *) content
, name_len
);
1753 } else if (!strcmp((const char *) attr_node
->name
,
1754 config_element_enabled
)) {
1759 content
= xmlNodeGetContent(attr_node
);
1761 ret
= -LTTNG_ERR_NOMEM
;
1765 ret
= parse_bool(content
, &enabled
);
1768 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1772 channel
->enabled
= enabled
;
1773 } else if (!strcmp((const char *) attr_node
->name
,
1774 config_element_overwrite_mode
)) {
1777 /* overwrite_mode */
1778 content
= xmlNodeGetContent(attr_node
);
1780 ret
= -LTTNG_ERR_NOMEM
;
1784 ret
= get_overwrite_mode(content
);
1787 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1791 channel
->attr
.overwrite
= ret
;
1792 } else if (!strcmp((const char *) attr_node
->name
,
1793 config_element_subbuf_size
)) {
1796 /* subbuffer_size */
1797 content
= xmlNodeGetContent(attr_node
);
1799 ret
= -LTTNG_ERR_NOMEM
;
1803 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1806 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1809 } else if (!strcmp((const char *) attr_node
->name
,
1810 config_element_num_subbuf
)) {
1813 /* subbuffer_count */
1814 content
= xmlNodeGetContent(attr_node
);
1816 ret
= -LTTNG_ERR_NOMEM
;
1820 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1823 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1826 } else if (!strcmp((const char *) attr_node
->name
,
1827 config_element_switch_timer_interval
)) {
1829 uint64_t switch_timer_interval
= 0;
1831 /* switch_timer_interval */
1832 content
= xmlNodeGetContent(attr_node
);
1834 ret
= -LTTNG_ERR_NOMEM
;
1838 ret
= parse_uint(content
, &switch_timer_interval
);
1841 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1845 if (switch_timer_interval
> UINT_MAX
) {
1846 WARN("switch_timer_interval out of range.");
1847 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1851 channel
->attr
.switch_timer_interval
=
1852 switch_timer_interval
;
1853 } else if (!strcmp((const char *) attr_node
->name
,
1854 config_element_read_timer_interval
)) {
1856 uint64_t read_timer_interval
= 0;
1858 /* read_timer_interval */
1859 content
= xmlNodeGetContent(attr_node
);
1861 ret
= -LTTNG_ERR_NOMEM
;
1865 ret
= parse_uint(content
, &read_timer_interval
);
1868 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1872 if (read_timer_interval
> UINT_MAX
) {
1873 WARN("read_timer_interval out of range.");
1874 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1878 channel
->attr
.read_timer_interval
=
1879 read_timer_interval
;
1880 } else if (!strcmp((const char *) attr_node
->name
,
1881 config_element_output_type
)) {
1885 content
= xmlNodeGetContent(attr_node
);
1887 ret
= -LTTNG_ERR_NOMEM
;
1891 ret
= get_output_type(content
);
1894 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1898 channel
->attr
.output
= ret
;
1899 } else if (!strcmp((const char *) attr_node
->name
,
1900 config_element_tracefile_size
)) {
1903 /* tracefile_size */
1904 content
= xmlNodeGetContent(attr_node
);
1906 ret
= -LTTNG_ERR_NOMEM
;
1910 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1913 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1916 } else if (!strcmp((const char *) attr_node
->name
,
1917 config_element_tracefile_count
)) {
1920 /* tracefile_count */
1921 content
= xmlNodeGetContent(attr_node
);
1923 ret
= -LTTNG_ERR_NOMEM
;
1927 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1930 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1933 } else if (!strcmp((const char *) attr_node
->name
,
1934 config_element_live_timer_interval
)) {
1936 uint64_t live_timer_interval
= 0;
1938 /* live_timer_interval */
1939 content
= xmlNodeGetContent(attr_node
);
1941 ret
= -LTTNG_ERR_NOMEM
;
1945 ret
= parse_uint(content
, &live_timer_interval
);
1948 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1952 if (live_timer_interval
> UINT_MAX
) {
1953 WARN("live_timer_interval out of range.");
1954 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1958 channel
->attr
.live_timer_interval
=
1959 live_timer_interval
;
1960 } else if (!strcmp((const char *) attr_node
->name
,
1961 config_element_events
)) {
1963 *events_node
= attr_node
;
1966 *contexts_node
= attr_node
;
1974 int process_context_node(xmlNodePtr context_node
,
1975 struct lttng_handle
*handle
, const char *channel_name
)
1978 struct lttng_event_context context
;
1979 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1982 assert(channel_name
);
1984 if (!context_child_node
) {
1985 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1989 memset(&context
, 0, sizeof(context
));
1991 if (!strcmp((const char *) context_child_node
->name
,
1992 config_element_type
)) {
1994 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1996 ret
= -LTTNG_ERR_NOMEM
;
2000 ret
= get_context_type(content
);
2003 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2009 xmlNodePtr perf_attr_node
;
2012 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2013 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2014 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2015 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2016 perf_attr_node
; perf_attr_node
=
2017 xmlNextElementSibling(perf_attr_node
)) {
2018 if (!strcmp((const char *) perf_attr_node
->name
,
2019 config_element_type
)) {
2024 content
= xmlNodeGetContent(perf_attr_node
);
2026 ret
= -LTTNG_ERR_NOMEM
;
2030 ret
= parse_uint(content
, &type
);
2033 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2037 if (type
> UINT32_MAX
) {
2038 WARN("perf context type out of range.");
2039 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2043 context
.u
.perf_counter
.type
= type
;
2044 } else if (!strcmp((const char *) perf_attr_node
->name
,
2045 config_element_config
)) {
2047 uint64_t config
= 0;
2050 content
= xmlNodeGetContent(perf_attr_node
);
2052 ret
= -LTTNG_ERR_NOMEM
;
2056 ret
= parse_uint(content
, &config
);
2059 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2063 context
.u
.perf_counter
.config
= config
;
2064 } else if (!strcmp((const char *) perf_attr_node
->name
,
2065 config_element_name
)) {
2070 content
= xmlNodeGetContent(perf_attr_node
);
2072 ret
= -LTTNG_ERR_NOMEM
;
2076 name_len
= strlen((char *) content
);
2077 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2078 WARN("perf context name too long.");
2079 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2084 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2091 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2097 int process_contexts_node(xmlNodePtr contexts_node
,
2098 struct lttng_handle
*handle
, const char *channel_name
)
2101 xmlNodePtr context_node
;
2103 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2104 context_node
= xmlNextElementSibling(context_node
)) {
2105 ret
= process_context_node(context_node
, handle
, channel_name
);
2115 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2116 struct lttng_handle
*handle
)
2119 xmlNodePtr targets_node
= NULL
;
2123 assert(pid_tracker_node
);
2124 /* get the targets node */
2125 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2126 node
= xmlNextElementSibling(node
)) {
2127 if (!strcmp((const char *) node
->name
,
2128 config_element_targets
)) {
2129 targets_node
= node
;
2134 if (!targets_node
) {
2135 ret
= LTTNG_ERR_INVALID
;
2139 /* Go through all pid_target node */
2140 child
= xmlChildElementCount(targets_node
);
2142 /* The session is explicitly set to target nothing. */
2143 ret
= lttng_untrack_pid(handle
, -1);
2148 for (node
= xmlFirstElementChild(targets_node
); node
;
2149 node
= xmlNextElementSibling(node
)) {
2150 xmlNodePtr pid_target_node
= node
;
2152 /* get pid node and track it */
2153 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2154 node
= xmlNextElementSibling(node
)) {
2155 if (!strcmp((const char *) node
->name
,
2156 config_element_pid
)) {
2158 xmlChar
*content
= NULL
;
2160 content
= xmlNodeGetContent(node
);
2162 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2166 ret
= parse_int(content
, &pid
);
2169 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2173 ret
= lttng_track_pid(handle
, (int) pid
);
2179 node
= pid_target_node
;
2188 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2191 struct lttng_domain domain
= { 0 };
2192 struct lttng_handle
*handle
= NULL
;
2193 xmlNodePtr channels_node
= NULL
;
2194 xmlNodePtr trackers_node
= NULL
;
2195 xmlNodePtr pid_tracker_node
= NULL
;
2198 assert(session_name
);
2200 ret
= init_domain(domain_node
, &domain
);
2205 handle
= lttng_create_handle(session_name
, &domain
);
2207 ret
= -LTTNG_ERR_NOMEM
;
2211 /* get the channels node */
2212 for (node
= xmlFirstElementChild(domain_node
); node
;
2213 node
= xmlNextElementSibling(node
)) {
2214 if (!strcmp((const char *) node
->name
,
2215 config_element_channels
)) {
2216 channels_node
= node
;
2221 if (!channels_node
) {
2225 /* create all channels */
2226 for (node
= xmlFirstElementChild(channels_node
); node
;
2227 node
= xmlNextElementSibling(node
)) {
2228 struct lttng_channel channel
;
2229 xmlNodePtr contexts_node
= NULL
;
2230 xmlNodePtr events_node
= NULL
;
2231 xmlNodePtr channel_attr_node
;
2233 memset(&channel
, 0, sizeof(channel
));
2234 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2236 for (channel_attr_node
= xmlFirstElementChild(node
);
2237 channel_attr_node
; channel_attr_node
=
2238 xmlNextElementSibling(channel_attr_node
)) {
2239 ret
= process_channel_attr_node(channel_attr_node
,
2240 &channel
, &contexts_node
, &events_node
);
2246 ret
= lttng_enable_channel(handle
, &channel
);
2251 ret
= process_events_node(events_node
, handle
, channel
.name
);
2256 ret
= process_contexts_node(contexts_node
, handle
,
2263 /* get the trackers node */
2264 for (node
= xmlFirstElementChild(domain_node
); node
;
2265 node
= xmlNextElementSibling(node
)) {
2266 if (!strcmp((const char *) node
->name
,
2267 config_element_trackers
)) {
2268 trackers_node
= node
;
2273 if (!trackers_node
) {
2277 for (node
= xmlFirstElementChild(trackers_node
); node
;
2278 node
= xmlNextElementSibling(node
)) {
2279 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2280 pid_tracker_node
= node
;
2281 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2288 if (!pid_tracker_node
) {
2289 lttng_track_pid(handle
, -1);
2293 lttng_destroy_handle(handle
);
2298 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2301 int ret
, started
= -1, snapshot_mode
= -1;
2302 uint64_t live_timer_interval
= UINT64_MAX
;
2303 xmlChar
*name
= NULL
;
2304 xmlChar
*shm_path
= NULL
;
2305 xmlNodePtr domains_node
= NULL
;
2306 xmlNodePtr output_node
= NULL
;
2308 struct lttng_domain
*kernel_domain
= NULL
;
2309 struct lttng_domain
*ust_domain
= NULL
;
2310 struct lttng_domain
*jul_domain
= NULL
;
2311 struct lttng_domain
*log4j_domain
= NULL
;
2312 struct lttng_domain
*python_domain
= NULL
;
2314 for (node
= xmlFirstElementChild(session_node
); node
;
2315 node
= xmlNextElementSibling(node
)) {
2316 if (!name
&& !strcmp((const char *) node
->name
,
2317 config_element_name
)) {
2319 xmlChar
*node_content
= xmlNodeGetContent(node
);
2320 if (!node_content
) {
2321 ret
= -LTTNG_ERR_NOMEM
;
2325 name
= node_content
;
2326 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2327 config_element_domains
)) {
2329 domains_node
= node
;
2330 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2331 config_element_started
)) {
2333 xmlChar
*node_content
= xmlNodeGetContent(node
);
2334 if (!node_content
) {
2335 ret
= -LTTNG_ERR_NOMEM
;
2339 ret
= parse_bool(node_content
, &started
);
2342 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2345 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2346 config_element_output
)) {
2349 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2350 config_element_shared_memory_path
)) {
2351 /* shared memory path */
2352 xmlChar
*node_content
= xmlNodeGetContent(node
);
2353 if (!node_content
) {
2354 ret
= -LTTNG_ERR_NOMEM
;
2358 shm_path
= node_content
;
2360 /* attributes, snapshot_mode or live_timer_interval */
2361 xmlNodePtr attributes_child
=
2362 xmlFirstElementChild(node
);
2364 if (!strcmp((const char *) attributes_child
->name
,
2365 config_element_snapshot_mode
)) {
2367 xmlChar
*snapshot_mode_content
=
2368 xmlNodeGetContent(attributes_child
);
2369 if (!snapshot_mode_content
) {
2370 ret
= -LTTNG_ERR_NOMEM
;
2374 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2375 free(snapshot_mode_content
);
2377 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2381 /* live_timer_interval */
2382 xmlChar
*timer_interval_content
=
2383 xmlNodeGetContent(attributes_child
);
2384 if (!timer_interval_content
) {
2385 ret
= -LTTNG_ERR_NOMEM
;
2389 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2390 free(timer_interval_content
);
2392 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2400 /* Mandatory attribute, as defined in the session XSD */
2401 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2405 if (session_name
&& strcmp((char *) name
, session_name
)) {
2406 /* This is not the session we are looking for */
2407 ret
= -LTTNG_ERR_NO_SESSION
;
2411 /* Init domains to create the session handles */
2412 for (node
= xmlFirstElementChild(domains_node
); node
;
2413 node
= xmlNextElementSibling(node
)) {
2414 struct lttng_domain
*domain
;
2416 domain
= zmalloc(sizeof(*domain
));
2418 ret
= -LTTNG_ERR_NOMEM
;
2422 ret
= init_domain(node
, domain
);
2424 goto domain_init_error
;
2427 switch (domain
->type
) {
2428 case LTTNG_DOMAIN_KERNEL
:
2429 if (kernel_domain
) {
2430 /* Same domain seen twice, invalid! */
2431 goto domain_init_error
;
2433 kernel_domain
= domain
;
2435 case LTTNG_DOMAIN_UST
:
2437 /* Same domain seen twice, invalid! */
2438 goto domain_init_error
;
2440 ust_domain
= domain
;
2442 case LTTNG_DOMAIN_JUL
:
2444 /* Same domain seen twice, invalid! */
2445 goto domain_init_error
;
2447 jul_domain
= domain
;
2449 case LTTNG_DOMAIN_LOG4J
:
2451 /* Same domain seen twice, invalid! */
2452 goto domain_init_error
;
2454 log4j_domain
= domain
;
2456 case LTTNG_DOMAIN_PYTHON
:
2457 if (python_domain
) {
2458 /* Same domain seen twice, invalid! */
2459 goto domain_init_error
;
2461 python_domain
= domain
;
2464 WARN("Invalid domain type");
2465 goto domain_init_error
;
2470 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2475 /* Destroy session if it exists */
2476 ret
= lttng_destroy_session((const char *) name
);
2477 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2478 ERR("Failed to destroy existing session.");
2483 /* Create session type depending on output type */
2484 if (snapshot_mode
&& snapshot_mode
!= -1) {
2485 ret
= create_snapshot_session((const char *) name
, output_node
);
2486 } else if (live_timer_interval
&&
2487 live_timer_interval
!= UINT64_MAX
) {
2488 ret
= create_session((const char *) name
, kernel_domain
,
2489 ust_domain
, jul_domain
, log4j_domain
,
2490 output_node
, live_timer_interval
);
2492 /* regular session */
2493 ret
= create_session((const char *) name
, kernel_domain
,
2494 ust_domain
, jul_domain
, log4j_domain
,
2495 output_node
, UINT64_MAX
);
2502 ret
= lttng_set_session_shm_path((const char *) name
,
2503 (const char *) shm_path
);
2509 for (node
= xmlFirstElementChild(domains_node
); node
;
2510 node
= xmlNextElementSibling(node
)) {
2511 ret
= process_domain_node(node
, (const char *) name
);
2518 ret
= lttng_start_tracing((const char *) name
);
2526 ERR("Failed to load session %s: %s", (const char *) name
,
2527 lttng_strerror(ret
));
2528 lttng_destroy_session((const char *) name
);
2532 free(kernel_domain
);
2536 free(python_domain
);
2543 * Return 1 if the given path is readable by the current UID or 0 if not.
2544 * Return -1 if the path is EPERM.
2546 static int validate_file_read_creds(const char *path
)
2552 /* Can we read the file. */
2553 ret
= access(path
, R_OK
);
2557 if (errno
== EACCES
) {
2568 int load_session_from_file(const char *path
, const char *session_name
,
2569 struct session_config_validation_ctx
*validation_ctx
, int override
)
2571 int ret
, session_found
= !session_name
;
2572 xmlDocPtr doc
= NULL
;
2573 xmlNodePtr sessions_node
;
2574 xmlNodePtr session_node
;
2577 assert(validation_ctx
);
2579 ret
= validate_file_read_creds(path
);
2582 ret
= -LTTNG_ERR_EPERM
;
2584 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2589 doc
= xmlParseFile(path
);
2591 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2595 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2597 ERR("Session configuration file validation failed");
2598 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2602 sessions_node
= xmlDocGetRootElement(doc
);
2603 if (!sessions_node
) {
2607 for (session_node
= xmlFirstElementChild(sessions_node
);
2608 session_node
; session_node
=
2609 xmlNextElementSibling(session_node
)) {
2610 ret
= process_session_node(session_node
,
2611 session_name
, override
);
2612 if (session_name
&& ret
== 0) {
2613 /* Target session found and loaded */
2621 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2626 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2628 struct dirent
*alloc_dirent(const char *path
)
2632 struct dirent
*entry
;
2634 name_max
= pathconf(path
, _PC_NAME_MAX
);
2635 if (name_max
== -1) {
2636 name_max
= PATH_MAX
;
2638 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2639 entry
= zmalloc(len
);
2644 int load_session_from_path(const char *path
, const char *session_name
,
2645 struct session_config_validation_ctx
*validation_ctx
, int override
)
2647 int ret
, session_found
= !session_name
;
2648 DIR *directory
= NULL
;
2651 assert(validation_ctx
);
2653 directory
= opendir(path
);
2657 /* Try the file loading. */
2660 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2663 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2668 struct dirent
*entry
;
2669 struct dirent
*result
;
2670 char *file_path
= NULL
;
2671 size_t path_len
= strlen(path
);
2673 if (path_len
>= PATH_MAX
) {
2674 ret
= -LTTNG_ERR_INVALID
;
2678 entry
= alloc_dirent(path
);
2680 ret
= -LTTNG_ERR_NOMEM
;
2684 file_path
= zmalloc(PATH_MAX
);
2686 ret
= -LTTNG_ERR_NOMEM
;
2691 strncpy(file_path
, path
, path_len
);
2692 if (file_path
[path_len
- 1] != '/') {
2693 file_path
[path_len
++] = '/';
2697 /* Search for *.lttng files */
2698 while (!readdir_r(directory
, entry
, &result
) && result
) {
2699 size_t file_name_len
= strlen(result
->d_name
);
2701 if (file_name_len
<=
2702 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2706 if (path_len
+ file_name_len
>= PATH_MAX
) {
2710 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2711 result
->d_name
+ file_name_len
- sizeof(
2712 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2716 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2717 file_path
[path_len
+ file_name_len
] = '\0';
2719 ret
= load_session_from_file(file_path
, session_name
,
2720 validation_ctx
, override
);
2721 if (session_name
&& !ret
) {
2730 ret
= load_session_from_file(path
, session_name
,
2731 validation_ctx
, override
);
2741 if (closedir(directory
)) {
2746 if (!session_found
) {
2747 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2754 * Validate that the given path's credentials and the current process have the
2755 * same UID. If so, return 1 else return 0 if it does NOT match.
2757 static int validate_path_creds(const char *path
)
2759 int ret
, uid
= getuid();
2768 ret
= stat(path
, &buf
);
2770 if (errno
!= ENOENT
) {
2773 ret
= -LTTNG_ERR_INVALID
;
2777 if (buf
.st_uid
!= uid
) {
2788 int config_load_session(const char *path
, const char *session_name
,
2789 int override
, unsigned int autoload
)
2792 const char *path_ptr
= NULL
;
2793 struct session_config_validation_ctx validation_ctx
= { 0 };
2795 ret
= init_session_config_validation_ctx(&validation_ctx
);
2802 const char *sys_path
;
2805 home_path
= utils_get_home_dir();
2807 char path
[PATH_MAX
];
2810 * Try user session configuration path. Ignore error here so we can
2811 * continue loading the system wide sessions.
2814 ret
= snprintf(path
, sizeof(path
),
2815 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2816 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2818 PERROR("snprintf session autoload home config path");
2823 * Credentials are only validated for the autoload in order to
2824 * avoid any user session daemon to try to load kernel sessions
2825 * automatically and failing all the times.
2827 ret
= validate_path_creds(path
);
2832 ret
= snprintf(path
, sizeof(path
),
2833 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2835 PERROR("snprintf session home config path");
2841 ret
= load_session_from_path(path_ptr
, session_name
,
2842 &validation_ctx
, override
);
2843 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2847 * Continue even if the session was found since we have to try
2848 * the system wide sessions.
2853 /* Reset path pointer for the system wide dir. */
2856 /* Try system wide configuration directory. */
2858 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2859 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2860 ret
= validate_path_creds(sys_path
);
2862 path_ptr
= sys_path
;
2865 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2866 path_ptr
= sys_path
;
2870 ret
= load_session_from_path(path_ptr
, session_name
,
2871 &validation_ctx
, override
);
2874 ret
= access(path
, F_OK
);
2879 ret
= -LTTNG_ERR_INVALID
;
2880 WARN("Session configuration path does not exist.");
2883 ret
= -LTTNG_ERR_EPERM
;
2886 ret
= -LTTNG_ERR_UNK
;
2892 ret
= load_session_from_path(path
, session_name
,
2893 &validation_ctx
, override
);
2896 fini_session_config_validation_ctx(&validation_ctx
);
2897 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2899 * Don't report an error if no sessions are found when called
2900 * without a session_name or a search path.