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
;
235 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
:
236 context_type_string
= config_event_context_interruptible
;
238 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
:
239 context_type_string
= config_event_context_preemptible
;
241 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
:
242 context_type_string
= config_event_context_need_reschedule
;
244 case LTTNG_KERNEL_CONTEXT_MIGRATABLE
:
245 context_type_string
= config_event_context_migratable
;
248 context_type_string
= NULL
;
251 return context_type_string
;
255 const char *get_ust_context_type_string(
256 enum lttng_ust_context_type context_type
)
258 const char *context_type_string
;
260 switch (context_type
) {
261 case LTTNG_UST_CONTEXT_PROCNAME
:
262 context_type_string
= config_event_context_procname
;
264 case LTTNG_UST_CONTEXT_VPID
:
265 context_type_string
= config_event_context_vpid
;
267 case LTTNG_UST_CONTEXT_VTID
:
268 context_type_string
= config_event_context_vtid
;
270 case LTTNG_UST_CONTEXT_IP
:
271 context_type_string
= config_event_context_ip
;
273 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
274 context_type_string
= config_event_context_pthread_id
;
276 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
277 context_type_string
= config_event_context_app
;
279 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
281 * Error, should not be stored in the XML, perf contexts
282 * are stored as a node of type event_perf_context_type.
285 context_type_string
= NULL
;
289 return context_type_string
;
293 const char *get_buffer_type_string(
294 enum lttng_buffer_type buffer_type
)
296 const char *buffer_type_string
;
298 switch (buffer_type
) {
299 case LTTNG_BUFFER_PER_PID
:
300 buffer_type_string
= config_buffer_type_per_pid
;
302 case LTTNG_BUFFER_PER_UID
:
303 buffer_type_string
= config_buffer_type_per_uid
;
305 case LTTNG_BUFFER_GLOBAL
:
306 buffer_type_string
= config_buffer_type_global
;
309 buffer_type_string
= NULL
;
312 return buffer_type_string
;
316 const char *get_loglevel_type_string(
317 enum lttng_ust_loglevel_type loglevel_type
)
319 const char *loglevel_type_string
;
321 switch (loglevel_type
) {
322 case LTTNG_UST_LOGLEVEL_ALL
:
323 loglevel_type_string
= config_loglevel_type_all
;
325 case LTTNG_UST_LOGLEVEL_RANGE
:
326 loglevel_type_string
= config_loglevel_type_range
;
328 case LTTNG_UST_LOGLEVEL_SINGLE
:
329 loglevel_type_string
= config_loglevel_type_single
;
332 loglevel_type_string
= NULL
;
335 return loglevel_type_string
;
339 int save_kernel_event(struct config_writer
*writer
,
340 struct ltt_kernel_event
*event
)
343 const char *instrumentation_type
;
345 ret
= config_writer_open_element(writer
, config_element_event
);
347 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
351 if (event
->event
->name
[0]) {
352 ret
= config_writer_write_element_string(writer
,
353 config_element_name
, event
->event
->name
);
355 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
360 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
363 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
367 instrumentation_type
= get_kernel_instrumentation_string(
368 event
->event
->instrumentation
);
369 if (!instrumentation_type
) {
370 ret
= LTTNG_ERR_INVALID
;
374 ret
= config_writer_write_element_string(writer
, config_element_type
,
375 instrumentation_type
);
377 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
381 if (event
->filter_expression
) {
382 ret
= config_writer_write_element_string(writer
,
383 config_element_filter
,
384 event
->filter_expression
);
386 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
391 if (event
->event
->instrumentation
== LTTNG_KERNEL_FUNCTION
||
392 event
->event
->instrumentation
== LTTNG_KERNEL_KPROBE
||
393 event
->event
->instrumentation
== LTTNG_KERNEL_KRETPROBE
) {
395 ret
= config_writer_open_element(writer
,
396 config_element_attributes
);
398 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
402 switch (event
->event
->instrumentation
) {
403 case LTTNG_KERNEL_SYSCALL
:
404 case LTTNG_KERNEL_FUNCTION
:
405 ret
= config_writer_open_element(writer
,
406 config_element_function_attributes
);
408 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
412 ret
= config_writer_write_element_string(writer
,
414 event
->event
->u
.ftrace
.symbol_name
);
416 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
420 /* /function attributes */
421 ret
= config_writer_close_element(writer
);
423 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
427 case LTTNG_KERNEL_KPROBE
:
428 case LTTNG_KERNEL_KRETPROBE
:
430 const char *symbol_name
;
434 if (event
->event
->instrumentation
==
435 LTTNG_KERNEL_KPROBE
) {
437 * Comments in lttng-kernel.h mention that
438 * either addr or symbol_name are set, not both.
440 addr
= event
->event
->u
.kprobe
.addr
;
441 offset
= event
->event
->u
.kprobe
.offset
;
442 symbol_name
= addr
? NULL
:
443 event
->event
->u
.kprobe
.symbol_name
;
446 event
->event
->u
.kretprobe
.symbol_name
;
447 addr
= event
->event
->u
.kretprobe
.addr
;
448 offset
= event
->event
->u
.kretprobe
.offset
;
451 ret
= config_writer_open_element(writer
,
452 config_element_probe_attributes
);
454 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
459 ret
= config_writer_write_element_string(writer
,
460 config_element_symbol_name
,
463 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
469 ret
= config_writer_write_element_unsigned_int(
470 writer
, config_element_address
, addr
);
472 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
478 ret
= config_writer_write_element_unsigned_int(
479 writer
, config_element_offset
, offset
);
481 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
486 ret
= config_writer_close_element(writer
);
488 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
494 ERR("Unsupported kernel instrumentation type.");
495 ret
= LTTNG_ERR_INVALID
;
500 ret
= config_writer_close_element(writer
);
502 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
508 ret
= config_writer_close_element(writer
);
510 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
518 int save_kernel_events(struct config_writer
*writer
,
519 struct ltt_kernel_channel
*kchan
)
522 struct ltt_kernel_event
*event
;
524 ret
= config_writer_open_element(writer
, config_element_events
);
526 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
530 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
531 ret
= save_kernel_event(writer
, event
);
538 ret
= config_writer_close_element(writer
);
540 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
548 int save_ust_event(struct config_writer
*writer
,
549 struct ltt_ust_event
*event
)
552 const char *loglevel_type_string
;
554 ret
= config_writer_open_element(writer
, config_element_event
);
556 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
560 if (event
->attr
.name
[0]) {
561 ret
= config_writer_write_element_string(writer
,
562 config_element_name
, event
->attr
.name
);
564 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
569 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
572 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
576 if (event
->attr
.instrumentation
!= LTTNG_UST_TRACEPOINT
) {
577 ERR("Unsupported UST instrumentation type.");
578 ret
= LTTNG_ERR_INVALID
;
581 ret
= config_writer_write_element_string(writer
, config_element_type
,
582 config_event_type_tracepoint
);
584 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
588 loglevel_type_string
= get_loglevel_type_string(
589 event
->attr
.loglevel_type
);
590 if (!loglevel_type_string
) {
591 ERR("Unsupported UST loglevel type.");
592 ret
= LTTNG_ERR_INVALID
;
596 ret
= config_writer_write_element_string(writer
,
597 config_element_loglevel_type
, loglevel_type_string
);
599 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
603 /* The log level is irrelevant if no "filtering" is enabled */
604 if (event
->attr
.loglevel_type
!= LTTNG_UST_LOGLEVEL_ALL
) {
605 ret
= config_writer_write_element_signed_int(writer
,
606 config_element_loglevel
, event
->attr
.loglevel
);
608 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
613 if (event
->filter_expression
) {
614 ret
= config_writer_write_element_string(writer
,
615 config_element_filter
, event
->filter_expression
);
617 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
622 if (event
->exclusion
&& event
->exclusion
->count
) {
625 ret
= config_writer_open_element(writer
,
626 config_element_exclusions
);
628 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
632 for (i
= 0; i
< event
->exclusion
->count
; i
++) {
633 ret
= config_writer_write_element_string(writer
,
634 config_element_exclusion
,
635 LTTNG_EVENT_EXCLUSION_NAME_AT(
636 event
->exclusion
, i
));
638 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
644 ret
= config_writer_close_element(writer
);
646 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
652 ret
= config_writer_close_element(writer
);
654 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
662 int save_ust_events(struct config_writer
*writer
,
663 struct lttng_ht
*events
)
666 struct ltt_ust_event
*event
;
667 struct lttng_ht_node_str
*node
;
668 struct lttng_ht_iter iter
;
670 ret
= config_writer_open_element(writer
, config_element_events
);
672 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
677 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
678 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
680 if (event
->internal
) {
681 /* Internal events must not be exposed to clients */
684 ret
= save_ust_event(writer
, event
);
693 ret
= config_writer_close_element(writer
);
695 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
703 int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
704 struct agent_event
*agent_event
)
707 enum lttng_ust_loglevel_type ust_loglevel_type
;
709 ust_event
->enabled
= agent_event
->enabled
;
710 ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
711 if (lttng_strncpy(ust_event
->attr
.name
, agent_event
->name
,
712 LTTNG_SYMBOL_NAME_LEN
)) {
716 switch (agent_event
->loglevel_type
) {
717 case LTTNG_EVENT_LOGLEVEL_ALL
:
718 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
720 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
721 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
723 case LTTNG_EVENT_LOGLEVEL_RANGE
:
724 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
727 ERR("Invalid agent_event loglevel_type.");
732 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
733 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
734 ust_event
->filter_expression
= agent_event
->filter_expression
;
735 ust_event
->exclusion
= agent_event
->exclusion
;
741 int save_agent_events(struct config_writer
*writer
,
742 struct ltt_ust_channel
*chan
,
746 struct lttng_ht_iter iter
;
747 struct lttng_ht_node_str
*node
;
749 ret
= config_writer_open_element(writer
, config_element_events
);
751 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
756 cds_lfht_for_each_entry(agent
->events
->ht
, &iter
.iter
, node
, node
) {
758 struct agent_event
*agent_event
;
759 struct ltt_ust_event fake_event
;
761 memset(&fake_event
, 0, sizeof(fake_event
));
762 agent_event
= caa_container_of(node
, struct agent_event
, node
);
765 * Initialize a fake ust event to reuse the same serialization
766 * function since UST and agent events contain the same info
767 * (and one could wonder why they don't reuse the same
770 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
775 ret
= save_ust_event(writer
, &fake_event
);
784 ret
= config_writer_close_element(writer
);
786 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
794 int save_kernel_context(struct config_writer
*writer
,
795 struct lttng_kernel_context
*ctx
)
803 ret
= config_writer_open_element(writer
, config_element_context
);
805 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
809 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
810 ret
= config_writer_open_element(writer
,
811 config_element_context_perf
);
813 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
817 ret
= config_writer_write_element_unsigned_int(writer
,
818 config_element_type
, ctx
->u
.perf_counter
.type
);
820 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
824 ret
= config_writer_write_element_unsigned_int(writer
,
825 config_element_config
, ctx
->u
.perf_counter
.config
);
827 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
831 ret
= config_writer_write_element_string(writer
,
832 config_element_name
, ctx
->u
.perf_counter
.name
);
834 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
839 ret
= config_writer_close_element(writer
);
841 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
845 const char *context_type_string
=
846 get_kernel_context_type_string(ctx
->ctx
);
848 if (!context_type_string
) {
849 ERR("Unsupported kernel context type.");
850 ret
= LTTNG_ERR_INVALID
;
854 ret
= config_writer_write_element_string(writer
,
855 config_element_type
, context_type_string
);
857 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
863 ret
= config_writer_close_element(writer
);
865 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
874 int save_kernel_contexts(struct config_writer
*writer
,
875 struct ltt_kernel_channel
*kchan
)
878 struct ltt_kernel_context
*ctx
;
880 if (cds_list_empty(&kchan
->ctx_list
)) {
885 ret
= config_writer_open_element(writer
, config_element_contexts
);
887 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
891 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
892 ret
= save_kernel_context(writer
, &ctx
->ctx
);
899 ret
= config_writer_close_element(writer
);
901 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
909 int save_ust_context_perf_thread_counter(struct config_writer
*writer
,
910 struct ltt_ust_context
*ctx
)
917 /* Perf contexts are saved as event_perf_context_type */
918 ret
= config_writer_open_element(writer
, config_element_context_perf
);
920 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
924 ret
= config_writer_write_element_unsigned_int(writer
,
925 config_element_type
, ctx
->ctx
.u
.perf_counter
.type
);
927 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
931 ret
= config_writer_write_element_unsigned_int(writer
,
932 config_element_config
, ctx
->ctx
.u
.perf_counter
.config
);
934 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
938 ret
= config_writer_write_element_string(writer
, config_element_name
,
939 ctx
->ctx
.u
.perf_counter
.name
);
941 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
946 ret
= config_writer_close_element(writer
);
948 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
956 int save_ust_context_app_ctx(struct config_writer
*writer
,
957 struct ltt_ust_context
*ctx
)
964 /* Application contexts are saved as application_context_type */
965 ret
= config_writer_open_element(writer
, config_element_context_app
);
967 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
971 ret
= config_writer_write_element_string(writer
,
972 config_element_context_app_provider_name
,
973 ctx
->ctx
.u
.app_ctx
.provider_name
);
975 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
979 ret
= config_writer_write_element_string(writer
,
980 config_element_context_app_ctx_name
,
981 ctx
->ctx
.u
.app_ctx
.ctx_name
);
983 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
988 ret
= config_writer_close_element(writer
);
990 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
998 int save_ust_context_generic(struct config_writer
*writer
,
999 struct ltt_ust_context
*ctx
)
1002 const char *context_type_string
;
1007 /* Save context as event_context_type_type */
1008 context_type_string
= get_ust_context_type_string(
1010 if (!context_type_string
) {
1011 ERR("Unsupported UST context type.");
1012 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1016 ret
= config_writer_write_element_string(writer
,
1017 config_element_type
, context_type_string
);
1019 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1027 int save_ust_context(struct config_writer
*writer
,
1028 struct cds_list_head
*ctx_list
)
1031 struct ltt_ust_context
*ctx
;
1036 ret
= config_writer_open_element(writer
, config_element_contexts
);
1038 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1042 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
1043 ret
= config_writer_open_element(writer
,
1044 config_element_context
);
1046 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1050 switch (ctx
->ctx
.ctx
) {
1051 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
1052 ret
= save_ust_context_perf_thread_counter(writer
, ctx
);
1054 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1055 ret
= save_ust_context_app_ctx(writer
, ctx
);
1058 /* Save generic context. */
1059 ret
= save_ust_context_generic(writer
, ctx
);
1066 ret
= config_writer_close_element(writer
);
1068 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1074 ret
= config_writer_close_element(writer
);
1076 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1084 int save_kernel_channel(struct config_writer
*writer
,
1085 struct ltt_kernel_channel
*kchan
)
1092 ret
= config_writer_open_element(writer
, config_element_channel
);
1094 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1098 ret
= config_writer_write_element_string(writer
, config_element_name
,
1099 kchan
->channel
->name
);
1101 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1105 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1106 kchan
->channel
->enabled
);
1108 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1112 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1117 ret
= save_kernel_events(writer
, kchan
);
1122 ret
= save_kernel_contexts(writer
, kchan
);
1128 ret
= config_writer_close_element(writer
);
1130 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1138 int save_ust_channel(struct config_writer
*writer
,
1139 struct ltt_ust_channel
*ust_chan
,
1140 struct ltt_ust_session
*session
)
1148 ret
= config_writer_open_element(writer
, config_element_channel
);
1150 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1154 ret
= config_writer_write_element_string(writer
, config_element_name
,
1157 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1161 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1164 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1168 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1173 ret
= config_writer_write_element_unsigned_int(writer
,
1174 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1176 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1180 ret
= config_writer_write_element_unsigned_int(writer
,
1181 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1183 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1187 ret
= config_writer_write_element_unsigned_int(writer
,
1188 config_element_live_timer_interval
,
1189 session
->live_timer_interval
);
1191 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1195 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1196 ret
= save_ust_events(writer
, ust_chan
->events
);
1201 struct agent
*agent
= NULL
;
1203 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1205 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1206 ERR("Could not find agent associated to UST subdomain");
1211 * Channels associated with a UST sub-domain (such as JUL, Log4j
1212 * or Python) don't have any non-internal events. We retrieve
1213 * the "agent" events associated with this channel and serialize
1216 ret
= save_agent_events(writer
, ust_chan
, agent
);
1222 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1228 ret
= config_writer_close_element(writer
);
1230 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1238 int save_kernel_session(struct config_writer
*writer
,
1239 struct ltt_session
*session
)
1242 struct ltt_kernel_channel
*kchan
;
1247 ret
= config_writer_write_element_string(writer
, config_element_type
,
1248 config_domain_type_kernel
);
1250 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1254 ret
= config_writer_write_element_string(writer
,
1255 config_element_buffer_type
, config_buffer_type_global
);
1257 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1261 ret
= config_writer_open_element(writer
,
1262 config_element_channels
);
1264 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1268 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1270 ret
= save_kernel_channel(writer
, kchan
);
1277 ret
= config_writer_close_element(writer
);
1279 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1287 const char *get_config_domain_str(enum lttng_domain_type domain
)
1289 const char *str_dom
;
1292 case LTTNG_DOMAIN_KERNEL
:
1293 str_dom
= config_domain_type_kernel
;
1295 case LTTNG_DOMAIN_UST
:
1296 str_dom
= config_domain_type_ust
;
1298 case LTTNG_DOMAIN_JUL
:
1299 str_dom
= config_domain_type_jul
;
1301 case LTTNG_DOMAIN_LOG4J
:
1302 str_dom
= config_domain_type_log4j
;
1304 case LTTNG_DOMAIN_PYTHON
:
1305 str_dom
= config_domain_type_python
;
1315 int save_pid_tracker(struct config_writer
*writer
,
1316 struct ltt_session
*sess
, int domain
)
1319 ssize_t nr_pids
= 0, i
;
1320 int32_t *pids
= NULL
;
1323 case LTTNG_DOMAIN_KERNEL
:
1325 nr_pids
= kernel_list_tracker_pids(sess
->kernel_session
, &pids
);
1327 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1332 case LTTNG_DOMAIN_UST
:
1334 nr_pids
= trace_ust_list_tracker_pids(sess
->ust_session
, &pids
);
1336 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1341 case LTTNG_DOMAIN_JUL
:
1342 case LTTNG_DOMAIN_LOG4J
:
1343 case LTTNG_DOMAIN_PYTHON
:
1345 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1349 /* Only create a pid_tracker if enabled or untrack all */
1350 if (nr_pids
!= 1 || (nr_pids
== 1 && pids
[0] != -1)) {
1351 ret
= config_writer_open_element(writer
,
1352 config_element_pid_tracker
);
1354 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1358 ret
= config_writer_open_element(writer
,
1359 config_element_targets
);
1361 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1365 for (i
= 0; i
< nr_pids
; i
++) {
1366 ret
= config_writer_open_element(writer
,
1367 config_element_target_pid
);
1369 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1373 ret
= config_writer_write_element_unsigned_int(writer
,
1374 config_element_pid
, pids
[i
]);
1376 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1381 ret
= config_writer_close_element(writer
);
1383 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1389 ret
= config_writer_close_element(writer
);
1391 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1396 ret
= config_writer_close_element(writer
);
1398 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1408 int save_ust_domain(struct config_writer
*writer
,
1409 struct ltt_session
*session
, enum lttng_domain_type domain
)
1412 struct ltt_ust_channel
*ust_chan
;
1413 const char *buffer_type_string
;
1414 struct lttng_ht_node_str
*node
;
1415 struct lttng_ht_iter iter
;
1416 const char *config_domain_name
;
1421 ret
= config_writer_open_element(writer
,
1422 config_element_domain
);
1424 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1428 config_domain_name
= get_config_domain_str(domain
);
1429 if (!config_domain_name
) {
1430 ret
= LTTNG_ERR_INVALID
;
1434 ret
= config_writer_write_element_string(writer
,
1435 config_element_type
, config_domain_name
);
1437 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1441 buffer_type_string
= get_buffer_type_string(
1442 session
->ust_session
->buffer_type
);
1443 if (!buffer_type_string
) {
1444 ERR("Unsupported buffer type.");
1445 ret
= LTTNG_ERR_INVALID
;
1449 ret
= config_writer_write_element_string(writer
,
1450 config_element_buffer_type
, buffer_type_string
);
1452 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1456 ret
= config_writer_open_element(writer
, config_element_channels
);
1458 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1463 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1464 &iter
.iter
, node
, node
) {
1465 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1466 if (domain
== ust_chan
->domain
) {
1467 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1477 ret
= config_writer_close_element(writer
);
1479 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1483 if (domain
== LTTNG_DOMAIN_UST
) {
1484 ret
= config_writer_open_element(writer
,
1485 config_element_trackers
);
1487 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1491 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_UST
);
1497 ret
= config_writer_close_element(writer
);
1504 ret
= config_writer_close_element(writer
);
1506 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1515 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1522 if (!session
->kernel_session
&& !session
->ust_session
) {
1526 ret
= config_writer_open_element(writer
, config_element_domains
);
1528 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1533 if (session
->kernel_session
) {
1534 ret
= config_writer_open_element(writer
,
1535 config_element_domain
);
1537 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1541 ret
= save_kernel_session(writer
, session
);
1546 ret
= config_writer_open_element(writer
,
1547 config_element_trackers
);
1549 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1553 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_KERNEL
);
1559 ret
= config_writer_close_element(writer
);
1561 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1565 ret
= config_writer_close_element(writer
);
1567 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1572 if (session
->ust_session
) {
1573 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
1578 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
1583 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
1588 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
1595 ret
= config_writer_close_element(writer
);
1597 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1605 int save_consumer_output(struct config_writer
*writer
,
1606 struct consumer_output
*output
)
1613 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1615 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1619 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1622 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1626 ret
= config_writer_open_element(writer
, config_element_destination
);
1628 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1632 switch (output
->type
) {
1633 case CONSUMER_DST_LOCAL
:
1634 ret
= config_writer_write_element_string(writer
,
1635 config_element_path
, output
->dst
.trace_path
);
1637 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1641 case CONSUMER_DST_NET
:
1645 uri
= zmalloc(PATH_MAX
);
1647 ret
= LTTNG_ERR_NOMEM
;
1651 ret
= config_writer_open_element(writer
, config_element_net_output
);
1653 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1654 goto end_net_output
;
1657 if (output
->dst
.net
.control_isset
&&
1658 output
->dst
.net
.data_isset
) {
1659 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1661 ret
= LTTNG_ERR_INVALID
;
1662 goto end_net_output
;
1665 ret
= config_writer_write_element_string(writer
,
1666 config_element_control_uri
, uri
);
1668 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1669 goto end_net_output
;
1672 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1674 ret
= LTTNG_ERR_INVALID
;
1675 goto end_net_output
;
1678 ret
= config_writer_write_element_string(writer
,
1679 config_element_data_uri
, uri
);
1681 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1682 goto end_net_output
;
1691 ret
= !output
->dst
.net
.control_isset
?
1692 LTTNG_ERR_URL_CTRL_MISS
:
1693 LTTNG_ERR_URL_DATA_MISS
;
1698 ret
= config_writer_close_element(writer
);
1700 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1706 ERR("Unsupported consumer output type.");
1707 ret
= LTTNG_ERR_INVALID
;
1712 ret
= config_writer_close_element(writer
);
1714 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1718 /* /consumer_output */
1719 ret
= config_writer_close_element(writer
);
1721 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1729 int save_snapshot_outputs(struct config_writer
*writer
,
1730 struct snapshot
*snapshot
)
1733 struct lttng_ht_iter iter
;
1734 struct snapshot_output
*output
;
1739 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1741 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1746 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1748 ret
= config_writer_open_element(writer
,
1749 config_element_output
);
1751 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1755 ret
= config_writer_write_element_string(writer
,
1756 config_element_name
, output
->name
);
1758 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1762 ret
= config_writer_write_element_unsigned_int(writer
,
1763 config_element_max_size
, output
->max_size
);
1765 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1769 ret
= save_consumer_output(writer
, output
->consumer
);
1775 ret
= config_writer_close_element(writer
);
1777 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1783 /* /snapshot_outputs */
1784 ret
= config_writer_close_element(writer
);
1786 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1798 int save_session_output(struct config_writer
*writer
,
1799 struct ltt_session
*session
)
1806 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1807 (!session
->snapshot_mode
&& !session
->consumer
)) {
1808 /* Session is in no output mode */
1813 ret
= config_writer_open_element(writer
, config_element_output
);
1815 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1819 if (session
->snapshot_mode
) {
1820 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1825 if (session
->consumer
) {
1826 ret
= save_consumer_output(writer
, session
->consumer
);
1834 ret
= config_writer_close_element(writer
);
1836 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1844 * Save the given session.
1846 * Return 0 on success else a LTTNG_ERR* code.
1849 int save_session(struct ltt_session
*session
,
1850 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1853 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1854 char config_file_path
[PATH_MAX
];
1856 struct config_writer
*writer
= NULL
;
1857 size_t session_name_len
;
1858 const char *provided_path
;
1864 session_name_len
= strlen(session
->name
);
1865 memset(config_file_path
, 0, sizeof(config_file_path
));
1867 if (!session_access_ok(session
,
1868 LTTNG_SOCK_GET_UID_CRED(creds
),
1869 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1870 ret
= LTTNG_ERR_EPERM
;
1874 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1875 if (provided_path
) {
1876 DBG3("Save session in provided path %s", provided_path
);
1877 len
= strlen(provided_path
);
1878 if (len
>= sizeof(config_file_path
)) {
1879 ret
= LTTNG_ERR_SET_URL
;
1882 strncpy(config_file_path
, provided_path
, len
);
1885 char *home_dir
= utils_get_user_home_dir(
1886 LTTNG_SOCK_GET_UID_CRED(creds
));
1888 ret
= LTTNG_ERR_SET_URL
;
1892 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1893 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1896 PERROR("snprintf save session");
1897 ret
= LTTNG_ERR_SET_URL
;
1904 * Check the path fits in the config file path dst including the '/'
1905 * followed by trailing .lttng extension and the NULL terminated string.
1907 if ((len
+ session_name_len
+ 2 +
1908 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1909 > sizeof(config_file_path
)) {
1910 ret
= LTTNG_ERR_SET_URL
;
1914 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1915 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1917 ret
= LTTNG_ERR_SET_URL
;
1922 * At this point, we know that everything fits in the buffer. Validation
1923 * was done just above.
1925 config_file_path
[len
++] = '/';
1926 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1927 len
+= session_name_len
;
1928 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1929 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1930 config_file_path
[len
] = '\0';
1932 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1933 /* File exists, notify the user since the overwrite flag is off. */
1934 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1938 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1939 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1940 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1942 PERROR("Could not create configuration file");
1943 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1948 writer
= config_writer_create(fd
, 1);
1950 ret
= LTTNG_ERR_NOMEM
;
1954 ret
= config_writer_open_element(writer
, config_element_sessions
);
1956 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1960 ret
= config_writer_open_element(writer
, config_element_session
);
1962 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1966 ret
= config_writer_write_element_string(writer
, config_element_name
,
1969 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1973 if(session
->shm_path
[0] != '\0') {
1974 ret
= config_writer_write_element_string(writer
,
1975 config_element_shared_memory_path
,
1978 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1983 ret
= save_domains(writer
, session
);
1988 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1991 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1995 if (session
->snapshot_mode
|| session
->live_timer
) {
1996 ret
= config_writer_open_element(writer
, config_element_attributes
);
1998 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2002 if (session
->snapshot_mode
) {
2003 ret
= config_writer_write_element_bool(writer
,
2004 config_element_snapshot_mode
, 1);
2006 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2010 ret
= config_writer_write_element_unsigned_int(writer
,
2011 config_element_live_timer_interval
, session
->live_timer
);
2013 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2019 ret
= config_writer_close_element(writer
);
2021 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2026 ret
= save_session_output(writer
, session
);
2032 ret
= config_writer_close_element(writer
);
2034 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2039 ret
= config_writer_close_element(writer
);
2041 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2045 if (writer
&& config_writer_destroy(writer
)) {
2046 /* Preserve the original error code */
2047 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2050 /* Delete file in case of error */
2051 if (file_opened
&& unlink(config_file_path
)) {
2052 PERROR("Unlinking XML session configuration.");
2059 PERROR("Closing XML session configuration");
2066 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
2067 lttng_sock_cred
*creds
)
2070 const char *session_name
;
2071 struct ltt_session
*session
;
2073 session_lock_list();
2075 session_name
= lttng_save_session_attr_get_session_name(attr
);
2077 session
= session_find_by_name(session_name
);
2079 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2083 session_lock(session
);
2084 ret
= save_session(session
, attr
, creds
);
2085 session_unlock(session
);
2090 struct ltt_session_list
*list
= session_get_list();
2092 cds_list_for_each_entry(session
, &list
->head
, list
) {
2093 session_lock(session
);
2094 ret
= save_session(session
, attr
, creds
);
2095 session_unlock(session
);
2097 /* Don't abort if we don't have the required permissions. */
2098 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
2106 session_unlock_list();