2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "lttng-syscall.hpp"
13 #include "session.hpp"
14 #include "trace-ust.hpp"
16 #include <common/config/session-config.hpp>
17 #include <common/defaults.hpp>
18 #include <common/error.hpp>
19 #include <common/runas.hpp>
20 #include <common/urcu.hpp>
21 #include <common/utils.hpp>
23 #include <lttng/save-internal.hpp>
29 #include <urcu/uatomic.h>
31 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
32 static int save_kernel_channel_attributes(struct config_writer
*writer
,
33 struct lttng_channel_attr
*attr
)
37 ret
= config_writer_write_element_string(writer
,
38 config_element_overwrite_mode
,
39 attr
->overwrite
? config_overwrite_mode_overwrite
:
40 config_overwrite_mode_discard
);
42 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
46 ret
= config_writer_write_element_unsigned_int(
47 writer
, config_element_subbuf_size
, attr
->subbuf_size
);
49 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
53 ret
= config_writer_write_element_unsigned_int(
54 writer
, config_element_num_subbuf
, attr
->num_subbuf
);
56 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
60 ret
= config_writer_write_element_unsigned_int(
61 writer
, config_element_switch_timer_interval
, attr
->switch_timer_interval
);
63 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
67 ret
= config_writer_write_element_unsigned_int(
68 writer
, config_element_read_timer_interval
, attr
->read_timer_interval
);
70 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
74 ret
= config_writer_write_element_string(writer
,
75 config_element_output_type
,
76 attr
->output
== LTTNG_EVENT_SPLICE
?
77 config_output_type_splice
:
78 config_output_type_mmap
);
80 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
84 ret
= config_writer_write_element_unsigned_int(
85 writer
, config_element_tracefile_size
, attr
->tracefile_size
);
87 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
91 ret
= config_writer_write_element_unsigned_int(
92 writer
, config_element_tracefile_count
, attr
->tracefile_count
);
94 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
98 ret
= config_writer_write_element_unsigned_int(
99 writer
, config_element_live_timer_interval
, attr
->live_timer_interval
);
101 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
105 if (attr
->extended
.ptr
) {
106 struct lttng_channel_extended
*ext
= nullptr;
108 ext
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
109 ret
= config_writer_write_element_unsigned_int(
110 writer
, config_element_monitor_timer_interval
, ext
->monitor_timer_interval
);
112 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
116 ret
= config_writer_write_element_signed_int(
117 writer
, config_element_blocking_timeout
, ext
->blocking_timeout
);
119 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
129 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
130 static int save_ust_channel_attributes(struct config_writer
*writer
,
131 struct lttng_ust_abi_channel_attr
*attr
)
134 struct ltt_ust_channel
*channel
= nullptr;
136 ret
= config_writer_write_element_string(writer
,
137 config_element_overwrite_mode
,
138 attr
->overwrite
? config_overwrite_mode_overwrite
:
139 config_overwrite_mode_discard
);
141 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
145 ret
= config_writer_write_element_unsigned_int(
146 writer
, config_element_subbuf_size
, attr
->subbuf_size
);
148 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
152 ret
= config_writer_write_element_unsigned_int(
153 writer
, config_element_num_subbuf
, attr
->num_subbuf
);
155 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
159 ret
= config_writer_write_element_unsigned_int(
160 writer
, config_element_switch_timer_interval
, attr
->switch_timer_interval
);
162 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
166 ret
= config_writer_write_element_unsigned_int(
167 writer
, config_element_read_timer_interval
, attr
->read_timer_interval
);
169 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
173 ret
= config_writer_write_element_string(writer
,
174 config_element_output_type
,
175 attr
->output
== LTTNG_UST_ABI_MMAP
?
176 config_output_type_mmap
:
177 config_output_type_splice
);
179 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
183 ret
= config_writer_write_element_signed_int(
184 writer
, config_element_blocking_timeout
, attr
->u
.s
.blocking_timeout
);
186 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
191 * Fetch the monitor timer which is located in the parent of
192 * lttng_ust_channel_attr
194 channel
= lttng::utils::container_of(attr
, <t_ust_channel::attr
);
195 ret
= config_writer_write_element_unsigned_int(
196 writer
, config_element_monitor_timer_interval
, channel
->monitor_timer_interval
);
198 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
208 get_kernel_instrumentation_string(enum lttng_kernel_abi_instrumentation instrumentation
)
210 const char *instrumentation_string
;
212 switch (instrumentation
) {
213 case LTTNG_KERNEL_ABI_ALL
:
214 instrumentation_string
= config_event_type_all
;
216 case LTTNG_KERNEL_ABI_TRACEPOINT
:
217 instrumentation_string
= config_event_type_tracepoint
;
219 case LTTNG_KERNEL_ABI_KPROBE
:
220 instrumentation_string
= config_event_type_probe
;
222 case LTTNG_KERNEL_ABI_UPROBE
:
223 instrumentation_string
= config_event_type_userspace_probe
;
225 case LTTNG_KERNEL_ABI_FUNCTION
:
226 instrumentation_string
= config_event_type_function_entry
;
228 case LTTNG_KERNEL_ABI_KRETPROBE
:
229 instrumentation_string
= config_event_type_function
;
231 case LTTNG_KERNEL_ABI_NOOP
:
232 instrumentation_string
= config_event_type_noop
;
234 case LTTNG_KERNEL_ABI_SYSCALL
:
235 instrumentation_string
= config_event_type_syscall
;
238 instrumentation_string
= nullptr;
241 return instrumentation_string
;
244 static const char *get_kernel_context_type_string(enum lttng_kernel_abi_context_type context_type
)
246 const char *context_type_string
;
248 switch (context_type
) {
249 case LTTNG_KERNEL_ABI_CONTEXT_PID
:
250 context_type_string
= config_event_context_pid
;
252 case LTTNG_KERNEL_ABI_CONTEXT_PROCNAME
:
253 context_type_string
= config_event_context_procname
;
255 case LTTNG_KERNEL_ABI_CONTEXT_PRIO
:
256 context_type_string
= config_event_context_prio
;
258 case LTTNG_KERNEL_ABI_CONTEXT_NICE
:
259 context_type_string
= config_event_context_nice
;
261 case LTTNG_KERNEL_ABI_CONTEXT_VPID
:
262 context_type_string
= config_event_context_vpid
;
264 case LTTNG_KERNEL_ABI_CONTEXT_TID
:
265 context_type_string
= config_event_context_tid
;
267 case LTTNG_KERNEL_ABI_CONTEXT_VTID
:
268 context_type_string
= config_event_context_vtid
;
270 case LTTNG_KERNEL_ABI_CONTEXT_PPID
:
271 context_type_string
= config_event_context_ppid
;
273 case LTTNG_KERNEL_ABI_CONTEXT_VPPID
:
274 context_type_string
= config_event_context_vppid
;
276 case LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME
:
277 context_type_string
= config_event_context_hostname
;
279 case LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE
:
280 context_type_string
= config_event_context_interruptible
;
282 case LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE
:
283 context_type_string
= config_event_context_preemptible
;
285 case LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE
:
286 context_type_string
= config_event_context_need_reschedule
;
288 case LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE
:
289 context_type_string
= config_event_context_migratable
;
291 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER
:
292 context_type_string
= config_event_context_callstack_user
;
294 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL
:
295 context_type_string
= config_event_context_callstack_kernel
;
297 case LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS
:
298 context_type_string
= config_event_context_cgroup_ns
;
300 case LTTNG_KERNEL_ABI_CONTEXT_IPC_NS
:
301 context_type_string
= config_event_context_ipc_ns
;
303 case LTTNG_KERNEL_ABI_CONTEXT_MNT_NS
:
304 context_type_string
= config_event_context_mnt_ns
;
306 case LTTNG_KERNEL_ABI_CONTEXT_NET_NS
:
307 context_type_string
= config_event_context_net_ns
;
309 case LTTNG_KERNEL_ABI_CONTEXT_PID_NS
:
310 context_type_string
= config_event_context_pid_ns
;
312 case LTTNG_KERNEL_ABI_CONTEXT_TIME_NS
:
313 context_type_string
= config_event_context_time_ns
;
315 case LTTNG_KERNEL_ABI_CONTEXT_USER_NS
:
316 context_type_string
= config_event_context_user_ns
;
318 case LTTNG_KERNEL_ABI_CONTEXT_UTS_NS
:
319 context_type_string
= config_event_context_uts_ns
;
321 case LTTNG_KERNEL_ABI_CONTEXT_UID
:
322 context_type_string
= config_event_context_uid
;
324 case LTTNG_KERNEL_ABI_CONTEXT_EUID
:
325 context_type_string
= config_event_context_euid
;
327 case LTTNG_KERNEL_ABI_CONTEXT_SUID
:
328 context_type_string
= config_event_context_suid
;
330 case LTTNG_KERNEL_ABI_CONTEXT_GID
:
331 context_type_string
= config_event_context_gid
;
333 case LTTNG_KERNEL_ABI_CONTEXT_EGID
:
334 context_type_string
= config_event_context_egid
;
336 case LTTNG_KERNEL_ABI_CONTEXT_SGID
:
337 context_type_string
= config_event_context_sgid
;
339 case LTTNG_KERNEL_ABI_CONTEXT_VUID
:
340 context_type_string
= config_event_context_vuid
;
342 case LTTNG_KERNEL_ABI_CONTEXT_VEUID
:
343 context_type_string
= config_event_context_veuid
;
345 case LTTNG_KERNEL_ABI_CONTEXT_VSUID
:
346 context_type_string
= config_event_context_vsuid
;
348 case LTTNG_KERNEL_ABI_CONTEXT_VGID
:
349 context_type_string
= config_event_context_vgid
;
351 case LTTNG_KERNEL_ABI_CONTEXT_VEGID
:
352 context_type_string
= config_event_context_vegid
;
354 case LTTNG_KERNEL_ABI_CONTEXT_VSGID
:
355 context_type_string
= config_event_context_vsgid
;
358 context_type_string
= nullptr;
361 return context_type_string
;
364 static const char *get_ust_context_type_string(enum lttng_ust_abi_context_type context_type
)
366 const char *context_type_string
;
368 switch (context_type
) {
369 case LTTNG_UST_ABI_CONTEXT_PROCNAME
:
370 context_type_string
= config_event_context_procname
;
372 case LTTNG_UST_ABI_CONTEXT_VPID
:
373 context_type_string
= config_event_context_vpid
;
375 case LTTNG_UST_ABI_CONTEXT_VTID
:
376 context_type_string
= config_event_context_vtid
;
378 case LTTNG_UST_ABI_CONTEXT_IP
:
379 context_type_string
= config_event_context_ip
;
381 case LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
:
382 context_type_string
= config_event_context_pthread_id
;
384 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
385 context_type_string
= config_event_context_app
;
387 case LTTNG_UST_ABI_CONTEXT_CGROUP_NS
:
388 context_type_string
= config_event_context_cgroup_ns
;
390 case LTTNG_UST_ABI_CONTEXT_IPC_NS
:
391 context_type_string
= config_event_context_ipc_ns
;
393 case LTTNG_UST_ABI_CONTEXT_MNT_NS
:
394 context_type_string
= config_event_context_mnt_ns
;
396 case LTTNG_UST_ABI_CONTEXT_NET_NS
:
397 context_type_string
= config_event_context_net_ns
;
399 case LTTNG_UST_ABI_CONTEXT_TIME_NS
:
400 context_type_string
= config_event_context_time_ns
;
402 case LTTNG_UST_ABI_CONTEXT_PID_NS
:
403 context_type_string
= config_event_context_pid_ns
;
405 case LTTNG_UST_ABI_CONTEXT_USER_NS
:
406 context_type_string
= config_event_context_user_ns
;
408 case LTTNG_UST_ABI_CONTEXT_UTS_NS
:
409 context_type_string
= config_event_context_uts_ns
;
411 case LTTNG_UST_ABI_CONTEXT_VUID
:
412 context_type_string
= config_event_context_vuid
;
414 case LTTNG_UST_ABI_CONTEXT_VEUID
:
415 context_type_string
= config_event_context_veuid
;
417 case LTTNG_UST_ABI_CONTEXT_VSUID
:
418 context_type_string
= config_event_context_vsuid
;
420 case LTTNG_UST_ABI_CONTEXT_VGID
:
421 context_type_string
= config_event_context_vgid
;
423 case LTTNG_UST_ABI_CONTEXT_VEGID
:
424 context_type_string
= config_event_context_vegid
;
426 case LTTNG_UST_ABI_CONTEXT_VSGID
:
427 context_type_string
= config_event_context_vsgid
;
429 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
431 * Error, should not be stored in the XML, perf contexts
432 * are stored as a node of type event_perf_context_type.
435 context_type_string
= nullptr;
439 return context_type_string
;
442 static const char *get_buffer_type_string(enum lttng_buffer_type buffer_type
)
444 const char *buffer_type_string
;
446 switch (buffer_type
) {
447 case LTTNG_BUFFER_PER_PID
:
448 buffer_type_string
= config_buffer_type_per_pid
;
450 case LTTNG_BUFFER_PER_UID
:
451 buffer_type_string
= config_buffer_type_per_uid
;
453 case LTTNG_BUFFER_GLOBAL
:
454 buffer_type_string
= config_buffer_type_global
;
457 buffer_type_string
= nullptr;
460 return buffer_type_string
;
463 static const char *get_loglevel_type_string(enum lttng_ust_abi_loglevel_type loglevel_type
)
465 const char *loglevel_type_string
;
467 switch (loglevel_type
) {
468 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
469 loglevel_type_string
= config_loglevel_type_all
;
471 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
472 loglevel_type_string
= config_loglevel_type_range
;
474 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
475 loglevel_type_string
= config_loglevel_type_single
;
478 loglevel_type_string
= nullptr;
481 return loglevel_type_string
;
484 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
485 static int save_kernel_function_event(struct config_writer
*writer
, struct ltt_kernel_event
*event
)
489 ret
= config_writer_open_element(writer
, config_element_function_attributes
);
491 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
495 ret
= config_writer_write_element_string(
496 writer
, config_element_name
, event
->event
->u
.ftrace
.symbol_name
);
498 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
502 /* /function attributes */
503 ret
= config_writer_close_element(writer
);
505 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
512 static int save_kernel_kprobe_event(struct config_writer
*writer
, struct ltt_kernel_event
*event
)
515 const char *symbol_name
;
519 switch (event
->event
->instrumentation
) {
520 case LTTNG_KERNEL_ABI_KPROBE
:
522 * Comments in lttng-kernel.h mention that
523 * either addr or symbol_name are set, not both.
525 addr
= event
->event
->u
.kprobe
.addr
;
526 offset
= event
->event
->u
.kprobe
.offset
;
527 symbol_name
= addr
? nullptr : event
->event
->u
.kprobe
.symbol_name
;
529 case LTTNG_KERNEL_ABI_KRETPROBE
:
530 addr
= event
->event
->u
.kretprobe
.addr
;
531 offset
= event
->event
->u
.kretprobe
.offset
;
532 symbol_name
= addr
? nullptr : event
->event
->u
.kretprobe
.symbol_name
;
535 ERR("Unsupported kernel instrumentation type.");
536 ret
= LTTNG_ERR_INVALID
;
540 ret
= config_writer_open_element(writer
, config_element_probe_attributes
);
542 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
547 ret
= config_writer_write_element_unsigned_int(
548 writer
, config_element_address
, addr
);
550 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
553 } else if (symbol_name
) {
554 ret
= config_writer_write_element_string(
555 writer
, config_element_symbol_name
, symbol_name
);
557 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
560 /* If the offset is non-zero, write it.*/
562 ret
= config_writer_write_element_unsigned_int(
563 writer
, config_element_offset
, offset
);
565 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
571 * This really should not happen as we are either setting the
572 * address or the symbol above.
574 ERR("Invalid probe/function description.");
575 ret
= LTTNG_ERR_INVALID
;
579 ret
= config_writer_close_element(writer
);
581 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
589 * Save the userspace probe tracepoint event associated with the event to the
592 static int save_kernel_userspace_probe_tracepoint_event(struct config_writer
*writer
,
593 struct ltt_kernel_event
*event
)
596 const char *probe_name
, *provider_name
, *binary_path
;
597 const struct lttng_userspace_probe_location
*userspace_probe_location
;
598 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
599 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
601 /* Get userspace probe location from the event. */
602 userspace_probe_location
= event
->userspace_probe_location
;
603 if (!userspace_probe_location
) {
604 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
608 /* Get lookup method and lookup method type. */
609 lookup_method
= lttng_userspace_probe_location_get_lookup_method(userspace_probe_location
);
610 if (!lookup_method
) {
611 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
615 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
617 /* Get the binary path, probe name and provider name. */
619 lttng_userspace_probe_location_tracepoint_get_binary_path(userspace_probe_location
);
621 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
626 lttng_userspace_probe_location_tracepoint_get_probe_name(userspace_probe_location
);
628 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
632 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
633 userspace_probe_location
);
634 if (!provider_name
) {
635 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
639 /* Open a userspace probe tracepoint attribute. */
640 ret
= config_writer_open_element(writer
,
641 config_element_userspace_probe_tracepoint_attributes
);
643 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
647 switch (lookup_type
) {
648 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
649 ret
= config_writer_write_element_string(
651 config_element_userspace_probe_lookup
,
652 config_element_userspace_probe_lookup_tracepoint_sdt
);
654 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
659 ERR("Unsupported kernel userspace probe tracepoint lookup method.");
660 ret
= LTTNG_ERR_INVALID
;
664 /* Write the binary path, provider name and the probe name. */
665 ret
= config_writer_write_element_string(
666 writer
, config_element_userspace_probe_location_binary_path
, binary_path
);
668 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
672 ret
= config_writer_write_element_string(
674 config_element_userspace_probe_tracepoint_location_provider_name
,
677 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
681 ret
= config_writer_write_element_string(
682 writer
, config_element_userspace_probe_tracepoint_location_probe_name
, probe_name
);
684 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
688 /* Close the userspace probe tracepoint attribute. */
689 ret
= config_writer_close_element(writer
);
691 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
700 * Save the userspace probe function event associated with the event to the
703 static int save_kernel_userspace_probe_function_event(struct config_writer
*writer
,
704 struct ltt_kernel_event
*event
)
707 const char *function_name
, *binary_path
;
708 const struct lttng_userspace_probe_location
*userspace_probe_location
;
709 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
710 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
712 /* Get userspace probe location from the event. */
713 userspace_probe_location
= event
->userspace_probe_location
;
714 if (!userspace_probe_location
) {
715 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
719 /* Get lookup method and lookup method type. */
720 lookup_method
= lttng_userspace_probe_location_get_lookup_method(userspace_probe_location
);
721 if (!lookup_method
) {
722 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
726 /* Get the binary path and the function name. */
728 lttng_userspace_probe_location_function_get_binary_path(userspace_probe_location
);
730 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
735 lttng_userspace_probe_location_function_get_function_name(userspace_probe_location
);
736 if (!function_name
) {
737 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
741 /* Open a userspace probe function attribute. */
742 ret
= config_writer_open_element(writer
,
743 config_element_userspace_probe_function_attributes
);
745 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
749 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
750 switch (lookup_type
) {
751 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
752 ret
= config_writer_write_element_string(
754 config_element_userspace_probe_lookup
,
755 config_element_userspace_probe_lookup_function_elf
);
757 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
761 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
762 ret
= config_writer_write_element_string(
764 config_element_userspace_probe_lookup
,
765 config_element_userspace_probe_lookup_function_default
);
767 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
772 ERR("Unsupported kernel userspace probe function lookup method.");
773 ret
= LTTNG_ERR_INVALID
;
777 /* Write the binary path and the function name. */
778 ret
= config_writer_write_element_string(
779 writer
, config_element_userspace_probe_location_binary_path
, binary_path
);
781 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
785 ret
= config_writer_write_element_string(
787 config_element_userspace_probe_function_location_function_name
,
790 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
794 /* Close the userspace probe function attribute. */
795 ret
= config_writer_close_element(writer
);
797 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
805 static int save_kernel_userspace_probe_event(struct config_writer
*writer
,
806 struct ltt_kernel_event
*event
)
809 struct lttng_userspace_probe_location
*userspace_probe_location
;
811 /* Get userspace probe location from the event. */
812 userspace_probe_location
= event
->userspace_probe_location
;
813 if (!userspace_probe_location
) {
814 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
818 switch (lttng_userspace_probe_location_get_type(userspace_probe_location
)) {
819 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
821 ret
= save_kernel_userspace_probe_function_event(writer
, event
);
823 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
828 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
830 ret
= save_kernel_userspace_probe_tracepoint_event(writer
, event
);
832 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
837 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
:
839 ERR("Unsupported kernel userspace probe location type.");
840 ret
= LTTNG_ERR_INVALID
;
848 static int save_kernel_event(struct config_writer
*writer
, struct ltt_kernel_event
*event
)
851 const char *instrumentation_type
;
853 ret
= config_writer_open_element(writer
, config_element_event
);
855 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
859 if (event
->event
->name
[0]) {
860 ret
= config_writer_write_element_string(
861 writer
, config_element_name
, event
->event
->name
);
863 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
868 ret
= config_writer_write_element_bool(writer
, config_element_enabled
, event
->enabled
);
870 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
874 instrumentation_type
= get_kernel_instrumentation_string(event
->event
->instrumentation
);
875 if (!instrumentation_type
) {
876 ret
= LTTNG_ERR_INVALID
;
880 ret
= config_writer_write_element_string(writer
, config_element_type
, instrumentation_type
);
882 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
886 if (event
->filter_expression
) {
887 ret
= config_writer_write_element_string(
888 writer
, config_element_filter
, event
->filter_expression
);
890 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
895 if (event
->event
->instrumentation
== LTTNG_KERNEL_ABI_FUNCTION
||
896 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_KPROBE
||
897 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_UPROBE
||
898 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_KRETPROBE
) {
899 ret
= config_writer_open_element(writer
, config_element_attributes
);
901 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
905 switch (event
->event
->instrumentation
) {
906 case LTTNG_KERNEL_ABI_SYSCALL
:
907 case LTTNG_KERNEL_ABI_FUNCTION
:
908 ret
= save_kernel_function_event(writer
, event
);
913 case LTTNG_KERNEL_ABI_KPROBE
:
914 case LTTNG_KERNEL_ABI_KRETPROBE
:
915 ret
= save_kernel_kprobe_event(writer
, event
);
920 case LTTNG_KERNEL_ABI_UPROBE
:
921 ret
= save_kernel_userspace_probe_event(writer
, event
);
927 ERR("Unsupported kernel instrumentation type.");
928 ret
= LTTNG_ERR_INVALID
;
933 ret
= config_writer_close_element(writer
);
935 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
941 ret
= config_writer_close_element(writer
);
943 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
952 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
953 static int save_kernel_events(struct config_writer
*writer
, struct ltt_kernel_channel
*kchan
)
956 struct ltt_kernel_event
*event
;
958 ret
= config_writer_open_element(writer
, config_element_events
);
960 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
964 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
965 ret
= save_kernel_event(writer
, event
);
966 if (ret
!= LTTNG_OK
) {
972 ret
= config_writer_close_element(writer
);
974 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
983 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
984 static int save_ust_event(struct config_writer
*writer
, struct ltt_ust_event
*event
)
987 const char *loglevel_type_string
;
989 ret
= config_writer_open_element(writer
, config_element_event
);
991 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
995 if (event
->attr
.name
[0]) {
996 ret
= config_writer_write_element_string(
997 writer
, config_element_name
, event
->attr
.name
);
999 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1004 ret
= config_writer_write_element_bool(writer
, config_element_enabled
, event
->enabled
);
1006 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1010 if (event
->attr
.instrumentation
!= LTTNG_UST_ABI_TRACEPOINT
) {
1011 ERR("Unsupported UST instrumentation type.");
1012 ret
= LTTNG_ERR_INVALID
;
1015 ret
= config_writer_write_element_string(
1016 writer
, config_element_type
, config_event_type_tracepoint
);
1018 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1022 loglevel_type_string
=
1023 get_loglevel_type_string((lttng_ust_abi_loglevel_type
) event
->attr
.loglevel_type
);
1024 if (!loglevel_type_string
) {
1025 ERR("Unsupported UST loglevel type.");
1026 ret
= LTTNG_ERR_INVALID
;
1030 ret
= config_writer_write_element_string(
1031 writer
, config_element_loglevel_type
, loglevel_type_string
);
1033 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1037 /* The log level is irrelevant if no "filtering" is enabled */
1038 if (event
->attr
.loglevel_type
!= LTTNG_UST_ABI_LOGLEVEL_ALL
) {
1039 ret
= config_writer_write_element_signed_int(
1040 writer
, config_element_loglevel
, event
->attr
.loglevel
);
1042 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1047 if (event
->filter_expression
) {
1048 ret
= config_writer_write_element_string(
1049 writer
, config_element_filter
, event
->filter_expression
);
1051 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1056 if (event
->exclusion
&& event
->exclusion
->count
) {
1059 ret
= config_writer_open_element(writer
, config_element_exclusions
);
1061 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1065 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
1066 ret
= config_writer_write_element_string(
1068 config_element_exclusion
,
1069 LTTNG_EVENT_EXCLUSION_NAME_AT(event
->exclusion
, i
));
1071 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1077 ret
= config_writer_close_element(writer
);
1079 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1085 ret
= config_writer_close_element(writer
);
1087 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1096 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1097 static int save_ust_events(struct config_writer
*writer
, struct lttng_ht
*events
)
1100 struct ltt_ust_event
*event
;
1101 struct lttng_ht_node_str
*node
;
1102 struct lttng_ht_iter iter
;
1104 ret
= config_writer_open_element(writer
, config_element_events
);
1106 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1111 lttng::urcu::read_lock_guard read_lock
;
1113 cds_lfht_for_each_entry (events
->ht
, &iter
.iter
, node
, node
) {
1114 event
= lttng::utils::container_of(node
, <t_ust_event::node
);
1116 if (event
->internal
) {
1117 /* Internal events must not be exposed to clients */
1120 ret
= save_ust_event(writer
, event
);
1121 if (ret
!= LTTNG_OK
) {
1128 ret
= config_writer_close_element(writer
);
1130 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1139 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1140 static int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
1141 struct agent_event
*agent_event
)
1144 enum lttng_ust_abi_loglevel_type ust_loglevel_type
;
1146 ust_event
->enabled
= AGENT_EVENT_IS_ENABLED(agent_event
);
1147 ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
1148 if (lttng_strncpy(ust_event
->attr
.name
, agent_event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
1149 ret
= LTTNG_ERR_INVALID
;
1152 switch (agent_event
->loglevel_type
) {
1153 case LTTNG_EVENT_LOGLEVEL_ALL
:
1154 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
1156 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1157 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
1159 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1160 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
1163 ERR("Invalid agent_event loglevel_type.");
1164 ret
= LTTNG_ERR_INVALID
;
1168 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
1169 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
1170 ust_event
->filter_expression
= agent_event
->filter_expression
;
1171 ust_event
->exclusion
= agent_event
->exclusion
;
1178 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1179 static int save_agent_events(struct config_writer
*writer
, struct agent
*agent
)
1182 struct lttng_ht_iter iter
;
1183 struct lttng_ht_node_str
*node
;
1185 ret
= config_writer_open_element(writer
, config_element_events
);
1187 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1192 lttng::urcu::read_lock_guard read_lock
;
1194 cds_lfht_for_each_entry (agent
->events
->ht
, &iter
.iter
, node
, node
) {
1195 struct agent_event
*agent_event
;
1196 struct ltt_ust_event fake_event
;
1198 memset(&fake_event
, 0, sizeof(fake_event
));
1199 agent_event
= lttng::utils::container_of(node
, &agent_event::node
);
1202 * Initialize a fake ust event to reuse the same serialization
1203 * function since UST and agent events contain the same info
1204 * (and one could wonder why they don't reuse the same
1207 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
1208 if (ret
!= LTTNG_OK
) {
1211 ret
= save_ust_event(writer
, &fake_event
);
1212 if (ret
!= LTTNG_OK
) {
1219 ret
= config_writer_close_element(writer
);
1221 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1230 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1231 static int save_kernel_context(struct config_writer
*writer
, struct lttng_kernel_abi_context
*ctx
)
1239 ret
= config_writer_open_element(writer
, config_element_context
);
1241 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1245 if (ctx
->ctx
== LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER
) {
1246 ret
= config_writer_open_element(writer
, config_element_context_perf
);
1248 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1252 ret
= config_writer_write_element_unsigned_int(
1253 writer
, config_element_type
, ctx
->u
.perf_counter
.type
);
1255 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1259 ret
= config_writer_write_element_unsigned_int(
1260 writer
, config_element_config
, ctx
->u
.perf_counter
.config
);
1262 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1266 ret
= config_writer_write_element_string(
1267 writer
, config_element_name
, ctx
->u
.perf_counter
.name
);
1269 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1274 ret
= config_writer_close_element(writer
);
1276 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1280 const char *context_type_string
= get_kernel_context_type_string(ctx
->ctx
);
1282 if (!context_type_string
) {
1283 ERR("Unsupported kernel context type.");
1284 ret
= LTTNG_ERR_INVALID
;
1288 ret
= config_writer_write_element_string(
1289 writer
, config_element_type
, context_type_string
);
1291 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1297 ret
= config_writer_close_element(writer
);
1299 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1308 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1309 static int save_kernel_contexts(struct config_writer
*writer
, struct ltt_kernel_channel
*kchan
)
1312 struct ltt_kernel_context
*ctx
;
1314 if (cds_list_empty(&kchan
->ctx_list
)) {
1319 ret
= config_writer_open_element(writer
, config_element_contexts
);
1321 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1325 cds_list_for_each_entry (ctx
, &kchan
->ctx_list
, list
) {
1326 ret
= save_kernel_context(writer
, &ctx
->ctx
);
1327 if (ret
!= LTTNG_OK
) {
1333 ret
= config_writer_close_element(writer
);
1335 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1344 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1345 static int save_ust_context_perf_thread_counter(struct config_writer
*writer
,
1346 struct ltt_ust_context
*ctx
)
1350 LTTNG_ASSERT(writer
);
1353 /* Perf contexts are saved as event_perf_context_type */
1354 ret
= config_writer_open_element(writer
, config_element_context_perf
);
1356 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1360 ret
= config_writer_write_element_unsigned_int(
1361 writer
, config_element_type
, ctx
->ctx
.u
.perf_counter
.type
);
1363 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1367 ret
= config_writer_write_element_unsigned_int(
1368 writer
, config_element_config
, ctx
->ctx
.u
.perf_counter
.config
);
1370 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1374 ret
= config_writer_write_element_string(
1375 writer
, config_element_name
, ctx
->ctx
.u
.perf_counter
.name
);
1377 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1382 ret
= config_writer_close_element(writer
);
1384 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1393 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1394 static int save_ust_context_app_ctx(struct config_writer
*writer
, struct ltt_ust_context
*ctx
)
1398 LTTNG_ASSERT(writer
);
1401 /* Application contexts are saved as application_context_type */
1402 ret
= config_writer_open_element(writer
, config_element_context_app
);
1404 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1408 ret
= config_writer_write_element_string(
1409 writer
, config_element_context_app_provider_name
, ctx
->ctx
.u
.app_ctx
.provider_name
);
1411 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1415 ret
= config_writer_write_element_string(
1416 writer
, config_element_context_app_ctx_name
, ctx
->ctx
.u
.app_ctx
.ctx_name
);
1418 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1423 ret
= config_writer_close_element(writer
);
1425 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1434 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1435 static int save_ust_context_generic(struct config_writer
*writer
, struct ltt_ust_context
*ctx
)
1438 const char *context_type_string
;
1440 LTTNG_ASSERT(writer
);
1443 /* Save context as event_context_type_type */
1444 context_type_string
= get_ust_context_type_string(ctx
->ctx
.ctx
);
1445 if (!context_type_string
) {
1446 ERR("Unsupported UST context type.");
1447 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1451 ret
= config_writer_write_element_string(writer
, config_element_type
, context_type_string
);
1453 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1462 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1463 static int save_ust_context(struct config_writer
*writer
, struct cds_list_head
*ctx_list
)
1466 struct ltt_ust_context
*ctx
;
1468 LTTNG_ASSERT(writer
);
1469 LTTNG_ASSERT(ctx_list
);
1471 ret
= config_writer_open_element(writer
, config_element_contexts
);
1473 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1477 cds_list_for_each_entry (ctx
, ctx_list
, list
) {
1478 ret
= config_writer_open_element(writer
, config_element_context
);
1480 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1484 switch (ctx
->ctx
.ctx
) {
1485 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
1486 ret
= save_ust_context_perf_thread_counter(writer
, ctx
);
1488 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1489 ret
= save_ust_context_app_ctx(writer
, ctx
);
1492 /* Save generic context. */
1493 ret
= save_ust_context_generic(writer
, ctx
);
1495 if (ret
!= LTTNG_OK
) {
1500 ret
= config_writer_close_element(writer
);
1502 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1508 ret
= config_writer_close_element(writer
);
1510 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1519 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1520 static int save_kernel_channel(struct config_writer
*writer
, struct ltt_kernel_channel
*kchan
)
1524 LTTNG_ASSERT(writer
);
1525 LTTNG_ASSERT(kchan
);
1527 ret
= config_writer_open_element(writer
, config_element_channel
);
1529 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1533 ret
= config_writer_write_element_string(writer
, config_element_name
, kchan
->channel
->name
);
1535 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1539 ret
= config_writer_write_element_bool(
1540 writer
, config_element_enabled
, kchan
->channel
->enabled
);
1542 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1546 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1547 if (ret
!= LTTNG_OK
) {
1551 ret
= save_kernel_events(writer
, kchan
);
1552 if (ret
!= LTTNG_OK
) {
1556 ret
= save_kernel_contexts(writer
, kchan
);
1557 if (ret
!= LTTNG_OK
) {
1562 ret
= config_writer_close_element(writer
);
1564 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1573 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1574 static int save_ust_channel(struct config_writer
*writer
,
1575 struct ltt_ust_channel
*ust_chan
,
1576 struct ltt_ust_session
*session
)
1580 LTTNG_ASSERT(writer
);
1581 LTTNG_ASSERT(ust_chan
);
1582 LTTNG_ASSERT(session
);
1584 ret
= config_writer_open_element(writer
, config_element_channel
);
1586 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1590 ret
= config_writer_write_element_string(writer
, config_element_name
, ust_chan
->name
);
1592 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1596 ret
= config_writer_write_element_bool(writer
, config_element_enabled
, ust_chan
->enabled
);
1598 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1602 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1603 if (ret
!= LTTNG_OK
) {
1607 ret
= config_writer_write_element_unsigned_int(
1608 writer
, config_element_tracefile_size
, ust_chan
->tracefile_size
);
1610 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1614 ret
= config_writer_write_element_unsigned_int(
1615 writer
, config_element_tracefile_count
, ust_chan
->tracefile_count
);
1617 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1621 ret
= config_writer_write_element_unsigned_int(
1622 writer
, config_element_live_timer_interval
, session
->live_timer_interval
);
1624 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1628 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1629 ret
= save_ust_events(writer
, ust_chan
->events
);
1630 if (ret
!= LTTNG_OK
) {
1634 struct agent
*agent
= nullptr;
1636 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1638 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1639 ERR("Could not find agent associated to UST subdomain");
1644 * Channels associated with a UST sub-domain (such as JUL, Log4j
1645 * or Python) don't have any non-internal events. We retrieve
1646 * the "agent" events associated with this channel and serialize
1649 ret
= save_agent_events(writer
, agent
);
1650 if (ret
!= LTTNG_OK
) {
1655 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1656 if (ret
!= LTTNG_OK
) {
1661 ret
= config_writer_close_element(writer
);
1663 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1672 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1673 static int save_kernel_session(struct config_writer
*writer
, const ltt_session::locked_ref
& session
)
1676 struct ltt_kernel_channel
*kchan
;
1678 LTTNG_ASSERT(writer
);
1680 ret
= config_writer_write_element_string(
1681 writer
, config_element_type
, config_domain_type_kernel
);
1683 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1687 ret
= config_writer_write_element_string(
1688 writer
, config_element_buffer_type
, config_buffer_type_global
);
1690 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1694 ret
= config_writer_open_element(writer
, config_element_channels
);
1696 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1700 cds_list_for_each_entry (kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1701 ret
= save_kernel_channel(writer
, kchan
);
1702 if (ret
!= LTTNG_OK
) {
1708 ret
= config_writer_close_element(writer
);
1710 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1719 static const char *get_config_domain_str(enum lttng_domain_type domain
)
1721 const char *str_dom
;
1724 case LTTNG_DOMAIN_KERNEL
:
1725 str_dom
= config_domain_type_kernel
;
1727 case LTTNG_DOMAIN_UST
:
1728 str_dom
= config_domain_type_ust
;
1730 case LTTNG_DOMAIN_JUL
:
1731 str_dom
= config_domain_type_jul
;
1733 case LTTNG_DOMAIN_LOG4J
:
1734 str_dom
= config_domain_type_log4j
;
1736 case LTTNG_DOMAIN_PYTHON
:
1737 str_dom
= config_domain_type_python
;
1746 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1747 static int save_process_attr_tracker(struct config_writer
*writer
,
1748 const ltt_session::locked_ref
& session
,
1750 enum lttng_process_attr process_attr
)
1753 const char *element_id_tracker
, *element_target_id
, *element_id
;
1754 const struct process_attr_tracker
*tracker
;
1755 enum lttng_tracking_policy tracking_policy
;
1756 struct lttng_process_attr_values
*values
= nullptr;
1758 switch (process_attr
) {
1759 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1760 element_id_tracker
= config_element_process_attr_tracker_pid
;
1761 element_target_id
= config_element_process_attr_pid_value
;
1762 element_id
= config_element_process_attr_id
;
1764 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1765 element_id_tracker
= config_element_process_attr_tracker_vpid
;
1766 element_target_id
= config_element_process_attr_vpid_value
;
1767 element_id
= config_element_process_attr_id
;
1769 case LTTNG_PROCESS_ATTR_USER_ID
:
1770 element_id_tracker
= config_element_process_attr_tracker_uid
;
1771 element_target_id
= config_element_process_attr_uid_value
;
1772 element_id
= config_element_process_attr_id
;
1774 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1775 element_id_tracker
= config_element_process_attr_tracker_vuid
;
1776 element_target_id
= config_element_process_attr_vuid_value
;
1777 element_id
= config_element_process_attr_id
;
1779 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1780 element_id_tracker
= config_element_process_attr_tracker_gid
;
1781 element_target_id
= config_element_process_attr_gid_value
;
1782 element_id
= config_element_process_attr_id
;
1784 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1785 element_id_tracker
= config_element_process_attr_tracker_vgid
;
1786 element_target_id
= config_element_process_attr_vgid_value
;
1787 element_id
= config_element_process_attr_id
;
1790 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1795 case LTTNG_DOMAIN_KERNEL
:
1797 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1798 LTTNG_ASSERT(tracker
);
1801 case LTTNG_DOMAIN_UST
:
1803 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1804 LTTNG_ASSERT(tracker
);
1807 case LTTNG_DOMAIN_JUL
:
1808 case LTTNG_DOMAIN_LOG4J
:
1809 case LTTNG_DOMAIN_PYTHON
:
1811 ret
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1815 tracking_policy
= process_attr_tracker_get_tracking_policy(tracker
);
1816 if (tracking_policy
== LTTNG_TRACKING_POLICY_INCLUDE_ALL
) {
1817 /* Tracking all, nothing to output. */
1822 ret
= config_writer_open_element(writer
, element_id_tracker
);
1824 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1828 ret
= config_writer_open_element(writer
, config_element_process_attr_values
);
1830 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1834 if (tracking_policy
== LTTNG_TRACKING_POLICY_INCLUDE_SET
) {
1835 unsigned int i
, count
;
1836 enum process_attr_tracker_status status
=
1837 process_attr_tracker_get_inclusion_set(tracker
, &values
);
1839 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1840 ret
= LTTNG_ERR_NOMEM
;
1844 count
= _lttng_process_attr_values_get_count(values
);
1846 for (i
= 0; i
< count
; i
++) {
1847 unsigned int integral_value
= UINT_MAX
;
1848 const char *name
= nullptr;
1849 const struct process_attr_value
*value
=
1850 lttng_process_attr_tracker_values_get_at_index(values
, i
);
1852 LTTNG_ASSERT(value
);
1853 ret
= config_writer_open_element(writer
, element_target_id
);
1855 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1859 switch (value
->type
) {
1860 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
1861 integral_value
= (unsigned int) value
->value
.pid
;
1863 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
1864 integral_value
= (unsigned int) value
->value
.uid
;
1866 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
1867 integral_value
= (unsigned int) value
->value
.gid
;
1869 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
1870 name
= value
->value
.user_name
;
1873 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
1874 name
= value
->value
.group_name
;
1882 ret
= config_writer_write_element_string(
1883 writer
, config_element_name
, name
);
1885 ret
= config_writer_write_element_unsigned_int(
1886 writer
, element_id
, integral_value
);
1890 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1894 /* /$element_target_id */
1895 ret
= config_writer_close_element(writer
);
1897 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1904 ret
= config_writer_close_element(writer
);
1906 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1910 /* /$element_id_tracker */
1911 ret
= config_writer_close_element(writer
);
1913 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1919 lttng_process_attr_values_destroy(values
);
1923 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1924 static int save_process_attr_trackers(struct config_writer
*writer
,
1925 const ltt_session::locked_ref
& session
,
1931 case LTTNG_DOMAIN_KERNEL
:
1932 ret
= save_process_attr_tracker(
1933 writer
, session
, domain
, LTTNG_PROCESS_ATTR_PROCESS_ID
);
1934 if (ret
!= LTTNG_OK
) {
1937 ret
= save_process_attr_tracker(
1938 writer
, session
, domain
, LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
);
1939 if (ret
!= LTTNG_OK
) {
1942 ret
= save_process_attr_tracker(
1943 writer
, session
, domain
, LTTNG_PROCESS_ATTR_USER_ID
);
1944 if (ret
!= LTTNG_OK
) {
1947 ret
= save_process_attr_tracker(
1948 writer
, session
, domain
, LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
);
1949 if (ret
!= LTTNG_OK
) {
1952 ret
= save_process_attr_tracker(
1953 writer
, session
, domain
, LTTNG_PROCESS_ATTR_GROUP_ID
);
1954 if (ret
!= LTTNG_OK
) {
1957 ret
= save_process_attr_tracker(
1958 writer
, session
, domain
, LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
);
1959 if (ret
!= LTTNG_OK
) {
1963 case LTTNG_DOMAIN_UST
:
1964 ret
= save_process_attr_tracker(
1965 writer
, session
, domain
, LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
);
1966 if (ret
!= LTTNG_OK
) {
1969 ret
= save_process_attr_tracker(
1970 writer
, session
, domain
, LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
);
1971 if (ret
!= LTTNG_OK
) {
1974 ret
= save_process_attr_tracker(
1975 writer
, session
, domain
, LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
);
1976 if (ret
!= LTTNG_OK
) {
1981 ret
= LTTNG_ERR_INVALID
;
1989 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1990 static int save_ust_domain(struct config_writer
*writer
,
1991 const ltt_session::locked_ref
& session
,
1992 enum lttng_domain_type domain
)
1995 struct ltt_ust_channel
*ust_chan
;
1996 const char *buffer_type_string
;
1997 struct lttng_ht_node_str
*node
;
1998 struct lttng_ht_iter iter
;
1999 const char *config_domain_name
;
2001 LTTNG_ASSERT(writer
);
2003 ret
= config_writer_open_element(writer
, config_element_domain
);
2005 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2009 config_domain_name
= get_config_domain_str(domain
);
2010 if (!config_domain_name
) {
2011 ret
= LTTNG_ERR_INVALID
;
2015 ret
= config_writer_write_element_string(writer
, config_element_type
, config_domain_name
);
2017 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2021 buffer_type_string
= get_buffer_type_string(session
->ust_session
->buffer_type
);
2022 if (!buffer_type_string
) {
2023 ERR("Unsupported buffer type.");
2024 ret
= LTTNG_ERR_INVALID
;
2028 ret
= config_writer_write_element_string(
2029 writer
, config_element_buffer_type
, buffer_type_string
);
2031 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2035 ret
= config_writer_open_element(writer
, config_element_channels
);
2037 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2042 lttng::urcu::read_lock_guard read_lock
;
2044 cds_lfht_for_each_entry (
2045 session
->ust_session
->domain_global
.channels
->ht
, &iter
.iter
, node
, node
) {
2046 ust_chan
= lttng::utils::container_of(node
, <t_ust_channel::node
);
2047 if (domain
== ust_chan
->domain
) {
2048 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
2049 if (ret
!= LTTNG_OK
) {
2057 ret
= config_writer_close_element(writer
);
2059 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2063 if (domain
== LTTNG_DOMAIN_UST
) {
2064 ret
= config_writer_open_element(writer
, config_element_process_attr_trackers
);
2066 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2070 ret
= save_process_attr_trackers(writer
, session
, LTTNG_DOMAIN_UST
);
2071 if (ret
!= LTTNG_OK
) {
2076 ret
= config_writer_close_element(writer
);
2078 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2084 ret
= config_writer_close_element(writer
);
2086 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2095 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2096 static int save_domains(struct config_writer
*writer
, const ltt_session::locked_ref
& session
)
2100 LTTNG_ASSERT(writer
);
2102 if (!session
->kernel_session
&& !session
->ust_session
) {
2106 ret
= config_writer_open_element(writer
, config_element_domains
);
2108 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2112 if (session
->kernel_session
) {
2113 ret
= config_writer_open_element(writer
, config_element_domain
);
2115 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2119 ret
= save_kernel_session(writer
, session
);
2120 if (ret
!= LTTNG_OK
) {
2124 ret
= config_writer_open_element(writer
, config_element_process_attr_trackers
);
2126 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2130 ret
= save_process_attr_trackers(writer
, session
, LTTNG_DOMAIN_KERNEL
);
2131 if (ret
!= LTTNG_OK
) {
2136 ret
= config_writer_close_element(writer
);
2138 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2142 ret
= config_writer_close_element(writer
);
2144 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2149 if (session
->ust_session
) {
2150 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
2151 if (ret
!= LTTNG_OK
) {
2155 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
2156 if (ret
!= LTTNG_OK
) {
2160 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
2161 if (ret
!= LTTNG_OK
) {
2165 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
2166 if (ret
!= LTTNG_OK
) {
2172 ret
= config_writer_close_element(writer
);
2174 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2183 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2184 static int save_consumer_output(struct config_writer
*writer
, struct consumer_output
*output
)
2188 LTTNG_ASSERT(writer
);
2189 LTTNG_ASSERT(output
);
2191 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
2193 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2197 ret
= config_writer_write_element_bool(writer
, config_element_enabled
, output
->enabled
);
2199 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2203 ret
= config_writer_open_element(writer
, config_element_destination
);
2205 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2209 switch (output
->type
) {
2210 case CONSUMER_DST_LOCAL
:
2211 ret
= config_writer_write_element_string(
2212 writer
, config_element_path
, output
->dst
.session_root_path
);
2214 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2218 case CONSUMER_DST_NET
:
2222 uri
= calloc
<char>(PATH_MAX
);
2224 ret
= LTTNG_ERR_NOMEM
;
2228 ret
= config_writer_open_element(writer
, config_element_net_output
);
2230 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2231 goto end_net_output
;
2234 if (output
->dst
.net
.control_isset
&& output
->dst
.net
.data_isset
) {
2235 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
2237 ret
= LTTNG_ERR_INVALID
;
2238 goto end_net_output
;
2241 ret
= config_writer_write_element_string(
2242 writer
, config_element_control_uri
, uri
);
2244 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2245 goto end_net_output
;
2248 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
2250 ret
= LTTNG_ERR_INVALID
;
2251 goto end_net_output
;
2254 ret
= config_writer_write_element_string(
2255 writer
, config_element_data_uri
, uri
);
2257 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2258 goto end_net_output
;
2263 if (ret
!= LTTNG_OK
) {
2267 ret
= !output
->dst
.net
.control_isset
? LTTNG_ERR_URL_CTRL_MISS
:
2268 LTTNG_ERR_URL_DATA_MISS
;
2273 ret
= config_writer_close_element(writer
);
2275 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2281 ERR("Unsupported consumer output type.");
2282 ret
= LTTNG_ERR_INVALID
;
2287 ret
= config_writer_close_element(writer
);
2289 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2293 /* /consumer_output */
2294 ret
= config_writer_close_element(writer
);
2296 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2305 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2306 static int save_snapshot_outputs(struct config_writer
*writer
, struct snapshot
*snapshot
)
2309 struct lttng_ht_iter iter
;
2310 struct snapshot_output
*output
;
2312 LTTNG_ASSERT(writer
);
2313 LTTNG_ASSERT(snapshot
);
2315 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
2317 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2322 lttng::urcu::read_lock_guard read_lock
;
2324 cds_lfht_for_each_entry (snapshot
->output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
2325 ret
= config_writer_open_element(writer
, config_element_output
);
2327 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2331 ret
= config_writer_write_element_string(
2332 writer
, config_element_name
, output
->name
);
2334 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2338 ret
= config_writer_write_element_unsigned_int(
2339 writer
, config_element_max_size
, output
->max_size
);
2341 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2345 ret
= save_consumer_output(writer
, output
->consumer
);
2346 if (ret
!= LTTNG_OK
) {
2351 ret
= config_writer_close_element(writer
);
2353 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2359 /* /snapshot_outputs */
2360 ret
= config_writer_close_element(writer
);
2362 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2373 /* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2374 static int save_session_output(struct config_writer
*writer
, const ltt_session::locked_ref
& session
)
2378 LTTNG_ASSERT(writer
);
2380 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
2381 (!session
->snapshot_mode
&& !session
->consumer
)) {
2382 /* Session is in no output mode */
2387 ret
= config_writer_open_element(writer
, config_element_output
);
2389 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2393 if (session
->snapshot_mode
) {
2394 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
2395 if (ret
!= LTTNG_OK
) {
2399 if (session
->consumer
) {
2400 ret
= save_consumer_output(writer
, session
->consumer
);
2401 if (ret
!= LTTNG_OK
) {
2408 ret
= config_writer_close_element(writer
);
2410 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2418 static int save_session_rotation_schedule(struct config_writer
*writer
,
2419 enum lttng_rotation_schedule_type type
,
2423 const char *element_name
;
2424 const char *value_name
;
2427 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
2428 element_name
= config_element_rotation_schedule_periodic
;
2429 value_name
= config_element_rotation_schedule_periodic_time_us
;
2431 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
2432 element_name
= config_element_rotation_schedule_size_threshold
;
2433 value_name
= config_element_rotation_schedule_size_threshold_bytes
;
2440 ret
= config_writer_open_element(writer
, element_name
);
2445 ret
= config_writer_write_element_unsigned_int(writer
, value_name
, value
);
2450 /* Close schedule descriptor element. */
2451 ret
= config_writer_close_element(writer
);
2459 static int save_session_rotation_schedules(struct config_writer
*writer
,
2460 const ltt_session::locked_ref
& session
)
2464 ret
= config_writer_open_element(writer
, config_element_rotation_schedules
);
2468 if (session
->rotate_timer_period
) {
2469 ret
= save_session_rotation_schedule(writer
,
2470 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
,
2471 session
->rotate_timer_period
);
2473 goto close_schedules
;
2476 if (session
->rotate_size
) {
2477 ret
= save_session_rotation_schedule(
2478 writer
, LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
, session
->rotate_size
);
2480 goto close_schedules
;
2485 /* Close rotation schedules element. */
2486 ret
= config_writer_close_element(writer
);
2495 * Save the given session.
2497 * Return LTTNG_OK on success else a LTTNG_ERR* code.
2499 static int save_session(const ltt_session::locked_ref
& session
,
2500 struct lttng_save_session_attr
*attr
,
2501 lttng_sock_cred
*creds
)
2504 char config_file_path
[LTTNG_PATH_MAX
];
2506 struct config_writer
*writer
= nullptr;
2507 size_t session_name_len
;
2508 const char *provided_path
;
2509 int file_open_flags
= O_CREAT
| O_WRONLY
| O_TRUNC
;
2512 LTTNG_ASSERT(creds
);
2514 session_name_len
= strlen(session
->name
);
2515 memset(config_file_path
, 0, sizeof(config_file_path
));
2517 if (!session_access_ok(session
, LTTNG_SOCK_GET_UID_CRED(creds
)) || session
->destroyed
) {
2518 ret
= LTTNG_ERR_EPERM
;
2522 provided_path
= lttng_save_session_attr_get_output_url(attr
);
2523 if (provided_path
) {
2524 DBG3("Save session in provided path %s", provided_path
);
2525 len
= strlen(provided_path
);
2526 if (len
>= sizeof(config_file_path
)) {
2527 ret
= LTTNG_ERR_SET_URL
;
2530 strncpy(config_file_path
, provided_path
, sizeof(config_file_path
));
2533 char *home_dir
= utils_get_user_home_dir(LTTNG_SOCK_GET_UID_CRED(creds
));
2535 ret
= LTTNG_ERR_SET_URL
;
2539 ret_len
= snprintf(config_file_path
,
2540 sizeof(config_file_path
),
2541 DEFAULT_SESSION_HOME_CONFIGPATH
,
2545 PERROR("snprintf save session");
2546 ret
= LTTNG_ERR_SET_URL
;
2553 * Check the path fits in the config file path dst including the '/'
2554 * followed by trailing .lttng extension and the NULL terminated string.
2556 if ((len
+ session_name_len
+ 2 + sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) >
2557 sizeof(config_file_path
)) {
2558 ret
= LTTNG_ERR_SET_URL
;
2562 ret
= run_as_mkdir_recursive(config_file_path
,
2564 LTTNG_SOCK_GET_UID_CRED(creds
),
2565 LTTNG_SOCK_GET_GID_CRED(creds
));
2567 ret
= LTTNG_ERR_SET_URL
;
2572 * At this point, we know that everything fits in the buffer. Validation
2573 * was done just above.
2575 config_file_path
[len
++] = '/';
2576 strncpy(config_file_path
+ len
, session
->name
, sizeof(config_file_path
) - len
);
2577 len
+= session_name_len
;
2578 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2579 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2580 config_file_path
[len
] = '\0';
2582 if (!attr
->overwrite
) {
2583 file_open_flags
|= O_EXCL
;
2586 fd
= run_as_open(config_file_path
,
2588 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
2589 LTTNG_SOCK_GET_UID_CRED(creds
),
2590 LTTNG_SOCK_GET_GID_CRED(creds
));
2592 PERROR("Could not create configuration file");
2595 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
2598 ret
= LTTNG_ERR_EPERM
;
2601 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2607 writer
= config_writer_create(fd
, 1);
2609 ret
= LTTNG_ERR_NOMEM
;
2613 ret
= config_writer_open_element(writer
, config_element_sessions
);
2615 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2619 ret
= config_writer_open_element(writer
, config_element_session
);
2621 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2625 ret
= config_writer_write_element_string(writer
, config_element_name
, session
->name
);
2627 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2631 if (session
->shm_path
[0] != '\0') {
2632 ret
= config_writer_write_element_string(
2633 writer
, config_element_shared_memory_path
, session
->shm_path
);
2635 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2640 ret
= save_domains(writer
, session
);
2641 if (ret
!= LTTNG_OK
) {
2645 ret
= config_writer_write_element_bool(writer
, config_element_started
, session
->active
);
2647 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2651 if (session
->snapshot_mode
|| session
->live_timer
|| session
->rotate_timer_period
||
2652 session
->rotate_size
) {
2653 ret
= config_writer_open_element(writer
, config_element_attributes
);
2655 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2659 if (session
->snapshot_mode
) {
2660 ret
= config_writer_write_element_bool(
2661 writer
, config_element_snapshot_mode
, 1);
2663 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2666 } else if (session
->live_timer
) {
2667 ret
= config_writer_write_element_unsigned_int(
2668 writer
, config_element_live_timer_interval
, session
->live_timer
);
2670 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2674 if (session
->rotate_timer_period
|| session
->rotate_size
) {
2675 ret
= save_session_rotation_schedules(writer
, session
);
2677 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2683 ret
= config_writer_close_element(writer
);
2685 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2690 ret
= save_session_output(writer
, session
);
2691 if (ret
!= LTTNG_OK
) {
2696 ret
= config_writer_close_element(writer
);
2698 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2703 ret
= config_writer_close_element(writer
);
2705 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2711 if (writer
&& config_writer_destroy(writer
)) {
2712 /* Preserve the original error code */
2713 ret
= ret
!= LTTNG_OK
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2715 if (ret
!= LTTNG_OK
) {
2716 /* Delete file in case of error */
2717 if ((fd
>= 0) && unlink(config_file_path
)) {
2718 PERROR("Unlinking XML session configuration.");
2725 closeret
= close(fd
);
2727 PERROR("Closing XML session configuration");
2734 int cmd_save_sessions(struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
2736 const auto list_lock
= lttng::sessiond::lock_session_list();
2737 const auto session_name
= lttng_save_session_attr_get_session_name(attr
);
2741 * Mind the order of the declaration of list_lock vs session:
2742 * the session list lock must always be released _after_ the release of
2743 * a session's reference (the destruction of a ref/locked_ref) to ensure
2744 * since the reference's release may unpublish the session from the list of
2748 const auto session
= ltt_session::find_locked_session(session_name
);
2749 const auto save_ret
= save_session(session
, attr
, creds
);
2750 if (save_ret
!= LTTNG_OK
) {
2753 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
2754 WARN_FMT("Failed to save session: {} {}", ex
.what(), ex
.source_location
);
2755 return LTTNG_ERR_SESS_NOT_FOUND
;
2758 struct ltt_session_list
*list
= session_get_list();
2759 struct ltt_session
*raw_session_ptr
;
2761 cds_list_for_each_entry (raw_session_ptr
, &list
->head
, list
) {
2762 auto session
= [raw_session_ptr
]() {
2763 session_get(raw_session_ptr
);
2764 raw_session_ptr
->lock();
2765 return ltt_session::make_locked_ref(*raw_session_ptr
);
2767 const auto save_ret
= save_session(session
, attr
, creds
);
2769 /* Don't abort if we don't have the required permissions. */
2770 if (save_ret
!= LTTNG_OK
&& save_ret
!= LTTNG_ERR_EPERM
) {