Commit | Line | Data |
---|---|---|
1501a7f3 JG |
1 | /* |
2 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * more details. | |
12 | * | |
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. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <assert.h> | |
1501a7f3 | 20 | #include <ctype.h> |
1501a7f3 JG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
36f2332b | 24 | #include <inttypes.h> |
dcf266c0 JG |
25 | #include <dirent.h> |
26 | #include <unistd.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/stat.h> | |
1501a7f3 JG |
29 | |
30 | #include <common/defaults.h> | |
31 | #include <common/error.h> | |
32 | #include <common/macros.h> | |
33 | #include <common/utils.h> | |
fb198a11 JG |
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> | |
dcf266c0 JG |
39 | #include <libxml/tree.h> |
40 | #include <lttng/lttng.h> | |
41 | #include <lttng/snapshot.h> | |
1501a7f3 JG |
42 | |
43 | #include "config.h" | |
36f2332b | 44 | #include "config-internal.h" |
1501a7f3 JG |
45 | |
46 | struct handler_filter_args { | |
47 | const char* section; | |
48 | config_entry_handler_cb handler; | |
49 | void *user_data; | |
50 | }; | |
51 | ||
dcf266c0 JG |
52 | struct session_config_validation_ctx { |
53 | xmlSchemaParserCtxtPtr parser_ctx; | |
54 | xmlSchemaPtr schema; | |
55 | xmlSchemaValidCtxtPtr schema_validation_ctx; | |
56 | }; | |
57 | ||
1501a7f3 JG |
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"; | |
36f2332b | 64 | const char * const config_xml_encoding = "UTF-8"; |
fb198a11 | 65 | const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */ |
36f2332b JG |
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"; | |
1501a7f3 | 69 | |
fb198a11 JG |
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"; | |
117 | ||
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"; | |
121 | ||
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"; | |
125 | ||
126 | const char * const config_overwrite_mode_discard = "DISCARD"; | |
127 | const char * const config_overwrite_mode_overwrite = "OVERWRITE"; | |
128 | ||
129 | const char * const config_output_type_splice = "SPLICE"; | |
130 | const char * const config_output_type_mmap = "MMAP"; | |
131 | ||
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"; | |
135 | ||
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"; | |
145 | ||
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"; | |
158 | ||
dcf266c0 JG |
159 | struct consumer_output { |
160 | int enabled; | |
161 | char *path; | |
162 | char *control_uri; | |
163 | char *data_uri; | |
164 | }; | |
165 | ||
1501a7f3 JG |
166 | static int config_entry_handler_filter(struct handler_filter_args *args, |
167 | const char *section, const char *name, const char *value) | |
168 | { | |
169 | int ret = 0; | |
170 | struct config_entry entry = { section, name, value }; | |
171 | ||
172 | assert(args); | |
173 | ||
174 | if (!section || !name || !value) { | |
175 | ret = -EIO; | |
176 | goto end; | |
177 | } | |
178 | ||
179 | if (args->section) { | |
180 | if (strcmp(args->section, section)) { | |
181 | goto end; | |
182 | } | |
183 | } | |
184 | ||
185 | ret = args->handler(&entry, args->user_data); | |
186 | end: | |
187 | return ret; | |
188 | } | |
189 | ||
190 | LTTNG_HIDDEN | |
191 | int config_get_section_entries(const char *override_path, const char *section, | |
192 | config_entry_handler_cb handler, void *user_data) | |
193 | { | |
194 | int ret = 0; | |
195 | FILE *config_file = NULL; | |
196 | struct handler_filter_args filter = { section, handler, user_data }; | |
197 | ||
198 | if (override_path) { | |
199 | config_file = fopen(override_path, "r"); | |
200 | if (config_file) { | |
201 | DBG("Loaded daemon configuration file at %s", | |
202 | override_path); | |
203 | } else { | |
204 | ERR("Failed to open daemon configuration file at %s", | |
205 | override_path); | |
206 | ret = -ENOENT; | |
207 | goto end; | |
208 | } | |
209 | } else { | |
210 | char *path = utils_get_home_dir(); | |
211 | ||
212 | /* Try to open the user's daemon configuration file */ | |
213 | if (path) { | |
214 | ret = asprintf(&path, DEFAULT_DAEMON_HOME_CONFIGPATH, path); | |
215 | if (ret < 0) { | |
216 | goto end; | |
217 | } | |
218 | ||
219 | ret = 0; | |
220 | config_file = fopen(path, "r"); | |
221 | if (config_file) { | |
222 | DBG("Loaded daemon configuration file at %s", path); | |
223 | } | |
224 | ||
225 | free(path); | |
226 | } | |
227 | ||
228 | /* Try to open the system daemon configuration file */ | |
229 | if (!config_file) { | |
230 | config_file = fopen(DEFAULT_DAEMON_HOME_CONFIGPATH, "r"); | |
231 | } | |
232 | } | |
233 | ||
234 | if (!config_file) { | |
235 | DBG("No daemon configuration file found."); | |
236 | goto end; | |
237 | } | |
238 | ||
239 | ret = ini_parse_file(config_file, | |
240 | (ini_entry_handler) config_entry_handler_filter, (void *) &filter); | |
241 | ||
242 | end: | |
243 | return ret; | |
244 | } | |
245 | ||
246 | LTTNG_HIDDEN | |
247 | int config_parse_value(const char *value) | |
248 | { | |
249 | int i, ret = 0; | |
250 | char *endptr, *lower_str; | |
251 | size_t len; | |
252 | unsigned long v; | |
253 | ||
254 | len = strlen(value); | |
255 | if (!len) { | |
256 | ret = -1; | |
257 | goto end; | |
258 | } | |
259 | ||
260 | v = strtoul(value, &endptr, 10); | |
261 | if (endptr != value) { | |
262 | ret = v; | |
263 | goto end; | |
264 | } | |
265 | ||
266 | lower_str = zmalloc(len + 1); | |
267 | if (!lower_str) { | |
268 | PERROR("zmalloc"); | |
269 | ret = -errno; | |
270 | goto end; | |
271 | } | |
272 | ||
273 | for (i = 0; i < len; i++) { | |
274 | lower_str[i] = tolower(value[i]); | |
275 | } | |
276 | ||
277 | if (!strcmp(lower_str, config_str_yes) || | |
278 | !strcmp(lower_str, config_str_true) || | |
279 | !strcmp(lower_str, config_str_on)) { | |
280 | ret = 1; | |
281 | } else if (!strcmp(lower_str, config_str_no) || | |
282 | !strcmp(lower_str, config_str_false) || | |
283 | !strcmp(lower_str, config_str_off)) { | |
284 | ret = 0; | |
285 | } else { | |
286 | ret = -1; | |
287 | } | |
288 | ||
289 | free(lower_str); | |
290 | end: | |
291 | return ret; | |
292 | } | |
36f2332b JG |
293 | |
294 | /* | |
295 | * Returns a xmlChar string which must be released using xmlFree(). | |
296 | */ | |
297 | static xmlChar *encode_string(const char *in_str) | |
298 | { | |
299 | xmlChar *out_str = NULL; | |
300 | xmlCharEncodingHandlerPtr handler; | |
301 | int out_len, ret, in_len; | |
302 | ||
303 | assert(in_str); | |
304 | ||
305 | handler = xmlFindCharEncodingHandler(config_xml_encoding); | |
306 | if (!handler) { | |
307 | ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!"); | |
308 | goto end; | |
309 | } | |
310 | ||
311 | in_len = strlen(in_str); | |
312 | /* | |
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 | |
315 | * bytes. | |
316 | */ | |
317 | out_len = (in_len * 2) + 1; | |
318 | out_str = xmlMalloc(out_len); | |
319 | if (!out_str) { | |
320 | goto end; | |
321 | } | |
322 | ||
323 | ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len); | |
324 | if (ret < 0) { | |
325 | xmlFree(out_str); | |
326 | out_str = NULL; | |
327 | goto end; | |
328 | } | |
329 | ||
330 | /* out_len is now the size of out_str */ | |
331 | out_str[out_len] = '\0'; | |
332 | end: | |
333 | return out_str; | |
334 | } | |
335 | ||
336 | LTTNG_HIDDEN | |
337 | struct config_writer *config_writer_create(int fd_output) | |
338 | { | |
339 | int ret; | |
340 | struct config_writer *writer; | |
341 | xmlOutputBufferPtr buffer; | |
342 | ||
343 | writer = zmalloc(sizeof(struct config_writer)); | |
344 | if (!writer) { | |
345 | PERROR("zmalloc config_writer_create"); | |
346 | goto end; | |
347 | } | |
348 | ||
349 | buffer = xmlOutputBufferCreateFd(fd_output, NULL); | |
350 | if (!buffer) { | |
351 | goto error_destroy; | |
352 | } | |
353 | ||
354 | writer->writer = xmlNewTextWriter(buffer); | |
355 | ret = xmlTextWriterStartDocument(writer->writer, NULL, | |
356 | config_xml_encoding, NULL); | |
357 | if (ret < 0) { | |
358 | goto error_destroy; | |
359 | } | |
360 | ||
361 | ret = xmlTextWriterSetIndentString(writer->writer, | |
362 | BAD_CAST config_xml_indent_string); | |
363 | if (ret) { | |
364 | goto error_destroy; | |
365 | } | |
366 | ||
367 | ret = xmlTextWriterSetIndent(writer->writer, 1); | |
368 | if (ret) { | |
369 | goto error_destroy; | |
370 | } | |
371 | ||
372 | end: | |
373 | return writer; | |
374 | error_destroy: | |
375 | config_writer_destroy(writer); | |
376 | return NULL; | |
377 | } | |
378 | ||
379 | LTTNG_HIDDEN | |
380 | int config_writer_destroy(struct config_writer *writer) | |
381 | { | |
382 | int ret = 0; | |
383 | ||
384 | if (!writer) { | |
385 | ret = -EINVAL; | |
386 | goto end; | |
387 | } | |
388 | ||
389 | if (xmlTextWriterEndDocument(writer->writer) < 0) { | |
390 | WARN("Could not close XML document"); | |
391 | ret = -EIO; | |
392 | } | |
393 | ||
394 | if (writer->writer) { | |
395 | xmlFreeTextWriter(writer->writer); | |
396 | } | |
397 | ||
398 | free(writer); | |
399 | end: | |
400 | return ret; | |
401 | } | |
402 | ||
403 | LTTNG_HIDDEN | |
404 | int config_writer_open_element(struct config_writer *writer, | |
405 | const char *element_name) | |
406 | { | |
407 | int ret; | |
408 | xmlChar *encoded_element_name; | |
409 | ||
410 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
411 | ret = -1; | |
412 | goto end; | |
413 | } | |
414 | ||
415 | encoded_element_name = encode_string(element_name); | |
416 | if (!encoded_element_name) { | |
417 | ret = -1; | |
418 | goto end; | |
419 | } | |
420 | ||
421 | ret = xmlTextWriterStartElement(writer->writer, encoded_element_name); | |
422 | xmlFree(encoded_element_name); | |
423 | end: | |
424 | return ret > 0 ? 0 : ret; | |
425 | } | |
426 | ||
427 | LTTNG_HIDDEN | |
428 | int config_writer_close_element(struct config_writer *writer) | |
429 | { | |
430 | int ret; | |
431 | ||
432 | if (!writer || !writer->writer) { | |
433 | ret = -1; | |
434 | goto end; | |
435 | } | |
436 | ||
437 | ret = xmlTextWriterEndElement(writer->writer); | |
438 | end: | |
439 | return ret > 0 ? 0 : ret; | |
440 | } | |
441 | ||
442 | LTTNG_HIDDEN | |
443 | int config_writer_write_element_unsigned_int(struct config_writer *writer, | |
444 | const char *element_name, uint64_t value) | |
445 | { | |
446 | int ret; | |
447 | xmlChar *encoded_element_name; | |
448 | ||
449 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
450 | ret = -1; | |
451 | goto end; | |
452 | } | |
453 | ||
454 | encoded_element_name = encode_string(element_name); | |
455 | if (!encoded_element_name) { | |
456 | ret = -1; | |
457 | goto end; | |
458 | } | |
459 | ||
460 | ret = xmlTextWriterWriteFormatElement(writer->writer, | |
461 | encoded_element_name, "%" PRIu64, value); | |
462 | xmlFree(encoded_element_name); | |
463 | end: | |
464 | return ret > 0 ? 0 : ret; | |
465 | } | |
466 | ||
467 | LTTNG_HIDDEN | |
468 | int config_writer_write_element_signed_int(struct config_writer *writer, | |
469 | const char *element_name, int64_t value) | |
470 | { | |
471 | int ret; | |
472 | xmlChar *encoded_element_name; | |
473 | ||
474 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
475 | ret = -1; | |
476 | goto end; | |
477 | } | |
478 | ||
479 | encoded_element_name = encode_string(element_name); | |
480 | if (!encoded_element_name) { | |
481 | ret = -1; | |
482 | goto end; | |
483 | } | |
484 | ||
485 | ret = xmlTextWriterWriteFormatElement(writer->writer, | |
486 | encoded_element_name, "%" PRIi64, value); | |
487 | xmlFree(encoded_element_name); | |
488 | end: | |
489 | return ret > 0 ? 0 : ret; | |
490 | } | |
491 | ||
492 | LTTNG_HIDDEN | |
493 | int config_writer_write_element_bool(struct config_writer *writer, | |
494 | const char *element_name, int value) | |
495 | { | |
496 | return config_writer_write_element_string(writer, element_name, | |
497 | value ? config_xml_true : config_xml_false); | |
498 | } | |
499 | ||
500 | LTTNG_HIDDEN | |
501 | int config_writer_write_element_string(struct config_writer *writer, | |
502 | const char *element_name, const char *value) | |
503 | { | |
504 | int ret; | |
505 | xmlChar *encoded_element_name = NULL; | |
506 | xmlChar *encoded_value = NULL; | |
507 | ||
508 | if (!writer || !writer->writer || !element_name || !element_name[0] || | |
509 | !value) { | |
510 | ret = -1; | |
511 | goto end; | |
512 | } | |
513 | ||
514 | encoded_element_name = encode_string(element_name); | |
515 | if (!encoded_element_name) { | |
516 | ret = -1; | |
517 | goto end; | |
518 | } | |
519 | ||
520 | encoded_value = encode_string(value); | |
521 | if (!encoded_value) { | |
522 | ret = -1; | |
523 | goto end; | |
524 | } | |
525 | ||
526 | ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name, | |
dcf266c0 | 527 | encoded_value); |
36f2332b JG |
528 | end: |
529 | xmlFree(encoded_element_name); | |
530 | xmlFree(encoded_value); | |
531 | return ret > 0 ? 0 : ret; | |
532 | } | |
dcf266c0 JG |
533 | |
534 | static | |
535 | void xml_error_handler(void *ctx, const char *format, ...) | |
536 | { | |
537 | char *errMsg; | |
538 | va_list args; | |
539 | int ret; | |
540 | ||
541 | va_start(args, format); | |
542 | ret = vasprintf(&errMsg, format, args); | |
6c043b48 | 543 | va_end(args); |
dcf266c0 JG |
544 | if (ret == -1) { |
545 | ERR("String allocation failed in xml error handler"); | |
546 | return; | |
547 | } | |
dcf266c0 JG |
548 | |
549 | fprintf(stderr, "XML Error: %s", errMsg); | |
550 | free(errMsg); | |
551 | } | |
552 | ||
553 | static | |
554 | void fini_session_config_validation_ctx( | |
555 | struct session_config_validation_ctx *ctx) | |
556 | { | |
557 | if (ctx->parser_ctx) { | |
558 | xmlSchemaFreeParserCtxt(ctx->parser_ctx); | |
559 | } | |
560 | ||
561 | if (ctx->schema) { | |
562 | xmlSchemaFree(ctx->schema); | |
563 | } | |
564 | ||
565 | if (ctx->schema_validation_ctx) { | |
566 | xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx); | |
567 | } | |
568 | ||
569 | memset(ctx, 0, sizeof(struct session_config_validation_ctx)); | |
570 | } | |
571 | ||
572 | static | |
573 | int init_session_config_validation_ctx( | |
574 | struct session_config_validation_ctx *ctx) | |
575 | { | |
576 | int ret; | |
577 | ||
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; | |
582 | goto end; | |
583 | } | |
584 | xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler, | |
585 | xml_error_handler, NULL); | |
586 | ||
587 | ctx->schema = xmlSchemaParse(ctx->parser_ctx); | |
588 | if (!ctx->schema) { | |
589 | ERR("XSD parsing failed"); | |
590 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
591 | goto end; | |
592 | } | |
593 | ||
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; | |
598 | goto end; | |
599 | } | |
600 | ||
601 | xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler, | |
602 | xml_error_handler, NULL); | |
603 | ret = 0; | |
604 | ||
605 | end: | |
606 | if (ret) { | |
607 | fini_session_config_validation_ctx(ctx); | |
608 | } | |
609 | ||
610 | return ret; | |
611 | } | |
612 | ||
613 | static | |
614 | int parse_uint(xmlChar *str, uint64_t *val) | |
615 | { | |
616 | int ret; | |
617 | char *endptr; | |
618 | ||
619 | if (!str || !val) { | |
620 | ret = -1; | |
621 | goto end; | |
622 | } | |
623 | ||
624 | *val = strtoull((const char *) str, &endptr, 10); | |
625 | if (!endptr || *endptr) { | |
626 | ret = -1; | |
627 | } else { | |
628 | ret = 0; | |
629 | } | |
630 | ||
631 | end: | |
632 | return ret; | |
633 | } | |
634 | ||
635 | static | |
636 | int parse_int(xmlChar *str, int64_t *val) | |
637 | { | |
638 | int ret; | |
639 | char *endptr; | |
640 | ||
641 | if (!str || !val) { | |
642 | ret = -1; | |
643 | goto end; | |
644 | } | |
645 | ||
646 | *val = strtoll((const char *) str, &endptr, 10); | |
647 | if (!endptr || *endptr) { | |
648 | ret = -1; | |
649 | } else { | |
650 | ret = 0; | |
651 | } | |
652 | ||
653 | end: | |
654 | return ret; | |
655 | } | |
656 | ||
657 | static | |
658 | int parse_bool(xmlChar *str, int *val) | |
659 | { | |
660 | int ret = 0; | |
661 | ||
662 | if (!str || !val) { | |
663 | ret = -1; | |
664 | goto end; | |
665 | } | |
666 | ||
667 | if (!strcmp((const char *) str, config_xml_true)) { | |
668 | *val = 1; | |
669 | } else if (!strcmp((const char *) str, config_xml_false)) { | |
670 | *val = 0; | |
671 | } else { | |
672 | WARN("Invalid boolean value encoutered (%s).", | |
673 | (const char *) str); | |
674 | ret = -1; | |
675 | } | |
676 | end: | |
677 | return ret; | |
678 | } | |
679 | ||
680 | static | |
681 | int get_domain_type(xmlChar *domain) | |
682 | { | |
683 | int ret; | |
684 | ||
685 | if (!domain) { | |
686 | goto error; | |
687 | } | |
688 | ||
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; | |
695 | } else { | |
696 | goto error; | |
697 | } | |
698 | ||
699 | return ret; | |
700 | error: | |
701 | return -1; | |
702 | } | |
703 | ||
704 | static | |
705 | int get_buffer_type(xmlChar *buffer_type) | |
706 | { | |
707 | int ret; | |
708 | ||
709 | if (!buffer_type) { | |
710 | goto error; | |
711 | } | |
712 | ||
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; | |
719 | } else { | |
720 | goto error; | |
721 | } | |
722 | ||
723 | return ret; | |
724 | error: | |
725 | return -1; | |
726 | } | |
727 | ||
728 | static | |
729 | int get_overwrite_mode(xmlChar *overwrite_mode) | |
730 | { | |
731 | int ret; | |
732 | ||
733 | if (!overwrite_mode) { | |
734 | goto error; | |
735 | } | |
736 | ||
737 | if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) { | |
738 | ret = 1; | |
739 | } else if (!strcmp((char *) overwrite_mode, | |
740 | config_overwrite_mode_discard)) { | |
741 | ret = 0; | |
742 | } else { | |
743 | goto error; | |
744 | } | |
745 | ||
746 | return ret; | |
747 | error: | |
748 | return -1; | |
749 | } | |
750 | ||
751 | static | |
752 | int get_output_type(xmlChar *output_type) | |
753 | { | |
754 | int ret; | |
755 | ||
756 | if (!output_type) { | |
757 | goto error; | |
758 | } | |
759 | ||
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; | |
764 | } else { | |
765 | goto error; | |
766 | } | |
767 | ||
768 | return ret; | |
769 | error: | |
770 | return -1; | |
771 | } | |
772 | ||
773 | static | |
774 | int get_event_type(xmlChar *event_type) | |
775 | { | |
776 | int ret; | |
777 | ||
778 | if (!event_type) { | |
779 | goto error; | |
780 | } | |
781 | ||
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; | |
797 | } else { | |
798 | goto error; | |
799 | } | |
800 | ||
801 | return ret; | |
802 | error: | |
803 | return -1; | |
804 | } | |
805 | ||
806 | static | |
807 | int get_loglevel_type(xmlChar *loglevel_type) | |
808 | { | |
809 | int ret; | |
810 | ||
811 | if (!loglevel_type) { | |
812 | goto error; | |
813 | } | |
814 | ||
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; | |
823 | } else { | |
824 | goto error; | |
825 | } | |
826 | ||
827 | return ret; | |
828 | error: | |
829 | return -1; | |
830 | } | |
831 | ||
832 | /* | |
833 | * Return the context type or -1 on error. | |
834 | */ | |
835 | static | |
836 | int get_context_type(xmlChar *context_type) | |
837 | { | |
838 | int ret; | |
839 | ||
840 | if (!context_type) { | |
841 | goto error; | |
842 | } | |
843 | ||
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; | |
879 | } else { | |
880 | goto error; | |
881 | } | |
882 | ||
883 | return ret; | |
884 | error: | |
885 | return -1; | |
886 | } | |
887 | ||
888 | static | |
889 | int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain) | |
890 | { | |
891 | int ret; | |
892 | xmlNodePtr node; | |
893 | ||
894 | for (node = xmlFirstElementChild(domain_node); node; | |
895 | node = xmlNextElementSibling(node)) { | |
896 | if (!strcmp((const char *) node->name, config_element_type)) { | |
897 | /* domain type */ | |
898 | xmlChar *node_content = xmlNodeGetContent(node); | |
899 | if (!node_content) { | |
900 | ret = -LTTNG_ERR_NOMEM; | |
901 | goto end; | |
902 | } | |
903 | ||
904 | ret = get_domain_type(node_content); | |
905 | free(node_content); | |
906 | if (ret < 0) { | |
907 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
908 | goto end; | |
909 | } | |
910 | ||
911 | domain->type = ret; | |
912 | } else if (!strcmp((const char *) node->name, | |
913 | config_element_buffer_type)) { | |
914 | /* buffer type */ | |
915 | xmlChar *node_content = xmlNodeGetContent(node); | |
916 | if (!node_content) { | |
917 | ret = -LTTNG_ERR_NOMEM; | |
918 | goto end; | |
919 | } | |
920 | ||
921 | ret = get_buffer_type(node_content); | |
922 | free(node_content); | |
923 | if (ret < 0) { | |
924 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
925 | goto end; | |
926 | } | |
927 | ||
928 | domain->buf_type = ret; | |
929 | } | |
930 | } | |
931 | ret = 0; | |
932 | end: | |
933 | return ret; | |
934 | } | |
935 | ||
936 | static | |
937 | int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri, | |
938 | char **data_uri) | |
939 | { | |
940 | xmlNodePtr node; | |
941 | ||
942 | for (node = xmlFirstElementChild(net_output_node); node; | |
943 | node = xmlNextElementSibling(node)) { | |
944 | if (!strcmp((const char *) node->name, config_element_control_uri)) { | |
945 | /* control_uri */ | |
946 | *control_uri = (char *) xmlNodeGetContent(node); | |
947 | if (!*control_uri) { | |
948 | break; | |
949 | } | |
950 | } else { | |
951 | /* data_uri */ | |
952 | *data_uri = (char *) xmlNodeGetContent(node); | |
953 | if (!*data_uri) { | |
954 | break; | |
955 | } | |
956 | } | |
957 | } | |
958 | ||
959 | return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
960 | } | |
961 | ||
962 | static | |
963 | int process_consumer_output(xmlNodePtr consumer_output_node, | |
964 | struct consumer_output *output) | |
965 | { | |
966 | int ret; | |
967 | xmlNodePtr node; | |
968 | ||
969 | assert(output); | |
970 | ||
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); | |
975 | ||
976 | /* enabled */ | |
977 | if (!enabled_str) { | |
978 | ret = -LTTNG_ERR_NOMEM; | |
979 | goto end; | |
980 | } | |
981 | ||
982 | ret = parse_bool(enabled_str, &output->enabled); | |
983 | free(enabled_str); | |
984 | if (ret) { | |
985 | goto end; | |
986 | } | |
987 | } else { | |
988 | xmlNodePtr output_type_node; | |
989 | ||
990 | /* destination */ | |
991 | output_type_node = xmlFirstElementChild(node); | |
992 | if (!output_type_node) { | |
993 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
994 | goto end; | |
995 | } | |
996 | ||
997 | if (!strcmp((const char *) output_type_node->name, | |
998 | config_element_path)) { | |
999 | /* path */ | |
1000 | output->path = (char *) xmlNodeGetContent(output_type_node); | |
1001 | if (!output->path) { | |
1002 | ret = -LTTNG_ERR_NOMEM; | |
1003 | goto end; | |
1004 | } | |
1005 | } else { | |
1006 | /* net_output */ | |
1007 | ret = get_net_output_uris(output_type_node, | |
1008 | &output->control_uri, &output->data_uri); | |
1009 | if (ret) { | |
1010 | goto end; | |
1011 | } | |
1012 | } | |
1013 | } | |
1014 | } | |
1015 | ret = 0; | |
1016 | ||
1017 | end: | |
1018 | if (ret) { | |
1019 | free(output->path); | |
1020 | free(output->control_uri); | |
1021 | free(output->data_uri); | |
1022 | memset(output, 0, sizeof(struct consumer_output)); | |
1023 | } | |
1024 | return ret; | |
1025 | } | |
1026 | ||
1027 | static | |
1028 | int create_session_net_output(const char *name, struct lttng_domain *domain, | |
1029 | const char *control_uri, const char *data_uri) | |
1030 | { | |
1031 | int ret; | |
1032 | struct lttng_handle *handle; | |
1033 | const char *uri = NULL; | |
1034 | ||
1035 | assert(name); | |
1036 | assert(domain); | |
1037 | ||
1038 | handle = lttng_create_handle(name, domain); | |
1039 | if (!handle) { | |
1040 | ret = -LTTNG_ERR_NOMEM; | |
1041 | goto end; | |
1042 | } | |
1043 | ||
1044 | if (!control_uri || !data_uri) { | |
1045 | uri = control_uri ? control_uri : data_uri; | |
1046 | control_uri = uri; | |
1047 | data_uri = uri; | |
1048 | } | |
1049 | ||
1050 | ret = lttng_set_consumer_url(handle, control_uri, data_uri); | |
1051 | lttng_destroy_handle(handle); | |
1052 | end: | |
1053 | return ret; | |
1054 | } | |
1055 | ||
1056 | static | |
1057 | int create_snapshot_session(const char *session_name, xmlNodePtr output_node) | |
1058 | { | |
1059 | int ret; | |
1060 | xmlNodePtr node = NULL; | |
1061 | xmlNodePtr snapshot_output_list_node; | |
1062 | xmlNodePtr snapshot_output_node; | |
1063 | ||
1064 | assert(session_name); | |
1065 | ||
1066 | ret = lttng_create_session_snapshot(session_name, NULL); | |
1067 | if (ret) { | |
1068 | goto end; | |
1069 | } | |
1070 | ||
1071 | if (!output_node) { | |
1072 | goto end; | |
1073 | } | |
1074 | ||
1075 | snapshot_output_list_node = xmlFirstElementChild(output_node); | |
1076 | ||
1077 | /* Parse and create snapshot outputs */ | |
1078 | ||
1079 | for (snapshot_output_node = | |
1080 | xmlFirstElementChild(snapshot_output_list_node); | |
1081 | snapshot_output_node; snapshot_output_node = | |
1082 | xmlNextElementSibling(snapshot_output_node)) { | |
1083 | char *name = NULL; | |
1084 | uint64_t max_size = UINT64_MAX; | |
1085 | struct consumer_output output = { 0 }; | |
1086 | struct lttng_snapshot_output *snapshot_output = NULL; | |
1087 | ||
1088 | for (node = xmlFirstElementChild(snapshot_output_node); node; | |
1089 | node = xmlNextElementSibling(node)) { | |
1090 | if (!strcmp((const char *) node->name, | |
1091 | config_element_name)) { | |
1092 | /* name */ | |
1093 | name = (char *) xmlNodeGetContent(node); | |
1094 | if (!name) { | |
1095 | ret = -LTTNG_ERR_NOMEM; | |
1096 | goto error_snapshot_output; | |
1097 | } | |
1098 | } else if (!strcmp((const char *) node->name, | |
1099 | config_element_max_size)) { | |
1100 | xmlChar *content = xmlNodeGetContent(node); | |
1101 | ||
1102 | /* max_size */ | |
1103 | if (!content) { | |
1104 | ret = -LTTNG_ERR_NOMEM; | |
1105 | goto error_snapshot_output; | |
1106 | } | |
1107 | ret = parse_uint(content, &max_size); | |
1108 | free(content); | |
1109 | if (ret) { | |
1110 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1111 | goto error_snapshot_output; | |
1112 | } | |
1113 | } else { | |
1114 | /* consumer_output */ | |
1115 | ret = process_consumer_output(node, &output); | |
1116 | if (ret) { | |
1117 | goto error_snapshot_output; | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | snapshot_output = lttng_snapshot_output_create(); | |
1123 | if (!snapshot_output) { | |
1124 | ret = -LTTNG_ERR_NOMEM; | |
1125 | goto error_snapshot_output; | |
1126 | } | |
1127 | ||
1128 | ret = lttng_snapshot_output_set_name(name, snapshot_output); | |
1129 | if (ret) { | |
1130 | goto error_snapshot_output; | |
1131 | } | |
1132 | ||
1133 | ret = lttng_snapshot_output_set_size(max_size, snapshot_output); | |
1134 | if (ret) { | |
1135 | goto error_snapshot_output; | |
1136 | } | |
1137 | ||
1138 | if (output.path) { | |
1139 | ret = lttng_snapshot_output_set_ctrl_url(output.path, | |
1140 | snapshot_output); | |
1141 | if (ret) { | |
1142 | goto error_snapshot_output; | |
1143 | } | |
1144 | } else { | |
1145 | if (output.control_uri) { | |
1146 | ret = lttng_snapshot_output_set_ctrl_url(output.control_uri, | |
1147 | snapshot_output); | |
1148 | if (ret) { | |
1149 | goto error_snapshot_output; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | if (output.data_uri) { | |
1154 | ret = lttng_snapshot_output_set_data_url(output.data_uri, | |
1155 | snapshot_output); | |
1156 | if (ret) { | |
1157 | goto error_snapshot_output; | |
1158 | } | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | ret = lttng_snapshot_add_output(session_name, snapshot_output); | |
1163 | error_snapshot_output: | |
1164 | free(name); | |
1165 | free(output.path); | |
1166 | free(output.control_uri); | |
1167 | free(output.data_uri); | |
1168 | lttng_snapshot_output_destroy(snapshot_output); | |
1169 | if (ret) { | |
1170 | goto end; | |
1171 | } | |
1172 | } | |
1173 | end: | |
1174 | return ret; | |
1175 | } | |
1176 | ||
1177 | static | |
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) | |
1184 | { | |
1185 | int ret; | |
1186 | struct consumer_output output = { 0 }; | |
1187 | xmlNodePtr consumer_output_node; | |
1188 | ||
1189 | assert(name); | |
1190 | assert(kernel_domain); | |
1191 | assert(ust_domain); | |
1192 | assert(jul_domain); | |
1193 | ||
1194 | if (output_node) { | |
1195 | consumer_output_node = xmlFirstElementChild(output_node); | |
1196 | if (!consumer_output_node) { | |
1197 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1198 | goto end; | |
1199 | } | |
1200 | ||
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; | |
1206 | goto end; | |
1207 | } | |
1208 | ||
1209 | ret = process_consumer_output(consumer_output_node, &output); | |
1210 | if (ret) { | |
1211 | goto end; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if (live_timer_interval != UINT64_MAX && | |
1216 | !output.control_uri && !output.data_uri) { | |
1217 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1218 | goto end; | |
1219 | } | |
1220 | ||
1221 | if (output.control_uri || output.data_uri) { | |
1222 | int i; | |
1223 | struct lttng_domain *domain; | |
1224 | struct lttng_domain *domains[] = | |
1225 | { kernel_domain, ust_domain, jul_domain }; | |
1226 | ||
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; | |
1231 | ||
1232 | /* URL has to be provided, even if we'll overwrite it after. */ | |
1233 | ret = lttng_create_session_live(name, url, live_timer_interval); | |
1234 | } else { | |
1235 | ret = lttng_create_session(name, NULL); | |
1236 | } | |
1237 | if (ret) { | |
1238 | goto end; | |
1239 | } | |
1240 | ||
301423df | 1241 | for (i = 0; i < (sizeof(domains) / sizeof(domains[0])); i++) { |
dcf266c0 JG |
1242 | domain = domains[i]; |
1243 | if (!domain) { | |
1244 | continue; | |
1245 | } | |
1246 | ||
1247 | ret = create_session_net_output(name, domain, output.control_uri, | |
1248 | output.data_uri); | |
1249 | if (ret) { | |
1250 | goto end; | |
1251 | } | |
1252 | } | |
1253 | } else { | |
1254 | /* either local output or no output */ | |
1255 | ret = lttng_create_session(name, output.path); | |
1256 | if (ret) { | |
1257 | goto end; | |
1258 | } | |
1259 | } | |
1260 | end: | |
1261 | free(output.path); | |
1262 | free(output.control_uri); | |
1263 | free(output.data_uri); | |
1264 | return ret; | |
1265 | } | |
1266 | static | |
1267 | int process_probe_attribute_node(xmlNodePtr probe_attribute_node, | |
1268 | struct lttng_event_probe_attr *attr) | |
1269 | { | |
1270 | int ret; | |
1271 | ||
1272 | assert(probe_attribute_node); | |
1273 | assert(attr); | |
1274 | ||
1275 | if (!strcmp((const char *) probe_attribute_node->name, | |
1276 | config_element_address)) { | |
1277 | xmlChar *content; | |
1278 | uint64_t addr = 0; | |
1279 | ||
1280 | /* addr */ | |
1281 | content = xmlNodeGetContent(probe_attribute_node); | |
1282 | if (!content) { | |
1283 | ret = -LTTNG_ERR_NOMEM; | |
1284 | goto end; | |
1285 | } | |
1286 | ||
1287 | ret = parse_uint(content, &addr); | |
1288 | free(content); | |
1289 | if (ret) { | |
1290 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1291 | goto end; | |
1292 | } | |
1293 | ||
1294 | attr->addr = addr; | |
1295 | } else if (!strcmp((const char *) probe_attribute_node->name, | |
1296 | config_element_offset)) { | |
1297 | xmlChar *content; | |
1298 | uint64_t offset = 0; | |
1299 | ||
1300 | /* offset */ | |
1301 | content = xmlNodeGetContent(probe_attribute_node); | |
1302 | if (!content) { | |
1303 | ret = -LTTNG_ERR_NOMEM; | |
1304 | goto end; | |
1305 | } | |
1306 | ||
1307 | ret = parse_uint(content, &offset); | |
1308 | free(content); | |
1309 | if (ret) { | |
1310 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1311 | goto end; | |
1312 | } | |
1313 | ||
1314 | attr->offset = offset; | |
1315 | } else if (!strcmp((const char *) probe_attribute_node->name, | |
1316 | config_element_symbol_name)) { | |
1317 | xmlChar *content; | |
1318 | size_t name_len; | |
1319 | ||
1320 | /* symbol_name */ | |
1321 | content = xmlNodeGetContent(probe_attribute_node); | |
1322 | if (!content) { | |
1323 | ret = -LTTNG_ERR_NOMEM; | |
1324 | goto end; | |
1325 | } | |
1326 | ||
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; | |
1331 | free(content); | |
1332 | goto end; | |
1333 | } | |
1334 | ||
1335 | strncpy(attr->symbol_name, (const char *) content, name_len); | |
1336 | free(content); | |
1337 | } | |
1338 | ret = 0; | |
1339 | end: | |
1340 | return ret; | |
1341 | } | |
1342 | ||
1343 | static | |
1344 | int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle, | |
1345 | const char *channel_name) | |
1346 | { | |
1347 | int ret, i; | |
1348 | xmlNodePtr node; | |
1349 | struct lttng_event event; | |
1350 | char **exclusions = NULL; | |
1351 | unsigned long exclusion_count = 0; | |
1352 | char *filter_expression = NULL; | |
1353 | ||
1354 | assert(event_node); | |
1355 | assert(handle); | |
1356 | assert(channel_name); | |
1357 | ||
1358 | memset(&event, 0, sizeof(event)); | |
1359 | ||
1360 | for (node = xmlFirstElementChild(event_node); node; | |
1361 | node = xmlNextElementSibling(node)) { | |
1362 | if (!strcmp((const char *) node->name, config_element_name)) { | |
1363 | xmlChar *content; | |
1364 | size_t name_len; | |
1365 | ||
1366 | /* name */ | |
1367 | content = xmlNodeGetContent(node); | |
1368 | if (!content) { | |
1369 | ret = -LTTNG_ERR_NOMEM; | |
1370 | goto end; | |
1371 | } | |
1372 | ||
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; | |
1377 | free(content); | |
1378 | goto end; | |
1379 | } | |
1380 | ||
1381 | strncpy(event.name, (const char *) content, name_len); | |
1382 | free(content); | |
1383 | } else if (!strcmp((const char *) node->name, | |
1384 | config_element_enabled)) { | |
1385 | xmlChar *content = xmlNodeGetContent(node); | |
1386 | ||
1387 | /* enabled */ | |
1388 | if (!content) { | |
1389 | ret = -LTTNG_ERR_NOMEM; | |
1390 | goto end; | |
1391 | } | |
1392 | ||
1393 | ret = parse_bool(content, &event.enabled); | |
1394 | free(content); | |
1395 | if (ret) { | |
1396 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1397 | goto end; | |
1398 | } | |
1399 | } else if (!strcmp((const char *) node->name, | |
1400 | config_element_type)) { | |
1401 | xmlChar *content = xmlNodeGetContent(node); | |
1402 | ||
1403 | /* type */ | |
1404 | if (!content) { | |
1405 | ret = -LTTNG_ERR_NOMEM; | |
1406 | goto end; | |
1407 | } | |
1408 | ||
1409 | ret = get_event_type(content); | |
1410 | free(content); | |
1411 | if (ret < 0) { | |
1412 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1413 | goto end; | |
1414 | } | |
1415 | ||
1416 | event.type = ret; | |
1417 | } else if (!strcmp((const char *) node->name, | |
1418 | config_element_loglevel_type)) { | |
1419 | xmlChar *content = xmlNodeGetContent(node); | |
1420 | ||
1421 | /* loglevel_type */ | |
1422 | if (!content) { | |
1423 | ret = -LTTNG_ERR_NOMEM; | |
1424 | goto end; | |
1425 | } | |
1426 | ||
1427 | ret = get_loglevel_type(content); | |
1428 | free(content); | |
1429 | if (ret < 0) { | |
1430 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1431 | goto end; | |
1432 | } | |
1433 | ||
1434 | event.loglevel_type = ret; | |
1435 | } else if (!strcmp((const char *) node->name, | |
1436 | config_element_loglevel)) { | |
1437 | xmlChar *content; | |
1438 | int64_t loglevel = 0; | |
1439 | ||
1440 | /* loglevel */ | |
1441 | content = xmlNodeGetContent(node); | |
1442 | if (!content) { | |
1443 | ret = -LTTNG_ERR_NOMEM; | |
1444 | goto end; | |
1445 | } | |
1446 | ||
1447 | ret = parse_int(content, &loglevel); | |
1448 | free(content); | |
1449 | if (ret) { | |
1450 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1451 | goto end; | |
1452 | } | |
1453 | ||
1454 | if (loglevel > INT_MAX || loglevel < INT_MIN) { | |
1455 | WARN("loglevel out of range."); | |
1456 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1457 | goto end; | |
1458 | } | |
1459 | ||
1460 | event.loglevel = loglevel; | |
1461 | } else if (!strcmp((const char *) node->name, | |
1462 | config_element_filter)) { | |
1463 | xmlChar *content = | |
1464 | xmlNodeGetContent(node); | |
1465 | ||
1466 | /* filter */ | |
1467 | if (!content) { | |
1468 | ret = -LTTNG_ERR_NOMEM; | |
1469 | goto end; | |
1470 | } | |
1471 | ||
1472 | filter_expression = strdup((char *) content); | |
1473 | free(content); | |
1474 | if (!filter_expression) { | |
1475 | ret = -LTTNG_ERR_NOMEM; | |
1476 | goto end; | |
1477 | } | |
1478 | } else if (!strcmp((const char *) node->name, | |
1479 | config_element_exclusions)) { | |
1480 | xmlNodePtr exclusion_node; | |
1481 | int exclusion_index = 0; | |
1482 | ||
1483 | /* exclusions */ | |
1484 | if (exclusions) { | |
1485 | /* | |
1486 | * Exclusions has already been initialized, | |
1487 | * invalid file. | |
1488 | */ | |
1489 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1490 | goto end; | |
1491 | } | |
1492 | ||
1493 | exclusion_count = xmlChildElementCount(node); | |
1494 | if (!exclusion_count) { | |
1495 | continue; | |
1496 | } | |
1497 | ||
1498 | exclusions = zmalloc(exclusion_count * sizeof(char *)); | |
1499 | if (!exclusions) { | |
1500 | exclusion_count = 0; | |
1501 | ret = -LTTNG_ERR_NOMEM; | |
1502 | goto end; | |
1503 | } | |
1504 | ||
1505 | for (exclusion_node = xmlFirstElementChild(node); exclusion_node; | |
1506 | exclusion_node = xmlNextElementSibling(exclusion_node)) { | |
1507 | xmlChar *content = | |
1508 | xmlNodeGetContent(exclusion_node); | |
1509 | ||
1510 | if (!content) { | |
1511 | ret = -LTTNG_ERR_NOMEM; | |
1512 | goto end; | |
1513 | } | |
1514 | ||
1515 | exclusions[exclusion_index] = strdup((const char *) content); | |
1516 | free(content); | |
1517 | if (!exclusions[exclusion_index]) { | |
1518 | ret = -LTTNG_ERR_NOMEM; | |
1519 | goto end; | |
1520 | } | |
1521 | exclusion_index++; | |
1522 | } | |
1523 | ||
1524 | event.exclusion = 1; | |
1525 | } else if (!strcmp((const char *) node->name, | |
1526 | config_element_attributes)) { | |
1527 | xmlNodePtr attribute_node = xmlFirstElementChild(node); | |
1528 | ||
1529 | /* attributes */ | |
1530 | if (!attribute_node) { | |
1531 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1532 | goto end; | |
1533 | } | |
1534 | ||
1535 | if (!strcmp((const char *) node->name, | |
1536 | config_element_probe_attributes)) { | |
1537 | xmlNodePtr probe_attribute_node; | |
1538 | ||
1539 | /* probe_attributes */ | |
1540 | for (probe_attribute_node = | |
1541 | xmlFirstElementChild(attribute_node); probe_attribute_node; | |
1542 | probe_attribute_node = xmlNextElementSibling( | |
1543 | probe_attribute_node)) { | |
1544 | ||
1545 | ret = process_probe_attribute_node(probe_attribute_node, | |
1546 | &event.attr.probe); | |
1547 | if (ret) { | |
1548 | goto end; | |
1549 | } | |
1550 | } | |
1551 | } else { | |
1552 | size_t sym_len; | |
1553 | xmlChar *content; | |
1554 | xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node); | |
1555 | ||
1556 | /* function_attributes */ | |
1557 | content = xmlNodeGetContent(symbol_node); | |
1558 | if (!content) { | |
1559 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1560 | goto end; | |
1561 | } | |
1562 | ||
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; | |
1567 | free(content); | |
1568 | goto end; | |
1569 | } | |
1570 | ||
1571 | strncpy(event.attr.ftrace.symbol_name, (char *) content, | |
1572 | sym_len); | |
1573 | free(content); | |
1574 | } | |
1575 | } | |
1576 | } | |
1577 | ||
1578 | ret = lttng_enable_event_with_exclusions(handle, &event, channel_name, | |
1579 | filter_expression, exclusion_count, exclusions); | |
1580 | end: | |
1581 | for (i = 0; i < exclusion_count; i++) { | |
1582 | free(exclusions[i]); | |
1583 | } | |
1584 | ||
1585 | free(exclusions); | |
1586 | free(filter_expression); | |
1587 | return ret; | |
1588 | } | |
1589 | ||
1590 | static | |
1591 | int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle, | |
1592 | const char *channel_name) | |
1593 | { | |
1594 | int ret = 0; | |
1595 | xmlNodePtr node; | |
1596 | ||
1597 | assert(events_node); | |
1598 | assert(handle); | |
1599 | assert(channel_name); | |
1600 | ||
1601 | for (node = xmlFirstElementChild(events_node); node; | |
1602 | node = xmlNextElementSibling(node)) { | |
1603 | ret = process_event_node(node, handle, channel_name); | |
1604 | if (ret) { | |
1605 | goto end; | |
1606 | } | |
1607 | } | |
1608 | end: | |
1609 | return ret; | |
1610 | } | |
1611 | ||
1612 | static | |
1613 | int process_channel_attr_node(xmlNodePtr attr_node, | |
1614 | struct lttng_channel *channel, xmlNodePtr *contexts_node, | |
1615 | xmlNodePtr *events_node) | |
1616 | { | |
1617 | int ret; | |
1618 | ||
1619 | assert(attr_node); | |
1620 | assert(channel); | |
1621 | assert(contexts_node); | |
1622 | assert(events_node); | |
1623 | ||
1624 | if (!strcmp((const char *) attr_node->name, config_element_name)) { | |
1625 | xmlChar *content; | |
1626 | size_t name_len; | |
1627 | ||
1628 | /* name */ | |
1629 | content = xmlNodeGetContent(attr_node); | |
1630 | if (!content) { | |
1631 | ret = -LTTNG_ERR_NOMEM; | |
1632 | goto end; | |
1633 | } | |
1634 | ||
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; | |
1639 | free(content); | |
1640 | goto end; | |
1641 | } | |
1642 | ||
1643 | strncpy(channel->name, (const char *) content, name_len); | |
1644 | free(content); | |
1645 | } else if (!strcmp((const char *) attr_node->name, | |
1646 | config_element_enabled)) { | |
1647 | xmlChar *content; | |
1648 | int enabled; | |
1649 | ||
1650 | /* enabled */ | |
1651 | content = xmlNodeGetContent(attr_node); | |
1652 | if (!content) { | |
1653 | ret = -LTTNG_ERR_NOMEM; | |
1654 | goto end; | |
1655 | } | |
1656 | ||
1657 | ret = parse_bool(content, &enabled); | |
1658 | free(content); | |
1659 | if (ret) { | |
1660 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1661 | goto end; | |
1662 | } | |
1663 | ||
1664 | channel->enabled = enabled; | |
1665 | } else if (!strcmp((const char *) attr_node->name, | |
1666 | config_element_overwrite_mode)) { | |
1667 | xmlChar *content; | |
1668 | ||
1669 | /* overwrite_mode */ | |
1670 | content = xmlNodeGetContent(attr_node); | |
1671 | if (!content) { | |
1672 | ret = -LTTNG_ERR_NOMEM; | |
1673 | goto end; | |
1674 | } | |
1675 | ||
1676 | ret = get_overwrite_mode(content); | |
1677 | free(content); | |
1678 | if (ret < 0) { | |
1679 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1680 | goto end; | |
1681 | } | |
1682 | ||
1683 | channel->attr.overwrite = ret; | |
1684 | } else if (!strcmp((const char *) attr_node->name, | |
1685 | config_element_subbuf_size)) { | |
1686 | xmlChar *content; | |
1687 | ||
1688 | /* subbuffer_size */ | |
1689 | content = xmlNodeGetContent(attr_node); | |
1690 | if (!content) { | |
1691 | ret = -LTTNG_ERR_NOMEM; | |
1692 | goto end; | |
1693 | } | |
1694 | ||
1695 | ret = parse_uint(content, &channel->attr.subbuf_size); | |
1696 | free(content); | |
1697 | if (ret) { | |
1698 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1699 | goto end; | |
1700 | } | |
1701 | } else if (!strcmp((const char *) attr_node->name, | |
1702 | config_element_num_subbuf)) { | |
1703 | xmlChar *content; | |
1704 | ||
1705 | /* subbuffer_count */ | |
1706 | content = xmlNodeGetContent(attr_node); | |
1707 | if (!content) { | |
1708 | ret = -LTTNG_ERR_NOMEM; | |
1709 | goto end; | |
1710 | } | |
1711 | ||
1712 | ret = parse_uint(content, &channel->attr.num_subbuf); | |
1713 | free(content); | |
1714 | if (ret) { | |
1715 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1716 | goto end; | |
1717 | } | |
1718 | } else if (!strcmp((const char *) attr_node->name, | |
1719 | config_element_switch_timer_interval)) { | |
1720 | xmlChar *content; | |
1721 | uint64_t switch_timer_interval = 0; | |
1722 | ||
1723 | /* switch_timer_interval */ | |
1724 | content = xmlNodeGetContent(attr_node); | |
1725 | if (!content) { | |
1726 | ret = -LTTNG_ERR_NOMEM; | |
1727 | goto end; | |
1728 | } | |
1729 | ||
1730 | ret = parse_uint(content, &switch_timer_interval); | |
1731 | free(content); | |
1732 | if (ret) { | |
1733 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1734 | goto end; | |
1735 | } | |
1736 | ||
1737 | if (switch_timer_interval > UINT_MAX) { | |
1738 | WARN("switch_timer_interval out of range."); | |
1739 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1740 | goto end; | |
1741 | } | |
1742 | ||
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)) { | |
1747 | xmlChar *content; | |
1748 | uint64_t read_timer_interval = 0; | |
1749 | ||
1750 | /* read_timer_interval */ | |
1751 | content = xmlNodeGetContent(attr_node); | |
1752 | if (!content) { | |
1753 | ret = -LTTNG_ERR_NOMEM; | |
1754 | goto end; | |
1755 | } | |
1756 | ||
1757 | ret = parse_uint(content, &read_timer_interval); | |
1758 | free(content); | |
1759 | if (ret) { | |
1760 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1761 | goto end; | |
1762 | } | |
1763 | ||
1764 | if (read_timer_interval > UINT_MAX) { | |
1765 | WARN("read_timer_interval out of range."); | |
1766 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1767 | goto end; | |
1768 | } | |
1769 | ||
1770 | channel->attr.read_timer_interval = | |
1771 | read_timer_interval; | |
1772 | } else if (!strcmp((const char *) attr_node->name, | |
1773 | config_element_output_type)) { | |
1774 | xmlChar *content; | |
1775 | ||
1776 | /* output_type */ | |
1777 | content = xmlNodeGetContent(attr_node); | |
1778 | if (!content) { | |
1779 | ret = -LTTNG_ERR_NOMEM; | |
1780 | goto end; | |
1781 | } | |
1782 | ||
1783 | ret = get_output_type(content); | |
1784 | free(content); | |
1785 | if (ret < 0) { | |
1786 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1787 | goto end; | |
1788 | } | |
1789 | ||
1790 | channel->attr.output = ret; | |
1791 | } else if (!strcmp((const char *) attr_node->name, | |
1792 | config_element_tracefile_size)) { | |
1793 | xmlChar *content; | |
1794 | ||
1795 | /* tracefile_size */ | |
1796 | content = xmlNodeGetContent(attr_node); | |
1797 | if (!content) { | |
1798 | ret = -LTTNG_ERR_NOMEM; | |
1799 | goto end; | |
1800 | } | |
1801 | ||
1802 | ret = parse_uint(content, &channel->attr.tracefile_size); | |
1803 | free(content); | |
1804 | if (ret) { | |
1805 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1806 | goto end; | |
1807 | } | |
1808 | } else if (!strcmp((const char *) attr_node->name, | |
1809 | config_element_tracefile_count)) { | |
1810 | xmlChar *content; | |
1811 | ||
1812 | /* tracefile_count */ | |
1813 | content = xmlNodeGetContent(attr_node); | |
1814 | if (!content) { | |
1815 | ret = -LTTNG_ERR_NOMEM; | |
1816 | goto end; | |
1817 | } | |
1818 | ||
1819 | ret = parse_uint(content, &channel->attr.tracefile_count); | |
1820 | free(content); | |
1821 | if (ret) { | |
1822 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1823 | goto end; | |
1824 | } | |
1825 | } else if (!strcmp((const char *) attr_node->name, | |
1826 | config_element_live_timer_interval)) { | |
1827 | xmlChar *content; | |
1828 | uint64_t live_timer_interval = 0; | |
1829 | ||
1830 | /* live_timer_interval */ | |
1831 | content = xmlNodeGetContent(attr_node); | |
1832 | if (!content) { | |
1833 | ret = -LTTNG_ERR_NOMEM; | |
1834 | goto end; | |
1835 | } | |
1836 | ||
1837 | ret = parse_uint(content, &live_timer_interval); | |
1838 | free(content); | |
1839 | if (ret) { | |
1840 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1841 | goto end; | |
1842 | } | |
1843 | ||
1844 | if (live_timer_interval > UINT_MAX) { | |
1845 | WARN("live_timer_interval out of range."); | |
1846 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1847 | goto end; | |
1848 | } | |
1849 | ||
1850 | channel->attr.live_timer_interval = | |
1851 | live_timer_interval; | |
1852 | } else if (!strcmp((const char *) attr_node->name, | |
1853 | config_element_events)) { | |
1854 | /* events */ | |
1855 | *events_node = attr_node; | |
1856 | } else { | |
1857 | /* contexts */ | |
1858 | *contexts_node = attr_node; | |
1859 | } | |
1860 | ret = 0; | |
1861 | end: | |
1862 | return ret; | |
1863 | } | |
1864 | ||
1865 | static | |
1866 | int process_context_node(xmlNodePtr context_node, | |
1867 | struct lttng_handle *handle, const char *channel_name) | |
1868 | { | |
1869 | int ret; | |
1870 | struct lttng_event_context context; | |
1871 | xmlNodePtr context_child_node = xmlFirstElementChild(context_node); | |
1872 | ||
1873 | assert(handle); | |
1874 | assert(channel_name); | |
1875 | ||
1876 | if (!context_child_node) { | |
1877 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1878 | goto end; | |
1879 | } | |
1880 | ||
1881 | memset(&context, 0, sizeof(context)); | |
1882 | ||
1883 | if (!strcmp((const char *) context_child_node->name, | |
1884 | config_element_type)) { | |
1885 | /* type */ | |
1886 | xmlChar *content = xmlNodeGetContent(context_child_node); | |
1887 | if (!content) { | |
1888 | ret = -LTTNG_ERR_NOMEM; | |
1889 | goto end; | |
1890 | } | |
1891 | ||
1892 | ret = get_context_type(content); | |
1893 | free(content); | |
1894 | if (ret < 0) { | |
1895 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1896 | goto end; | |
1897 | } | |
1898 | ||
1899 | context.ctx = ret; | |
1900 | } else { | |
1901 | xmlNodePtr perf_attr_node; | |
1902 | ||
1903 | /* perf */ | |
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)) { | |
1910 | xmlChar *content; | |
1911 | uint64_t type = 0; | |
1912 | ||
1913 | /* type */ | |
1914 | content = xmlNodeGetContent(perf_attr_node); | |
1915 | if (!content) { | |
1916 | ret = -LTTNG_ERR_NOMEM; | |
1917 | goto end; | |
1918 | } | |
1919 | ||
1920 | ret = parse_uint(content, &type); | |
1921 | free(content); | |
1922 | if (ret) { | |
1923 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1924 | goto end; | |
1925 | } | |
1926 | ||
1927 | if (type > UINT32_MAX) { | |
1928 | WARN("perf context type out of range."); | |
1929 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1930 | goto end; | |
1931 | } | |
1932 | ||
1933 | context.u.perf_counter.type = type; | |
1934 | } else if (!strcmp((const char *) perf_attr_node->name, | |
1935 | config_element_config)) { | |
1936 | xmlChar *content; | |
1937 | uint64_t config = 0; | |
1938 | ||
1939 | /* config */ | |
1940 | content = xmlNodeGetContent(perf_attr_node); | |
1941 | if (!content) { | |
1942 | ret = -LTTNG_ERR_NOMEM; | |
1943 | goto end; | |
1944 | } | |
1945 | ||
1946 | ret = parse_uint(content, &config); | |
1947 | free(content); | |
1948 | if (ret) { | |
1949 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1950 | goto end; | |
1951 | } | |
1952 | ||
1953 | context.u.perf_counter.config = config; | |
1954 | } else if (!strcmp((const char *) perf_attr_node->name, | |
1955 | config_element_name)) { | |
1956 | xmlChar *content; | |
1957 | size_t name_len; | |
1958 | ||
1959 | /* name */ | |
1960 | content = xmlNodeGetContent(perf_attr_node); | |
1961 | if (!content) { | |
1962 | ret = -LTTNG_ERR_NOMEM; | |
1963 | goto end; | |
1964 | } | |
1965 | ||
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; | |
1970 | free(content); | |
1971 | goto end; | |
1972 | } | |
1973 | ||
1974 | strncpy(context.u.perf_counter.name, (const char *) content, | |
1975 | name_len); | |
1976 | free(content); | |
1977 | } | |
1978 | } | |
1979 | } | |
1980 | ||
1981 | ret = lttng_add_context(handle, &context, NULL, channel_name); | |
1982 | end: | |
1983 | return ret; | |
1984 | } | |
1985 | ||
1986 | static | |
1987 | int process_contexts_node(xmlNodePtr contexts_node, | |
1988 | struct lttng_handle *handle, const char *channel_name) | |
1989 | { | |
1990 | int ret = 0; | |
1991 | xmlNodePtr context_node; | |
1992 | ||
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); | |
1996 | if (ret) { | |
1997 | goto end; | |
1998 | } | |
1999 | } | |
2000 | end: | |
2001 | return ret; | |
2002 | } | |
2003 | ||
2004 | static | |
2005 | int process_domain_node(xmlNodePtr domain_node, const char *session_name) | |
2006 | { | |
2007 | int ret; | |
2008 | struct lttng_domain domain = { 0 }; | |
2009 | struct lttng_handle *handle = NULL; | |
2010 | xmlNodePtr channels_node = NULL; | |
2011 | xmlNodePtr node; | |
2012 | ||
2013 | assert(session_name); | |
2014 | ||
2015 | ret = init_domain(domain_node, &domain); | |
2016 | if (ret) { | |
2017 | goto end; | |
2018 | } | |
2019 | ||
2020 | handle = lttng_create_handle(session_name, &domain); | |
2021 | if (!handle) { | |
2022 | ret = -LTTNG_ERR_NOMEM; | |
2023 | goto end; | |
2024 | } | |
2025 | ||
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; | |
2032 | break; | |
2033 | } | |
2034 | } | |
2035 | ||
2036 | if (!channels_node) { | |
2037 | goto end; | |
2038 | } | |
2039 | ||
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; | |
2047 | ||
2048 | memset(&channel, 0, sizeof(channel)); | |
2049 | lttng_channel_set_default_attr(&domain, &channel.attr); | |
2050 | ||
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); | |
2056 | if (ret) { | |
2057 | goto end; | |
2058 | } | |
2059 | } | |
2060 | ||
2061 | ret = lttng_enable_channel(handle, &channel); | |
2062 | if (ret < 0) { | |
2063 | goto end; | |
2064 | } | |
2065 | ||
2066 | ret = process_events_node(events_node, handle, channel.name); | |
2067 | if (ret) { | |
2068 | goto end; | |
2069 | } | |
2070 | ||
2071 | ret = process_contexts_node(contexts_node, handle, | |
2072 | channel.name); | |
2073 | if (ret) { | |
2074 | goto end; | |
2075 | } | |
2076 | } | |
2077 | end: | |
2078 | lttng_destroy_handle(handle); | |
2079 | return ret; | |
2080 | } | |
2081 | ||
2082 | static | |
2083 | int process_session_node(xmlNodePtr session_node, const char *session_name, | |
2084 | int override) | |
2085 | { | |
2086 | int ret, started = -1, snapshot_mode = -1; | |
2087 | uint64_t live_timer_interval = UINT64_MAX; | |
2088 | char *name = NULL; | |
2089 | xmlNodePtr domains_node = NULL; | |
2090 | xmlNodePtr output_node = NULL; | |
2091 | xmlNodePtr node; | |
2092 | struct lttng_domain *kernel_domain = NULL; | |
2093 | struct lttng_domain *ust_domain = NULL; | |
2094 | struct lttng_domain *jul_domain = NULL; | |
2095 | ||
2096 | for (node = xmlFirstElementChild(session_node); node; | |
2097 | node = xmlNextElementSibling(node)) { | |
2098 | if (!name && !strcmp((const char *) node->name, | |
2099 | config_element_name)) { | |
2100 | /* name */ | |
2101 | xmlChar *node_content = xmlNodeGetContent(node); | |
2102 | if (!node_content) { | |
2103 | ret = -LTTNG_ERR_NOMEM; | |
2104 | goto end; | |
2105 | } | |
2106 | ||
2107 | name = (char *) node_content; | |
2108 | } else if (!domains_node && !strcmp((const char *) node->name, | |
2109 | config_element_domains)) { | |
2110 | /* domains */ | |
2111 | domains_node = node; | |
2112 | } else if (started == -1 && !strcmp((const char *) node->name, | |
2113 | config_element_started)) { | |
2114 | /* started */ | |
2115 | xmlChar *node_content = xmlNodeGetContent(node); | |
2116 | if (!node_content) { | |
2117 | ret = -LTTNG_ERR_NOMEM; | |
2118 | goto end; | |
2119 | } | |
2120 | ||
2121 | ret = parse_bool(node_content, &started); | |
2122 | free(node_content); | |
2123 | if (ret) { | |
2124 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2125 | goto end; | |
2126 | } | |
2127 | } else if (!output_node && !strcmp((const char *) node->name, | |
2128 | config_element_output)) { | |
2129 | /* output */ | |
2130 | output_node = node; | |
2131 | } else { | |
2132 | /* attributes, snapshot_mode or live_timer_interval */ | |
2133 | xmlNodePtr attributes_child = | |
2134 | xmlFirstElementChild(node); | |
2135 | ||
2136 | if (!strcmp((const char *) attributes_child->name, | |
2137 | config_element_snapshot_mode)) { | |
2138 | /* snapshot_mode */ | |
2139 | xmlChar *snapshot_mode_content = | |
2140 | xmlNodeGetContent(attributes_child); | |
2141 | if (!snapshot_mode_content) { | |
2142 | ret = -LTTNG_ERR_NOMEM; | |
2143 | goto end; | |
2144 | } | |
2145 | ||
2146 | ret = parse_bool(snapshot_mode_content, &snapshot_mode); | |
2147 | free(snapshot_mode_content); | |
2148 | if (ret) { | |
2149 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2150 | goto end; | |
2151 | } | |
2152 | } else { | |
2153 | /* live_timer_interval */ | |
2154 | xmlChar *timer_interval_content = | |
2155 | xmlNodeGetContent(attributes_child); | |
2156 | if (!timer_interval_content) { | |
2157 | ret = -LTTNG_ERR_NOMEM; | |
2158 | goto end; | |
2159 | } | |
2160 | ||
2161 | ret = parse_uint(timer_interval_content, &live_timer_interval); | |
2162 | free(timer_interval_content); | |
2163 | if (ret) { | |
2164 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2165 | goto end; | |
2166 | } | |
2167 | } | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | if (!name) { | |
2172 | /* Mandatory attribute, as defined in the session XSD */ | |
2173 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2174 | goto end; | |
2175 | } | |
2176 | ||
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; | |
2180 | goto end; | |
2181 | } | |
2182 | ||
2183 | /* Init domains to create the session handles */ | |
2184 | for (node = xmlFirstElementChild(domains_node); node; | |
2185 | node = xmlNextElementSibling(node)) { | |
2186 | struct lttng_domain *domain; | |
2187 | ||
2188 | domain = zmalloc(sizeof(*domain)); | |
2189 | if (!domain) { | |
2190 | ret = -LTTNG_ERR_NOMEM; | |
2191 | goto end; | |
2192 | } | |
2193 | ||
2194 | ret = init_domain(node, domain); | |
2195 | if (ret) { | |
2196 | goto domain_init_error; | |
2197 | } | |
2198 | ||
2199 | switch (domain->type) { | |
2200 | case LTTNG_DOMAIN_KERNEL: | |
c33e6729 DG |
2201 | if (kernel_domain) { |
2202 | /* Same domain seen twice, invalid! */ | |
2203 | goto domain_init_error; | |
2204 | } | |
dcf266c0 JG |
2205 | kernel_domain = domain; |
2206 | break; | |
2207 | case LTTNG_DOMAIN_UST: | |
c33e6729 DG |
2208 | if (ust_domain) { |
2209 | /* Same domain seen twice, invalid! */ | |
2210 | goto domain_init_error; | |
2211 | } | |
dcf266c0 JG |
2212 | ust_domain = domain; |
2213 | break; | |
2214 | case LTTNG_DOMAIN_JUL: | |
c33e6729 DG |
2215 | if (jul_domain) { |
2216 | /* Same domain seen twice, invalid! */ | |
2217 | goto domain_init_error; | |
2218 | } | |
dcf266c0 JG |
2219 | jul_domain = domain; |
2220 | break; | |
2221 | default: | |
2222 | WARN("Invalid domain type"); | |
2223 | goto domain_init_error; | |
2224 | } | |
2225 | continue; | |
2226 | domain_init_error: | |
2227 | free(domain); | |
2228 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2229 | goto end; | |
2230 | } | |
2231 | ||
2232 | if (override) { | |
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."); | |
2237 | goto end; | |
2238 | } | |
2239 | } | |
2240 | ||
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); | |
2248 | } else { | |
2249 | /* regular session */ | |
2250 | ret = create_session(name, kernel_domain, ust_domain, jul_domain, | |
2251 | output_node, UINT64_MAX); | |
2252 | } | |
2253 | ||
2254 | if (ret) { | |
2255 | goto end; | |
2256 | } | |
2257 | ||
2258 | for (node = xmlFirstElementChild(domains_node); node; | |
2259 | node = xmlNextElementSibling(node)) { | |
2260 | ret = process_domain_node(node, name); | |
2261 | if (ret) { | |
2262 | goto end; | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | if (started) { | |
2267 | ret = lttng_start_tracing(name); | |
2268 | if (ret) { | |
2269 | goto end; | |
2270 | } | |
2271 | } | |
2272 | end: | |
2273 | free(kernel_domain); | |
2274 | free(ust_domain); | |
2275 | free(jul_domain); | |
2276 | free(name); | |
2277 | return ret; | |
2278 | } | |
2279 | ||
2280 | static | |
2281 | int load_session_from_file(const char *path, const char *session_name, | |
2282 | struct session_config_validation_ctx *validation_ctx, int override) | |
2283 | { | |
2284 | int ret, session_found = !session_name; | |
2285 | xmlDocPtr doc = NULL; | |
2286 | xmlNodePtr sessions_node; | |
2287 | xmlNodePtr session_node; | |
2288 | struct stat sb; | |
2289 | ||
2290 | assert(path); | |
2291 | assert(validation_ctx); | |
2292 | ||
2293 | ret = stat(path, &sb); | |
2294 | if (ret) { | |
2295 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; | |
2296 | goto end; | |
2297 | } | |
2298 | ||
2299 | doc = xmlParseFile(path); | |
2300 | if (!doc) { | |
2301 | ret = -LTTNG_ERR_LOAD_IO_FAIL; | |
2302 | goto end; | |
2303 | } | |
2304 | ||
2305 | ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc); | |
2306 | if (ret) { | |
2307 | ERR("Session configuration file validation failed"); | |
2308 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2309 | goto end; | |
2310 | } | |
2311 | ||
2312 | sessions_node = xmlDocGetRootElement(doc); | |
2313 | if (!sessions_node) { | |
2314 | goto end; | |
2315 | } | |
2316 | ||
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 */ | |
2324 | session_found = 1; | |
2325 | break; | |
2326 | } | |
2327 | } | |
2328 | end: | |
2329 | xmlFreeDoc(doc); | |
2330 | if (!ret) { | |
2331 | ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOT_FOUND; | |
2332 | } | |
2333 | return ret; | |
2334 | } | |
2335 | ||
2336 | static | |
2337 | int load_session_from_path(const char *path, const char *session_name, | |
2338 | struct session_config_validation_ctx *validation_ctx, int override) | |
2339 | { | |
2340 | int ret, session_found = !session_name; | |
dcf266c0 JG |
2341 | DIR *directory = NULL; |
2342 | ||
2343 | assert(path); | |
2344 | assert(validation_ctx); | |
2345 | ||
4af16958 DG |
2346 | directory = opendir(path); |
2347 | if (!directory) { | |
2348 | if (errno != ENOTDIR) { | |
2349 | ret = -LTTNG_ERR_LOAD_IO_FAIL; | |
2350 | goto end; | |
2351 | } | |
dcf266c0 | 2352 | } |
4af16958 | 2353 | if (directory) { |
dcf266c0 JG |
2354 | struct dirent *entry; |
2355 | struct dirent *result; | |
2356 | char *file_path = NULL; | |
2357 | size_t path_len = strlen(path); | |
2358 | ||
2359 | if (path_len >= PATH_MAX) { | |
2360 | ret = -LTTNG_ERR_INVALID; | |
2361 | goto end; | |
2362 | } | |
2363 | ||
2364 | entry = zmalloc(sizeof(*entry)); | |
2365 | if (!entry) { | |
2366 | ret = -LTTNG_ERR_NOMEM; | |
2367 | goto end; | |
2368 | } | |
2369 | ||
dcf266c0 JG |
2370 | file_path = zmalloc(PATH_MAX); |
2371 | if (!file_path) { | |
2372 | ret = -LTTNG_ERR_NOMEM; | |
2373 | free(entry); | |
2374 | goto end; | |
2375 | } | |
2376 | ||
2377 | strncpy(file_path, path, path_len); | |
2378 | if (file_path[path_len - 1] != '/') { | |
2379 | file_path[path_len++] = '/'; | |
2380 | } | |
2381 | ||
4af16958 | 2382 | ret = 0; |
dcf266c0 JG |
2383 | /* Search for *.lttng files */ |
2384 | while (!readdir_r(directory, entry, &result) && result) { | |
2385 | size_t file_name_len = strlen(result->d_name); | |
2386 | ||
2387 | if (file_name_len <= | |
2388 | sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) { | |
2389 | continue; | |
2390 | } | |
2391 | ||
2392 | if (path_len + file_name_len > PATH_MAX) { | |
2393 | continue; | |
2394 | } | |
2395 | ||
2396 | if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION, | |
2397 | result->d_name + file_name_len - sizeof( | |
2398 | DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) { | |
2399 | continue; | |
2400 | } | |
2401 | ||
2402 | strncpy(file_path + path_len, result->d_name, file_name_len); | |
2403 | file_path[path_len + file_name_len] = '\0'; | |
2404 | ||
2405 | ret = load_session_from_file(file_path, session_name, | |
2406 | validation_ctx, override); | |
2407 | if (session_name && !ret) { | |
2408 | session_found = 1; | |
2409 | break; | |
2410 | } | |
2411 | } | |
2412 | ||
2413 | free(entry); | |
2414 | free(file_path); | |
2415 | } else { | |
2416 | ret = load_session_from_file(path, session_name, | |
2417 | validation_ctx, override); | |
2418 | if (ret) { | |
2419 | goto end; | |
2420 | } else { | |
2421 | session_found = 1; | |
2422 | } | |
2423 | } | |
2424 | ||
2425 | end: | |
2426 | if (directory) { | |
2427 | if (closedir(directory)) { | |
2428 | PERROR("closedir"); | |
2429 | } | |
2430 | } | |
2431 | ||
2432 | if (!session_found) { | |
2433 | ret = -LTTNG_ERR_LOAD_SESSION_NOT_FOUND; | |
2434 | } | |
2435 | ||
2436 | return ret; | |
2437 | } | |
2438 | ||
2439 | LTTNG_HIDDEN | |
2440 | int config_load_session(const char *path, const char *session_name, | |
2441 | int override) | |
2442 | { | |
2443 | int ret; | |
2444 | struct session_config_validation_ctx validation_ctx = { 0 }; | |
2445 | ||
2446 | ret = init_session_config_validation_ctx(&validation_ctx); | |
2447 | if (ret) { | |
2448 | goto end; | |
2449 | } | |
2450 | ||
2451 | if (!path) { | |
2452 | /* Try home path */ | |
2453 | char *home_path = utils_get_home_dir(); | |
2454 | if (home_path) { | |
2455 | char *path; | |
2456 | ||
2457 | ret = asprintf(&path, DEFAULT_SESSION_HOME_CONFIGPATH, | |
2458 | home_path); | |
2459 | if (ret < 0) { | |
2460 | goto end; | |
2461 | } | |
2462 | ||
2463 | ret = load_session_from_path(path, NULL, | |
2464 | &validation_ctx, 0); | |
2465 | if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) { | |
2466 | free(path); | |
2467 | goto end; | |
2468 | } | |
2469 | ||
2470 | free(path); | |
2471 | } | |
2472 | ||
2473 | /* Try system session configuration path */ | |
2474 | ret = load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH, NULL, | |
2475 | &validation_ctx, 0); | |
2476 | if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) { | |
2477 | goto end; | |
2478 | } | |
2479 | } else { | |
2480 | ret = access(path, F_OK); | |
2481 | if (ret < 0) { | |
2482 | PERROR("access"); | |
2483 | switch (errno) { | |
2484 | case ENOENT: | |
2485 | ret = -LTTNG_ERR_INVALID; | |
2486 | WARN("Session configuration path does not exist."); | |
2487 | break; | |
2488 | case EACCES: | |
2489 | ret = -LTTNG_ERR_EPERM; | |
2490 | break; | |
2491 | default: | |
2492 | ret = -LTTNG_ERR_UNK; | |
2493 | break; | |
2494 | } | |
2495 | goto end; | |
2496 | } | |
2497 | ||
2498 | ret = load_session_from_path(path, session_name, | |
2499 | &validation_ctx, override); | |
2500 | } | |
2501 | end: | |
2502 | fini_session_config_validation_ctx(&validation_ctx); | |
2503 | return ret; | |
2504 | } |