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>
30 #include <common/defaults.h>
31 #include <common/error.h>
32 #include <common/macros.h>
33 #include <common/utils.h>
34 #include <common/config/config-session-internal.h>
35 #include <lttng/lttng-error.h>
36 #include <libxml/parser.h>
37 #include <libxml/valid.h>
38 #include <libxml/xmlschemas.h>
39 #include <libxml/tree.h>
40 #include <lttng/lttng.h>
41 #include <lttng/snapshot.h>
44 #include "config-internal.h"
46 struct handler_filter_args
{
48 config_entry_handler_cb handler
;
52 struct session_config_validation_ctx
{
53 xmlSchemaParserCtxtPtr parser_ctx
;
55 xmlSchemaValidCtxtPtr schema_validation_ctx
;
58 const char * const config_str_yes
= "yes";
59 const char * const config_str_true
= "true";
60 const char * const config_str_on
= "on";
61 const char * const config_str_no
= "no";
62 const char * const config_str_false
= "false";
63 const char * const config_str_off
= "off";
64 const char * const config_xml_encoding
= "UTF-8";
65 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
66 const char * const config_xml_indent_string
= "\t";
67 const char * const config_xml_true
= "true";
68 const char * const config_xml_false
= "false";
70 const char * const config_element_channel
= "channel";
71 const char * const config_element_channels
= "channels";
72 const char * const config_element_domain
= "domain";
73 const char * const config_element_domains
= "domains";
74 const char * const config_element_event
= "event";
75 const char * const config_element_events
= "events";
76 const char * const config_element_context
= "context";
77 const char * const config_element_contexts
= "contexts";
78 const char * const config_element_attributes
= "attributes";
79 const char * const config_element_exclusion
= "exclusion";
80 const char * const config_element_exclusions
= "exclusions";
81 const char * const config_element_function_attributes
= "function_attributes";
82 const char * const config_element_probe_attributes
= "probe_attributes";
83 const char * const config_element_symbol_name
= "symbol_name";
84 const char * const config_element_address
= "address";
85 const char * const config_element_offset
= "offset";
86 const char * const config_element_name
= "name";
87 const char * const config_element_enabled
= "enabled";
88 const char * const config_element_overwrite_mode
= "overwrite_mode";
89 const char * const config_element_subbuf_size
= "subbuffer_size";
90 const char * const config_element_num_subbuf
= "subbuffer_count";
91 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
92 const char * const config_element_read_timer_interval
= "read_timer_interval";
93 const char * const config_element_output
= "output";
94 const char * const config_element_output_type
= "output_type";
95 const char * const config_element_tracefile_size
= "tracefile_size";
96 const char * const config_element_tracefile_count
= "tracefile_count";
97 const char * const config_element_live_timer_interval
= "live_timer_interval";
98 const char * const config_element_type
= "type";
99 const char * const config_element_buffer_type
= "buffer_type";
100 const char * const config_element_session
= "session";
101 const char * const config_element_sessions
= "sessions";
102 const char * const config_element_perf
= "perf";
103 const char * const config_element_config
= "config";
104 const char * const config_element_started
= "started";
105 const char * const config_element_snapshot_mode
= "snapshot_mode";
106 const char * const config_element_loglevel
= "loglevel";
107 const char * const config_element_loglevel_type
= "loglevel_type";
108 const char * const config_element_filter
= "filter";
109 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
110 const char * const config_element_consumer_output
= "consumer_output";
111 const char * const config_element_destination
= "destination";
112 const char * const config_element_path
= "path";
113 const char * const config_element_net_output
= "net_output";
114 const char * const config_element_control_uri
= "control_uri";
115 const char * const config_element_data_uri
= "data_uri";
116 const char * const config_element_max_size
= "max_size";
118 const char * const config_domain_type_kernel
= "KERNEL";
119 const char * const config_domain_type_ust
= "UST";
120 const char * const config_domain_type_jul
= "JUL";
122 const char * const config_buffer_type_per_pid
= "PER_PID";
123 const char * const config_buffer_type_per_uid
= "PER_UID";
124 const char * const config_buffer_type_global
= "GLOBAL";
126 const char * const config_overwrite_mode_discard
= "DISCARD";
127 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
129 const char * const config_output_type_splice
= "SPLICE";
130 const char * const config_output_type_mmap
= "MMAP";
132 const char * const config_loglevel_type_all
= "ALL";
133 const char * const config_loglevel_type_range
= "RANGE";
134 const char * const config_loglevel_type_single
= "SINGLE";
136 const char * const config_event_type_all
= "ALL";
137 const char * const config_event_type_tracepoint
= "TRACEPOINT";
138 const char * const config_event_type_probe
= "PROBE";
139 const char * const config_event_type_function
= "FUNCTION";
140 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
141 const char * const config_event_type_noop
= "NOOP";
142 const char * const config_event_type_syscall
= "SYSCALL";
143 const char * const config_event_type_kprobe
= "KPROBE";
144 const char * const config_event_type_kretprobe
= "KRETPROBE";
146 const char * const config_event_context_pid
= "PID";
147 const char * const config_event_context_procname
= "PROCNAME";
148 const char * const config_event_context_prio
= "PRIO";
149 const char * const config_event_context_nice
= "NICE";
150 const char * const config_event_context_vpid
= "VPID";
151 const char * const config_event_context_tid
= "TID";
152 const char * const config_event_context_vtid
= "VTID";
153 const char * const config_event_context_ppid
= "PPID";
154 const char * const config_event_context_vppid
= "VPPID";
155 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
156 const char * const config_event_context_hostname
= "HOSTNAME";
157 const char * const config_event_context_ip
= "IP";
159 struct consumer_output
{
166 static int config_entry_handler_filter(struct handler_filter_args
*args
,
167 const char *section
, const char *name
, const char *value
)
170 struct config_entry entry
= { section
, name
, value
};
174 if (!section
|| !name
|| !value
) {
180 if (strcmp(args
->section
, section
)) {
185 ret
= args
->handler(&entry
, args
->user_data
);
191 int config_get_section_entries(const char *override_path
, const char *section
,
192 config_entry_handler_cb handler
, void *user_data
)
195 FILE *config_file
= NULL
;
196 struct handler_filter_args filter
= { section
, handler
, user_data
};
199 config_file
= fopen(override_path
, "r");
201 DBG("Loaded daemon configuration file at %s",
204 ERR("Failed to open daemon configuration file at %s",
210 char *path
= utils_get_home_dir();
212 /* Try to open the user's daemon configuration file */
214 ret
= asprintf(&path
, DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
220 config_file
= fopen(path
, "r");
222 DBG("Loaded daemon configuration file at %s", path
);
228 /* Try to open the system daemon configuration file */
230 config_file
= fopen(DEFAULT_DAEMON_HOME_CONFIGPATH
, "r");
235 DBG("No daemon configuration file found.");
239 ret
= ini_parse_file(config_file
,
240 (ini_entry_handler
) config_entry_handler_filter
, (void *) &filter
);
247 int config_parse_value(const char *value
)
250 char *endptr
, *lower_str
;
260 v
= strtoul(value
, &endptr
, 10);
261 if (endptr
!= value
) {
266 lower_str
= zmalloc(len
+ 1);
273 for (i
= 0; i
< len
; i
++) {
274 lower_str
[i
] = tolower(value
[i
]);
277 if (!strcmp(lower_str
, config_str_yes
) ||
278 !strcmp(lower_str
, config_str_true
) ||
279 !strcmp(lower_str
, config_str_on
)) {
281 } else if (!strcmp(lower_str
, config_str_no
) ||
282 !strcmp(lower_str
, config_str_false
) ||
283 !strcmp(lower_str
, config_str_off
)) {
295 * Returns a xmlChar string which must be released using xmlFree().
297 static xmlChar
*encode_string(const char *in_str
)
299 xmlChar
*out_str
= NULL
;
300 xmlCharEncodingHandlerPtr handler
;
301 int out_len
, ret
, in_len
;
305 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
307 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
311 in_len
= strlen(in_str
);
313 * Add 1 byte for the NULL terminted character. The factor 2 here is
314 * because UTF-8 can be on two bytes so this fits the worst case for each
317 out_len
= (in_len
* 2) + 1;
318 out_str
= xmlMalloc(out_len
);
323 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
330 /* out_len is now the size of out_str */
331 out_str
[out_len
] = '\0';
337 struct config_writer
*config_writer_create(int fd_output
)
340 struct config_writer
*writer
;
341 xmlOutputBufferPtr buffer
;
343 writer
= zmalloc(sizeof(struct config_writer
));
345 PERROR("zmalloc config_writer_create");
349 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
354 writer
->writer
= xmlNewTextWriter(buffer
);
355 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
356 config_xml_encoding
, NULL
);
361 ret
= xmlTextWriterSetIndentString(writer
->writer
,
362 BAD_CAST config_xml_indent_string
);
367 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
375 config_writer_destroy(writer
);
380 int config_writer_destroy(struct config_writer
*writer
)
389 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
390 WARN("Could not close XML document");
394 if (writer
->writer
) {
395 xmlFreeTextWriter(writer
->writer
);
404 int config_writer_open_element(struct config_writer
*writer
,
405 const char *element_name
)
408 xmlChar
*encoded_element_name
;
410 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
415 encoded_element_name
= encode_string(element_name
);
416 if (!encoded_element_name
) {
421 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
422 xmlFree(encoded_element_name
);
424 return ret
> 0 ? 0 : ret
;
428 int config_writer_close_element(struct config_writer
*writer
)
432 if (!writer
|| !writer
->writer
) {
437 ret
= xmlTextWriterEndElement(writer
->writer
);
439 return ret
> 0 ? 0 : ret
;
443 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
444 const char *element_name
, uint64_t value
)
447 xmlChar
*encoded_element_name
;
449 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
454 encoded_element_name
= encode_string(element_name
);
455 if (!encoded_element_name
) {
460 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
461 encoded_element_name
, "%" PRIu64
, value
);
462 xmlFree(encoded_element_name
);
464 return ret
> 0 ? 0 : ret
;
468 int config_writer_write_element_signed_int(struct config_writer
*writer
,
469 const char *element_name
, int64_t value
)
472 xmlChar
*encoded_element_name
;
474 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
479 encoded_element_name
= encode_string(element_name
);
480 if (!encoded_element_name
) {
485 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
486 encoded_element_name
, "%" PRIi64
, value
);
487 xmlFree(encoded_element_name
);
489 return ret
> 0 ? 0 : ret
;
493 int config_writer_write_element_bool(struct config_writer
*writer
,
494 const char *element_name
, int value
)
496 return config_writer_write_element_string(writer
, element_name
,
497 value
? config_xml_true
: config_xml_false
);
501 int config_writer_write_element_string(struct config_writer
*writer
,
502 const char *element_name
, const char *value
)
505 xmlChar
*encoded_element_name
= NULL
;
506 xmlChar
*encoded_value
= NULL
;
508 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
514 encoded_element_name
= encode_string(element_name
);
515 if (!encoded_element_name
) {
520 encoded_value
= encode_string(value
);
521 if (!encoded_value
) {
526 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
529 xmlFree(encoded_element_name
);
530 xmlFree(encoded_value
);
531 return ret
> 0 ? 0 : ret
;
535 void xml_error_handler(void *ctx
, const char *format
, ...)
541 va_start(args
, format
);
542 ret
= vasprintf(&errMsg
, format
, args
);
544 ERR("String allocation failed in xml error handler");
549 fprintf(stderr
, "XML Error: %s", errMsg
);
554 void fini_session_config_validation_ctx(
555 struct session_config_validation_ctx
*ctx
)
557 if (ctx
->parser_ctx
) {
558 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
562 xmlSchemaFree(ctx
->schema
);
565 if (ctx
->schema_validation_ctx
) {
566 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
569 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
573 int init_session_config_validation_ctx(
574 struct session_config_validation_ctx
*ctx
)
578 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(DEFAULT_SESSION_CONFIG_XSD_PATH
);
579 if (!ctx
->parser_ctx
) {
580 ERR("XSD parser context creation failed");
581 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
584 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
585 xml_error_handler
, NULL
);
587 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
589 ERR("XSD parsing failed");
590 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
594 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
595 if (!ctx
->schema_validation_ctx
) {
596 ERR("XSD validation context creation failed");
597 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
601 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
602 xml_error_handler
, NULL
);
607 fini_session_config_validation_ctx(ctx
);
614 int parse_uint(xmlChar
*str
, uint64_t *val
)
624 *val
= strtoull((const char *) str
, &endptr
, 10);
625 if (!endptr
|| *endptr
) {
636 int parse_int(xmlChar
*str
, int64_t *val
)
646 *val
= strtoll((const char *) str
, &endptr
, 10);
647 if (!endptr
|| *endptr
) {
658 int parse_bool(xmlChar
*str
, int *val
)
667 if (!strcmp((const char *) str
, config_xml_true
)) {
669 } else if (!strcmp((const char *) str
, config_xml_false
)) {
672 WARN("Invalid boolean value encoutered (%s).",
681 int get_domain_type(xmlChar
*domain
)
689 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
690 ret
= LTTNG_DOMAIN_KERNEL
;
691 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
692 ret
= LTTNG_DOMAIN_UST
;
693 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
694 ret
= LTTNG_DOMAIN_JUL
;
705 int get_buffer_type(xmlChar
*buffer_type
)
713 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
714 ret
= LTTNG_BUFFER_GLOBAL
;
715 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
716 ret
= LTTNG_BUFFER_PER_UID
;
717 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
718 ret
= LTTNG_BUFFER_PER_PID
;
729 int get_overwrite_mode(xmlChar
*overwrite_mode
)
733 if (!overwrite_mode
) {
737 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
739 } else if (!strcmp((char *) overwrite_mode
,
740 config_overwrite_mode_discard
)) {
752 int get_output_type(xmlChar
*output_type
)
760 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
761 ret
= LTTNG_EVENT_MMAP
;
762 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
763 ret
= LTTNG_EVENT_SPLICE
;
774 int get_event_type(xmlChar
*event_type
)
782 if (!strcmp((char *) event_type
, config_event_type_all
)) {
783 ret
= LTTNG_EVENT_ALL
;
784 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
785 ret
= LTTNG_EVENT_TRACEPOINT
;
786 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
787 ret
= LTTNG_EVENT_PROBE
;
788 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
789 ret
= LTTNG_EVENT_FUNCTION
;
790 } else if (!strcmp((char *) event_type
,
791 config_event_type_function_entry
)) {
792 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
793 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
794 ret
= LTTNG_EVENT_NOOP
;
795 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
796 ret
= LTTNG_EVENT_SYSCALL
;
807 int get_loglevel_type(xmlChar
*loglevel_type
)
811 if (!loglevel_type
) {
815 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
816 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
817 } else if (!strcmp((char *) loglevel_type
,
818 config_loglevel_type_range
)) {
819 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
820 } else if (!strcmp((char *) loglevel_type
,
821 config_loglevel_type_single
)) {
822 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
833 * Return the context type or -1 on error.
836 int get_context_type(xmlChar
*context_type
)
844 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
845 ret
= LTTNG_EVENT_CONTEXT_PID
;
846 } else if (!strcmp((char *) context_type
,
847 config_event_context_procname
)) {
848 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
849 } else if (!strcmp((char *) context_type
,
850 config_event_context_prio
)) {
851 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
852 } else if (!strcmp((char *) context_type
,
853 config_event_context_nice
)) {
854 ret
= LTTNG_EVENT_CONTEXT_NICE
;
855 } else if (!strcmp((char *) context_type
,
856 config_event_context_vpid
)) {
857 ret
= LTTNG_EVENT_CONTEXT_VPID
;
858 } else if (!strcmp((char *) context_type
,
859 config_event_context_tid
)) {
860 ret
= LTTNG_EVENT_CONTEXT_TID
;
861 } else if (!strcmp((char *) context_type
,
862 config_event_context_vtid
)) {
863 ret
= LTTNG_EVENT_CONTEXT_VTID
;
864 } else if (!strcmp((char *) context_type
,
865 config_event_context_ppid
)) {
866 ret
= LTTNG_EVENT_CONTEXT_PPID
;
867 } else if (!strcmp((char *) context_type
,
868 config_event_context_vppid
)) {
869 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
870 } else if (!strcmp((char *) context_type
,
871 config_event_context_pthread_id
)) {
872 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
873 } else if (!strcmp((char *) context_type
,
874 config_event_context_hostname
)) {
875 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
876 } else if (!strcmp((char *) context_type
,
877 config_event_context_ip
)) {
878 ret
= LTTNG_EVENT_CONTEXT_IP
;
889 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
894 for (node
= xmlFirstElementChild(domain_node
); node
;
895 node
= xmlNextElementSibling(node
)) {
896 if (!strcmp((const char *) node
->name
, config_element_type
)) {
898 xmlChar
*node_content
= xmlNodeGetContent(node
);
900 ret
= -LTTNG_ERR_NOMEM
;
904 ret
= get_domain_type(node_content
);
907 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
912 } else if (!strcmp((const char *) node
->name
,
913 config_element_buffer_type
)) {
915 xmlChar
*node_content
= xmlNodeGetContent(node
);
917 ret
= -LTTNG_ERR_NOMEM
;
921 ret
= get_buffer_type(node_content
);
924 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
928 domain
->buf_type
= ret
;
937 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
942 for (node
= xmlFirstElementChild(net_output_node
); node
;
943 node
= xmlNextElementSibling(node
)) {
944 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
946 *control_uri
= (char *) xmlNodeGetContent(node
);
952 *data_uri
= (char *) xmlNodeGetContent(node
);
959 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
963 int process_consumer_output(xmlNodePtr consumer_output_node
,
964 struct consumer_output
*output
)
971 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
972 node
= xmlNextElementSibling(node
)) {
973 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
974 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
978 ret
= -LTTNG_ERR_NOMEM
;
982 ret
= parse_bool(enabled_str
, &output
->enabled
);
988 xmlNodePtr output_type_node
;
991 output_type_node
= xmlFirstElementChild(node
);
992 if (!output_type_node
) {
993 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
997 if (!strcmp((const char *) output_type_node
->name
,
998 config_element_path
)) {
1000 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1001 if (!output
->path
) {
1002 ret
= -LTTNG_ERR_NOMEM
;
1007 ret
= get_net_output_uris(output_type_node
,
1008 &output
->control_uri
, &output
->data_uri
);
1020 free(output
->control_uri
);
1021 free(output
->data_uri
);
1022 memset(output
, 0, sizeof(struct consumer_output
));
1028 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1029 const char *control_uri
, const char *data_uri
)
1032 struct lttng_handle
*handle
;
1033 const char *uri
= NULL
;
1038 handle
= lttng_create_handle(name
, domain
);
1040 ret
= -LTTNG_ERR_NOMEM
;
1044 if (!control_uri
|| !data_uri
) {
1045 uri
= control_uri
? control_uri
: data_uri
;
1050 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1051 lttng_destroy_handle(handle
);
1057 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1060 xmlNodePtr node
= NULL
;
1061 xmlNodePtr snapshot_output_list_node
;
1062 xmlNodePtr snapshot_output_node
;
1064 assert(session_name
);
1066 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1075 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1077 /* Parse and create snapshot outputs */
1079 for (snapshot_output_node
=
1080 xmlFirstElementChild(snapshot_output_list_node
);
1081 snapshot_output_node
; snapshot_output_node
=
1082 xmlNextElementSibling(snapshot_output_node
)) {
1084 uint64_t max_size
= UINT64_MAX
;
1085 struct consumer_output output
= { 0 };
1086 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1088 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1089 node
= xmlNextElementSibling(node
)) {
1090 if (!strcmp((const char *) node
->name
,
1091 config_element_name
)) {
1093 name
= (char *) xmlNodeGetContent(node
);
1095 ret
= -LTTNG_ERR_NOMEM
;
1096 goto error_snapshot_output
;
1098 } else if (!strcmp((const char *) node
->name
,
1099 config_element_max_size
)) {
1100 xmlChar
*content
= xmlNodeGetContent(node
);
1104 ret
= -LTTNG_ERR_NOMEM
;
1105 goto error_snapshot_output
;
1107 ret
= parse_uint(content
, &max_size
);
1110 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1111 goto error_snapshot_output
;
1114 /* consumer_output */
1115 ret
= process_consumer_output(node
, &output
);
1117 goto error_snapshot_output
;
1122 snapshot_output
= lttng_snapshot_output_create();
1123 if (!snapshot_output
) {
1124 ret
= -LTTNG_ERR_NOMEM
;
1125 goto error_snapshot_output
;
1128 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1130 goto error_snapshot_output
;
1133 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1135 goto error_snapshot_output
;
1139 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1142 goto error_snapshot_output
;
1145 if (output
.control_uri
) {
1146 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1149 goto error_snapshot_output
;
1153 if (output
.data_uri
) {
1154 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1157 goto error_snapshot_output
;
1162 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1163 error_snapshot_output
:
1166 free(output
.control_uri
);
1167 free(output
.data_uri
);
1168 lttng_snapshot_output_destroy(snapshot_output
);
1178 int create_session(const char *name
,
1179 struct lttng_domain
*kernel_domain
,
1180 struct lttng_domain
*ust_domain
,
1181 struct lttng_domain
*jul_domain
,
1182 xmlNodePtr output_node
,
1183 uint64_t live_timer_interval
)
1186 struct consumer_output output
= { 0 };
1187 xmlNodePtr consumer_output_node
;
1190 assert(kernel_domain
);
1195 consumer_output_node
= xmlFirstElementChild(output_node
);
1196 if (!consumer_output_node
) {
1197 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1201 if (strcmp((const char *) consumer_output_node
->name
,
1202 config_element_consumer_output
)) {
1203 WARN("Invalid output type, expected %s node",
1204 config_element_consumer_output
);
1205 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1209 ret
= process_consumer_output(consumer_output_node
, &output
);
1215 if (live_timer_interval
!= UINT64_MAX
&&
1216 !output
.control_uri
&& !output
.data_uri
) {
1217 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1221 if (output
.control_uri
|| output
.data_uri
) {
1223 struct lttng_domain
*domain
;
1224 struct lttng_domain
*domains
[] =
1225 { kernel_domain
, ust_domain
, jul_domain
};
1227 /* network destination */
1228 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1229 const char *url
= output
.control_uri
?
1230 output
.control_uri
: output
.data_uri
;
1232 /* URL has to be provided, even if we'll overwrite it after. */
1233 ret
= lttng_create_session_live(name
, url
, live_timer_interval
);
1235 ret
= lttng_create_session(name
, NULL
);
1241 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1242 domain
= domains
[i
];
1247 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1254 /* either local output or no output */
1255 ret
= lttng_create_session(name
, output
.path
);
1262 free(output
.control_uri
);
1263 free(output
.data_uri
);
1267 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1268 struct lttng_event_probe_attr
*attr
)
1272 assert(probe_attribute_node
);
1275 if (!strcmp((const char *) probe_attribute_node
->name
,
1276 config_element_address
)) {
1281 content
= xmlNodeGetContent(probe_attribute_node
);
1283 ret
= -LTTNG_ERR_NOMEM
;
1287 ret
= parse_uint(content
, &addr
);
1290 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1295 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1296 config_element_offset
)) {
1298 uint64_t offset
= 0;
1301 content
= xmlNodeGetContent(probe_attribute_node
);
1303 ret
= -LTTNG_ERR_NOMEM
;
1307 ret
= parse_uint(content
, &offset
);
1310 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1314 attr
->offset
= offset
;
1315 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1316 config_element_symbol_name
)) {
1321 content
= xmlNodeGetContent(probe_attribute_node
);
1323 ret
= -LTTNG_ERR_NOMEM
;
1327 name_len
= strlen((char *) content
);
1328 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1329 WARN("symbol_name too long.");
1330 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1335 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1344 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1345 const char *channel_name
)
1349 struct lttng_event event
;
1350 char **exclusions
= NULL
;
1351 unsigned long exclusion_count
= 0;
1352 char *filter_expression
= NULL
;
1356 assert(channel_name
);
1358 memset(&event
, 0, sizeof(event
));
1360 for (node
= xmlFirstElementChild(event_node
); node
;
1361 node
= xmlNextElementSibling(node
)) {
1362 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1367 content
= xmlNodeGetContent(node
);
1369 ret
= -LTTNG_ERR_NOMEM
;
1373 name_len
= strlen((char *) content
);
1374 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1375 WARN("Channel name too long.");
1376 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1381 strncpy(event
.name
, (const char *) content
, name_len
);
1383 } else if (!strcmp((const char *) node
->name
,
1384 config_element_enabled
)) {
1385 xmlChar
*content
= xmlNodeGetContent(node
);
1389 ret
= -LTTNG_ERR_NOMEM
;
1393 ret
= parse_bool(content
, &event
.enabled
);
1396 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1399 } else if (!strcmp((const char *) node
->name
,
1400 config_element_type
)) {
1401 xmlChar
*content
= xmlNodeGetContent(node
);
1405 ret
= -LTTNG_ERR_NOMEM
;
1409 ret
= get_event_type(content
);
1412 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1417 } else if (!strcmp((const char *) node
->name
,
1418 config_element_loglevel_type
)) {
1419 xmlChar
*content
= xmlNodeGetContent(node
);
1423 ret
= -LTTNG_ERR_NOMEM
;
1427 ret
= get_loglevel_type(content
);
1430 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1434 event
.loglevel_type
= ret
;
1435 } else if (!strcmp((const char *) node
->name
,
1436 config_element_loglevel
)) {
1438 int64_t loglevel
= 0;
1441 content
= xmlNodeGetContent(node
);
1443 ret
= -LTTNG_ERR_NOMEM
;
1447 ret
= parse_int(content
, &loglevel
);
1450 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1454 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1455 WARN("loglevel out of range.");
1456 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1460 event
.loglevel
= loglevel
;
1461 } else if (!strcmp((const char *) node
->name
,
1462 config_element_filter
)) {
1464 xmlNodeGetContent(node
);
1468 ret
= -LTTNG_ERR_NOMEM
;
1472 filter_expression
= strdup((char *) content
);
1474 if (!filter_expression
) {
1475 ret
= -LTTNG_ERR_NOMEM
;
1478 } else if (!strcmp((const char *) node
->name
,
1479 config_element_exclusions
)) {
1480 xmlNodePtr exclusion_node
;
1481 int exclusion_index
= 0;
1486 * Exclusions has already been initialized,
1489 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1493 exclusion_count
= xmlChildElementCount(node
);
1494 if (!exclusion_count
) {
1498 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1500 exclusion_count
= 0;
1501 ret
= -LTTNG_ERR_NOMEM
;
1505 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1506 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1508 xmlNodeGetContent(exclusion_node
);
1511 ret
= -LTTNG_ERR_NOMEM
;
1515 exclusions
[exclusion_index
] = strdup((const char *) content
);
1517 if (!exclusions
[exclusion_index
]) {
1518 ret
= -LTTNG_ERR_NOMEM
;
1524 event
.exclusion
= 1;
1525 } else if (!strcmp((const char *) node
->name
,
1526 config_element_attributes
)) {
1527 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1530 if (!attribute_node
) {
1531 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1535 if (!strcmp((const char *) node
->name
,
1536 config_element_probe_attributes
)) {
1537 xmlNodePtr probe_attribute_node
;
1539 /* probe_attributes */
1540 for (probe_attribute_node
=
1541 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1542 probe_attribute_node
= xmlNextElementSibling(
1543 probe_attribute_node
)) {
1545 ret
= process_probe_attribute_node(probe_attribute_node
,
1554 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1556 /* function_attributes */
1557 content
= xmlNodeGetContent(symbol_node
);
1559 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1563 sym_len
= strlen((char *) content
);
1564 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1565 WARN("Function name too long.");
1566 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1571 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1578 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1579 filter_expression
, exclusion_count
, exclusions
);
1581 for (i
= 0; i
< exclusion_count
; i
++) {
1582 free(exclusions
[i
]);
1586 free(filter_expression
);
1591 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1592 const char *channel_name
)
1597 assert(events_node
);
1599 assert(channel_name
);
1601 for (node
= xmlFirstElementChild(events_node
); node
;
1602 node
= xmlNextElementSibling(node
)) {
1603 ret
= process_event_node(node
, handle
, channel_name
);
1613 int process_channel_attr_node(xmlNodePtr attr_node
,
1614 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1615 xmlNodePtr
*events_node
)
1621 assert(contexts_node
);
1622 assert(events_node
);
1624 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1629 content
= xmlNodeGetContent(attr_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(channel
->name
, (const char *) content
, name_len
);
1645 } else if (!strcmp((const char *) attr_node
->name
,
1646 config_element_enabled
)) {
1651 content
= xmlNodeGetContent(attr_node
);
1653 ret
= -LTTNG_ERR_NOMEM
;
1657 ret
= parse_bool(content
, &enabled
);
1660 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1664 channel
->enabled
= enabled
;
1665 } else if (!strcmp((const char *) attr_node
->name
,
1666 config_element_overwrite_mode
)) {
1669 /* overwrite_mode */
1670 content
= xmlNodeGetContent(attr_node
);
1672 ret
= -LTTNG_ERR_NOMEM
;
1676 ret
= get_overwrite_mode(content
);
1679 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1683 channel
->attr
.overwrite
= ret
;
1684 } else if (!strcmp((const char *) attr_node
->name
,
1685 config_element_subbuf_size
)) {
1688 /* subbuffer_size */
1689 content
= xmlNodeGetContent(attr_node
);
1691 ret
= -LTTNG_ERR_NOMEM
;
1695 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1698 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1701 } else if (!strcmp((const char *) attr_node
->name
,
1702 config_element_num_subbuf
)) {
1705 /* subbuffer_count */
1706 content
= xmlNodeGetContent(attr_node
);
1708 ret
= -LTTNG_ERR_NOMEM
;
1712 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1715 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1718 } else if (!strcmp((const char *) attr_node
->name
,
1719 config_element_switch_timer_interval
)) {
1721 uint64_t switch_timer_interval
= 0;
1723 /* switch_timer_interval */
1724 content
= xmlNodeGetContent(attr_node
);
1726 ret
= -LTTNG_ERR_NOMEM
;
1730 ret
= parse_uint(content
, &switch_timer_interval
);
1733 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1737 if (switch_timer_interval
> UINT_MAX
) {
1738 WARN("switch_timer_interval out of range.");
1739 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1743 channel
->attr
.switch_timer_interval
=
1744 switch_timer_interval
;
1745 } else if (!strcmp((const char *) attr_node
->name
,
1746 config_element_read_timer_interval
)) {
1748 uint64_t read_timer_interval
= 0;
1750 /* read_timer_interval */
1751 content
= xmlNodeGetContent(attr_node
);
1753 ret
= -LTTNG_ERR_NOMEM
;
1757 ret
= parse_uint(content
, &read_timer_interval
);
1760 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1764 if (read_timer_interval
> UINT_MAX
) {
1765 WARN("read_timer_interval out of range.");
1766 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1770 channel
->attr
.read_timer_interval
=
1771 read_timer_interval
;
1772 } else if (!strcmp((const char *) attr_node
->name
,
1773 config_element_output_type
)) {
1777 content
= xmlNodeGetContent(attr_node
);
1779 ret
= -LTTNG_ERR_NOMEM
;
1783 ret
= get_output_type(content
);
1786 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1790 channel
->attr
.output
= ret
;
1791 } else if (!strcmp((const char *) attr_node
->name
,
1792 config_element_tracefile_size
)) {
1795 /* tracefile_size */
1796 content
= xmlNodeGetContent(attr_node
);
1798 ret
= -LTTNG_ERR_NOMEM
;
1802 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1805 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1808 } else if (!strcmp((const char *) attr_node
->name
,
1809 config_element_tracefile_count
)) {
1812 /* tracefile_count */
1813 content
= xmlNodeGetContent(attr_node
);
1815 ret
= -LTTNG_ERR_NOMEM
;
1819 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1822 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 } else if (!strcmp((const char *) attr_node
->name
,
1826 config_element_live_timer_interval
)) {
1828 uint64_t live_timer_interval
= 0;
1830 /* live_timer_interval */
1831 content
= xmlNodeGetContent(attr_node
);
1833 ret
= -LTTNG_ERR_NOMEM
;
1837 ret
= parse_uint(content
, &live_timer_interval
);
1840 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1844 if (live_timer_interval
> UINT_MAX
) {
1845 WARN("live_timer_interval out of range.");
1846 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1850 channel
->attr
.live_timer_interval
=
1851 live_timer_interval
;
1852 } else if (!strcmp((const char *) attr_node
->name
,
1853 config_element_events
)) {
1855 *events_node
= attr_node
;
1858 *contexts_node
= attr_node
;
1866 int process_context_node(xmlNodePtr context_node
,
1867 struct lttng_handle
*handle
, const char *channel_name
)
1870 struct lttng_event_context context
;
1871 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1874 assert(channel_name
);
1876 if (!context_child_node
) {
1877 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1881 memset(&context
, 0, sizeof(context
));
1883 if (!strcmp((const char *) context_child_node
->name
,
1884 config_element_type
)) {
1886 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1888 ret
= -LTTNG_ERR_NOMEM
;
1892 ret
= get_context_type(content
);
1895 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1901 xmlNodePtr perf_attr_node
;
1904 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1905 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1906 perf_attr_node
; perf_attr_node
=
1907 xmlNextElementSibling(perf_attr_node
)) {
1908 if (!strcmp((const char *) perf_attr_node
->name
,
1909 config_element_type
)) {
1914 content
= xmlNodeGetContent(perf_attr_node
);
1916 ret
= -LTTNG_ERR_NOMEM
;
1920 ret
= parse_uint(content
, &type
);
1923 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1927 if (type
> UINT32_MAX
) {
1928 WARN("perf context type out of range.");
1929 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1933 context
.u
.perf_counter
.type
= type
;
1934 } else if (!strcmp((const char *) perf_attr_node
->name
,
1935 config_element_config
)) {
1937 uint64_t config
= 0;
1940 content
= xmlNodeGetContent(perf_attr_node
);
1942 ret
= -LTTNG_ERR_NOMEM
;
1946 ret
= parse_uint(content
, &config
);
1949 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1953 context
.u
.perf_counter
.config
= config
;
1954 } else if (!strcmp((const char *) perf_attr_node
->name
,
1955 config_element_name
)) {
1960 content
= xmlNodeGetContent(perf_attr_node
);
1962 ret
= -LTTNG_ERR_NOMEM
;
1966 name_len
= strlen((char *) content
);
1967 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1968 WARN("perf context name too long.");
1969 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1974 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
1981 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
1987 int process_contexts_node(xmlNodePtr contexts_node
,
1988 struct lttng_handle
*handle
, const char *channel_name
)
1991 xmlNodePtr context_node
;
1993 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
1994 context_node
= xmlNextElementSibling(context_node
)) {
1995 ret
= process_context_node(context_node
, handle
, channel_name
);
2005 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2008 struct lttng_domain domain
= { 0 };
2009 struct lttng_handle
*handle
= NULL
;
2010 xmlNodePtr channels_node
= NULL
;
2013 assert(session_name
);
2015 ret
= init_domain(domain_node
, &domain
);
2020 handle
= lttng_create_handle(session_name
, &domain
);
2022 ret
= -LTTNG_ERR_NOMEM
;
2026 /* get the channels node */
2027 for (node
= xmlFirstElementChild(domain_node
); node
;
2028 node
= xmlNextElementSibling(node
)) {
2029 if (!strcmp((const char *) node
->name
,
2030 config_element_channels
)) {
2031 channels_node
= node
;
2036 if (!channels_node
) {
2040 /* create all channels */
2041 for (node
= xmlFirstElementChild(channels_node
); node
;
2042 node
= xmlNextElementSibling(node
)) {
2043 struct lttng_channel channel
;
2044 xmlNodePtr contexts_node
= NULL
;
2045 xmlNodePtr events_node
= NULL
;
2046 xmlNodePtr channel_attr_node
;
2048 memset(&channel
, 0, sizeof(channel
));
2049 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2051 for (channel_attr_node
= xmlFirstElementChild(node
);
2052 channel_attr_node
; channel_attr_node
=
2053 xmlNextElementSibling(channel_attr_node
)) {
2054 ret
= process_channel_attr_node(channel_attr_node
,
2055 &channel
, &contexts_node
, &events_node
);
2061 ret
= lttng_enable_channel(handle
, &channel
);
2066 ret
= process_events_node(events_node
, handle
, channel
.name
);
2071 ret
= process_contexts_node(contexts_node
, handle
,
2078 lttng_destroy_handle(handle
);
2083 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2086 int ret
, started
= -1, snapshot_mode
= -1;
2087 uint64_t live_timer_interval
= UINT64_MAX
;
2089 xmlNodePtr domains_node
= NULL
;
2090 xmlNodePtr output_node
= NULL
;
2092 struct lttng_domain
*kernel_domain
= NULL
;
2093 struct lttng_domain
*ust_domain
= NULL
;
2094 struct lttng_domain
*jul_domain
= NULL
;
2096 for (node
= xmlFirstElementChild(session_node
); node
;
2097 node
= xmlNextElementSibling(node
)) {
2098 if (!name
&& !strcmp((const char *) node
->name
,
2099 config_element_name
)) {
2101 xmlChar
*node_content
= xmlNodeGetContent(node
);
2102 if (!node_content
) {
2103 ret
= -LTTNG_ERR_NOMEM
;
2107 name
= (char *) node_content
;
2108 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2109 config_element_domains
)) {
2111 domains_node
= node
;
2112 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2113 config_element_started
)) {
2115 xmlChar
*node_content
= xmlNodeGetContent(node
);
2116 if (!node_content
) {
2117 ret
= -LTTNG_ERR_NOMEM
;
2121 ret
= parse_bool(node_content
, &started
);
2124 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2127 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2128 config_element_output
)) {
2132 /* attributes, snapshot_mode or live_timer_interval */
2133 xmlNodePtr attributes_child
=
2134 xmlFirstElementChild(node
);
2136 if (!strcmp((const char *) attributes_child
->name
,
2137 config_element_snapshot_mode
)) {
2139 xmlChar
*snapshot_mode_content
=
2140 xmlNodeGetContent(attributes_child
);
2141 if (!snapshot_mode_content
) {
2142 ret
= -LTTNG_ERR_NOMEM
;
2146 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2147 free(snapshot_mode_content
);
2149 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2153 /* live_timer_interval */
2154 xmlChar
*timer_interval_content
=
2155 xmlNodeGetContent(attributes_child
);
2156 if (!timer_interval_content
) {
2157 ret
= -LTTNG_ERR_NOMEM
;
2161 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2162 free(timer_interval_content
);
2164 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2172 /* Mandatory attribute, as defined in the session XSD */
2173 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2177 if (session_name
&& strcmp(name
, session_name
)) {
2178 /* This is not the session we are looking for */
2179 ret
= -LTTNG_ERR_LOAD_SESSION_NOT_FOUND
;
2183 /* Init domains to create the session handles */
2184 for (node
= xmlFirstElementChild(domains_node
); node
;
2185 node
= xmlNextElementSibling(node
)) {
2186 struct lttng_domain
*domain
;
2188 domain
= zmalloc(sizeof(*domain
));
2190 ret
= -LTTNG_ERR_NOMEM
;
2194 ret
= init_domain(node
, domain
);
2196 goto domain_init_error
;
2199 switch (domain
->type
) {
2200 case LTTNG_DOMAIN_KERNEL
:
2201 if (kernel_domain
) {
2202 /* Same domain seen twice, invalid! */
2203 goto domain_init_error
;
2205 kernel_domain
= domain
;
2207 case LTTNG_DOMAIN_UST
:
2209 /* Same domain seen twice, invalid! */
2210 goto domain_init_error
;
2212 ust_domain
= domain
;
2214 case LTTNG_DOMAIN_JUL
:
2216 /* Same domain seen twice, invalid! */
2217 goto domain_init_error
;
2219 jul_domain
= domain
;
2222 WARN("Invalid domain type");
2223 goto domain_init_error
;
2228 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2233 /* Destroy session if it exists */
2234 ret
= lttng_destroy_session(name
);
2235 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2236 ERR("Failed to destroy existing session.");
2241 /* Create session type depending on output type */
2242 if (snapshot_mode
&& snapshot_mode
!= -1) {
2243 ret
= create_snapshot_session(name
, output_node
);
2244 } else if (live_timer_interval
&&
2245 live_timer_interval
!= UINT64_MAX
) {
2246 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2247 output_node
, live_timer_interval
);
2249 /* regular session */
2250 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2251 output_node
, UINT64_MAX
);
2258 for (node
= xmlFirstElementChild(domains_node
); node
;
2259 node
= xmlNextElementSibling(node
)) {
2260 ret
= process_domain_node(node
, name
);
2267 ret
= lttng_start_tracing(name
);
2273 free(kernel_domain
);
2281 int load_session_from_file(const char *path
, const char *session_name
,
2282 struct session_config_validation_ctx
*validation_ctx
, int override
)
2284 int ret
, session_found
= !session_name
;
2285 xmlDocPtr doc
= NULL
;
2286 xmlNodePtr sessions_node
;
2287 xmlNodePtr session_node
;
2291 assert(validation_ctx
);
2293 ret
= stat(path
, &sb
);
2295 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2299 doc
= xmlParseFile(path
);
2301 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2305 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2307 ERR("Session configuration file validation failed");
2308 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2312 sessions_node
= xmlDocGetRootElement(doc
);
2313 if (!sessions_node
) {
2317 for (session_node
= xmlFirstElementChild(sessions_node
);
2318 session_node
; session_node
=
2319 xmlNextElementSibling(session_node
)) {
2320 ret
= process_session_node(session_node
,
2321 session_name
, override
);
2322 if (session_name
&& ret
== 0) {
2323 /* Target session found and loaded */
2331 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOT_FOUND
;
2337 int load_session_from_path(const char *path
, const char *session_name
,
2338 struct session_config_validation_ctx
*validation_ctx
, int override
)
2340 int ret
, session_found
= !session_name
;
2342 DIR *directory
= NULL
;
2345 assert(validation_ctx
);
2347 ret
= stat(path
, &sb
);
2349 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2352 if (S_ISDIR(sb
.st_mode
)) {
2353 struct dirent
*entry
;
2354 struct dirent
*result
;
2355 char *file_path
= NULL
;
2356 size_t path_len
= strlen(path
);
2358 if (path_len
>= PATH_MAX
) {
2359 ret
= -LTTNG_ERR_INVALID
;
2363 entry
= zmalloc(sizeof(*entry
));
2365 ret
= -LTTNG_ERR_NOMEM
;
2369 directory
= opendir(path
);
2371 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2376 file_path
= zmalloc(PATH_MAX
);
2378 ret
= -LTTNG_ERR_NOMEM
;
2383 strncpy(file_path
, path
, path_len
);
2384 if (file_path
[path_len
- 1] != '/') {
2385 file_path
[path_len
++] = '/';
2388 /* Search for *.lttng files */
2389 while (!readdir_r(directory
, entry
, &result
) && result
) {
2390 size_t file_name_len
= strlen(result
->d_name
);
2392 if (file_name_len
<=
2393 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2397 if (path_len
+ file_name_len
> PATH_MAX
) {
2401 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2402 result
->d_name
+ file_name_len
- sizeof(
2403 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2407 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2408 file_path
[path_len
+ file_name_len
] = '\0';
2410 ret
= load_session_from_file(file_path
, session_name
,
2411 validation_ctx
, override
);
2412 if (session_name
&& !ret
) {
2421 ret
= load_session_from_file(path
, session_name
,
2422 validation_ctx
, override
);
2432 if (closedir(directory
)) {
2437 if (!session_found
) {
2438 ret
= -LTTNG_ERR_LOAD_SESSION_NOT_FOUND
;
2445 int config_load_session(const char *path
, const char *session_name
,
2449 struct session_config_validation_ctx validation_ctx
= { 0 };
2451 ret
= init_session_config_validation_ctx(&validation_ctx
);
2458 char *home_path
= utils_get_home_dir();
2462 ret
= asprintf(&path
, DEFAULT_SESSION_HOME_CONFIGPATH
,
2468 ret
= load_session_from_path(path
, NULL
,
2469 &validation_ctx
, 0);
2470 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2478 /* Try system session configuration path */
2479 ret
= load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH
, NULL
,
2480 &validation_ctx
, 0);
2481 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2485 ret
= access(path
, F_OK
);
2490 ret
= -LTTNG_ERR_INVALID
;
2491 WARN("Session configuration path does not exist.");
2494 ret
= -LTTNG_ERR_EPERM
;
2497 ret
= -LTTNG_ERR_UNK
;
2503 ret
= load_session_from_path(path
, session_name
,
2504 &validation_ctx
, override
);
2507 fini_session_config_validation_ctx(&validation_ctx
);