2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <common/compat/getenv.h>
36 #include <lttng/lttng-error.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlschemas.h>
40 #include <libxml/tree.h>
41 #include <libxml/xpath.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.h>
45 #include "session-config.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 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
101 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
102 const char * const config_element_type
= "type";
103 const char * const config_element_buffer_type
= "buffer_type";
104 const char * const config_element_session
= "session";
105 const char * const config_element_sessions
= "sessions";
106 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
107 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
108 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
109 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
110 const char * const config_element_config
= "config";
111 const char * const config_element_started
= "started";
112 const char * const config_element_snapshot_mode
= "snapshot_mode";
113 const char * const config_element_loglevel
= "loglevel";
114 const char * const config_element_loglevel_type
= "loglevel_type";
115 const char * const config_element_filter
= "filter";
116 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
117 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
118 const char * const config_element_consumer_output
= "consumer_output";
119 const char * const config_element_destination
= "destination";
120 const char * const config_element_path
= "path";
121 const char * const config_element_net_output
= "net_output";
122 const char * const config_element_control_uri
= "control_uri";
123 const char * const config_element_data_uri
= "data_uri";
124 const char * const config_element_max_size
= "max_size";
125 const char * const config_element_pid
= "pid";
126 const char * const config_element_pids
= "pids";
127 const char * const config_element_shared_memory_path
= "shared_memory_path";
128 const char * const config_element_pid_tracker
= "pid_tracker";
129 const char * const config_element_trackers
= "trackers";
130 const char * const config_element_targets
= "targets";
131 const char * const config_element_target_pid
= "pid_target";
132 const char * const config_element_omit_name
= "omit_name";
133 const char * const config_element_omit_output
= "omit_output";
135 const char * const config_domain_type_kernel
= "KERNEL";
136 const char * const config_domain_type_ust
= "UST";
137 const char * const config_domain_type_jul
= "JUL";
138 const char * const config_domain_type_log4j
= "LOG4J";
139 const char * const config_domain_type_python
= "PYTHON";
141 const char * const config_buffer_type_per_pid
= "PER_PID";
142 const char * const config_buffer_type_per_uid
= "PER_UID";
143 const char * const config_buffer_type_global
= "GLOBAL";
145 const char * const config_overwrite_mode_discard
= "DISCARD";
146 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
148 const char * const config_output_type_splice
= "SPLICE";
149 const char * const config_output_type_mmap
= "MMAP";
151 const char * const config_loglevel_type_all
= "ALL";
152 const char * const config_loglevel_type_range
= "RANGE";
153 const char * const config_loglevel_type_single
= "SINGLE";
155 const char * const config_event_type_all
= "ALL";
156 const char * const config_event_type_tracepoint
= "TRACEPOINT";
157 const char * const config_event_type_probe
= "PROBE";
158 const char * const config_event_type_function
= "FUNCTION";
159 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
160 const char * const config_event_type_noop
= "NOOP";
161 const char * const config_event_type_syscall
= "SYSCALL";
162 const char * const config_event_type_kprobe
= "KPROBE";
163 const char * const config_event_type_kretprobe
= "KRETPROBE";
165 const char * const config_event_context_pid
= "PID";
166 const char * const config_event_context_procname
= "PROCNAME";
167 const char * const config_event_context_prio
= "PRIO";
168 const char * const config_event_context_nice
= "NICE";
169 const char * const config_event_context_vpid
= "VPID";
170 const char * const config_event_context_tid
= "TID";
171 const char * const config_event_context_vtid
= "VTID";
172 const char * const config_event_context_ppid
= "PPID";
173 const char * const config_event_context_vppid
= "VPPID";
174 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
175 const char * const config_event_context_hostname
= "HOSTNAME";
176 const char * const config_event_context_ip
= "IP";
177 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
178 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
179 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
180 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
181 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
182 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
184 /* Deprecated symbols */
185 const char * const config_element_perf
;
187 enum process_event_node_phase
{
192 struct consumer_output
{
199 static int config_entry_handler_filter(struct handler_filter_args
*args
,
200 const char *section
, const char *name
, const char *value
)
203 struct config_entry entry
= { section
, name
, value
};
207 if (!section
|| !name
|| !value
) {
213 if (strcmp(args
->section
, section
)) {
218 ret
= args
->handler(&entry
, args
->user_data
);
224 const char *config_get_domain_str(enum lttng_domain_type domain
)
229 case LTTNG_DOMAIN_KERNEL
:
230 str_dom
= config_domain_type_kernel
;
232 case LTTNG_DOMAIN_UST
:
233 str_dom
= config_domain_type_ust
;
235 case LTTNG_DOMAIN_JUL
:
236 str_dom
= config_domain_type_jul
;
238 case LTTNG_DOMAIN_LOG4J
:
239 str_dom
= config_domain_type_log4j
;
241 case LTTNG_DOMAIN_PYTHON
:
242 str_dom
= config_domain_type_python
;
252 const char *config_get_loglevel_type_string(
253 enum lttng_loglevel_type loglevel_type
)
255 const char *loglevel_type_string
;
257 switch (loglevel_type
) {
258 case LTTNG_EVENT_LOGLEVEL_ALL
:
259 loglevel_type_string
= config_loglevel_type_all
;
261 case LTTNG_EVENT_LOGLEVEL_RANGE
:
262 loglevel_type_string
= config_loglevel_type_range
;
264 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
265 loglevel_type_string
= config_loglevel_type_single
;
268 loglevel_type_string
= NULL
;
271 return loglevel_type_string
;
275 int config_get_section_entries(const char *override_path
, const char *section
,
276 config_entry_handler_cb handler
, void *user_data
)
280 FILE *config_file
= NULL
;
281 struct handler_filter_args filter
= { section
, handler
, user_data
};
283 /* First, try system-wide conf. file. */
284 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
286 config_file
= fopen(path
, "r");
288 DBG("Loading daemon conf file at %s", path
);
290 * Return value is not very important here since error or not, we
291 * continue and try the next possible conf. file.
293 (void) ini_parse_file(config_file
,
294 (ini_entry_handler
) config_entry_handler_filter
,
299 /* Second is the user local configuration. */
300 path
= utils_get_home_dir();
302 char fullpath
[PATH_MAX
];
304 ret
= snprintf(fullpath
, sizeof(fullpath
),
305 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
307 PERROR("snprintf user conf. path");
311 config_file
= fopen(fullpath
, "r");
313 DBG("Loading daemon user conf file at %s", path
);
315 * Return value is not very important here since error or not, we
316 * continue and try the next possible conf. file.
318 (void) ini_parse_file(config_file
,
319 (ini_entry_handler
) config_entry_handler_filter
,
325 /* Final path is the one that the user might have provided. */
327 config_file
= fopen(override_path
, "r");
329 DBG("Loading daemon command line conf file at %s", override_path
);
330 (void) ini_parse_file(config_file
,
331 (ini_entry_handler
) config_entry_handler_filter
,
335 ERR("Failed to open daemon configuration file at %s",
342 /* Everything went well. */
350 int config_parse_value(const char *value
)
353 char *endptr
, *lower_str
;
363 v
= strtoul(value
, &endptr
, 10);
364 if (endptr
!= value
) {
369 lower_str
= zmalloc(len
+ 1);
376 for (i
= 0; i
< len
; i
++) {
377 lower_str
[i
] = tolower(value
[i
]);
380 if (!strcmp(lower_str
, config_str_yes
) ||
381 !strcmp(lower_str
, config_str_true
) ||
382 !strcmp(lower_str
, config_str_on
)) {
384 } else if (!strcmp(lower_str
, config_str_no
) ||
385 !strcmp(lower_str
, config_str_false
) ||
386 !strcmp(lower_str
, config_str_off
)) {
398 * Returns a xmlChar string which must be released using xmlFree().
400 static xmlChar
*encode_string(const char *in_str
)
402 xmlChar
*out_str
= NULL
;
403 xmlCharEncodingHandlerPtr handler
;
404 int out_len
, ret
, in_len
;
408 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
410 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
414 in_len
= strlen(in_str
);
416 * Add 1 byte for the NULL terminted character. The factor 4 here is
417 * used because UTF-8 characters can take up to 4 bytes.
419 out_len
= (in_len
* 4) + 1;
420 out_str
= xmlMalloc(out_len
);
425 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
432 /* out_len is now the size of out_str */
433 out_str
[out_len
] = '\0';
439 struct config_writer
*config_writer_create(int fd_output
, int indent
)
442 struct config_writer
*writer
;
443 xmlOutputBufferPtr buffer
;
445 writer
= zmalloc(sizeof(struct config_writer
));
447 PERROR("zmalloc config_writer_create");
451 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
456 writer
->writer
= xmlNewTextWriter(buffer
);
457 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
458 config_xml_encoding
, NULL
);
463 ret
= xmlTextWriterSetIndentString(writer
->writer
,
464 BAD_CAST config_xml_indent_string
);
469 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
477 config_writer_destroy(writer
);
482 int config_writer_destroy(struct config_writer
*writer
)
491 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
492 WARN("Could not close XML document");
496 if (writer
->writer
) {
497 xmlFreeTextWriter(writer
->writer
);
506 int config_writer_open_element(struct config_writer
*writer
,
507 const char *element_name
)
510 xmlChar
*encoded_element_name
;
512 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
517 encoded_element_name
= encode_string(element_name
);
518 if (!encoded_element_name
) {
523 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
524 xmlFree(encoded_element_name
);
526 return ret
>= 0 ? 0 : ret
;
530 int config_writer_write_attribute(struct config_writer
*writer
,
531 const char *name
, const char *value
)
534 xmlChar
*encoded_name
= NULL
;
535 xmlChar
*encoded_value
= NULL
;
537 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
542 encoded_name
= encode_string(name
);
548 encoded_value
= encode_string(value
);
549 if (!encoded_value
) {
554 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
557 xmlFree(encoded_name
);
558 xmlFree(encoded_value
);
559 return ret
>= 0 ? 0 : ret
;
563 int config_writer_close_element(struct config_writer
*writer
)
567 if (!writer
|| !writer
->writer
) {
572 ret
= xmlTextWriterEndElement(writer
->writer
);
574 return ret
>= 0 ? 0 : ret
;
578 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
579 const char *element_name
, uint64_t value
)
582 xmlChar
*encoded_element_name
;
584 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
589 encoded_element_name
= encode_string(element_name
);
590 if (!encoded_element_name
) {
595 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
596 encoded_element_name
, "%" PRIu64
, value
);
597 xmlFree(encoded_element_name
);
599 return ret
>= 0 ? 0 : ret
;
603 int config_writer_write_element_signed_int(struct config_writer
*writer
,
604 const char *element_name
, int64_t value
)
607 xmlChar
*encoded_element_name
;
609 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
614 encoded_element_name
= encode_string(element_name
);
615 if (!encoded_element_name
) {
620 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
621 encoded_element_name
, "%" PRIi64
, value
);
622 xmlFree(encoded_element_name
);
624 return ret
>= 0 ? 0 : ret
;
628 int config_writer_write_element_bool(struct config_writer
*writer
,
629 const char *element_name
, int value
)
631 return config_writer_write_element_string(writer
, element_name
,
632 value
? config_xml_true
: config_xml_false
);
636 int config_writer_write_element_string(struct config_writer
*writer
,
637 const char *element_name
, const char *value
)
640 xmlChar
*encoded_element_name
= NULL
;
641 xmlChar
*encoded_value
= NULL
;
643 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
649 encoded_element_name
= encode_string(element_name
);
650 if (!encoded_element_name
) {
655 encoded_value
= encode_string(value
);
656 if (!encoded_value
) {
661 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
664 xmlFree(encoded_element_name
);
665 xmlFree(encoded_value
);
666 return ret
>= 0 ? 0 : ret
;
670 int config_writer_write_config_element(struct config_writer
*writer
,
671 const struct config_element
*element
)
675 xmlNodePtr local_copy
= NULL
;
676 xmlNodePtr place_holder
= NULL
;
677 xmlDocPtr document
= NULL
;
678 xmlBufferPtr buffer
= NULL
;
681 assert(element
->element
);
684 /* Create a new doc and set root node as passed element */
685 document
= xmlNewDoc(BAD_CAST
"1.0");
686 document
->parseFlags
|= XML_PARSE_NOBLANKS
;
688 ERR("Unable to create xml document");
693 local_copy
= xmlCopyNode(element
->element
, 1);
695 ERR("Unable to copy an xml node");
700 /* Hand off the local_copy to the xmlDoc */
701 place_holder
= xmlDocSetRootElement(document
, local_copy
);
703 ERR("The document root was already set");
704 xmlFreeNode(local_copy
);
709 buffer
= xmlBufferCreate();
715 ret
= xmlNodeDump(buffer
, document
, local_copy
, 0, 0);
716 xmlTextWriterWriteRaw(writer
->writer
, xmlBufferContent(buffer
));
718 xmlBufferFree(buffer
);
719 xmlFreeDoc(document
);
724 void xml_error_handler(void *ctx
, const char *format
, ...)
730 va_start(args
, format
);
731 ret
= vasprintf(&errMsg
, format
, args
);
734 ERR("String allocation failed in xml error handler");
738 fprintf(stderr
, "XML Error: %s", errMsg
);
743 void fini_session_config_validation_ctx(
744 struct session_config_validation_ctx
*ctx
)
746 if (ctx
->parser_ctx
) {
747 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
751 xmlSchemaFree(ctx
->schema
);
754 if (ctx
->schema_validation_ctx
) {
755 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
758 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
762 char *get_session_config_xsd_path()
765 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
766 size_t base_path_len
;
770 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
773 base_path_len
= strlen(base_path
);
774 max_path_len
= base_path_len
+
775 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
776 xsd_path
= zmalloc(max_path_len
);
781 strncpy(xsd_path
, base_path
, max_path_len
);
782 if (xsd_path
[base_path_len
- 1] != '/') {
783 xsd_path
[base_path_len
++] = '/';
786 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
787 max_path_len
- base_path_len
);
793 int init_session_config_validation_ctx(
794 struct session_config_validation_ctx
*ctx
)
797 char *xsd_path
= get_session_config_xsd_path();
800 ret
= -LTTNG_ERR_NOMEM
;
804 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
805 if (!ctx
->parser_ctx
) {
806 ERR("XSD parser context creation failed");
807 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
810 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
811 xml_error_handler
, NULL
);
813 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
815 ERR("XSD parsing failed");
816 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
820 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
821 if (!ctx
->schema_validation_ctx
) {
822 ERR("XSD validation context creation failed");
823 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
827 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
828 xml_error_handler
, NULL
);
833 fini_session_config_validation_ctx(ctx
);
841 int parse_uint(xmlChar
*str
, uint64_t *val
)
851 *val
= strtoull((const char *) str
, &endptr
, 10);
852 if (!endptr
|| *endptr
) {
863 int parse_int(xmlChar
*str
, int64_t *val
)
873 *val
= strtoll((const char *) str
, &endptr
, 10);
874 if (!endptr
|| *endptr
) {
885 int parse_bool(xmlChar
*str
, int *val
)
894 if (!strcmp((const char *) str
, config_xml_true
)) {
896 } else if (!strcmp((const char *) str
, config_xml_false
)) {
899 WARN("Invalid boolean value encoutered (%s).",
908 int get_domain_type(xmlChar
*domain
)
916 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
917 ret
= LTTNG_DOMAIN_KERNEL
;
918 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
919 ret
= LTTNG_DOMAIN_UST
;
920 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
921 ret
= LTTNG_DOMAIN_JUL
;
922 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
923 ret
= LTTNG_DOMAIN_LOG4J
;
924 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
925 ret
= LTTNG_DOMAIN_PYTHON
;
936 int get_buffer_type(xmlChar
*buffer_type
)
944 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
945 ret
= LTTNG_BUFFER_GLOBAL
;
946 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
947 ret
= LTTNG_BUFFER_PER_UID
;
948 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
949 ret
= LTTNG_BUFFER_PER_PID
;
960 int get_overwrite_mode(xmlChar
*overwrite_mode
)
964 if (!overwrite_mode
) {
968 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
970 } else if (!strcmp((char *) overwrite_mode
,
971 config_overwrite_mode_discard
)) {
983 int get_output_type(xmlChar
*output_type
)
991 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
992 ret
= LTTNG_EVENT_MMAP
;
993 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
994 ret
= LTTNG_EVENT_SPLICE
;
1005 int config_get_event_type(const char *event_type
)
1013 if (!strcmp(event_type
, config_event_type_all
)) {
1014 ret
= LTTNG_EVENT_ALL
;
1015 } else if (!strcmp(event_type
, config_event_type_tracepoint
)) {
1016 ret
= LTTNG_EVENT_TRACEPOINT
;
1017 } else if (!strcmp(event_type
, config_event_type_probe
)) {
1018 ret
= LTTNG_EVENT_PROBE
;
1019 } else if (!strcmp(event_type
, config_event_type_function
)) {
1020 ret
= LTTNG_EVENT_FUNCTION
;
1021 } else if (!strcmp(event_type
,
1022 config_event_type_function_entry
)) {
1023 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
1024 } else if (!strcmp(event_type
, config_event_type_noop
)) {
1025 ret
= LTTNG_EVENT_NOOP
;
1026 } else if (!strcmp(event_type
, config_event_type_syscall
)) {
1027 ret
= LTTNG_EVENT_SYSCALL
;
1038 int get_loglevel_type(xmlChar
*loglevel_type
)
1042 if (!loglevel_type
) {
1046 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
1047 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
1048 } else if (!strcmp((char *) loglevel_type
,
1049 config_loglevel_type_range
)) {
1050 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1051 } else if (!strcmp((char *) loglevel_type
,
1052 config_loglevel_type_single
)) {
1053 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1064 * Return the context type or -1 on error.
1067 int get_context_type(xmlChar
*context_type
)
1071 if (!context_type
) {
1075 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
1076 ret
= LTTNG_EVENT_CONTEXT_PID
;
1077 } else if (!strcmp((char *) context_type
,
1078 config_event_context_procname
)) {
1079 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1080 } else if (!strcmp((char *) context_type
,
1081 config_event_context_prio
)) {
1082 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
1083 } else if (!strcmp((char *) context_type
,
1084 config_event_context_nice
)) {
1085 ret
= LTTNG_EVENT_CONTEXT_NICE
;
1086 } else if (!strcmp((char *) context_type
,
1087 config_event_context_vpid
)) {
1088 ret
= LTTNG_EVENT_CONTEXT_VPID
;
1089 } else if (!strcmp((char *) context_type
,
1090 config_event_context_tid
)) {
1091 ret
= LTTNG_EVENT_CONTEXT_TID
;
1092 } else if (!strcmp((char *) context_type
,
1093 config_event_context_vtid
)) {
1094 ret
= LTTNG_EVENT_CONTEXT_VTID
;
1095 } else if (!strcmp((char *) context_type
,
1096 config_event_context_ppid
)) {
1097 ret
= LTTNG_EVENT_CONTEXT_PPID
;
1098 } else if (!strcmp((char *) context_type
,
1099 config_event_context_vppid
)) {
1100 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1101 } else if (!strcmp((char *) context_type
,
1102 config_event_context_pthread_id
)) {
1103 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1104 } else if (!strcmp((char *) context_type
,
1105 config_event_context_hostname
)) {
1106 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1107 } else if (!strcmp((char *) context_type
,
1108 config_event_context_ip
)) {
1109 ret
= LTTNG_EVENT_CONTEXT_IP
;
1110 } else if (!strcmp((char *) context_type
,
1111 config_event_context_interruptible
)) {
1112 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1113 } else if (!strcmp((char *) context_type
,
1114 config_event_context_preemptible
)) {
1115 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1116 } else if (!strcmp((char *) context_type
,
1117 config_event_context_need_reschedule
)) {
1118 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1119 } else if (!strcmp((char *) context_type
,
1120 config_event_context_migratable
)) {
1121 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1132 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1137 for (node
= xmlFirstElementChild(domain_node
); node
;
1138 node
= xmlNextElementSibling(node
)) {
1139 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1141 xmlChar
*node_content
= xmlNodeGetContent(node
);
1142 if (!node_content
) {
1143 ret
= -LTTNG_ERR_NOMEM
;
1147 ret
= get_domain_type(node_content
);
1150 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1155 } else if (!strcmp((const char *) node
->name
,
1156 config_element_buffer_type
)) {
1158 xmlChar
*node_content
= xmlNodeGetContent(node
);
1159 if (!node_content
) {
1160 ret
= -LTTNG_ERR_NOMEM
;
1164 ret
= get_buffer_type(node_content
);
1167 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1171 domain
->buf_type
= ret
;
1180 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1185 for (node
= xmlFirstElementChild(net_output_node
); node
;
1186 node
= xmlNextElementSibling(node
)) {
1187 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1189 *control_uri
= (char *) xmlNodeGetContent(node
);
1190 if (!*control_uri
) {
1195 *data_uri
= (char *) xmlNodeGetContent(node
);
1202 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1206 int process_consumer_output(xmlNodePtr consumer_output_node
,
1207 struct consumer_output
*output
)
1214 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1215 node
= xmlNextElementSibling(node
)) {
1216 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1217 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1221 ret
= -LTTNG_ERR_NOMEM
;
1225 ret
= parse_bool(enabled_str
, &output
->enabled
);
1231 xmlNodePtr output_type_node
;
1234 output_type_node
= xmlFirstElementChild(node
);
1235 if (!output_type_node
) {
1236 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1240 if (!strcmp((const char *) output_type_node
->name
,
1241 config_element_path
)) {
1243 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1244 if (!output
->path
) {
1245 ret
= -LTTNG_ERR_NOMEM
;
1250 ret
= get_net_output_uris(output_type_node
,
1251 &output
->control_uri
, &output
->data_uri
);
1263 free(output
->control_uri
);
1264 free(output
->data_uri
);
1265 memset(output
, 0, sizeof(struct consumer_output
));
1271 int create_session_net_output(const char *name
, const char *control_uri
,
1272 const char *data_uri
)
1275 struct lttng_handle
*handle
;
1276 const char *uri
= NULL
;
1280 handle
= lttng_create_handle(name
, NULL
);
1282 ret
= -LTTNG_ERR_NOMEM
;
1286 if (!control_uri
|| !data_uri
) {
1287 uri
= control_uri
? control_uri
: data_uri
;
1292 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1293 lttng_destroy_handle(handle
);
1299 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1302 xmlNodePtr node
= NULL
;
1303 xmlNodePtr snapshot_output_list_node
;
1304 xmlNodePtr snapshot_output_node
;
1306 assert(session_name
);
1308 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1317 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1319 /* Parse and create snapshot outputs */
1321 for (snapshot_output_node
=
1322 xmlFirstElementChild(snapshot_output_list_node
);
1323 snapshot_output_node
; snapshot_output_node
=
1324 xmlNextElementSibling(snapshot_output_node
)) {
1326 uint64_t max_size
= DEFAULT_SNAPSHOT_MAX_SIZE
;
1327 struct consumer_output output
= { 0 };
1328 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1330 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1331 node
= xmlNextElementSibling(node
)) {
1332 if (!strcmp((const char *) node
->name
,
1333 config_element_name
)) {
1335 name
= (char *) xmlNodeGetContent(node
);
1337 ret
= -LTTNG_ERR_NOMEM
;
1338 goto error_snapshot_output
;
1340 } else if (!strcmp((const char *) node
->name
,
1341 config_element_max_size
)) {
1342 xmlChar
*content
= xmlNodeGetContent(node
);
1346 ret
= -LTTNG_ERR_NOMEM
;
1347 goto error_snapshot_output
;
1349 ret
= parse_uint(content
, &max_size
);
1352 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1353 goto error_snapshot_output
;
1356 /* consumer_output */
1357 ret
= process_consumer_output(node
, &output
);
1359 goto error_snapshot_output
;
1364 snapshot_output
= lttng_snapshot_output_create();
1365 if (!snapshot_output
) {
1366 ret
= -LTTNG_ERR_NOMEM
;
1367 goto error_snapshot_output
;
1371 /* Generate a default name */
1373 pret
= asprintf(&name
, DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
1374 lttng_snapshot_output_get_id(snapshot_output
));
1377 PERROR("snprintf output name");
1378 goto error_snapshot_output
;
1382 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1384 goto error_snapshot_output
;
1387 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1389 goto error_snapshot_output
;
1393 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1396 goto error_snapshot_output
;
1399 if (output
.control_uri
) {
1400 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1403 goto error_snapshot_output
;
1407 if (output
.data_uri
) {
1408 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1411 goto error_snapshot_output
;
1416 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1417 error_snapshot_output
:
1420 free(output
.control_uri
);
1421 free(output
.data_uri
);
1422 lttng_snapshot_output_destroy(snapshot_output
);
1432 int create_session(const char *name
,
1433 struct lttng_domain
*kernel_domain
,
1434 struct lttng_domain
*ust_domain
,
1435 struct lttng_domain
*jul_domain
,
1436 struct lttng_domain
*log4j_domain
,
1437 xmlNodePtr output_node
,
1438 uint64_t live_timer_interval
)
1441 struct consumer_output output
= { 0 };
1442 xmlNodePtr consumer_output_node
;
1447 consumer_output_node
= xmlFirstElementChild(output_node
);
1448 if (!consumer_output_node
) {
1449 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1453 if (strcmp((const char *) consumer_output_node
->name
,
1454 config_element_consumer_output
)) {
1455 WARN("Invalid output type, expected %s node",
1456 config_element_consumer_output
);
1457 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1461 ret
= process_consumer_output(consumer_output_node
, &output
);
1467 if (live_timer_interval
!= UINT64_MAX
&&
1468 !output
.control_uri
&& !output
.data_uri
) {
1469 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1473 if (output
.control_uri
|| output
.data_uri
) {
1474 /* network destination */
1475 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1477 * URLs are provided for sure since the test above make sure that
1478 * with a live timer the data and control URIs are provided. So,
1479 * NULL is passed here and will be set right after.
1481 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1483 ret
= lttng_create_session(name
, NULL
);
1489 ret
= create_session_net_output(name
, output
.control_uri
,
1496 /* either local output or no output */
1497 ret
= lttng_create_session(name
, output
.path
);
1504 free(output
.control_uri
);
1505 free(output
.data_uri
);
1509 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1510 struct lttng_event_probe_attr
*attr
)
1514 assert(probe_attribute_node
);
1517 if (!strcmp((const char *) probe_attribute_node
->name
,
1518 config_element_address
)) {
1523 content
= xmlNodeGetContent(probe_attribute_node
);
1525 ret
= -LTTNG_ERR_NOMEM
;
1529 ret
= parse_uint(content
, &addr
);
1532 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1537 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1538 config_element_offset
)) {
1540 uint64_t offset
= 0;
1543 content
= xmlNodeGetContent(probe_attribute_node
);
1545 ret
= -LTTNG_ERR_NOMEM
;
1549 ret
= parse_uint(content
, &offset
);
1552 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1556 attr
->offset
= offset
;
1557 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1558 config_element_symbol_name
)) {
1563 content
= xmlNodeGetContent(probe_attribute_node
);
1565 ret
= -LTTNG_ERR_NOMEM
;
1569 name_len
= strlen((char *) content
);
1570 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1571 WARN("symbol_name too long.");
1572 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1577 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1586 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1587 const char *channel_name
, const enum process_event_node_phase phase
)
1591 struct lttng_event event
;
1592 char **exclusions
= NULL
;
1593 unsigned long exclusion_count
= 0;
1594 char *filter_expression
= NULL
;
1598 assert(channel_name
);
1600 memset(&event
, 0, sizeof(event
));
1602 /* Initialize default log level which varies by domain */
1603 switch (handle
->domain
.type
)
1605 case LTTNG_DOMAIN_JUL
:
1606 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1608 case LTTNG_DOMAIN_LOG4J
:
1609 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1611 case LTTNG_DOMAIN_PYTHON
:
1612 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1614 case LTTNG_DOMAIN_UST
:
1615 case LTTNG_DOMAIN_KERNEL
:
1616 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1622 for (node
= xmlFirstElementChild(event_node
); node
;
1623 node
= xmlNextElementSibling(node
)) {
1624 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1629 content
= xmlNodeGetContent(node
);
1631 ret
= -LTTNG_ERR_NOMEM
;
1635 name_len
= strlen((char *) content
);
1636 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1637 WARN("Channel name too long.");
1638 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1643 strncpy(event
.name
, (const char *) content
, name_len
);
1645 } else if (!strcmp((const char *) node
->name
,
1646 config_element_enabled
)) {
1647 xmlChar
*content
= xmlNodeGetContent(node
);
1651 ret
= -LTTNG_ERR_NOMEM
;
1655 ret
= parse_bool(content
, &event
.enabled
);
1658 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1661 } else if (!strcmp((const char *) node
->name
,
1662 config_element_type
)) {
1663 xmlChar
*content
= xmlNodeGetContent(node
);
1667 ret
= -LTTNG_ERR_NOMEM
;
1671 ret
= config_get_event_type((char *) content
);
1674 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1679 } else if (!strcmp((const char *) node
->name
,
1680 config_element_loglevel_type
)) {
1681 xmlChar
*content
= xmlNodeGetContent(node
);
1685 ret
= -LTTNG_ERR_NOMEM
;
1689 ret
= get_loglevel_type(content
);
1692 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1696 event
.loglevel_type
= ret
;
1697 } else if (!strcmp((const char *) node
->name
,
1698 config_element_loglevel
)) {
1700 int64_t loglevel
= 0;
1703 content
= xmlNodeGetContent(node
);
1705 ret
= -LTTNG_ERR_NOMEM
;
1709 ret
= parse_int(content
, &loglevel
);
1712 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1716 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1717 WARN("loglevel out of range.");
1718 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1722 event
.loglevel
= loglevel
;
1723 } else if (!strcmp((const char *) node
->name
,
1724 config_element_filter
)) {
1726 xmlNodeGetContent(node
);
1730 ret
= -LTTNG_ERR_NOMEM
;
1734 free(filter_expression
);
1735 filter_expression
= strdup((char *) content
);
1737 if (!filter_expression
) {
1738 ret
= -LTTNG_ERR_NOMEM
;
1741 } else if (!strcmp((const char *) node
->name
,
1742 config_element_exclusions
)) {
1743 xmlNodePtr exclusion_node
;
1744 int exclusion_index
= 0;
1749 * Exclusions has already been initialized,
1752 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1756 exclusion_count
= xmlChildElementCount(node
);
1757 if (!exclusion_count
) {
1761 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1763 exclusion_count
= 0;
1764 ret
= -LTTNG_ERR_NOMEM
;
1768 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1769 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1771 xmlNodeGetContent(exclusion_node
);
1774 ret
= -LTTNG_ERR_NOMEM
;
1778 exclusions
[exclusion_index
] = strdup((const char *) content
);
1780 if (!exclusions
[exclusion_index
]) {
1781 ret
= -LTTNG_ERR_NOMEM
;
1787 event
.exclusion
= 1;
1788 } else if (!strcmp((const char *) node
->name
,
1789 config_element_attributes
)) {
1790 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1793 if (!attribute_node
) {
1794 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1798 if (!strcmp((const char *) node
->name
,
1799 config_element_probe_attributes
)) {
1800 xmlNodePtr probe_attribute_node
;
1802 /* probe_attributes */
1803 for (probe_attribute_node
=
1804 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1805 probe_attribute_node
= xmlNextElementSibling(
1806 probe_attribute_node
)) {
1808 ret
= process_probe_attribute_node(probe_attribute_node
,
1817 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1819 /* function_attributes */
1820 content
= xmlNodeGetContent(symbol_node
);
1822 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1826 sym_len
= strlen((char *) content
);
1827 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1828 WARN("Function name too long.");
1829 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1834 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1841 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1842 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1843 filter_expression
, exclusion_count
, exclusions
);
1845 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1846 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1851 for (i
= 0; i
< exclusion_count
; i
++) {
1852 free(exclusions
[i
]);
1856 free(filter_expression
);
1861 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1862 const char *channel_name
)
1865 struct lttng_event event
;
1868 assert(events_node
);
1870 assert(channel_name
);
1872 for (node
= xmlFirstElementChild(events_node
); node
;
1873 node
= xmlNextElementSibling(node
)) {
1874 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1881 * Disable all events to enable only the necessary events.
1882 * Limitations regarding lttng_disable_events and tuple descriptor
1883 * force this approach.
1885 memset(&event
, 0, sizeof(event
));
1886 event
.loglevel
= -1;
1887 event
.type
= LTTNG_EVENT_ALL
;
1888 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1893 for (node
= xmlFirstElementChild(events_node
); node
;
1894 node
= xmlNextElementSibling(node
)) {
1895 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1906 int process_channel_attr_node(xmlNodePtr attr_node
, bool snapshot_mode
,
1907 enum lttng_domain_type domain
, struct lttng_channel
*channel
,
1908 xmlNodePtr
*contexts_node
, xmlNodePtr
*events_node
)
1914 assert(contexts_node
);
1915 assert(events_node
);
1917 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1922 content
= xmlNodeGetContent(attr_node
);
1924 ret
= -LTTNG_ERR_NOMEM
;
1928 name_len
= strlen((char *) content
);
1929 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1930 WARN("Channel name too long.");
1931 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1936 strncpy(channel
->name
, (const char *) content
, name_len
);
1938 } else if (!strcmp((const char *) attr_node
->name
,
1939 config_element_enabled
)) {
1944 content
= xmlNodeGetContent(attr_node
);
1946 ret
= -LTTNG_ERR_NOMEM
;
1950 ret
= parse_bool(content
, &enabled
);
1953 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1957 channel
->enabled
= enabled
;
1958 } else if (!strcmp((const char *) attr_node
->name
,
1959 config_element_overwrite_mode
)) {
1962 /* overwrite_mode */
1963 content
= xmlNodeGetContent(attr_node
);
1965 ret
= -LTTNG_ERR_NOMEM
;
1969 ret
= get_overwrite_mode(content
);
1972 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1976 channel
->attr
.overwrite
= ret
;
1977 } else if (!strcmp((const char *) attr_node
->name
,
1978 config_element_subbuf_size
)) {
1981 /* subbuffer_size */
1982 content
= xmlNodeGetContent(attr_node
);
1984 ret
= -LTTNG_ERR_NOMEM
;
1988 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1991 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1994 } else if (!strcmp((const char *) attr_node
->name
,
1995 config_element_num_subbuf
)) {
1998 /* subbuffer_count */
1999 content
= xmlNodeGetContent(attr_node
);
2001 ret
= -LTTNG_ERR_NOMEM
;
2005 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
2008 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2011 } else if (!strcmp((const char *) attr_node
->name
,
2012 config_element_switch_timer_interval
)) {
2014 uint64_t switch_timer_interval
= 0;
2016 /* switch_timer_interval */
2017 content
= xmlNodeGetContent(attr_node
);
2019 ret
= -LTTNG_ERR_NOMEM
;
2023 ret
= parse_uint(content
, &switch_timer_interval
);
2026 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2030 if (switch_timer_interval
> UINT_MAX
) {
2031 WARN("switch_timer_interval out of range.");
2032 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2036 channel
->attr
.switch_timer_interval
=
2037 switch_timer_interval
;
2038 } else if (!strcmp((const char *) attr_node
->name
,
2039 config_element_read_timer_interval
)) {
2041 uint64_t read_timer_interval
= 0;
2043 /* read_timer_interval */
2044 content
= xmlNodeGetContent(attr_node
);
2046 ret
= -LTTNG_ERR_NOMEM
;
2050 ret
= parse_uint(content
, &read_timer_interval
);
2053 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2057 if (read_timer_interval
> UINT_MAX
) {
2058 WARN("read_timer_interval out of range.");
2059 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2063 channel
->attr
.read_timer_interval
=
2064 read_timer_interval
;
2065 } else if (!strcmp((const char *) attr_node
->name
,
2066 config_element_output_type
)) {
2070 content
= xmlNodeGetContent(attr_node
);
2072 ret
= -LTTNG_ERR_NOMEM
;
2076 ret
= get_output_type(content
);
2079 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2083 channel
->attr
.output
= ret
;
2084 } else if (!strcmp((const char *) attr_node
->name
,
2085 config_element_tracefile_size
)) {
2088 /* tracefile_size */
2089 content
= xmlNodeGetContent(attr_node
);
2091 ret
= -LTTNG_ERR_NOMEM
;
2095 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2098 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2101 } else if (!strcmp((const char *) attr_node
->name
,
2102 config_element_tracefile_count
)) {
2105 /* tracefile_count */
2106 content
= xmlNodeGetContent(attr_node
);
2108 ret
= -LTTNG_ERR_NOMEM
;
2112 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2115 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2118 } else if (!strcmp((const char *) attr_node
->name
,
2119 config_element_live_timer_interval
)) {
2121 uint64_t live_timer_interval
= 0;
2123 /* live_timer_interval */
2124 content
= xmlNodeGetContent(attr_node
);
2126 ret
= -LTTNG_ERR_NOMEM
;
2130 ret
= parse_uint(content
, &live_timer_interval
);
2133 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2137 if (live_timer_interval
> UINT_MAX
) {
2138 WARN("live_timer_interval out of range.");
2139 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2143 channel
->attr
.live_timer_interval
=
2144 live_timer_interval
;
2145 } else if (!strcmp((const char *) attr_node
->name
,
2146 config_element_events
)) {
2148 *events_node
= attr_node
;
2151 *contexts_node
= attr_node
;
2160 int process_context_node(xmlNodePtr context_node
,
2161 struct lttng_handle
*handle
, const char *channel_name
)
2164 struct lttng_event_context context
;
2165 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2168 assert(channel_name
);
2170 if (!context_child_node
) {
2171 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2175 memset(&context
, 0, sizeof(context
));
2177 if (!strcmp((const char *) context_child_node
->name
,
2178 config_element_type
)) {
2180 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2183 ret
= -LTTNG_ERR_NOMEM
;
2187 ret
= get_context_type(content
);
2190 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2195 } else if (!strcmp((const char *) context_child_node
->name
,
2196 config_element_context_perf
)) {
2198 xmlNodePtr perf_attr_node
;
2200 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2201 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2202 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2203 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2204 perf_attr_node
; perf_attr_node
=
2205 xmlNextElementSibling(perf_attr_node
)) {
2206 if (!strcmp((const char *) perf_attr_node
->name
,
2207 config_element_type
)) {
2212 content
= xmlNodeGetContent(perf_attr_node
);
2214 ret
= -LTTNG_ERR_NOMEM
;
2218 ret
= parse_uint(content
, &type
);
2221 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2225 if (type
> UINT32_MAX
) {
2226 WARN("perf context type out of range.");
2227 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2231 context
.u
.perf_counter
.type
= type
;
2232 } else if (!strcmp((const char *) perf_attr_node
->name
,
2233 config_element_config
)) {
2235 uint64_t config
= 0;
2238 content
= xmlNodeGetContent(perf_attr_node
);
2240 ret
= -LTTNG_ERR_NOMEM
;
2244 ret
= parse_uint(content
, &config
);
2247 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2251 context
.u
.perf_counter
.config
= config
;
2252 } else if (!strcmp((const char *) perf_attr_node
->name
,
2253 config_element_name
)) {
2258 content
= xmlNodeGetContent(perf_attr_node
);
2260 ret
= -LTTNG_ERR_NOMEM
;
2264 name_len
= strlen((char *) content
);
2265 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2266 WARN("perf context name too long.");
2267 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2272 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2277 } else if (!strcmp((const char *) context_child_node
->name
,
2278 config_element_context_app
)) {
2279 /* application context */
2280 xmlNodePtr app_ctx_node
;
2282 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2283 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2284 app_ctx_node
; app_ctx_node
=
2285 xmlNextElementSibling(app_ctx_node
)) {
2287 char **target
= strcmp(
2288 (const char *) app_ctx_node
->name
,
2289 config_element_context_app_provider_name
) == 0 ?
2290 &context
.u
.app_ctx
.provider_name
:
2291 &context
.u
.app_ctx
.ctx_name
;
2293 content
= xmlNodeGetContent(app_ctx_node
);
2295 ret
= -LTTNG_ERR_NOMEM
;
2299 *target
= (char *) content
;
2302 /* Unrecognized context type */
2303 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2307 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2308 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2309 free(context
.u
.app_ctx
.provider_name
);
2310 free(context
.u
.app_ctx
.ctx_name
);
2317 int process_contexts_node(xmlNodePtr contexts_node
,
2318 struct lttng_handle
*handle
, const char *channel_name
)
2321 xmlNodePtr context_node
;
2323 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2324 context_node
= xmlNextElementSibling(context_node
)) {
2325 ret
= process_context_node(context_node
, handle
, channel_name
);
2335 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2336 struct lttng_handle
*handle
)
2339 xmlNodePtr targets_node
= NULL
;
2343 assert(pid_tracker_node
);
2344 /* get the targets node */
2345 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2346 node
= xmlNextElementSibling(node
)) {
2347 if (!strcmp((const char *) node
->name
,
2348 config_element_targets
)) {
2349 targets_node
= node
;
2354 if (!targets_node
) {
2355 ret
= LTTNG_ERR_INVALID
;
2359 /* Go through all pid_target node */
2360 child
= xmlChildElementCount(targets_node
);
2362 /* The session is explicitly set to target nothing. */
2363 ret
= lttng_untrack_pid(handle
, -1);
2368 for (node
= xmlFirstElementChild(targets_node
); node
;
2369 node
= xmlNextElementSibling(node
)) {
2370 xmlNodePtr pid_target_node
= node
;
2372 /* get pid node and track it */
2373 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2374 node
= xmlNextElementSibling(node
)) {
2375 if (!strcmp((const char *) node
->name
,
2376 config_element_pid
)) {
2378 xmlChar
*content
= NULL
;
2380 content
= xmlNodeGetContent(node
);
2382 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2386 ret
= parse_int(content
, &pid
);
2389 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2393 ret
= lttng_track_pid(handle
, (int) pid
);
2399 node
= pid_target_node
;
2408 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
,
2412 struct lttng_domain domain
= { 0 };
2413 struct lttng_handle
*handle
= NULL
;
2414 xmlNodePtr channels_node
= NULL
;
2415 xmlNodePtr trackers_node
= NULL
;
2416 xmlNodePtr pid_tracker_node
= NULL
;
2419 assert(session_name
);
2421 ret
= init_domain(domain_node
, &domain
);
2426 handle
= lttng_create_handle(session_name
, &domain
);
2428 ret
= -LTTNG_ERR_NOMEM
;
2432 /* get the channels node */
2433 for (node
= xmlFirstElementChild(domain_node
); node
;
2434 node
= xmlNextElementSibling(node
)) {
2435 if (!strcmp((const char *) node
->name
,
2436 config_element_channels
)) {
2437 channels_node
= node
;
2442 if (!channels_node
) {
2446 /* create all channels */
2447 for (node
= xmlFirstElementChild(channels_node
); node
;
2448 node
= xmlNextElementSibling(node
)) {
2449 struct lttng_channel channel
;
2450 xmlNodePtr contexts_node
= NULL
;
2451 xmlNodePtr events_node
= NULL
;
2452 xmlNodePtr channel_attr_node
;
2454 memset(&channel
, 0, sizeof(channel
));
2455 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2457 for (channel_attr_node
= xmlFirstElementChild(node
);
2458 channel_attr_node
; channel_attr_node
=
2459 xmlNextElementSibling(channel_attr_node
)) {
2460 ret
= process_channel_attr_node(channel_attr_node
,
2461 snapshot_mode
, domain
.type
, &channel
,
2462 &contexts_node
, &events_node
);
2468 if (!channel
.name
) {
2469 ERR("Encountered a channel with no name attribute.");
2470 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2474 ret
= lttng_enable_channel(handle
, &channel
);
2479 ret
= process_events_node(events_node
, handle
, channel
.name
);
2484 ret
= process_contexts_node(contexts_node
, handle
,
2491 /* get the trackers node */
2492 for (node
= xmlFirstElementChild(domain_node
); node
;
2493 node
= xmlNextElementSibling(node
)) {
2494 if (!strcmp((const char *) node
->name
,
2495 config_element_trackers
)) {
2496 trackers_node
= node
;
2501 if (!trackers_node
) {
2505 for (node
= xmlFirstElementChild(trackers_node
); node
;
2506 node
= xmlNextElementSibling(node
)) {
2507 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2508 pid_tracker_node
= node
;
2509 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2516 if (!pid_tracker_node
) {
2517 lttng_track_pid(handle
, -1);
2521 lttng_destroy_handle(handle
);
2526 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2529 int ret
, started
= -1, snapshot_mode
= -1;
2530 uint64_t live_timer_interval
= UINT64_MAX
;
2531 xmlChar
*name
= NULL
;
2532 xmlChar
*shm_path
= NULL
;
2533 xmlNodePtr domains_node
= NULL
;
2534 xmlNodePtr output_node
= NULL
;
2536 struct lttng_domain
*kernel_domain
= NULL
;
2537 struct lttng_domain
*ust_domain
= NULL
;
2538 struct lttng_domain
*jul_domain
= NULL
;
2539 struct lttng_domain
*log4j_domain
= NULL
;
2540 struct lttng_domain
*python_domain
= NULL
;
2542 for (node
= xmlFirstElementChild(session_node
); node
;
2543 node
= xmlNextElementSibling(node
)) {
2544 if (!name
&& !strcmp((const char *) node
->name
,
2545 config_element_name
)) {
2547 xmlChar
*node_content
= xmlNodeGetContent(node
);
2548 if (!node_content
) {
2549 ret
= -LTTNG_ERR_NOMEM
;
2553 name
= node_content
;
2554 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2555 config_element_domains
)) {
2557 domains_node
= node
;
2558 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2559 config_element_started
)) {
2561 xmlChar
*node_content
= xmlNodeGetContent(node
);
2562 if (!node_content
) {
2563 ret
= -LTTNG_ERR_NOMEM
;
2567 ret
= parse_bool(node_content
, &started
);
2570 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2573 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2574 config_element_output
)) {
2577 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2578 config_element_shared_memory_path
)) {
2579 /* shared memory path */
2580 xmlChar
*node_content
= xmlNodeGetContent(node
);
2581 if (!node_content
) {
2582 ret
= -LTTNG_ERR_NOMEM
;
2586 shm_path
= node_content
;
2588 /* attributes, snapshot_mode or live_timer_interval */
2589 xmlNodePtr attributes_child
=
2590 xmlFirstElementChild(node
);
2592 if (!strcmp((const char *) attributes_child
->name
,
2593 config_element_snapshot_mode
)) {
2595 xmlChar
*snapshot_mode_content
=
2596 xmlNodeGetContent(attributes_child
);
2597 if (!snapshot_mode_content
) {
2598 ret
= -LTTNG_ERR_NOMEM
;
2602 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2603 free(snapshot_mode_content
);
2605 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2609 /* live_timer_interval */
2610 xmlChar
*timer_interval_content
=
2611 xmlNodeGetContent(attributes_child
);
2612 if (!timer_interval_content
) {
2613 ret
= -LTTNG_ERR_NOMEM
;
2617 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2618 free(timer_interval_content
);
2620 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2628 /* Mandatory attribute, as defined in the session XSD */
2629 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2633 if (session_name
&& strcmp((char *) name
, session_name
)) {
2634 /* This is not the session we are looking for */
2635 ret
= -LTTNG_ERR_NO_SESSION
;
2639 /* Init domains to create the session handles */
2640 for (node
= xmlFirstElementChild(domains_node
); node
;
2641 node
= xmlNextElementSibling(node
)) {
2642 struct lttng_domain
*domain
;
2644 domain
= zmalloc(sizeof(*domain
));
2646 ret
= -LTTNG_ERR_NOMEM
;
2650 ret
= init_domain(node
, domain
);
2652 goto domain_init_error
;
2655 switch (domain
->type
) {
2656 case LTTNG_DOMAIN_KERNEL
:
2657 if (kernel_domain
) {
2658 /* Same domain seen twice, invalid! */
2659 goto domain_init_error
;
2661 kernel_domain
= domain
;
2663 case LTTNG_DOMAIN_UST
:
2665 /* Same domain seen twice, invalid! */
2666 goto domain_init_error
;
2668 ust_domain
= domain
;
2670 case LTTNG_DOMAIN_JUL
:
2672 /* Same domain seen twice, invalid! */
2673 goto domain_init_error
;
2675 jul_domain
= domain
;
2677 case LTTNG_DOMAIN_LOG4J
:
2679 /* Same domain seen twice, invalid! */
2680 goto domain_init_error
;
2682 log4j_domain
= domain
;
2684 case LTTNG_DOMAIN_PYTHON
:
2685 if (python_domain
) {
2686 /* Same domain seen twice, invalid! */
2687 goto domain_init_error
;
2689 python_domain
= domain
;
2692 WARN("Invalid domain type");
2693 goto domain_init_error
;
2698 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2703 /* Destroy session if it exists */
2704 ret
= lttng_destroy_session((const char *) name
);
2705 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2706 ERR("Failed to destroy existing session.");
2711 /* Create session type depending on output type */
2712 if (snapshot_mode
&& snapshot_mode
!= -1) {
2713 ret
= create_snapshot_session((const char *) name
, output_node
);
2714 } else if (live_timer_interval
&&
2715 live_timer_interval
!= UINT64_MAX
) {
2716 ret
= create_session((const char *) name
, kernel_domain
,
2717 ust_domain
, jul_domain
, log4j_domain
,
2718 output_node
, live_timer_interval
);
2720 /* regular session */
2721 ret
= create_session((const char *) name
, kernel_domain
,
2722 ust_domain
, jul_domain
, log4j_domain
,
2723 output_node
, UINT64_MAX
);
2730 ret
= lttng_set_session_shm_path((const char *) name
,
2731 (const char *) shm_path
);
2737 for (node
= xmlFirstElementChild(domains_node
); node
;
2738 node
= xmlNextElementSibling(node
)) {
2739 ret
= process_domain_node(node
, (const char *) name
,
2740 snapshot_mode
== 1);
2747 ret
= lttng_start_tracing((const char *) name
);
2755 ERR("Failed to load session %s: %s", (const char *) name
,
2756 lttng_strerror(ret
));
2757 lttng_destroy_session((const char *) name
);
2761 free(kernel_domain
);
2765 free(python_domain
);
2772 * Return 1 if the given path is readable by the current UID or 0 if not.
2773 * Return -1 if the path is EPERM.
2775 static int validate_file_read_creds(const char *path
)
2781 /* Can we read the file. */
2782 ret
= access(path
, R_OK
);
2786 if (errno
== EACCES
) {
2797 int load_session_from_file(const char *path
, const char *session_name
,
2798 struct session_config_validation_ctx
*validation_ctx
, int override
)
2800 int ret
, session_found
= !session_name
;
2801 xmlDocPtr doc
= NULL
;
2802 xmlNodePtr sessions_node
;
2803 xmlNodePtr session_node
;
2806 assert(validation_ctx
);
2808 ret
= validate_file_read_creds(path
);
2811 ret
= -LTTNG_ERR_EPERM
;
2813 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2818 doc
= xmlParseFile(path
);
2820 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2824 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2826 ERR("Session configuration file validation failed");
2827 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2831 sessions_node
= xmlDocGetRootElement(doc
);
2832 if (!sessions_node
) {
2836 for (session_node
= xmlFirstElementChild(sessions_node
);
2837 session_node
; session_node
=
2838 xmlNextElementSibling(session_node
)) {
2839 ret
= process_session_node(session_node
,
2840 session_name
, override
);
2841 if (session_name
&& ret
== 0) {
2842 /* Target session found and loaded */
2850 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2856 int load_session_from_document(struct config_document
*document
, const char *session_name
,
2857 struct session_config_validation_ctx
*validation_ctx
, int override
)
2859 int ret
, session_found
= !session_name
;
2860 xmlNodePtr sessions_node
;
2861 xmlNodePtr session_node
;
2863 assert(validation_ctx
);
2865 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, document
->document
);
2867 ERR("Session configuration file validation failed");
2868 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2872 sessions_node
= xmlDocGetRootElement(document
->document
);
2873 if (!sessions_node
) {
2877 for (session_node
= xmlFirstElementChild(sessions_node
);
2878 session_node
; session_node
=
2879 xmlNextElementSibling(session_node
)) {
2880 ret
= process_session_node(session_node
,
2881 session_name
, override
);
2882 if (session_name
&& ret
== 0) {
2883 /* Target session found and loaded */
2890 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2895 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2897 struct dirent
*alloc_dirent(const char *path
)
2901 struct dirent
*entry
;
2903 name_max
= pathconf(path
, _PC_NAME_MAX
);
2904 if (name_max
== -1) {
2905 name_max
= PATH_MAX
;
2907 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2908 entry
= zmalloc(len
);
2913 int load_session_from_path(const char *path
, const char *session_name
,
2914 struct session_config_validation_ctx
*validation_ctx
, int override
)
2916 int ret
, session_found
= !session_name
;
2917 DIR *directory
= NULL
;
2920 assert(validation_ctx
);
2922 directory
= opendir(path
);
2926 /* Try the file loading. */
2929 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2932 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2937 struct dirent
*entry
;
2938 struct dirent
*result
;
2939 char *file_path
= NULL
;
2940 size_t path_len
= strlen(path
);
2942 if (path_len
>= PATH_MAX
) {
2943 ret
= -LTTNG_ERR_INVALID
;
2947 entry
= alloc_dirent(path
);
2949 ret
= -LTTNG_ERR_NOMEM
;
2953 file_path
= zmalloc(PATH_MAX
);
2955 ret
= -LTTNG_ERR_NOMEM
;
2960 strncpy(file_path
, path
, path_len
);
2961 if (file_path
[path_len
- 1] != '/') {
2962 file_path
[path_len
++] = '/';
2966 /* Search for *.lttng files */
2967 while (!readdir_r(directory
, entry
, &result
) && result
) {
2968 size_t file_name_len
= strlen(result
->d_name
);
2970 if (file_name_len
<=
2971 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2975 if (path_len
+ file_name_len
>= PATH_MAX
) {
2979 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2980 result
->d_name
+ file_name_len
- sizeof(
2981 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2985 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2986 file_path
[path_len
+ file_name_len
] = '\0';
2988 ret
= load_session_from_file(file_path
, session_name
,
2989 validation_ctx
, override
);
2990 if (session_name
&& !ret
) {
2999 ret
= load_session_from_file(path
, session_name
,
3000 validation_ctx
, override
);
3010 if (closedir(directory
)) {
3015 if (session_found
) {
3023 * Validate that the given path's credentials and the current process have the
3024 * same UID. If so, return 1 else return 0 if it does NOT match.
3026 static int validate_path_creds(const char *path
)
3028 int ret
, uid
= getuid();
3037 ret
= stat(path
, &buf
);
3039 if (errno
!= ENOENT
) {
3042 ret
= -LTTNG_ERR_INVALID
;
3046 if (buf
.st_uid
!= uid
) {
3057 int config_load_session(const char *path
, const char *session_name
,
3058 int override
, unsigned int autoload
)
3061 bool session_loaded
= false;
3062 const char *path_ptr
= NULL
;
3063 struct session_config_validation_ctx validation_ctx
= { 0 };
3065 ret
= init_session_config_validation_ctx(&validation_ctx
);
3072 const char *sys_path
;
3075 home_path
= utils_get_home_dir();
3077 char path
[PATH_MAX
];
3080 * Try user session configuration path. Ignore error here so we can
3081 * continue loading the system wide sessions.
3084 ret
= snprintf(path
, sizeof(path
),
3085 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3086 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3088 PERROR("snprintf session autoload home config path");
3093 * Credentials are only validated for the autoload in order to
3094 * avoid any user session daemon to try to load kernel sessions
3095 * automatically and failing all the times.
3097 ret
= validate_path_creds(path
);
3102 ret
= snprintf(path
, sizeof(path
),
3103 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3105 PERROR("snprintf session home config path");
3111 ret
= load_session_from_path(path_ptr
, session_name
,
3112 &validation_ctx
, override
);
3113 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3117 * Continue even if the session was found since we have to try
3118 * the system wide sessions.
3120 session_loaded
= true;
3124 /* Reset path pointer for the system wide dir. */
3127 /* Try system wide configuration directory. */
3129 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3130 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3131 ret
= validate_path_creds(sys_path
);
3133 path_ptr
= sys_path
;
3136 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3137 path_ptr
= sys_path
;
3141 ret
= load_session_from_path(path_ptr
, session_name
,
3142 &validation_ctx
, override
);
3144 session_loaded
= true;
3148 ret
= access(path
, F_OK
);
3153 ret
= -LTTNG_ERR_INVALID
;
3154 WARN("Session configuration path does not exist.");
3157 ret
= -LTTNG_ERR_EPERM
;
3160 ret
= -LTTNG_ERR_UNK
;
3166 ret
= load_session_from_path(path
, session_name
,
3167 &validation_ctx
, override
);
3170 fini_session_config_validation_ctx(&validation_ctx
);
3171 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3173 * Don't report an error if no sessions are found when called
3174 * without a session_name or a search path.
3179 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3180 /* A matching session was found in one of the search paths. */
3187 void __attribute__((destructor
)) session_config_exit(void)
3193 struct config_document
*config_document_get(const char *path
)
3196 struct config_document
*document
= NULL
;
3197 struct session_config_validation_ctx validation_ctx
= { 0 };
3201 ret
= access(path
, F_OK
);
3206 ERR("Configuration document path does not exist.");
3209 ERR("Configuration document permission denied.");
3212 ERR("Configuration document unknown error.");
3218 ret
= init_session_config_validation_ctx(&validation_ctx
);
3223 ret
= validate_file_read_creds(path
);
3226 ERR("Configuration document permission denied.");
3228 ERR("Configuration document does not exist.");
3233 document
= zmalloc(sizeof(struct config_document
));
3240 * Do not care of blanks and also make sure that direct dump have no
3241 * indentations in them
3243 xmlKeepBlanksDefault(0);
3244 document
->document
= xmlParseFile(path
);
3245 if (!document
->document
) {
3246 ERR("Configuration document parsing failed.");
3250 ret
= xmlSchemaValidateDoc(validation_ctx
.schema_validation_ctx
,
3251 document
->document
);
3253 ERR("Session configuration file validation failed");
3258 fini_session_config_validation_ctx(&validation_ctx
);
3261 xmlFreeDoc(document
->document
);
3267 void config_document_free(struct config_document
*document
)
3270 xmlFreeDoc(document
->document
);
3271 document
->document
= NULL
;
3278 int config_document_replace_element_value(struct config_document
*document
,
3279 const char *xpath
, const char *value
)
3282 int xpath_result_size
;
3284 xmlXPathContextPtr xpath_context
= NULL
;
3285 xmlXPathObjectPtr xpath_object
= NULL
;
3286 xmlNodeSetPtr xpath_result_set
= NULL
;
3287 xmlChar
*internal_xpath
= NULL
;
3288 xmlChar
*internal_value
= NULL
;
3294 internal_xpath
= encode_string(xpath
);
3295 if (!internal_xpath
) {
3296 ret
= -LTTNG_ERR_NOMEM
;
3297 ERR("Unable to encode xpath string");
3301 internal_value
= encode_string(value
);
3302 if (!internal_value
) {
3303 ret
= -LTTNG_ERR_NOMEM
;
3304 ERR("Unable to encode xpath replace value string");
3308 /* Initialize xpath context */
3309 xpath_context
= xmlXPathNewContext(document
->document
);
3310 if (!xpath_context
) {
3311 ret
= -LTTNG_ERR_NOMEM
;
3312 ERR("Unable to create xpath context");
3316 /* Evaluate de xpath expression */
3317 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3318 if (!xpath_object
) {
3319 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3323 /* TODO: from here could be extracted and previopus could be a step in
3324 * subsequent operation, modify/substitute/delete node.
3326 xpath_result_set
= xpath_object
->nodesetval
;
3327 if (!xpath_result_set
) {
3328 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3332 xpath_result_size
= xpath_result_set
->nodeNr
;
3335 * Reverse traversal since last element could be nested under parent
3336 * element present in the result set.
3338 for (int i
= xpath_result_size
- 1; i
>=0; i
--) {
3339 assert(xpath_result_set
->nodeTab
[i
]);
3340 xmlNodeSetContent(xpath_result_set
->nodeTab
[i
], internal_value
);
3343 * Libxml2 quirk regarding the freing and namesplace node see
3344 * libxml2 example and documentation for more details.
3346 if (xpath_result_set
->nodeTab
[i
]->type
!= XML_NAMESPACE_DECL
) {
3347 xpath_result_set
->nodeTab
[i
] = NULL
;
3352 xmlXPathFreeContext(xpath_context
);
3353 xmlXPathFreeObject(xpath_object
);
3354 xmlFree(internal_xpath
);
3355 xmlFree(internal_value
);
3360 int config_load_configuration_sessions(struct config_document
*document
,
3361 const char *session_name
, int override
)
3364 struct session_config_validation_ctx validation_ctx
= { 0 };
3366 ret
= init_session_config_validation_ctx(&validation_ctx
);
3370 ret
= load_session_from_document(document
, session_name
,
3371 &validation_ctx
, override
);
3373 fini_session_config_validation_ctx(&validation_ctx
);
3378 int config_document_replace_element(struct config_document
*document
,
3379 const char *xpath
, const struct config_element
*element
)
3382 int xpath_result_size
;
3384 xmlXPathContextPtr xpath_context
= NULL
;
3385 xmlXPathObjectPtr xpath_object
= NULL
;
3386 xmlNodeSetPtr xpath_result_set
= NULL
;
3387 xmlChar
*internal_xpath
= NULL
;
3388 xmlNodePtr old_node
= NULL
;
3389 xmlNodePtr copy
= NULL
;
3394 assert(element
->element
);
3396 internal_xpath
= encode_string(xpath
);
3397 if (!internal_xpath
) {
3398 ret
= -LTTNG_ERR_NOMEM
;
3399 ERR("Unable to encode xpath string");
3403 /* Initialize xpath context */
3404 xpath_context
= xmlXPathNewContext(document
->document
);
3405 if (!xpath_context
) {
3406 ret
= -LTTNG_ERR_NOMEM
;
3407 ERR("Unable to create xpath context");
3411 /* Evaluate the xpath expression */
3412 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3413 if (!xpath_object
) {
3414 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3418 xpath_result_set
= xpath_object
->nodesetval
;
3419 if (!xpath_result_set
) {
3420 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3424 xpath_result_size
= xpath_result_set
->nodeNr
;
3426 if (xpath_result_size
> 1) {
3427 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3431 if (xpath_result_size
== 0) {
3432 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3436 assert(xpath_result_set
->nodeTab
[0]);
3438 /* Do a copy of the element to ease caller memory management */
3439 copy
= xmlCopyNode(element
->element
, 1);
3441 ret
= -LTTNG_ERR_NOMEM
;
3445 old_node
= xmlReplaceNode(xpath_result_set
->nodeTab
[0], copy
);
3446 if (xpath_result_set
->nodeTab
[0]->type
!= XML_NAMESPACE_DECL
)
3447 xpath_result_set
->nodeTab
[0] = NULL
;
3449 ret
= -LTTNG_ERR_CONFIG_REPLACEMENT
;
3453 xmlUnlinkNode(old_node
);
3454 xmlFreeNode(old_node
);
3456 xmlXPathFreeContext(xpath_context
);
3457 xmlXPathFreeObject(xpath_object
);
3458 xmlFree(internal_xpath
);
3463 char *config_document_get_element_value(struct config_document
*document
,
3467 int xpath_result_size
;
3468 int value_result_size
;
3470 xmlXPathContextPtr xpath_context
= NULL
;
3471 xmlXPathObjectPtr xpath_object
= NULL
;
3472 xmlNodeSetPtr xpath_result_set
= NULL
;
3473 xmlChar
*internal_xpath
= NULL
;
3474 xmlChar
*internal_value
= NULL
;
3479 internal_xpath
= encode_string(xpath
);
3480 if (!internal_xpath
) {
3482 ERR("Unable to encode xpath string");
3486 /* Initialize xpath context */
3487 xpath_context
= xmlXPathNewContext(document
->document
);
3488 if (!xpath_context
) {
3490 ERR("Unable to create xpath context");
3494 /* Evaluate the xpath expression */
3495 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3496 if (!xpath_object
) {
3498 ERR("Unable to evaluate xpath query (invalid query format)");
3502 xpath_result_set
= xpath_object
->nodesetval
;
3503 if (!xpath_result_set
) {
3508 xpath_result_size
= xpath_result_set
->nodeNr
;
3510 if (xpath_result_size
> 1) {
3511 ERR("To many result while fetching config element value");
3516 if (xpath_result_size
== 0) {
3522 * Reverse traversal since last element could be nested under parent
3523 * element present in the result set.
3525 assert(xpath_result_set
->nodeTab
[0]);
3526 internal_value
= xmlNodeGetContent(xpath_result_set
->nodeTab
[0]);
3527 if (!internal_value
) {
3532 value_result_size
= xmlStrlen(internal_value
);
3533 value
= calloc(value_result_size
+ 1, sizeof(char));
3534 strncpy(value
, (char *) internal_value
, value_result_size
);
3537 xmlXPathFreeContext(xpath_context
);
3538 xmlXPathFreeObject(xpath_object
);
3539 xmlFree(internal_xpath
);
3540 xmlFree(internal_value
);
3545 int config_document_element_exist(struct config_document
*document
,
3549 int xpath_result_size
;
3551 xmlXPathContextPtr xpath_context
= NULL
;
3552 xmlXPathObjectPtr xpath_object
= NULL
;
3553 xmlNodeSetPtr xpath_result_set
= NULL
;
3554 xmlChar
*internal_xpath
= NULL
;
3557 assert(document
->document
);
3560 internal_xpath
= encode_string(xpath
);
3561 if (!internal_xpath
) {
3562 ERR("Unable to encode xpath string");
3566 /* Initialize xpath context */
3567 xpath_context
= xmlXPathNewContext(document
->document
);
3568 if (!xpath_context
) {
3569 ERR("Unable to create xpath context");
3573 /* Evaluate the xpath expression */
3574 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3575 if (!xpath_object
) {
3576 ERR("Unable to evaluate xpath query (invalid query format)");
3580 xpath_result_set
= xpath_object
->nodesetval
;
3581 if (!xpath_result_set
) {
3585 xpath_result_size
= xpath_result_set
->nodeNr
;
3587 if (xpath_result_size
> 0) {
3591 xmlXPathFreeContext(xpath_context
);
3592 xmlXPathFreeObject(xpath_object
);
3593 xmlFree(internal_xpath
);
3599 struct config_element
*config_element_create(const char *name
,
3602 struct config_element
*element
;
3606 xmlChar
*internal_name
= NULL
;
3607 xmlChar
*internal_value
= NULL
;
3609 internal_name
= encode_string(name
);
3610 if (!internal_name
) {
3611 ERR("Unable to encode config element name string");
3618 internal_value
= encode_string(value
);
3619 if (!internal_value
) {
3620 ERR("Unable to encode config_element value string");
3626 element
= zmalloc(sizeof(struct config_element
));
3631 element
->element
= xmlNewNode(NULL
, internal_name
);
3632 if (!element
->element
) {
3638 if (internal_value
) {
3639 xmlNodeAddContent(element
->element
, internal_value
);
3642 xmlFree(internal_name
);
3643 xmlFree(internal_value
);
3648 int config_element_add_child(struct config_element
*parent
,
3649 const struct config_element
*child
)
3653 assert(parent
->element
);
3654 assert(child
->element
);
3657 xmlNodePtr node
= NULL
;
3658 xmlNodePtr copy
= NULL
;
3660 /* Do a copy to ease the memory management for caller */
3661 copy
= xmlCopyNode(child
->element
, 1);
3663 ret
= -LTTNG_ERR_NOMEM
;
3666 node
= xmlAddChild(parent
->element
, copy
);
3669 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3678 int config_document_insert_element(struct config_document
*document
,
3679 const char *xpath
, const struct config_element
*element
)
3682 int xpath_result_size
;
3684 xmlXPathContextPtr xpath_context
= NULL
;
3685 xmlXPathObjectPtr xpath_object
= NULL
;
3686 xmlNodeSetPtr xpath_result_set
= NULL
;
3687 xmlChar
*internal_xpath
= NULL
;
3688 xmlNodePtr child_node
= NULL
;
3689 xmlNodePtr local_copy
= NULL
;
3694 assert(element
->element
);
3696 internal_xpath
= encode_string(xpath
);
3697 if (!internal_xpath
) {
3698 ret
= -LTTNG_ERR_NOMEM
;
3702 /* Initialize xpath context */
3703 xpath_context
= xmlXPathNewContext(document
->document
);
3704 if (!xpath_context
) {
3705 ret
= -LTTNG_ERR_NOMEM
;
3709 /* Evaluate the xpath expression */
3710 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3711 if (!xpath_object
) {
3712 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3716 xpath_result_set
= xpath_object
->nodesetval
;
3717 if (!xpath_result_set
) {
3718 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3722 xpath_result_size
= xpath_result_set
->nodeNr
;
3724 if (xpath_result_size
> 1) {
3725 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3729 if (xpath_result_size
== 0) {
3730 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3734 assert(xpath_result_set
->nodeTab
[0]);
3735 /* Do a copy to simply memory management */
3736 local_copy
= xmlCopyNode(element
->element
, 1);
3738 ret
= -LTTNG_ERR_NOMEM
;
3742 child_node
= xmlAddChild(xpath_result_set
->nodeTab
[0], local_copy
);
3744 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3745 xmlFreeNode(local_copy
);
3749 xmlXPathFreeContext(xpath_context
);
3750 xmlXPathFreeObject(xpath_object
);
3751 xmlFree(internal_xpath
);
3756 void config_element_free(struct config_element
*element
) {
3757 if (element
&& element
->element
) {
3758 xmlFreeNode(element
->element
);
3765 void config_element_free_array(struct config_element
**array
, int size
) {
3768 for (int i
= 0; i
< size
; i
++) {
3772 if (array
[i
]->element
) {
3773 xmlFreeNode(array
[i
]->element
);
3782 void config_document_get_element_array(const struct config_document
*document
, const char *xpath
, struct config_element
***element_array
, int *array_size
)
3784 int xpath_result_size
;
3786 xmlXPathContextPtr xpath_context
= NULL
;
3787 xmlXPathObjectPtr xpath_object
= NULL
;
3788 xmlNodeSetPtr xpath_result_set
= NULL
;
3789 xmlChar
*internal_xpath
= NULL
;
3790 xmlChar
*internal_value
= NULL
;
3795 *element_array
=NULL
;
3799 internal_xpath
= encode_string(xpath
);
3800 if (!internal_xpath
) {
3801 ERR("Unable to encode xpath string");
3805 /* Initialize xpath context */
3806 xpath_context
= xmlXPathNewContext(document
->document
);
3807 if (!xpath_context
) {
3808 ERR("Unable to create xpath context");
3812 /* Evaluate the xpath expression */
3813 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3814 if (!xpath_object
) {
3815 ERR("Unable to evaluate xpath query (invalid query format)");
3819 xpath_result_set
= xpath_object
->nodesetval
;
3820 if (!xpath_result_set
) {
3824 xpath_result_size
= xpath_result_set
->nodeNr
;
3826 if (xpath_result_size
== 0) {
3830 /* alloc the array */
3831 *element_array
= calloc(xpath_result_size
, sizeof(struct config_element
*));
3832 *array_size
= xpath_result_size
;
3833 if (!element_array
) {
3834 ERR("Calloc config element array");
3838 for (int i
= 0; i
< xpath_result_size
; i
++) {
3839 xmlNodePtr node_copy
= NULL
;
3840 node_copy
= xmlCopyNode(xpath_result_set
->nodeTab
[i
], 1);
3842 ERR("Xml copy of event node");
3845 /* Hand off ownership */
3846 (*element_array
)[i
] = malloc(sizeof(struct config_element
));
3847 if (!((*element_array
)[i
])){
3848 ERR("Malloc config_element array");
3849 xmlFreeNode(node_copy
);
3852 (*element_array
)[i
]->element
= node_copy
;
3855 xmlXPathFreeContext(xpath_context
);
3856 xmlXPathFreeObject(xpath_object
);
3857 xmlFree(internal_xpath
);
3858 xmlFree(internal_value
);
3862 if (element_array
) {
3863 config_element_free_array(*element_array
, *array_size
);
3868 xmlXPathFreeContext(xpath_context
);
3869 xmlXPathFreeObject(xpath_object
);
3870 xmlFree(internal_xpath
);
3871 xmlFree(internal_value
);
3876 void config_element_get_element_array(const struct config_element
*element
, const char *xpath
, struct config_element
***element_array
, int *array_size
)
3878 int xpath_result_size
;
3880 xmlXPathContextPtr xpath_context
= NULL
;
3881 xmlXPathObjectPtr xpath_object
= NULL
;
3882 xmlNodeSetPtr xpath_result_set
= NULL
;
3883 xmlChar
*internal_xpath
= NULL
;
3884 xmlChar
*internal_value
= NULL
;
3885 xmlNodePtr local_copy
= NULL
;
3886 xmlNodePtr place_holder
= NULL
;
3887 xmlDocPtr document
= NULL
;
3890 assert(element
->element
);
3893 *element_array
=NULL
;
3896 /* Create a new doc and set root node as passed element */
3897 document
= xmlNewDoc(BAD_CAST
"1.0");
3899 ERR("Unable to create xml document");
3903 local_copy
= xmlCopyNode(element
->element
, 1);
3905 ERR("Unable to copy an xml node");
3909 /* Hand off the local_copy to the xmlDoc */
3910 place_holder
= xmlDocSetRootElement(document
, local_copy
);
3912 ERR("The document root was already set");
3913 xmlFreeNode(local_copy
);
3917 internal_xpath
= encode_string(xpath
);
3918 if (!internal_xpath
) {
3919 ERR("Unable to encode xpath string");
3923 /* Initialize xpath context */
3924 xpath_context
= xmlXPathNewContext(document
);
3925 if (!xpath_context
) {
3926 ERR("Unable to create xpath context");
3930 /* Evaluate the xpath expression */
3931 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3932 if (!xpath_object
) {
3933 ERR("Unable to evaluate xpath query (invalid query format)");
3937 xpath_result_set
= xpath_object
->nodesetval
;
3938 if (!xpath_result_set
) {
3942 xpath_result_size
= xpath_result_set
->nodeNr
;
3944 if (xpath_result_size
== 0) {
3948 /* alloc the array */
3949 *element_array
= calloc(xpath_result_size
, sizeof(struct config_element
*));
3950 *array_size
= xpath_result_size
;
3951 if (!element_array
) {
3952 ERR("Calloc config element array");
3956 for (int i
= 0; i
< xpath_result_size
; i
++) {
3957 xmlNodePtr node_copy
= NULL
;
3958 node_copy
= xmlCopyNode(xpath_result_set
->nodeTab
[i
], 1);
3960 ERR("Xml copy of event node");
3963 /* Hand off ownership */
3964 (*element_array
)[i
] = malloc(sizeof(struct config_element
));
3965 if (!((*element_array
)[i
])){
3966 ERR("Malloc config_element array");
3967 xmlFreeNode(node_copy
);
3970 (*element_array
)[i
]->element
= node_copy
;
3973 xmlXPathFreeContext(xpath_context
);
3974 xmlXPathFreeObject(xpath_object
);
3975 xmlFree(internal_xpath
);
3976 xmlFree(internal_value
);
3977 xmlFreeDoc(document
);
3981 if (element_array
) {
3982 config_element_free_array(*element_array
, *array_size
);
3987 xmlXPathFreeContext(xpath_context
);
3988 xmlXPathFreeObject(xpath_object
);
3989 xmlFree(internal_xpath
);
3990 xmlFree(internal_value
);
3993 /* Create an internal copy of the config_element.
3994 * The users is responsible for the freeing of the passed child.
3997 int config_element_add_or_replace_child(struct config_element
*parent
, struct config_element
*child
)
4001 assert(parent
->element
);
4002 assert(child
->element
);
4005 xmlNodePtr local_copy
;
4006 xmlNodePtr x_ret
= NULL
;
4007 xmlNodePtr iterator
= NULL
;
4009 xmlNodePtr x_parent
= parent
->element
;
4010 xmlNodePtr x_child
= child
->element
;
4012 /* Find and delete the node with the same name if present */
4013 iterator
= x_parent
->children
;
4014 while (iterator
!= NULL
) {
4015 if ((!xmlStrcmp(iterator
->name
, x_child
->name
))){
4016 xmlNodePtr del
= iterator
;
4017 iterator
= iterator
->next
;
4021 iterator
= iterator
->next
;
4026 local_copy
= xmlCopyNode(x_child
, 1);
4028 ERR("Duplication of node to be insert failed");
4029 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
4032 x_ret
= xmlAddChild(x_parent
, local_copy
);
4034 xmlFreeNode(local_copy
);
4035 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
4042 char *config_element_get_element_value(const struct config_element
*element
, const char *xpath
)
4045 int xpath_result_size
;
4046 int value_result_size
;
4048 xmlXPathContextPtr xpath_context
= NULL
;
4049 xmlXPathObjectPtr xpath_object
= NULL
;
4050 xmlNodeSetPtr xpath_result_set
= NULL
;
4051 xmlChar
*internal_xpath
= NULL
;
4052 xmlChar
*internal_value
= NULL
;
4053 xmlDocPtr document
= NULL
;
4054 xmlNodePtr local_copy
= NULL
;
4055 xmlNodePtr place_holder
= NULL
;
4058 assert(element
->element
);
4061 /* Create a new doc and set root node as passed element */
4062 document
= xmlNewDoc(BAD_CAST
"1.0");
4065 ERR("Unable to create xml document");
4069 local_copy
= xmlCopyNode(element
->element
, 1);
4072 ERR("Unable to copy an xml node");
4076 /* Hand off the local_copy to the xmlDoc */
4077 place_holder
= xmlDocSetRootElement(document
, local_copy
);
4080 ERR("The document root was already set");
4081 xmlFreeNode(local_copy
);
4085 internal_xpath
= encode_string(xpath
);
4086 if (!internal_xpath
) {
4087 /*TODO set valid error */
4089 ERR("Unable to encode xpath string");
4093 /* Initialize xpath context */
4094 xpath_context
= xmlXPathNewContext(document
);
4095 if (!xpath_context
) {
4096 /*TODO set valid error */
4098 ERR("Unable to create xpath context");
4102 /* Evaluate the xpath expression */
4103 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
4104 if (!xpath_object
) {
4105 /*TODO set valid error */
4107 ERR("Unable to evaluate xpath query (invalid query format)");
4111 xpath_result_set
= xpath_object
->nodesetval
;
4112 if (!xpath_result_set
) {
4113 /*TODO set valid error */
4118 xpath_result_size
= xpath_result_set
->nodeNr
;
4120 if (xpath_result_size
> 1) {
4121 /*TODO set valid error */
4122 ERR("To many result while fetching config element value");
4127 if (xpath_result_size
== 0) {
4132 assert(xpath_result_set
->nodeTab
[0]);
4133 internal_value
= xmlNodeGetContent(xpath_result_set
->nodeTab
[0]);
4134 if (!internal_value
) {
4139 value_result_size
= xmlStrlen(internal_value
);
4140 value
= calloc(value_result_size
+ 1, sizeof(char));
4141 strncpy(value
, (char *) internal_value
, value_result_size
);
4144 xmlXPathFreeContext(xpath_context
);
4145 xmlXPathFreeObject(xpath_object
);
4146 xmlFree(internal_xpath
);
4147 xmlFree(internal_value
);
4148 xmlFreeDoc(document
);
4154 int config_process_event_element(const struct config_element
*element
, const char *session_name
, int domain_type
, const char *channel_name
) {
4158 struct lttng_domain domain
;
4159 struct lttng_handle
*handle
;
4162 assert(element
->element
);
4164 memset(&domain
, 0, sizeof(struct lttng_domain
));
4167 domain
.type
= domain_type
;
4168 switch (domain
.type
) {
4169 case LTTNG_DOMAIN_KERNEL
:
4170 domain
.buf_type
= LTTNG_BUFFER_GLOBAL
;
4172 case LTTNG_DOMAIN_UST
:
4173 case LTTNG_DOMAIN_JUL
:
4174 case LTTNG_DOMAIN_LOG4J
:
4175 case LTTNG_DOMAIN_PYTHON
:
4176 domain
.buf_type
= LTTNG_BUFFER_PER_UID
;
4178 case LTTNG_DOMAIN_NONE
:
4183 handle
= lttng_create_handle(session_name
, &domain
);
4185 ret
= -LTTNG_ERR_HANDLE_CREATION
;
4190 node
= element
->element
;
4191 /* Check if element is really and event */
4192 if (xmlStrcmp(BAD_CAST config_element_event
, node
->name
)) {
4193 ret
= -LTTNG_ERR_CONFIG_INVALID_ELEMENT
;
4197 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);