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
, struct lttng_domain
*domain
,
1106 const char *control_uri
, const char *data_uri
)
1109 struct lttng_handle
*handle
;
1110 const char *uri
= NULL
;
1115 handle
= lttng_create_handle(name
, domain
);
1117 ret
= -LTTNG_ERR_NOMEM
;
1121 if (!control_uri
|| !data_uri
) {
1122 uri
= control_uri
? control_uri
: data_uri
;
1127 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1128 lttng_destroy_handle(handle
);
1134 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1137 xmlNodePtr node
= NULL
;
1138 xmlNodePtr snapshot_output_list_node
;
1139 xmlNodePtr snapshot_output_node
;
1141 assert(session_name
);
1143 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1152 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1154 /* Parse and create snapshot outputs */
1156 for (snapshot_output_node
=
1157 xmlFirstElementChild(snapshot_output_list_node
);
1158 snapshot_output_node
; snapshot_output_node
=
1159 xmlNextElementSibling(snapshot_output_node
)) {
1161 uint64_t max_size
= UINT64_MAX
;
1162 struct consumer_output output
= { 0 };
1163 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1165 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1166 node
= xmlNextElementSibling(node
)) {
1167 if (!strcmp((const char *) node
->name
,
1168 config_element_name
)) {
1170 name
= (char *) xmlNodeGetContent(node
);
1172 ret
= -LTTNG_ERR_NOMEM
;
1173 goto error_snapshot_output
;
1175 } else if (!strcmp((const char *) node
->name
,
1176 config_element_max_size
)) {
1177 xmlChar
*content
= xmlNodeGetContent(node
);
1181 ret
= -LTTNG_ERR_NOMEM
;
1182 goto error_snapshot_output
;
1184 ret
= parse_uint(content
, &max_size
);
1187 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1188 goto error_snapshot_output
;
1191 /* consumer_output */
1192 ret
= process_consumer_output(node
, &output
);
1194 goto error_snapshot_output
;
1199 snapshot_output
= lttng_snapshot_output_create();
1200 if (!snapshot_output
) {
1201 ret
= -LTTNG_ERR_NOMEM
;
1202 goto error_snapshot_output
;
1205 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1207 goto error_snapshot_output
;
1210 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1212 goto error_snapshot_output
;
1216 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1219 goto error_snapshot_output
;
1222 if (output
.control_uri
) {
1223 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1226 goto error_snapshot_output
;
1230 if (output
.data_uri
) {
1231 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1234 goto error_snapshot_output
;
1239 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1240 error_snapshot_output
:
1243 free(output
.control_uri
);
1244 free(output
.data_uri
);
1245 lttng_snapshot_output_destroy(snapshot_output
);
1255 int create_session(const char *name
,
1256 struct lttng_domain
*kernel_domain
,
1257 struct lttng_domain
*ust_domain
,
1258 struct lttng_domain
*jul_domain
,
1259 struct lttng_domain
*log4j_domain
,
1260 xmlNodePtr output_node
,
1261 uint64_t live_timer_interval
)
1264 struct consumer_output output
= { 0 };
1265 xmlNodePtr consumer_output_node
;
1270 consumer_output_node
= xmlFirstElementChild(output_node
);
1271 if (!consumer_output_node
) {
1272 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1276 if (strcmp((const char *) consumer_output_node
->name
,
1277 config_element_consumer_output
)) {
1278 WARN("Invalid output type, expected %s node",
1279 config_element_consumer_output
);
1280 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1284 ret
= process_consumer_output(consumer_output_node
, &output
);
1290 if (live_timer_interval
!= UINT64_MAX
&&
1291 !output
.control_uri
&& !output
.data_uri
) {
1292 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1296 if (output
.control_uri
|| output
.data_uri
) {
1298 struct lttng_domain
*domain
;
1299 struct lttng_domain
*domains
[] =
1300 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1302 /* network destination */
1303 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1305 * URLs are provided for sure since the test above make sure that
1306 * with a live timer the data and control URIs are provided. So,
1307 * NULL is passed here and will be set right after.
1309 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1311 ret
= lttng_create_session(name
, NULL
);
1317 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1318 domain
= domains
[i
];
1323 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1330 /* either local output or no output */
1331 ret
= lttng_create_session(name
, output
.path
);
1338 free(output
.control_uri
);
1339 free(output
.data_uri
);
1343 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1344 struct lttng_event_probe_attr
*attr
)
1348 assert(probe_attribute_node
);
1351 if (!strcmp((const char *) probe_attribute_node
->name
,
1352 config_element_address
)) {
1357 content
= xmlNodeGetContent(probe_attribute_node
);
1359 ret
= -LTTNG_ERR_NOMEM
;
1363 ret
= parse_uint(content
, &addr
);
1366 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1371 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1372 config_element_offset
)) {
1374 uint64_t offset
= 0;
1377 content
= xmlNodeGetContent(probe_attribute_node
);
1379 ret
= -LTTNG_ERR_NOMEM
;
1383 ret
= parse_uint(content
, &offset
);
1386 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1390 attr
->offset
= offset
;
1391 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1392 config_element_symbol_name
)) {
1397 content
= xmlNodeGetContent(probe_attribute_node
);
1399 ret
= -LTTNG_ERR_NOMEM
;
1403 name_len
= strlen((char *) content
);
1404 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1405 WARN("symbol_name too long.");
1406 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1411 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1420 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1421 const char *channel_name
, const enum process_event_node_phase phase
)
1425 struct lttng_event event
;
1426 char **exclusions
= NULL
;
1427 unsigned long exclusion_count
= 0;
1428 char *filter_expression
= NULL
;
1432 assert(channel_name
);
1434 memset(&event
, 0, sizeof(event
));
1436 /* Initialize default log level which varies by domain */
1437 switch (handle
->domain
.type
)
1439 case LTTNG_DOMAIN_JUL
:
1440 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1442 case LTTNG_DOMAIN_LOG4J
:
1443 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1445 case LTTNG_DOMAIN_PYTHON
:
1446 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1448 case LTTNG_DOMAIN_UST
:
1449 case LTTNG_DOMAIN_KERNEL
:
1450 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1456 for (node
= xmlFirstElementChild(event_node
); node
;
1457 node
= xmlNextElementSibling(node
)) {
1458 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1463 content
= xmlNodeGetContent(node
);
1465 ret
= -LTTNG_ERR_NOMEM
;
1469 name_len
= strlen((char *) content
);
1470 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1471 WARN("Channel name too long.");
1472 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1477 strncpy(event
.name
, (const char *) content
, name_len
);
1479 } else if (!strcmp((const char *) node
->name
,
1480 config_element_enabled
)) {
1481 xmlChar
*content
= xmlNodeGetContent(node
);
1485 ret
= -LTTNG_ERR_NOMEM
;
1489 ret
= parse_bool(content
, &event
.enabled
);
1492 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1495 } else if (!strcmp((const char *) node
->name
,
1496 config_element_type
)) {
1497 xmlChar
*content
= xmlNodeGetContent(node
);
1501 ret
= -LTTNG_ERR_NOMEM
;
1505 ret
= get_event_type(content
);
1508 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1513 } else if (!strcmp((const char *) node
->name
,
1514 config_element_loglevel_type
)) {
1515 xmlChar
*content
= xmlNodeGetContent(node
);
1519 ret
= -LTTNG_ERR_NOMEM
;
1523 ret
= get_loglevel_type(content
);
1526 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1530 event
.loglevel_type
= ret
;
1531 } else if (!strcmp((const char *) node
->name
,
1532 config_element_loglevel
)) {
1534 int64_t loglevel
= 0;
1537 content
= xmlNodeGetContent(node
);
1539 ret
= -LTTNG_ERR_NOMEM
;
1543 ret
= parse_int(content
, &loglevel
);
1546 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1550 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1551 WARN("loglevel out of range.");
1552 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1556 event
.loglevel
= loglevel
;
1557 } else if (!strcmp((const char *) node
->name
,
1558 config_element_filter
)) {
1560 xmlNodeGetContent(node
);
1564 ret
= -LTTNG_ERR_NOMEM
;
1568 free(filter_expression
);
1569 filter_expression
= strdup((char *) content
);
1571 if (!filter_expression
) {
1572 ret
= -LTTNG_ERR_NOMEM
;
1575 } else if (!strcmp((const char *) node
->name
,
1576 config_element_exclusions
)) {
1577 xmlNodePtr exclusion_node
;
1578 int exclusion_index
= 0;
1583 * Exclusions has already been initialized,
1586 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1590 exclusion_count
= xmlChildElementCount(node
);
1591 if (!exclusion_count
) {
1595 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1597 exclusion_count
= 0;
1598 ret
= -LTTNG_ERR_NOMEM
;
1602 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1603 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1605 xmlNodeGetContent(exclusion_node
);
1608 ret
= -LTTNG_ERR_NOMEM
;
1612 exclusions
[exclusion_index
] = strdup((const char *) content
);
1614 if (!exclusions
[exclusion_index
]) {
1615 ret
= -LTTNG_ERR_NOMEM
;
1621 event
.exclusion
= 1;
1622 } else if (!strcmp((const char *) node
->name
,
1623 config_element_attributes
)) {
1624 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1627 if (!attribute_node
) {
1628 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1632 if (!strcmp((const char *) node
->name
,
1633 config_element_probe_attributes
)) {
1634 xmlNodePtr probe_attribute_node
;
1636 /* probe_attributes */
1637 for (probe_attribute_node
=
1638 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1639 probe_attribute_node
= xmlNextElementSibling(
1640 probe_attribute_node
)) {
1642 ret
= process_probe_attribute_node(probe_attribute_node
,
1651 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1653 /* function_attributes */
1654 content
= xmlNodeGetContent(symbol_node
);
1656 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1660 sym_len
= strlen((char *) content
);
1661 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1662 WARN("Function name too long.");
1663 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1668 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1675 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1676 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1677 filter_expression
, exclusion_count
, exclusions
);
1679 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1680 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1685 for (i
= 0; i
< exclusion_count
; i
++) {
1686 free(exclusions
[i
]);
1690 free(filter_expression
);
1695 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1696 const char *channel_name
)
1699 struct lttng_event event
;
1702 assert(events_node
);
1704 assert(channel_name
);
1706 for (node
= xmlFirstElementChild(events_node
); node
;
1707 node
= xmlNextElementSibling(node
)) {
1708 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1715 * Disable all events to enable only the necessary events.
1716 * Limitations regarding lttng_disable_events and tuple descriptor
1717 * force this approach.
1719 memset(&event
, 0, sizeof(event
));
1720 event
.loglevel
= -1;
1721 event
.type
= LTTNG_EVENT_ALL
;
1722 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1727 for (node
= xmlFirstElementChild(events_node
); node
;
1728 node
= xmlNextElementSibling(node
)) {
1729 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1740 int process_channel_attr_node(xmlNodePtr attr_node
,
1741 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1742 xmlNodePtr
*events_node
)
1748 assert(contexts_node
);
1749 assert(events_node
);
1751 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1756 content
= xmlNodeGetContent(attr_node
);
1758 ret
= -LTTNG_ERR_NOMEM
;
1762 name_len
= strlen((char *) content
);
1763 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1764 WARN("Channel name too long.");
1765 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1770 strncpy(channel
->name
, (const char *) content
, name_len
);
1772 } else if (!strcmp((const char *) attr_node
->name
,
1773 config_element_enabled
)) {
1778 content
= xmlNodeGetContent(attr_node
);
1780 ret
= -LTTNG_ERR_NOMEM
;
1784 ret
= parse_bool(content
, &enabled
);
1787 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1791 channel
->enabled
= enabled
;
1792 } else if (!strcmp((const char *) attr_node
->name
,
1793 config_element_overwrite_mode
)) {
1796 /* overwrite_mode */
1797 content
= xmlNodeGetContent(attr_node
);
1799 ret
= -LTTNG_ERR_NOMEM
;
1803 ret
= get_overwrite_mode(content
);
1806 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1810 channel
->attr
.overwrite
= ret
;
1811 } else if (!strcmp((const char *) attr_node
->name
,
1812 config_element_subbuf_size
)) {
1815 /* subbuffer_size */
1816 content
= xmlNodeGetContent(attr_node
);
1818 ret
= -LTTNG_ERR_NOMEM
;
1822 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1825 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1828 } else if (!strcmp((const char *) attr_node
->name
,
1829 config_element_num_subbuf
)) {
1832 /* subbuffer_count */
1833 content
= xmlNodeGetContent(attr_node
);
1835 ret
= -LTTNG_ERR_NOMEM
;
1839 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1842 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1845 } else if (!strcmp((const char *) attr_node
->name
,
1846 config_element_switch_timer_interval
)) {
1848 uint64_t switch_timer_interval
= 0;
1850 /* switch_timer_interval */
1851 content
= xmlNodeGetContent(attr_node
);
1853 ret
= -LTTNG_ERR_NOMEM
;
1857 ret
= parse_uint(content
, &switch_timer_interval
);
1860 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1864 if (switch_timer_interval
> UINT_MAX
) {
1865 WARN("switch_timer_interval out of range.");
1866 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1870 channel
->attr
.switch_timer_interval
=
1871 switch_timer_interval
;
1872 } else if (!strcmp((const char *) attr_node
->name
,
1873 config_element_read_timer_interval
)) {
1875 uint64_t read_timer_interval
= 0;
1877 /* read_timer_interval */
1878 content
= xmlNodeGetContent(attr_node
);
1880 ret
= -LTTNG_ERR_NOMEM
;
1884 ret
= parse_uint(content
, &read_timer_interval
);
1887 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1891 if (read_timer_interval
> UINT_MAX
) {
1892 WARN("read_timer_interval out of range.");
1893 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1897 channel
->attr
.read_timer_interval
=
1898 read_timer_interval
;
1899 } else if (!strcmp((const char *) attr_node
->name
,
1900 config_element_output_type
)) {
1904 content
= xmlNodeGetContent(attr_node
);
1906 ret
= -LTTNG_ERR_NOMEM
;
1910 ret
= get_output_type(content
);
1913 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1917 channel
->attr
.output
= ret
;
1918 } else if (!strcmp((const char *) attr_node
->name
,
1919 config_element_tracefile_size
)) {
1922 /* tracefile_size */
1923 content
= xmlNodeGetContent(attr_node
);
1925 ret
= -LTTNG_ERR_NOMEM
;
1929 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1932 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1935 } else if (!strcmp((const char *) attr_node
->name
,
1936 config_element_tracefile_count
)) {
1939 /* tracefile_count */
1940 content
= xmlNodeGetContent(attr_node
);
1942 ret
= -LTTNG_ERR_NOMEM
;
1946 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1949 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1952 } else if (!strcmp((const char *) attr_node
->name
,
1953 config_element_live_timer_interval
)) {
1955 uint64_t live_timer_interval
= 0;
1957 /* live_timer_interval */
1958 content
= xmlNodeGetContent(attr_node
);
1960 ret
= -LTTNG_ERR_NOMEM
;
1964 ret
= parse_uint(content
, &live_timer_interval
);
1967 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1971 if (live_timer_interval
> UINT_MAX
) {
1972 WARN("live_timer_interval out of range.");
1973 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1977 channel
->attr
.live_timer_interval
=
1978 live_timer_interval
;
1979 } else if (!strcmp((const char *) attr_node
->name
,
1980 config_element_events
)) {
1982 *events_node
= attr_node
;
1985 *contexts_node
= attr_node
;
1993 int process_context_node(xmlNodePtr context_node
,
1994 struct lttng_handle
*handle
, const char *channel_name
)
1997 struct lttng_event_context context
;
1998 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2001 assert(channel_name
);
2003 if (!context_child_node
) {
2004 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2008 memset(&context
, 0, sizeof(context
));
2010 if (!strcmp((const char *) context_child_node
->name
,
2011 config_element_type
)) {
2013 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2015 ret
= -LTTNG_ERR_NOMEM
;
2019 ret
= get_context_type(content
);
2022 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2028 xmlNodePtr perf_attr_node
;
2031 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2032 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2033 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2034 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2035 perf_attr_node
; perf_attr_node
=
2036 xmlNextElementSibling(perf_attr_node
)) {
2037 if (!strcmp((const char *) perf_attr_node
->name
,
2038 config_element_type
)) {
2043 content
= xmlNodeGetContent(perf_attr_node
);
2045 ret
= -LTTNG_ERR_NOMEM
;
2049 ret
= parse_uint(content
, &type
);
2052 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2056 if (type
> UINT32_MAX
) {
2057 WARN("perf context type out of range.");
2058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2062 context
.u
.perf_counter
.type
= type
;
2063 } else if (!strcmp((const char *) perf_attr_node
->name
,
2064 config_element_config
)) {
2066 uint64_t config
= 0;
2069 content
= xmlNodeGetContent(perf_attr_node
);
2071 ret
= -LTTNG_ERR_NOMEM
;
2075 ret
= parse_uint(content
, &config
);
2078 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2082 context
.u
.perf_counter
.config
= config
;
2083 } else if (!strcmp((const char *) perf_attr_node
->name
,
2084 config_element_name
)) {
2089 content
= xmlNodeGetContent(perf_attr_node
);
2091 ret
= -LTTNG_ERR_NOMEM
;
2095 name_len
= strlen((char *) content
);
2096 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2097 WARN("perf context name too long.");
2098 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2103 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2110 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2116 int process_contexts_node(xmlNodePtr contexts_node
,
2117 struct lttng_handle
*handle
, const char *channel_name
)
2120 xmlNodePtr context_node
;
2122 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2123 context_node
= xmlNextElementSibling(context_node
)) {
2124 ret
= process_context_node(context_node
, handle
, channel_name
);
2134 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2135 struct lttng_handle
*handle
)
2138 xmlNodePtr targets_node
= NULL
;
2142 assert(pid_tracker_node
);
2143 /* get the targets node */
2144 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2145 node
= xmlNextElementSibling(node
)) {
2146 if (!strcmp((const char *) node
->name
,
2147 config_element_targets
)) {
2148 targets_node
= node
;
2153 if (!targets_node
) {
2154 ret
= LTTNG_ERR_INVALID
;
2158 /* Go through all pid_target node */
2159 child
= xmlChildElementCount(targets_node
);
2161 /* The session is explicitly set to target nothing. */
2162 ret
= lttng_untrack_pid(handle
, -1);
2167 for (node
= xmlFirstElementChild(targets_node
); node
;
2168 node
= xmlNextElementSibling(node
)) {
2169 xmlNodePtr pid_target_node
= node
;
2171 /* get pid node and track it */
2172 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2173 node
= xmlNextElementSibling(node
)) {
2174 if (!strcmp((const char *) node
->name
,
2175 config_element_pid
)) {
2177 xmlChar
*content
= NULL
;
2179 content
= xmlNodeGetContent(node
);
2181 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2185 ret
= parse_int(content
, &pid
);
2188 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2192 ret
= lttng_track_pid(handle
, (int) pid
);
2198 node
= pid_target_node
;
2207 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2210 struct lttng_domain domain
= { 0 };
2211 struct lttng_handle
*handle
= NULL
;
2212 xmlNodePtr channels_node
= NULL
;
2213 xmlNodePtr trackers_node
= NULL
;
2214 xmlNodePtr pid_tracker_node
= NULL
;
2217 assert(session_name
);
2219 ret
= init_domain(domain_node
, &domain
);
2224 handle
= lttng_create_handle(session_name
, &domain
);
2226 ret
= -LTTNG_ERR_NOMEM
;
2230 /* get the channels node */
2231 for (node
= xmlFirstElementChild(domain_node
); node
;
2232 node
= xmlNextElementSibling(node
)) {
2233 if (!strcmp((const char *) node
->name
,
2234 config_element_channels
)) {
2235 channels_node
= node
;
2240 if (!channels_node
) {
2244 /* create all channels */
2245 for (node
= xmlFirstElementChild(channels_node
); node
;
2246 node
= xmlNextElementSibling(node
)) {
2247 struct lttng_channel channel
;
2248 xmlNodePtr contexts_node
= NULL
;
2249 xmlNodePtr events_node
= NULL
;
2250 xmlNodePtr channel_attr_node
;
2252 memset(&channel
, 0, sizeof(channel
));
2253 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2255 for (channel_attr_node
= xmlFirstElementChild(node
);
2256 channel_attr_node
; channel_attr_node
=
2257 xmlNextElementSibling(channel_attr_node
)) {
2258 ret
= process_channel_attr_node(channel_attr_node
,
2259 &channel
, &contexts_node
, &events_node
);
2265 ret
= lttng_enable_channel(handle
, &channel
);
2270 ret
= process_events_node(events_node
, handle
, channel
.name
);
2275 ret
= process_contexts_node(contexts_node
, handle
,
2282 /* get the trackers node */
2283 for (node
= xmlFirstElementChild(domain_node
); node
;
2284 node
= xmlNextElementSibling(node
)) {
2285 if (!strcmp((const char *) node
->name
,
2286 config_element_trackers
)) {
2287 trackers_node
= node
;
2292 if (!trackers_node
) {
2296 for (node
= xmlFirstElementChild(trackers_node
); node
;
2297 node
= xmlNextElementSibling(node
)) {
2298 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2299 pid_tracker_node
= node
;
2300 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2307 if (!pid_tracker_node
) {
2308 lttng_track_pid(handle
, -1);
2312 lttng_destroy_handle(handle
);
2317 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2320 int ret
, started
= -1, snapshot_mode
= -1;
2321 uint64_t live_timer_interval
= UINT64_MAX
;
2322 xmlChar
*name
= NULL
;
2323 xmlChar
*shm_path
= NULL
;
2324 xmlNodePtr domains_node
= NULL
;
2325 xmlNodePtr output_node
= NULL
;
2327 struct lttng_domain
*kernel_domain
= NULL
;
2328 struct lttng_domain
*ust_domain
= NULL
;
2329 struct lttng_domain
*jul_domain
= NULL
;
2330 struct lttng_domain
*log4j_domain
= NULL
;
2331 struct lttng_domain
*python_domain
= NULL
;
2333 for (node
= xmlFirstElementChild(session_node
); node
;
2334 node
= xmlNextElementSibling(node
)) {
2335 if (!name
&& !strcmp((const char *) node
->name
,
2336 config_element_name
)) {
2338 xmlChar
*node_content
= xmlNodeGetContent(node
);
2339 if (!node_content
) {
2340 ret
= -LTTNG_ERR_NOMEM
;
2344 name
= node_content
;
2345 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2346 config_element_domains
)) {
2348 domains_node
= node
;
2349 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2350 config_element_started
)) {
2352 xmlChar
*node_content
= xmlNodeGetContent(node
);
2353 if (!node_content
) {
2354 ret
= -LTTNG_ERR_NOMEM
;
2358 ret
= parse_bool(node_content
, &started
);
2361 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2364 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2365 config_element_output
)) {
2368 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2369 config_element_shared_memory_path
)) {
2370 /* shared memory path */
2371 xmlChar
*node_content
= xmlNodeGetContent(node
);
2372 if (!node_content
) {
2373 ret
= -LTTNG_ERR_NOMEM
;
2377 shm_path
= node_content
;
2379 /* attributes, snapshot_mode or live_timer_interval */
2380 xmlNodePtr attributes_child
=
2381 xmlFirstElementChild(node
);
2383 if (!strcmp((const char *) attributes_child
->name
,
2384 config_element_snapshot_mode
)) {
2386 xmlChar
*snapshot_mode_content
=
2387 xmlNodeGetContent(attributes_child
);
2388 if (!snapshot_mode_content
) {
2389 ret
= -LTTNG_ERR_NOMEM
;
2393 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2394 free(snapshot_mode_content
);
2396 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2400 /* live_timer_interval */
2401 xmlChar
*timer_interval_content
=
2402 xmlNodeGetContent(attributes_child
);
2403 if (!timer_interval_content
) {
2404 ret
= -LTTNG_ERR_NOMEM
;
2408 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2409 free(timer_interval_content
);
2411 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2419 /* Mandatory attribute, as defined in the session XSD */
2420 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2424 if (session_name
&& strcmp((char *) name
, session_name
)) {
2425 /* This is not the session we are looking for */
2426 ret
= -LTTNG_ERR_NO_SESSION
;
2430 /* Init domains to create the session handles */
2431 for (node
= xmlFirstElementChild(domains_node
); node
;
2432 node
= xmlNextElementSibling(node
)) {
2433 struct lttng_domain
*domain
;
2435 domain
= zmalloc(sizeof(*domain
));
2437 ret
= -LTTNG_ERR_NOMEM
;
2441 ret
= init_domain(node
, domain
);
2443 goto domain_init_error
;
2446 switch (domain
->type
) {
2447 case LTTNG_DOMAIN_KERNEL
:
2448 if (kernel_domain
) {
2449 /* Same domain seen twice, invalid! */
2450 goto domain_init_error
;
2452 kernel_domain
= domain
;
2454 case LTTNG_DOMAIN_UST
:
2456 /* Same domain seen twice, invalid! */
2457 goto domain_init_error
;
2459 ust_domain
= domain
;
2461 case LTTNG_DOMAIN_JUL
:
2463 /* Same domain seen twice, invalid! */
2464 goto domain_init_error
;
2466 jul_domain
= domain
;
2468 case LTTNG_DOMAIN_LOG4J
:
2470 /* Same domain seen twice, invalid! */
2471 goto domain_init_error
;
2473 log4j_domain
= domain
;
2475 case LTTNG_DOMAIN_PYTHON
:
2476 if (python_domain
) {
2477 /* Same domain seen twice, invalid! */
2478 goto domain_init_error
;
2480 python_domain
= domain
;
2483 WARN("Invalid domain type");
2484 goto domain_init_error
;
2489 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2494 /* Destroy session if it exists */
2495 ret
= lttng_destroy_session((const char *) name
);
2496 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2497 ERR("Failed to destroy existing session.");
2502 /* Create session type depending on output type */
2503 if (snapshot_mode
&& snapshot_mode
!= -1) {
2504 ret
= create_snapshot_session((const char *) name
, output_node
);
2505 } else if (live_timer_interval
&&
2506 live_timer_interval
!= UINT64_MAX
) {
2507 ret
= create_session((const char *) name
, kernel_domain
,
2508 ust_domain
, jul_domain
, log4j_domain
,
2509 output_node
, live_timer_interval
);
2511 /* regular session */
2512 ret
= create_session((const char *) name
, kernel_domain
,
2513 ust_domain
, jul_domain
, log4j_domain
,
2514 output_node
, UINT64_MAX
);
2521 ret
= lttng_set_session_shm_path((const char *) name
,
2522 (const char *) shm_path
);
2528 for (node
= xmlFirstElementChild(domains_node
); node
;
2529 node
= xmlNextElementSibling(node
)) {
2530 ret
= process_domain_node(node
, (const char *) name
);
2537 ret
= lttng_start_tracing((const char *) name
);
2545 ERR("Failed to load session %s: %s", (const char *) name
,
2546 lttng_strerror(ret
));
2547 lttng_destroy_session((const char *) name
);
2551 free(kernel_domain
);
2555 free(python_domain
);
2562 * Return 1 if the given path is readable by the current UID or 0 if not.
2563 * Return -1 if the path is EPERM.
2565 static int validate_file_read_creds(const char *path
)
2571 /* Can we read the file. */
2572 ret
= access(path
, R_OK
);
2576 if (errno
== EACCES
) {
2587 int load_session_from_file(const char *path
, const char *session_name
,
2588 struct session_config_validation_ctx
*validation_ctx
, int override
)
2590 int ret
, session_found
= !session_name
;
2591 xmlDocPtr doc
= NULL
;
2592 xmlNodePtr sessions_node
;
2593 xmlNodePtr session_node
;
2596 assert(validation_ctx
);
2598 ret
= validate_file_read_creds(path
);
2601 ret
= -LTTNG_ERR_EPERM
;
2603 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2608 doc
= xmlParseFile(path
);
2610 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2614 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2616 ERR("Session configuration file validation failed");
2617 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2621 sessions_node
= xmlDocGetRootElement(doc
);
2622 if (!sessions_node
) {
2626 for (session_node
= xmlFirstElementChild(sessions_node
);
2627 session_node
; session_node
=
2628 xmlNextElementSibling(session_node
)) {
2629 ret
= process_session_node(session_node
,
2630 session_name
, override
);
2631 if (session_name
&& ret
== 0) {
2632 /* Target session found and loaded */
2640 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2645 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2647 struct dirent
*alloc_dirent(const char *path
)
2651 struct dirent
*entry
;
2653 name_max
= pathconf(path
, _PC_NAME_MAX
);
2654 if (name_max
== -1) {
2655 name_max
= PATH_MAX
;
2657 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2658 entry
= zmalloc(len
);
2663 int load_session_from_path(const char *path
, const char *session_name
,
2664 struct session_config_validation_ctx
*validation_ctx
, int override
)
2666 int ret
, session_found
= !session_name
;
2667 DIR *directory
= NULL
;
2670 assert(validation_ctx
);
2672 directory
= opendir(path
);
2676 /* Try the file loading. */
2679 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2682 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2687 struct dirent
*entry
;
2688 struct dirent
*result
;
2689 char *file_path
= NULL
;
2690 size_t path_len
= strlen(path
);
2692 if (path_len
>= PATH_MAX
) {
2693 ret
= -LTTNG_ERR_INVALID
;
2697 entry
= alloc_dirent(path
);
2699 ret
= -LTTNG_ERR_NOMEM
;
2703 file_path
= zmalloc(PATH_MAX
);
2705 ret
= -LTTNG_ERR_NOMEM
;
2710 strncpy(file_path
, path
, path_len
);
2711 if (file_path
[path_len
- 1] != '/') {
2712 file_path
[path_len
++] = '/';
2716 /* Search for *.lttng files */
2717 while (!readdir_r(directory
, entry
, &result
) && result
) {
2718 size_t file_name_len
= strlen(result
->d_name
);
2720 if (file_name_len
<=
2721 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2725 if (path_len
+ file_name_len
>= PATH_MAX
) {
2729 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2730 result
->d_name
+ file_name_len
- sizeof(
2731 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2735 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2736 file_path
[path_len
+ file_name_len
] = '\0';
2738 ret
= load_session_from_file(file_path
, session_name
,
2739 validation_ctx
, override
);
2740 if (session_name
&& !ret
) {
2749 ret
= load_session_from_file(path
, session_name
,
2750 validation_ctx
, override
);
2760 if (closedir(directory
)) {
2765 if (session_found
) {
2773 * Validate that the given path's credentials and the current process have the
2774 * same UID. If so, return 1 else return 0 if it does NOT match.
2776 static int validate_path_creds(const char *path
)
2778 int ret
, uid
= getuid();
2787 ret
= stat(path
, &buf
);
2789 if (errno
!= ENOENT
) {
2792 ret
= -LTTNG_ERR_INVALID
;
2796 if (buf
.st_uid
!= uid
) {
2807 int config_load_session(const char *path
, const char *session_name
,
2808 int override
, unsigned int autoload
)
2811 bool session_loaded
= false;
2812 const char *path_ptr
= NULL
;
2813 struct session_config_validation_ctx validation_ctx
= { 0 };
2815 ret
= init_session_config_validation_ctx(&validation_ctx
);
2822 const char *sys_path
;
2825 home_path
= utils_get_home_dir();
2827 char path
[PATH_MAX
];
2830 * Try user session configuration path. Ignore error here so we can
2831 * continue loading the system wide sessions.
2834 ret
= snprintf(path
, sizeof(path
),
2835 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2836 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2838 PERROR("snprintf session autoload home config path");
2843 * Credentials are only validated for the autoload in order to
2844 * avoid any user session daemon to try to load kernel sessions
2845 * automatically and failing all the times.
2847 ret
= validate_path_creds(path
);
2852 ret
= snprintf(path
, sizeof(path
),
2853 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2855 PERROR("snprintf session home config path");
2861 ret
= load_session_from_path(path_ptr
, session_name
,
2862 &validation_ctx
, override
);
2863 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2867 * Continue even if the session was found since we have to try
2868 * the system wide sessions.
2870 session_loaded
= true;
2874 /* Reset path pointer for the system wide dir. */
2877 /* Try system wide configuration directory. */
2879 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2880 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2881 ret
= validate_path_creds(sys_path
);
2883 path_ptr
= sys_path
;
2886 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2887 path_ptr
= sys_path
;
2891 ret
= load_session_from_path(path_ptr
, session_name
,
2892 &validation_ctx
, override
);
2894 session_loaded
= true;
2898 ret
= access(path
, F_OK
);
2903 ret
= -LTTNG_ERR_INVALID
;
2904 WARN("Session configuration path does not exist.");
2907 ret
= -LTTNG_ERR_EPERM
;
2910 ret
= -LTTNG_ERR_UNK
;
2916 ret
= load_session_from_path(path
, session_name
,
2917 &validation_ctx
, override
);
2920 fini_session_config_validation_ctx(&validation_ctx
);
2921 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2923 * Don't report an error if no sessions are found when called
2924 * without a session_name or a search path.
2929 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2930 /* A matching session was found in one of the search paths. */