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.
23 #include <urcu/uatomic.h>
26 #include <common/defaults.h>
27 #include <common/error.h>
28 #include <common/config/config.h>
29 #include <common/utils.h>
30 #include <common/runas.h>
31 #include <lttng/save-internal.h>
36 #include "trace-ust.h"
39 int save_kernel_channel_attributes(struct config_writer
*writer
,
40 struct lttng_channel_attr
*attr
)
44 ret
= config_writer_write_element_string(writer
,
45 config_element_overwrite_mode
,
46 attr
->overwrite
? config_overwrite_mode_overwrite
:
47 config_overwrite_mode_discard
);
52 ret
= config_writer_write_element_unsigned_int(writer
,
53 config_element_subbuf_size
, attr
->subbuf_size
);
58 ret
= config_writer_write_element_unsigned_int(writer
,
59 config_element_num_subbuf
,
65 ret
= config_writer_write_element_unsigned_int(writer
,
66 config_element_switch_timer_interval
,
67 attr
->switch_timer_interval
);
72 ret
= config_writer_write_element_unsigned_int(writer
,
73 config_element_read_timer_interval
,
74 attr
->read_timer_interval
);
79 ret
= config_writer_write_element_string(writer
,
80 config_element_output_type
,
81 attr
->output
== LTTNG_EVENT_SPLICE
?
82 config_output_type_splice
: config_output_type_mmap
);
87 ret
= config_writer_write_element_unsigned_int(writer
,
88 config_element_tracefile_size
, attr
->tracefile_size
);
93 ret
= config_writer_write_element_unsigned_int(writer
,
94 config_element_tracefile_count
,
95 attr
->tracefile_count
);
100 ret
= config_writer_write_element_unsigned_int(writer
,
101 config_element_live_timer_interval
,
102 attr
->live_timer_interval
);
107 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
111 int save_ust_channel_attributes(struct config_writer
*writer
,
112 struct lttng_ust_channel_attr
*attr
)
116 ret
= config_writer_write_element_string(writer
,
117 config_element_overwrite_mode
,
118 attr
->overwrite
? config_overwrite_mode_overwrite
:
119 config_overwrite_mode_discard
);
124 ret
= config_writer_write_element_unsigned_int(writer
,
125 config_element_subbuf_size
, attr
->subbuf_size
);
130 ret
= config_writer_write_element_unsigned_int(writer
,
131 config_element_num_subbuf
,
137 ret
= config_writer_write_element_unsigned_int(writer
,
138 config_element_switch_timer_interval
,
139 attr
->switch_timer_interval
);
144 ret
= config_writer_write_element_unsigned_int(writer
,
145 config_element_read_timer_interval
,
146 attr
->read_timer_interval
);
151 ret
= config_writer_write_element_string(writer
,
152 config_element_output_type
,
153 attr
->output
== LTTNG_UST_MMAP
?
154 config_output_type_mmap
: config_output_type_splice
);
159 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
163 const char *get_kernel_instrumentation_string(
164 enum lttng_kernel_instrumentation instrumentation
)
166 const char *instrumentation_string
;
168 switch (instrumentation
) {
169 case LTTNG_KERNEL_ALL
:
170 instrumentation_string
= config_event_type_all
;
172 case LTTNG_KERNEL_TRACEPOINT
:
173 instrumentation_string
= config_event_type_tracepoint
;
175 case LTTNG_KERNEL_KPROBE
:
176 instrumentation_string
= config_event_type_kprobe
;
178 case LTTNG_KERNEL_FUNCTION
:
179 instrumentation_string
= config_event_type_function
;
181 case LTTNG_KERNEL_KRETPROBE
:
182 instrumentation_string
= config_event_type_kretprobe
;
184 case LTTNG_KERNEL_NOOP
:
185 instrumentation_string
= config_event_type_noop
;
187 case LTTNG_KERNEL_SYSCALL
:
188 instrumentation_string
= config_event_type_syscall
;
191 instrumentation_string
= NULL
;
194 return instrumentation_string
;
198 const char *get_kernel_context_type_string(
199 enum lttng_kernel_context_type context_type
)
201 const char *context_type_string
;
203 switch (context_type
) {
204 case LTTNG_KERNEL_CONTEXT_PID
:
205 context_type_string
= config_event_context_pid
;
207 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
208 context_type_string
= config_event_context_procname
;
210 case LTTNG_KERNEL_CONTEXT_PRIO
:
211 context_type_string
= config_event_context_prio
;
213 case LTTNG_KERNEL_CONTEXT_NICE
:
214 context_type_string
= config_event_context_nice
;
216 case LTTNG_KERNEL_CONTEXT_VPID
:
217 context_type_string
= config_event_context_vpid
;
219 case LTTNG_KERNEL_CONTEXT_TID
:
220 context_type_string
= config_event_context_tid
;
222 case LTTNG_KERNEL_CONTEXT_VTID
:
223 context_type_string
= config_event_context_vtid
;
225 case LTTNG_KERNEL_CONTEXT_PPID
:
226 context_type_string
= config_event_context_ppid
;
228 case LTTNG_KERNEL_CONTEXT_VPPID
:
229 context_type_string
= config_event_context_vppid
;
231 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
232 context_type_string
= config_event_context_hostname
;
235 context_type_string
= NULL
;
238 return context_type_string
;
242 const char *get_ust_context_type_string(
243 enum lttng_ust_context_type context_type
)
245 const char *context_type_string
;
247 switch (context_type
) {
248 case LTTNG_UST_CONTEXT_PROCNAME
:
249 context_type_string
= config_event_context_procname
;
251 case LTTNG_UST_CONTEXT_VPID
:
252 context_type_string
= config_event_context_vpid
;
254 case LTTNG_UST_CONTEXT_VTID
:
255 context_type_string
= config_event_context_vtid
;
257 case LTTNG_UST_CONTEXT_IP
:
258 context_type_string
= config_event_context_ip
;
260 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
261 context_type_string
= config_event_context_pthread_id
;
263 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
265 * Error, should not be stored in the XML, perf contexts
266 * are stored as a node of type event_perf_context_type.
269 context_type_string
= NULL
;
273 return context_type_string
;
277 const char *get_buffer_type_string(
278 enum lttng_buffer_type buffer_type
)
280 const char *buffer_type_string
;
282 switch (buffer_type
) {
283 case LTTNG_BUFFER_PER_PID
:
284 buffer_type_string
= config_buffer_type_per_pid
;
286 case LTTNG_BUFFER_PER_UID
:
287 buffer_type_string
= config_buffer_type_per_uid
;
289 case LTTNG_BUFFER_GLOBAL
:
290 buffer_type_string
= config_buffer_type_global
;
293 buffer_type_string
= NULL
;
296 return buffer_type_string
;
300 const char *get_loglevel_type_string(
301 enum lttng_ust_loglevel_type loglevel_type
)
303 const char *loglevel_type_string
;
305 switch (loglevel_type
) {
306 case LTTNG_UST_LOGLEVEL_ALL
:
307 loglevel_type_string
= config_loglevel_type_all
;
309 case LTTNG_UST_LOGLEVEL_RANGE
:
310 loglevel_type_string
= config_loglevel_type_range
;
312 case LTTNG_UST_LOGLEVEL_SINGLE
:
313 loglevel_type_string
= config_loglevel_type_single
;
316 loglevel_type_string
= NULL
;
319 return loglevel_type_string
;
323 int save_kernel_event(struct config_writer
*writer
,
324 struct ltt_kernel_event
*event
)
327 const char *instrumentation_type
;
329 ret
= config_writer_open_element(writer
, config_element_event
);
331 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
335 if (event
->event
->name
[0]) {
336 ret
= config_writer_write_element_string(writer
,
337 config_element_name
, event
->event
->name
);
339 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
344 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
347 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
351 instrumentation_type
= get_kernel_instrumentation_string(
352 event
->event
->instrumentation
);
353 if (!instrumentation_type
) {
354 ret
= LTTNG_ERR_INVALID
;
358 ret
= config_writer_write_element_string(writer
, config_element_type
,
359 instrumentation_type
);
361 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
365 if (event
->event
->instrumentation
== LTTNG_KERNEL_FUNCTION
||
366 event
->event
->instrumentation
== LTTNG_KERNEL_KPROBE
||
367 event
->event
->instrumentation
== LTTNG_KERNEL_KRETPROBE
) {
369 ret
= config_writer_open_element(writer
,
370 config_element_attributes
);
372 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
376 switch (event
->event
->instrumentation
) {
377 case LTTNG_KERNEL_SYSCALL
:
378 case LTTNG_KERNEL_FUNCTION
:
379 ret
= config_writer_open_element(writer
,
380 config_element_function_attributes
);
382 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
386 ret
= config_writer_write_element_string(writer
,
388 event
->event
->u
.ftrace
.symbol_name
);
390 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
394 /* /function attributes */
395 ret
= config_writer_close_element(writer
);
397 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
401 case LTTNG_KERNEL_KPROBE
:
402 case LTTNG_KERNEL_KRETPROBE
:
404 const char *symbol_name
;
408 if (event
->event
->instrumentation
==
409 LTTNG_KERNEL_KPROBE
) {
411 * Comments in lttng-kernel.h mention that
412 * either addr or symbol_name are set, not both.
414 addr
= event
->event
->u
.kprobe
.addr
;
415 offset
= event
->event
->u
.kprobe
.offset
;
416 symbol_name
= addr
? NULL
:
417 event
->event
->u
.kprobe
.symbol_name
;
420 event
->event
->u
.kretprobe
.symbol_name
;
421 addr
= event
->event
->u
.kretprobe
.addr
;
422 offset
= event
->event
->u
.kretprobe
.offset
;
425 ret
= config_writer_open_element(writer
,
426 config_element_probe_attributes
);
428 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
433 ret
= config_writer_write_element_string(writer
,
434 config_element_symbol_name
,
437 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
443 ret
= config_writer_write_element_unsigned_int(
444 writer
, config_element_address
, addr
);
446 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
452 ret
= config_writer_write_element_unsigned_int(
453 writer
, config_element_offset
, offset
);
455 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
460 ret
= config_writer_close_element(writer
);
462 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
468 ERR("Unsupported kernel instrumentation type.");
469 ret
= LTTNG_ERR_INVALID
;
474 ret
= config_writer_close_element(writer
);
476 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
482 ret
= config_writer_close_element(writer
);
484 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
492 int save_kernel_syscall(struct config_writer
*writer
,
493 struct ltt_kernel_channel
*kchan
)
497 struct lttng_event
*events
= NULL
;
502 count
= syscall_list_channel(kchan
, &events
, 0);
504 /* No syscalls, just gracefully return. */
509 for (i
= 0; i
< count
; i
++) {
510 struct ltt_kernel_event
*kevent
;
512 /* Create a temporary kevent in order to save it. */
513 kevent
= trace_kernel_create_event(&events
[i
]);
518 /* Init list in order so the destroy call can del the node. */
519 CDS_INIT_LIST_HEAD(&kevent
->list
);
521 ret
= save_kernel_event(writer
, kevent
);
522 trace_kernel_destroy_event(kevent
);
528 /* Everything went well */
537 int save_kernel_events(struct config_writer
*writer
,
538 struct ltt_kernel_channel
*kchan
)
541 struct ltt_kernel_event
*event
;
543 ret
= config_writer_open_element(writer
, config_element_events
);
545 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
549 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
550 ret
= save_kernel_event(writer
, event
);
556 /* Save syscalls if any. */
557 ret
= save_kernel_syscall(writer
, kchan
);
563 ret
= config_writer_close_element(writer
);
565 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
573 int save_ust_event(struct config_writer
*writer
,
574 struct ltt_ust_event
*event
)
577 const char *loglevel_type_string
;
579 ret
= config_writer_open_element(writer
, config_element_event
);
581 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
585 if (event
->attr
.name
[0]) {
586 ret
= config_writer_write_element_string(writer
,
587 config_element_name
, event
->attr
.name
);
589 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
594 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
597 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
601 if (event
->attr
.instrumentation
!= LTTNG_UST_TRACEPOINT
) {
602 ERR("Unsupported UST instrumentation type.");
603 ret
= LTTNG_ERR_INVALID
;
606 ret
= config_writer_write_element_string(writer
, config_element_type
,
607 config_event_type_tracepoint
);
609 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
613 loglevel_type_string
= get_loglevel_type_string(
614 event
->attr
.loglevel_type
);
615 if (!loglevel_type_string
) {
616 ERR("Unsupported UST loglevel type.");
617 ret
= LTTNG_ERR_INVALID
;
621 ret
= config_writer_write_element_string(writer
,
622 config_element_loglevel_type
, loglevel_type_string
);
624 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
628 ret
= config_writer_write_element_signed_int(writer
,
629 config_element_loglevel
, event
->attr
.loglevel
);
631 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
635 if (event
->filter_expression
) {
636 ret
= config_writer_write_element_string(writer
,
637 config_element_filter
, event
->filter_expression
);
639 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
644 if (event
->exclusion
&& event
->exclusion
->count
) {
647 ret
= config_writer_open_element(writer
,
648 config_element_exclusions
);
650 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
654 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
655 ret
= config_writer_write_element_string(writer
,
656 config_element_exclusion
,
657 &event
->exclusion
->names
[0][i
]);
659 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
665 ret
= config_writer_close_element(writer
);
667 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
673 ret
= config_writer_close_element(writer
);
675 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
683 int save_ust_events(struct config_writer
*writer
,
684 struct lttng_ht
*events
)
687 struct ltt_ust_event
*event
;
688 struct lttng_ht_node_str
*node
;
689 struct lttng_ht_iter iter
;
691 ret
= config_writer_open_element(writer
, config_element_events
);
693 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
698 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
699 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
701 ret
= save_ust_event(writer
, event
);
710 ret
= config_writer_close_element(writer
);
712 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
719 /* TODO: save/restore tracker pid */
722 int save_kernel_context(struct config_writer
*writer
,
723 struct lttng_kernel_context
*ctx
)
731 ret
= config_writer_open_element(writer
, config_element_context
);
733 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
737 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
738 ret
= config_writer_open_element(writer
, config_element_perf
);
740 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
744 ret
= config_writer_write_element_unsigned_int(writer
,
745 config_element_type
, ctx
->u
.perf_counter
.type
);
747 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
751 ret
= config_writer_write_element_unsigned_int(writer
,
752 config_element_config
, ctx
->u
.perf_counter
.config
);
754 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
758 ret
= config_writer_write_element_string(writer
,
759 config_element_name
, ctx
->u
.perf_counter
.name
);
761 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
766 ret
= config_writer_close_element(writer
);
768 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
772 const char *context_type_string
=
773 get_kernel_context_type_string(ctx
->ctx
);
775 if (!context_type_string
) {
776 ERR("Unsupported kernel context type.");
777 ret
= LTTNG_ERR_INVALID
;
781 ret
= config_writer_write_element_string(writer
,
782 config_element_type
, context_type_string
);
784 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
790 ret
= config_writer_close_element(writer
);
792 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
801 int save_kernel_contexts(struct config_writer
*writer
,
802 struct ltt_kernel_channel
*kchan
)
805 struct ltt_kernel_context
*ctx
;
807 if (cds_list_empty(&kchan
->ctx_list
)) {
812 ret
= config_writer_open_element(writer
, config_element_contexts
);
814 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
818 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
819 ret
= save_kernel_context(writer
, &ctx
->ctx
);
826 ret
= config_writer_close_element(writer
);
828 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
836 int save_ust_context(struct config_writer
*writer
,
837 struct cds_list_head
*ctx_list
)
840 struct ltt_ust_context
*ctx
;
845 ret
= config_writer_open_element(writer
, config_element_contexts
);
847 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
851 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
852 const char *context_type_string
;
855 ret
= config_writer_open_element(writer
,
856 config_element_context
);
858 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
862 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
863 /* Perf contexts are saved as event_perf_context_type */
864 ret
= config_writer_open_element(writer
,
865 config_element_perf
);
867 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
871 ret
= config_writer_write_element_unsigned_int(writer
,
873 ctx
->ctx
.u
.perf_counter
.type
);
875 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
879 ret
= config_writer_write_element_unsigned_int(writer
,
880 config_element_config
,
881 ctx
->ctx
.u
.perf_counter
.config
);
883 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
887 ret
= config_writer_write_element_string(writer
,
889 ctx
->ctx
.u
.perf_counter
.name
);
891 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
896 ret
= config_writer_close_element(writer
);
898 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
902 /* Save context as event_context_type_type */
903 context_type_string
= get_ust_context_type_string(
905 if (!context_type_string
) {
906 ERR("Unsupported UST context type.")
907 ret
= LTTNG_ERR_INVALID
;
911 ret
= config_writer_write_element_string(writer
,
912 config_element_type
, context_type_string
);
914 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
920 ret
= config_writer_close_element(writer
);
922 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
928 ret
= config_writer_close_element(writer
);
930 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
938 int save_kernel_channel(struct config_writer
*writer
,
939 struct ltt_kernel_channel
*kchan
)
946 ret
= config_writer_open_element(writer
, config_element_channel
);
948 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
952 ret
= config_writer_write_element_string(writer
, config_element_name
,
953 kchan
->channel
->name
);
955 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
959 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
960 kchan
->channel
->enabled
);
962 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
966 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
971 ret
= save_kernel_events(writer
, kchan
);
976 ret
= save_kernel_contexts(writer
, kchan
);
982 ret
= config_writer_close_element(writer
);
984 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
992 int save_ust_channel(struct config_writer
*writer
,
993 struct ltt_ust_channel
*ust_chan
,
994 struct ltt_ust_session
*session
)
1002 ret
= config_writer_open_element(writer
, config_element_channel
);
1004 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1008 ret
= config_writer_write_element_string(writer
, config_element_name
,
1011 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1015 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1018 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1022 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1027 ret
= config_writer_write_element_unsigned_int(writer
,
1028 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1030 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1034 ret
= config_writer_write_element_unsigned_int(writer
,
1035 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1037 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1041 ret
= config_writer_write_element_unsigned_int(writer
,
1042 config_element_live_timer_interval
,
1043 session
->live_timer_interval
);
1045 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1049 ret
= save_ust_events(writer
, ust_chan
->events
);
1051 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1055 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1061 ret
= config_writer_close_element(writer
);
1063 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1071 int save_kernel_session(struct config_writer
*writer
,
1072 struct ltt_session
*session
)
1075 struct ltt_kernel_channel
*kchan
;
1080 ret
= config_writer_write_element_string(writer
, config_element_type
,
1081 config_domain_type_kernel
);
1083 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1087 ret
= config_writer_write_element_string(writer
,
1088 config_element_buffer_type
, config_buffer_type_global
);
1090 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1094 ret
= config_writer_open_element(writer
,
1095 config_element_channels
);
1097 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1101 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1103 ret
= save_kernel_channel(writer
, kchan
);
1110 ret
= config_writer_close_element(writer
);
1112 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1120 int save_ust_session(struct config_writer
*writer
,
1121 struct ltt_session
*session
, int save_agent
)
1124 struct ltt_ust_channel
*ust_chan
;
1125 const char *buffer_type_string
;
1126 struct lttng_ht_node_str
*node
;
1127 struct lttng_ht_iter iter
;
1132 ret
= config_writer_write_element_string(writer
, config_element_type
,
1133 save_agent
? config_domain_type_jul
: config_domain_type_ust
);
1135 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1139 buffer_type_string
= get_buffer_type_string(
1140 session
->ust_session
->buffer_type
);
1141 if (!buffer_type_string
) {
1142 ERR("Unsupported buffer type.");
1143 ret
= LTTNG_ERR_INVALID
;
1147 ret
= config_writer_write_element_string(writer
,
1148 config_element_buffer_type
, buffer_type_string
);
1150 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1154 ret
= config_writer_open_element(writer
, config_element_channels
);
1156 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1161 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1162 &iter
.iter
, node
, node
) {
1165 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1166 agent_channel
= !strcmp(DEFAULT_JUL_CHANNEL_NAME
, ust_chan
->name
) ||
1167 !strcmp(DEFAULT_LOG4J_CHANNEL_NAME
, ust_chan
->name
) ||
1168 !strcmp(DEFAULT_PYTHON_CHANNEL_NAME
, ust_chan
->name
);
1169 if (!(save_agent
^ agent_channel
)) {
1170 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1180 ret
= config_writer_close_element(writer
);
1182 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1190 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1197 if (!session
->kernel_session
&& !session
->ust_session
) {
1201 ret
= config_writer_open_element(writer
, config_element_domains
);
1203 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1208 if (session
->kernel_session
) {
1209 ret
= config_writer_open_element(writer
,
1210 config_element_domain
);
1212 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1216 ret
= save_kernel_session(writer
, session
);
1222 ret
= config_writer_close_element(writer
);
1224 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1229 if (session
->ust_session
) {
1230 unsigned long agent_count
;
1232 ret
= config_writer_open_element(writer
,
1233 config_element_domain
);
1235 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1239 ret
= save_ust_session(writer
, session
, 0);
1245 ret
= config_writer_close_element(writer
);
1247 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1253 lttng_ht_get_count(session
->ust_session
->agents
);
1256 if (agent_count
> 0) {
1257 ret
= config_writer_open_element(writer
,
1258 config_element_domain
);
1260 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1264 ret
= save_ust_session(writer
, session
, 1);
1270 ret
= config_writer_close_element(writer
);
1272 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1278 if (session
->ust_session
) {
1282 ret
= config_writer_close_element(writer
);
1284 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1292 int save_consumer_output(struct config_writer
*writer
,
1293 struct consumer_output
*output
)
1300 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1302 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1306 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1309 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1313 ret
= config_writer_open_element(writer
, config_element_destination
);
1315 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1319 switch (output
->type
) {
1320 case CONSUMER_DST_LOCAL
:
1321 ret
= config_writer_write_element_string(writer
,
1322 config_element_path
, output
->dst
.trace_path
);
1324 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1328 case CONSUMER_DST_NET
:
1332 uri
= zmalloc(PATH_MAX
);
1334 ret
= LTTNG_ERR_NOMEM
;
1338 ret
= config_writer_open_element(writer
, config_element_net_output
);
1340 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1341 goto end_net_output
;
1344 if (output
->dst
.net
.control_isset
&&
1345 output
->dst
.net
.data_isset
) {
1346 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1348 ret
= LTTNG_ERR_INVALID
;
1349 goto end_net_output
;
1352 ret
= config_writer_write_element_string(writer
,
1353 config_element_control_uri
, uri
);
1355 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1356 goto end_net_output
;
1359 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1361 ret
= LTTNG_ERR_INVALID
;
1362 goto end_net_output
;
1365 ret
= config_writer_write_element_string(writer
,
1366 config_element_data_uri
, uri
);
1368 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1369 goto end_net_output
;
1378 ret
= !output
->dst
.net
.control_isset
?
1379 LTTNG_ERR_URL_CTRL_MISS
:
1380 LTTNG_ERR_URL_DATA_MISS
;
1385 ret
= config_writer_close_element(writer
);
1387 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1393 ERR("Unsupported consumer output type.");
1394 ret
= LTTNG_ERR_INVALID
;
1399 ret
= config_writer_close_element(writer
);
1401 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1405 /* /consumer_output */
1406 ret
= config_writer_close_element(writer
);
1408 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1416 int save_snapshot_outputs(struct config_writer
*writer
,
1417 struct snapshot
*snapshot
)
1420 struct lttng_ht_iter iter
;
1421 struct snapshot_output
*output
;
1426 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1428 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1433 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1435 ret
= config_writer_open_element(writer
,
1436 config_element_output
);
1438 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1442 ret
= config_writer_write_element_string(writer
,
1443 config_element_name
, output
->name
);
1445 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1449 ret
= config_writer_write_element_unsigned_int(writer
,
1450 config_element_max_size
, output
->max_size
);
1452 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1456 ret
= save_consumer_output(writer
, output
->consumer
);
1462 ret
= config_writer_close_element(writer
);
1464 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1470 /* /snapshot_outputs */
1471 ret
= config_writer_close_element(writer
);
1473 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1485 int save_session_output(struct config_writer
*writer
,
1486 struct ltt_session
*session
)
1493 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1494 (!session
->snapshot_mode
&& !session
->consumer
)) {
1495 /* Session is in no output mode */
1500 ret
= config_writer_open_element(writer
, config_element_output
);
1502 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1506 if (session
->snapshot_mode
) {
1507 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1512 if (session
->consumer
) {
1513 ret
= save_consumer_output(writer
, session
->consumer
);
1521 ret
= config_writer_close_element(writer
);
1523 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1531 * Save the given session.
1533 * Return 0 on success else a LTTNG_ERR* code.
1536 int save_session(struct ltt_session
*session
,
1537 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1540 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1541 char config_file_path
[PATH_MAX
];
1543 struct config_writer
*writer
= NULL
;
1544 size_t session_name_len
;
1545 const char *provided_path
;
1551 session_name_len
= strlen(session
->name
);
1552 memset(config_file_path
, 0, sizeof(config_file_path
));
1554 if (!session_access_ok(session
,
1555 LTTNG_SOCK_GET_UID_CRED(creds
),
1556 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1557 ret
= LTTNG_ERR_EPERM
;
1561 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1562 if (provided_path
) {
1563 DBG3("Save session in provided path %s", provided_path
);
1564 len
= strlen(provided_path
);
1565 if (len
>= sizeof(config_file_path
)) {
1566 ret
= LTTNG_ERR_SET_URL
;
1569 strncpy(config_file_path
, provided_path
, len
);
1572 char *home_dir
= utils_get_user_home_dir(
1573 LTTNG_SOCK_GET_UID_CRED(creds
));
1575 ret
= LTTNG_ERR_SET_URL
;
1579 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1580 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1583 PERROR("snprintf save session");
1584 ret
= LTTNG_ERR_SET_URL
;
1591 * Check the path fits in the config file path dst including the '/'
1592 * followed by trailing .lttng extension and the NULL terminated string.
1594 if ((len
+ session_name_len
+ 2 +
1595 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1596 > sizeof(config_file_path
)) {
1597 ret
= LTTNG_ERR_SET_URL
;
1601 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1602 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1604 ret
= LTTNG_ERR_SET_URL
;
1609 * At this point, we know that everything fits in the buffer. Validation
1610 * was done just above.
1612 config_file_path
[len
++] = '/';
1613 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1614 len
+= session_name_len
;
1615 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1616 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1617 config_file_path
[len
] = '\0';
1619 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1620 /* File exists, notify the user since the overwrite flag is off. */
1621 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1625 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1626 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1627 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1629 PERROR("Could not create configuration file");
1630 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1635 writer
= config_writer_create(fd
, 1);
1637 ret
= LTTNG_ERR_NOMEM
;
1641 ret
= config_writer_open_element(writer
, config_element_sessions
);
1643 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1647 ret
= config_writer_open_element(writer
, config_element_session
);
1649 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1653 ret
= config_writer_write_element_string(writer
, config_element_name
,
1656 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1660 ret
= save_domains(writer
, session
);
1665 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1668 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1672 if (session
->snapshot_mode
|| session
->live_timer
) {
1673 ret
= config_writer_open_element(writer
, config_element_attributes
);
1675 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1679 if (session
->snapshot_mode
) {
1680 ret
= config_writer_write_element_bool(writer
,
1681 config_element_snapshot_mode
, 1);
1683 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1687 ret
= config_writer_write_element_unsigned_int(writer
,
1688 config_element_live_timer_interval
, session
->live_timer
);
1690 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1696 ret
= config_writer_close_element(writer
);
1698 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1703 ret
= save_session_output(writer
, session
);
1709 ret
= config_writer_close_element(writer
);
1711 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1716 ret
= config_writer_close_element(writer
);
1718 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1722 if (writer
&& config_writer_destroy(writer
)) {
1723 /* Preserve the original error code */
1724 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
1727 /* Delete file in case of error */
1728 if (file_opened
&& unlink(config_file_path
)) {
1729 PERROR("Unlinking XML session configuration.");
1736 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
1737 lttng_sock_cred
*creds
)
1740 const char *session_name
;
1741 struct ltt_session
*session
;
1743 session_lock_list();
1745 session_name
= lttng_save_session_attr_get_session_name(attr
);
1747 session
= session_find_by_name(session_name
);
1749 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1753 session_lock(session
);
1754 ret
= save_session(session
, attr
, creds
);
1755 session_unlock(session
);
1760 struct ltt_session_list
*list
= session_get_list();
1762 cds_list_for_each_entry(session
, &list
->head
, list
) {
1763 session_lock(session
);
1764 ret
= save_session(session
, attr
, creds
);
1765 session_unlock(session
);
1767 /* Don't abort if we don't have the required permissions. */
1768 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
1776 session_unlock_list();