2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/config/config.h>
28 #include <common/utils.h>
29 #include <common/runas.h>
30 #include <lttng/save-internal.h>
34 #include "trace-ust.h"
37 int save_kernel_channel_attributes(struct config_writer
*writer
,
38 struct lttng_channel_attr
*attr
)
42 ret
= config_writer_write_element_string(writer
,
43 config_element_overwrite_mode
,
44 attr
->overwrite
? config_overwrite_mode_overwrite
:
45 config_overwrite_mode_discard
);
50 ret
= config_writer_write_element_unsigned_int(writer
,
51 config_element_subbuf_size
, attr
->subbuf_size
);
56 ret
= config_writer_write_element_unsigned_int(writer
,
57 config_element_num_subbuf
,
63 ret
= config_writer_write_element_unsigned_int(writer
,
64 config_element_switch_timer_interval
,
65 attr
->switch_timer_interval
);
70 ret
= config_writer_write_element_unsigned_int(writer
,
71 config_element_read_timer_interval
,
72 attr
->read_timer_interval
);
77 ret
= config_writer_write_element_string(writer
,
78 config_element_output_type
,
79 attr
->output
== LTTNG_EVENT_SPLICE
?
80 config_output_type_splice
: config_output_type_mmap
);
85 ret
= config_writer_write_element_unsigned_int(writer
,
86 config_element_tracefile_size
, attr
->tracefile_size
);
91 ret
= config_writer_write_element_unsigned_int(writer
,
92 config_element_tracefile_count
,
93 attr
->tracefile_count
);
98 ret
= config_writer_write_element_unsigned_int(writer
,
99 config_element_live_timer_interval
,
100 attr
->live_timer_interval
);
105 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
109 int save_ust_channel_attributes(struct config_writer
*writer
,
110 struct lttng_ust_channel_attr
*attr
)
114 ret
= config_writer_write_element_string(writer
,
115 config_element_overwrite_mode
,
116 attr
->overwrite
? config_overwrite_mode_overwrite
:
117 config_overwrite_mode_discard
);
122 ret
= config_writer_write_element_unsigned_int(writer
,
123 config_element_subbuf_size
, attr
->subbuf_size
);
128 ret
= config_writer_write_element_unsigned_int(writer
,
129 config_element_num_subbuf
,
135 ret
= config_writer_write_element_unsigned_int(writer
,
136 config_element_switch_timer_interval
,
137 attr
->switch_timer_interval
);
142 ret
= config_writer_write_element_unsigned_int(writer
,
143 config_element_read_timer_interval
,
144 attr
->read_timer_interval
);
149 ret
= config_writer_write_element_string(writer
,
150 config_element_output_type
,
151 attr
->output
== LTTNG_UST_MMAP
?
152 config_output_type_mmap
: config_output_type_splice
);
157 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
161 const char *get_kernel_instrumentation_string(
162 enum lttng_kernel_instrumentation instrumentation
)
164 const char *instrumentation_string
;
166 switch (instrumentation
) {
167 case LTTNG_KERNEL_ALL
:
168 instrumentation_string
= config_event_type_all
;
170 case LTTNG_KERNEL_TRACEPOINT
:
171 instrumentation_string
= config_event_type_tracepoint
;
173 case LTTNG_KERNEL_KPROBE
:
174 instrumentation_string
= config_event_type_kprobe
;
176 case LTTNG_KERNEL_FUNCTION
:
177 instrumentation_string
= config_event_type_function
;
179 case LTTNG_KERNEL_KRETPROBE
:
180 instrumentation_string
= config_event_type_kretprobe
;
182 case LTTNG_KERNEL_NOOP
:
183 instrumentation_string
= config_event_type_noop
;
185 case LTTNG_KERNEL_SYSCALL
:
186 instrumentation_string
= config_event_type_syscall
;
189 instrumentation_string
= NULL
;
192 return instrumentation_string
;
196 const char *get_kernel_context_type_string(
197 enum lttng_kernel_context_type context_type
)
199 const char *context_type_string
;
201 switch (context_type
) {
202 case LTTNG_KERNEL_CONTEXT_PID
:
203 context_type_string
= config_event_context_pid
;
205 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
206 context_type_string
= config_event_context_procname
;
208 case LTTNG_KERNEL_CONTEXT_PRIO
:
209 context_type_string
= config_event_context_prio
;
211 case LTTNG_KERNEL_CONTEXT_NICE
:
212 context_type_string
= config_event_context_nice
;
214 case LTTNG_KERNEL_CONTEXT_VPID
:
215 context_type_string
= config_event_context_vpid
;
217 case LTTNG_KERNEL_CONTEXT_TID
:
218 context_type_string
= config_event_context_tid
;
220 case LTTNG_KERNEL_CONTEXT_VTID
:
221 context_type_string
= config_event_context_vtid
;
223 case LTTNG_KERNEL_CONTEXT_PPID
:
224 context_type_string
= config_event_context_ppid
;
226 case LTTNG_KERNEL_CONTEXT_VPPID
:
227 context_type_string
= config_event_context_vppid
;
229 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
230 context_type_string
= config_event_context_hostname
;
233 context_type_string
= NULL
;
236 return context_type_string
;
240 const char *get_ust_context_type_string(
241 enum lttng_ust_context_type context_type
)
243 const char *context_type_string
;
245 switch (context_type
) {
246 case LTTNG_UST_CONTEXT_PROCNAME
:
247 context_type_string
= config_event_context_procname
;
249 case LTTNG_UST_CONTEXT_VPID
:
250 context_type_string
= config_event_context_vpid
;
252 case LTTNG_UST_CONTEXT_VTID
:
253 context_type_string
= config_event_context_vtid
;
255 case LTTNG_UST_CONTEXT_IP
:
256 context_type_string
= config_event_context_ip
;
258 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
259 context_type_string
= config_event_context_pthread_id
;
261 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
262 context_type_string
= config_event_context_perf_thread_counter
;
265 context_type_string
= NULL
;
269 return context_type_string
;
273 const char *get_buffer_type_string(
274 enum lttng_buffer_type buffer_type
)
276 const char *buffer_type_string
;
278 switch (buffer_type
) {
279 case LTTNG_BUFFER_PER_PID
:
280 buffer_type_string
= config_buffer_type_per_pid
;
282 case LTTNG_BUFFER_PER_UID
:
283 buffer_type_string
= config_buffer_type_per_uid
;
285 case LTTNG_BUFFER_GLOBAL
:
286 buffer_type_string
= config_buffer_type_global
;
289 buffer_type_string
= NULL
;
292 return buffer_type_string
;
296 const char *get_loglevel_type_string(
297 enum lttng_ust_loglevel_type loglevel_type
)
299 const char *loglevel_type_string
;
301 switch (loglevel_type
) {
302 case LTTNG_UST_LOGLEVEL_ALL
:
303 loglevel_type_string
= config_loglevel_type_all
;
305 case LTTNG_UST_LOGLEVEL_RANGE
:
306 loglevel_type_string
= config_loglevel_type_range
;
308 case LTTNG_UST_LOGLEVEL_SINGLE
:
309 loglevel_type_string
= config_loglevel_type_single
;
312 loglevel_type_string
= NULL
;
315 return loglevel_type_string
;
319 int save_kernel_event(struct config_writer
*writer
,
320 struct ltt_kernel_event
*event
)
323 const char *instrumentation_type
;
325 ret
= config_writer_open_element(writer
, config_element_event
);
327 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
331 if (event
->event
->name
[0]) {
332 ret
= config_writer_write_element_string(writer
,
333 config_element_name
, event
->event
->name
);
335 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
340 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
343 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
347 instrumentation_type
= get_kernel_instrumentation_string(
348 event
->event
->instrumentation
);
349 if (!instrumentation_type
) {
350 ret
= LTTNG_ERR_INVALID
;
354 ret
= config_writer_write_element_string(writer
, config_element_type
,
355 instrumentation_type
);
357 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
361 if (event
->event
->instrumentation
== LTTNG_KERNEL_FUNCTION
||
362 event
->event
->instrumentation
== LTTNG_KERNEL_KPROBE
||
363 event
->event
->instrumentation
== LTTNG_KERNEL_KRETPROBE
) {
365 ret
= config_writer_open_element(writer
,
366 config_element_attributes
);
368 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
372 switch (event
->event
->instrumentation
) {
373 case LTTNG_KERNEL_FUNCTION
:
374 ret
= config_writer_open_element(writer
,
375 config_element_function_attributes
);
377 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
381 ret
= config_writer_write_element_string(writer
,
383 event
->event
->u
.ftrace
.symbol_name
);
385 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
389 /* /function attributes */
390 ret
= config_writer_close_element(writer
);
392 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
396 case LTTNG_KERNEL_KPROBE
:
397 case LTTNG_KERNEL_KRETPROBE
:
399 const char *symbol_name
;
403 if (event
->event
->instrumentation
==
404 LTTNG_KERNEL_KPROBE
) {
406 * Comments in lttng-kernel.h mention that
407 * either addr or symbol_name are set, not both.
409 addr
= event
->event
->u
.kprobe
.addr
;
410 offset
= event
->event
->u
.kprobe
.offset
;
411 symbol_name
= addr
? NULL
:
412 event
->event
->u
.kprobe
.symbol_name
;
415 event
->event
->u
.kretprobe
.symbol_name
;
416 addr
= event
->event
->u
.kretprobe
.addr
;
417 offset
= event
->event
->u
.kretprobe
.offset
;
420 ret
= config_writer_open_element(writer
,
421 config_element_probe_attributes
);
423 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
428 ret
= config_writer_write_element_string(writer
,
429 config_element_symbol_name
,
432 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
438 ret
= config_writer_write_element_unsigned_int(
439 writer
, config_element_address
, addr
);
441 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
447 ret
= config_writer_write_element_unsigned_int(
448 writer
, config_element_offset
, offset
);
450 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
455 ret
= config_writer_close_element(writer
);
457 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
463 ERR("Unsupported kernel instrumentation type.");
464 ret
= LTTNG_ERR_INVALID
;
469 ret
= config_writer_close_element(writer
);
471 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
477 ret
= config_writer_close_element(writer
);
479 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
487 int save_kernel_events(struct config_writer
*writer
,
488 struct ltt_kernel_event_list
*event_list
)
491 struct ltt_kernel_event
*event
;
493 ret
= config_writer_open_element(writer
, config_element_events
);
495 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
499 cds_list_for_each_entry(event
, &event_list
->head
, list
) {
500 ret
= save_kernel_event(writer
, event
);
507 ret
= config_writer_close_element(writer
);
509 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
517 int save_ust_event(struct config_writer
*writer
,
518 struct ltt_ust_event
*event
)
521 const char *loglevel_type_string
;
523 ret
= config_writer_open_element(writer
, config_element_event
);
525 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
529 if (event
->attr
.name
[0]) {
530 ret
= config_writer_write_element_string(writer
,
531 config_element_name
, event
->attr
.name
);
533 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
538 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
541 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
545 if (event
->attr
.instrumentation
!= LTTNG_UST_TRACEPOINT
) {
546 ERR("Unsupported UST instrumentation type.");
547 ret
= LTTNG_ERR_INVALID
;
550 ret
= config_writer_write_element_string(writer
, config_element_type
,
551 config_event_type_tracepoint
);
553 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
557 loglevel_type_string
= get_loglevel_type_string(
558 event
->attr
.loglevel_type
);
559 if (!loglevel_type_string
) {
560 ERR("Unsupported UST loglevel type.");
561 ret
= LTTNG_ERR_INVALID
;
565 ret
= config_writer_write_element_string(writer
,
566 config_element_loglevel_type
, loglevel_type_string
);
568 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
572 ret
= config_writer_write_element_signed_int(writer
,
573 config_element_loglevel
, event
->attr
.loglevel
);
575 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
579 if (event
->filter_expression
) {
580 ret
= config_writer_write_element_string(writer
,
581 config_element_filter
, event
->filter_expression
);
583 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
588 if (event
->exclusion
&& event
->exclusion
->count
) {
591 ret
= config_writer_open_element(writer
,
592 config_element_exclusions
);
594 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
598 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
599 ret
= config_writer_write_element_string(writer
,
600 config_element_exclusion
,
601 &event
->exclusion
->names
[0][i
]);
603 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
609 ret
= config_writer_close_element(writer
);
611 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
617 ret
= config_writer_close_element(writer
);
619 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
627 int save_ust_events(struct config_writer
*writer
,
628 struct lttng_ht
*events
)
631 struct ltt_ust_event
*event
;
632 struct lttng_ht_node_str
*node
;
633 struct lttng_ht_iter iter
;
635 ret
= config_writer_open_element(writer
, config_element_events
);
637 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
642 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
643 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
645 ret
= save_ust_event(writer
, event
);
654 ret
= config_writer_close_element(writer
);
656 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
664 int save_kernel_context(struct config_writer
*writer
,
665 struct lttng_kernel_context
*ctx
)
673 ret
= config_writer_open_element(writer
, config_element_context
);
675 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
679 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
680 ret
= config_writer_open_element(writer
, config_element_perf
);
682 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
686 ret
= config_writer_write_element_unsigned_int(writer
,
687 config_element_type
, ctx
->u
.perf_counter
.type
);
689 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
693 ret
= config_writer_write_element_unsigned_int(writer
,
694 config_element_config
, ctx
->u
.perf_counter
.config
);
696 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
700 ret
= config_writer_write_element_string(writer
,
701 config_element_name
, ctx
->u
.perf_counter
.name
);
703 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
708 ret
= config_writer_close_element(writer
);
710 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
714 const char *context_type_string
=
715 get_kernel_context_type_string(ctx
->ctx
);
717 if (!context_type_string
) {
718 ERR("Unsupported kernel context type.");
719 ret
= LTTNG_ERR_INVALID
;
723 ret
= config_writer_write_element_string(writer
,
724 config_element_type
, context_type_string
);
726 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
732 ret
= config_writer_close_element(writer
);
734 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
743 int save_kernel_contexts(struct config_writer
*writer
,
744 struct ltt_kernel_channel
*kchan
)
747 struct ltt_kernel_context
*ctx
;
749 ret
= config_writer_open_element(writer
, config_element_contexts
);
751 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
755 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
756 ret
= save_kernel_context(writer
, &ctx
->ctx
);
763 ret
= config_writer_close_element(writer
);
765 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
773 int save_ust_context(struct config_writer
*writer
,
774 struct cds_list_head
*ctx_list
)
777 struct ltt_ust_context
*ctx
;
782 ret
= config_writer_open_element(writer
, config_element_contexts
);
784 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
788 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
789 const char *context_type_string
;
791 context_type_string
= get_ust_context_type_string(ctx
->ctx
.ctx
);
792 if (!context_type_string
) {
793 ERR("Unsupported UST context type.")
794 ret
= LTTNG_ERR_INVALID
;
798 ret
= config_writer_open_element(writer
,
799 config_element_context
);
801 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
805 ret
= config_writer_write_element_string(writer
,
806 config_element_type
, context_type_string
);
808 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
813 ret
= config_writer_close_element(writer
);
815 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
821 ret
= config_writer_close_element(writer
);
823 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
831 int save_kernel_channel(struct config_writer
*writer
,
832 struct ltt_kernel_channel
*kchan
)
839 ret
= config_writer_open_element(writer
, config_element_channel
);
841 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
845 ret
= config_writer_write_element_string(writer
, config_element_name
,
846 kchan
->channel
->name
);
848 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
852 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
853 kchan
->channel
->enabled
);
855 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
859 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
864 ret
= save_kernel_events(writer
, &kchan
->events_list
);
869 ret
= save_kernel_contexts(writer
, kchan
);
875 ret
= config_writer_close_element(writer
);
877 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
885 int save_ust_channel(struct config_writer
*writer
,
886 struct ltt_ust_channel
*ust_chan
,
887 struct ltt_ust_session
*session
)
895 ret
= config_writer_open_element(writer
, config_element_channel
);
897 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
901 ret
= config_writer_write_element_string(writer
, config_element_name
,
904 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
908 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
911 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
915 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
920 ret
= config_writer_write_element_unsigned_int(writer
,
921 config_element_tracefile_size
, ust_chan
->tracefile_size
);
923 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
927 ret
= config_writer_write_element_unsigned_int(writer
,
928 config_element_tracefile_count
, ust_chan
->tracefile_count
);
930 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
934 ret
= config_writer_write_element_unsigned_int(writer
,
935 config_element_live_timer_interval
,
936 session
->live_timer_interval
);
938 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
942 ret
= save_ust_events(writer
, ust_chan
->events
);
944 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
948 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
954 ret
= config_writer_close_element(writer
);
956 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
964 int save_kernel_session(struct config_writer
*writer
,
965 struct ltt_session
*session
)
968 struct ltt_kernel_channel
*kchan
;
973 ret
= config_writer_write_element_string(writer
, config_element_type
,
974 config_domain_type_kernel
);
976 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
980 ret
= config_writer_write_element_string(writer
,
981 config_element_buffer_type
, config_buffer_type_global
);
983 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
987 ret
= config_writer_open_element(writer
,
988 config_element_channels
);
990 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
994 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
996 ret
= save_kernel_channel(writer
, kchan
);
1003 ret
= config_writer_close_element(writer
);
1005 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1013 int save_ust_session(struct config_writer
*writer
,
1014 struct ltt_session
*session
, int save_jul
)
1017 struct ltt_ust_channel
*ust_chan
;
1018 const char *buffer_type_string
;
1019 struct lttng_ht_node_str
*node
;
1020 struct lttng_ht_iter iter
;
1025 ret
= config_writer_write_element_string(writer
, config_element_type
,
1026 save_jul
? config_domain_type_jul
: config_domain_type_ust
);
1028 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1032 buffer_type_string
= get_buffer_type_string(
1033 session
->ust_session
->buffer_type
);
1034 if (!buffer_type_string
) {
1035 ERR("Unsupported buffer type.");
1036 ret
= LTTNG_ERR_INVALID
;
1040 ret
= config_writer_write_element_string(writer
,
1041 config_element_buffer_type
, buffer_type_string
);
1043 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1047 ret
= config_writer_open_element(writer
, config_element_channels
);
1049 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1054 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1055 &iter
.iter
, node
, node
) {
1058 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1059 jul_channel
= !strcmp(DEFAULT_JUL_CHANNEL_NAME
, ust_chan
->name
);
1060 if (!(save_jul
^ jul_channel
)) {
1061 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1071 ret
= config_writer_close_element(writer
);
1073 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1081 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1088 if (!session
->kernel_session
&& !session
->ust_session
) {
1092 ret
= config_writer_open_element(writer
, config_element_domains
);
1094 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1099 if (session
->kernel_session
) {
1100 ret
= config_writer_open_element(writer
,
1101 config_element_domain
);
1103 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1107 ret
= save_kernel_session(writer
, session
);
1113 ret
= config_writer_close_element(writer
);
1115 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1120 if (session
->ust_session
) {
1121 ret
= config_writer_open_element(writer
,
1122 config_element_domain
);
1124 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1128 ret
= save_ust_session(writer
, session
, 0);
1134 ret
= config_writer_close_element(writer
);
1136 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1141 if (session
->ust_session
&&
1142 session
->ust_session
->domain_jul
.being_used
) {
1143 ret
= config_writer_open_element(writer
,
1144 config_element_domain
);
1146 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1150 ret
= save_ust_session(writer
, session
, 1);
1156 ret
= config_writer_close_element(writer
);
1158 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1164 ret
= config_writer_close_element(writer
);
1166 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1174 int save_consumer_output(struct config_writer
*writer
,
1175 struct consumer_output
*output
)
1182 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1184 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1188 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1191 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1195 ret
= config_writer_open_element(writer
, config_element_destination
);
1197 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1201 switch (output
->type
) {
1202 case CONSUMER_DST_LOCAL
:
1203 ret
= config_writer_write_element_string(writer
,
1204 config_element_path
, output
->dst
.trace_path
);
1206 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1210 case CONSUMER_DST_NET
:
1214 uri
= zmalloc(PATH_MAX
);
1216 ret
= LTTNG_ERR_NOMEM
;
1220 ret
= config_writer_open_element(writer
, config_element_net_output
);
1222 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1223 goto end_net_output
;
1226 if (output
->dst
.net
.control_isset
&&
1227 output
->dst
.net
.data_isset
) {
1228 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1230 ret
= LTTNG_ERR_INVALID
;
1231 goto end_net_output
;
1234 ret
= config_writer_write_element_string(writer
,
1235 config_element_control_uri
, uri
);
1237 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1238 goto end_net_output
;
1241 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1243 ret
= LTTNG_ERR_INVALID
;
1244 goto end_net_output
;
1247 ret
= config_writer_write_element_string(writer
,
1248 config_element_data_uri
, uri
);
1250 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1251 goto end_net_output
;
1260 ret
= !output
->dst
.net
.control_isset
?
1261 LTTNG_ERR_URL_CTRL_MISS
:
1262 LTTNG_ERR_URL_DATA_MISS
;
1267 ret
= config_writer_close_element(writer
);
1269 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1275 ERR("Unsupported consumer output type.");
1276 ret
= LTTNG_ERR_INVALID
;
1281 ret
= config_writer_close_element(writer
);
1283 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1287 /* /consumer_output */
1288 ret
= config_writer_close_element(writer
);
1290 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1298 int save_snapshot_outputs(struct config_writer
*writer
,
1299 struct snapshot
*snapshot
)
1302 struct lttng_ht_iter iter
;
1303 struct snapshot_output
*output
;
1308 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1310 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1315 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1317 ret
= config_writer_open_element(writer
,
1318 config_element_output
);
1320 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1324 ret
= config_writer_write_element_string(writer
,
1325 config_element_name
, output
->name
);
1327 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1331 ret
= config_writer_write_element_unsigned_int(writer
,
1332 config_element_max_size
, output
->max_size
);
1334 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1338 ret
= save_consumer_output(writer
, output
->consumer
);
1344 ret
= config_writer_close_element(writer
);
1346 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1352 /* /snapshot_outputs */
1353 ret
= config_writer_close_element(writer
);
1355 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1367 int save_session_output(struct config_writer
*writer
,
1368 struct ltt_session
*session
)
1375 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1376 (!session
->snapshot_mode
&& !session
->consumer
)) {
1377 /* Session is in no output mode */
1382 ret
= config_writer_open_element(writer
, config_element_output
);
1384 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1388 if (session
->snapshot_mode
) {
1389 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1394 if (session
->consumer
) {
1395 ret
= save_consumer_output(writer
, session
->consumer
);
1403 ret
= config_writer_close_element(writer
);
1405 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1413 * Save the given session.
1415 * Return 0 on success else a LTTNG_ERR* code.
1418 int save_session(struct ltt_session
*session
,
1419 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1422 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1423 char config_file_path
[PATH_MAX
];
1425 struct config_writer
*writer
= NULL
;
1426 size_t session_name_len
;
1427 const char *provided_path
;
1433 session_name_len
= strlen(session
->name
);
1434 memset(config_file_path
, 0, sizeof(config_file_path
));
1436 if (!session_access_ok(session
,
1437 LTTNG_SOCK_GET_UID_CRED(creds
),
1438 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1439 ret
= LTTNG_ERR_EPERM
;
1443 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1444 if (provided_path
) {
1445 DBG3("Save session in provided path %s", provided_path
);
1446 len
= strlen(provided_path
);
1447 if (len
>= sizeof(config_file_path
)) {
1448 ret
= LTTNG_ERR_SET_URL
;
1451 strncpy(config_file_path
, provided_path
, len
);
1454 char *home_dir
= utils_get_user_home_dir(
1455 LTTNG_SOCK_GET_UID_CRED(creds
));
1457 ret
= LTTNG_ERR_SET_URL
;
1461 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1462 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1465 PERROR("snprintf save session");
1466 ret
= LTTNG_ERR_SET_URL
;
1473 * Check the path fits in the config file path dst including the '/'
1474 * followed by trailing .lttng extension and the NULL terminated string.
1476 if ((len
+ session_name_len
+ 2 +
1477 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1478 > sizeof(config_file_path
)) {
1479 ret
= LTTNG_ERR_SET_URL
;
1483 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1484 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1486 ret
= LTTNG_ERR_SET_URL
;
1491 * At this point, we know that everything fits in the buffer. Validation
1492 * was done just above.
1494 config_file_path
[len
++] = '/';
1495 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1496 len
+= session_name_len
;
1497 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1498 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1499 config_file_path
[len
] = '\0';
1501 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1502 /* File exists, notify the user since the overwrite flag is off. */
1503 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1507 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1508 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1509 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1511 PERROR("Could not create configuration file");
1512 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1517 writer
= config_writer_create(fd
);
1519 ret
= LTTNG_ERR_NOMEM
;
1523 ret
= config_writer_open_element(writer
, config_element_sessions
);
1525 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1529 ret
= config_writer_open_element(writer
, config_element_session
);
1531 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1535 ret
= config_writer_write_element_string(writer
, config_element_name
,
1538 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1542 ret
= save_domains(writer
, session
);
1547 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1550 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1554 if (session
->snapshot_mode
|| session
->live_timer
) {
1555 ret
= config_writer_open_element(writer
, config_element_attributes
);
1557 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1561 if (session
->snapshot_mode
) {
1562 ret
= config_writer_write_element_bool(writer
,
1563 config_element_snapshot_mode
, 1);
1565 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1569 ret
= config_writer_write_element_unsigned_int(writer
,
1570 config_element_live_timer_interval
, session
->live_timer
);
1572 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1578 ret
= config_writer_close_element(writer
);
1580 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1585 ret
= save_session_output(writer
, session
);
1591 ret
= config_writer_close_element(writer
);
1593 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1598 ret
= config_writer_close_element(writer
);
1600 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1604 if (writer
&& config_writer_destroy(writer
)) {
1605 /* Preserve the original error code */
1606 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
1609 /* Delete file in case of error */
1610 if (file_opened
&& unlink(config_file_path
)) {
1611 PERROR("Unlinking XML session configuration.");
1618 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
1619 lttng_sock_cred
*creds
)
1622 const char *session_name
;
1623 struct ltt_session
*session
;
1625 session_lock_list();
1627 session_name
= lttng_save_session_attr_get_session_name(attr
);
1629 session
= session_find_by_name(session_name
);
1631 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1635 session_lock(session
);
1636 ret
= save_session(session
, attr
, creds
);
1637 session_unlock(session
);
1642 struct ltt_session_list
*list
= session_get_list();
1644 cds_list_for_each_entry(session
, &list
->head
, list
) {
1645 session_lock(session
);
1646 ret
= save_session(session
, attr
, creds
);
1647 session_unlock(session
);
1649 /* Don't abort if we don't have the required permissions. */
1650 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
1658 session_unlock_list();