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
;
720 int save_kernel_context(struct config_writer
*writer
,
721 struct lttng_kernel_context
*ctx
)
729 ret
= config_writer_open_element(writer
, config_element_context
);
731 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
735 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
736 ret
= config_writer_open_element(writer
, config_element_perf
);
738 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
742 ret
= config_writer_write_element_unsigned_int(writer
,
743 config_element_type
, ctx
->u
.perf_counter
.type
);
745 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
749 ret
= config_writer_write_element_unsigned_int(writer
,
750 config_element_config
, ctx
->u
.perf_counter
.config
);
752 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
756 ret
= config_writer_write_element_string(writer
,
757 config_element_name
, ctx
->u
.perf_counter
.name
);
759 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
764 ret
= config_writer_close_element(writer
);
766 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
770 const char *context_type_string
=
771 get_kernel_context_type_string(ctx
->ctx
);
773 if (!context_type_string
) {
774 ERR("Unsupported kernel context type.");
775 ret
= LTTNG_ERR_INVALID
;
779 ret
= config_writer_write_element_string(writer
,
780 config_element_type
, context_type_string
);
782 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
788 ret
= config_writer_close_element(writer
);
790 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
799 int save_kernel_contexts(struct config_writer
*writer
,
800 struct ltt_kernel_channel
*kchan
)
803 struct ltt_kernel_context
*ctx
;
805 if (cds_list_empty(&kchan
->ctx_list
)) {
810 ret
= config_writer_open_element(writer
, config_element_contexts
);
812 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
816 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
817 ret
= save_kernel_context(writer
, &ctx
->ctx
);
824 ret
= config_writer_close_element(writer
);
826 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
834 int save_ust_context(struct config_writer
*writer
,
835 struct cds_list_head
*ctx_list
)
838 struct ltt_ust_context
*ctx
;
843 ret
= config_writer_open_element(writer
, config_element_contexts
);
845 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
849 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
850 const char *context_type_string
;
853 ret
= config_writer_open_element(writer
,
854 config_element_context
);
856 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
860 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
861 /* Perf contexts are saved as event_perf_context_type */
862 ret
= config_writer_open_element(writer
,
863 config_element_perf
);
865 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
869 ret
= config_writer_write_element_unsigned_int(writer
,
871 ctx
->ctx
.u
.perf_counter
.type
);
873 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
877 ret
= config_writer_write_element_unsigned_int(writer
,
878 config_element_config
,
879 ctx
->ctx
.u
.perf_counter
.config
);
881 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
885 ret
= config_writer_write_element_string(writer
,
887 ctx
->ctx
.u
.perf_counter
.name
);
889 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
894 ret
= config_writer_close_element(writer
);
896 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
900 /* Save context as event_context_type_type */
901 context_type_string
= get_ust_context_type_string(
903 if (!context_type_string
) {
904 ERR("Unsupported UST context type.")
905 ret
= LTTNG_ERR_INVALID
;
909 ret
= config_writer_write_element_string(writer
,
910 config_element_type
, context_type_string
);
912 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
918 ret
= config_writer_close_element(writer
);
920 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
926 ret
= config_writer_close_element(writer
);
928 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
936 int save_kernel_channel(struct config_writer
*writer
,
937 struct ltt_kernel_channel
*kchan
)
944 ret
= config_writer_open_element(writer
, config_element_channel
);
946 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
950 ret
= config_writer_write_element_string(writer
, config_element_name
,
951 kchan
->channel
->name
);
953 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
957 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
958 kchan
->channel
->enabled
);
960 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
964 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
969 ret
= save_kernel_events(writer
, kchan
);
974 ret
= save_kernel_contexts(writer
, kchan
);
980 ret
= config_writer_close_element(writer
);
982 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
990 int save_ust_channel(struct config_writer
*writer
,
991 struct ltt_ust_channel
*ust_chan
,
992 struct ltt_ust_session
*session
)
1000 ret
= config_writer_open_element(writer
, config_element_channel
);
1002 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1006 ret
= config_writer_write_element_string(writer
, config_element_name
,
1009 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1013 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1016 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1020 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1025 ret
= config_writer_write_element_unsigned_int(writer
,
1026 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1028 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1032 ret
= config_writer_write_element_unsigned_int(writer
,
1033 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1035 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1039 ret
= config_writer_write_element_unsigned_int(writer
,
1040 config_element_live_timer_interval
,
1041 session
->live_timer_interval
);
1043 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1047 ret
= save_ust_events(writer
, ust_chan
->events
);
1049 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1053 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1059 ret
= config_writer_close_element(writer
);
1061 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1069 int save_kernel_session(struct config_writer
*writer
,
1070 struct ltt_session
*session
)
1073 struct ltt_kernel_channel
*kchan
;
1078 ret
= config_writer_write_element_string(writer
, config_element_type
,
1079 config_domain_type_kernel
);
1081 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1085 ret
= config_writer_write_element_string(writer
,
1086 config_element_buffer_type
, config_buffer_type_global
);
1088 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1092 ret
= config_writer_open_element(writer
,
1093 config_element_channels
);
1095 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1099 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1101 ret
= save_kernel_channel(writer
, kchan
);
1108 ret
= config_writer_close_element(writer
);
1110 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1118 int save_ust_session(struct config_writer
*writer
,
1119 struct ltt_session
*session
, int save_agent
)
1122 struct ltt_ust_channel
*ust_chan
;
1123 const char *buffer_type_string
;
1124 struct lttng_ht_node_str
*node
;
1125 struct lttng_ht_iter iter
;
1130 ret
= config_writer_write_element_string(writer
, config_element_type
,
1131 save_agent
? config_domain_type_jul
: config_domain_type_ust
);
1133 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1137 buffer_type_string
= get_buffer_type_string(
1138 session
->ust_session
->buffer_type
);
1139 if (!buffer_type_string
) {
1140 ERR("Unsupported buffer type.");
1141 ret
= LTTNG_ERR_INVALID
;
1145 ret
= config_writer_write_element_string(writer
,
1146 config_element_buffer_type
, buffer_type_string
);
1148 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1152 ret
= config_writer_open_element(writer
, config_element_channels
);
1154 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1159 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1160 &iter
.iter
, node
, node
) {
1163 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1164 agent_channel
= !strcmp(DEFAULT_JUL_CHANNEL_NAME
, ust_chan
->name
) ||
1165 !strcmp(DEFAULT_LOG4J_CHANNEL_NAME
, ust_chan
->name
) ||
1166 !strcmp(DEFAULT_PYTHON_CHANNEL_NAME
, ust_chan
->name
);
1167 if (!(save_agent
^ agent_channel
)) {
1168 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1178 ret
= config_writer_close_element(writer
);
1180 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1188 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1195 if (!session
->kernel_session
&& !session
->ust_session
) {
1199 ret
= config_writer_open_element(writer
, config_element_domains
);
1201 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1206 if (session
->kernel_session
) {
1207 ret
= config_writer_open_element(writer
,
1208 config_element_domain
);
1210 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1214 ret
= save_kernel_session(writer
, session
);
1220 ret
= config_writer_close_element(writer
);
1222 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1227 if (session
->ust_session
) {
1228 ret
= config_writer_open_element(writer
,
1229 config_element_domain
);
1231 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1235 ret
= save_ust_session(writer
, session
, 0);
1241 ret
= config_writer_close_element(writer
);
1243 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1248 if (session
->ust_session
&&
1249 lttng_ht_get_count(session
->ust_session
->agents
) > 0) {
1250 ret
= config_writer_open_element(writer
,
1251 config_element_domain
);
1253 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1257 ret
= save_ust_session(writer
, session
, 1);
1263 ret
= config_writer_close_element(writer
);
1265 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1271 ret
= config_writer_close_element(writer
);
1273 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1281 int save_consumer_output(struct config_writer
*writer
,
1282 struct consumer_output
*output
)
1289 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1291 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1295 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1298 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1302 ret
= config_writer_open_element(writer
, config_element_destination
);
1304 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1308 switch (output
->type
) {
1309 case CONSUMER_DST_LOCAL
:
1310 ret
= config_writer_write_element_string(writer
,
1311 config_element_path
, output
->dst
.trace_path
);
1313 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1317 case CONSUMER_DST_NET
:
1321 uri
= zmalloc(PATH_MAX
);
1323 ret
= LTTNG_ERR_NOMEM
;
1327 ret
= config_writer_open_element(writer
, config_element_net_output
);
1329 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1330 goto end_net_output
;
1333 if (output
->dst
.net
.control_isset
&&
1334 output
->dst
.net
.data_isset
) {
1335 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1337 ret
= LTTNG_ERR_INVALID
;
1338 goto end_net_output
;
1341 ret
= config_writer_write_element_string(writer
,
1342 config_element_control_uri
, uri
);
1344 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1345 goto end_net_output
;
1348 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1350 ret
= LTTNG_ERR_INVALID
;
1351 goto end_net_output
;
1354 ret
= config_writer_write_element_string(writer
,
1355 config_element_data_uri
, uri
);
1357 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1358 goto end_net_output
;
1367 ret
= !output
->dst
.net
.control_isset
?
1368 LTTNG_ERR_URL_CTRL_MISS
:
1369 LTTNG_ERR_URL_DATA_MISS
;
1374 ret
= config_writer_close_element(writer
);
1376 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1382 ERR("Unsupported consumer output type.");
1383 ret
= LTTNG_ERR_INVALID
;
1388 ret
= config_writer_close_element(writer
);
1390 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1394 /* /consumer_output */
1395 ret
= config_writer_close_element(writer
);
1397 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1405 int save_snapshot_outputs(struct config_writer
*writer
,
1406 struct snapshot
*snapshot
)
1409 struct lttng_ht_iter iter
;
1410 struct snapshot_output
*output
;
1415 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1417 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1422 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1424 ret
= config_writer_open_element(writer
,
1425 config_element_output
);
1427 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1431 ret
= config_writer_write_element_string(writer
,
1432 config_element_name
, output
->name
);
1434 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1438 ret
= config_writer_write_element_unsigned_int(writer
,
1439 config_element_max_size
, output
->max_size
);
1441 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1445 ret
= save_consumer_output(writer
, output
->consumer
);
1451 ret
= config_writer_close_element(writer
);
1453 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1459 /* /snapshot_outputs */
1460 ret
= config_writer_close_element(writer
);
1462 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1474 int save_session_output(struct config_writer
*writer
,
1475 struct ltt_session
*session
)
1482 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1483 (!session
->snapshot_mode
&& !session
->consumer
)) {
1484 /* Session is in no output mode */
1489 ret
= config_writer_open_element(writer
, config_element_output
);
1491 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1495 if (session
->snapshot_mode
) {
1496 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1501 if (session
->consumer
) {
1502 ret
= save_consumer_output(writer
, session
->consumer
);
1510 ret
= config_writer_close_element(writer
);
1512 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1520 * Save the given session.
1522 * Return 0 on success else a LTTNG_ERR* code.
1525 int save_session(struct ltt_session
*session
,
1526 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1529 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1530 char config_file_path
[PATH_MAX
];
1532 struct config_writer
*writer
= NULL
;
1533 size_t session_name_len
;
1534 const char *provided_path
;
1540 session_name_len
= strlen(session
->name
);
1541 memset(config_file_path
, 0, sizeof(config_file_path
));
1543 if (!session_access_ok(session
,
1544 LTTNG_SOCK_GET_UID_CRED(creds
),
1545 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1546 ret
= LTTNG_ERR_EPERM
;
1550 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1551 if (provided_path
) {
1552 DBG3("Save session in provided path %s", provided_path
);
1553 len
= strlen(provided_path
);
1554 if (len
>= sizeof(config_file_path
)) {
1555 ret
= LTTNG_ERR_SET_URL
;
1558 strncpy(config_file_path
, provided_path
, len
);
1561 char *home_dir
= utils_get_user_home_dir(
1562 LTTNG_SOCK_GET_UID_CRED(creds
));
1564 ret
= LTTNG_ERR_SET_URL
;
1568 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1569 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1572 PERROR("snprintf save session");
1573 ret
= LTTNG_ERR_SET_URL
;
1580 * Check the path fits in the config file path dst including the '/'
1581 * followed by trailing .lttng extension and the NULL terminated string.
1583 if ((len
+ session_name_len
+ 2 +
1584 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1585 > sizeof(config_file_path
)) {
1586 ret
= LTTNG_ERR_SET_URL
;
1590 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1591 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1593 ret
= LTTNG_ERR_SET_URL
;
1598 * At this point, we know that everything fits in the buffer. Validation
1599 * was done just above.
1601 config_file_path
[len
++] = '/';
1602 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1603 len
+= session_name_len
;
1604 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1605 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1606 config_file_path
[len
] = '\0';
1608 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1609 /* File exists, notify the user since the overwrite flag is off. */
1610 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1614 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1615 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1616 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1618 PERROR("Could not create configuration file");
1619 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1624 writer
= config_writer_create(fd
, 1);
1626 ret
= LTTNG_ERR_NOMEM
;
1630 ret
= config_writer_open_element(writer
, config_element_sessions
);
1632 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1636 ret
= config_writer_open_element(writer
, config_element_session
);
1638 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1642 ret
= config_writer_write_element_string(writer
, config_element_name
,
1645 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1649 ret
= save_domains(writer
, session
);
1654 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1657 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1661 if (session
->snapshot_mode
|| session
->live_timer
) {
1662 ret
= config_writer_open_element(writer
, config_element_attributes
);
1664 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1668 if (session
->snapshot_mode
) {
1669 ret
= config_writer_write_element_bool(writer
,
1670 config_element_snapshot_mode
, 1);
1672 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1676 ret
= config_writer_write_element_unsigned_int(writer
,
1677 config_element_live_timer_interval
, session
->live_timer
);
1679 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1685 ret
= config_writer_close_element(writer
);
1687 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1692 ret
= save_session_output(writer
, session
);
1698 ret
= config_writer_close_element(writer
);
1700 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1705 ret
= config_writer_close_element(writer
);
1707 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1711 if (writer
&& config_writer_destroy(writer
)) {
1712 /* Preserve the original error code */
1713 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
1716 /* Delete file in case of error */
1717 if (file_opened
&& unlink(config_file_path
)) {
1718 PERROR("Unlinking XML session configuration.");
1725 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
1726 lttng_sock_cred
*creds
)
1729 const char *session_name
;
1730 struct ltt_session
*session
;
1732 session_lock_list();
1734 session_name
= lttng_save_session_attr_get_session_name(attr
);
1736 session
= session_find_by_name(session_name
);
1738 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1742 session_lock(session
);
1743 ret
= save_session(session
, attr
, creds
);
1744 session_unlock(session
);
1749 struct ltt_session_list
*list
= session_get_list();
1751 cds_list_for_each_entry(session
, &list
->head
, list
) {
1752 session_lock(session
);
1753 ret
= save_session(session
, attr
, creds
);
1754 session_unlock(session
);
1756 /* Don't abort if we don't have the required permissions. */
1757 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
1765 session_unlock_list();