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 unsigned long agent_count
;
1230 ret
= config_writer_open_element(writer
,
1231 config_element_domain
);
1233 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1237 ret
= save_ust_session(writer
, session
, 0);
1243 ret
= config_writer_close_element(writer
);
1245 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1251 lttng_ht_get_count(session
->ust_session
->agents
);
1254 if (agent_count
> 0) {
1255 ret
= config_writer_open_element(writer
,
1256 config_element_domain
);
1258 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1262 ret
= save_ust_session(writer
, session
, 1);
1268 ret
= config_writer_close_element(writer
);
1270 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1276 if (session
->ust_session
) {
1280 ret
= config_writer_close_element(writer
);
1282 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1290 int save_consumer_output(struct config_writer
*writer
,
1291 struct consumer_output
*output
)
1298 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1300 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1304 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1307 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1311 ret
= config_writer_open_element(writer
, config_element_destination
);
1313 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1317 switch (output
->type
) {
1318 case CONSUMER_DST_LOCAL
:
1319 ret
= config_writer_write_element_string(writer
,
1320 config_element_path
, output
->dst
.trace_path
);
1322 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1326 case CONSUMER_DST_NET
:
1330 uri
= zmalloc(PATH_MAX
);
1332 ret
= LTTNG_ERR_NOMEM
;
1336 ret
= config_writer_open_element(writer
, config_element_net_output
);
1338 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1339 goto end_net_output
;
1342 if (output
->dst
.net
.control_isset
&&
1343 output
->dst
.net
.data_isset
) {
1344 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1346 ret
= LTTNG_ERR_INVALID
;
1347 goto end_net_output
;
1350 ret
= config_writer_write_element_string(writer
,
1351 config_element_control_uri
, uri
);
1353 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1354 goto end_net_output
;
1357 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1359 ret
= LTTNG_ERR_INVALID
;
1360 goto end_net_output
;
1363 ret
= config_writer_write_element_string(writer
,
1364 config_element_data_uri
, uri
);
1366 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1367 goto end_net_output
;
1376 ret
= !output
->dst
.net
.control_isset
?
1377 LTTNG_ERR_URL_CTRL_MISS
:
1378 LTTNG_ERR_URL_DATA_MISS
;
1383 ret
= config_writer_close_element(writer
);
1385 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1391 ERR("Unsupported consumer output type.");
1392 ret
= LTTNG_ERR_INVALID
;
1397 ret
= config_writer_close_element(writer
);
1399 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1403 /* /consumer_output */
1404 ret
= config_writer_close_element(writer
);
1406 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1414 int save_snapshot_outputs(struct config_writer
*writer
,
1415 struct snapshot
*snapshot
)
1418 struct lttng_ht_iter iter
;
1419 struct snapshot_output
*output
;
1424 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1426 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1431 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1433 ret
= config_writer_open_element(writer
,
1434 config_element_output
);
1436 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1440 ret
= config_writer_write_element_string(writer
,
1441 config_element_name
, output
->name
);
1443 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1447 ret
= config_writer_write_element_unsigned_int(writer
,
1448 config_element_max_size
, output
->max_size
);
1450 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1454 ret
= save_consumer_output(writer
, output
->consumer
);
1460 ret
= config_writer_close_element(writer
);
1462 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1468 /* /snapshot_outputs */
1469 ret
= config_writer_close_element(writer
);
1471 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1483 int save_session_output(struct config_writer
*writer
,
1484 struct ltt_session
*session
)
1491 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1492 (!session
->snapshot_mode
&& !session
->consumer
)) {
1493 /* Session is in no output mode */
1498 ret
= config_writer_open_element(writer
, config_element_output
);
1500 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1504 if (session
->snapshot_mode
) {
1505 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1510 if (session
->consumer
) {
1511 ret
= save_consumer_output(writer
, session
->consumer
);
1519 ret
= config_writer_close_element(writer
);
1521 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1529 * Save the given session.
1531 * Return 0 on success else a LTTNG_ERR* code.
1534 int save_session(struct ltt_session
*session
,
1535 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1538 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1539 char config_file_path
[PATH_MAX
];
1541 struct config_writer
*writer
= NULL
;
1542 size_t session_name_len
;
1543 const char *provided_path
;
1549 session_name_len
= strlen(session
->name
);
1550 memset(config_file_path
, 0, sizeof(config_file_path
));
1552 if (!session_access_ok(session
,
1553 LTTNG_SOCK_GET_UID_CRED(creds
),
1554 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1555 ret
= LTTNG_ERR_EPERM
;
1559 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1560 if (provided_path
) {
1561 DBG3("Save session in provided path %s", provided_path
);
1562 len
= strlen(provided_path
);
1563 if (len
>= sizeof(config_file_path
)) {
1564 ret
= LTTNG_ERR_SET_URL
;
1567 strncpy(config_file_path
, provided_path
, len
);
1570 char *home_dir
= utils_get_user_home_dir(
1571 LTTNG_SOCK_GET_UID_CRED(creds
));
1573 ret
= LTTNG_ERR_SET_URL
;
1577 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1578 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1581 PERROR("snprintf save session");
1582 ret
= LTTNG_ERR_SET_URL
;
1589 * Check the path fits in the config file path dst including the '/'
1590 * followed by trailing .lttng extension and the NULL terminated string.
1592 if ((len
+ session_name_len
+ 2 +
1593 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1594 > sizeof(config_file_path
)) {
1595 ret
= LTTNG_ERR_SET_URL
;
1599 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1600 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1602 ret
= LTTNG_ERR_SET_URL
;
1607 * At this point, we know that everything fits in the buffer. Validation
1608 * was done just above.
1610 config_file_path
[len
++] = '/';
1611 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1612 len
+= session_name_len
;
1613 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1614 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1615 config_file_path
[len
] = '\0';
1617 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1618 /* File exists, notify the user since the overwrite flag is off. */
1619 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1623 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1624 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1625 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1627 PERROR("Could not create configuration file");
1628 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1633 writer
= config_writer_create(fd
, 1);
1635 ret
= LTTNG_ERR_NOMEM
;
1639 ret
= config_writer_open_element(writer
, config_element_sessions
);
1641 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1645 ret
= config_writer_open_element(writer
, config_element_session
);
1647 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1651 ret
= config_writer_write_element_string(writer
, config_element_name
,
1654 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1658 ret
= save_domains(writer
, session
);
1663 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1666 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1670 if (session
->snapshot_mode
|| session
->live_timer
) {
1671 ret
= config_writer_open_element(writer
, config_element_attributes
);
1673 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1677 if (session
->snapshot_mode
) {
1678 ret
= config_writer_write_element_bool(writer
,
1679 config_element_snapshot_mode
, 1);
1681 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1685 ret
= config_writer_write_element_unsigned_int(writer
,
1686 config_element_live_timer_interval
, session
->live_timer
);
1688 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1694 ret
= config_writer_close_element(writer
);
1696 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1701 ret
= save_session_output(writer
, session
);
1707 ret
= config_writer_close_element(writer
);
1709 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1714 ret
= config_writer_close_element(writer
);
1716 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1720 if (writer
&& config_writer_destroy(writer
)) {
1721 /* Preserve the original error code */
1722 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
1725 /* Delete file in case of error */
1726 if (file_opened
&& unlink(config_file_path
)) {
1727 PERROR("Unlinking XML session configuration.");
1734 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
1735 lttng_sock_cred
*creds
)
1738 const char *session_name
;
1739 struct ltt_session
*session
;
1741 session_lock_list();
1743 session_name
= lttng_save_session_attr_get_session_name(attr
);
1745 session
= session_find_by_name(session_name
);
1747 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1751 session_lock(session
);
1752 ret
= save_session(session
, attr
, creds
);
1753 session_unlock(session
);
1758 struct ltt_session_list
*list
= session_get_list();
1760 cds_list_for_each_entry(session
, &list
->head
, list
) {
1761 session_lock(session
);
1762 ret
= save_session(session
, attr
, creds
);
1763 session_unlock(session
);
1765 /* Don't abort if we don't have the required permissions. */
1766 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
1774 session_unlock_list();