2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <urcu/uatomic.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/config/session-config.h>
17 #include <common/utils.h>
18 #include <common/runas.h>
19 #include <lttng/save-internal.h>
24 #include "lttng-syscall.h"
25 #include "trace-ust.h"
28 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
30 int save_kernel_channel_attributes(struct config_writer
*writer
,
31 struct lttng_channel_attr
*attr
)
35 ret
= config_writer_write_element_string(writer
,
36 config_element_overwrite_mode
,
37 attr
->overwrite
? config_overwrite_mode_overwrite
:
38 config_overwrite_mode_discard
);
40 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
44 ret
= config_writer_write_element_unsigned_int(writer
,
45 config_element_subbuf_size
, attr
->subbuf_size
);
47 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
51 ret
= config_writer_write_element_unsigned_int(writer
,
52 config_element_num_subbuf
,
55 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
59 ret
= config_writer_write_element_unsigned_int(writer
,
60 config_element_switch_timer_interval
,
61 attr
->switch_timer_interval
);
63 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
67 ret
= config_writer_write_element_unsigned_int(writer
,
68 config_element_read_timer_interval
,
69 attr
->read_timer_interval
);
71 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
75 ret
= config_writer_write_element_string(writer
,
76 config_element_output_type
,
77 attr
->output
== LTTNG_EVENT_SPLICE
?
78 config_output_type_splice
: config_output_type_mmap
);
80 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
84 ret
= config_writer_write_element_unsigned_int(writer
,
85 config_element_tracefile_size
, attr
->tracefile_size
);
87 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
91 ret
= config_writer_write_element_unsigned_int(writer
,
92 config_element_tracefile_count
,
93 attr
->tracefile_count
);
95 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
99 ret
= config_writer_write_element_unsigned_int(writer
,
100 config_element_live_timer_interval
,
101 attr
->live_timer_interval
);
103 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
107 if (attr
->extended
.ptr
) {
108 struct lttng_channel_extended
*ext
= NULL
;
110 ext
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
111 ret
= config_writer_write_element_unsigned_int(writer
,
112 config_element_monitor_timer_interval
,
113 ext
->monitor_timer_interval
);
115 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
119 ret
= config_writer_write_element_signed_int(writer
,
120 config_element_blocking_timeout
,
121 ext
->blocking_timeout
);
123 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
133 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
135 int save_ust_channel_attributes(struct config_writer
*writer
,
136 struct lttng_ust_abi_channel_attr
*attr
)
139 struct ltt_ust_channel
*channel
= NULL
;
141 ret
= config_writer_write_element_string(writer
,
142 config_element_overwrite_mode
,
143 attr
->overwrite
? config_overwrite_mode_overwrite
:
144 config_overwrite_mode_discard
);
146 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
150 ret
= config_writer_write_element_unsigned_int(writer
,
151 config_element_subbuf_size
, attr
->subbuf_size
);
153 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
157 ret
= config_writer_write_element_unsigned_int(writer
,
158 config_element_num_subbuf
,
161 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
165 ret
= config_writer_write_element_unsigned_int(writer
,
166 config_element_switch_timer_interval
,
167 attr
->switch_timer_interval
);
169 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
173 ret
= config_writer_write_element_unsigned_int(writer
,
174 config_element_read_timer_interval
,
175 attr
->read_timer_interval
);
177 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
181 ret
= config_writer_write_element_string(writer
,
182 config_element_output_type
,
183 attr
->output
== LTTNG_UST_ABI_MMAP
?
184 config_output_type_mmap
: config_output_type_splice
);
186 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
190 ret
= config_writer_write_element_signed_int(writer
,
191 config_element_blocking_timeout
,
192 attr
->u
.s
.blocking_timeout
);
194 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
199 * Fetch the monitor timer which is located in the parent of
200 * lttng_ust_channel_attr
202 channel
= caa_container_of(attr
, struct ltt_ust_channel
, attr
);
203 ret
= config_writer_write_element_unsigned_int(writer
,
204 config_element_monitor_timer_interval
,
205 channel
->monitor_timer_interval
);
207 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
217 const char *get_kernel_instrumentation_string(
218 enum lttng_kernel_abi_instrumentation instrumentation
)
220 const char *instrumentation_string
;
222 switch (instrumentation
) {
223 case LTTNG_KERNEL_ABI_ALL
:
224 instrumentation_string
= config_event_type_all
;
226 case LTTNG_KERNEL_ABI_TRACEPOINT
:
227 instrumentation_string
= config_event_type_tracepoint
;
229 case LTTNG_KERNEL_ABI_KPROBE
:
230 instrumentation_string
= config_event_type_probe
;
232 case LTTNG_KERNEL_ABI_UPROBE
:
233 instrumentation_string
= config_event_type_userspace_probe
;
235 case LTTNG_KERNEL_ABI_FUNCTION
:
236 instrumentation_string
= config_event_type_function_entry
;
238 case LTTNG_KERNEL_ABI_KRETPROBE
:
239 instrumentation_string
= config_event_type_function
;
241 case LTTNG_KERNEL_ABI_NOOP
:
242 instrumentation_string
= config_event_type_noop
;
244 case LTTNG_KERNEL_ABI_SYSCALL
:
245 instrumentation_string
= config_event_type_syscall
;
248 instrumentation_string
= NULL
;
251 return instrumentation_string
;
255 const char *get_kernel_context_type_string(
256 enum lttng_kernel_abi_context_type context_type
)
258 const char *context_type_string
;
260 switch (context_type
) {
261 case LTTNG_KERNEL_ABI_CONTEXT_PID
:
262 context_type_string
= config_event_context_pid
;
264 case LTTNG_KERNEL_ABI_CONTEXT_PROCNAME
:
265 context_type_string
= config_event_context_procname
;
267 case LTTNG_KERNEL_ABI_CONTEXT_PRIO
:
268 context_type_string
= config_event_context_prio
;
270 case LTTNG_KERNEL_ABI_CONTEXT_NICE
:
271 context_type_string
= config_event_context_nice
;
273 case LTTNG_KERNEL_ABI_CONTEXT_VPID
:
274 context_type_string
= config_event_context_vpid
;
276 case LTTNG_KERNEL_ABI_CONTEXT_TID
:
277 context_type_string
= config_event_context_tid
;
279 case LTTNG_KERNEL_ABI_CONTEXT_VTID
:
280 context_type_string
= config_event_context_vtid
;
282 case LTTNG_KERNEL_ABI_CONTEXT_PPID
:
283 context_type_string
= config_event_context_ppid
;
285 case LTTNG_KERNEL_ABI_CONTEXT_VPPID
:
286 context_type_string
= config_event_context_vppid
;
288 case LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME
:
289 context_type_string
= config_event_context_hostname
;
291 case LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE
:
292 context_type_string
= config_event_context_interruptible
;
294 case LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE
:
295 context_type_string
= config_event_context_preemptible
;
297 case LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE
:
298 context_type_string
= config_event_context_need_reschedule
;
300 case LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE
:
301 context_type_string
= config_event_context_migratable
;
303 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER
:
304 context_type_string
= config_event_context_callstack_user
;
306 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL
:
307 context_type_string
= config_event_context_callstack_kernel
;
309 case LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS
:
310 context_type_string
= config_event_context_cgroup_ns
;
312 case LTTNG_KERNEL_ABI_CONTEXT_IPC_NS
:
313 context_type_string
= config_event_context_ipc_ns
;
315 case LTTNG_KERNEL_ABI_CONTEXT_MNT_NS
:
316 context_type_string
= config_event_context_mnt_ns
;
318 case LTTNG_KERNEL_ABI_CONTEXT_NET_NS
:
319 context_type_string
= config_event_context_net_ns
;
321 case LTTNG_KERNEL_ABI_CONTEXT_PID_NS
:
322 context_type_string
= config_event_context_pid_ns
;
324 case LTTNG_KERNEL_ABI_CONTEXT_TIME_NS
:
325 context_type_string
= config_event_context_time_ns
;
327 case LTTNG_KERNEL_ABI_CONTEXT_USER_NS
:
328 context_type_string
= config_event_context_user_ns
;
330 case LTTNG_KERNEL_ABI_CONTEXT_UTS_NS
:
331 context_type_string
= config_event_context_uts_ns
;
333 case LTTNG_KERNEL_ABI_CONTEXT_UID
:
334 context_type_string
= config_event_context_uid
;
336 case LTTNG_KERNEL_ABI_CONTEXT_EUID
:
337 context_type_string
= config_event_context_euid
;
339 case LTTNG_KERNEL_ABI_CONTEXT_SUID
:
340 context_type_string
= config_event_context_suid
;
342 case LTTNG_KERNEL_ABI_CONTEXT_GID
:
343 context_type_string
= config_event_context_gid
;
345 case LTTNG_KERNEL_ABI_CONTEXT_EGID
:
346 context_type_string
= config_event_context_egid
;
348 case LTTNG_KERNEL_ABI_CONTEXT_SGID
:
349 context_type_string
= config_event_context_sgid
;
351 case LTTNG_KERNEL_ABI_CONTEXT_VUID
:
352 context_type_string
= config_event_context_vuid
;
354 case LTTNG_KERNEL_ABI_CONTEXT_VEUID
:
355 context_type_string
= config_event_context_veuid
;
357 case LTTNG_KERNEL_ABI_CONTEXT_VSUID
:
358 context_type_string
= config_event_context_vsuid
;
360 case LTTNG_KERNEL_ABI_CONTEXT_VGID
:
361 context_type_string
= config_event_context_vgid
;
363 case LTTNG_KERNEL_ABI_CONTEXT_VEGID
:
364 context_type_string
= config_event_context_vegid
;
366 case LTTNG_KERNEL_ABI_CONTEXT_VSGID
:
367 context_type_string
= config_event_context_vsgid
;
370 context_type_string
= NULL
;
373 return context_type_string
;
377 const char *get_ust_context_type_string(
378 enum lttng_ust_abi_context_type context_type
)
380 const char *context_type_string
;
382 switch (context_type
) {
383 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
384 context_type_string
= config_event_context_procname
;
386 case LTTNG_UST_ABI_CONTEXT_VPID
:
387 context_type_string
= config_event_context_vpid
;
389 case LTTNG_UST_ABI_CONTEXT_VTID
:
390 context_type_string
= config_event_context_vtid
;
392 case LTTNG_UST_ABI_CONTEXT_IP
:
393 context_type_string
= config_event_context_ip
;
395 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
396 context_type_string
= config_event_context_pthread_id
;
398 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
399 context_type_string
= config_event_context_app
;
401 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
402 context_type_string
= config_event_context_cgroup_ns
;
404 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
405 context_type_string
= config_event_context_ipc_ns
;
407 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
408 context_type_string
= config_event_context_mnt_ns
;
410 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
411 context_type_string
= config_event_context_net_ns
;
413 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
414 context_type_string
= config_event_context_time_ns
;
416 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
417 context_type_string
= config_event_context_pid_ns
;
419 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
420 context_type_string
= config_event_context_user_ns
;
422 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
423 context_type_string
= config_event_context_uts_ns
;
425 case LTTNG_UST_ABI_CONTEXT_VUID
:
426 context_type_string
= config_event_context_vuid
;
428 case LTTNG_UST_ABI_CONTEXT_VEUID
:
429 context_type_string
= config_event_context_veuid
;
431 case LTTNG_UST_ABI_CONTEXT_VSUID
:
432 context_type_string
= config_event_context_vsuid
;
434 case LTTNG_UST_ABI_CONTEXT_VGID
:
435 context_type_string
= config_event_context_vgid
;
437 case LTTNG_UST_ABI_CONTEXT_VEGID
:
438 context_type_string
= config_event_context_vegid
;
440 case LTTNG_UST_ABI_CONTEXT_VSGID
:
441 context_type_string
= config_event_context_vsgid
;
443 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
445 * Error, should not be stored in the XML, perf contexts
446 * are stored as a node of type event_perf_context_type.
449 context_type_string
= NULL
;
453 return context_type_string
;
457 const char *get_buffer_type_string(
458 enum lttng_buffer_type buffer_type
)
460 const char *buffer_type_string
;
462 switch (buffer_type
) {
463 case LTTNG_BUFFER_PER_PID
:
464 buffer_type_string
= config_buffer_type_per_pid
;
466 case LTTNG_BUFFER_PER_UID
:
467 buffer_type_string
= config_buffer_type_per_uid
;
469 case LTTNG_BUFFER_GLOBAL
:
470 buffer_type_string
= config_buffer_type_global
;
473 buffer_type_string
= NULL
;
476 return buffer_type_string
;
480 const char *get_loglevel_type_string(
481 enum lttng_ust_abi_loglevel_type loglevel_type
)
483 const char *loglevel_type_string
;
485 switch (loglevel_type
) {
486 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
487 loglevel_type_string
= config_loglevel_type_all
;
489 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
490 loglevel_type_string
= config_loglevel_type_range
;
492 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
493 loglevel_type_string
= config_loglevel_type_single
;
496 loglevel_type_string
= NULL
;
499 return loglevel_type_string
;
502 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
504 int save_kernel_function_event(struct config_writer
*writer
,
505 struct ltt_kernel_event
*event
)
509 ret
= config_writer_open_element(writer
, config_element_function_attributes
);
511 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
515 ret
= config_writer_write_element_string(writer
, config_element_name
,
516 event
->event
->u
.ftrace
.symbol_name
);
518 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
522 /* /function attributes */
523 ret
= config_writer_close_element(writer
);
525 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
533 int save_kernel_kprobe_event(struct config_writer
*writer
,
534 struct ltt_kernel_event
*event
)
537 const char *symbol_name
;
541 switch (event
->event
->instrumentation
) {
542 case LTTNG_KERNEL_ABI_KPROBE
:
544 * Comments in lttng-kernel.h mention that
545 * either addr or symbol_name are set, not both.
547 addr
= event
->event
->u
.kprobe
.addr
;
548 offset
= event
->event
->u
.kprobe
.offset
;
549 symbol_name
= addr
? NULL
: event
->event
->u
.kprobe
.symbol_name
;
551 case LTTNG_KERNEL_ABI_KRETPROBE
:
552 addr
= event
->event
->u
.kretprobe
.addr
;
553 offset
= event
->event
->u
.kretprobe
.offset
;
554 symbol_name
= addr
? NULL
: event
->event
->u
.kretprobe
.symbol_name
;
558 ERR("Unsupported kernel instrumentation type.");
559 ret
= LTTNG_ERR_INVALID
;
563 ret
= config_writer_open_element(writer
, config_element_probe_attributes
);
565 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
570 ret
= config_writer_write_element_unsigned_int( writer
,
571 config_element_address
, addr
);
573 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
576 } else if (symbol_name
) {
577 ret
= config_writer_write_element_string(writer
,
578 config_element_symbol_name
, symbol_name
);
580 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
583 /* If the offset is non-zero, write it.*/
585 ret
= config_writer_write_element_unsigned_int(writer
,
586 config_element_offset
, offset
);
588 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
594 * This really should not happen as we are either setting the
595 * address or the symbol above.
597 ERR("Invalid probe/function description.");
598 ret
= LTTNG_ERR_INVALID
;
603 ret
= config_writer_close_element(writer
);
605 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
613 * Save the userspace probe tracepoint event associated with the event to the
617 int save_kernel_userspace_probe_tracepoint_event(struct config_writer
*writer
,
618 struct ltt_kernel_event
*event
)
621 const char *probe_name
, *provider_name
, *binary_path
;
622 const struct lttng_userspace_probe_location
*userspace_probe_location
;
623 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
624 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
626 /* Get userspace probe location from the event. */
627 userspace_probe_location
= event
->userspace_probe_location
;
628 if (!userspace_probe_location
) {
629 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
633 /* Get lookup method and lookup method type. */
634 lookup_method
= lttng_userspace_probe_location_get_lookup_method(userspace_probe_location
);
635 if (!lookup_method
) {
636 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
640 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
642 /* Get the binary path, probe name and provider name. */
644 lttng_userspace_probe_location_tracepoint_get_binary_path(
645 userspace_probe_location
);
647 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
652 lttng_userspace_probe_location_tracepoint_get_probe_name(
653 userspace_probe_location
);
655 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
660 lttng_userspace_probe_location_tracepoint_get_provider_name(
661 userspace_probe_location
);
662 if (!provider_name
) {
663 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
667 /* Open a userspace probe tracepoint attribute. */
668 ret
= config_writer_open_element(writer
, config_element_userspace_probe_tracepoint_attributes
);
670 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
674 switch (lookup_type
) {
675 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
676 ret
= config_writer_write_element_string(writer
,
677 config_element_userspace_probe_lookup
,
678 config_element_userspace_probe_lookup_tracepoint_sdt
);
680 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
685 ERR("Unsupported kernel userspace probe tracepoint lookup method.");
686 ret
= LTTNG_ERR_INVALID
;
690 /* Write the binary path, provider name and the probe name. */
691 ret
= config_writer_write_element_string(writer
,
692 config_element_userspace_probe_location_binary_path
,
695 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
699 ret
= config_writer_write_element_string(writer
,
700 config_element_userspace_probe_tracepoint_location_provider_name
,
703 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
707 ret
= config_writer_write_element_string(writer
,
708 config_element_userspace_probe_tracepoint_location_probe_name
,
711 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
715 /* Close the userspace probe tracepoint attribute. */
716 ret
= config_writer_close_element(writer
);
718 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
727 * Save the userspace probe function event associated with the event to the
731 int save_kernel_userspace_probe_function_event(struct config_writer
*writer
,
732 struct ltt_kernel_event
*event
)
735 const char *function_name
, *binary_path
;
736 const struct lttng_userspace_probe_location
*userspace_probe_location
;
737 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
738 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
740 /* Get userspace probe location from the event. */
741 userspace_probe_location
= event
->userspace_probe_location
;
742 if (!userspace_probe_location
) {
743 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
747 /* Get lookup method and lookup method type. */
748 lookup_method
= lttng_userspace_probe_location_get_lookup_method(
749 userspace_probe_location
);
750 if (!lookup_method
) {
751 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
755 /* Get the binary path and the function name. */
757 lttng_userspace_probe_location_function_get_binary_path(
758 userspace_probe_location
);
760 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
765 lttng_userspace_probe_location_function_get_function_name(
766 userspace_probe_location
);
767 if (!function_name
) {
768 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
772 /* Open a userspace probe function attribute. */
773 ret
= config_writer_open_element(writer
,
774 config_element_userspace_probe_function_attributes
);
776 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
780 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
781 switch (lookup_type
) {
782 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
783 ret
= config_writer_write_element_string(writer
,
784 config_element_userspace_probe_lookup
,
785 config_element_userspace_probe_lookup_function_elf
);
787 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
791 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
792 ret
= config_writer_write_element_string(writer
,
793 config_element_userspace_probe_lookup
,
794 config_element_userspace_probe_lookup_function_default
);
796 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
801 ERR("Unsupported kernel userspace probe function lookup method.");
802 ret
= LTTNG_ERR_INVALID
;
806 /* Write the binary path and the function name. */
807 ret
= config_writer_write_element_string(writer
,
808 config_element_userspace_probe_location_binary_path
,
811 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
815 ret
= config_writer_write_element_string(writer
,
816 config_element_userspace_probe_function_location_function_name
,
819 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
823 /* Close the userspace probe function attribute. */
824 ret
= config_writer_close_element(writer
);
826 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
835 int save_kernel_userspace_probe_event(struct config_writer
*writer
,
836 struct ltt_kernel_event
*event
)
839 struct lttng_userspace_probe_location
*userspace_probe_location
;
841 /* Get userspace probe location from the event. */
842 userspace_probe_location
= event
->userspace_probe_location
;
843 if (!userspace_probe_location
) {
844 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
848 switch(lttng_userspace_probe_location_get_type(userspace_probe_location
)) {
849 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
851 ret
= save_kernel_userspace_probe_function_event(writer
, event
);
853 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
858 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
860 ret
= save_kernel_userspace_probe_tracepoint_event(writer
, event
);
862 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
867 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
:
869 ERR("Unsupported kernel userspace probe location type.");
870 ret
= LTTNG_ERR_INVALID
;
879 int save_kernel_event(struct config_writer
*writer
,
880 struct ltt_kernel_event
*event
)
883 const char *instrumentation_type
;
885 ret
= config_writer_open_element(writer
, config_element_event
);
887 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
891 if (event
->event
->name
[0]) {
892 ret
= config_writer_write_element_string(writer
,
893 config_element_name
, event
->event
->name
);
895 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
900 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
903 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
907 instrumentation_type
= get_kernel_instrumentation_string(
908 event
->event
->instrumentation
);
909 if (!instrumentation_type
) {
910 ret
= LTTNG_ERR_INVALID
;
914 ret
= config_writer_write_element_string(writer
, config_element_type
,
915 instrumentation_type
);
917 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
921 if (event
->filter_expression
) {
922 ret
= config_writer_write_element_string(writer
,
923 config_element_filter
,
924 event
->filter_expression
);
926 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
931 if (event
->event
->instrumentation
== LTTNG_KERNEL_ABI_FUNCTION
||
932 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_KPROBE
||
933 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_UPROBE
||
934 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_KRETPROBE
) {
936 ret
= config_writer_open_element(writer
,
937 config_element_attributes
);
939 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
943 switch (event
->event
->instrumentation
) {
944 case LTTNG_KERNEL_ABI_SYSCALL
:
945 case LTTNG_KERNEL_ABI_FUNCTION
:
946 ret
= save_kernel_function_event(writer
, event
);
951 case LTTNG_KERNEL_ABI_KPROBE
:
952 case LTTNG_KERNEL_ABI_KRETPROBE
:
953 ret
= save_kernel_kprobe_event(writer
, event
);
958 case LTTNG_KERNEL_ABI_UPROBE
:
959 ret
= save_kernel_userspace_probe_event(writer
, event
);
965 ERR("Unsupported kernel instrumentation type.");
966 ret
= LTTNG_ERR_INVALID
;
971 ret
= config_writer_close_element(writer
);
973 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
979 ret
= config_writer_close_element(writer
);
981 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
990 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
992 int save_kernel_events(struct config_writer
*writer
,
993 struct ltt_kernel_channel
*kchan
)
996 struct ltt_kernel_event
*event
;
998 ret
= config_writer_open_element(writer
, config_element_events
);
1000 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1004 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1005 ret
= save_kernel_event(writer
, event
);
1006 if (ret
!= LTTNG_OK
) {
1012 ret
= config_writer_close_element(writer
);
1014 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1023 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1025 int save_ust_event(struct config_writer
*writer
,
1026 struct ltt_ust_event
*event
)
1029 const char *loglevel_type_string
;
1031 ret
= config_writer_open_element(writer
, config_element_event
);
1033 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1037 if (event
->attr
.name
[0]) {
1038 ret
= config_writer_write_element_string(writer
,
1039 config_element_name
, event
->attr
.name
);
1041 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1046 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1049 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1053 if (event
->attr
.instrumentation
!= LTTNG_UST_ABI_TRACEPOINT
) {
1054 ERR("Unsupported UST instrumentation type.");
1055 ret
= LTTNG_ERR_INVALID
;
1058 ret
= config_writer_write_element_string(writer
, config_element_type
,
1059 config_event_type_tracepoint
);
1061 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1065 loglevel_type_string
= get_loglevel_type_string(
1066 (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel_type
);
1067 if (!loglevel_type_string
) {
1068 ERR("Unsupported UST loglevel type.");
1069 ret
= LTTNG_ERR_INVALID
;
1073 ret
= config_writer_write_element_string(writer
,
1074 config_element_loglevel_type
, loglevel_type_string
);
1076 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1080 /* The log level is irrelevant if no "filtering" is enabled */
1081 if (event
->attr
.loglevel_type
!= LTTNG_UST_ABI_LOGLEVEL_ALL
) {
1082 ret
= config_writer_write_element_signed_int(writer
,
1083 config_element_loglevel
, event
->attr
.loglevel
);
1085 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1090 if (event
->filter_expression
) {
1091 ret
= config_writer_write_element_string(writer
,
1092 config_element_filter
, event
->filter_expression
);
1094 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1099 if (event
->exclusion
&& event
->exclusion
->count
) {
1102 ret
= config_writer_open_element(writer
,
1103 config_element_exclusions
);
1105 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1109 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
1110 ret
= config_writer_write_element_string(writer
,
1111 config_element_exclusion
,
1112 LTTNG_EVENT_EXCLUSION_NAME_AT(
1113 event
->exclusion
, i
));
1115 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1121 ret
= config_writer_close_element(writer
);
1123 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1129 ret
= config_writer_close_element(writer
);
1131 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1140 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1142 int save_ust_events(struct config_writer
*writer
,
1143 struct lttng_ht
*events
)
1146 struct ltt_ust_event
*event
;
1147 struct lttng_ht_node_str
*node
;
1148 struct lttng_ht_iter iter
;
1150 ret
= config_writer_open_element(writer
, config_element_events
);
1152 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1157 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1158 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
1160 if (event
->internal
) {
1161 /* Internal events must not be exposed to clients */
1164 ret
= save_ust_event(writer
, event
);
1165 if (ret
!= LTTNG_OK
) {
1173 ret
= config_writer_close_element(writer
);
1175 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1184 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1186 int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
1187 struct agent_event
*agent_event
)
1190 enum lttng_ust_abi_loglevel_type ust_loglevel_type
;
1192 ust_event
->enabled
= AGENT_EVENT_IS_ENABLED(agent_event
);
1193 ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
1194 if (lttng_strncpy(ust_event
->attr
.name
, agent_event
->name
,
1195 LTTNG_SYMBOL_NAME_LEN
)) {
1196 ret
= LTTNG_ERR_INVALID
;
1199 switch (agent_event
->loglevel_type
) {
1200 case LTTNG_EVENT_LOGLEVEL_ALL
:
1201 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
1203 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1204 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
1206 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1207 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
1210 ERR("Invalid agent_event loglevel_type.");
1211 ret
= LTTNG_ERR_INVALID
;
1215 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
1216 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
1217 ust_event
->filter_expression
= agent_event
->filter_expression
;
1218 ust_event
->exclusion
= agent_event
->exclusion
;
1225 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1227 int save_agent_events(struct config_writer
*writer
,
1228 struct agent
*agent
)
1231 struct lttng_ht_iter iter
;
1232 struct lttng_ht_node_str
*node
;
1234 ret
= config_writer_open_element(writer
, config_element_events
);
1236 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1241 cds_lfht_for_each_entry(agent
->events
->ht
, &iter
.iter
, node
, node
) {
1242 struct agent_event
*agent_event
;
1243 struct ltt_ust_event fake_event
;
1245 memset(&fake_event
, 0, sizeof(fake_event
));
1246 agent_event
= caa_container_of(node
, struct agent_event
, node
);
1249 * Initialize a fake ust event to reuse the same serialization
1250 * function since UST and agent events contain the same info
1251 * (and one could wonder why they don't reuse the same
1254 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
1255 if (ret
!= LTTNG_OK
) {
1259 ret
= save_ust_event(writer
, &fake_event
);
1260 if (ret
!= LTTNG_OK
) {
1268 ret
= config_writer_close_element(writer
);
1270 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1279 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1281 int save_kernel_context(struct config_writer
*writer
,
1282 struct lttng_kernel_abi_context
*ctx
)
1290 ret
= config_writer_open_element(writer
, config_element_context
);
1292 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1296 if (ctx
->ctx
== LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER
) {
1297 ret
= config_writer_open_element(writer
,
1298 config_element_context_perf
);
1300 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1304 ret
= config_writer_write_element_unsigned_int(writer
,
1305 config_element_type
, ctx
->u
.perf_counter
.type
);
1307 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1311 ret
= config_writer_write_element_unsigned_int(writer
,
1312 config_element_config
, ctx
->u
.perf_counter
.config
);
1314 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1318 ret
= config_writer_write_element_string(writer
,
1319 config_element_name
, ctx
->u
.perf_counter
.name
);
1321 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1326 ret
= config_writer_close_element(writer
);
1328 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1332 const char *context_type_string
=
1333 get_kernel_context_type_string(ctx
->ctx
);
1335 if (!context_type_string
) {
1336 ERR("Unsupported kernel context type.");
1337 ret
= LTTNG_ERR_INVALID
;
1341 ret
= config_writer_write_element_string(writer
,
1342 config_element_type
, context_type_string
);
1344 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1350 ret
= config_writer_close_element(writer
);
1352 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1361 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1363 int save_kernel_contexts(struct config_writer
*writer
,
1364 struct ltt_kernel_channel
*kchan
)
1367 struct ltt_kernel_context
*ctx
;
1369 if (cds_list_empty(&kchan
->ctx_list
)) {
1374 ret
= config_writer_open_element(writer
, config_element_contexts
);
1376 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1380 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
1381 ret
= save_kernel_context(writer
, &ctx
->ctx
);
1382 if (ret
!= LTTNG_OK
) {
1388 ret
= config_writer_close_element(writer
);
1390 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1399 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1401 int save_ust_context_perf_thread_counter(struct config_writer
*writer
,
1402 struct ltt_ust_context
*ctx
)
1406 LTTNG_ASSERT(writer
);
1409 /* Perf contexts are saved as event_perf_context_type */
1410 ret
= config_writer_open_element(writer
, config_element_context_perf
);
1412 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1416 ret
= config_writer_write_element_unsigned_int(writer
,
1417 config_element_type
, ctx
->ctx
.u
.perf_counter
.type
);
1419 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1423 ret
= config_writer_write_element_unsigned_int(writer
,
1424 config_element_config
, ctx
->ctx
.u
.perf_counter
.config
);
1426 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1430 ret
= config_writer_write_element_string(writer
, config_element_name
,
1431 ctx
->ctx
.u
.perf_counter
.name
);
1433 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1438 ret
= config_writer_close_element(writer
);
1440 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1449 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1451 int save_ust_context_app_ctx(struct config_writer
*writer
,
1452 struct ltt_ust_context
*ctx
)
1456 LTTNG_ASSERT(writer
);
1459 /* Application contexts are saved as application_context_type */
1460 ret
= config_writer_open_element(writer
, config_element_context_app
);
1462 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1466 ret
= config_writer_write_element_string(writer
,
1467 config_element_context_app_provider_name
,
1468 ctx
->ctx
.u
.app_ctx
.provider_name
);
1470 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1474 ret
= config_writer_write_element_string(writer
,
1475 config_element_context_app_ctx_name
,
1476 ctx
->ctx
.u
.app_ctx
.ctx_name
);
1478 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1483 ret
= config_writer_close_element(writer
);
1485 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1494 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1496 int save_ust_context_generic(struct config_writer
*writer
,
1497 struct ltt_ust_context
*ctx
)
1500 const char *context_type_string
;
1502 LTTNG_ASSERT(writer
);
1505 /* Save context as event_context_type_type */
1506 context_type_string
= get_ust_context_type_string(
1508 if (!context_type_string
) {
1509 ERR("Unsupported UST context type.");
1510 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1514 ret
= config_writer_write_element_string(writer
,
1515 config_element_type
, context_type_string
);
1517 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1526 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1528 int save_ust_context(struct config_writer
*writer
,
1529 struct cds_list_head
*ctx_list
)
1532 struct ltt_ust_context
*ctx
;
1534 LTTNG_ASSERT(writer
);
1535 LTTNG_ASSERT(ctx_list
);
1537 ret
= config_writer_open_element(writer
, config_element_contexts
);
1539 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1543 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
1544 ret
= config_writer_open_element(writer
,
1545 config_element_context
);
1547 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1551 switch (ctx
->ctx
.ctx
) {
1552 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1553 ret
= save_ust_context_perf_thread_counter(writer
, ctx
);
1555 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1556 ret
= save_ust_context_app_ctx(writer
, ctx
);
1559 /* Save generic context. */
1560 ret
= save_ust_context_generic(writer
, ctx
);
1562 if (ret
!= LTTNG_OK
) {
1567 ret
= config_writer_close_element(writer
);
1569 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1575 ret
= config_writer_close_element(writer
);
1577 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1586 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1588 int save_kernel_channel(struct config_writer
*writer
,
1589 struct ltt_kernel_channel
*kchan
)
1593 LTTNG_ASSERT(writer
);
1594 LTTNG_ASSERT(kchan
);
1596 ret
= config_writer_open_element(writer
, config_element_channel
);
1598 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1602 ret
= config_writer_write_element_string(writer
, config_element_name
,
1603 kchan
->channel
->name
);
1605 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1609 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1610 kchan
->channel
->enabled
);
1612 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1616 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1617 if (ret
!= LTTNG_OK
) {
1621 ret
= save_kernel_events(writer
, kchan
);
1622 if (ret
!= LTTNG_OK
) {
1626 ret
= save_kernel_contexts(writer
, kchan
);
1627 if (ret
!= LTTNG_OK
) {
1632 ret
= config_writer_close_element(writer
);
1634 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1643 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1645 int save_ust_channel(struct config_writer
*writer
,
1646 struct ltt_ust_channel
*ust_chan
,
1647 struct ltt_ust_session
*session
)
1651 LTTNG_ASSERT(writer
);
1652 LTTNG_ASSERT(ust_chan
);
1653 LTTNG_ASSERT(session
);
1655 ret
= config_writer_open_element(writer
, config_element_channel
);
1657 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1661 ret
= config_writer_write_element_string(writer
, config_element_name
,
1664 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1668 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1671 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1675 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1676 if (ret
!= LTTNG_OK
) {
1680 ret
= config_writer_write_element_unsigned_int(writer
,
1681 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1683 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1687 ret
= config_writer_write_element_unsigned_int(writer
,
1688 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1690 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1694 ret
= config_writer_write_element_unsigned_int(writer
,
1695 config_element_live_timer_interval
,
1696 session
->live_timer_interval
);
1698 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1702 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1703 ret
= save_ust_events(writer
, ust_chan
->events
);
1704 if (ret
!= LTTNG_OK
) {
1708 struct agent
*agent
= NULL
;
1710 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1712 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1713 ERR("Could not find agent associated to UST subdomain");
1718 * Channels associated with a UST sub-domain (such as JUL, Log4j
1719 * or Python) don't have any non-internal events. We retrieve
1720 * the "agent" events associated with this channel and serialize
1723 ret
= save_agent_events(writer
, agent
);
1724 if (ret
!= LTTNG_OK
) {
1729 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1730 if (ret
!= LTTNG_OK
) {
1735 ret
= config_writer_close_element(writer
);
1737 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1746 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1748 int save_kernel_session(struct config_writer
*writer
,
1749 struct ltt_session
*session
)
1752 struct ltt_kernel_channel
*kchan
;
1754 LTTNG_ASSERT(writer
);
1755 LTTNG_ASSERT(session
);
1757 ret
= config_writer_write_element_string(writer
, config_element_type
,
1758 config_domain_type_kernel
);
1760 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1764 ret
= config_writer_write_element_string(writer
,
1765 config_element_buffer_type
, config_buffer_type_global
);
1767 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1771 ret
= config_writer_open_element(writer
,
1772 config_element_channels
);
1774 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1778 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1780 ret
= save_kernel_channel(writer
, kchan
);
1781 if (ret
!= LTTNG_OK
) {
1787 ret
= config_writer_close_element(writer
);
1789 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1799 const char *get_config_domain_str(enum lttng_domain_type domain
)
1801 const char *str_dom
;
1804 case LTTNG_DOMAIN_KERNEL
:
1805 str_dom
= config_domain_type_kernel
;
1807 case LTTNG_DOMAIN_UST
:
1808 str_dom
= config_domain_type_ust
;
1810 case LTTNG_DOMAIN_JUL
:
1811 str_dom
= config_domain_type_jul
;
1813 case LTTNG_DOMAIN_LOG4J
:
1814 str_dom
= config_domain_type_log4j
;
1816 case LTTNG_DOMAIN_PYTHON
:
1817 str_dom
= config_domain_type_python
;
1826 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1827 static int save_process_attr_tracker(struct config_writer
*writer
,
1828 struct ltt_session
*sess
,
1830 enum lttng_process_attr process_attr
)
1833 const char *element_id_tracker
, *element_target_id
, *element_id
;
1834 const struct process_attr_tracker
*tracker
;
1835 enum lttng_tracking_policy tracking_policy
;
1836 struct lttng_process_attr_values
*values
= NULL
;
1838 switch (process_attr
) {
1839 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1840 element_id_tracker
= config_element_process_attr_tracker_pid
;
1841 element_target_id
= config_element_process_attr_pid_value
;
1842 element_id
= config_element_process_attr_id
;
1844 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1845 element_id_tracker
= config_element_process_attr_tracker_vpid
;
1846 element_target_id
= config_element_process_attr_vpid_value
;
1847 element_id
= config_element_process_attr_id
;
1849 case LTTNG_PROCESS_ATTR_USER_ID
:
1850 element_id_tracker
= config_element_process_attr_tracker_uid
;
1851 element_target_id
= config_element_process_attr_uid_value
;
1852 element_id
= config_element_process_attr_id
;
1854 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1855 element_id_tracker
= config_element_process_attr_tracker_vuid
;
1856 element_target_id
= config_element_process_attr_vuid_value
;
1857 element_id
= config_element_process_attr_id
;
1859 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1860 element_id_tracker
= config_element_process_attr_tracker_gid
;
1861 element_target_id
= config_element_process_attr_gid_value
;
1862 element_id
= config_element_process_attr_id
;
1864 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1865 element_id_tracker
= config_element_process_attr_tracker_vgid
;
1866 element_target_id
= config_element_process_attr_vgid_value
;
1867 element_id
= config_element_process_attr_id
;
1870 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1875 case LTTNG_DOMAIN_KERNEL
:
1877 tracker
= kernel_get_process_attr_tracker(
1878 sess
->kernel_session
, process_attr
);
1879 LTTNG_ASSERT(tracker
);
1882 case LTTNG_DOMAIN_UST
:
1884 tracker
= trace_ust_get_process_attr_tracker(
1885 sess
->ust_session
, process_attr
);
1886 LTTNG_ASSERT(tracker
);
1889 case LTTNG_DOMAIN_JUL
:
1890 case LTTNG_DOMAIN_LOG4J
:
1891 case LTTNG_DOMAIN_PYTHON
:
1893 ret
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1897 tracking_policy
= process_attr_tracker_get_tracking_policy(tracker
);
1898 if (tracking_policy
== LTTNG_TRACKING_POLICY_INCLUDE_ALL
) {
1899 /* Tracking all, nothing to output. */
1904 ret
= config_writer_open_element(writer
, element_id_tracker
);
1906 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1910 ret
= config_writer_open_element(
1911 writer
, config_element_process_attr_values
);
1913 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1917 if (tracking_policy
== LTTNG_TRACKING_POLICY_INCLUDE_SET
) {
1918 unsigned int i
, count
;
1919 enum process_attr_tracker_status status
=
1920 process_attr_tracker_get_inclusion_set(
1923 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1924 ret
= LTTNG_ERR_NOMEM
;
1928 count
= _lttng_process_attr_values_get_count(values
);
1930 for (i
= 0; i
< count
; i
++) {
1931 unsigned int integral_value
= UINT_MAX
;
1932 const char *name
= NULL
;
1933 const struct process_attr_value
*value
=
1934 lttng_process_attr_tracker_values_get_at_index(
1937 LTTNG_ASSERT(value
);
1938 ret
= config_writer_open_element(
1939 writer
, element_target_id
);
1941 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1945 switch (value
->type
) {
1946 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
1948 (unsigned int) value
->value
.pid
;
1950 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
1952 (unsigned int) value
->value
.uid
;
1954 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
1956 (unsigned int) value
->value
.gid
;
1958 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
1959 name
= value
->value
.user_name
;
1962 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
1963 name
= value
->value
.group_name
;
1971 ret
= config_writer_write_element_string(writer
,
1972 config_element_name
, name
);
1974 ret
= config_writer_write_element_unsigned_int(
1980 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1984 /* /$element_target_id */
1985 ret
= config_writer_close_element(writer
);
1987 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1994 ret
= config_writer_close_element(writer
);
1996 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2000 /* /$element_id_tracker */
2001 ret
= config_writer_close_element(writer
);
2003 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2009 lttng_process_attr_values_destroy(values
);
2013 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2014 static int save_process_attr_trackers(struct config_writer
*writer
,
2015 struct ltt_session
*sess
,
2021 case LTTNG_DOMAIN_KERNEL
:
2022 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2023 LTTNG_PROCESS_ATTR_PROCESS_ID
);
2024 if (ret
!= LTTNG_OK
) {
2027 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2028 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
);
2029 if (ret
!= LTTNG_OK
) {
2032 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2033 LTTNG_PROCESS_ATTR_USER_ID
);
2034 if (ret
!= LTTNG_OK
) {
2037 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2038 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
);
2039 if (ret
!= LTTNG_OK
) {
2042 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2043 LTTNG_PROCESS_ATTR_GROUP_ID
);
2044 if (ret
!= LTTNG_OK
) {
2047 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2048 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
);
2049 if (ret
!= LTTNG_OK
) {
2053 case LTTNG_DOMAIN_UST
:
2054 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2055 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
);
2056 if (ret
!= LTTNG_OK
) {
2059 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2060 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
);
2061 if (ret
!= LTTNG_OK
) {
2064 ret
= save_process_attr_tracker(writer
, sess
, domain
,
2065 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
);
2066 if (ret
!= LTTNG_OK
) {
2071 ret
= LTTNG_ERR_INVALID
;
2079 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2081 int save_ust_domain(struct config_writer
*writer
,
2082 struct ltt_session
*session
, enum lttng_domain_type domain
)
2085 struct ltt_ust_channel
*ust_chan
;
2086 const char *buffer_type_string
;
2087 struct lttng_ht_node_str
*node
;
2088 struct lttng_ht_iter iter
;
2089 const char *config_domain_name
;
2091 LTTNG_ASSERT(writer
);
2092 LTTNG_ASSERT(session
);
2094 ret
= config_writer_open_element(writer
,
2095 config_element_domain
);
2097 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2101 config_domain_name
= get_config_domain_str(domain
);
2102 if (!config_domain_name
) {
2103 ret
= LTTNG_ERR_INVALID
;
2107 ret
= config_writer_write_element_string(writer
,
2108 config_element_type
, config_domain_name
);
2110 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2114 buffer_type_string
= get_buffer_type_string(
2115 session
->ust_session
->buffer_type
);
2116 if (!buffer_type_string
) {
2117 ERR("Unsupported buffer type.");
2118 ret
= LTTNG_ERR_INVALID
;
2122 ret
= config_writer_write_element_string(writer
,
2123 config_element_buffer_type
, buffer_type_string
);
2125 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2129 ret
= config_writer_open_element(writer
, config_element_channels
);
2131 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2136 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2137 &iter
.iter
, node
, node
) {
2138 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
2139 if (domain
== ust_chan
->domain
) {
2140 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
2141 if (ret
!= LTTNG_OK
) {
2150 ret
= config_writer_close_element(writer
);
2152 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2156 if (domain
== LTTNG_DOMAIN_UST
) {
2157 ret
= config_writer_open_element(
2158 writer
, config_element_process_attr_trackers
);
2160 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2164 ret
= save_process_attr_trackers(
2165 writer
, session
, LTTNG_DOMAIN_UST
);
2166 if (ret
!= LTTNG_OK
) {
2171 ret
= config_writer_close_element(writer
);
2173 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2179 ret
= config_writer_close_element(writer
);
2181 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2190 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2192 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
2196 LTTNG_ASSERT(writer
);
2197 LTTNG_ASSERT(session
);
2199 if (!session
->kernel_session
&& !session
->ust_session
) {
2203 ret
= config_writer_open_element(writer
, config_element_domains
);
2205 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2209 if (session
->kernel_session
) {
2210 ret
= config_writer_open_element(writer
,
2211 config_element_domain
);
2213 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2217 ret
= save_kernel_session(writer
, session
);
2218 if (ret
!= LTTNG_OK
) {
2222 ret
= config_writer_open_element(
2223 writer
, config_element_process_attr_trackers
);
2225 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2229 ret
= save_process_attr_trackers(
2230 writer
, session
, LTTNG_DOMAIN_KERNEL
);
2231 if (ret
!= LTTNG_OK
) {
2236 ret
= config_writer_close_element(writer
);
2238 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2242 ret
= config_writer_close_element(writer
);
2244 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2249 if (session
->ust_session
) {
2250 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
2251 if (ret
!= LTTNG_OK
) {
2255 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
2256 if (ret
!= LTTNG_OK
) {
2260 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
2261 if (ret
!= LTTNG_OK
) {
2265 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
2266 if (ret
!= LTTNG_OK
) {
2272 ret
= config_writer_close_element(writer
);
2274 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2283 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2285 int save_consumer_output(struct config_writer
*writer
,
2286 struct consumer_output
*output
)
2290 LTTNG_ASSERT(writer
);
2291 LTTNG_ASSERT(output
);
2293 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
2295 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2299 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
2302 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2306 ret
= config_writer_open_element(writer
, config_element_destination
);
2308 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2312 switch (output
->type
) {
2313 case CONSUMER_DST_LOCAL
:
2314 ret
= config_writer_write_element_string(writer
,
2315 config_element_path
, output
->dst
.session_root_path
);
2317 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2321 case CONSUMER_DST_NET
:
2325 uri
= (char *) zmalloc(PATH_MAX
);
2327 ret
= LTTNG_ERR_NOMEM
;
2331 ret
= config_writer_open_element(writer
, config_element_net_output
);
2333 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2334 goto end_net_output
;
2337 if (output
->dst
.net
.control_isset
&&
2338 output
->dst
.net
.data_isset
) {
2339 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
2341 ret
= LTTNG_ERR_INVALID
;
2342 goto end_net_output
;
2345 ret
= config_writer_write_element_string(writer
,
2346 config_element_control_uri
, uri
);
2348 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2349 goto end_net_output
;
2352 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
2354 ret
= LTTNG_ERR_INVALID
;
2355 goto end_net_output
;
2358 ret
= config_writer_write_element_string(writer
,
2359 config_element_data_uri
, uri
);
2361 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2362 goto end_net_output
;
2367 if (ret
!= LTTNG_OK
) {
2371 ret
= !output
->dst
.net
.control_isset
?
2372 LTTNG_ERR_URL_CTRL_MISS
:
2373 LTTNG_ERR_URL_DATA_MISS
;
2378 ret
= config_writer_close_element(writer
);
2380 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2386 ERR("Unsupported consumer output type.");
2387 ret
= LTTNG_ERR_INVALID
;
2392 ret
= config_writer_close_element(writer
);
2394 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2398 /* /consumer_output */
2399 ret
= config_writer_close_element(writer
);
2401 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2410 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2412 int save_snapshot_outputs(struct config_writer
*writer
,
2413 struct snapshot
*snapshot
)
2416 struct lttng_ht_iter iter
;
2417 struct snapshot_output
*output
;
2419 LTTNG_ASSERT(writer
);
2420 LTTNG_ASSERT(snapshot
);
2422 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
2424 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2429 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
2431 ret
= config_writer_open_element(writer
,
2432 config_element_output
);
2434 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2438 ret
= config_writer_write_element_string(writer
,
2439 config_element_name
, output
->name
);
2441 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2445 ret
= config_writer_write_element_unsigned_int(writer
,
2446 config_element_max_size
, output
->max_size
);
2448 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2452 ret
= save_consumer_output(writer
, output
->consumer
);
2453 if (ret
!= LTTNG_OK
) {
2458 ret
= config_writer_close_element(writer
);
2460 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2466 /* /snapshot_outputs */
2467 ret
= config_writer_close_element(writer
);
2469 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2481 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2483 int save_session_output(struct config_writer
*writer
,
2484 struct ltt_session
*session
)
2488 LTTNG_ASSERT(writer
);
2489 LTTNG_ASSERT(session
);
2491 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
2492 (!session
->snapshot_mode
&& !session
->consumer
)) {
2493 /* Session is in no output mode */
2498 ret
= config_writer_open_element(writer
, config_element_output
);
2500 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2504 if (session
->snapshot_mode
) {
2505 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
2506 if (ret
!= LTTNG_OK
) {
2510 if (session
->consumer
) {
2511 ret
= save_consumer_output(writer
, session
->consumer
);
2512 if (ret
!= LTTNG_OK
) {
2519 ret
= config_writer_close_element(writer
);
2521 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2530 int save_session_rotation_schedule(struct config_writer
*writer
,
2531 enum lttng_rotation_schedule_type type
, uint64_t value
)
2534 const char *element_name
;
2535 const char *value_name
;
2538 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
2539 element_name
= config_element_rotation_schedule_periodic
;
2540 value_name
= config_element_rotation_schedule_periodic_time_us
;
2542 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
2543 element_name
= config_element_rotation_schedule_size_threshold
;
2544 value_name
= config_element_rotation_schedule_size_threshold_bytes
;
2551 ret
= config_writer_open_element(writer
, element_name
);
2556 ret
= config_writer_write_element_unsigned_int(writer
,
2562 /* Close schedule descriptor element. */
2563 ret
= config_writer_close_element(writer
);
2572 int save_session_rotation_schedules(struct config_writer
*writer
,
2573 struct ltt_session
*session
)
2577 ret
= config_writer_open_element(writer
,
2578 config_element_rotation_schedules
);
2582 if (session
->rotate_timer_period
) {
2583 ret
= save_session_rotation_schedule(writer
,
2584 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
,
2585 session
->rotate_timer_period
);
2587 goto close_schedules
;
2590 if (session
->rotate_size
) {
2591 ret
= save_session_rotation_schedule(writer
,
2592 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
,
2593 session
->rotate_size
);
2595 goto close_schedules
;
2600 /* Close rotation schedules element. */
2601 ret
= config_writer_close_element(writer
);
2610 * Save the given session.
2612 * Return LTTNG_OK on success else a LTTNG_ERR* code.
2615 int save_session(struct ltt_session
*session
,
2616 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
2619 char config_file_path
[LTTNG_PATH_MAX
];
2621 struct config_writer
*writer
= NULL
;
2622 size_t session_name_len
;
2623 const char *provided_path
;
2624 int file_open_flags
= O_CREAT
| O_WRONLY
| O_TRUNC
;
2626 LTTNG_ASSERT(session
);
2628 LTTNG_ASSERT(creds
);
2630 session_name_len
= strlen(session
->name
);
2631 memset(config_file_path
, 0, sizeof(config_file_path
));
2633 if (!session_access_ok(session
,
2634 LTTNG_SOCK_GET_UID_CRED(creds
)) || session
->destroyed
) {
2635 ret
= LTTNG_ERR_EPERM
;
2639 provided_path
= lttng_save_session_attr_get_output_url(attr
);
2640 if (provided_path
) {
2641 DBG3("Save session in provided path %s", provided_path
);
2642 len
= strlen(provided_path
);
2643 if (len
>= sizeof(config_file_path
)) {
2644 ret
= LTTNG_ERR_SET_URL
;
2647 strncpy(config_file_path
, provided_path
, sizeof(config_file_path
));
2650 char *home_dir
= utils_get_user_home_dir(
2651 LTTNG_SOCK_GET_UID_CRED(creds
));
2653 ret
= LTTNG_ERR_SET_URL
;
2657 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
2658 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
2661 PERROR("snprintf save session");
2662 ret
= LTTNG_ERR_SET_URL
;
2669 * Check the path fits in the config file path dst including the '/'
2670 * followed by trailing .lttng extension and the NULL terminated string.
2672 if ((len
+ session_name_len
+ 2 +
2673 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
2674 > sizeof(config_file_path
)) {
2675 ret
= LTTNG_ERR_SET_URL
;
2679 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
2680 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
2682 ret
= LTTNG_ERR_SET_URL
;
2687 * At this point, we know that everything fits in the buffer. Validation
2688 * was done just above.
2690 config_file_path
[len
++] = '/';
2691 strncpy(config_file_path
+ len
, session
->name
, sizeof(config_file_path
) - len
);
2692 len
+= session_name_len
;
2693 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2694 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2695 config_file_path
[len
] = '\0';
2697 if (!attr
->overwrite
) {
2698 file_open_flags
|= O_EXCL
;
2701 fd
= run_as_open(config_file_path
, file_open_flags
,
2702 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
2703 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
2705 PERROR("Could not create configuration file");
2708 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
2711 ret
= LTTNG_ERR_EPERM
;
2714 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2720 writer
= config_writer_create(fd
, 1);
2722 ret
= LTTNG_ERR_NOMEM
;
2726 ret
= config_writer_open_element(writer
, config_element_sessions
);
2728 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2732 ret
= config_writer_open_element(writer
, config_element_session
);
2734 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2738 ret
= config_writer_write_element_string(writer
, config_element_name
,
2741 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2745 if (session
->shm_path
[0] != '\0') {
2746 ret
= config_writer_write_element_string(writer
,
2747 config_element_shared_memory_path
,
2750 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2755 ret
= save_domains(writer
, session
);
2756 if (ret
!= LTTNG_OK
) {
2760 ret
= config_writer_write_element_bool(writer
, config_element_started
,
2763 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2767 if (session
->snapshot_mode
|| session
->live_timer
||
2768 session
->rotate_timer_period
|| session
->rotate_size
) {
2769 ret
= config_writer_open_element(writer
, config_element_attributes
);
2771 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2775 if (session
->snapshot_mode
) {
2776 ret
= config_writer_write_element_bool(writer
,
2777 config_element_snapshot_mode
, 1);
2779 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2782 } else if (session
->live_timer
) {
2783 ret
= config_writer_write_element_unsigned_int(writer
,
2784 config_element_live_timer_interval
, session
->live_timer
);
2786 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2790 if (session
->rotate_timer_period
|| session
->rotate_size
) {
2791 ret
= save_session_rotation_schedules(writer
,
2794 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2800 ret
= config_writer_close_element(writer
);
2802 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2807 ret
= save_session_output(writer
, session
);
2808 if (ret
!= LTTNG_OK
) {
2813 ret
= config_writer_close_element(writer
);
2815 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2820 ret
= config_writer_close_element(writer
);
2822 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2828 if (writer
&& config_writer_destroy(writer
)) {
2829 /* Preserve the original error code */
2830 ret
= ret
!= LTTNG_OK
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2832 if (ret
!= LTTNG_OK
) {
2833 /* Delete file in case of error */
2834 if ((fd
>= 0) && unlink(config_file_path
)) {
2835 PERROR("Unlinking XML session configuration.");
2842 closeret
= close(fd
);
2844 PERROR("Closing XML session configuration");
2851 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
2852 lttng_sock_cred
*creds
)
2855 const char *session_name
;
2856 struct ltt_session
*session
;
2858 session_lock_list();
2860 session_name
= lttng_save_session_attr_get_session_name(attr
);
2862 session
= session_find_by_name(session_name
);
2864 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2868 session_lock(session
);
2869 ret
= save_session(session
, attr
, creds
);
2870 session_unlock(session
);
2871 session_put(session
);
2872 if (ret
!= LTTNG_OK
) {
2876 struct ltt_session_list
*list
= session_get_list();
2878 cds_list_for_each_entry(session
, &list
->head
, list
) {
2879 if (!session_get(session
)) {
2882 session_lock(session
);
2883 ret
= save_session(session
, attr
, creds
);
2884 session_unlock(session
);
2885 session_put(session
);
2886 /* Don't abort if we don't have the required permissions. */
2887 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_EPERM
) {
2895 session_unlock_list();