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
;
298 context_type_string
= NULL
;
301 return context_type_string
;
305 const char *get_ust_context_type_string(
306 enum lttng_ust_context_type context_type
)
308 const char *context_type_string
;
310 switch (context_type
) {
311 case LTTNG_UST_CONTEXT_PROCNAME
:
312 context_type_string
= config_event_context_procname
;
314 case LTTNG_UST_CONTEXT_VPID
:
315 context_type_string
= config_event_context_vpid
;
317 case LTTNG_UST_CONTEXT_VTID
:
318 context_type_string
= config_event_context_vtid
;
320 case LTTNG_UST_CONTEXT_IP
:
321 context_type_string
= config_event_context_ip
;
323 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
324 context_type_string
= config_event_context_pthread_id
;
326 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
327 context_type_string
= config_event_context_app
;
329 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
331 * Error, should not be stored in the XML, perf contexts
332 * are stored as a node of type event_perf_context_type.
335 context_type_string
= NULL
;
339 return context_type_string
;
343 const char *get_buffer_type_string(
344 enum lttng_buffer_type buffer_type
)
346 const char *buffer_type_string
;
348 switch (buffer_type
) {
349 case LTTNG_BUFFER_PER_PID
:
350 buffer_type_string
= config_buffer_type_per_pid
;
352 case LTTNG_BUFFER_PER_UID
:
353 buffer_type_string
= config_buffer_type_per_uid
;
355 case LTTNG_BUFFER_GLOBAL
:
356 buffer_type_string
= config_buffer_type_global
;
359 buffer_type_string
= NULL
;
362 return buffer_type_string
;
366 const char *get_loglevel_type_string(
367 enum lttng_ust_loglevel_type loglevel_type
)
369 const char *loglevel_type_string
;
371 switch (loglevel_type
) {
372 case LTTNG_UST_LOGLEVEL_ALL
:
373 loglevel_type_string
= config_loglevel_type_all
;
375 case LTTNG_UST_LOGLEVEL_RANGE
:
376 loglevel_type_string
= config_loglevel_type_range
;
378 case LTTNG_UST_LOGLEVEL_SINGLE
:
379 loglevel_type_string
= config_loglevel_type_single
;
382 loglevel_type_string
= NULL
;
385 return loglevel_type_string
;
389 int save_kernel_function_event(struct config_writer
*writer
,
390 struct ltt_kernel_event
*event
)
394 ret
= config_writer_open_element(writer
, config_element_function_attributes
);
396 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
400 ret
= config_writer_write_element_string(writer
, config_element_name
,
401 event
->event
->u
.ftrace
.symbol_name
);
403 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
407 /* /function attributes */
408 ret
= config_writer_close_element(writer
);
410 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
418 int save_kernel_kprobe_event(struct config_writer
*writer
,
419 struct ltt_kernel_event
*event
)
422 const char *symbol_name
;
426 switch (event
->event
->instrumentation
) {
427 case LTTNG_KERNEL_KPROBE
:
429 * Comments in lttng-kernel.h mention that
430 * either addr or symbol_name are set, not both.
432 addr
= event
->event
->u
.kprobe
.addr
;
433 offset
= event
->event
->u
.kprobe
.offset
;
434 symbol_name
= addr
? NULL
: event
->event
->u
.kprobe
.symbol_name
;
436 case LTTNG_KERNEL_KRETPROBE
:
437 addr
= event
->event
->u
.kretprobe
.addr
;
438 offset
= event
->event
->u
.kretprobe
.offset
;
439 symbol_name
= addr
? NULL
: event
->event
->u
.kretprobe
.symbol_name
;
443 ERR("Unsupported kernel instrumentation type.");
444 ret
= LTTNG_ERR_INVALID
;
448 ret
= config_writer_open_element(writer
, config_element_probe_attributes
);
450 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
455 ret
= config_writer_write_element_unsigned_int( writer
,
456 config_element_address
, addr
);
458 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
461 } else if (symbol_name
) {
462 ret
= config_writer_write_element_string(writer
,
463 config_element_symbol_name
, symbol_name
);
465 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
468 /* If the offset is non-zero, write it.*/
470 ret
= config_writer_write_element_unsigned_int(writer
,
471 config_element_offset
, offset
);
473 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
479 * This really should not happen as we are either setting the
480 * address or the symbol above.
482 ERR("Invalid probe/function description.");
483 ret
= LTTNG_ERR_INVALID
;
488 ret
= config_writer_close_element(writer
);
490 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
498 * Save the userspace probe tracepoint event associated with the event to the
502 int save_kernel_userspace_probe_tracepoint_event(struct config_writer
*writer
,
503 struct ltt_kernel_event
*event
)
506 const char *probe_name
, *provider_name
, *binary_path
;
507 const struct lttng_userspace_probe_location
*userspace_probe_location
;
508 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
509 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
511 /* Get userspace probe location from the event. */
512 userspace_probe_location
= event
->userspace_probe_location
;
513 if (!userspace_probe_location
) {
514 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
518 /* Get lookup method and lookup method type. */
519 lookup_method
= lttng_userspace_probe_location_get_lookup_method(userspace_probe_location
);
520 if (!lookup_method
) {
521 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
525 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
527 /* Get the binary path, probe name and provider name. */
529 lttng_userspace_probe_location_tracepoint_get_binary_path(
530 userspace_probe_location
);
532 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
537 lttng_userspace_probe_location_tracepoint_get_probe_name(
538 userspace_probe_location
);
540 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
545 lttng_userspace_probe_location_tracepoint_get_provider_name(
546 userspace_probe_location
);
547 if (!provider_name
) {
548 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
552 /* Open a userspace probe tracepoint attribute. */
553 ret
= config_writer_open_element(writer
, config_element_userspace_probe_tracepoint_attributes
);
555 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
559 switch (lookup_type
) {
560 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
561 ret
= config_writer_write_element_string(writer
,
562 config_element_userspace_probe_lookup
,
563 config_element_userspace_probe_lookup_tracepoint_sdt
);
565 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
570 ERR("Unsupported kernel userspace probe tracepoint lookup method.");
571 ret
= LTTNG_ERR_INVALID
;
575 /* Write the binary path, provider name and the probe name. */
576 ret
= config_writer_write_element_string(writer
,
577 config_element_userspace_probe_location_binary_path
,
580 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
584 ret
= config_writer_write_element_string(writer
,
585 config_element_userspace_probe_tracepoint_location_provider_name
,
588 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
592 ret
= config_writer_write_element_string(writer
,
593 config_element_userspace_probe_tracepoint_location_probe_name
,
596 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
600 /* Close the userspace probe tracepoint attribute. */
601 ret
= config_writer_close_element(writer
);
603 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
612 * Save the userspace probe function event associated with the event to the
616 int save_kernel_userspace_probe_function_event(struct config_writer
*writer
,
617 struct ltt_kernel_event
*event
)
620 const char *function_name
, *binary_path
;
621 const struct lttng_userspace_probe_location
*userspace_probe_location
;
622 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
623 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
625 /* Get userspace probe location from the event. */
626 userspace_probe_location
= event
->userspace_probe_location
;
627 if (!userspace_probe_location
) {
628 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
632 /* Get lookup method and lookup method type. */
633 lookup_method
= lttng_userspace_probe_location_get_lookup_method(
634 userspace_probe_location
);
635 if (!lookup_method
) {
636 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
640 /* Get the binary path and the function name. */
642 lttng_userspace_probe_location_function_get_binary_path(
643 userspace_probe_location
);
645 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
650 lttng_userspace_probe_location_function_get_function_name(
651 userspace_probe_location
);
652 if (!function_name
) {
653 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
657 /* Open a userspace probe function attribute. */
658 ret
= config_writer_open_element(writer
,
659 config_element_userspace_probe_function_attributes
);
661 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
665 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
666 switch (lookup_type
) {
667 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
668 ret
= config_writer_write_element_string(writer
,
669 config_element_userspace_probe_lookup
,
670 config_element_userspace_probe_lookup_function_elf
);
672 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
676 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
677 ret
= config_writer_write_element_string(writer
,
678 config_element_userspace_probe_lookup
,
679 config_element_userspace_probe_lookup_function_default
);
681 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
686 ERR("Unsupported kernel userspace probe function lookup method.");
687 ret
= LTTNG_ERR_INVALID
;
691 /* Write the binary path and the function name. */
692 ret
= config_writer_write_element_string(writer
,
693 config_element_userspace_probe_location_binary_path
,
696 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
700 ret
= config_writer_write_element_string(writer
,
701 config_element_userspace_probe_function_location_function_name
,
704 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
708 /* Close the userspace probe function attribute. */
709 ret
= config_writer_close_element(writer
);
711 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
720 int save_kernel_userspace_probe_event(struct config_writer
*writer
,
721 struct ltt_kernel_event
*event
)
724 struct lttng_userspace_probe_location
*userspace_probe_location
;
726 /* Get userspace probe location from the event. */
727 userspace_probe_location
= event
->userspace_probe_location
;
728 if (!userspace_probe_location
) {
729 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
733 switch(lttng_userspace_probe_location_get_type(userspace_probe_location
)) {
734 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
736 ret
= save_kernel_userspace_probe_function_event(writer
, event
);
738 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
743 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
745 ret
= save_kernel_userspace_probe_tracepoint_event(writer
, event
);
747 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
752 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
:
754 ERR("Unsupported kernel userspace probe location type.");
755 ret
= LTTNG_ERR_INVALID
;
764 int save_kernel_event(struct config_writer
*writer
,
765 struct ltt_kernel_event
*event
)
768 const char *instrumentation_type
;
770 ret
= config_writer_open_element(writer
, config_element_event
);
772 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
776 if (event
->event
->name
[0]) {
777 ret
= config_writer_write_element_string(writer
,
778 config_element_name
, event
->event
->name
);
780 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
785 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
788 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
792 instrumentation_type
= get_kernel_instrumentation_string(
793 event
->event
->instrumentation
);
794 if (!instrumentation_type
) {
795 ret
= LTTNG_ERR_INVALID
;
799 ret
= config_writer_write_element_string(writer
, config_element_type
,
800 instrumentation_type
);
802 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
806 if (event
->filter_expression
) {
807 ret
= config_writer_write_element_string(writer
,
808 config_element_filter
,
809 event
->filter_expression
);
811 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
816 if (event
->event
->instrumentation
== LTTNG_KERNEL_FUNCTION
||
817 event
->event
->instrumentation
== LTTNG_KERNEL_KPROBE
||
818 event
->event
->instrumentation
== LTTNG_KERNEL_UPROBE
||
819 event
->event
->instrumentation
== LTTNG_KERNEL_KRETPROBE
) {
821 ret
= config_writer_open_element(writer
,
822 config_element_attributes
);
824 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
828 switch (event
->event
->instrumentation
) {
829 case LTTNG_KERNEL_SYSCALL
:
830 case LTTNG_KERNEL_FUNCTION
:
831 ret
= save_kernel_function_event(writer
, event
);
836 case LTTNG_KERNEL_KPROBE
:
837 case LTTNG_KERNEL_KRETPROBE
:
838 ret
= save_kernel_kprobe_event(writer
, event
);
843 case LTTNG_KERNEL_UPROBE
:
844 ret
= save_kernel_userspace_probe_event(writer
, event
);
850 ERR("Unsupported kernel instrumentation type.");
851 ret
= LTTNG_ERR_INVALID
;
856 ret
= config_writer_close_element(writer
);
858 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
864 ret
= config_writer_close_element(writer
);
866 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
874 int save_kernel_events(struct config_writer
*writer
,
875 struct ltt_kernel_channel
*kchan
)
878 struct ltt_kernel_event
*event
;
880 ret
= config_writer_open_element(writer
, config_element_events
);
882 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
886 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
887 ret
= save_kernel_event(writer
, event
);
894 ret
= config_writer_close_element(writer
);
896 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
904 int save_ust_event(struct config_writer
*writer
,
905 struct ltt_ust_event
*event
)
908 const char *loglevel_type_string
;
910 ret
= config_writer_open_element(writer
, config_element_event
);
912 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
916 if (event
->attr
.name
[0]) {
917 ret
= config_writer_write_element_string(writer
,
918 config_element_name
, event
->attr
.name
);
920 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
925 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
928 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
932 if (event
->attr
.instrumentation
!= LTTNG_UST_TRACEPOINT
) {
933 ERR("Unsupported UST instrumentation type.");
934 ret
= LTTNG_ERR_INVALID
;
937 ret
= config_writer_write_element_string(writer
, config_element_type
,
938 config_event_type_tracepoint
);
940 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
944 loglevel_type_string
= get_loglevel_type_string(
945 event
->attr
.loglevel_type
);
946 if (!loglevel_type_string
) {
947 ERR("Unsupported UST loglevel type.");
948 ret
= LTTNG_ERR_INVALID
;
952 ret
= config_writer_write_element_string(writer
,
953 config_element_loglevel_type
, loglevel_type_string
);
955 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
959 /* The log level is irrelevant if no "filtering" is enabled */
960 if (event
->attr
.loglevel_type
!= LTTNG_UST_LOGLEVEL_ALL
) {
961 ret
= config_writer_write_element_signed_int(writer
,
962 config_element_loglevel
, event
->attr
.loglevel
);
964 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
969 if (event
->filter_expression
) {
970 ret
= config_writer_write_element_string(writer
,
971 config_element_filter
, event
->filter_expression
);
973 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
978 if (event
->exclusion
&& event
->exclusion
->count
) {
981 ret
= config_writer_open_element(writer
,
982 config_element_exclusions
);
984 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
988 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
989 ret
= config_writer_write_element_string(writer
,
990 config_element_exclusion
,
991 LTTNG_EVENT_EXCLUSION_NAME_AT(
992 event
->exclusion
, i
));
994 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1000 ret
= config_writer_close_element(writer
);
1002 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1008 ret
= config_writer_close_element(writer
);
1010 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1018 int save_ust_events(struct config_writer
*writer
,
1019 struct lttng_ht
*events
)
1022 struct ltt_ust_event
*event
;
1023 struct lttng_ht_node_str
*node
;
1024 struct lttng_ht_iter iter
;
1026 ret
= config_writer_open_element(writer
, config_element_events
);
1028 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1033 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1034 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
1036 if (event
->internal
) {
1037 /* Internal events must not be exposed to clients */
1040 ret
= save_ust_event(writer
, event
);
1049 ret
= config_writer_close_element(writer
);
1051 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1059 int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
1060 struct agent_event
*agent_event
)
1063 enum lttng_ust_loglevel_type ust_loglevel_type
;
1065 ust_event
->enabled
= agent_event
->enabled
;
1066 ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1067 if (lttng_strncpy(ust_event
->attr
.name
, agent_event
->name
,
1068 LTTNG_SYMBOL_NAME_LEN
)) {
1072 switch (agent_event
->loglevel_type
) {
1073 case LTTNG_EVENT_LOGLEVEL_ALL
:
1074 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1076 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1077 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
1079 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1080 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
1083 ERR("Invalid agent_event loglevel_type.");
1088 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
1089 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
1090 ust_event
->filter_expression
= agent_event
->filter_expression
;
1091 ust_event
->exclusion
= agent_event
->exclusion
;
1097 int save_agent_events(struct config_writer
*writer
,
1098 struct agent
*agent
)
1101 struct lttng_ht_iter iter
;
1102 struct lttng_ht_node_str
*node
;
1104 ret
= config_writer_open_element(writer
, config_element_events
);
1106 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1111 cds_lfht_for_each_entry(agent
->events
->ht
, &iter
.iter
, node
, node
) {
1113 struct agent_event
*agent_event
;
1114 struct ltt_ust_event fake_event
;
1116 memset(&fake_event
, 0, sizeof(fake_event
));
1117 agent_event
= caa_container_of(node
, struct agent_event
, node
);
1120 * Initialize a fake ust event to reuse the same serialization
1121 * function since UST and agent events contain the same info
1122 * (and one could wonder why they don't reuse the same
1125 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
1130 ret
= save_ust_event(writer
, &fake_event
);
1139 ret
= config_writer_close_element(writer
);
1141 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1149 int save_kernel_context(struct config_writer
*writer
,
1150 struct lttng_kernel_context
*ctx
)
1158 ret
= config_writer_open_element(writer
, config_element_context
);
1160 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1164 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
1165 ret
= config_writer_open_element(writer
,
1166 config_element_context_perf
);
1168 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1172 ret
= config_writer_write_element_unsigned_int(writer
,
1173 config_element_type
, ctx
->u
.perf_counter
.type
);
1175 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1179 ret
= config_writer_write_element_unsigned_int(writer
,
1180 config_element_config
, ctx
->u
.perf_counter
.config
);
1182 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1186 ret
= config_writer_write_element_string(writer
,
1187 config_element_name
, ctx
->u
.perf_counter
.name
);
1189 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1194 ret
= config_writer_close_element(writer
);
1196 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1200 const char *context_type_string
=
1201 get_kernel_context_type_string(ctx
->ctx
);
1203 if (!context_type_string
) {
1204 ERR("Unsupported kernel context type.");
1205 ret
= LTTNG_ERR_INVALID
;
1209 ret
= config_writer_write_element_string(writer
,
1210 config_element_type
, context_type_string
);
1212 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1218 ret
= config_writer_close_element(writer
);
1220 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1229 int save_kernel_contexts(struct config_writer
*writer
,
1230 struct ltt_kernel_channel
*kchan
)
1233 struct ltt_kernel_context
*ctx
;
1235 if (cds_list_empty(&kchan
->ctx_list
)) {
1240 ret
= config_writer_open_element(writer
, config_element_contexts
);
1242 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1246 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
1247 ret
= save_kernel_context(writer
, &ctx
->ctx
);
1254 ret
= config_writer_close_element(writer
);
1256 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1264 int save_ust_context_perf_thread_counter(struct config_writer
*writer
,
1265 struct ltt_ust_context
*ctx
)
1272 /* Perf contexts are saved as event_perf_context_type */
1273 ret
= config_writer_open_element(writer
, config_element_context_perf
);
1275 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1279 ret
= config_writer_write_element_unsigned_int(writer
,
1280 config_element_type
, ctx
->ctx
.u
.perf_counter
.type
);
1282 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1286 ret
= config_writer_write_element_unsigned_int(writer
,
1287 config_element_config
, ctx
->ctx
.u
.perf_counter
.config
);
1289 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1293 ret
= config_writer_write_element_string(writer
, config_element_name
,
1294 ctx
->ctx
.u
.perf_counter
.name
);
1296 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1301 ret
= config_writer_close_element(writer
);
1303 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1311 int save_ust_context_app_ctx(struct config_writer
*writer
,
1312 struct ltt_ust_context
*ctx
)
1319 /* Application contexts are saved as application_context_type */
1320 ret
= config_writer_open_element(writer
, config_element_context_app
);
1322 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1326 ret
= config_writer_write_element_string(writer
,
1327 config_element_context_app_provider_name
,
1328 ctx
->ctx
.u
.app_ctx
.provider_name
);
1330 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1334 ret
= config_writer_write_element_string(writer
,
1335 config_element_context_app_ctx_name
,
1336 ctx
->ctx
.u
.app_ctx
.ctx_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_generic(struct config_writer
*writer
,
1354 struct ltt_ust_context
*ctx
)
1357 const char *context_type_string
;
1362 /* Save context as event_context_type_type */
1363 context_type_string
= get_ust_context_type_string(
1365 if (!context_type_string
) {
1366 ERR("Unsupported UST context type.");
1367 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1371 ret
= config_writer_write_element_string(writer
,
1372 config_element_type
, context_type_string
);
1374 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1382 int save_ust_context(struct config_writer
*writer
,
1383 struct cds_list_head
*ctx_list
)
1386 struct ltt_ust_context
*ctx
;
1391 ret
= config_writer_open_element(writer
, config_element_contexts
);
1393 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1397 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
1398 ret
= config_writer_open_element(writer
,
1399 config_element_context
);
1401 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1405 switch (ctx
->ctx
.ctx
) {
1406 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
1407 ret
= save_ust_context_perf_thread_counter(writer
, ctx
);
1409 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1410 ret
= save_ust_context_app_ctx(writer
, ctx
);
1413 /* Save generic context. */
1414 ret
= save_ust_context_generic(writer
, ctx
);
1421 ret
= config_writer_close_element(writer
);
1423 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1429 ret
= config_writer_close_element(writer
);
1431 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1439 int save_kernel_channel(struct config_writer
*writer
,
1440 struct ltt_kernel_channel
*kchan
)
1447 ret
= config_writer_open_element(writer
, config_element_channel
);
1449 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1453 ret
= config_writer_write_element_string(writer
, config_element_name
,
1454 kchan
->channel
->name
);
1456 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1460 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1461 kchan
->channel
->enabled
);
1463 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1467 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1472 ret
= save_kernel_events(writer
, kchan
);
1477 ret
= save_kernel_contexts(writer
, kchan
);
1483 ret
= config_writer_close_element(writer
);
1485 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1493 int save_ust_channel(struct config_writer
*writer
,
1494 struct ltt_ust_channel
*ust_chan
,
1495 struct ltt_ust_session
*session
)
1503 ret
= config_writer_open_element(writer
, config_element_channel
);
1505 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1509 ret
= config_writer_write_element_string(writer
, config_element_name
,
1512 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1516 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1519 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1523 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1528 ret
= config_writer_write_element_unsigned_int(writer
,
1529 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1531 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1535 ret
= config_writer_write_element_unsigned_int(writer
,
1536 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1538 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1542 ret
= config_writer_write_element_unsigned_int(writer
,
1543 config_element_live_timer_interval
,
1544 session
->live_timer_interval
);
1546 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1550 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1551 ret
= save_ust_events(writer
, ust_chan
->events
);
1556 struct agent
*agent
= NULL
;
1558 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1560 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1561 ERR("Could not find agent associated to UST subdomain");
1566 * Channels associated with a UST sub-domain (such as JUL, Log4j
1567 * or Python) don't have any non-internal events. We retrieve
1568 * the "agent" events associated with this channel and serialize
1571 ret
= save_agent_events(writer
, agent
);
1577 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1583 ret
= config_writer_close_element(writer
);
1585 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1593 int save_kernel_session(struct config_writer
*writer
,
1594 struct ltt_session
*session
)
1597 struct ltt_kernel_channel
*kchan
;
1602 ret
= config_writer_write_element_string(writer
, config_element_type
,
1603 config_domain_type_kernel
);
1605 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1609 ret
= config_writer_write_element_string(writer
,
1610 config_element_buffer_type
, config_buffer_type_global
);
1612 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1616 ret
= config_writer_open_element(writer
,
1617 config_element_channels
);
1619 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1623 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1625 ret
= save_kernel_channel(writer
, kchan
);
1632 ret
= config_writer_close_element(writer
);
1634 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1642 const char *get_config_domain_str(enum lttng_domain_type domain
)
1644 const char *str_dom
;
1647 case LTTNG_DOMAIN_KERNEL
:
1648 str_dom
= config_domain_type_kernel
;
1650 case LTTNG_DOMAIN_UST
:
1651 str_dom
= config_domain_type_ust
;
1653 case LTTNG_DOMAIN_JUL
:
1654 str_dom
= config_domain_type_jul
;
1656 case LTTNG_DOMAIN_LOG4J
:
1657 str_dom
= config_domain_type_log4j
;
1659 case LTTNG_DOMAIN_PYTHON
:
1660 str_dom
= config_domain_type_python
;
1670 int save_pid_tracker(struct config_writer
*writer
,
1671 struct ltt_session
*sess
, int domain
)
1674 ssize_t nr_pids
= 0, i
;
1675 int32_t *pids
= NULL
;
1678 case LTTNG_DOMAIN_KERNEL
:
1680 nr_pids
= kernel_list_tracker_pids(sess
->kernel_session
, &pids
);
1682 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1687 case LTTNG_DOMAIN_UST
:
1689 nr_pids
= trace_ust_list_tracker_pids(sess
->ust_session
, &pids
);
1691 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1696 case LTTNG_DOMAIN_JUL
:
1697 case LTTNG_DOMAIN_LOG4J
:
1698 case LTTNG_DOMAIN_PYTHON
:
1700 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1704 /* Only create a pid_tracker if enabled or untrack all */
1705 if (nr_pids
!= 1 || (nr_pids
== 1 && pids
[0] != -1)) {
1706 ret
= config_writer_open_element(writer
,
1707 config_element_pid_tracker
);
1709 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1713 ret
= config_writer_open_element(writer
,
1714 config_element_targets
);
1716 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1720 for (i
= 0; i
< nr_pids
; i
++) {
1721 ret
= config_writer_open_element(writer
,
1722 config_element_target_pid
);
1724 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1728 ret
= config_writer_write_element_unsigned_int(writer
,
1729 config_element_pid
, pids
[i
]);
1731 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1736 ret
= config_writer_close_element(writer
);
1738 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1744 ret
= config_writer_close_element(writer
);
1746 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1751 ret
= config_writer_close_element(writer
);
1753 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1763 int save_ust_domain(struct config_writer
*writer
,
1764 struct ltt_session
*session
, enum lttng_domain_type domain
)
1767 struct ltt_ust_channel
*ust_chan
;
1768 const char *buffer_type_string
;
1769 struct lttng_ht_node_str
*node
;
1770 struct lttng_ht_iter iter
;
1771 const char *config_domain_name
;
1776 ret
= config_writer_open_element(writer
,
1777 config_element_domain
);
1779 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1783 config_domain_name
= get_config_domain_str(domain
);
1784 if (!config_domain_name
) {
1785 ret
= LTTNG_ERR_INVALID
;
1789 ret
= config_writer_write_element_string(writer
,
1790 config_element_type
, config_domain_name
);
1792 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1796 buffer_type_string
= get_buffer_type_string(
1797 session
->ust_session
->buffer_type
);
1798 if (!buffer_type_string
) {
1799 ERR("Unsupported buffer type.");
1800 ret
= LTTNG_ERR_INVALID
;
1804 ret
= config_writer_write_element_string(writer
,
1805 config_element_buffer_type
, buffer_type_string
);
1807 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1811 ret
= config_writer_open_element(writer
, config_element_channels
);
1813 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1818 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1819 &iter
.iter
, node
, node
) {
1820 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1821 if (domain
== ust_chan
->domain
) {
1822 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1832 ret
= config_writer_close_element(writer
);
1834 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1838 if (domain
== LTTNG_DOMAIN_UST
) {
1839 ret
= config_writer_open_element(writer
,
1840 config_element_trackers
);
1842 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1846 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_UST
);
1852 ret
= config_writer_close_element(writer
);
1859 ret
= config_writer_close_element(writer
);
1861 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1870 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1877 if (!session
->kernel_session
&& !session
->ust_session
) {
1881 ret
= config_writer_open_element(writer
, config_element_domains
);
1883 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1888 if (session
->kernel_session
) {
1889 ret
= config_writer_open_element(writer
,
1890 config_element_domain
);
1892 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1896 ret
= save_kernel_session(writer
, session
);
1901 ret
= config_writer_open_element(writer
,
1902 config_element_trackers
);
1904 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1908 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_KERNEL
);
1914 ret
= config_writer_close_element(writer
);
1916 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1920 ret
= config_writer_close_element(writer
);
1922 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1927 if (session
->ust_session
) {
1928 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
1933 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
1938 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
1943 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
1950 ret
= config_writer_close_element(writer
);
1952 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1960 int save_consumer_output(struct config_writer
*writer
,
1961 struct consumer_output
*output
)
1968 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1970 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1974 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1977 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1981 ret
= config_writer_open_element(writer
, config_element_destination
);
1983 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1987 switch (output
->type
) {
1988 case CONSUMER_DST_LOCAL
:
1989 ret
= config_writer_write_element_string(writer
,
1990 config_element_path
, output
->dst
.session_root_path
);
1992 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1996 case CONSUMER_DST_NET
:
2000 uri
= zmalloc(PATH_MAX
);
2002 ret
= LTTNG_ERR_NOMEM
;
2006 ret
= config_writer_open_element(writer
, config_element_net_output
);
2008 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2009 goto end_net_output
;
2012 if (output
->dst
.net
.control_isset
&&
2013 output
->dst
.net
.data_isset
) {
2014 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
2016 ret
= LTTNG_ERR_INVALID
;
2017 goto end_net_output
;
2020 ret
= config_writer_write_element_string(writer
,
2021 config_element_control_uri
, uri
);
2023 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2024 goto end_net_output
;
2027 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
2029 ret
= LTTNG_ERR_INVALID
;
2030 goto end_net_output
;
2033 ret
= config_writer_write_element_string(writer
,
2034 config_element_data_uri
, uri
);
2036 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2037 goto end_net_output
;
2046 ret
= !output
->dst
.net
.control_isset
?
2047 LTTNG_ERR_URL_CTRL_MISS
:
2048 LTTNG_ERR_URL_DATA_MISS
;
2053 ret
= config_writer_close_element(writer
);
2055 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2061 ERR("Unsupported consumer output type.");
2062 ret
= LTTNG_ERR_INVALID
;
2067 ret
= config_writer_close_element(writer
);
2069 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2073 /* /consumer_output */
2074 ret
= config_writer_close_element(writer
);
2076 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2084 int save_snapshot_outputs(struct config_writer
*writer
,
2085 struct snapshot
*snapshot
)
2088 struct lttng_ht_iter iter
;
2089 struct snapshot_output
*output
;
2094 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
2096 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2101 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
2103 ret
= config_writer_open_element(writer
,
2104 config_element_output
);
2106 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2110 ret
= config_writer_write_element_string(writer
,
2111 config_element_name
, output
->name
);
2113 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2117 ret
= config_writer_write_element_unsigned_int(writer
,
2118 config_element_max_size
, output
->max_size
);
2120 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2124 ret
= save_consumer_output(writer
, output
->consumer
);
2130 ret
= config_writer_close_element(writer
);
2132 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2138 /* /snapshot_outputs */
2139 ret
= config_writer_close_element(writer
);
2141 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2153 int save_session_output(struct config_writer
*writer
,
2154 struct ltt_session
*session
)
2161 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
2162 (!session
->snapshot_mode
&& !session
->consumer
)) {
2163 /* Session is in no output mode */
2168 ret
= config_writer_open_element(writer
, config_element_output
);
2170 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2174 if (session
->snapshot_mode
) {
2175 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
2180 if (session
->consumer
) {
2181 ret
= save_consumer_output(writer
, session
->consumer
);
2189 ret
= config_writer_close_element(writer
);
2191 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2199 int save_session_rotation_schedule(struct config_writer
*writer
,
2200 enum lttng_rotation_schedule_type type
, uint64_t value
)
2203 const char *element_name
;
2204 const char *value_name
;
2207 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
2208 element_name
= config_element_rotation_schedule_periodic
;
2209 value_name
= config_element_rotation_schedule_periodic_time_us
;
2211 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
2212 element_name
= config_element_rotation_schedule_size_threshold
;
2213 value_name
= config_element_rotation_schedule_size_threshold_bytes
;
2220 ret
= config_writer_open_element(writer
, element_name
);
2225 ret
= config_writer_write_element_unsigned_int(writer
,
2231 /* Close schedule descriptor element. */
2232 ret
= config_writer_close_element(writer
);
2241 int save_session_rotation_schedules(struct config_writer
*writer
,
2242 struct ltt_session
*session
)
2246 ret
= config_writer_open_element(writer
,
2247 config_element_rotation_schedules
);
2251 if (session
->rotate_timer_period
) {
2252 ret
= save_session_rotation_schedule(writer
,
2253 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
,
2254 session
->rotate_timer_period
);
2256 goto close_schedules
;
2259 if (session
->rotate_size
) {
2260 ret
= save_session_rotation_schedule(writer
,
2261 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
,
2262 session
->rotate_size
);
2264 goto close_schedules
;
2269 /* Close rotation schedules element. */
2270 ret
= config_writer_close_element(writer
);
2279 * Save the given session.
2281 * Return 0 on success else a LTTNG_ERR* code.
2284 int save_session(struct ltt_session
*session
,
2285 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
2288 char config_file_path
[LTTNG_PATH_MAX
];
2290 struct config_writer
*writer
= NULL
;
2291 size_t session_name_len
;
2292 const char *provided_path
;
2298 session_name_len
= strlen(session
->name
);
2299 memset(config_file_path
, 0, sizeof(config_file_path
));
2301 if (!session_access_ok(session
,
2302 LTTNG_SOCK_GET_UID_CRED(creds
),
2303 LTTNG_SOCK_GET_GID_CRED(creds
)) || session
->destroyed
) {
2304 ret
= LTTNG_ERR_EPERM
;
2308 provided_path
= lttng_save_session_attr_get_output_url(attr
);
2309 if (provided_path
) {
2310 DBG3("Save session in provided path %s", provided_path
);
2311 len
= strlen(provided_path
);
2312 if (len
>= sizeof(config_file_path
)) {
2313 ret
= LTTNG_ERR_SET_URL
;
2316 strncpy(config_file_path
, provided_path
, sizeof(config_file_path
));
2319 char *home_dir
= utils_get_user_home_dir(
2320 LTTNG_SOCK_GET_UID_CRED(creds
));
2322 ret
= LTTNG_ERR_SET_URL
;
2326 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
2327 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
2330 PERROR("snprintf save session");
2331 ret
= LTTNG_ERR_SET_URL
;
2338 * Check the path fits in the config file path dst including the '/'
2339 * followed by trailing .lttng extension and the NULL terminated string.
2341 if ((len
+ session_name_len
+ 2 +
2342 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
2343 > sizeof(config_file_path
)) {
2344 ret
= LTTNG_ERR_SET_URL
;
2348 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
2349 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
2351 ret
= LTTNG_ERR_SET_URL
;
2356 * At this point, we know that everything fits in the buffer. Validation
2357 * was done just above.
2359 config_file_path
[len
++] = '/';
2360 strncpy(config_file_path
+ len
, session
->name
, sizeof(config_file_path
) - len
);
2361 len
+= session_name_len
;
2362 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2363 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
2364 config_file_path
[len
] = '\0';
2366 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
2367 /* File exists, notify the user since the overwrite flag is off. */
2368 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
2372 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2373 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
2374 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
2376 PERROR("Could not create configuration file");
2377 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2381 writer
= config_writer_create(fd
, 1);
2383 ret
= LTTNG_ERR_NOMEM
;
2387 ret
= config_writer_open_element(writer
, config_element_sessions
);
2389 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2393 ret
= config_writer_open_element(writer
, config_element_session
);
2395 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2399 ret
= config_writer_write_element_string(writer
, config_element_name
,
2402 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2406 if(session
->shm_path
[0] != '\0') {
2407 ret
= config_writer_write_element_string(writer
,
2408 config_element_shared_memory_path
,
2411 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2416 ret
= save_domains(writer
, session
);
2421 ret
= config_writer_write_element_bool(writer
, config_element_started
,
2424 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2428 if (session
->snapshot_mode
|| session
->live_timer
||
2429 session
->rotate_timer_period
|| session
->rotate_size
) {
2430 ret
= config_writer_open_element(writer
, config_element_attributes
);
2432 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2436 if (session
->snapshot_mode
) {
2437 ret
= config_writer_write_element_bool(writer
,
2438 config_element_snapshot_mode
, 1);
2440 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2443 } else if (session
->live_timer
) {
2444 ret
= config_writer_write_element_unsigned_int(writer
,
2445 config_element_live_timer_interval
, session
->live_timer
);
2447 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2451 if (session
->rotate_timer_period
|| session
->rotate_size
) {
2452 ret
= save_session_rotation_schedules(writer
,
2455 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2461 ret
= config_writer_close_element(writer
);
2463 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2468 ret
= save_session_output(writer
, session
);
2474 ret
= config_writer_close_element(writer
);
2476 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2481 ret
= config_writer_close_element(writer
);
2483 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2487 if (writer
&& config_writer_destroy(writer
)) {
2488 /* Preserve the original error code */
2489 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2492 /* Delete file in case of error */
2493 if ((fd
>= 0) && unlink(config_file_path
)) {
2494 PERROR("Unlinking XML session configuration.");
2501 PERROR("Closing XML session configuration");
2508 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
2509 lttng_sock_cred
*creds
)
2512 const char *session_name
;
2513 struct ltt_session
*session
;
2515 session_lock_list();
2517 session_name
= lttng_save_session_attr_get_session_name(attr
);
2519 session
= session_find_by_name(session_name
);
2521 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2525 session_lock(session
);
2526 ret
= save_session(session
, attr
, creds
);
2527 session_unlock(session
);
2528 session_put(session
);
2533 struct ltt_session_list
*list
= session_get_list();
2535 cds_list_for_each_entry(session
, &list
->head
, list
) {
2536 if (!session_get(session
)) {
2539 session_lock(session
);
2540 ret
= save_session(session
, attr
, creds
);
2541 session_unlock(session
);
2542 session_put(session
);
2543 /* Don't abort if we don't have the required permissions. */
2544 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
2552 session_unlock_list();