2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/config/session-config.h>
28 #include <common/utils.h>
29 #include <common/runas.h>
30 #include <lttng/save-internal.h>
36 #include "trace-ust.h"
40 int save_kernel_channel_attributes(struct config_writer
*writer
,
41 struct lttng_channel_attr
*attr
)
45 ret
= config_writer_write_element_string(writer
,
46 config_element_overwrite_mode
,
47 attr
->overwrite
? config_overwrite_mode_overwrite
:
48 config_overwrite_mode_discard
);
53 ret
= config_writer_write_element_unsigned_int(writer
,
54 config_element_subbuf_size
, attr
->subbuf_size
);
59 ret
= config_writer_write_element_unsigned_int(writer
,
60 config_element_num_subbuf
,
66 ret
= config_writer_write_element_unsigned_int(writer
,
67 config_element_switch_timer_interval
,
68 attr
->switch_timer_interval
);
73 ret
= config_writer_write_element_unsigned_int(writer
,
74 config_element_read_timer_interval
,
75 attr
->read_timer_interval
);
80 ret
= config_writer_write_element_string(writer
,
81 config_element_output_type
,
82 attr
->output
== LTTNG_EVENT_SPLICE
?
83 config_output_type_splice
: config_output_type_mmap
);
88 ret
= config_writer_write_element_unsigned_int(writer
,
89 config_element_tracefile_size
, attr
->tracefile_size
);
94 ret
= config_writer_write_element_unsigned_int(writer
,
95 config_element_tracefile_count
,
96 attr
->tracefile_count
);
101 ret
= config_writer_write_element_unsigned_int(writer
,
102 config_element_live_timer_interval
,
103 attr
->live_timer_interval
);
108 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
112 int save_ust_channel_attributes(struct config_writer
*writer
,
113 struct lttng_ust_channel_attr
*attr
)
117 ret
= config_writer_write_element_string(writer
,
118 config_element_overwrite_mode
,
119 attr
->overwrite
? config_overwrite_mode_overwrite
:
120 config_overwrite_mode_discard
);
125 ret
= config_writer_write_element_unsigned_int(writer
,
126 config_element_subbuf_size
, attr
->subbuf_size
);
131 ret
= config_writer_write_element_unsigned_int(writer
,
132 config_element_num_subbuf
,
138 ret
= config_writer_write_element_unsigned_int(writer
,
139 config_element_switch_timer_interval
,
140 attr
->switch_timer_interval
);
145 ret
= config_writer_write_element_unsigned_int(writer
,
146 config_element_read_timer_interval
,
147 attr
->read_timer_interval
);
152 ret
= config_writer_write_element_string(writer
,
153 config_element_output_type
,
154 attr
->output
== LTTNG_UST_MMAP
?
155 config_output_type_mmap
: config_output_type_splice
);
160 return ret
? LTTNG_ERR_SAVE_IO_FAIL
: 0;
164 const char *get_kernel_instrumentation_string(
165 enum lttng_kernel_instrumentation instrumentation
)
167 const char *instrumentation_string
;
169 switch (instrumentation
) {
170 case LTTNG_KERNEL_ALL
:
171 instrumentation_string
= config_event_type_all
;
173 case LTTNG_KERNEL_TRACEPOINT
:
174 instrumentation_string
= config_event_type_tracepoint
;
176 case LTTNG_KERNEL_KPROBE
:
177 instrumentation_string
= config_event_type_kprobe
;
179 case LTTNG_KERNEL_FUNCTION
:
180 instrumentation_string
= config_event_type_function
;
182 case LTTNG_KERNEL_KRETPROBE
:
183 instrumentation_string
= config_event_type_kretprobe
;
185 case LTTNG_KERNEL_NOOP
:
186 instrumentation_string
= config_event_type_noop
;
188 case LTTNG_KERNEL_SYSCALL
:
189 instrumentation_string
= config_event_type_syscall
;
192 instrumentation_string
= NULL
;
195 return instrumentation_string
;
199 const char *get_kernel_context_type_string(
200 enum lttng_kernel_context_type context_type
)
202 const char *context_type_string
;
204 switch (context_type
) {
205 case LTTNG_KERNEL_CONTEXT_PID
:
206 context_type_string
= config_event_context_pid
;
208 case LTTNG_KERNEL_CONTEXT_PROCNAME
:
209 context_type_string
= config_event_context_procname
;
211 case LTTNG_KERNEL_CONTEXT_PRIO
:
212 context_type_string
= config_event_context_prio
;
214 case LTTNG_KERNEL_CONTEXT_NICE
:
215 context_type_string
= config_event_context_nice
;
217 case LTTNG_KERNEL_CONTEXT_VPID
:
218 context_type_string
= config_event_context_vpid
;
220 case LTTNG_KERNEL_CONTEXT_TID
:
221 context_type_string
= config_event_context_tid
;
223 case LTTNG_KERNEL_CONTEXT_VTID
:
224 context_type_string
= config_event_context_vtid
;
226 case LTTNG_KERNEL_CONTEXT_PPID
:
227 context_type_string
= config_event_context_ppid
;
229 case LTTNG_KERNEL_CONTEXT_VPPID
:
230 context_type_string
= config_event_context_vppid
;
232 case LTTNG_KERNEL_CONTEXT_HOSTNAME
:
233 context_type_string
= config_event_context_hostname
;
236 context_type_string
= NULL
;
239 return context_type_string
;
243 const char *get_ust_context_type_string(
244 enum lttng_ust_context_type context_type
)
246 const char *context_type_string
;
248 switch (context_type
) {
249 case LTTNG_UST_CONTEXT_PROCNAME
:
250 context_type_string
= config_event_context_procname
;
252 case LTTNG_UST_CONTEXT_VPID
:
253 context_type_string
= config_event_context_vpid
;
255 case LTTNG_UST_CONTEXT_VTID
:
256 context_type_string
= config_event_context_vtid
;
258 case LTTNG_UST_CONTEXT_IP
:
259 context_type_string
= config_event_context_ip
;
261 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
262 context_type_string
= config_event_context_pthread_id
;
264 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
266 * Error, should not be stored in the XML, perf contexts
267 * are stored as a node of type event_perf_context_type.
270 context_type_string
= NULL
;
274 return context_type_string
;
278 const char *get_buffer_type_string(
279 enum lttng_buffer_type buffer_type
)
281 const char *buffer_type_string
;
283 switch (buffer_type
) {
284 case LTTNG_BUFFER_PER_PID
:
285 buffer_type_string
= config_buffer_type_per_pid
;
287 case LTTNG_BUFFER_PER_UID
:
288 buffer_type_string
= config_buffer_type_per_uid
;
290 case LTTNG_BUFFER_GLOBAL
:
291 buffer_type_string
= config_buffer_type_global
;
294 buffer_type_string
= NULL
;
297 return buffer_type_string
;
301 const char *get_loglevel_type_string(
302 enum lttng_ust_loglevel_type loglevel_type
)
304 const char *loglevel_type_string
;
306 switch (loglevel_type
) {
307 case LTTNG_UST_LOGLEVEL_ALL
:
308 loglevel_type_string
= config_loglevel_type_all
;
310 case LTTNG_UST_LOGLEVEL_RANGE
:
311 loglevel_type_string
= config_loglevel_type_range
;
313 case LTTNG_UST_LOGLEVEL_SINGLE
:
314 loglevel_type_string
= config_loglevel_type_single
;
317 loglevel_type_string
= NULL
;
320 return loglevel_type_string
;
324 int save_kernel_event(struct config_writer
*writer
,
325 struct ltt_kernel_event
*event
)
328 const char *instrumentation_type
;
330 ret
= config_writer_open_element(writer
, config_element_event
);
332 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
336 if (event
->event
->name
[0]) {
337 ret
= config_writer_write_element_string(writer
,
338 config_element_name
, event
->event
->name
);
340 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
345 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
348 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
352 instrumentation_type
= get_kernel_instrumentation_string(
353 event
->event
->instrumentation
);
354 if (!instrumentation_type
) {
355 ret
= LTTNG_ERR_INVALID
;
359 ret
= config_writer_write_element_string(writer
, config_element_type
,
360 instrumentation_type
);
362 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
366 if (event
->filter_expression
) {
367 ret
= config_writer_write_element_string(writer
,
368 config_element_filter
,
369 event
->filter_expression
);
371 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
376 if (event
->event
->instrumentation
== LTTNG_KERNEL_FUNCTION
||
377 event
->event
->instrumentation
== LTTNG_KERNEL_KPROBE
||
378 event
->event
->instrumentation
== LTTNG_KERNEL_KRETPROBE
) {
380 ret
= config_writer_open_element(writer
,
381 config_element_attributes
);
383 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
387 switch (event
->event
->instrumentation
) {
388 case LTTNG_KERNEL_SYSCALL
:
389 case LTTNG_KERNEL_FUNCTION
:
390 ret
= config_writer_open_element(writer
,
391 config_element_function_attributes
);
393 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
397 ret
= config_writer_write_element_string(writer
,
399 event
->event
->u
.ftrace
.symbol_name
);
401 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
405 /* /function attributes */
406 ret
= config_writer_close_element(writer
);
408 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
412 case LTTNG_KERNEL_KPROBE
:
413 case LTTNG_KERNEL_KRETPROBE
:
415 const char *symbol_name
;
419 if (event
->event
->instrumentation
==
420 LTTNG_KERNEL_KPROBE
) {
422 * Comments in lttng-kernel.h mention that
423 * either addr or symbol_name are set, not both.
425 addr
= event
->event
->u
.kprobe
.addr
;
426 offset
= event
->event
->u
.kprobe
.offset
;
427 symbol_name
= addr
? NULL
:
428 event
->event
->u
.kprobe
.symbol_name
;
431 event
->event
->u
.kretprobe
.symbol_name
;
432 addr
= event
->event
->u
.kretprobe
.addr
;
433 offset
= event
->event
->u
.kretprobe
.offset
;
436 ret
= config_writer_open_element(writer
,
437 config_element_probe_attributes
);
439 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
444 ret
= config_writer_write_element_string(writer
,
445 config_element_symbol_name
,
448 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
454 ret
= config_writer_write_element_unsigned_int(
455 writer
, config_element_address
, addr
);
457 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
463 ret
= config_writer_write_element_unsigned_int(
464 writer
, config_element_offset
, offset
);
466 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
471 ret
= config_writer_close_element(writer
);
473 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
479 ERR("Unsupported kernel instrumentation type.");
480 ret
= LTTNG_ERR_INVALID
;
485 ret
= config_writer_close_element(writer
);
487 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
493 ret
= config_writer_close_element(writer
);
495 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
503 int save_kernel_syscall(struct config_writer
*writer
,
504 struct ltt_kernel_channel
*kchan
)
508 struct lttng_event
*events
= NULL
;
513 count
= syscall_list_channel(kchan
, &events
, 0);
515 /* No syscalls, just gracefully return. */
520 for (i
= 0; i
< count
; i
++) {
521 struct ltt_kernel_event
*kevent
;
523 /* Create a temporary kevent in order to save it. */
525 * TODO: struct lttng_event does not really work for a filter,
526 * but unfortunately, it is exposed as external API (and used as
527 * internal representation. Using NULL meanwhile.
529 kevent
= trace_kernel_create_event(&events
[i
],
535 /* Init list in order so the destroy call can del the node. */
536 CDS_INIT_LIST_HEAD(&kevent
->list
);
538 ret
= save_kernel_event(writer
, kevent
);
539 trace_kernel_destroy_event(kevent
);
545 /* Everything went well */
554 int save_kernel_events(struct config_writer
*writer
,
555 struct ltt_kernel_channel
*kchan
)
558 struct ltt_kernel_event
*event
;
560 ret
= config_writer_open_element(writer
, config_element_events
);
562 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
566 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
567 ret
= save_kernel_event(writer
, event
);
573 /* Save syscalls if any. */
574 ret
= save_kernel_syscall(writer
, kchan
);
580 ret
= config_writer_close_element(writer
);
582 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
590 int save_ust_event(struct config_writer
*writer
,
591 struct ltt_ust_event
*event
)
594 const char *loglevel_type_string
;
596 ret
= config_writer_open_element(writer
, config_element_event
);
598 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
602 if (event
->attr
.name
[0]) {
603 ret
= config_writer_write_element_string(writer
,
604 config_element_name
, event
->attr
.name
);
606 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
611 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
614 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
618 if (event
->attr
.instrumentation
!= LTTNG_UST_TRACEPOINT
) {
619 ERR("Unsupported UST instrumentation type.");
620 ret
= LTTNG_ERR_INVALID
;
623 ret
= config_writer_write_element_string(writer
, config_element_type
,
624 config_event_type_tracepoint
);
626 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
630 loglevel_type_string
= get_loglevel_type_string(
631 event
->attr
.loglevel_type
);
632 if (!loglevel_type_string
) {
633 ERR("Unsupported UST loglevel type.");
634 ret
= LTTNG_ERR_INVALID
;
638 ret
= config_writer_write_element_string(writer
,
639 config_element_loglevel_type
, loglevel_type_string
);
641 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
645 /* The log level is irrelevant if no "filtering" is enabled */
646 if (event
->attr
.loglevel_type
!= LTTNG_UST_LOGLEVEL_ALL
) {
647 ret
= config_writer_write_element_signed_int(writer
,
648 config_element_loglevel
, event
->attr
.loglevel
);
650 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
655 if (event
->filter_expression
) {
656 ret
= config_writer_write_element_string(writer
,
657 config_element_filter
, event
->filter_expression
);
659 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
664 if (event
->exclusion
&& event
->exclusion
->count
) {
667 ret
= config_writer_open_element(writer
,
668 config_element_exclusions
);
670 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
674 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
675 ret
= config_writer_write_element_string(writer
,
676 config_element_exclusion
,
677 LTTNG_EVENT_EXCLUSION_NAME_AT(
678 event
->exclusion
, i
));
680 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
686 ret
= config_writer_close_element(writer
);
688 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
694 ret
= config_writer_close_element(writer
);
696 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
704 int save_ust_events(struct config_writer
*writer
,
705 struct lttng_ht
*events
)
708 struct ltt_ust_event
*event
;
709 struct lttng_ht_node_str
*node
;
710 struct lttng_ht_iter iter
;
712 ret
= config_writer_open_element(writer
, config_element_events
);
714 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
719 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
720 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
722 if (event
->internal
) {
723 /* Internal events must not be exposed to clients */
726 ret
= save_ust_event(writer
, event
);
735 ret
= config_writer_close_element(writer
);
737 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
745 int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
746 struct agent_event
*agent_event
)
749 enum lttng_ust_loglevel_type ust_loglevel_type
;
751 ust_event
->enabled
= agent_event
->enabled
;
752 ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
753 strncpy(ust_event
->attr
.name
, agent_event
->name
, LTTNG_SYMBOL_NAME_LEN
);
754 switch (agent_event
->loglevel_type
) {
755 case LTTNG_EVENT_LOGLEVEL_ALL
:
756 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
758 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
759 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
761 case LTTNG_EVENT_LOGLEVEL_RANGE
:
762 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
765 ERR("Invalid agent_event loglevel_type.");
770 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
771 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
772 ust_event
->filter_expression
= agent_event
->filter_expression
;
773 ust_event
->exclusion
= agent_event
->exclusion
;
779 int save_agent_events(struct config_writer
*writer
,
780 struct ltt_ust_channel
*chan
,
784 struct lttng_ht_iter iter
;
785 struct lttng_ht_node_str
*node
;
787 ret
= config_writer_open_element(writer
, config_element_events
);
789 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
794 cds_lfht_for_each_entry(agent
->events
->ht
, &iter
.iter
, node
, node
) {
796 struct agent_event
*agent_event
;
797 struct ltt_ust_event fake_event
;
799 memset(&fake_event
, 0, sizeof(fake_event
));
800 agent_event
= caa_container_of(node
, struct agent_event
, node
);
803 * Initialize a fake ust event to reuse the same serialization
804 * function since UST and agent events contain the same info
805 * (and one could wonder why they don't reuse the same
808 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
813 ret
= save_ust_event(writer
, &fake_event
);
822 ret
= config_writer_close_element(writer
);
824 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
832 int save_kernel_context(struct config_writer
*writer
,
833 struct lttng_kernel_context
*ctx
)
841 ret
= config_writer_open_element(writer
, config_element_context
);
843 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
847 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
848 ret
= config_writer_open_element(writer
, config_element_perf
);
850 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
854 ret
= config_writer_write_element_unsigned_int(writer
,
855 config_element_type
, ctx
->u
.perf_counter
.type
);
857 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
861 ret
= config_writer_write_element_unsigned_int(writer
,
862 config_element_config
, ctx
->u
.perf_counter
.config
);
864 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
868 ret
= config_writer_write_element_string(writer
,
869 config_element_name
, ctx
->u
.perf_counter
.name
);
871 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
876 ret
= config_writer_close_element(writer
);
878 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
882 const char *context_type_string
=
883 get_kernel_context_type_string(ctx
->ctx
);
885 if (!context_type_string
) {
886 ERR("Unsupported kernel context type.");
887 ret
= LTTNG_ERR_INVALID
;
891 ret
= config_writer_write_element_string(writer
,
892 config_element_type
, context_type_string
);
894 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
900 ret
= config_writer_close_element(writer
);
902 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
911 int save_kernel_contexts(struct config_writer
*writer
,
912 struct ltt_kernel_channel
*kchan
)
915 struct ltt_kernel_context
*ctx
;
917 if (cds_list_empty(&kchan
->ctx_list
)) {
922 ret
= config_writer_open_element(writer
, config_element_contexts
);
924 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
928 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
929 ret
= save_kernel_context(writer
, &ctx
->ctx
);
936 ret
= config_writer_close_element(writer
);
938 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
946 int save_ust_context(struct config_writer
*writer
,
947 struct cds_list_head
*ctx_list
)
950 struct ltt_ust_context
*ctx
;
955 ret
= config_writer_open_element(writer
, config_element_contexts
);
957 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
961 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
962 const char *context_type_string
;
965 ret
= config_writer_open_element(writer
,
966 config_element_context
);
968 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
972 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
973 /* Perf contexts are saved as event_perf_context_type */
974 ret
= config_writer_open_element(writer
,
975 config_element_perf
);
977 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
981 ret
= config_writer_write_element_unsigned_int(writer
,
983 ctx
->ctx
.u
.perf_counter
.type
);
985 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
989 ret
= config_writer_write_element_unsigned_int(writer
,
990 config_element_config
,
991 ctx
->ctx
.u
.perf_counter
.config
);
993 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
997 ret
= config_writer_write_element_string(writer
,
999 ctx
->ctx
.u
.perf_counter
.name
);
1001 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1006 ret
= config_writer_close_element(writer
);
1008 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1012 /* Save context as event_context_type_type */
1013 context_type_string
= get_ust_context_type_string(
1015 if (!context_type_string
) {
1016 ERR("Unsupported UST context type.")
1017 ret
= LTTNG_ERR_INVALID
;
1021 ret
= config_writer_write_element_string(writer
,
1022 config_element_type
, context_type_string
);
1024 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1030 ret
= config_writer_close_element(writer
);
1032 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1038 ret
= config_writer_close_element(writer
);
1040 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1048 int save_kernel_channel(struct config_writer
*writer
,
1049 struct ltt_kernel_channel
*kchan
)
1056 ret
= config_writer_open_element(writer
, config_element_channel
);
1058 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1062 ret
= config_writer_write_element_string(writer
, config_element_name
,
1063 kchan
->channel
->name
);
1065 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1069 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1070 kchan
->channel
->enabled
);
1072 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1076 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1081 ret
= save_kernel_events(writer
, kchan
);
1086 ret
= save_kernel_contexts(writer
, kchan
);
1092 ret
= config_writer_close_element(writer
);
1094 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1102 int save_ust_channel(struct config_writer
*writer
,
1103 struct ltt_ust_channel
*ust_chan
,
1104 struct ltt_ust_session
*session
)
1112 ret
= config_writer_open_element(writer
, config_element_channel
);
1114 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1118 ret
= config_writer_write_element_string(writer
, config_element_name
,
1121 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1125 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1128 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1132 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1137 ret
= config_writer_write_element_unsigned_int(writer
,
1138 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1140 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1144 ret
= config_writer_write_element_unsigned_int(writer
,
1145 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1147 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1151 ret
= config_writer_write_element_unsigned_int(writer
,
1152 config_element_live_timer_interval
,
1153 session
->live_timer_interval
);
1155 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1159 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1160 ret
= save_ust_events(writer
, ust_chan
->events
);
1165 struct agent
*agent
= NULL
;
1167 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1169 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1170 ERR("Could not find agent associated to UST subdomain");
1175 * Channels associated with a UST sub-domain (such as JUL, Log4j
1176 * or Python) don't have any non-internal events. We retrieve
1177 * the "agent" events associated with this channel and serialize
1180 ret
= save_agent_events(writer
, ust_chan
, agent
);
1186 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1192 ret
= config_writer_close_element(writer
);
1194 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1202 int save_kernel_session(struct config_writer
*writer
,
1203 struct ltt_session
*session
)
1206 struct ltt_kernel_channel
*kchan
;
1211 ret
= config_writer_write_element_string(writer
, config_element_type
,
1212 config_domain_type_kernel
);
1214 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1218 ret
= config_writer_write_element_string(writer
,
1219 config_element_buffer_type
, config_buffer_type_global
);
1221 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1225 ret
= config_writer_open_element(writer
,
1226 config_element_channels
);
1228 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1232 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1234 ret
= save_kernel_channel(writer
, kchan
);
1241 ret
= config_writer_close_element(writer
);
1243 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1251 const char *get_config_domain_str(enum lttng_domain_type domain
)
1253 const char *str_dom
;
1256 case LTTNG_DOMAIN_KERNEL
:
1257 str_dom
= config_domain_type_kernel
;
1259 case LTTNG_DOMAIN_UST
:
1260 str_dom
= config_domain_type_ust
;
1262 case LTTNG_DOMAIN_JUL
:
1263 str_dom
= config_domain_type_jul
;
1265 case LTTNG_DOMAIN_LOG4J
:
1266 str_dom
= config_domain_type_log4j
;
1268 case LTTNG_DOMAIN_PYTHON
:
1269 str_dom
= config_domain_type_python
;
1279 int save_pid_tracker(struct config_writer
*writer
,
1280 struct ltt_session
*sess
, int domain
)
1283 ssize_t nr_pids
= 0, i
;
1284 int32_t *pids
= NULL
;
1287 case LTTNG_DOMAIN_KERNEL
:
1289 nr_pids
= kernel_list_tracker_pids(sess
->kernel_session
, &pids
);
1291 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1296 case LTTNG_DOMAIN_UST
:
1298 nr_pids
= trace_ust_list_tracker_pids(sess
->ust_session
, &pids
);
1300 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1305 case LTTNG_DOMAIN_JUL
:
1306 case LTTNG_DOMAIN_LOG4J
:
1307 case LTTNG_DOMAIN_PYTHON
:
1309 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1313 /* Only create a pid_tracker if enabled or untrack all */
1314 if (nr_pids
!= 1 || (nr_pids
== 1 && pids
[0] != -1)) {
1315 ret
= config_writer_open_element(writer
,
1316 config_element_pid_tracker
);
1318 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1322 ret
= config_writer_open_element(writer
,
1323 config_element_targets
);
1325 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1329 for (i
= 0; i
< nr_pids
; i
++) {
1330 ret
= config_writer_open_element(writer
,
1331 config_element_target_pid
);
1333 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1337 ret
= config_writer_write_element_unsigned_int(writer
,
1338 config_element_pid
, pids
[i
]);
1340 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1345 ret
= config_writer_close_element(writer
);
1347 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1353 ret
= config_writer_close_element(writer
);
1355 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1360 ret
= config_writer_close_element(writer
);
1362 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1372 int save_ust_domain(struct config_writer
*writer
,
1373 struct ltt_session
*session
, enum lttng_domain_type domain
)
1376 struct ltt_ust_channel
*ust_chan
;
1377 const char *buffer_type_string
;
1378 struct lttng_ht_node_str
*node
;
1379 struct lttng_ht_iter iter
;
1380 const char *config_domain_name
;
1385 ret
= config_writer_open_element(writer
,
1386 config_element_domain
);
1388 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1392 config_domain_name
= get_config_domain_str(domain
);
1393 if (!config_domain_name
) {
1394 ret
= LTTNG_ERR_INVALID
;
1398 ret
= config_writer_write_element_string(writer
,
1399 config_element_type
, config_domain_name
);
1401 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1405 buffer_type_string
= get_buffer_type_string(
1406 session
->ust_session
->buffer_type
);
1407 if (!buffer_type_string
) {
1408 ERR("Unsupported buffer type.");
1409 ret
= LTTNG_ERR_INVALID
;
1413 ret
= config_writer_write_element_string(writer
,
1414 config_element_buffer_type
, buffer_type_string
);
1416 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1420 ret
= config_writer_open_element(writer
, config_element_channels
);
1422 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1427 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1428 &iter
.iter
, node
, node
) {
1429 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1430 if (domain
== ust_chan
->domain
) {
1431 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1441 ret
= config_writer_close_element(writer
);
1443 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1447 if (domain
== LTTNG_DOMAIN_UST
) {
1448 ret
= config_writer_open_element(writer
,
1449 config_element_trackers
);
1451 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1455 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_UST
);
1461 ret
= config_writer_close_element(writer
);
1468 ret
= config_writer_close_element(writer
);
1470 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1479 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1486 if (!session
->kernel_session
&& !session
->ust_session
) {
1490 ret
= config_writer_open_element(writer
, config_element_domains
);
1492 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1497 if (session
->kernel_session
) {
1498 ret
= config_writer_open_element(writer
,
1499 config_element_domain
);
1501 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1505 ret
= save_kernel_session(writer
, session
);
1510 ret
= config_writer_open_element(writer
,
1511 config_element_trackers
);
1513 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1517 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_KERNEL
);
1523 ret
= config_writer_close_element(writer
);
1525 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1529 ret
= config_writer_close_element(writer
);
1531 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1536 if (session
->ust_session
) {
1537 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
1542 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
1547 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
1552 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
1559 ret
= config_writer_close_element(writer
);
1561 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1569 int save_consumer_output(struct config_writer
*writer
,
1570 struct consumer_output
*output
)
1577 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1579 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1583 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1586 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1590 ret
= config_writer_open_element(writer
, config_element_destination
);
1592 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1596 switch (output
->type
) {
1597 case CONSUMER_DST_LOCAL
:
1598 ret
= config_writer_write_element_string(writer
,
1599 config_element_path
, output
->dst
.trace_path
);
1601 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1605 case CONSUMER_DST_NET
:
1609 uri
= zmalloc(PATH_MAX
);
1611 ret
= LTTNG_ERR_NOMEM
;
1615 ret
= config_writer_open_element(writer
, config_element_net_output
);
1617 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1618 goto end_net_output
;
1621 if (output
->dst
.net
.control_isset
&&
1622 output
->dst
.net
.data_isset
) {
1623 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1625 ret
= LTTNG_ERR_INVALID
;
1626 goto end_net_output
;
1629 ret
= config_writer_write_element_string(writer
,
1630 config_element_control_uri
, uri
);
1632 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1633 goto end_net_output
;
1636 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1638 ret
= LTTNG_ERR_INVALID
;
1639 goto end_net_output
;
1642 ret
= config_writer_write_element_string(writer
,
1643 config_element_data_uri
, uri
);
1645 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1646 goto end_net_output
;
1655 ret
= !output
->dst
.net
.control_isset
?
1656 LTTNG_ERR_URL_CTRL_MISS
:
1657 LTTNG_ERR_URL_DATA_MISS
;
1662 ret
= config_writer_close_element(writer
);
1664 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1670 ERR("Unsupported consumer output type.");
1671 ret
= LTTNG_ERR_INVALID
;
1676 ret
= config_writer_close_element(writer
);
1678 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1682 /* /consumer_output */
1683 ret
= config_writer_close_element(writer
);
1685 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1693 int save_snapshot_outputs(struct config_writer
*writer
,
1694 struct snapshot
*snapshot
)
1697 struct lttng_ht_iter iter
;
1698 struct snapshot_output
*output
;
1703 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1705 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1710 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1712 ret
= config_writer_open_element(writer
,
1713 config_element_output
);
1715 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1719 ret
= config_writer_write_element_string(writer
,
1720 config_element_name
, output
->name
);
1722 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1726 ret
= config_writer_write_element_unsigned_int(writer
,
1727 config_element_max_size
, output
->max_size
);
1729 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1733 ret
= save_consumer_output(writer
, output
->consumer
);
1739 ret
= config_writer_close_element(writer
);
1741 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1747 /* /snapshot_outputs */
1748 ret
= config_writer_close_element(writer
);
1750 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1762 int save_session_output(struct config_writer
*writer
,
1763 struct ltt_session
*session
)
1770 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1771 (!session
->snapshot_mode
&& !session
->consumer
)) {
1772 /* Session is in no output mode */
1777 ret
= config_writer_open_element(writer
, config_element_output
);
1779 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1783 if (session
->snapshot_mode
) {
1784 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1789 if (session
->consumer
) {
1790 ret
= save_consumer_output(writer
, session
->consumer
);
1798 ret
= config_writer_close_element(writer
);
1800 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1808 * Save the given session.
1810 * Return 0 on success else a LTTNG_ERR* code.
1813 int save_session(struct ltt_session
*session
,
1814 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1817 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1818 char config_file_path
[PATH_MAX
];
1820 struct config_writer
*writer
= NULL
;
1821 size_t session_name_len
;
1822 const char *provided_path
;
1828 session_name_len
= strlen(session
->name
);
1829 memset(config_file_path
, 0, sizeof(config_file_path
));
1831 if (!session_access_ok(session
,
1832 LTTNG_SOCK_GET_UID_CRED(creds
),
1833 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1834 ret
= LTTNG_ERR_EPERM
;
1838 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1839 if (provided_path
) {
1840 DBG3("Save session in provided path %s", provided_path
);
1841 len
= strlen(provided_path
);
1842 if (len
>= sizeof(config_file_path
)) {
1843 ret
= LTTNG_ERR_SET_URL
;
1846 strncpy(config_file_path
, provided_path
, len
);
1849 char *home_dir
= utils_get_user_home_dir(
1850 LTTNG_SOCK_GET_UID_CRED(creds
));
1852 ret
= LTTNG_ERR_SET_URL
;
1856 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1857 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1860 PERROR("snprintf save session");
1861 ret
= LTTNG_ERR_SET_URL
;
1868 * Check the path fits in the config file path dst including the '/'
1869 * followed by trailing .lttng extension and the NULL terminated string.
1871 if ((len
+ session_name_len
+ 2 +
1872 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1873 > sizeof(config_file_path
)) {
1874 ret
= LTTNG_ERR_SET_URL
;
1878 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1879 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1881 ret
= LTTNG_ERR_SET_URL
;
1886 * At this point, we know that everything fits in the buffer. Validation
1887 * was done just above.
1889 config_file_path
[len
++] = '/';
1890 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1891 len
+= session_name_len
;
1892 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1893 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1894 config_file_path
[len
] = '\0';
1896 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1897 /* File exists, notify the user since the overwrite flag is off. */
1898 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1902 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1903 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1904 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1906 PERROR("Could not create configuration file");
1907 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1912 writer
= config_writer_create(fd
, 1);
1914 ret
= LTTNG_ERR_NOMEM
;
1918 ret
= config_writer_open_element(writer
, config_element_sessions
);
1920 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1924 ret
= config_writer_open_element(writer
, config_element_session
);
1926 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1930 ret
= config_writer_write_element_string(writer
, config_element_name
,
1933 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1937 if(session
->shm_path
[0] != '\0') {
1938 ret
= config_writer_write_element_string(writer
,
1939 config_element_shared_memory_path
,
1942 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1947 ret
= save_domains(writer
, session
);
1952 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1955 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1959 if (session
->snapshot_mode
|| session
->live_timer
) {
1960 ret
= config_writer_open_element(writer
, config_element_attributes
);
1962 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1966 if (session
->snapshot_mode
) {
1967 ret
= config_writer_write_element_bool(writer
,
1968 config_element_snapshot_mode
, 1);
1970 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1974 ret
= config_writer_write_element_unsigned_int(writer
,
1975 config_element_live_timer_interval
, session
->live_timer
);
1977 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1983 ret
= config_writer_close_element(writer
);
1985 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1990 ret
= save_session_output(writer
, session
);
1996 ret
= config_writer_close_element(writer
);
1998 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2003 ret
= config_writer_close_element(writer
);
2005 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2009 if (writer
&& config_writer_destroy(writer
)) {
2010 /* Preserve the original error code */
2011 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2014 /* Delete file in case of error */
2015 if (file_opened
&& unlink(config_file_path
)) {
2016 PERROR("Unlinking XML session configuration.");
2023 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
2024 lttng_sock_cred
*creds
)
2027 const char *session_name
;
2028 struct ltt_session
*session
;
2030 session_lock_list();
2032 session_name
= lttng_save_session_attr_get_session_name(attr
);
2034 session
= session_find_by_name(session_name
);
2036 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2040 session_lock(session
);
2041 ret
= save_session(session
, attr
, creds
);
2042 session_unlock(session
);
2047 struct ltt_session_list
*list
= session_get_list();
2049 cds_list_for_each_entry(session
, &list
->head
, list
) {
2050 session_lock(session
);
2051 ret
= save_session(session
, attr
, creds
);
2052 session_unlock(session
);
2054 /* Don't abort if we don't have the required permissions. */
2055 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
2063 session_unlock_list();