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 for (node
= xmlFirstElementChild(event_node
); node
;
1431 node
= xmlNextElementSibling(node
)) {
1432 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1437 content
= xmlNodeGetContent(node
);
1439 ret
= -LTTNG_ERR_NOMEM
;
1443 name_len
= strlen((char *) content
);
1444 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1445 WARN("Channel name too long.");
1446 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1451 strncpy(event
.name
, (const char *) content
, name_len
);
1453 } else if (!strcmp((const char *) node
->name
,
1454 config_element_enabled
)) {
1455 xmlChar
*content
= xmlNodeGetContent(node
);
1459 ret
= -LTTNG_ERR_NOMEM
;
1463 ret
= parse_bool(content
, &event
.enabled
);
1466 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1469 } else if (!strcmp((const char *) node
->name
,
1470 config_element_type
)) {
1471 xmlChar
*content
= xmlNodeGetContent(node
);
1475 ret
= -LTTNG_ERR_NOMEM
;
1479 ret
= get_event_type(content
);
1482 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1487 } else if (!strcmp((const char *) node
->name
,
1488 config_element_loglevel_type
)) {
1489 xmlChar
*content
= xmlNodeGetContent(node
);
1493 ret
= -LTTNG_ERR_NOMEM
;
1497 ret
= get_loglevel_type(content
);
1500 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1504 event
.loglevel_type
= ret
;
1505 } else if (!strcmp((const char *) node
->name
,
1506 config_element_loglevel
)) {
1508 int64_t loglevel
= 0;
1511 content
= xmlNodeGetContent(node
);
1513 ret
= -LTTNG_ERR_NOMEM
;
1517 ret
= parse_int(content
, &loglevel
);
1520 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1524 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1525 WARN("loglevel out of range.");
1526 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1530 event
.loglevel
= loglevel
;
1531 } else if (!strcmp((const char *) node
->name
,
1532 config_element_filter
)) {
1534 xmlNodeGetContent(node
);
1538 ret
= -LTTNG_ERR_NOMEM
;
1542 filter_expression
= strdup((char *) content
);
1544 if (!filter_expression
) {
1545 ret
= -LTTNG_ERR_NOMEM
;
1548 } else if (!strcmp((const char *) node
->name
,
1549 config_element_exclusions
)) {
1550 xmlNodePtr exclusion_node
;
1551 int exclusion_index
= 0;
1556 * Exclusions has already been initialized,
1559 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1563 exclusion_count
= xmlChildElementCount(node
);
1564 if (!exclusion_count
) {
1568 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1570 exclusion_count
= 0;
1571 ret
= -LTTNG_ERR_NOMEM
;
1575 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1576 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1578 xmlNodeGetContent(exclusion_node
);
1581 ret
= -LTTNG_ERR_NOMEM
;
1585 exclusions
[exclusion_index
] = strdup((const char *) content
);
1587 if (!exclusions
[exclusion_index
]) {
1588 ret
= -LTTNG_ERR_NOMEM
;
1594 event
.exclusion
= 1;
1595 } else if (!strcmp((const char *) node
->name
,
1596 config_element_attributes
)) {
1597 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1600 if (!attribute_node
) {
1601 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1605 if (!strcmp((const char *) node
->name
,
1606 config_element_probe_attributes
)) {
1607 xmlNodePtr probe_attribute_node
;
1609 /* probe_attributes */
1610 for (probe_attribute_node
=
1611 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1612 probe_attribute_node
= xmlNextElementSibling(
1613 probe_attribute_node
)) {
1615 ret
= process_probe_attribute_node(probe_attribute_node
,
1624 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1626 /* function_attributes */
1627 content
= xmlNodeGetContent(symbol_node
);
1629 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1633 sym_len
= strlen((char *) content
);
1634 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1635 WARN("Function name too long.");
1636 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1641 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1648 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1649 filter_expression
, exclusion_count
, exclusions
);
1654 if (!event
.enabled
) {
1656 * Note that we should use lttng_disable_event_ext() (2.6+) to
1657 * eliminate the risk of clashing on events of the same
1658 * name (with different event types and loglevels).
1660 * Unfortunately, lttng_disable_event_ext() only performs a
1661 * match on the name and event type and errors out if any other
1662 * event attribute is not set to its default value.
1664 * This will disable all events that match this name.
1666 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1669 for (i
= 0; i
< exclusion_count
; i
++) {
1670 free(exclusions
[i
]);
1674 free(filter_expression
);
1679 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1680 const char *channel_name
)
1685 assert(events_node
);
1687 assert(channel_name
);
1689 for (node
= xmlFirstElementChild(events_node
); node
;
1690 node
= xmlNextElementSibling(node
)) {
1691 ret
= process_event_node(node
, handle
, channel_name
);
1701 int process_channel_attr_node(xmlNodePtr attr_node
,
1702 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1703 xmlNodePtr
*events_node
)
1709 assert(contexts_node
);
1710 assert(events_node
);
1712 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1717 content
= xmlNodeGetContent(attr_node
);
1719 ret
= -LTTNG_ERR_NOMEM
;
1723 name_len
= strlen((char *) content
);
1724 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1725 WARN("Channel name too long.");
1726 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1731 strncpy(channel
->name
, (const char *) content
, name_len
);
1733 } else if (!strcmp((const char *) attr_node
->name
,
1734 config_element_enabled
)) {
1739 content
= xmlNodeGetContent(attr_node
);
1741 ret
= -LTTNG_ERR_NOMEM
;
1745 ret
= parse_bool(content
, &enabled
);
1748 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1752 channel
->enabled
= enabled
;
1753 } else if (!strcmp((const char *) attr_node
->name
,
1754 config_element_overwrite_mode
)) {
1757 /* overwrite_mode */
1758 content
= xmlNodeGetContent(attr_node
);
1760 ret
= -LTTNG_ERR_NOMEM
;
1764 ret
= get_overwrite_mode(content
);
1767 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1771 channel
->attr
.overwrite
= ret
;
1772 } else if (!strcmp((const char *) attr_node
->name
,
1773 config_element_subbuf_size
)) {
1776 /* subbuffer_size */
1777 content
= xmlNodeGetContent(attr_node
);
1779 ret
= -LTTNG_ERR_NOMEM
;
1783 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1786 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1789 } else if (!strcmp((const char *) attr_node
->name
,
1790 config_element_num_subbuf
)) {
1793 /* subbuffer_count */
1794 content
= xmlNodeGetContent(attr_node
);
1796 ret
= -LTTNG_ERR_NOMEM
;
1800 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1803 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1806 } else if (!strcmp((const char *) attr_node
->name
,
1807 config_element_switch_timer_interval
)) {
1809 uint64_t switch_timer_interval
= 0;
1811 /* switch_timer_interval */
1812 content
= xmlNodeGetContent(attr_node
);
1814 ret
= -LTTNG_ERR_NOMEM
;
1818 ret
= parse_uint(content
, &switch_timer_interval
);
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 if (switch_timer_interval
> UINT_MAX
) {
1826 WARN("switch_timer_interval out of range.");
1827 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1831 channel
->attr
.switch_timer_interval
=
1832 switch_timer_interval
;
1833 } else if (!strcmp((const char *) attr_node
->name
,
1834 config_element_read_timer_interval
)) {
1836 uint64_t read_timer_interval
= 0;
1838 /* read_timer_interval */
1839 content
= xmlNodeGetContent(attr_node
);
1841 ret
= -LTTNG_ERR_NOMEM
;
1845 ret
= parse_uint(content
, &read_timer_interval
);
1848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 if (read_timer_interval
> UINT_MAX
) {
1853 WARN("read_timer_interval out of range.");
1854 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1858 channel
->attr
.read_timer_interval
=
1859 read_timer_interval
;
1860 } else if (!strcmp((const char *) attr_node
->name
,
1861 config_element_output_type
)) {
1865 content
= xmlNodeGetContent(attr_node
);
1867 ret
= -LTTNG_ERR_NOMEM
;
1871 ret
= get_output_type(content
);
1874 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1878 channel
->attr
.output
= ret
;
1879 } else if (!strcmp((const char *) attr_node
->name
,
1880 config_element_tracefile_size
)) {
1883 /* tracefile_size */
1884 content
= xmlNodeGetContent(attr_node
);
1886 ret
= -LTTNG_ERR_NOMEM
;
1890 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1893 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1896 } else if (!strcmp((const char *) attr_node
->name
,
1897 config_element_tracefile_count
)) {
1900 /* tracefile_count */
1901 content
= xmlNodeGetContent(attr_node
);
1903 ret
= -LTTNG_ERR_NOMEM
;
1907 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1910 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1913 } else if (!strcmp((const char *) attr_node
->name
,
1914 config_element_live_timer_interval
)) {
1916 uint64_t live_timer_interval
= 0;
1918 /* live_timer_interval */
1919 content
= xmlNodeGetContent(attr_node
);
1921 ret
= -LTTNG_ERR_NOMEM
;
1925 ret
= parse_uint(content
, &live_timer_interval
);
1928 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1932 if (live_timer_interval
> UINT_MAX
) {
1933 WARN("live_timer_interval out of range.");
1934 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1938 channel
->attr
.live_timer_interval
=
1939 live_timer_interval
;
1940 } else if (!strcmp((const char *) attr_node
->name
,
1941 config_element_events
)) {
1943 *events_node
= attr_node
;
1946 *contexts_node
= attr_node
;
1954 int process_context_node(xmlNodePtr context_node
,
1955 struct lttng_handle
*handle
, const char *channel_name
)
1958 struct lttng_event_context context
;
1959 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1962 assert(channel_name
);
1964 if (!context_child_node
) {
1965 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1969 memset(&context
, 0, sizeof(context
));
1971 if (!strcmp((const char *) context_child_node
->name
,
1972 config_element_type
)) {
1974 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1976 ret
= -LTTNG_ERR_NOMEM
;
1980 ret
= get_context_type(content
);
1983 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1989 xmlNodePtr perf_attr_node
;
1992 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1993 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1994 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1995 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1996 perf_attr_node
; perf_attr_node
=
1997 xmlNextElementSibling(perf_attr_node
)) {
1998 if (!strcmp((const char *) perf_attr_node
->name
,
1999 config_element_type
)) {
2004 content
= xmlNodeGetContent(perf_attr_node
);
2006 ret
= -LTTNG_ERR_NOMEM
;
2010 ret
= parse_uint(content
, &type
);
2013 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2017 if (type
> UINT32_MAX
) {
2018 WARN("perf context type out of range.");
2019 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2023 context
.u
.perf_counter
.type
= type
;
2024 } else if (!strcmp((const char *) perf_attr_node
->name
,
2025 config_element_config
)) {
2027 uint64_t config
= 0;
2030 content
= xmlNodeGetContent(perf_attr_node
);
2032 ret
= -LTTNG_ERR_NOMEM
;
2036 ret
= parse_uint(content
, &config
);
2039 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2043 context
.u
.perf_counter
.config
= config
;
2044 } else if (!strcmp((const char *) perf_attr_node
->name
,
2045 config_element_name
)) {
2050 content
= xmlNodeGetContent(perf_attr_node
);
2052 ret
= -LTTNG_ERR_NOMEM
;
2056 name_len
= strlen((char *) content
);
2057 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2058 WARN("perf context name too long.");
2059 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2064 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2071 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2077 int process_contexts_node(xmlNodePtr contexts_node
,
2078 struct lttng_handle
*handle
, const char *channel_name
)
2081 xmlNodePtr context_node
;
2083 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2084 context_node
= xmlNextElementSibling(context_node
)) {
2085 ret
= process_context_node(context_node
, handle
, channel_name
);
2095 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2096 struct lttng_handle
*handle
)
2099 xmlNodePtr targets_node
= NULL
;
2103 assert(pid_tracker_node
);
2104 /* get the targets node */
2105 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2106 node
= xmlNextElementSibling(node
)) {
2107 if (!strcmp((const char *) node
->name
,
2108 config_element_targets
)) {
2109 targets_node
= node
;
2114 if (!targets_node
) {
2115 ret
= LTTNG_ERR_INVALID
;
2119 /* Go through all pid_target node */
2120 child
= xmlChildElementCount(targets_node
);
2122 /* The session is explicitly set to target nothing. */
2123 ret
= lttng_untrack_pid(handle
, -1);
2128 for (node
= xmlFirstElementChild(targets_node
); node
;
2129 node
= xmlNextElementSibling(node
)) {
2130 xmlNodePtr pid_target_node
= node
;
2132 /* get pid node and track it */
2133 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2134 node
= xmlNextElementSibling(node
)) {
2135 if (!strcmp((const char *) node
->name
,
2136 config_element_pid
)) {
2138 xmlChar
*content
= NULL
;
2140 content
= xmlNodeGetContent(node
);
2142 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2146 ret
= parse_int(content
, &pid
);
2149 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2153 ret
= lttng_track_pid(handle
, (int) pid
);
2159 node
= pid_target_node
;
2168 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2171 struct lttng_domain domain
= { 0 };
2172 struct lttng_handle
*handle
= NULL
;
2173 xmlNodePtr channels_node
= NULL
;
2174 xmlNodePtr trackers_node
= NULL
;
2175 xmlNodePtr pid_tracker_node
= NULL
;
2178 assert(session_name
);
2180 ret
= init_domain(domain_node
, &domain
);
2185 handle
= lttng_create_handle(session_name
, &domain
);
2187 ret
= -LTTNG_ERR_NOMEM
;
2191 /* get the channels node */
2192 for (node
= xmlFirstElementChild(domain_node
); node
;
2193 node
= xmlNextElementSibling(node
)) {
2194 if (!strcmp((const char *) node
->name
,
2195 config_element_channels
)) {
2196 channels_node
= node
;
2201 if (!channels_node
) {
2205 /* create all channels */
2206 for (node
= xmlFirstElementChild(channels_node
); node
;
2207 node
= xmlNextElementSibling(node
)) {
2208 struct lttng_channel channel
;
2209 xmlNodePtr contexts_node
= NULL
;
2210 xmlNodePtr events_node
= NULL
;
2211 xmlNodePtr channel_attr_node
;
2213 memset(&channel
, 0, sizeof(channel
));
2214 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2216 for (channel_attr_node
= xmlFirstElementChild(node
);
2217 channel_attr_node
; channel_attr_node
=
2218 xmlNextElementSibling(channel_attr_node
)) {
2219 ret
= process_channel_attr_node(channel_attr_node
,
2220 &channel
, &contexts_node
, &events_node
);
2226 ret
= lttng_enable_channel(handle
, &channel
);
2231 ret
= process_events_node(events_node
, handle
, channel
.name
);
2236 ret
= process_contexts_node(contexts_node
, handle
,
2243 /* get the trackers node */
2244 for (node
= xmlFirstElementChild(domain_node
); node
;
2245 node
= xmlNextElementSibling(node
)) {
2246 if (!strcmp((const char *) node
->name
,
2247 config_element_trackers
)) {
2248 trackers_node
= node
;
2253 if (!trackers_node
) {
2257 for (node
= xmlFirstElementChild(trackers_node
); node
;
2258 node
= xmlNextElementSibling(node
)) {
2259 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2260 pid_tracker_node
= node
;
2261 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2268 if (!pid_tracker_node
) {
2269 lttng_track_pid(handle
, -1);
2273 lttng_destroy_handle(handle
);
2278 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2281 int ret
, started
= -1, snapshot_mode
= -1;
2282 uint64_t live_timer_interval
= UINT64_MAX
;
2283 xmlChar
*name
= NULL
;
2284 xmlChar
*shm_path
= NULL
;
2285 xmlNodePtr domains_node
= NULL
;
2286 xmlNodePtr output_node
= NULL
;
2288 struct lttng_domain
*kernel_domain
= NULL
;
2289 struct lttng_domain
*ust_domain
= NULL
;
2290 struct lttng_domain
*jul_domain
= NULL
;
2291 struct lttng_domain
*log4j_domain
= NULL
;
2292 struct lttng_domain
*python_domain
= NULL
;
2294 for (node
= xmlFirstElementChild(session_node
); node
;
2295 node
= xmlNextElementSibling(node
)) {
2296 if (!name
&& !strcmp((const char *) node
->name
,
2297 config_element_name
)) {
2299 xmlChar
*node_content
= xmlNodeGetContent(node
);
2300 if (!node_content
) {
2301 ret
= -LTTNG_ERR_NOMEM
;
2305 name
= node_content
;
2306 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2307 config_element_domains
)) {
2309 domains_node
= node
;
2310 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2311 config_element_started
)) {
2313 xmlChar
*node_content
= xmlNodeGetContent(node
);
2314 if (!node_content
) {
2315 ret
= -LTTNG_ERR_NOMEM
;
2319 ret
= parse_bool(node_content
, &started
);
2322 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2325 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2326 config_element_output
)) {
2329 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2330 config_element_shared_memory_path
)) {
2331 /* shared memory path */
2332 xmlChar
*node_content
= xmlNodeGetContent(node
);
2333 if (!node_content
) {
2334 ret
= -LTTNG_ERR_NOMEM
;
2338 shm_path
= node_content
;
2340 /* attributes, snapshot_mode or live_timer_interval */
2341 xmlNodePtr attributes_child
=
2342 xmlFirstElementChild(node
);
2344 if (!strcmp((const char *) attributes_child
->name
,
2345 config_element_snapshot_mode
)) {
2347 xmlChar
*snapshot_mode_content
=
2348 xmlNodeGetContent(attributes_child
);
2349 if (!snapshot_mode_content
) {
2350 ret
= -LTTNG_ERR_NOMEM
;
2354 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2355 free(snapshot_mode_content
);
2357 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2361 /* live_timer_interval */
2362 xmlChar
*timer_interval_content
=
2363 xmlNodeGetContent(attributes_child
);
2364 if (!timer_interval_content
) {
2365 ret
= -LTTNG_ERR_NOMEM
;
2369 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2370 free(timer_interval_content
);
2372 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2380 /* Mandatory attribute, as defined in the session XSD */
2381 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2385 if (session_name
&& strcmp((char *) name
, session_name
)) {
2386 /* This is not the session we are looking for */
2387 ret
= -LTTNG_ERR_NO_SESSION
;
2391 /* Init domains to create the session handles */
2392 for (node
= xmlFirstElementChild(domains_node
); node
;
2393 node
= xmlNextElementSibling(node
)) {
2394 struct lttng_domain
*domain
;
2396 domain
= zmalloc(sizeof(*domain
));
2398 ret
= -LTTNG_ERR_NOMEM
;
2402 ret
= init_domain(node
, domain
);
2404 goto domain_init_error
;
2407 switch (domain
->type
) {
2408 case LTTNG_DOMAIN_KERNEL
:
2409 if (kernel_domain
) {
2410 /* Same domain seen twice, invalid! */
2411 goto domain_init_error
;
2413 kernel_domain
= domain
;
2415 case LTTNG_DOMAIN_UST
:
2417 /* Same domain seen twice, invalid! */
2418 goto domain_init_error
;
2420 ust_domain
= domain
;
2422 case LTTNG_DOMAIN_JUL
:
2424 /* Same domain seen twice, invalid! */
2425 goto domain_init_error
;
2427 jul_domain
= domain
;
2429 case LTTNG_DOMAIN_LOG4J
:
2431 /* Same domain seen twice, invalid! */
2432 goto domain_init_error
;
2434 log4j_domain
= domain
;
2436 case LTTNG_DOMAIN_PYTHON
:
2437 if (python_domain
) {
2438 /* Same domain seen twice, invalid! */
2439 goto domain_init_error
;
2441 python_domain
= domain
;
2444 WARN("Invalid domain type");
2445 goto domain_init_error
;
2450 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2455 /* Destroy session if it exists */
2456 ret
= lttng_destroy_session((const char *) name
);
2457 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2458 ERR("Failed to destroy existing session.");
2463 /* Create session type depending on output type */
2464 if (snapshot_mode
&& snapshot_mode
!= -1) {
2465 ret
= create_snapshot_session((const char *) name
, output_node
);
2466 } else if (live_timer_interval
&&
2467 live_timer_interval
!= UINT64_MAX
) {
2468 ret
= create_session((const char *) name
, kernel_domain
,
2469 ust_domain
, jul_domain
, log4j_domain
,
2470 output_node
, live_timer_interval
);
2472 /* regular session */
2473 ret
= create_session((const char *) name
, kernel_domain
,
2474 ust_domain
, jul_domain
, log4j_domain
,
2475 output_node
, UINT64_MAX
);
2482 ret
= lttng_set_session_shm_path((const char *) name
,
2483 (const char *) shm_path
);
2489 for (node
= xmlFirstElementChild(domains_node
); node
;
2490 node
= xmlNextElementSibling(node
)) {
2491 ret
= process_domain_node(node
, (const char *) name
);
2498 ret
= lttng_start_tracing((const char *) name
);
2506 ERR("Failed to load session %s: %s", (const char *) name
,
2507 lttng_strerror(ret
));
2508 lttng_destroy_session((const char *) name
);
2512 free(kernel_domain
);
2516 free(python_domain
);
2523 * Return 1 if the given path is readable by the current UID or 0 if not.
2524 * Return -1 if the path is EPERM.
2526 static int validate_file_read_creds(const char *path
)
2532 /* Can we read the file. */
2533 ret
= access(path
, R_OK
);
2537 if (errno
== EACCES
) {
2548 int load_session_from_file(const char *path
, const char *session_name
,
2549 struct session_config_validation_ctx
*validation_ctx
, int override
)
2551 int ret
, session_found
= !session_name
;
2552 xmlDocPtr doc
= NULL
;
2553 xmlNodePtr sessions_node
;
2554 xmlNodePtr session_node
;
2557 assert(validation_ctx
);
2559 ret
= validate_file_read_creds(path
);
2562 ret
= -LTTNG_ERR_EPERM
;
2564 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2569 doc
= xmlParseFile(path
);
2571 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2575 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2577 ERR("Session configuration file validation failed");
2578 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2582 sessions_node
= xmlDocGetRootElement(doc
);
2583 if (!sessions_node
) {
2587 for (session_node
= xmlFirstElementChild(sessions_node
);
2588 session_node
; session_node
=
2589 xmlNextElementSibling(session_node
)) {
2590 ret
= process_session_node(session_node
,
2591 session_name
, override
);
2592 if (session_name
&& ret
== 0) {
2593 /* Target session found and loaded */
2601 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2606 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2608 struct dirent
*alloc_dirent(const char *path
)
2612 struct dirent
*entry
;
2614 name_max
= pathconf(path
, _PC_NAME_MAX
);
2615 if (name_max
== -1) {
2616 name_max
= PATH_MAX
;
2618 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2619 entry
= zmalloc(len
);
2624 int load_session_from_path(const char *path
, const char *session_name
,
2625 struct session_config_validation_ctx
*validation_ctx
, int override
)
2627 int ret
, session_found
= !session_name
;
2628 DIR *directory
= NULL
;
2631 assert(validation_ctx
);
2633 directory
= opendir(path
);
2637 /* Try the file loading. */
2640 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2643 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2648 struct dirent
*entry
;
2649 struct dirent
*result
;
2650 char *file_path
= NULL
;
2651 size_t path_len
= strlen(path
);
2653 if (path_len
>= PATH_MAX
) {
2654 ret
= -LTTNG_ERR_INVALID
;
2658 entry
= alloc_dirent(path
);
2660 ret
= -LTTNG_ERR_NOMEM
;
2664 file_path
= zmalloc(PATH_MAX
);
2666 ret
= -LTTNG_ERR_NOMEM
;
2671 strncpy(file_path
, path
, path_len
);
2672 if (file_path
[path_len
- 1] != '/') {
2673 file_path
[path_len
++] = '/';
2677 /* Search for *.lttng files */
2678 while (!readdir_r(directory
, entry
, &result
) && result
) {
2679 size_t file_name_len
= strlen(result
->d_name
);
2681 if (file_name_len
<=
2682 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2686 if (path_len
+ file_name_len
>= PATH_MAX
) {
2690 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2691 result
->d_name
+ file_name_len
- sizeof(
2692 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2696 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2697 file_path
[path_len
+ file_name_len
] = '\0';
2699 ret
= load_session_from_file(file_path
, session_name
,
2700 validation_ctx
, override
);
2701 if (session_name
&& !ret
) {
2710 ret
= load_session_from_file(path
, session_name
,
2711 validation_ctx
, override
);
2721 if (closedir(directory
)) {
2726 if (!session_found
) {
2727 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2734 * Validate that the given path's credentials and the current process have the
2735 * same UID. If so, return 1 else return 0 if it does NOT match.
2737 static int validate_path_creds(const char *path
)
2739 int ret
, uid
= getuid();
2748 ret
= stat(path
, &buf
);
2750 if (errno
!= ENOENT
) {
2753 ret
= -LTTNG_ERR_INVALID
;
2757 if (buf
.st_uid
!= uid
) {
2768 int config_load_session(const char *path
, const char *session_name
,
2769 int override
, unsigned int autoload
)
2772 const char *path_ptr
= NULL
;
2773 struct session_config_validation_ctx validation_ctx
= { 0 };
2775 ret
= init_session_config_validation_ctx(&validation_ctx
);
2782 const char *sys_path
;
2785 home_path
= utils_get_home_dir();
2787 char path
[PATH_MAX
];
2790 * Try user session configuration path. Ignore error here so we can
2791 * continue loading the system wide sessions.
2794 ret
= snprintf(path
, sizeof(path
),
2795 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2796 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2798 PERROR("snprintf session autoload home config path");
2803 * Credentials are only validated for the autoload in order to
2804 * avoid any user session daemon to try to load kernel sessions
2805 * automatically and failing all the times.
2807 ret
= validate_path_creds(path
);
2812 ret
= snprintf(path
, sizeof(path
),
2813 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2815 PERROR("snprintf session home config path");
2821 ret
= load_session_from_path(path_ptr
, session_name
,
2822 &validation_ctx
, override
);
2823 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2827 * Continue even if the session was found since we have to try
2828 * the system wide sessions.
2833 /* Reset path pointer for the system wide dir. */
2836 /* Try system wide configuration directory. */
2838 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2839 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2840 ret
= validate_path_creds(sys_path
);
2842 path_ptr
= sys_path
;
2845 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2846 path_ptr
= sys_path
;
2850 ret
= load_session_from_path(path_ptr
, session_name
,
2851 &validation_ctx
, override
);
2854 ret
= access(path
, F_OK
);
2859 ret
= -LTTNG_ERR_INVALID
;
2860 WARN("Session configuration path does not exist.");
2863 ret
= -LTTNG_ERR_EPERM
;
2866 ret
= -LTTNG_ERR_UNK
;
2872 ret
= load_session_from_path(path
, session_name
,
2873 &validation_ctx
, override
);
2876 fini_session_config_validation_ctx(&validation_ctx
);
2877 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2879 * Don't report an error if no sessions are found when called
2880 * without a session_name or a search path.