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>
32 #include <common/defaults.h>
33 #include <common/error.h>
34 #include <common/macros.h>
35 #include <common/utils.h>
36 #include <common/compat/getenv.h>
37 #include <lttng/lttng-error.h>
38 #include <libxml/parser.h>
39 #include <libxml/valid.h>
40 #include <libxml/xmlschemas.h>
41 #include <libxml/tree.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.h>
46 #include "config-internal.h"
48 struct handler_filter_args
{
50 config_entry_handler_cb handler
;
54 struct session_config_validation_ctx
{
55 xmlSchemaParserCtxtPtr parser_ctx
;
57 xmlSchemaValidCtxtPtr schema_validation_ctx
;
60 const char * const config_str_yes
= "yes";
61 const char * const config_str_true
= "true";
62 const char * const config_str_on
= "on";
63 const char * const config_str_no
= "no";
64 const char * const config_str_false
= "false";
65 const char * const config_str_off
= "off";
66 const char * const config_xml_encoding
= "UTF-8";
67 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
68 const char * const config_xml_indent_string
= "\t";
69 const char * const config_xml_true
= "true";
70 const char * const config_xml_false
= "false";
72 const char * const config_element_channel
= "channel";
73 const char * const config_element_channels
= "channels";
74 const char * const config_element_domain
= "domain";
75 const char * const config_element_domains
= "domains";
76 const char * const config_element_event
= "event";
77 const char * const config_element_events
= "events";
78 const char * const config_element_context
= "context";
79 const char * const config_element_contexts
= "contexts";
80 const char * const config_element_attributes
= "attributes";
81 const char * const config_element_exclusion
= "exclusion";
82 const char * const config_element_exclusions
= "exclusions";
83 const char * const config_element_function_attributes
= "function_attributes";
84 const char * const config_element_probe_attributes
= "probe_attributes";
85 const char * const config_element_symbol_name
= "symbol_name";
86 const char * const config_element_address
= "address";
87 const char * const config_element_offset
= "offset";
88 const char * const config_element_name
= "name";
89 const char * const config_element_enabled
= "enabled";
90 const char * const config_element_overwrite_mode
= "overwrite_mode";
91 const char * const config_element_subbuf_size
= "subbuffer_size";
92 const char * const config_element_num_subbuf
= "subbuffer_count";
93 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
94 const char * const config_element_read_timer_interval
= "read_timer_interval";
95 const char * const config_element_output
= "output";
96 const char * const config_element_output_type
= "output_type";
97 const char * const config_element_tracefile_size
= "tracefile_size";
98 const char * const config_element_tracefile_count
= "tracefile_count";
99 const char * const config_element_live_timer_interval
= "live_timer_interval";
100 const char * const config_element_type
= "type";
101 const char * const config_element_buffer_type
= "buffer_type";
102 const char * const config_element_session
= "session";
103 const char * const config_element_sessions
= "sessions";
104 const char * const config_element_perf
= "perf";
105 const char * const config_element_config
= "config";
106 const char * const config_element_started
= "started";
107 const char * const config_element_snapshot_mode
= "snapshot_mode";
108 const char * const config_element_loglevel
= "loglevel";
109 const char * const config_element_loglevel_type
= "loglevel_type";
110 const char * const config_element_filter
= "filter";
111 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
112 const char * const config_element_consumer_output
= "consumer_output";
113 const char * const config_element_destination
= "destination";
114 const char * const config_element_path
= "path";
115 const char * const config_element_net_output
= "net_output";
116 const char * const config_element_control_uri
= "control_uri";
117 const char * const config_element_data_uri
= "data_uri";
118 const char * const config_element_max_size
= "max_size";
119 const char * const config_element_pid
= "pid";
120 const char * const config_element_pids
= "pids";
121 const char * const config_element_shared_memory_path
= "shared_memory_path";
122 const char * const config_element_pid_tracker
= "pid_tracker";
123 const char * const config_element_trackers
= "trackers";
124 const char * const config_element_targets
= "targets";
125 const char * const config_element_target_pid
= "pid_target";
127 const char * const config_domain_type_kernel
= "KERNEL";
128 const char * const config_domain_type_ust
= "UST";
129 const char * const config_domain_type_jul
= "JUL";
130 const char * const config_domain_type_log4j
= "LOG4J";
131 const char * const config_domain_type_python
= "PYTHON";
133 const char * const config_buffer_type_per_pid
= "PER_PID";
134 const char * const config_buffer_type_per_uid
= "PER_UID";
135 const char * const config_buffer_type_global
= "GLOBAL";
137 const char * const config_overwrite_mode_discard
= "DISCARD";
138 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
140 const char * const config_output_type_splice
= "SPLICE";
141 const char * const config_output_type_mmap
= "MMAP";
143 const char * const config_loglevel_type_all
= "ALL";
144 const char * const config_loglevel_type_range
= "RANGE";
145 const char * const config_loglevel_type_single
= "SINGLE";
147 const char * const config_event_type_all
= "ALL";
148 const char * const config_event_type_tracepoint
= "TRACEPOINT";
149 const char * const config_event_type_probe
= "PROBE";
150 const char * const config_event_type_function
= "FUNCTION";
151 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
152 const char * const config_event_type_noop
= "NOOP";
153 const char * const config_event_type_syscall
= "SYSCALL";
154 const char * const config_event_type_kprobe
= "KPROBE";
155 const char * const config_event_type_kretprobe
= "KRETPROBE";
157 const char * const config_event_context_pid
= "PID";
158 const char * const config_event_context_procname
= "PROCNAME";
159 const char * const config_event_context_prio
= "PRIO";
160 const char * const config_event_context_nice
= "NICE";
161 const char * const config_event_context_vpid
= "VPID";
162 const char * const config_event_context_tid
= "TID";
163 const char * const config_event_context_vtid
= "VTID";
164 const char * const config_event_context_ppid
= "PPID";
165 const char * const config_event_context_vppid
= "VPPID";
166 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
167 const char * const config_event_context_hostname
= "HOSTNAME";
168 const char * const config_event_context_ip
= "IP";
169 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
171 enum process_event_node_phase
{
176 struct consumer_output
{
183 static int config_entry_handler_filter(struct handler_filter_args
*args
,
184 const char *section
, const char *name
, const char *value
)
187 struct config_entry entry
= { section
, name
, value
};
191 if (!section
|| !name
|| !value
) {
197 if (strcmp(args
->section
, section
)) {
202 ret
= args
->handler(&entry
, args
->user_data
);
208 int config_get_section_entries(const char *override_path
, const char *section
,
209 config_entry_handler_cb handler
, void *user_data
)
213 FILE *config_file
= NULL
;
214 struct handler_filter_args filter
= { section
, handler
, user_data
};
216 /* First, try system-wide conf. file. */
217 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
219 config_file
= fopen(path
, "r");
221 DBG("Loading daemon conf file at %s", path
);
223 * Return value is not very important here since error or not, we
224 * continue and try the next possible conf. file.
226 (void) ini_parse_file(config_file
,
227 (ini_entry_handler
) config_entry_handler_filter
,
232 /* Second is the user local configuration. */
233 path
= utils_get_home_dir();
235 char fullpath
[PATH_MAX
];
237 ret
= snprintf(fullpath
, sizeof(fullpath
),
238 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
240 PERROR("snprintf user conf. path");
244 config_file
= fopen(fullpath
, "r");
246 DBG("Loading daemon user conf file at %s", path
);
248 * Return value is not very important here since error or not, we
249 * continue and try the next possible conf. file.
251 (void) ini_parse_file(config_file
,
252 (ini_entry_handler
) config_entry_handler_filter
,
258 /* Final path is the one that the user might have provided. */
260 config_file
= fopen(override_path
, "r");
262 DBG("Loading daemon command line conf file at %s", override_path
);
263 (void) ini_parse_file(config_file
,
264 (ini_entry_handler
) config_entry_handler_filter
,
268 ERR("Failed to open daemon configuration file at %s",
275 /* Everything went well. */
283 int config_parse_value(const char *value
)
286 char *endptr
, *lower_str
;
296 v
= strtoul(value
, &endptr
, 10);
297 if (endptr
!= value
) {
302 lower_str
= zmalloc(len
+ 1);
309 for (i
= 0; i
< len
; i
++) {
310 lower_str
[i
] = tolower(value
[i
]);
313 if (!strcmp(lower_str
, config_str_yes
) ||
314 !strcmp(lower_str
, config_str_true
) ||
315 !strcmp(lower_str
, config_str_on
)) {
317 } else if (!strcmp(lower_str
, config_str_no
) ||
318 !strcmp(lower_str
, config_str_false
) ||
319 !strcmp(lower_str
, config_str_off
)) {
331 * Returns a xmlChar string which must be released using xmlFree().
333 static xmlChar
*encode_string(const char *in_str
)
335 xmlChar
*out_str
= NULL
;
336 xmlCharEncodingHandlerPtr handler
;
337 int out_len
, ret
, in_len
;
341 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
343 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
347 in_len
= strlen(in_str
);
349 * Add 1 byte for the NULL terminted character. The factor 4 here is
350 * used because UTF-8 characters can take up to 4 bytes.
352 out_len
= (in_len
* 4) + 1;
353 out_str
= xmlMalloc(out_len
);
358 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
365 /* out_len is now the size of out_str */
366 out_str
[out_len
] = '\0';
372 struct config_writer
*config_writer_create(int fd_output
, int indent
)
375 struct config_writer
*writer
;
376 xmlOutputBufferPtr buffer
;
378 writer
= zmalloc(sizeof(struct config_writer
));
380 PERROR("zmalloc config_writer_create");
384 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
389 writer
->writer
= xmlNewTextWriter(buffer
);
390 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
391 config_xml_encoding
, NULL
);
396 ret
= xmlTextWriterSetIndentString(writer
->writer
,
397 BAD_CAST config_xml_indent_string
);
402 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
410 config_writer_destroy(writer
);
415 int config_writer_destroy(struct config_writer
*writer
)
424 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
425 WARN("Could not close XML document");
429 if (writer
->writer
) {
430 xmlFreeTextWriter(writer
->writer
);
439 int config_writer_open_element(struct config_writer
*writer
,
440 const char *element_name
)
443 xmlChar
*encoded_element_name
;
445 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
450 encoded_element_name
= encode_string(element_name
);
451 if (!encoded_element_name
) {
456 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
457 xmlFree(encoded_element_name
);
459 return ret
> 0 ? 0 : ret
;
463 int config_writer_close_element(struct config_writer
*writer
)
467 if (!writer
|| !writer
->writer
) {
472 ret
= xmlTextWriterEndElement(writer
->writer
);
474 return ret
> 0 ? 0 : ret
;
478 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
479 const char *element_name
, uint64_t value
)
482 xmlChar
*encoded_element_name
;
484 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
489 encoded_element_name
= encode_string(element_name
);
490 if (!encoded_element_name
) {
495 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
496 encoded_element_name
, "%" PRIu64
, value
);
497 xmlFree(encoded_element_name
);
499 return ret
> 0 ? 0 : ret
;
503 int config_writer_write_element_signed_int(struct config_writer
*writer
,
504 const char *element_name
, int64_t value
)
507 xmlChar
*encoded_element_name
;
509 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
514 encoded_element_name
= encode_string(element_name
);
515 if (!encoded_element_name
) {
520 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
521 encoded_element_name
, "%" PRIi64
, value
);
522 xmlFree(encoded_element_name
);
524 return ret
> 0 ? 0 : ret
;
528 int config_writer_write_element_bool(struct config_writer
*writer
,
529 const char *element_name
, int value
)
531 return config_writer_write_element_string(writer
, element_name
,
532 value
? config_xml_true
: config_xml_false
);
536 int config_writer_write_element_string(struct config_writer
*writer
,
537 const char *element_name
, const char *value
)
540 xmlChar
*encoded_element_name
= NULL
;
541 xmlChar
*encoded_value
= NULL
;
543 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
549 encoded_element_name
= encode_string(element_name
);
550 if (!encoded_element_name
) {
555 encoded_value
= encode_string(value
);
556 if (!encoded_value
) {
561 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
564 xmlFree(encoded_element_name
);
565 xmlFree(encoded_value
);
566 return ret
> 0 ? 0 : ret
;
570 void xml_error_handler(void *ctx
, const char *format
, ...)
576 va_start(args
, format
);
577 ret
= vasprintf(&errMsg
, format
, args
);
580 ERR("String allocation failed in xml error handler");
584 fprintf(stderr
, "XML Error: %s", errMsg
);
589 void fini_session_config_validation_ctx(
590 struct session_config_validation_ctx
*ctx
)
592 if (ctx
->parser_ctx
) {
593 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
597 xmlSchemaFree(ctx
->schema
);
600 if (ctx
->schema_validation_ctx
) {
601 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
604 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
608 char *get_session_config_xsd_path()
611 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
612 size_t base_path_len
;
616 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
619 base_path_len
= strlen(base_path
);
620 max_path_len
= base_path_len
+
621 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
622 xsd_path
= zmalloc(max_path_len
);
627 strncpy(xsd_path
, base_path
, max_path_len
);
628 if (xsd_path
[base_path_len
- 1] != '/') {
629 xsd_path
[base_path_len
++] = '/';
632 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
633 max_path_len
- base_path_len
);
639 int init_session_config_validation_ctx(
640 struct session_config_validation_ctx
*ctx
)
643 char *xsd_path
= get_session_config_xsd_path();
646 ret
= -LTTNG_ERR_NOMEM
;
650 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
651 if (!ctx
->parser_ctx
) {
652 ERR("XSD parser context creation failed");
653 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
656 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
657 xml_error_handler
, NULL
);
659 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
661 ERR("XSD parsing failed");
662 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
666 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
667 if (!ctx
->schema_validation_ctx
) {
668 ERR("XSD validation context creation failed");
669 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
673 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
674 xml_error_handler
, NULL
);
679 fini_session_config_validation_ctx(ctx
);
687 int parse_uint(xmlChar
*str
, uint64_t *val
)
697 *val
= strtoull((const char *) str
, &endptr
, 10);
698 if (!endptr
|| *endptr
) {
709 int parse_int(xmlChar
*str
, int64_t *val
)
719 *val
= strtoll((const char *) str
, &endptr
, 10);
720 if (!endptr
|| *endptr
) {
731 int parse_bool(xmlChar
*str
, int *val
)
740 if (!strcmp((const char *) str
, config_xml_true
)) {
742 } else if (!strcmp((const char *) str
, config_xml_false
)) {
745 WARN("Invalid boolean value encoutered (%s).",
754 int get_domain_type(xmlChar
*domain
)
762 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
763 ret
= LTTNG_DOMAIN_KERNEL
;
764 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
765 ret
= LTTNG_DOMAIN_UST
;
766 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
767 ret
= LTTNG_DOMAIN_JUL
;
768 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
769 ret
= LTTNG_DOMAIN_LOG4J
;
770 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
771 ret
= LTTNG_DOMAIN_PYTHON
;
782 int get_buffer_type(xmlChar
*buffer_type
)
790 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
791 ret
= LTTNG_BUFFER_GLOBAL
;
792 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
793 ret
= LTTNG_BUFFER_PER_UID
;
794 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
795 ret
= LTTNG_BUFFER_PER_PID
;
806 int get_overwrite_mode(xmlChar
*overwrite_mode
)
810 if (!overwrite_mode
) {
814 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
816 } else if (!strcmp((char *) overwrite_mode
,
817 config_overwrite_mode_discard
)) {
829 int get_output_type(xmlChar
*output_type
)
837 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
838 ret
= LTTNG_EVENT_MMAP
;
839 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
840 ret
= LTTNG_EVENT_SPLICE
;
851 int get_event_type(xmlChar
*event_type
)
859 if (!strcmp((char *) event_type
, config_event_type_all
)) {
860 ret
= LTTNG_EVENT_ALL
;
861 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
862 ret
= LTTNG_EVENT_TRACEPOINT
;
863 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
864 ret
= LTTNG_EVENT_PROBE
;
865 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
866 ret
= LTTNG_EVENT_FUNCTION
;
867 } else if (!strcmp((char *) event_type
,
868 config_event_type_function_entry
)) {
869 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
870 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
871 ret
= LTTNG_EVENT_NOOP
;
872 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
873 ret
= LTTNG_EVENT_SYSCALL
;
884 int get_loglevel_type(xmlChar
*loglevel_type
)
888 if (!loglevel_type
) {
892 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
893 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
894 } else if (!strcmp((char *) loglevel_type
,
895 config_loglevel_type_range
)) {
896 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
897 } else if (!strcmp((char *) loglevel_type
,
898 config_loglevel_type_single
)) {
899 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
910 * Return the context type or -1 on error.
913 int get_context_type(xmlChar
*context_type
)
921 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
922 ret
= LTTNG_EVENT_CONTEXT_PID
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_procname
)) {
925 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_prio
)) {
928 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_nice
)) {
931 ret
= LTTNG_EVENT_CONTEXT_NICE
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_vpid
)) {
934 ret
= LTTNG_EVENT_CONTEXT_VPID
;
935 } else if (!strcmp((char *) context_type
,
936 config_event_context_tid
)) {
937 ret
= LTTNG_EVENT_CONTEXT_TID
;
938 } else if (!strcmp((char *) context_type
,
939 config_event_context_vtid
)) {
940 ret
= LTTNG_EVENT_CONTEXT_VTID
;
941 } else if (!strcmp((char *) context_type
,
942 config_event_context_ppid
)) {
943 ret
= LTTNG_EVENT_CONTEXT_PPID
;
944 } else if (!strcmp((char *) context_type
,
945 config_event_context_vppid
)) {
946 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
947 } else if (!strcmp((char *) context_type
,
948 config_event_context_pthread_id
)) {
949 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
950 } else if (!strcmp((char *) context_type
,
951 config_event_context_hostname
)) {
952 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
953 } else if (!strcmp((char *) context_type
,
954 config_event_context_ip
)) {
955 ret
= LTTNG_EVENT_CONTEXT_IP
;
966 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
971 for (node
= xmlFirstElementChild(domain_node
); node
;
972 node
= xmlNextElementSibling(node
)) {
973 if (!strcmp((const char *) node
->name
, config_element_type
)) {
975 xmlChar
*node_content
= xmlNodeGetContent(node
);
977 ret
= -LTTNG_ERR_NOMEM
;
981 ret
= get_domain_type(node_content
);
984 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
989 } else if (!strcmp((const char *) node
->name
,
990 config_element_buffer_type
)) {
992 xmlChar
*node_content
= xmlNodeGetContent(node
);
994 ret
= -LTTNG_ERR_NOMEM
;
998 ret
= get_buffer_type(node_content
);
1001 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1005 domain
->buf_type
= ret
;
1014 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1019 for (node
= xmlFirstElementChild(net_output_node
); node
;
1020 node
= xmlNextElementSibling(node
)) {
1021 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1023 *control_uri
= (char *) xmlNodeGetContent(node
);
1024 if (!*control_uri
) {
1029 *data_uri
= (char *) xmlNodeGetContent(node
);
1036 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1040 int process_consumer_output(xmlNodePtr consumer_output_node
,
1041 struct consumer_output
*output
)
1048 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1049 node
= xmlNextElementSibling(node
)) {
1050 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1051 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1055 ret
= -LTTNG_ERR_NOMEM
;
1059 ret
= parse_bool(enabled_str
, &output
->enabled
);
1065 xmlNodePtr output_type_node
;
1068 output_type_node
= xmlFirstElementChild(node
);
1069 if (!output_type_node
) {
1070 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1074 if (!strcmp((const char *) output_type_node
->name
,
1075 config_element_path
)) {
1077 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1078 if (!output
->path
) {
1079 ret
= -LTTNG_ERR_NOMEM
;
1084 ret
= get_net_output_uris(output_type_node
,
1085 &output
->control_uri
, &output
->data_uri
);
1097 free(output
->control_uri
);
1098 free(output
->data_uri
);
1099 memset(output
, 0, sizeof(struct consumer_output
));
1105 int create_session_net_output(const char *name
, const char *control_uri
,
1106 const char *data_uri
)
1109 struct lttng_handle
*handle
;
1110 const char *uri
= NULL
;
1114 handle
= lttng_create_handle(name
, NULL
);
1116 ret
= -LTTNG_ERR_NOMEM
;
1120 if (!control_uri
|| !data_uri
) {
1121 uri
= control_uri
? control_uri
: data_uri
;
1126 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1127 lttng_destroy_handle(handle
);
1133 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1136 xmlNodePtr node
= NULL
;
1137 xmlNodePtr snapshot_output_list_node
;
1138 xmlNodePtr snapshot_output_node
;
1140 assert(session_name
);
1142 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1151 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1153 /* Parse and create snapshot outputs */
1155 for (snapshot_output_node
=
1156 xmlFirstElementChild(snapshot_output_list_node
);
1157 snapshot_output_node
; snapshot_output_node
=
1158 xmlNextElementSibling(snapshot_output_node
)) {
1160 uint64_t max_size
= UINT64_MAX
;
1161 struct consumer_output output
= { 0 };
1162 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1164 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1165 node
= xmlNextElementSibling(node
)) {
1166 if (!strcmp((const char *) node
->name
,
1167 config_element_name
)) {
1169 name
= (char *) xmlNodeGetContent(node
);
1171 ret
= -LTTNG_ERR_NOMEM
;
1172 goto error_snapshot_output
;
1174 } else if (!strcmp((const char *) node
->name
,
1175 config_element_max_size
)) {
1176 xmlChar
*content
= xmlNodeGetContent(node
);
1180 ret
= -LTTNG_ERR_NOMEM
;
1181 goto error_snapshot_output
;
1183 ret
= parse_uint(content
, &max_size
);
1186 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1187 goto error_snapshot_output
;
1190 /* consumer_output */
1191 ret
= process_consumer_output(node
, &output
);
1193 goto error_snapshot_output
;
1198 snapshot_output
= lttng_snapshot_output_create();
1199 if (!snapshot_output
) {
1200 ret
= -LTTNG_ERR_NOMEM
;
1201 goto error_snapshot_output
;
1204 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1206 goto error_snapshot_output
;
1209 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1211 goto error_snapshot_output
;
1215 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1218 goto error_snapshot_output
;
1221 if (output
.control_uri
) {
1222 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1225 goto error_snapshot_output
;
1229 if (output
.data_uri
) {
1230 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1233 goto error_snapshot_output
;
1238 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1239 error_snapshot_output
:
1242 free(output
.control_uri
);
1243 free(output
.data_uri
);
1244 lttng_snapshot_output_destroy(snapshot_output
);
1254 int create_session(const char *name
,
1255 struct lttng_domain
*kernel_domain
,
1256 struct lttng_domain
*ust_domain
,
1257 struct lttng_domain
*jul_domain
,
1258 struct lttng_domain
*log4j_domain
,
1259 xmlNodePtr output_node
,
1260 uint64_t live_timer_interval
)
1263 struct consumer_output output
= { 0 };
1264 xmlNodePtr consumer_output_node
;
1269 consumer_output_node
= xmlFirstElementChild(output_node
);
1270 if (!consumer_output_node
) {
1271 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1275 if (strcmp((const char *) consumer_output_node
->name
,
1276 config_element_consumer_output
)) {
1277 WARN("Invalid output type, expected %s node",
1278 config_element_consumer_output
);
1279 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1283 ret
= process_consumer_output(consumer_output_node
, &output
);
1289 if (live_timer_interval
!= UINT64_MAX
&&
1290 !output
.control_uri
&& !output
.data_uri
) {
1291 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1295 if (output
.control_uri
|| output
.data_uri
) {
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 ret
= create_session_net_output(name
, output
.control_uri
,
1318 /* either local output or no output */
1319 ret
= lttng_create_session(name
, output
.path
);
1326 free(output
.control_uri
);
1327 free(output
.data_uri
);
1331 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1332 struct lttng_event_probe_attr
*attr
)
1336 assert(probe_attribute_node
);
1339 if (!strcmp((const char *) probe_attribute_node
->name
,
1340 config_element_address
)) {
1345 content
= xmlNodeGetContent(probe_attribute_node
);
1347 ret
= -LTTNG_ERR_NOMEM
;
1351 ret
= parse_uint(content
, &addr
);
1354 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1359 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1360 config_element_offset
)) {
1362 uint64_t offset
= 0;
1365 content
= xmlNodeGetContent(probe_attribute_node
);
1367 ret
= -LTTNG_ERR_NOMEM
;
1371 ret
= parse_uint(content
, &offset
);
1374 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1378 attr
->offset
= offset
;
1379 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1380 config_element_symbol_name
)) {
1385 content
= xmlNodeGetContent(probe_attribute_node
);
1387 ret
= -LTTNG_ERR_NOMEM
;
1391 name_len
= strlen((char *) content
);
1392 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1393 WARN("symbol_name too long.");
1394 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1399 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1408 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1409 const char *channel_name
, const enum process_event_node_phase phase
)
1413 struct lttng_event event
;
1414 char **exclusions
= NULL
;
1415 unsigned long exclusion_count
= 0;
1416 char *filter_expression
= NULL
;
1420 assert(channel_name
);
1422 memset(&event
, 0, sizeof(event
));
1424 /* Initialize default log level which varies by domain */
1425 switch (handle
->domain
.type
)
1427 case LTTNG_DOMAIN_JUL
:
1428 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1430 case LTTNG_DOMAIN_LOG4J
:
1431 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1433 case LTTNG_DOMAIN_PYTHON
:
1434 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1436 case LTTNG_DOMAIN_UST
:
1437 case LTTNG_DOMAIN_KERNEL
:
1438 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1444 for (node
= xmlFirstElementChild(event_node
); node
;
1445 node
= xmlNextElementSibling(node
)) {
1446 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1451 content
= xmlNodeGetContent(node
);
1453 ret
= -LTTNG_ERR_NOMEM
;
1457 name_len
= strlen((char *) content
);
1458 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1459 WARN("Channel name too long.");
1460 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1465 strncpy(event
.name
, (const char *) content
, name_len
);
1467 } else if (!strcmp((const char *) node
->name
,
1468 config_element_enabled
)) {
1469 xmlChar
*content
= xmlNodeGetContent(node
);
1473 ret
= -LTTNG_ERR_NOMEM
;
1477 ret
= parse_bool(content
, &event
.enabled
);
1480 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1483 } else if (!strcmp((const char *) node
->name
,
1484 config_element_type
)) {
1485 xmlChar
*content
= xmlNodeGetContent(node
);
1489 ret
= -LTTNG_ERR_NOMEM
;
1493 ret
= get_event_type(content
);
1496 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1501 } else if (!strcmp((const char *) node
->name
,
1502 config_element_loglevel_type
)) {
1503 xmlChar
*content
= xmlNodeGetContent(node
);
1507 ret
= -LTTNG_ERR_NOMEM
;
1511 ret
= get_loglevel_type(content
);
1514 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1518 event
.loglevel_type
= ret
;
1519 } else if (!strcmp((const char *) node
->name
,
1520 config_element_loglevel
)) {
1522 int64_t loglevel
= 0;
1525 content
= xmlNodeGetContent(node
);
1527 ret
= -LTTNG_ERR_NOMEM
;
1531 ret
= parse_int(content
, &loglevel
);
1534 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1538 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1539 WARN("loglevel out of range.");
1540 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1544 event
.loglevel
= loglevel
;
1545 } else if (!strcmp((const char *) node
->name
,
1546 config_element_filter
)) {
1548 xmlNodeGetContent(node
);
1552 ret
= -LTTNG_ERR_NOMEM
;
1556 free(filter_expression
);
1557 filter_expression
= strdup((char *) content
);
1559 if (!filter_expression
) {
1560 ret
= -LTTNG_ERR_NOMEM
;
1563 } else if (!strcmp((const char *) node
->name
,
1564 config_element_exclusions
)) {
1565 xmlNodePtr exclusion_node
;
1566 int exclusion_index
= 0;
1571 * Exclusions has already been initialized,
1574 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1578 exclusion_count
= xmlChildElementCount(node
);
1579 if (!exclusion_count
) {
1583 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1585 exclusion_count
= 0;
1586 ret
= -LTTNG_ERR_NOMEM
;
1590 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1591 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1593 xmlNodeGetContent(exclusion_node
);
1596 ret
= -LTTNG_ERR_NOMEM
;
1600 exclusions
[exclusion_index
] = strdup((const char *) content
);
1602 if (!exclusions
[exclusion_index
]) {
1603 ret
= -LTTNG_ERR_NOMEM
;
1609 event
.exclusion
= 1;
1610 } else if (!strcmp((const char *) node
->name
,
1611 config_element_attributes
)) {
1612 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1615 if (!attribute_node
) {
1616 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1620 if (!strcmp((const char *) node
->name
,
1621 config_element_probe_attributes
)) {
1622 xmlNodePtr probe_attribute_node
;
1624 /* probe_attributes */
1625 for (probe_attribute_node
=
1626 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1627 probe_attribute_node
= xmlNextElementSibling(
1628 probe_attribute_node
)) {
1630 ret
= process_probe_attribute_node(probe_attribute_node
,
1639 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1641 /* function_attributes */
1642 content
= xmlNodeGetContent(symbol_node
);
1644 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1648 sym_len
= strlen((char *) content
);
1649 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1650 WARN("Function name too long.");
1651 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1656 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1663 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1664 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1665 filter_expression
, exclusion_count
, exclusions
);
1667 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1668 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1673 for (i
= 0; i
< exclusion_count
; i
++) {
1674 free(exclusions
[i
]);
1678 free(filter_expression
);
1683 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1684 const char *channel_name
)
1687 struct lttng_event event
;
1690 assert(events_node
);
1692 assert(channel_name
);
1694 for (node
= xmlFirstElementChild(events_node
); node
;
1695 node
= xmlNextElementSibling(node
)) {
1696 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1703 * Disable all events to enable only the necessary events.
1704 * Limitations regarding lttng_disable_events and tuple descriptor
1705 * force this approach.
1707 memset(&event
, 0, sizeof(event
));
1708 event
.loglevel
= -1;
1709 event
.type
= LTTNG_EVENT_ALL
;
1710 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1715 for (node
= xmlFirstElementChild(events_node
); node
;
1716 node
= xmlNextElementSibling(node
)) {
1717 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1728 int process_channel_attr_node(xmlNodePtr attr_node
,
1729 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1730 xmlNodePtr
*events_node
)
1736 assert(contexts_node
);
1737 assert(events_node
);
1739 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1744 content
= xmlNodeGetContent(attr_node
);
1746 ret
= -LTTNG_ERR_NOMEM
;
1750 name_len
= strlen((char *) content
);
1751 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1752 WARN("Channel name too long.");
1753 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1758 strncpy(channel
->name
, (const char *) content
, name_len
);
1760 } else if (!strcmp((const char *) attr_node
->name
,
1761 config_element_enabled
)) {
1766 content
= xmlNodeGetContent(attr_node
);
1768 ret
= -LTTNG_ERR_NOMEM
;
1772 ret
= parse_bool(content
, &enabled
);
1775 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1779 channel
->enabled
= enabled
;
1780 } else if (!strcmp((const char *) attr_node
->name
,
1781 config_element_overwrite_mode
)) {
1784 /* overwrite_mode */
1785 content
= xmlNodeGetContent(attr_node
);
1787 ret
= -LTTNG_ERR_NOMEM
;
1791 ret
= get_overwrite_mode(content
);
1794 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1798 channel
->attr
.overwrite
= ret
;
1799 } else if (!strcmp((const char *) attr_node
->name
,
1800 config_element_subbuf_size
)) {
1803 /* subbuffer_size */
1804 content
= xmlNodeGetContent(attr_node
);
1806 ret
= -LTTNG_ERR_NOMEM
;
1810 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1813 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1816 } else if (!strcmp((const char *) attr_node
->name
,
1817 config_element_num_subbuf
)) {
1820 /* subbuffer_count */
1821 content
= xmlNodeGetContent(attr_node
);
1823 ret
= -LTTNG_ERR_NOMEM
;
1827 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1830 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1833 } else if (!strcmp((const char *) attr_node
->name
,
1834 config_element_switch_timer_interval
)) {
1836 uint64_t switch_timer_interval
= 0;
1838 /* switch_timer_interval */
1839 content
= xmlNodeGetContent(attr_node
);
1841 ret
= -LTTNG_ERR_NOMEM
;
1845 ret
= parse_uint(content
, &switch_timer_interval
);
1848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 if (switch_timer_interval
> UINT_MAX
) {
1853 WARN("switch_timer_interval out of range.");
1854 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1858 channel
->attr
.switch_timer_interval
=
1859 switch_timer_interval
;
1860 } else if (!strcmp((const char *) attr_node
->name
,
1861 config_element_read_timer_interval
)) {
1863 uint64_t read_timer_interval
= 0;
1865 /* read_timer_interval */
1866 content
= xmlNodeGetContent(attr_node
);
1868 ret
= -LTTNG_ERR_NOMEM
;
1872 ret
= parse_uint(content
, &read_timer_interval
);
1875 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1879 if (read_timer_interval
> UINT_MAX
) {
1880 WARN("read_timer_interval out of range.");
1881 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1885 channel
->attr
.read_timer_interval
=
1886 read_timer_interval
;
1887 } else if (!strcmp((const char *) attr_node
->name
,
1888 config_element_output_type
)) {
1892 content
= xmlNodeGetContent(attr_node
);
1894 ret
= -LTTNG_ERR_NOMEM
;
1898 ret
= get_output_type(content
);
1901 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1905 channel
->attr
.output
= ret
;
1906 } else if (!strcmp((const char *) attr_node
->name
,
1907 config_element_tracefile_size
)) {
1910 /* tracefile_size */
1911 content
= xmlNodeGetContent(attr_node
);
1913 ret
= -LTTNG_ERR_NOMEM
;
1917 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1920 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1923 } else if (!strcmp((const char *) attr_node
->name
,
1924 config_element_tracefile_count
)) {
1927 /* tracefile_count */
1928 content
= xmlNodeGetContent(attr_node
);
1930 ret
= -LTTNG_ERR_NOMEM
;
1934 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1937 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1940 } else if (!strcmp((const char *) attr_node
->name
,
1941 config_element_live_timer_interval
)) {
1943 uint64_t live_timer_interval
= 0;
1945 /* live_timer_interval */
1946 content
= xmlNodeGetContent(attr_node
);
1948 ret
= -LTTNG_ERR_NOMEM
;
1952 ret
= parse_uint(content
, &live_timer_interval
);
1955 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1959 if (live_timer_interval
> UINT_MAX
) {
1960 WARN("live_timer_interval out of range.");
1961 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1965 channel
->attr
.live_timer_interval
=
1966 live_timer_interval
;
1967 } else if (!strcmp((const char *) attr_node
->name
,
1968 config_element_events
)) {
1970 *events_node
= attr_node
;
1973 *contexts_node
= attr_node
;
1981 int process_context_node(xmlNodePtr context_node
,
1982 struct lttng_handle
*handle
, const char *channel_name
)
1985 struct lttng_event_context context
;
1986 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1989 assert(channel_name
);
1991 if (!context_child_node
) {
1992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1996 memset(&context
, 0, sizeof(context
));
1998 if (!strcmp((const char *) context_child_node
->name
,
1999 config_element_type
)) {
2001 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2003 ret
= -LTTNG_ERR_NOMEM
;
2007 ret
= get_context_type(content
);
2010 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2016 xmlNodePtr perf_attr_node
;
2019 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2020 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2021 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2022 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2023 perf_attr_node
; perf_attr_node
=
2024 xmlNextElementSibling(perf_attr_node
)) {
2025 if (!strcmp((const char *) perf_attr_node
->name
,
2026 config_element_type
)) {
2031 content
= xmlNodeGetContent(perf_attr_node
);
2033 ret
= -LTTNG_ERR_NOMEM
;
2037 ret
= parse_uint(content
, &type
);
2040 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2044 if (type
> UINT32_MAX
) {
2045 WARN("perf context type out of range.");
2046 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2050 context
.u
.perf_counter
.type
= type
;
2051 } else if (!strcmp((const char *) perf_attr_node
->name
,
2052 config_element_config
)) {
2054 uint64_t config
= 0;
2057 content
= xmlNodeGetContent(perf_attr_node
);
2059 ret
= -LTTNG_ERR_NOMEM
;
2063 ret
= parse_uint(content
, &config
);
2066 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2070 context
.u
.perf_counter
.config
= config
;
2071 } else if (!strcmp((const char *) perf_attr_node
->name
,
2072 config_element_name
)) {
2077 content
= xmlNodeGetContent(perf_attr_node
);
2079 ret
= -LTTNG_ERR_NOMEM
;
2083 name_len
= strlen((char *) content
);
2084 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2085 WARN("perf context name too long.");
2086 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2091 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2098 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2104 int process_contexts_node(xmlNodePtr contexts_node
,
2105 struct lttng_handle
*handle
, const char *channel_name
)
2108 xmlNodePtr context_node
;
2110 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2111 context_node
= xmlNextElementSibling(context_node
)) {
2112 ret
= process_context_node(context_node
, handle
, channel_name
);
2122 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2123 struct lttng_handle
*handle
)
2126 xmlNodePtr targets_node
= NULL
;
2130 assert(pid_tracker_node
);
2131 /* get the targets node */
2132 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2133 node
= xmlNextElementSibling(node
)) {
2134 if (!strcmp((const char *) node
->name
,
2135 config_element_targets
)) {
2136 targets_node
= node
;
2141 if (!targets_node
) {
2142 ret
= LTTNG_ERR_INVALID
;
2146 /* Go through all pid_target node */
2147 child
= xmlChildElementCount(targets_node
);
2149 /* The session is explicitly set to target nothing. */
2150 ret
= lttng_untrack_pid(handle
, -1);
2155 for (node
= xmlFirstElementChild(targets_node
); node
;
2156 node
= xmlNextElementSibling(node
)) {
2157 xmlNodePtr pid_target_node
= node
;
2159 /* get pid node and track it */
2160 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2161 node
= xmlNextElementSibling(node
)) {
2162 if (!strcmp((const char *) node
->name
,
2163 config_element_pid
)) {
2165 xmlChar
*content
= NULL
;
2167 content
= xmlNodeGetContent(node
);
2169 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2173 ret
= parse_int(content
, &pid
);
2176 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2180 ret
= lttng_track_pid(handle
, (int) pid
);
2186 node
= pid_target_node
;
2195 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2198 struct lttng_domain domain
= { 0 };
2199 struct lttng_handle
*handle
= NULL
;
2200 xmlNodePtr channels_node
= NULL
;
2201 xmlNodePtr trackers_node
= NULL
;
2202 xmlNodePtr pid_tracker_node
= NULL
;
2205 assert(session_name
);
2207 ret
= init_domain(domain_node
, &domain
);
2212 handle
= lttng_create_handle(session_name
, &domain
);
2214 ret
= -LTTNG_ERR_NOMEM
;
2218 /* get the channels node */
2219 for (node
= xmlFirstElementChild(domain_node
); node
;
2220 node
= xmlNextElementSibling(node
)) {
2221 if (!strcmp((const char *) node
->name
,
2222 config_element_channels
)) {
2223 channels_node
= node
;
2228 if (!channels_node
) {
2232 /* create all channels */
2233 for (node
= xmlFirstElementChild(channels_node
); node
;
2234 node
= xmlNextElementSibling(node
)) {
2235 struct lttng_channel channel
;
2236 xmlNodePtr contexts_node
= NULL
;
2237 xmlNodePtr events_node
= NULL
;
2238 xmlNodePtr channel_attr_node
;
2240 memset(&channel
, 0, sizeof(channel
));
2241 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2243 for (channel_attr_node
= xmlFirstElementChild(node
);
2244 channel_attr_node
; channel_attr_node
=
2245 xmlNextElementSibling(channel_attr_node
)) {
2246 ret
= process_channel_attr_node(channel_attr_node
,
2247 &channel
, &contexts_node
, &events_node
);
2253 ret
= lttng_enable_channel(handle
, &channel
);
2258 ret
= process_events_node(events_node
, handle
, channel
.name
);
2263 ret
= process_contexts_node(contexts_node
, handle
,
2270 /* get the trackers node */
2271 for (node
= xmlFirstElementChild(domain_node
); node
;
2272 node
= xmlNextElementSibling(node
)) {
2273 if (!strcmp((const char *) node
->name
,
2274 config_element_trackers
)) {
2275 trackers_node
= node
;
2280 if (!trackers_node
) {
2284 for (node
= xmlFirstElementChild(trackers_node
); node
;
2285 node
= xmlNextElementSibling(node
)) {
2286 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2287 pid_tracker_node
= node
;
2288 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2295 if (!pid_tracker_node
) {
2296 lttng_track_pid(handle
, -1);
2300 lttng_destroy_handle(handle
);
2305 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2308 int ret
, started
= -1, snapshot_mode
= -1;
2309 uint64_t live_timer_interval
= UINT64_MAX
;
2310 xmlChar
*name
= NULL
;
2311 xmlChar
*shm_path
= NULL
;
2312 xmlNodePtr domains_node
= NULL
;
2313 xmlNodePtr output_node
= NULL
;
2315 struct lttng_domain
*kernel_domain
= NULL
;
2316 struct lttng_domain
*ust_domain
= NULL
;
2317 struct lttng_domain
*jul_domain
= NULL
;
2318 struct lttng_domain
*log4j_domain
= NULL
;
2319 struct lttng_domain
*python_domain
= NULL
;
2321 for (node
= xmlFirstElementChild(session_node
); node
;
2322 node
= xmlNextElementSibling(node
)) {
2323 if (!name
&& !strcmp((const char *) node
->name
,
2324 config_element_name
)) {
2326 xmlChar
*node_content
= xmlNodeGetContent(node
);
2327 if (!node_content
) {
2328 ret
= -LTTNG_ERR_NOMEM
;
2332 name
= node_content
;
2333 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2334 config_element_domains
)) {
2336 domains_node
= node
;
2337 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2338 config_element_started
)) {
2340 xmlChar
*node_content
= xmlNodeGetContent(node
);
2341 if (!node_content
) {
2342 ret
= -LTTNG_ERR_NOMEM
;
2346 ret
= parse_bool(node_content
, &started
);
2349 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2352 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2353 config_element_output
)) {
2356 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2357 config_element_shared_memory_path
)) {
2358 /* shared memory path */
2359 xmlChar
*node_content
= xmlNodeGetContent(node
);
2360 if (!node_content
) {
2361 ret
= -LTTNG_ERR_NOMEM
;
2365 shm_path
= node_content
;
2367 /* attributes, snapshot_mode or live_timer_interval */
2368 xmlNodePtr attributes_child
=
2369 xmlFirstElementChild(node
);
2371 if (!strcmp((const char *) attributes_child
->name
,
2372 config_element_snapshot_mode
)) {
2374 xmlChar
*snapshot_mode_content
=
2375 xmlNodeGetContent(attributes_child
);
2376 if (!snapshot_mode_content
) {
2377 ret
= -LTTNG_ERR_NOMEM
;
2381 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2382 free(snapshot_mode_content
);
2384 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2388 /* live_timer_interval */
2389 xmlChar
*timer_interval_content
=
2390 xmlNodeGetContent(attributes_child
);
2391 if (!timer_interval_content
) {
2392 ret
= -LTTNG_ERR_NOMEM
;
2396 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2397 free(timer_interval_content
);
2399 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2407 /* Mandatory attribute, as defined in the session XSD */
2408 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2412 if (session_name
&& strcmp((char *) name
, session_name
)) {
2413 /* This is not the session we are looking for */
2414 ret
= -LTTNG_ERR_NO_SESSION
;
2418 /* Init domains to create the session handles */
2419 for (node
= xmlFirstElementChild(domains_node
); node
;
2420 node
= xmlNextElementSibling(node
)) {
2421 struct lttng_domain
*domain
;
2423 domain
= zmalloc(sizeof(*domain
));
2425 ret
= -LTTNG_ERR_NOMEM
;
2429 ret
= init_domain(node
, domain
);
2431 goto domain_init_error
;
2434 switch (domain
->type
) {
2435 case LTTNG_DOMAIN_KERNEL
:
2436 if (kernel_domain
) {
2437 /* Same domain seen twice, invalid! */
2438 goto domain_init_error
;
2440 kernel_domain
= domain
;
2442 case LTTNG_DOMAIN_UST
:
2444 /* Same domain seen twice, invalid! */
2445 goto domain_init_error
;
2447 ust_domain
= domain
;
2449 case LTTNG_DOMAIN_JUL
:
2451 /* Same domain seen twice, invalid! */
2452 goto domain_init_error
;
2454 jul_domain
= domain
;
2456 case LTTNG_DOMAIN_LOG4J
:
2458 /* Same domain seen twice, invalid! */
2459 goto domain_init_error
;
2461 log4j_domain
= domain
;
2463 case LTTNG_DOMAIN_PYTHON
:
2464 if (python_domain
) {
2465 /* Same domain seen twice, invalid! */
2466 goto domain_init_error
;
2468 python_domain
= domain
;
2471 WARN("Invalid domain type");
2472 goto domain_init_error
;
2477 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2482 /* Destroy session if it exists */
2483 ret
= lttng_destroy_session((const char *) name
);
2484 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2485 ERR("Failed to destroy existing session.");
2490 /* Create session type depending on output type */
2491 if (snapshot_mode
&& snapshot_mode
!= -1) {
2492 ret
= create_snapshot_session((const char *) name
, output_node
);
2493 } else if (live_timer_interval
&&
2494 live_timer_interval
!= UINT64_MAX
) {
2495 ret
= create_session((const char *) name
, kernel_domain
,
2496 ust_domain
, jul_domain
, log4j_domain
,
2497 output_node
, live_timer_interval
);
2499 /* regular session */
2500 ret
= create_session((const char *) name
, kernel_domain
,
2501 ust_domain
, jul_domain
, log4j_domain
,
2502 output_node
, UINT64_MAX
);
2509 ret
= lttng_set_session_shm_path((const char *) name
,
2510 (const char *) shm_path
);
2516 for (node
= xmlFirstElementChild(domains_node
); node
;
2517 node
= xmlNextElementSibling(node
)) {
2518 ret
= process_domain_node(node
, (const char *) name
);
2525 ret
= lttng_start_tracing((const char *) name
);
2533 ERR("Failed to load session %s: %s", (const char *) name
,
2534 lttng_strerror(ret
));
2535 lttng_destroy_session((const char *) name
);
2539 free(kernel_domain
);
2543 free(python_domain
);
2550 * Return 1 if the given path is readable by the current UID or 0 if not.
2551 * Return -1 if the path is EPERM.
2553 static int validate_file_read_creds(const char *path
)
2559 /* Can we read the file. */
2560 ret
= access(path
, R_OK
);
2564 if (errno
== EACCES
) {
2575 int load_session_from_file(const char *path
, const char *session_name
,
2576 struct session_config_validation_ctx
*validation_ctx
, int override
)
2578 int ret
, session_found
= !session_name
;
2579 xmlDocPtr doc
= NULL
;
2580 xmlNodePtr sessions_node
;
2581 xmlNodePtr session_node
;
2584 assert(validation_ctx
);
2586 ret
= validate_file_read_creds(path
);
2589 ret
= -LTTNG_ERR_EPERM
;
2591 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2596 doc
= xmlParseFile(path
);
2598 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2602 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2604 ERR("Session configuration file validation failed");
2605 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2609 sessions_node
= xmlDocGetRootElement(doc
);
2610 if (!sessions_node
) {
2614 for (session_node
= xmlFirstElementChild(sessions_node
);
2615 session_node
; session_node
=
2616 xmlNextElementSibling(session_node
)) {
2617 ret
= process_session_node(session_node
,
2618 session_name
, override
);
2619 if (session_name
&& ret
== 0) {
2620 /* Target session found and loaded */
2628 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2633 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2635 struct dirent
*alloc_dirent(const char *path
)
2639 struct dirent
*entry
;
2641 name_max
= pathconf(path
, _PC_NAME_MAX
);
2642 if (name_max
== -1) {
2643 name_max
= PATH_MAX
;
2645 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2646 entry
= zmalloc(len
);
2651 int load_session_from_path(const char *path
, const char *session_name
,
2652 struct session_config_validation_ctx
*validation_ctx
, int override
)
2654 int ret
, session_found
= !session_name
;
2655 DIR *directory
= NULL
;
2658 assert(validation_ctx
);
2660 directory
= opendir(path
);
2664 /* Try the file loading. */
2667 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2670 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2675 struct dirent
*entry
;
2676 struct dirent
*result
;
2677 char *file_path
= NULL
;
2678 size_t path_len
= strlen(path
);
2680 if (path_len
>= PATH_MAX
) {
2681 ret
= -LTTNG_ERR_INVALID
;
2685 entry
= alloc_dirent(path
);
2687 ret
= -LTTNG_ERR_NOMEM
;
2691 file_path
= zmalloc(PATH_MAX
);
2693 ret
= -LTTNG_ERR_NOMEM
;
2698 strncpy(file_path
, path
, path_len
);
2699 if (file_path
[path_len
- 1] != '/') {
2700 file_path
[path_len
++] = '/';
2704 /* Search for *.lttng files */
2705 while (!readdir_r(directory
, entry
, &result
) && result
) {
2706 size_t file_name_len
= strlen(result
->d_name
);
2708 if (file_name_len
<=
2709 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2713 if (path_len
+ file_name_len
>= PATH_MAX
) {
2717 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2718 result
->d_name
+ file_name_len
- sizeof(
2719 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2723 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2724 file_path
[path_len
+ file_name_len
] = '\0';
2726 ret
= load_session_from_file(file_path
, session_name
,
2727 validation_ctx
, override
);
2728 if (session_name
&& !ret
) {
2737 ret
= load_session_from_file(path
, session_name
,
2738 validation_ctx
, override
);
2748 if (closedir(directory
)) {
2753 if (session_found
) {
2761 * Validate that the given path's credentials and the current process have the
2762 * same UID. If so, return 1 else return 0 if it does NOT match.
2764 static int validate_path_creds(const char *path
)
2766 int ret
, uid
= getuid();
2775 ret
= stat(path
, &buf
);
2777 if (errno
!= ENOENT
) {
2780 ret
= -LTTNG_ERR_INVALID
;
2784 if (buf
.st_uid
!= uid
) {
2795 int config_load_session(const char *path
, const char *session_name
,
2796 int override
, unsigned int autoload
)
2799 bool session_loaded
= false;
2800 const char *path_ptr
= NULL
;
2801 struct session_config_validation_ctx validation_ctx
= { 0 };
2803 ret
= init_session_config_validation_ctx(&validation_ctx
);
2810 const char *sys_path
;
2813 home_path
= utils_get_home_dir();
2815 char path
[PATH_MAX
];
2818 * Try user session configuration path. Ignore error here so we can
2819 * continue loading the system wide sessions.
2822 ret
= snprintf(path
, sizeof(path
),
2823 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2824 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2826 PERROR("snprintf session autoload home config path");
2831 * Credentials are only validated for the autoload in order to
2832 * avoid any user session daemon to try to load kernel sessions
2833 * automatically and failing all the times.
2835 ret
= validate_path_creds(path
);
2840 ret
= snprintf(path
, sizeof(path
),
2841 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2843 PERROR("snprintf session home config path");
2849 ret
= load_session_from_path(path_ptr
, session_name
,
2850 &validation_ctx
, override
);
2851 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2855 * Continue even if the session was found since we have to try
2856 * the system wide sessions.
2858 session_loaded
= true;
2862 /* Reset path pointer for the system wide dir. */
2865 /* Try system wide configuration directory. */
2867 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2868 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2869 ret
= validate_path_creds(sys_path
);
2871 path_ptr
= sys_path
;
2874 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2875 path_ptr
= sys_path
;
2879 ret
= load_session_from_path(path_ptr
, session_name
,
2880 &validation_ctx
, override
);
2882 session_loaded
= true;
2886 ret
= access(path
, F_OK
);
2891 ret
= -LTTNG_ERR_INVALID
;
2892 WARN("Session configuration path does not exist.");
2895 ret
= -LTTNG_ERR_EPERM
;
2898 ret
= -LTTNG_ERR_UNK
;
2904 ret
= load_session_from_path(path
, session_name
,
2905 &validation_ctx
, override
);
2908 fini_session_config_validation_ctx(&validation_ctx
);
2909 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2911 * Don't report an error if no sessions are found when called
2912 * without a session_name or a search path.
2917 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2918 /* A matching session was found in one of the search paths. */
2925 void __attribute__((destructor
)) session_config_exit(void)