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 <lttng/lttng-error.h>
35 #include <libxml/parser.h>
36 #include <libxml/valid.h>
37 #include <libxml/xmlschemas.h>
38 #include <libxml/tree.h>
39 #include <lttng/lttng.h>
40 #include <lttng/snapshot.h>
43 #include "config-internal.h"
45 struct handler_filter_args
{
47 config_entry_handler_cb handler
;
51 struct session_config_validation_ctx
{
52 xmlSchemaParserCtxtPtr parser_ctx
;
54 xmlSchemaValidCtxtPtr schema_validation_ctx
;
57 const char * const config_str_yes
= "yes";
58 const char * const config_str_true
= "true";
59 const char * const config_str_on
= "on";
60 const char * const config_str_no
= "no";
61 const char * const config_str_false
= "false";
62 const char * const config_str_off
= "off";
63 const char * const config_xml_encoding
= "UTF-8";
64 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
65 const char * const config_xml_indent_string
= "\t";
66 const char * const config_xml_true
= "true";
67 const char * const config_xml_false
= "false";
69 const char * const config_element_channel
= "channel";
70 const char * const config_element_channels
= "channels";
71 const char * const config_element_domain
= "domain";
72 const char * const config_element_domains
= "domains";
73 const char * const config_element_event
= "event";
74 const char * const config_element_events
= "events";
75 const char * const config_element_context
= "context";
76 const char * const config_element_contexts
= "contexts";
77 const char * const config_element_attributes
= "attributes";
78 const char * const config_element_exclusion
= "exclusion";
79 const char * const config_element_exclusions
= "exclusions";
80 const char * const config_element_function_attributes
= "function_attributes";
81 const char * const config_element_probe_attributes
= "probe_attributes";
82 const char * const config_element_symbol_name
= "symbol_name";
83 const char * const config_element_address
= "address";
84 const char * const config_element_offset
= "offset";
85 const char * const config_element_name
= "name";
86 const char * const config_element_enabled
= "enabled";
87 const char * const config_element_overwrite_mode
= "overwrite_mode";
88 const char * const config_element_subbuf_size
= "subbuffer_size";
89 const char * const config_element_num_subbuf
= "subbuffer_count";
90 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
91 const char * const config_element_read_timer_interval
= "read_timer_interval";
92 const char * const config_element_output
= "output";
93 const char * const config_element_output_type
= "output_type";
94 const char * const config_element_tracefile_size
= "tracefile_size";
95 const char * const config_element_tracefile_count
= "tracefile_count";
96 const char * const config_element_live_timer_interval
= "live_timer_interval";
97 const char * const config_element_type
= "type";
98 const char * const config_element_buffer_type
= "buffer_type";
99 const char * const config_element_session
= "session";
100 const char * const config_element_sessions
= "sessions";
101 const char * const config_element_perf
= "perf";
102 const char * const config_element_config
= "config";
103 const char * const config_element_started
= "started";
104 const char * const config_element_snapshot_mode
= "snapshot_mode";
105 const char * const config_element_loglevel
= "loglevel";
106 const char * const config_element_loglevel_type
= "loglevel_type";
107 const char * const config_element_filter
= "filter";
108 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
109 const char * const config_element_consumer_output
= "consumer_output";
110 const char * const config_element_destination
= "destination";
111 const char * const config_element_path
= "path";
112 const char * const config_element_net_output
= "net_output";
113 const char * const config_element_control_uri
= "control_uri";
114 const char * const config_element_data_uri
= "data_uri";
115 const char * const config_element_max_size
= "max_size";
117 const char * const config_domain_type_kernel
= "KERNEL";
118 const char * const config_domain_type_ust
= "UST";
119 const char * const config_domain_type_jul
= "JUL";
121 const char * const config_buffer_type_per_pid
= "PER_PID";
122 const char * const config_buffer_type_per_uid
= "PER_UID";
123 const char * const config_buffer_type_global
= "GLOBAL";
125 const char * const config_overwrite_mode_discard
= "DISCARD";
126 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
128 const char * const config_output_type_splice
= "SPLICE";
129 const char * const config_output_type_mmap
= "MMAP";
131 const char * const config_loglevel_type_all
= "ALL";
132 const char * const config_loglevel_type_range
= "RANGE";
133 const char * const config_loglevel_type_single
= "SINGLE";
135 const char * const config_event_type_all
= "ALL";
136 const char * const config_event_type_tracepoint
= "TRACEPOINT";
137 const char * const config_event_type_probe
= "PROBE";
138 const char * const config_event_type_function
= "FUNCTION";
139 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
140 const char * const config_event_type_noop
= "NOOP";
141 const char * const config_event_type_syscall
= "SYSCALL";
142 const char * const config_event_type_kprobe
= "KPROBE";
143 const char * const config_event_type_kretprobe
= "KRETPROBE";
145 const char * const config_event_context_pid
= "PID";
146 const char * const config_event_context_procname
= "PROCNAME";
147 const char * const config_event_context_prio
= "PRIO";
148 const char * const config_event_context_nice
= "NICE";
149 const char * const config_event_context_vpid
= "VPID";
150 const char * const config_event_context_tid
= "TID";
151 const char * const config_event_context_vtid
= "VTID";
152 const char * const config_event_context_ppid
= "PPID";
153 const char * const config_event_context_vppid
= "VPPID";
154 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
155 const char * const config_event_context_hostname
= "HOSTNAME";
156 const char * const config_event_context_ip
= "IP";
158 struct consumer_output
{
165 static int config_entry_handler_filter(struct handler_filter_args
*args
,
166 const char *section
, const char *name
, const char *value
)
169 struct config_entry entry
= { section
, name
, value
};
173 if (!section
|| !name
|| !value
) {
179 if (strcmp(args
->section
, section
)) {
184 ret
= args
->handler(&entry
, args
->user_data
);
190 int config_get_section_entries(const char *override_path
, const char *section
,
191 config_entry_handler_cb handler
, void *user_data
)
194 FILE *config_file
= NULL
;
195 struct handler_filter_args filter
= { section
, handler
, user_data
};
198 config_file
= fopen(override_path
, "r");
200 DBG("Loaded daemon configuration file at %s",
203 ERR("Failed to open daemon configuration file at %s",
209 char *path
= utils_get_home_dir();
211 /* Try to open the user's daemon configuration file */
213 ret
= asprintf(&path
, DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
219 config_file
= fopen(path
, "r");
221 DBG("Loaded daemon configuration file at %s", path
);
227 /* Try to open the system daemon configuration file */
229 config_file
= fopen(DEFAULT_DAEMON_HOME_CONFIGPATH
, "r");
234 DBG("No daemon configuration file found.");
238 ret
= ini_parse_file(config_file
,
239 (ini_entry_handler
) config_entry_handler_filter
, (void *) &filter
);
246 int config_parse_value(const char *value
)
249 char *endptr
, *lower_str
;
259 v
= strtoul(value
, &endptr
, 10);
260 if (endptr
!= value
) {
265 lower_str
= zmalloc(len
+ 1);
272 for (i
= 0; i
< len
; i
++) {
273 lower_str
[i
] = tolower(value
[i
]);
276 if (!strcmp(lower_str
, config_str_yes
) ||
277 !strcmp(lower_str
, config_str_true
) ||
278 !strcmp(lower_str
, config_str_on
)) {
280 } else if (!strcmp(lower_str
, config_str_no
) ||
281 !strcmp(lower_str
, config_str_false
) ||
282 !strcmp(lower_str
, config_str_off
)) {
294 * Returns a xmlChar string which must be released using xmlFree().
296 static xmlChar
*encode_string(const char *in_str
)
298 xmlChar
*out_str
= NULL
;
299 xmlCharEncodingHandlerPtr handler
;
300 int out_len
, ret
, in_len
;
304 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
306 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
310 in_len
= strlen(in_str
);
312 * Add 1 byte for the NULL terminted character. The factor 2 here is
313 * because UTF-8 can be on two bytes so this fits the worst case for each
316 out_len
= (in_len
* 2) + 1;
317 out_str
= xmlMalloc(out_len
);
322 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
329 /* out_len is now the size of out_str */
330 out_str
[out_len
] = '\0';
336 struct config_writer
*config_writer_create(int fd_output
)
339 struct config_writer
*writer
;
340 xmlOutputBufferPtr buffer
;
342 writer
= zmalloc(sizeof(struct config_writer
));
344 PERROR("zmalloc config_writer_create");
348 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
353 writer
->writer
= xmlNewTextWriter(buffer
);
354 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
355 config_xml_encoding
, NULL
);
360 ret
= xmlTextWriterSetIndentString(writer
->writer
,
361 BAD_CAST config_xml_indent_string
);
366 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
374 config_writer_destroy(writer
);
379 int config_writer_destroy(struct config_writer
*writer
)
388 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
389 WARN("Could not close XML document");
393 if (writer
->writer
) {
394 xmlFreeTextWriter(writer
->writer
);
403 int config_writer_open_element(struct config_writer
*writer
,
404 const char *element_name
)
407 xmlChar
*encoded_element_name
;
409 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
414 encoded_element_name
= encode_string(element_name
);
415 if (!encoded_element_name
) {
420 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
421 xmlFree(encoded_element_name
);
423 return ret
> 0 ? 0 : ret
;
427 int config_writer_close_element(struct config_writer
*writer
)
431 if (!writer
|| !writer
->writer
) {
436 ret
= xmlTextWriterEndElement(writer
->writer
);
438 return ret
> 0 ? 0 : ret
;
442 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
443 const char *element_name
, uint64_t value
)
446 xmlChar
*encoded_element_name
;
448 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
453 encoded_element_name
= encode_string(element_name
);
454 if (!encoded_element_name
) {
459 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
460 encoded_element_name
, "%" PRIu64
, value
);
461 xmlFree(encoded_element_name
);
463 return ret
> 0 ? 0 : ret
;
467 int config_writer_write_element_signed_int(struct config_writer
*writer
,
468 const char *element_name
, int64_t value
)
471 xmlChar
*encoded_element_name
;
473 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
478 encoded_element_name
= encode_string(element_name
);
479 if (!encoded_element_name
) {
484 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
485 encoded_element_name
, "%" PRIi64
, value
);
486 xmlFree(encoded_element_name
);
488 return ret
> 0 ? 0 : ret
;
492 int config_writer_write_element_bool(struct config_writer
*writer
,
493 const char *element_name
, int value
)
495 return config_writer_write_element_string(writer
, element_name
,
496 value
? config_xml_true
: config_xml_false
);
500 int config_writer_write_element_string(struct config_writer
*writer
,
501 const char *element_name
, const char *value
)
504 xmlChar
*encoded_element_name
= NULL
;
505 xmlChar
*encoded_value
= NULL
;
507 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
513 encoded_element_name
= encode_string(element_name
);
514 if (!encoded_element_name
) {
519 encoded_value
= encode_string(value
);
520 if (!encoded_value
) {
525 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
528 xmlFree(encoded_element_name
);
529 xmlFree(encoded_value
);
530 return ret
> 0 ? 0 : ret
;
534 void xml_error_handler(void *ctx
, const char *format
, ...)
540 va_start(args
, format
);
541 ret
= vasprintf(&errMsg
, format
, args
);
544 ERR("String allocation failed in xml error handler");
548 fprintf(stderr
, "XML Error: %s", errMsg
);
553 void fini_session_config_validation_ctx(
554 struct session_config_validation_ctx
*ctx
)
556 if (ctx
->parser_ctx
) {
557 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
561 xmlSchemaFree(ctx
->schema
);
564 if (ctx
->schema_validation_ctx
) {
565 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
568 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
572 int init_session_config_validation_ctx(
573 struct session_config_validation_ctx
*ctx
)
577 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(DEFAULT_SESSION_CONFIG_XSD_PATH
);
578 if (!ctx
->parser_ctx
) {
579 ERR("XSD parser context creation failed");
580 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
583 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
584 xml_error_handler
, NULL
);
586 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
588 ERR("XSD parsing failed");
589 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
593 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
594 if (!ctx
->schema_validation_ctx
) {
595 ERR("XSD validation context creation failed");
596 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
600 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
601 xml_error_handler
, NULL
);
606 fini_session_config_validation_ctx(ctx
);
613 int parse_uint(xmlChar
*str
, uint64_t *val
)
623 *val
= strtoull((const char *) str
, &endptr
, 10);
624 if (!endptr
|| *endptr
) {
635 int parse_int(xmlChar
*str
, int64_t *val
)
645 *val
= strtoll((const char *) str
, &endptr
, 10);
646 if (!endptr
|| *endptr
) {
657 int parse_bool(xmlChar
*str
, int *val
)
666 if (!strcmp((const char *) str
, config_xml_true
)) {
668 } else if (!strcmp((const char *) str
, config_xml_false
)) {
671 WARN("Invalid boolean value encoutered (%s).",
680 int get_domain_type(xmlChar
*domain
)
688 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
689 ret
= LTTNG_DOMAIN_KERNEL
;
690 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
691 ret
= LTTNG_DOMAIN_UST
;
692 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
693 ret
= LTTNG_DOMAIN_JUL
;
704 int get_buffer_type(xmlChar
*buffer_type
)
712 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
713 ret
= LTTNG_BUFFER_GLOBAL
;
714 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
715 ret
= LTTNG_BUFFER_PER_UID
;
716 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
717 ret
= LTTNG_BUFFER_PER_PID
;
728 int get_overwrite_mode(xmlChar
*overwrite_mode
)
732 if (!overwrite_mode
) {
736 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
738 } else if (!strcmp((char *) overwrite_mode
,
739 config_overwrite_mode_discard
)) {
751 int get_output_type(xmlChar
*output_type
)
759 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
760 ret
= LTTNG_EVENT_MMAP
;
761 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
762 ret
= LTTNG_EVENT_SPLICE
;
773 int get_event_type(xmlChar
*event_type
)
781 if (!strcmp((char *) event_type
, config_event_type_all
)) {
782 ret
= LTTNG_EVENT_ALL
;
783 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
784 ret
= LTTNG_EVENT_TRACEPOINT
;
785 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
786 ret
= LTTNG_EVENT_PROBE
;
787 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
788 ret
= LTTNG_EVENT_FUNCTION
;
789 } else if (!strcmp((char *) event_type
,
790 config_event_type_function_entry
)) {
791 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
792 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
793 ret
= LTTNG_EVENT_NOOP
;
794 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
795 ret
= LTTNG_EVENT_SYSCALL
;
806 int get_loglevel_type(xmlChar
*loglevel_type
)
810 if (!loglevel_type
) {
814 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
815 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
816 } else if (!strcmp((char *) loglevel_type
,
817 config_loglevel_type_range
)) {
818 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
819 } else if (!strcmp((char *) loglevel_type
,
820 config_loglevel_type_single
)) {
821 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
832 * Return the context type or -1 on error.
835 int get_context_type(xmlChar
*context_type
)
843 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
844 ret
= LTTNG_EVENT_CONTEXT_PID
;
845 } else if (!strcmp((char *) context_type
,
846 config_event_context_procname
)) {
847 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
848 } else if (!strcmp((char *) context_type
,
849 config_event_context_prio
)) {
850 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
851 } else if (!strcmp((char *) context_type
,
852 config_event_context_nice
)) {
853 ret
= LTTNG_EVENT_CONTEXT_NICE
;
854 } else if (!strcmp((char *) context_type
,
855 config_event_context_vpid
)) {
856 ret
= LTTNG_EVENT_CONTEXT_VPID
;
857 } else if (!strcmp((char *) context_type
,
858 config_event_context_tid
)) {
859 ret
= LTTNG_EVENT_CONTEXT_TID
;
860 } else if (!strcmp((char *) context_type
,
861 config_event_context_vtid
)) {
862 ret
= LTTNG_EVENT_CONTEXT_VTID
;
863 } else if (!strcmp((char *) context_type
,
864 config_event_context_ppid
)) {
865 ret
= LTTNG_EVENT_CONTEXT_PPID
;
866 } else if (!strcmp((char *) context_type
,
867 config_event_context_vppid
)) {
868 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
869 } else if (!strcmp((char *) context_type
,
870 config_event_context_pthread_id
)) {
871 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
872 } else if (!strcmp((char *) context_type
,
873 config_event_context_hostname
)) {
874 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
875 } else if (!strcmp((char *) context_type
,
876 config_event_context_ip
)) {
877 ret
= LTTNG_EVENT_CONTEXT_IP
;
888 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
893 for (node
= xmlFirstElementChild(domain_node
); node
;
894 node
= xmlNextElementSibling(node
)) {
895 if (!strcmp((const char *) node
->name
, config_element_type
)) {
897 xmlChar
*node_content
= xmlNodeGetContent(node
);
899 ret
= -LTTNG_ERR_NOMEM
;
903 ret
= get_domain_type(node_content
);
906 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
911 } else if (!strcmp((const char *) node
->name
,
912 config_element_buffer_type
)) {
914 xmlChar
*node_content
= xmlNodeGetContent(node
);
916 ret
= -LTTNG_ERR_NOMEM
;
920 ret
= get_buffer_type(node_content
);
923 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
927 domain
->buf_type
= ret
;
936 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
941 for (node
= xmlFirstElementChild(net_output_node
); node
;
942 node
= xmlNextElementSibling(node
)) {
943 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
945 *control_uri
= (char *) xmlNodeGetContent(node
);
951 *data_uri
= (char *) xmlNodeGetContent(node
);
958 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
962 int process_consumer_output(xmlNodePtr consumer_output_node
,
963 struct consumer_output
*output
)
970 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
971 node
= xmlNextElementSibling(node
)) {
972 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
973 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
977 ret
= -LTTNG_ERR_NOMEM
;
981 ret
= parse_bool(enabled_str
, &output
->enabled
);
987 xmlNodePtr output_type_node
;
990 output_type_node
= xmlFirstElementChild(node
);
991 if (!output_type_node
) {
992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
996 if (!strcmp((const char *) output_type_node
->name
,
997 config_element_path
)) {
999 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1000 if (!output
->path
) {
1001 ret
= -LTTNG_ERR_NOMEM
;
1006 ret
= get_net_output_uris(output_type_node
,
1007 &output
->control_uri
, &output
->data_uri
);
1019 free(output
->control_uri
);
1020 free(output
->data_uri
);
1021 memset(output
, 0, sizeof(struct consumer_output
));
1027 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1028 const char *control_uri
, const char *data_uri
)
1031 struct lttng_handle
*handle
;
1032 const char *uri
= NULL
;
1037 handle
= lttng_create_handle(name
, domain
);
1039 ret
= -LTTNG_ERR_NOMEM
;
1043 if (!control_uri
|| !data_uri
) {
1044 uri
= control_uri
? control_uri
: data_uri
;
1049 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1050 lttng_destroy_handle(handle
);
1056 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1059 xmlNodePtr node
= NULL
;
1060 xmlNodePtr snapshot_output_list_node
;
1061 xmlNodePtr snapshot_output_node
;
1063 assert(session_name
);
1065 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1074 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1076 /* Parse and create snapshot outputs */
1078 for (snapshot_output_node
=
1079 xmlFirstElementChild(snapshot_output_list_node
);
1080 snapshot_output_node
; snapshot_output_node
=
1081 xmlNextElementSibling(snapshot_output_node
)) {
1083 uint64_t max_size
= UINT64_MAX
;
1084 struct consumer_output output
= { 0 };
1085 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1087 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1088 node
= xmlNextElementSibling(node
)) {
1089 if (!strcmp((const char *) node
->name
,
1090 config_element_name
)) {
1092 name
= (char *) xmlNodeGetContent(node
);
1094 ret
= -LTTNG_ERR_NOMEM
;
1095 goto error_snapshot_output
;
1097 } else if (!strcmp((const char *) node
->name
,
1098 config_element_max_size
)) {
1099 xmlChar
*content
= xmlNodeGetContent(node
);
1103 ret
= -LTTNG_ERR_NOMEM
;
1104 goto error_snapshot_output
;
1106 ret
= parse_uint(content
, &max_size
);
1109 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1110 goto error_snapshot_output
;
1113 /* consumer_output */
1114 ret
= process_consumer_output(node
, &output
);
1116 goto error_snapshot_output
;
1121 snapshot_output
= lttng_snapshot_output_create();
1122 if (!snapshot_output
) {
1123 ret
= -LTTNG_ERR_NOMEM
;
1124 goto error_snapshot_output
;
1127 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1129 goto error_snapshot_output
;
1132 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1134 goto error_snapshot_output
;
1138 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1141 goto error_snapshot_output
;
1144 if (output
.control_uri
) {
1145 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1148 goto error_snapshot_output
;
1152 if (output
.data_uri
) {
1153 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1156 goto error_snapshot_output
;
1161 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1162 error_snapshot_output
:
1165 free(output
.control_uri
);
1166 free(output
.data_uri
);
1167 lttng_snapshot_output_destroy(snapshot_output
);
1177 int create_session(const char *name
,
1178 struct lttng_domain
*kernel_domain
,
1179 struct lttng_domain
*ust_domain
,
1180 struct lttng_domain
*jul_domain
,
1181 xmlNodePtr output_node
,
1182 uint64_t live_timer_interval
)
1185 struct consumer_output output
= { 0 };
1186 xmlNodePtr consumer_output_node
;
1189 assert(kernel_domain
);
1194 consumer_output_node
= xmlFirstElementChild(output_node
);
1195 if (!consumer_output_node
) {
1196 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1200 if (strcmp((const char *) consumer_output_node
->name
,
1201 config_element_consumer_output
)) {
1202 WARN("Invalid output type, expected %s node",
1203 config_element_consumer_output
);
1204 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1208 ret
= process_consumer_output(consumer_output_node
, &output
);
1214 if (live_timer_interval
!= UINT64_MAX
&&
1215 !output
.control_uri
&& !output
.data_uri
) {
1216 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1220 if (output
.control_uri
|| output
.data_uri
) {
1222 struct lttng_domain
*domain
;
1223 struct lttng_domain
*domains
[] =
1224 { kernel_domain
, ust_domain
, jul_domain
};
1226 /* network destination */
1227 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1228 const char *url
= output
.control_uri
?
1229 output
.control_uri
: output
.data_uri
;
1231 /* URL has to be provided, even if we'll overwrite it after. */
1232 ret
= lttng_create_session_live(name
, url
, live_timer_interval
);
1234 ret
= lttng_create_session(name
, NULL
);
1240 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1241 domain
= domains
[i
];
1246 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1253 /* either local output or no output */
1254 ret
= lttng_create_session(name
, output
.path
);
1261 free(output
.control_uri
);
1262 free(output
.data_uri
);
1266 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1267 struct lttng_event_probe_attr
*attr
)
1271 assert(probe_attribute_node
);
1274 if (!strcmp((const char *) probe_attribute_node
->name
,
1275 config_element_address
)) {
1280 content
= xmlNodeGetContent(probe_attribute_node
);
1282 ret
= -LTTNG_ERR_NOMEM
;
1286 ret
= parse_uint(content
, &addr
);
1289 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1294 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1295 config_element_offset
)) {
1297 uint64_t offset
= 0;
1300 content
= xmlNodeGetContent(probe_attribute_node
);
1302 ret
= -LTTNG_ERR_NOMEM
;
1306 ret
= parse_uint(content
, &offset
);
1309 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1313 attr
->offset
= offset
;
1314 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1315 config_element_symbol_name
)) {
1320 content
= xmlNodeGetContent(probe_attribute_node
);
1322 ret
= -LTTNG_ERR_NOMEM
;
1326 name_len
= strlen((char *) content
);
1327 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1328 WARN("symbol_name too long.");
1329 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1334 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1343 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1344 const char *channel_name
)
1348 struct lttng_event event
;
1349 char **exclusions
= NULL
;
1350 unsigned long exclusion_count
= 0;
1351 char *filter_expression
= NULL
;
1355 assert(channel_name
);
1357 memset(&event
, 0, sizeof(event
));
1359 for (node
= xmlFirstElementChild(event_node
); node
;
1360 node
= xmlNextElementSibling(node
)) {
1361 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1366 content
= xmlNodeGetContent(node
);
1368 ret
= -LTTNG_ERR_NOMEM
;
1372 name_len
= strlen((char *) content
);
1373 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1374 WARN("Channel name too long.");
1375 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1380 strncpy(event
.name
, (const char *) content
, name_len
);
1382 } else if (!strcmp((const char *) node
->name
,
1383 config_element_enabled
)) {
1384 xmlChar
*content
= xmlNodeGetContent(node
);
1388 ret
= -LTTNG_ERR_NOMEM
;
1392 ret
= parse_bool(content
, &event
.enabled
);
1395 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1398 } else if (!strcmp((const char *) node
->name
,
1399 config_element_type
)) {
1400 xmlChar
*content
= xmlNodeGetContent(node
);
1404 ret
= -LTTNG_ERR_NOMEM
;
1408 ret
= get_event_type(content
);
1411 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1416 } else if (!strcmp((const char *) node
->name
,
1417 config_element_loglevel_type
)) {
1418 xmlChar
*content
= xmlNodeGetContent(node
);
1422 ret
= -LTTNG_ERR_NOMEM
;
1426 ret
= get_loglevel_type(content
);
1429 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1433 event
.loglevel_type
= ret
;
1434 } else if (!strcmp((const char *) node
->name
,
1435 config_element_loglevel
)) {
1437 int64_t loglevel
= 0;
1440 content
= xmlNodeGetContent(node
);
1442 ret
= -LTTNG_ERR_NOMEM
;
1446 ret
= parse_int(content
, &loglevel
);
1449 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1453 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1454 WARN("loglevel out of range.");
1455 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1459 event
.loglevel
= loglevel
;
1460 } else if (!strcmp((const char *) node
->name
,
1461 config_element_filter
)) {
1463 xmlNodeGetContent(node
);
1467 ret
= -LTTNG_ERR_NOMEM
;
1471 filter_expression
= strdup((char *) content
);
1473 if (!filter_expression
) {
1474 ret
= -LTTNG_ERR_NOMEM
;
1477 } else if (!strcmp((const char *) node
->name
,
1478 config_element_exclusions
)) {
1479 xmlNodePtr exclusion_node
;
1480 int exclusion_index
= 0;
1485 * Exclusions has already been initialized,
1488 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1492 exclusion_count
= xmlChildElementCount(node
);
1493 if (!exclusion_count
) {
1497 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1499 exclusion_count
= 0;
1500 ret
= -LTTNG_ERR_NOMEM
;
1504 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1505 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1507 xmlNodeGetContent(exclusion_node
);
1510 ret
= -LTTNG_ERR_NOMEM
;
1514 exclusions
[exclusion_index
] = strdup((const char *) content
);
1516 if (!exclusions
[exclusion_index
]) {
1517 ret
= -LTTNG_ERR_NOMEM
;
1523 event
.exclusion
= 1;
1524 } else if (!strcmp((const char *) node
->name
,
1525 config_element_attributes
)) {
1526 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1529 if (!attribute_node
) {
1530 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1534 if (!strcmp((const char *) node
->name
,
1535 config_element_probe_attributes
)) {
1536 xmlNodePtr probe_attribute_node
;
1538 /* probe_attributes */
1539 for (probe_attribute_node
=
1540 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1541 probe_attribute_node
= xmlNextElementSibling(
1542 probe_attribute_node
)) {
1544 ret
= process_probe_attribute_node(probe_attribute_node
,
1553 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1555 /* function_attributes */
1556 content
= xmlNodeGetContent(symbol_node
);
1558 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1562 sym_len
= strlen((char *) content
);
1563 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1564 WARN("Function name too long.");
1565 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1570 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1577 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1578 filter_expression
, exclusion_count
, exclusions
);
1580 for (i
= 0; i
< exclusion_count
; i
++) {
1581 free(exclusions
[i
]);
1585 free(filter_expression
);
1590 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1591 const char *channel_name
)
1596 assert(events_node
);
1598 assert(channel_name
);
1600 for (node
= xmlFirstElementChild(events_node
); node
;
1601 node
= xmlNextElementSibling(node
)) {
1602 ret
= process_event_node(node
, handle
, channel_name
);
1612 int process_channel_attr_node(xmlNodePtr attr_node
,
1613 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1614 xmlNodePtr
*events_node
)
1620 assert(contexts_node
);
1621 assert(events_node
);
1623 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1628 content
= xmlNodeGetContent(attr_node
);
1630 ret
= -LTTNG_ERR_NOMEM
;
1634 name_len
= strlen((char *) content
);
1635 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1636 WARN("Channel name too long.");
1637 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1642 strncpy(channel
->name
, (const char *) content
, name_len
);
1644 } else if (!strcmp((const char *) attr_node
->name
,
1645 config_element_enabled
)) {
1650 content
= xmlNodeGetContent(attr_node
);
1652 ret
= -LTTNG_ERR_NOMEM
;
1656 ret
= parse_bool(content
, &enabled
);
1659 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1663 channel
->enabled
= enabled
;
1664 } else if (!strcmp((const char *) attr_node
->name
,
1665 config_element_overwrite_mode
)) {
1668 /* overwrite_mode */
1669 content
= xmlNodeGetContent(attr_node
);
1671 ret
= -LTTNG_ERR_NOMEM
;
1675 ret
= get_overwrite_mode(content
);
1678 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1682 channel
->attr
.overwrite
= ret
;
1683 } else if (!strcmp((const char *) attr_node
->name
,
1684 config_element_subbuf_size
)) {
1687 /* subbuffer_size */
1688 content
= xmlNodeGetContent(attr_node
);
1690 ret
= -LTTNG_ERR_NOMEM
;
1694 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1697 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1700 } else if (!strcmp((const char *) attr_node
->name
,
1701 config_element_num_subbuf
)) {
1704 /* subbuffer_count */
1705 content
= xmlNodeGetContent(attr_node
);
1707 ret
= -LTTNG_ERR_NOMEM
;
1711 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1714 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1717 } else if (!strcmp((const char *) attr_node
->name
,
1718 config_element_switch_timer_interval
)) {
1720 uint64_t switch_timer_interval
= 0;
1722 /* switch_timer_interval */
1723 content
= xmlNodeGetContent(attr_node
);
1725 ret
= -LTTNG_ERR_NOMEM
;
1729 ret
= parse_uint(content
, &switch_timer_interval
);
1732 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1736 if (switch_timer_interval
> UINT_MAX
) {
1737 WARN("switch_timer_interval out of range.");
1738 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1742 channel
->attr
.switch_timer_interval
=
1743 switch_timer_interval
;
1744 } else if (!strcmp((const char *) attr_node
->name
,
1745 config_element_read_timer_interval
)) {
1747 uint64_t read_timer_interval
= 0;
1749 /* read_timer_interval */
1750 content
= xmlNodeGetContent(attr_node
);
1752 ret
= -LTTNG_ERR_NOMEM
;
1756 ret
= parse_uint(content
, &read_timer_interval
);
1759 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1763 if (read_timer_interval
> UINT_MAX
) {
1764 WARN("read_timer_interval out of range.");
1765 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1769 channel
->attr
.read_timer_interval
=
1770 read_timer_interval
;
1771 } else if (!strcmp((const char *) attr_node
->name
,
1772 config_element_output_type
)) {
1776 content
= xmlNodeGetContent(attr_node
);
1778 ret
= -LTTNG_ERR_NOMEM
;
1782 ret
= get_output_type(content
);
1785 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1789 channel
->attr
.output
= ret
;
1790 } else if (!strcmp((const char *) attr_node
->name
,
1791 config_element_tracefile_size
)) {
1794 /* tracefile_size */
1795 content
= xmlNodeGetContent(attr_node
);
1797 ret
= -LTTNG_ERR_NOMEM
;
1801 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1804 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1807 } else if (!strcmp((const char *) attr_node
->name
,
1808 config_element_tracefile_count
)) {
1811 /* tracefile_count */
1812 content
= xmlNodeGetContent(attr_node
);
1814 ret
= -LTTNG_ERR_NOMEM
;
1818 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1824 } else if (!strcmp((const char *) attr_node
->name
,
1825 config_element_live_timer_interval
)) {
1827 uint64_t live_timer_interval
= 0;
1829 /* live_timer_interval */
1830 content
= xmlNodeGetContent(attr_node
);
1832 ret
= -LTTNG_ERR_NOMEM
;
1836 ret
= parse_uint(content
, &live_timer_interval
);
1839 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1843 if (live_timer_interval
> UINT_MAX
) {
1844 WARN("live_timer_interval out of range.");
1845 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1849 channel
->attr
.live_timer_interval
=
1850 live_timer_interval
;
1851 } else if (!strcmp((const char *) attr_node
->name
,
1852 config_element_events
)) {
1854 *events_node
= attr_node
;
1857 *contexts_node
= attr_node
;
1865 int process_context_node(xmlNodePtr context_node
,
1866 struct lttng_handle
*handle
, const char *channel_name
)
1869 struct lttng_event_context context
;
1870 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1873 assert(channel_name
);
1875 if (!context_child_node
) {
1876 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1880 memset(&context
, 0, sizeof(context
));
1882 if (!strcmp((const char *) context_child_node
->name
,
1883 config_element_type
)) {
1885 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1887 ret
= -LTTNG_ERR_NOMEM
;
1891 ret
= get_context_type(content
);
1894 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1900 xmlNodePtr perf_attr_node
;
1903 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1904 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1905 perf_attr_node
; perf_attr_node
=
1906 xmlNextElementSibling(perf_attr_node
)) {
1907 if (!strcmp((const char *) perf_attr_node
->name
,
1908 config_element_type
)) {
1913 content
= xmlNodeGetContent(perf_attr_node
);
1915 ret
= -LTTNG_ERR_NOMEM
;
1919 ret
= parse_uint(content
, &type
);
1922 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1926 if (type
> UINT32_MAX
) {
1927 WARN("perf context type out of range.");
1928 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1932 context
.u
.perf_counter
.type
= type
;
1933 } else if (!strcmp((const char *) perf_attr_node
->name
,
1934 config_element_config
)) {
1936 uint64_t config
= 0;
1939 content
= xmlNodeGetContent(perf_attr_node
);
1941 ret
= -LTTNG_ERR_NOMEM
;
1945 ret
= parse_uint(content
, &config
);
1948 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1952 context
.u
.perf_counter
.config
= config
;
1953 } else if (!strcmp((const char *) perf_attr_node
->name
,
1954 config_element_name
)) {
1959 content
= xmlNodeGetContent(perf_attr_node
);
1961 ret
= -LTTNG_ERR_NOMEM
;
1965 name_len
= strlen((char *) content
);
1966 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1967 WARN("perf context name too long.");
1968 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1973 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
1980 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
1986 int process_contexts_node(xmlNodePtr contexts_node
,
1987 struct lttng_handle
*handle
, const char *channel_name
)
1990 xmlNodePtr context_node
;
1992 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
1993 context_node
= xmlNextElementSibling(context_node
)) {
1994 ret
= process_context_node(context_node
, handle
, channel_name
);
2004 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2007 struct lttng_domain domain
= { 0 };
2008 struct lttng_handle
*handle
= NULL
;
2009 xmlNodePtr channels_node
= NULL
;
2012 assert(session_name
);
2014 ret
= init_domain(domain_node
, &domain
);
2019 handle
= lttng_create_handle(session_name
, &domain
);
2021 ret
= -LTTNG_ERR_NOMEM
;
2025 /* get the channels node */
2026 for (node
= xmlFirstElementChild(domain_node
); node
;
2027 node
= xmlNextElementSibling(node
)) {
2028 if (!strcmp((const char *) node
->name
,
2029 config_element_channels
)) {
2030 channels_node
= node
;
2035 if (!channels_node
) {
2039 /* create all channels */
2040 for (node
= xmlFirstElementChild(channels_node
); node
;
2041 node
= xmlNextElementSibling(node
)) {
2042 struct lttng_channel channel
;
2043 xmlNodePtr contexts_node
= NULL
;
2044 xmlNodePtr events_node
= NULL
;
2045 xmlNodePtr channel_attr_node
;
2047 memset(&channel
, 0, sizeof(channel
));
2048 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2050 for (channel_attr_node
= xmlFirstElementChild(node
);
2051 channel_attr_node
; channel_attr_node
=
2052 xmlNextElementSibling(channel_attr_node
)) {
2053 ret
= process_channel_attr_node(channel_attr_node
,
2054 &channel
, &contexts_node
, &events_node
);
2060 ret
= lttng_enable_channel(handle
, &channel
);
2065 ret
= process_events_node(events_node
, handle
, channel
.name
);
2070 ret
= process_contexts_node(contexts_node
, handle
,
2077 lttng_destroy_handle(handle
);
2082 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2085 int ret
, started
= -1, snapshot_mode
= -1;
2086 uint64_t live_timer_interval
= UINT64_MAX
;
2088 xmlNodePtr domains_node
= NULL
;
2089 xmlNodePtr output_node
= NULL
;
2091 struct lttng_domain
*kernel_domain
= NULL
;
2092 struct lttng_domain
*ust_domain
= NULL
;
2093 struct lttng_domain
*jul_domain
= NULL
;
2095 for (node
= xmlFirstElementChild(session_node
); node
;
2096 node
= xmlNextElementSibling(node
)) {
2097 if (!name
&& !strcmp((const char *) node
->name
,
2098 config_element_name
)) {
2100 xmlChar
*node_content
= xmlNodeGetContent(node
);
2101 if (!node_content
) {
2102 ret
= -LTTNG_ERR_NOMEM
;
2106 name
= (char *) node_content
;
2107 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2108 config_element_domains
)) {
2110 domains_node
= node
;
2111 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2112 config_element_started
)) {
2114 xmlChar
*node_content
= xmlNodeGetContent(node
);
2115 if (!node_content
) {
2116 ret
= -LTTNG_ERR_NOMEM
;
2120 ret
= parse_bool(node_content
, &started
);
2123 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2126 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2127 config_element_output
)) {
2131 /* attributes, snapshot_mode or live_timer_interval */
2132 xmlNodePtr attributes_child
=
2133 xmlFirstElementChild(node
);
2135 if (!strcmp((const char *) attributes_child
->name
,
2136 config_element_snapshot_mode
)) {
2138 xmlChar
*snapshot_mode_content
=
2139 xmlNodeGetContent(attributes_child
);
2140 if (!snapshot_mode_content
) {
2141 ret
= -LTTNG_ERR_NOMEM
;
2145 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2146 free(snapshot_mode_content
);
2148 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2152 /* live_timer_interval */
2153 xmlChar
*timer_interval_content
=
2154 xmlNodeGetContent(attributes_child
);
2155 if (!timer_interval_content
) {
2156 ret
= -LTTNG_ERR_NOMEM
;
2160 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2161 free(timer_interval_content
);
2163 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2171 /* Mandatory attribute, as defined in the session XSD */
2172 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2176 if (session_name
&& strcmp(name
, session_name
)) {
2177 /* This is not the session we are looking for */
2178 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2182 /* Init domains to create the session handles */
2183 for (node
= xmlFirstElementChild(domains_node
); node
;
2184 node
= xmlNextElementSibling(node
)) {
2185 struct lttng_domain
*domain
;
2187 domain
= zmalloc(sizeof(*domain
));
2189 ret
= -LTTNG_ERR_NOMEM
;
2193 ret
= init_domain(node
, domain
);
2195 goto domain_init_error
;
2198 switch (domain
->type
) {
2199 case LTTNG_DOMAIN_KERNEL
:
2200 if (kernel_domain
) {
2201 /* Same domain seen twice, invalid! */
2202 goto domain_init_error
;
2204 kernel_domain
= domain
;
2206 case LTTNG_DOMAIN_UST
:
2208 /* Same domain seen twice, invalid! */
2209 goto domain_init_error
;
2211 ust_domain
= domain
;
2213 case LTTNG_DOMAIN_JUL
:
2215 /* Same domain seen twice, invalid! */
2216 goto domain_init_error
;
2218 jul_domain
= domain
;
2221 WARN("Invalid domain type");
2222 goto domain_init_error
;
2227 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2232 /* Destroy session if it exists */
2233 ret
= lttng_destroy_session(name
);
2234 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2235 ERR("Failed to destroy existing session.");
2240 /* Create session type depending on output type */
2241 if (snapshot_mode
&& snapshot_mode
!= -1) {
2242 ret
= create_snapshot_session(name
, output_node
);
2243 } else if (live_timer_interval
&&
2244 live_timer_interval
!= UINT64_MAX
) {
2245 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2246 output_node
, live_timer_interval
);
2248 /* regular session */
2249 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2250 output_node
, UINT64_MAX
);
2257 for (node
= xmlFirstElementChild(domains_node
); node
;
2258 node
= xmlNextElementSibling(node
)) {
2259 ret
= process_domain_node(node
, name
);
2266 ret
= lttng_start_tracing(name
);
2272 free(kernel_domain
);
2280 int load_session_from_file(const char *path
, const char *session_name
,
2281 struct session_config_validation_ctx
*validation_ctx
, int override
)
2283 int ret
, session_found
= !session_name
;
2284 xmlDocPtr doc
= NULL
;
2285 xmlNodePtr sessions_node
;
2286 xmlNodePtr session_node
;
2290 assert(validation_ctx
);
2292 ret
= stat(path
, &sb
);
2294 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2298 doc
= xmlParseFile(path
);
2300 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2304 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2306 ERR("Session configuration file validation failed");
2307 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2311 sessions_node
= xmlDocGetRootElement(doc
);
2312 if (!sessions_node
) {
2316 for (session_node
= xmlFirstElementChild(sessions_node
);
2317 session_node
; session_node
=
2318 xmlNextElementSibling(session_node
)) {
2319 ret
= process_session_node(session_node
,
2320 session_name
, override
);
2321 if (session_name
&& ret
== 0) {
2322 /* Target session found and loaded */
2330 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2336 int load_session_from_path(const char *path
, const char *session_name
,
2337 struct session_config_validation_ctx
*validation_ctx
, int override
)
2339 int ret
, session_found
= !session_name
;
2340 DIR *directory
= NULL
;
2343 assert(validation_ctx
);
2345 directory
= opendir(path
);
2347 if (errno
!= ENOTDIR
) {
2348 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
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 file_path
= zmalloc(PATH_MAX
);
2371 ret
= -LTTNG_ERR_NOMEM
;
2376 strncpy(file_path
, path
, path_len
);
2377 if (file_path
[path_len
- 1] != '/') {
2378 file_path
[path_len
++] = '/';
2382 /* Search for *.lttng files */
2383 while (!readdir_r(directory
, entry
, &result
) && result
) {
2384 size_t file_name_len
= strlen(result
->d_name
);
2386 if (file_name_len
<=
2387 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2391 if (path_len
+ file_name_len
> PATH_MAX
) {
2395 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2396 result
->d_name
+ file_name_len
- sizeof(
2397 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2401 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2402 file_path
[path_len
+ file_name_len
] = '\0';
2404 ret
= load_session_from_file(file_path
, session_name
,
2405 validation_ctx
, override
);
2406 if (session_name
&& !ret
) {
2415 ret
= load_session_from_file(path
, session_name
,
2416 validation_ctx
, override
);
2426 if (closedir(directory
)) {
2431 if (!session_found
) {
2432 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2439 int config_load_session(const char *path
, const char *session_name
,
2443 struct session_config_validation_ctx validation_ctx
= { 0 };
2445 ret
= init_session_config_validation_ctx(&validation_ctx
);
2452 char *home_path
= utils_get_home_dir();
2456 ret
= asprintf(&path
, DEFAULT_SESSION_HOME_CONFIGPATH
,
2462 ret
= load_session_from_path(path
, NULL
,
2463 &validation_ctx
, 0);
2464 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2472 /* Try system session configuration path */
2473 ret
= load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH
, NULL
,
2474 &validation_ctx
, 0);
2475 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2479 ret
= access(path
, F_OK
);
2484 ret
= -LTTNG_ERR_INVALID
;
2485 WARN("Session configuration path does not exist.");
2488 ret
= -LTTNG_ERR_EPERM
;
2491 ret
= -LTTNG_ERR_UNK
;
2497 ret
= load_session_from_path(path
, session_name
,
2498 &validation_ctx
, override
);
2501 fini_session_config_validation_ctx(&validation_ctx
);