2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/config/session-config.h>
28 #include <common/utils.h>
29 #include <common/runas.h>
30 #include <lttng/save-internal.h>
35 #include "lttng-syscall.h"
36 #include "trace-ust.h"
40 int save_kernel_channel_attributes(struct config_writer
*writer
,
41 struct lttng_channel_attr
*attr
)
45 ret
= config_writer_write_element_string(writer
,
46 config_element_overwrite_mode
,
47 attr
->overwrite
? config_overwrite_mode_overwrite
:
48 config_overwrite_mode_discard
);
53 ret
= config_writer_write_element_unsigned_int(writer
,
54 config_element_subbuf_size
, attr
->subbuf_size
);
59 ret
= config_writer_write_element_unsigned_int(writer
,
60 config_element_num_subbuf
,
66 ret
= config_writer_write_element_unsigned_int(writer
,
67 config_element_switch_timer_interval
,
68 attr
->switch_timer_interval
);
73 ret
= config_writer_write_element_unsigned_int(writer
,
74 config_element_read_timer_interval
,
75 attr
->read_timer_interval
);
80 ret
= config_writer_write_element_string(writer
,
81 config_element_output_type
,
82 attr
->output
== LTTNG_EVENT_SPLICE
?
83 config_output_type_splice
: config_output_type_mmap
);
88 ret
= config_writer_write_element_unsigned_int(writer
,
89 config_element_tracefile_size
, attr
->tracefile_size
);
94 ret
= config_writer_write_element_unsigned_int(writer
,
95 config_element_tracefile_count
,
96 attr
->tracefile_count
);
101 ret
= config_writer_write_element_unsigned_int(writer
,
102 config_element_live_timer_interval
,
103 attr
->live_timer_interval
);
108 if (attr
->extended
.ptr
) {
109 struct lttng_channel_extended
*ext
= NULL
;
111 ext
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
112 ret
= config_writer_write_element_unsigned_int(writer
,
113 config_element_monitor_timer_interval
,
114 ext
->monitor_timer_interval
);
119 ret
= config_writer_write_element_signed_int(writer
,
120 config_element_blocking_timeout
,
121 ext
->blocking_timeout
);
128 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
132 int save_ust_channel_attributes(struct config_writer
*writer
,
133 struct lttng_ust_channel_attr
*attr
)
136 struct ltt_ust_channel
*channel
= NULL
;
138 ret
= config_writer_write_element_string(writer
,
139 config_element_overwrite_mode
,
140 attr
->overwrite
? config_overwrite_mode_overwrite
:
141 config_overwrite_mode_discard
);
146 ret
= config_writer_write_element_unsigned_int(writer
,
147 config_element_subbuf_size
, attr
->subbuf_size
);
152 ret
= config_writer_write_element_unsigned_int(writer
,
153 config_element_num_subbuf
,
159 ret
= config_writer_write_element_unsigned_int(writer
,
160 config_element_switch_timer_interval
,
161 attr
->switch_timer_interval
);
166 ret
= config_writer_write_element_unsigned_int(writer
,
167 config_element_read_timer_interval
,
168 attr
->read_timer_interval
);
173 ret
= config_writer_write_element_string(writer
,
174 config_element_output_type
,
175 attr
->output
== LTTNG_UST_MMAP
?
176 config_output_type_mmap
: config_output_type_splice
);
181 ret
= config_writer_write_element_signed_int(writer
,
182 config_element_blocking_timeout
,
183 attr
->u
.s
.blocking_timeout
);
189 * Fetch the monitor timer which is located in the parent of
190 * lttng_ust_channel_attr
192 channel
= caa_container_of(attr
, struct ltt_ust_channel
, attr
);
193 ret
= config_writer_write_element_unsigned_int(writer
,
194 config_element_monitor_timer_interval
,
195 channel
->monitor_timer_interval
);
201 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
205 const char *get_kernel_instrumentation_string(
206 enum lttng_kernel_instrumentation instrumentation
)
208 const char *instrumentation_string
;
210 switch (instrumentation
) {
211 case LTTNG_KERNEL_ALL
:
212 instrumentation_string
= config_event_type_all
;
214 case LTTNG_KERNEL_TRACEPOINT
:
215 instrumentation_string
= config_event_type_tracepoint
;
217 case LTTNG_KERNEL_KPROBE
:
218 instrumentation_string
= config_event_type_probe
;
220 case LTTNG_KERNEL_UPROBE
:
221 instrumentation_string
= config_event_type_userspace_probe
;
223 case LTTNG_KERNEL_FUNCTION
:
224 instrumentation_string
= config_event_type_function_entry
;
226 case LTTNG_KERNEL_KRETPROBE
:
227 instrumentation_string
= config_event_type_function
;
229 case LTTNG_KERNEL_NOOP
:
230 instrumentation_string
= config_event_type_noop
;
232 case LTTNG_KERNEL_SYSCALL
:
233 instrumentation_string
= config_event_type_syscall
;
236 instrumentation_string
= NULL
;
239 return instrumentation_string
;
243 const char *get_kernel_context_type_string(
244 enum lttng_kernel_context_type context_type
)
246 const char *context_type_string
;
248 switch (context_type
) {
249 case LTTNG_KERNEL_CONTEXT_PID
:
250 context_type_string
= config_event_context_pid
;
252 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
253 context_type_string
= config_event_context_procname
;
255 case LTTNG_KERNEL_CONTEXT_PRIO
:
256 context_type_string
= config_event_context_prio
;
258 case LTTNG_KERNEL_CONTEXT_NICE
:
259 context_type_string
= config_event_context_nice
;
261 case LTTNG_KERNEL_CONTEXT_VPID
:
262 context_type_string
= config_event_context_vpid
;
264 case LTTNG_KERNEL_CONTEXT_TID
:
265 context_type_string
= config_event_context_tid
;
267 case LTTNG_KERNEL_CONTEXT_VTID
:
268 context_type_string
= config_event_context_vtid
;
270 case LTTNG_KERNEL_CONTEXT_PPID
:
271 context_type_string
= config_event_context_ppid
;
273 case LTTNG_KERNEL_CONTEXT_VPPID
:
274 context_type_string
= config_event_context_vppid
;
276 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
277 context_type_string
= config_event_context_hostname
;
279 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
280 context_type_string
= config_event_context_interruptible
;
282 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
283 context_type_string
= config_event_context_preemptible
;
285 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
286 context_type_string
= config_event_context_need_reschedule
;
288 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
289 context_type_string
= config_event_context_migratable
;
291 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
292 context_type_string
= config_event_context_callstack_user
;
294 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
295 context_type_string
= config_event_context_callstack_kernel
;
297 case LTTNG_KERNEL_CONTEXT_CGROUP_NS
:
298 context_type_string
= config_event_context_cgroup_ns
;
300 case LTTNG_KERNEL_CONTEXT_IPC_NS
:
301 context_type_string
= config_event_context_ipc_ns
;
303 case LTTNG_KERNEL_CONTEXT_MNT_NS
:
304 context_type_string
= config_event_context_mnt_ns
;
306 case LTTNG_KERNEL_CONTEXT_NET_NS
:
307 context_type_string
= config_event_context_net_ns
;
309 case LTTNG_KERNEL_CONTEXT_PID_NS
:
310 context_type_string
= config_event_context_pid_ns
;
312 case LTTNG_KERNEL_CONTEXT_USER_NS
:
313 context_type_string
= config_event_context_user_ns
;
315 case LTTNG_KERNEL_CONTEXT_UTS_NS
:
316 context_type_string
= config_event_context_uts_ns
;
319 context_type_string
= NULL
;
322 return context_type_string
;
326 const char *get_ust_context_type_string(
327 enum lttng_ust_context_type context_type
)
329 const char *context_type_string
;
331 switch (context_type
) {
332 case LTTNG_UST_CONTEXT_PROCNAME
:
333 context_type_string
= config_event_context_procname
;
335 case LTTNG_UST_CONTEXT_VPID
:
336 context_type_string
= config_event_context_vpid
;
338 case LTTNG_UST_CONTEXT_VTID
:
339 context_type_string
= config_event_context_vtid
;
341 case LTTNG_UST_CONTEXT_IP
:
342 context_type_string
= config_event_context_ip
;
344 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
345 context_type_string
= config_event_context_pthread_id
;
347 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
348 context_type_string
= config_event_context_app
;
350 case LTTNG_UST_CONTEXT_CGROUP_NS
:
351 context_type_string
= config_event_context_cgroup_ns
;
353 case LTTNG_UST_CONTEXT_IPC_NS
:
354 context_type_string
= config_event_context_ipc_ns
;
356 case LTTNG_UST_CONTEXT_MNT_NS
:
357 context_type_string
= config_event_context_mnt_ns
;
359 case LTTNG_UST_CONTEXT_NET_NS
:
360 context_type_string
= config_event_context_net_ns
;
362 case LTTNG_UST_CONTEXT_PID_NS
:
363 context_type_string
= config_event_context_pid_ns
;
365 case LTTNG_UST_CONTEXT_USER_NS
:
366 context_type_string
= config_event_context_user_ns
;
368 case LTTNG_UST_CONTEXT_UTS_NS
:
369 context_type_string
= config_event_context_uts_ns
;
371 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
373 * Error, should not be stored in the XML, perf contexts
374 * are stored as a node of type event_perf_context_type.
377 context_type_string
= NULL
;
381 return context_type_string
;
385 const char *get_buffer_type_string(
386 enum lttng_buffer_type buffer_type
)
388 const char *buffer_type_string
;
390 switch (buffer_type
) {
391 case LTTNG_BUFFER_PER_PID
:
392 buffer_type_string
= config_buffer_type_per_pid
;
394 case LTTNG_BUFFER_PER_UID
:
395 buffer_type_string
= config_buffer_type_per_uid
;
397 case LTTNG_BUFFER_GLOBAL
:
398 buffer_type_string
= config_buffer_type_global
;
401 buffer_type_string
= NULL
;
404 return buffer_type_string
;
408 const char *get_loglevel_type_string(
409 enum lttng_ust_loglevel_type loglevel_type
)
411 const char *loglevel_type_string
;
413 switch (loglevel_type
) {
414 case LTTNG_UST_LOGLEVEL_ALL
:
415 loglevel_type_string
= config_loglevel_type_all
;
417 case LTTNG_UST_LOGLEVEL_RANGE
:
418 loglevel_type_string
= config_loglevel_type_range
;
420 case LTTNG_UST_LOGLEVEL_SINGLE
:
421 loglevel_type_string
= config_loglevel_type_single
;
424 loglevel_type_string
= NULL
;
427 return loglevel_type_string
;
431 int save_kernel_function_event(struct config_writer
*writer
,
432 struct ltt_kernel_event
*event
)
436 ret
= config_writer_open_element(writer
, config_element_function_attributes
);
438 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
442 ret
= config_writer_write_element_string(writer
, config_element_name
,
443 event
->event
->u
.ftrace
.symbol_name
);
445 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
449 /* /function attributes */
450 ret
= config_writer_close_element(writer
);
452 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
460 int save_kernel_kprobe_event(struct config_writer
*writer
,
461 struct ltt_kernel_event
*event
)
464 const char *symbol_name
;
468 switch (event
->event
->instrumentation
) {
469 case LTTNG_KERNEL_KPROBE
:
471 * Comments in lttng-kernel.h mention that
472 * either addr or symbol_name are set, not both.
474 addr
= event
->event
->u
.kprobe
.addr
;
475 offset
= event
->event
->u
.kprobe
.offset
;
476 symbol_name
= addr
? NULL
: event
->event
->u
.kprobe
.symbol_name
;
478 case LTTNG_KERNEL_KRETPROBE
:
479 addr
= event
->event
->u
.kretprobe
.addr
;
480 offset
= event
->event
->u
.kretprobe
.offset
;
481 symbol_name
= addr
? NULL
: event
->event
->u
.kretprobe
.symbol_name
;
485 ERR("Unsupported kernel instrumentation type.");
486 ret
= LTTNG_ERR_INVALID
;
490 ret
= config_writer_open_element(writer
, config_element_probe_attributes
);
492 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
497 ret
= config_writer_write_element_unsigned_int( writer
,
498 config_element_address
, addr
);
500 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
503 } else if (symbol_name
) {
504 ret
= config_writer_write_element_string(writer
,
505 config_element_symbol_name
, symbol_name
);
507 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
510 /* If the offset is non-zero, write it.*/
512 ret
= config_writer_write_element_unsigned_int(writer
,
513 config_element_offset
, offset
);
515 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
521 * This really should not happen as we are either setting the
522 * address or the symbol above.
524 ERR("Invalid probe/function description.");
525 ret
= LTTNG_ERR_INVALID
;
530 ret
= config_writer_close_element(writer
);
532 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
540 * Save the userspace probe tracepoint event associated with the event to the
544 int save_kernel_userspace_probe_tracepoint_event(struct config_writer
*writer
,
545 struct ltt_kernel_event
*event
)
548 const char *probe_name
, *provider_name
, *binary_path
;
549 const struct lttng_userspace_probe_location
*userspace_probe_location
;
550 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
551 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
553 /* Get userspace probe location from the event. */
554 userspace_probe_location
= event
->userspace_probe_location
;
555 if (!userspace_probe_location
) {
556 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
560 /* Get lookup method and lookup method type. */
561 lookup_method
= lttng_userspace_probe_location_get_lookup_method(userspace_probe_location
);
562 if (!lookup_method
) {
563 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
567 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
569 /* Get the binary path, probe name and provider name. */
571 lttng_userspace_probe_location_tracepoint_get_binary_path(
572 userspace_probe_location
);
574 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
579 lttng_userspace_probe_location_tracepoint_get_probe_name(
580 userspace_probe_location
);
582 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
587 lttng_userspace_probe_location_tracepoint_get_provider_name(
588 userspace_probe_location
);
589 if (!provider_name
) {
590 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
594 /* Open a userspace probe tracepoint attribute. */
595 ret
= config_writer_open_element(writer
, config_element_userspace_probe_tracepoint_attributes
);
597 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
601 switch (lookup_type
) {
602 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
603 ret
= config_writer_write_element_string(writer
,
604 config_element_userspace_probe_lookup
,
605 config_element_userspace_probe_lookup_tracepoint_sdt
);
607 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
612 ERR("Unsupported kernel userspace probe tracepoint lookup method.");
613 ret
= LTTNG_ERR_INVALID
;
617 /* Write the binary path, provider name and the probe name. */
618 ret
= config_writer_write_element_string(writer
,
619 config_element_userspace_probe_location_binary_path
,
622 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
626 ret
= config_writer_write_element_string(writer
,
627 config_element_userspace_probe_tracepoint_location_provider_name
,
630 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
634 ret
= config_writer_write_element_string(writer
,
635 config_element_userspace_probe_tracepoint_location_probe_name
,
638 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
642 /* Close the userspace probe tracepoint attribute. */
643 ret
= config_writer_close_element(writer
);
645 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
654 * Save the userspace probe function event associated with the event to the
658 int save_kernel_userspace_probe_function_event(struct config_writer
*writer
,
659 struct ltt_kernel_event
*event
)
662 const char *function_name
, *binary_path
;
663 const struct lttng_userspace_probe_location
*userspace_probe_location
;
664 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
665 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
667 /* Get userspace probe location from the event. */
668 userspace_probe_location
= event
->userspace_probe_location
;
669 if (!userspace_probe_location
) {
670 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
674 /* Get lookup method and lookup method type. */
675 lookup_method
= lttng_userspace_probe_location_get_lookup_method(
676 userspace_probe_location
);
677 if (!lookup_method
) {
678 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
682 /* Get the binary path and the function name. */
684 lttng_userspace_probe_location_function_get_binary_path(
685 userspace_probe_location
);
687 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
692 lttng_userspace_probe_location_function_get_function_name(
693 userspace_probe_location
);
694 if (!function_name
) {
695 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
699 /* Open a userspace probe function attribute. */
700 ret
= config_writer_open_element(writer
,
701 config_element_userspace_probe_function_attributes
);
703 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
707 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
708 switch (lookup_type
) {
709 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
710 ret
= config_writer_write_element_string(writer
,
711 config_element_userspace_probe_lookup
,
712 config_element_userspace_probe_lookup_function_elf
);
714 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
718 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
719 ret
= config_writer_write_element_string(writer
,
720 config_element_userspace_probe_lookup
,
721 config_element_userspace_probe_lookup_function_default
);
723 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
728 ERR("Unsupported kernel userspace probe function lookup method.");
729 ret
= LTTNG_ERR_INVALID
;
733 /* Write the binary path and the function name. */
734 ret
= config_writer_write_element_string(writer
,
735 config_element_userspace_probe_location_binary_path
,
738 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
742 ret
= config_writer_write_element_string(writer
,
743 config_element_userspace_probe_function_location_function_name
,
746 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
750 /* Close the userspace probe function attribute. */
751 ret
= config_writer_close_element(writer
);
753 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
762 int save_kernel_userspace_probe_event(struct config_writer
*writer
,
763 struct ltt_kernel_event
*event
)
766 struct lttng_userspace_probe_location
*userspace_probe_location
;
768 /* Get userspace probe location from the event. */
769 userspace_probe_location
= event
->userspace_probe_location
;
770 if (!userspace_probe_location
) {
771 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
775 switch(lttng_userspace_probe_location_get_type(userspace_probe_location
)) {
776 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
778 ret
= save_kernel_userspace_probe_function_event(writer
, event
);
780 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
785 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
787 ret
= save_kernel_userspace_probe_tracepoint_event(writer
, event
);
789 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
794 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
:
796 ERR("Unsupported kernel userspace probe location type.");
797 ret
= LTTNG_ERR_INVALID
;
806 int save_kernel_event(struct config_writer
*writer
,
807 struct ltt_kernel_event
*event
)
810 const char *instrumentation_type
;
812 ret
= config_writer_open_element(writer
, config_element_event
);
814 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
818 if (event
->event
->name
[0]) {
819 ret
= config_writer_write_element_string(writer
,
820 config_element_name
, event
->event
->name
);
822 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
827 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
830 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
834 instrumentation_type
= get_kernel_instrumentation_string(
835 event
->event
->instrumentation
);
836 if (!instrumentation_type
) {
837 ret
= LTTNG_ERR_INVALID
;
841 ret
= config_writer_write_element_string(writer
, config_element_type
,
842 instrumentation_type
);
844 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
848 if (event
->filter_expression
) {
849 ret
= config_writer_write_element_string(writer
,
850 config_element_filter
,
851 event
->filter_expression
);
853 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
858 if (event
->event
->instrumentation
== LTTNG_KERNEL_FUNCTION
||
859 event
->event
->instrumentation
== LTTNG_KERNEL_KPROBE
||
860 event
->event
->instrumentation
== LTTNG_KERNEL_UPROBE
||
861 event
->event
->instrumentation
== LTTNG_KERNEL_KRETPROBE
) {
863 ret
= config_writer_open_element(writer
,
864 config_element_attributes
);
866 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
870 switch (event
->event
->instrumentation
) {
871 case LTTNG_KERNEL_SYSCALL
:
872 case LTTNG_KERNEL_FUNCTION
:
873 ret
= save_kernel_function_event(writer
, event
);
878 case LTTNG_KERNEL_KPROBE
:
879 case LTTNG_KERNEL_KRETPROBE
:
880 ret
= save_kernel_kprobe_event(writer
, event
);
885 case LTTNG_KERNEL_UPROBE
:
886 ret
= save_kernel_userspace_probe_event(writer
, event
);
892 ERR("Unsupported kernel instrumentation type.");
893 ret
= LTTNG_ERR_INVALID
;
898 ret
= config_writer_close_element(writer
);
900 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
906 ret
= config_writer_close_element(writer
);
908 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
916 int save_kernel_events(struct config_writer
*writer
,
917 struct ltt_kernel_channel
*kchan
)
920 struct ltt_kernel_event
*event
;
922 ret
= config_writer_open_element(writer
, config_element_events
);
924 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
928 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
929 ret
= save_kernel_event(writer
, event
);
936 ret
= config_writer_close_element(writer
);
938 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
946 int save_ust_event(struct config_writer
*writer
,
947 struct ltt_ust_event
*event
)
950 const char *loglevel_type_string
;
952 ret
= config_writer_open_element(writer
, config_element_event
);
954 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
958 if (event
->attr
.name
[0]) {
959 ret
= config_writer_write_element_string(writer
,
960 config_element_name
, event
->attr
.name
);
962 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
967 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
970 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
974 if (event
->attr
.instrumentation
!= LTTNG_UST_TRACEPOINT
) {
975 ERR("Unsupported UST instrumentation type.");
976 ret
= LTTNG_ERR_INVALID
;
979 ret
= config_writer_write_element_string(writer
, config_element_type
,
980 config_event_type_tracepoint
);
982 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
986 loglevel_type_string
= get_loglevel_type_string(
987 event
->attr
.loglevel_type
);
988 if (!loglevel_type_string
) {
989 ERR("Unsupported UST loglevel type.");
990 ret
= LTTNG_ERR_INVALID
;
994 ret
= config_writer_write_element_string(writer
,
995 config_element_loglevel_type
, loglevel_type_string
);
997 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1001 /* The log level is irrelevant if no "filtering" is enabled */
1002 if (event
->attr
.loglevel_type
!= LTTNG_UST_LOGLEVEL_ALL
) {
1003 ret
= config_writer_write_element_signed_int(writer
,
1004 config_element_loglevel
, event
->attr
.loglevel
);
1006 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1011 if (event
->filter_expression
) {
1012 ret
= config_writer_write_element_string(writer
,
1013 config_element_filter
, event
->filter_expression
);
1015 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1020 if (event
->exclusion
&& event
->exclusion
->count
) {
1023 ret
= config_writer_open_element(writer
,
1024 config_element_exclusions
);
1026 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1030 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
1031 ret
= config_writer_write_element_string(writer
,
1032 config_element_exclusion
,
1033 LTTNG_EVENT_EXCLUSION_NAME_AT(
1034 event
->exclusion
, i
));
1036 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1042 ret
= config_writer_close_element(writer
);
1044 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1050 ret
= config_writer_close_element(writer
);
1052 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1060 int save_ust_events(struct config_writer
*writer
,
1061 struct lttng_ht
*events
)
1064 struct ltt_ust_event
*event
;
1065 struct lttng_ht_node_str
*node
;
1066 struct lttng_ht_iter iter
;
1068 ret
= config_writer_open_element(writer
, config_element_events
);
1070 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1075 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1076 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
1078 if (event
->internal
) {
1079 /* Internal events must not be exposed to clients */
1082 ret
= save_ust_event(writer
, event
);
1091 ret
= config_writer_close_element(writer
);
1093 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1101 int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
1102 struct agent_event
*agent_event
)
1105 enum lttng_ust_loglevel_type ust_loglevel_type
;
1107 ust_event
->enabled
= agent_event
->enabled
;
1108 ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1109 if (lttng_strncpy(ust_event
->attr
.name
, agent_event
->name
,
1110 LTTNG_SYMBOL_NAME_LEN
)) {
1114 switch (agent_event
->loglevel_type
) {
1115 case LTTNG_EVENT_LOGLEVEL_ALL
:
1116 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1118 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1119 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
1121 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1122 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
1125 ERR("Invalid agent_event loglevel_type.");
1130 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
1131 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
1132 ust_event
->filter_expression
= agent_event
->filter_expression
;
1133 ust_event
->exclusion
= agent_event
->exclusion
;
1139 int save_agent_events(struct config_writer
*writer
,
1140 struct agent
*agent
)
1143 struct lttng_ht_iter iter
;
1144 struct lttng_ht_node_str
*node
;
1146 ret
= config_writer_open_element(writer
, config_element_events
);
1148 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1153 cds_lfht_for_each_entry(agent
->events
->ht
, &iter
.iter
, node
, node
) {
1155 struct agent_event
*agent_event
;
1156 struct ltt_ust_event fake_event
;
1158 memset(&fake_event
, 0, sizeof(fake_event
));
1159 agent_event
= caa_container_of(node
, struct agent_event
, node
);
1162 * Initialize a fake ust event to reuse the same serialization
1163 * function since UST and agent events contain the same info
1164 * (and one could wonder why they don't reuse the same
1167 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
1172 ret
= save_ust_event(writer
, &fake_event
);
1181 ret
= config_writer_close_element(writer
);
1183 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1191 int save_kernel_context(struct config_writer
*writer
,
1192 struct lttng_kernel_context
*ctx
)
1200 ret
= config_writer_open_element(writer
, config_element_context
);
1202 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1206 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
1207 ret
= config_writer_open_element(writer
,
1208 config_element_context_perf
);
1210 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1214 ret
= config_writer_write_element_unsigned_int(writer
,
1215 config_element_type
, ctx
->u
.perf_counter
.type
);
1217 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1221 ret
= config_writer_write_element_unsigned_int(writer
,
1222 config_element_config
, ctx
->u
.perf_counter
.config
);
1224 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1228 ret
= config_writer_write_element_string(writer
,
1229 config_element_name
, ctx
->u
.perf_counter
.name
);
1231 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1236 ret
= config_writer_close_element(writer
);
1238 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1242 const char *context_type_string
=
1243 get_kernel_context_type_string(ctx
->ctx
);
1245 if (!context_type_string
) {
1246 ERR("Unsupported kernel context type.");
1247 ret
= LTTNG_ERR_INVALID
;
1251 ret
= config_writer_write_element_string(writer
,
1252 config_element_type
, context_type_string
);
1254 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1260 ret
= config_writer_close_element(writer
);
1262 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1271 int save_kernel_contexts(struct config_writer
*writer
,
1272 struct ltt_kernel_channel
*kchan
)
1275 struct ltt_kernel_context
*ctx
;
1277 if (cds_list_empty(&kchan
->ctx_list
)) {
1282 ret
= config_writer_open_element(writer
, config_element_contexts
);
1284 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1288 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
1289 ret
= save_kernel_context(writer
, &ctx
->ctx
);
1296 ret
= config_writer_close_element(writer
);
1298 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1306 int save_ust_context_perf_thread_counter(struct config_writer
*writer
,
1307 struct ltt_ust_context
*ctx
)
1314 /* Perf contexts are saved as event_perf_context_type */
1315 ret
= config_writer_open_element(writer
, config_element_context_perf
);
1317 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1321 ret
= config_writer_write_element_unsigned_int(writer
,
1322 config_element_type
, ctx
->ctx
.u
.perf_counter
.type
);
1324 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1328 ret
= config_writer_write_element_unsigned_int(writer
,
1329 config_element_config
, ctx
->ctx
.u
.perf_counter
.config
);
1331 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1335 ret
= config_writer_write_element_string(writer
, config_element_name
,
1336 ctx
->ctx
.u
.perf_counter
.name
);
1338 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1343 ret
= config_writer_close_element(writer
);
1345 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1353 int save_ust_context_app_ctx(struct config_writer
*writer
,
1354 struct ltt_ust_context
*ctx
)
1361 /* Application contexts are saved as application_context_type */
1362 ret
= config_writer_open_element(writer
, config_element_context_app
);
1364 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1368 ret
= config_writer_write_element_string(writer
,
1369 config_element_context_app_provider_name
,
1370 ctx
->ctx
.u
.app_ctx
.provider_name
);
1372 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1376 ret
= config_writer_write_element_string(writer
,
1377 config_element_context_app_ctx_name
,
1378 ctx
->ctx
.u
.app_ctx
.ctx_name
);
1380 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1385 ret
= config_writer_close_element(writer
);
1387 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1395 int save_ust_context_generic(struct config_writer
*writer
,
1396 struct ltt_ust_context
*ctx
)
1399 const char *context_type_string
;
1404 /* Save context as event_context_type_type */
1405 context_type_string
= get_ust_context_type_string(
1407 if (!context_type_string
) {
1408 ERR("Unsupported UST context type.");
1409 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1413 ret
= config_writer_write_element_string(writer
,
1414 config_element_type
, context_type_string
);
1416 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1424 int save_ust_context(struct config_writer
*writer
,
1425 struct cds_list_head
*ctx_list
)
1428 struct ltt_ust_context
*ctx
;
1433 ret
= config_writer_open_element(writer
, config_element_contexts
);
1435 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1439 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
1440 ret
= config_writer_open_element(writer
,
1441 config_element_context
);
1443 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1447 switch (ctx
->ctx
.ctx
) {
1448 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
1449 ret
= save_ust_context_perf_thread_counter(writer
, ctx
);
1451 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1452 ret
= save_ust_context_app_ctx(writer
, ctx
);
1455 /* Save generic context. */
1456 ret
= save_ust_context_generic(writer
, ctx
);
1463 ret
= config_writer_close_element(writer
);
1465 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1471 ret
= config_writer_close_element(writer
);
1473 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1481 int save_kernel_channel(struct config_writer
*writer
,
1482 struct ltt_kernel_channel
*kchan
)
1489 ret
= config_writer_open_element(writer
, config_element_channel
);
1491 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1495 ret
= config_writer_write_element_string(writer
, config_element_name
,
1496 kchan
->channel
->name
);
1498 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1502 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1503 kchan
->channel
->enabled
);
1505 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1509 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1514 ret
= save_kernel_events(writer
, kchan
);
1519 ret
= save_kernel_contexts(writer
, kchan
);
1525 ret
= config_writer_close_element(writer
);
1527 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1535 int save_ust_channel(struct config_writer
*writer
,
1536 struct ltt_ust_channel
*ust_chan
,
1537 struct ltt_ust_session
*session
)
1545 ret
= config_writer_open_element(writer
, config_element_channel
);
1547 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1551 ret
= config_writer_write_element_string(writer
, config_element_name
,
1554 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1558 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1561 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1565 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1570 ret
= config_writer_write_element_unsigned_int(writer
,
1571 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1573 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1577 ret
= config_writer_write_element_unsigned_int(writer
,
1578 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1580 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1584 ret
= config_writer_write_element_unsigned_int(writer
,
1585 config_element_live_timer_interval
,
1586 session
->live_timer_interval
);
1588 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1592 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1593 ret
= save_ust_events(writer
, ust_chan
->events
);
1598 struct agent
*agent
= NULL
;
1600 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1602 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1603 ERR("Could not find agent associated to UST subdomain");
1608 * Channels associated with a UST sub-domain (such as JUL, Log4j
1609 * or Python) don't have any non-internal events. We retrieve
1610 * the "agent" events associated with this channel and serialize
1613 ret
= save_agent_events(writer
, agent
);
1619 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1625 ret
= config_writer_close_element(writer
);
1627 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1635 int save_kernel_session(struct config_writer
*writer
,
1636 struct ltt_session
*session
)
1639 struct ltt_kernel_channel
*kchan
;
1644 ret
= config_writer_write_element_string(writer
, config_element_type
,
1645 config_domain_type_kernel
);
1647 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1651 ret
= config_writer_write_element_string(writer
,
1652 config_element_buffer_type
, config_buffer_type_global
);
1654 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1658 ret
= config_writer_open_element(writer
,
1659 config_element_channels
);
1661 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1665 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1667 ret
= save_kernel_channel(writer
, kchan
);
1674 ret
= config_writer_close_element(writer
);
1676 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1684 const char *get_config_domain_str(enum lttng_domain_type domain
)
1686 const char *str_dom
;
1689 case LTTNG_DOMAIN_KERNEL
:
1690 str_dom
= config_domain_type_kernel
;
1692 case LTTNG_DOMAIN_UST
:
1693 str_dom
= config_domain_type_ust
;
1695 case LTTNG_DOMAIN_JUL
:
1696 str_dom
= config_domain_type_jul
;
1698 case LTTNG_DOMAIN_LOG4J
:
1699 str_dom
= config_domain_type_log4j
;
1701 case LTTNG_DOMAIN_PYTHON
:
1702 str_dom
= config_domain_type_python
;
1712 int save_pid_tracker(struct config_writer
*writer
,
1713 struct ltt_session
*sess
, int domain
)
1716 ssize_t nr_pids
= 0, i
;
1717 int32_t *pids
= NULL
;
1720 case LTTNG_DOMAIN_KERNEL
:
1722 nr_pids
= kernel_list_tracker_pids(sess
->kernel_session
, &pids
);
1724 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1729 case LTTNG_DOMAIN_UST
:
1731 nr_pids
= trace_ust_list_tracker_pids(sess
->ust_session
, &pids
);
1733 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1738 case LTTNG_DOMAIN_JUL
:
1739 case LTTNG_DOMAIN_LOG4J
:
1740 case LTTNG_DOMAIN_PYTHON
:
1742 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1746 /* Only create a pid_tracker if enabled or untrack all */
1747 if (nr_pids
!= 1 || (nr_pids
== 1 && pids
[0] != -1)) {
1748 ret
= config_writer_open_element(writer
,
1749 config_element_pid_tracker
);
1751 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1755 ret
= config_writer_open_element(writer
,
1756 config_element_targets
);
1758 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1762 for (i
= 0; i
< nr_pids
; i
++) {
1763 ret
= config_writer_open_element(writer
,
1764 config_element_target_pid
);
1766 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1770 ret
= config_writer_write_element_unsigned_int(writer
,
1771 config_element_pid
, pids
[i
]);
1773 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1778 ret
= config_writer_close_element(writer
);
1780 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1786 ret
= config_writer_close_element(writer
);
1788 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1793 ret
= config_writer_close_element(writer
);
1795 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1805 int save_ust_domain(struct config_writer
*writer
,
1806 struct ltt_session
*session
, enum lttng_domain_type domain
)
1809 struct ltt_ust_channel
*ust_chan
;
1810 const char *buffer_type_string
;
1811 struct lttng_ht_node_str
*node
;
1812 struct lttng_ht_iter iter
;
1813 const char *config_domain_name
;
1818 ret
= config_writer_open_element(writer
,
1819 config_element_domain
);
1821 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1825 config_domain_name
= get_config_domain_str(domain
);
1826 if (!config_domain_name
) {
1827 ret
= LTTNG_ERR_INVALID
;
1831 ret
= config_writer_write_element_string(writer
,
1832 config_element_type
, config_domain_name
);
1834 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1838 buffer_type_string
= get_buffer_type_string(
1839 session
->ust_session
->buffer_type
);
1840 if (!buffer_type_string
) {
1841 ERR("Unsupported buffer type.");
1842 ret
= LTTNG_ERR_INVALID
;
1846 ret
= config_writer_write_element_string(writer
,
1847 config_element_buffer_type
, buffer_type_string
);
1849 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1853 ret
= config_writer_open_element(writer
, config_element_channels
);
1855 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1860 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1861 &iter
.iter
, node
, node
) {
1862 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1863 if (domain
== ust_chan
->domain
) {
1864 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1874 ret
= config_writer_close_element(writer
);
1876 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1880 if (domain
== LTTNG_DOMAIN_UST
) {
1881 ret
= config_writer_open_element(writer
,
1882 config_element_trackers
);
1884 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1888 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_UST
);
1894 ret
= config_writer_close_element(writer
);
1901 ret
= config_writer_close_element(writer
);
1903 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1912 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1919 if (!session
->kernel_session
&& !session
->ust_session
) {
1923 ret
= config_writer_open_element(writer
, config_element_domains
);
1925 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1930 if (session
->kernel_session
) {
1931 ret
= config_writer_open_element(writer
,
1932 config_element_domain
);
1934 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1938 ret
= save_kernel_session(writer
, session
);
1943 ret
= config_writer_open_element(writer
,
1944 config_element_trackers
);
1946 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1950 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_KERNEL
);
1956 ret
= config_writer_close_element(writer
);
1958 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1962 ret
= config_writer_close_element(writer
);
1964 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1969 if (session
->ust_session
) {
1970 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
1975 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
1980 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
1985 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
1992 ret
= config_writer_close_element(writer
);
1994 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2002 int save_consumer_output(struct config_writer
*writer
,
2003 struct consumer_output
*output
)
2010 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
2012 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2016 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
2019 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2023 ret
= config_writer_open_element(writer
, config_element_destination
);
2025 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2029 switch (output
->type
) {
2030 case CONSUMER_DST_LOCAL
:
2031 ret
= config_writer_write_element_string(writer
,
2032 config_element_path
, output
->dst
.session_root_path
);
2034 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2038 case CONSUMER_DST_NET
:
2042 uri
= zmalloc(PATH_MAX
);
2044 ret
= LTTNG_ERR_NOMEM
;
2048 ret
= config_writer_open_element(writer
, config_element_net_output
);
2050 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2051 goto end_net_output
;
2054 if (output
->dst
.net
.control_isset
&&
2055 output
->dst
.net
.data_isset
) {
2056 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
2058 ret
= LTTNG_ERR_INVALID
;
2059 goto end_net_output
;
2062 ret
= config_writer_write_element_string(writer
,
2063 config_element_control_uri
, uri
);
2065 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2066 goto end_net_output
;
2069 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
2071 ret
= LTTNG_ERR_INVALID
;
2072 goto end_net_output
;
2075 ret
= config_writer_write_element_string(writer
,
2076 config_element_data_uri
, uri
);
2078 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2079 goto end_net_output
;
2088 ret
= !output
->dst
.net
.control_isset
?
2089 LTTNG_ERR_URL_CTRL_MISS
:
2090 LTTNG_ERR_URL_DATA_MISS
;
2095 ret
= config_writer_close_element(writer
);
2097 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2103 ERR("Unsupported consumer output type.");
2104 ret
= LTTNG_ERR_INVALID
;
2109 ret
= config_writer_close_element(writer
);
2111 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2115 /* /consumer_output */
2116 ret
= config_writer_close_element(writer
);
2118 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2126 int save_snapshot_outputs(struct config_writer
*writer
,
2127 struct snapshot
*snapshot
)
2130 struct lttng_ht_iter iter
;
2131 struct snapshot_output
*output
;
2136 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
2138 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2143 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
2145 ret
= config_writer_open_element(writer
,
2146 config_element_output
);
2148 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2152 ret
= config_writer_write_element_string(writer
,
2153 config_element_name
, output
->name
);
2155 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2159 ret
= config_writer_write_element_unsigned_int(writer
,
2160 config_element_max_size
, output
->max_size
);
2162 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2166 ret
= save_consumer_output(writer
, output
->consumer
);
2172 ret
= config_writer_close_element(writer
);
2174 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2180 /* /snapshot_outputs */
2181 ret
= config_writer_close_element(writer
);
2183 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2195 int save_session_output(struct config_writer
*writer
,
2196 struct ltt_session
*session
)
2203 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
2204 (!session
->snapshot_mode
&& !session
->consumer
)) {
2205 /* Session is in no output mode */
2210 ret
= config_writer_open_element(writer
, config_element_output
);
2212 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2216 if (session
->snapshot_mode
) {
2217 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
2222 if (session
->consumer
) {
2223 ret
= save_consumer_output(writer
, session
->consumer
);
2231 ret
= config_writer_close_element(writer
);
2233 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2241 int save_session_rotation_schedule(struct config_writer
*writer
,
2242 enum lttng_rotation_schedule_type type
, uint64_t value
)
2245 const char *element_name
;
2246 const char *value_name
;
2249 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
2250 element_name
= config_element_rotation_schedule_periodic
;
2251 value_name
= config_element_rotation_schedule_periodic_time_us
;
2253 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
2254 element_name
= config_element_rotation_schedule_size_threshold
;
2255 value_name
= config_element_rotation_schedule_size_threshold_bytes
;
2262 ret
= config_writer_open_element(writer
, element_name
);
2267 ret
= config_writer_write_element_unsigned_int(writer
,
2273 /* Close schedule descriptor element. */
2274 ret
= config_writer_close_element(writer
);
2283 int save_session_rotation_schedules(struct config_writer
*writer
,
2284 struct ltt_session
*session
)
2288 ret
= config_writer_open_element(writer
,
2289 config_element_rotation_schedules
);
2293 if (session
->rotate_timer_period
) {
2294 ret
= save_session_rotation_schedule(writer
,
2295 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
,
2296 session
->rotate_timer_period
);
2298 goto close_schedules
;
2301 if (session
->rotate_size
) {
2302 ret
= save_session_rotation_schedule(writer
,
2303 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
,
2304 session
->rotate_size
);
2306 goto close_schedules
;
2311 /* Close rotation schedules element. */
2312 ret
= config_writer_close_element(writer
);
2321 * Save the given session.
2323 * Return 0 on success else a LTTNG_ERR* code.
2326 int save_session(struct ltt_session
*session
,
2327 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
2330 char config_file_path
[LTTNG_PATH_MAX
];
2332 struct config_writer
*writer
= NULL
;
2333 size_t session_name_len
;
2334 const char *provided_path
;
2335 int file_open_flags
= O_CREAT
| O_WRONLY
| O_TRUNC
;
2341 session_name_len
= strlen(session
->name
);
2342 memset(config_file_path
, 0, sizeof(config_file_path
));
2344 if (!session_access_ok(session
,
2345 LTTNG_SOCK_GET_UID_CRED(creds
),
2346 LTTNG_SOCK_GET_GID_CRED(creds
)) || session
->destroyed
) {
2347 ret
= LTTNG_ERR_EPERM
;
2351 provided_path
= lttng_save_session_attr_get_output_url(attr
);
2352 if (provided_path
) {
2353 DBG3("Save session in provided path %s", provided_path
);
2354 len
= strlen(provided_path
);
2355 if (len
>= sizeof(config_file_path
)) {
2356 ret
= LTTNG_ERR_SET_URL
;
2359 strncpy(config_file_path
, provided_path
, sizeof(config_file_path
));
2362 char *home_dir
= utils_get_user_home_dir(
2363 LTTNG_SOCK_GET_UID_CRED(creds
));
2365 ret
= LTTNG_ERR_SET_URL
;
2369 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
2370 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
2373 PERROR("snprintf save session");
2374 ret
= LTTNG_ERR_SET_URL
;
2381 * Check the path fits in the config file path dst including the '/'
2382 * followed by trailing .lttng extension and the NULL terminated string.
2384 if ((len
+ session_name_len
+ 2 +
2385 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
2386 > sizeof(config_file_path
)) {
2387 ret
= LTTNG_ERR_SET_URL
;
2391 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
2392 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
2394 ret
= LTTNG_ERR_SET_URL
;
2399 * At this point, we know that everything fits in the buffer. Validation
2400 * was done just above.
2402 config_file_path
[len
++] = '/';
2403 strncpy(config_file_path
+ len
, session
->name
, sizeof(config_file_path
) - len
);
2404 len
+= session_name_len
;
2405 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2406 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2407 config_file_path
[len
] = '\0';
2409 if (!attr
->overwrite
) {
2410 file_open_flags
|= O_EXCL
;
2413 fd
= run_as_open(config_file_path
, file_open_flags
,
2414 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
2415 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
2417 PERROR("Could not create configuration file");
2420 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
2423 ret
= LTTNG_ERR_EPERM
;
2426 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2432 writer
= config_writer_create(fd
, 1);
2434 ret
= LTTNG_ERR_NOMEM
;
2438 ret
= config_writer_open_element(writer
, config_element_sessions
);
2440 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2444 ret
= config_writer_open_element(writer
, config_element_session
);
2446 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2450 ret
= config_writer_write_element_string(writer
, config_element_name
,
2453 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2457 if(session
->shm_path
[0] != '\0') {
2458 ret
= config_writer_write_element_string(writer
,
2459 config_element_shared_memory_path
,
2462 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2467 ret
= save_domains(writer
, session
);
2472 ret
= config_writer_write_element_bool(writer
, config_element_started
,
2475 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2479 if (session
->snapshot_mode
|| session
->live_timer
||
2480 session
->rotate_timer_period
|| session
->rotate_size
) {
2481 ret
= config_writer_open_element(writer
, config_element_attributes
);
2483 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2487 if (session
->snapshot_mode
) {
2488 ret
= config_writer_write_element_bool(writer
,
2489 config_element_snapshot_mode
, 1);
2491 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2494 } else if (session
->live_timer
) {
2495 ret
= config_writer_write_element_unsigned_int(writer
,
2496 config_element_live_timer_interval
, session
->live_timer
);
2498 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2502 if (session
->rotate_timer_period
|| session
->rotate_size
) {
2503 ret
= save_session_rotation_schedules(writer
,
2506 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2512 ret
= config_writer_close_element(writer
);
2514 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2519 ret
= save_session_output(writer
, session
);
2525 ret
= config_writer_close_element(writer
);
2527 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2532 ret
= config_writer_close_element(writer
);
2534 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2538 if (writer
&& config_writer_destroy(writer
)) {
2539 /* Preserve the original error code */
2540 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2543 /* Delete file in case of error */
2544 if ((fd
>= 0) && unlink(config_file_path
)) {
2545 PERROR("Unlinking XML session configuration.");
2552 PERROR("Closing XML session configuration");
2559 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
2560 lttng_sock_cred
*creds
)
2563 const char *session_name
;
2564 struct ltt_session
*session
;
2566 session_lock_list();
2568 session_name
= lttng_save_session_attr_get_session_name(attr
);
2570 session
= session_find_by_name(session_name
);
2572 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2576 session_lock(session
);
2577 ret
= save_session(session
, attr
, creds
);
2578 session_unlock(session
);
2579 session_put(session
);
2584 struct ltt_session_list
*list
= session_get_list();
2586 cds_list_for_each_entry(session
, &list
->head
, list
) {
2587 if (!session_get(session
)) {
2590 session_lock(session
);
2591 ret
= save_session(session
, attr
, creds
);
2592 session_unlock(session
);
2593 session_put(session
);
2594 /* Don't abort if we don't have the required permissions. */
2595 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
2603 session_unlock_list();