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 &event
->exclusion
->names
[0][i
]);
679 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
685 ret
= config_writer_close_element(writer
);
687 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
693 ret
= config_writer_close_element(writer
);
695 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
703 int save_ust_events(struct config_writer
*writer
,
704 struct lttng_ht
*events
)
707 struct ltt_ust_event
*event
;
708 struct lttng_ht_node_str
*node
;
709 struct lttng_ht_iter iter
;
711 ret
= config_writer_open_element(writer
, config_element_events
);
713 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
718 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
719 event
= caa_container_of(node
, struct ltt_ust_event
, node
);
721 if (event
->internal
) {
722 /* Internal events must not be exposed to clients */
725 ret
= save_ust_event(writer
, event
);
734 ret
= config_writer_close_element(writer
);
736 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
744 int init_ust_event_from_agent_event(struct ltt_ust_event
*ust_event
,
745 struct agent_event
*agent_event
)
748 enum lttng_ust_loglevel_type ust_loglevel_type
;
750 ust_event
->enabled
= agent_event
->enabled
;
751 ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
752 strncpy(ust_event
->attr
.name
, agent_event
->name
, LTTNG_SYMBOL_NAME_LEN
);
753 switch (agent_event
->loglevel_type
) {
754 case LTTNG_EVENT_LOGLEVEL_ALL
:
755 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
757 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
758 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
760 case LTTNG_EVENT_LOGLEVEL_RANGE
:
761 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
764 ERR("Invalid agent_event loglevel_type.");
769 ust_event
->attr
.loglevel_type
= ust_loglevel_type
;
770 ust_event
->attr
.loglevel
= agent_event
->loglevel_value
;
771 ust_event
->filter_expression
= agent_event
->filter_expression
;
772 ust_event
->exclusion
= agent_event
->exclusion
;
778 int save_agent_events(struct config_writer
*writer
,
779 struct ltt_ust_channel
*chan
,
783 struct lttng_ht_iter iter
;
784 struct lttng_ht_node_str
*node
;
786 ret
= config_writer_open_element(writer
, config_element_events
);
788 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
793 cds_lfht_for_each_entry(agent
->events
->ht
, &iter
.iter
, node
, node
) {
795 struct agent_event
*agent_event
;
796 struct ltt_ust_event fake_event
;
798 memset(&fake_event
, 0, sizeof(fake_event
));
799 agent_event
= caa_container_of(node
, struct agent_event
, node
);
802 * Initialize a fake ust event to reuse the same serialization
803 * function since UST and agent events contain the same info
804 * (and one could wonder why they don't reuse the same
807 ret
= init_ust_event_from_agent_event(&fake_event
, agent_event
);
812 ret
= save_ust_event(writer
, &fake_event
);
821 ret
= config_writer_close_element(writer
);
823 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
831 int save_kernel_context(struct config_writer
*writer
,
832 struct lttng_kernel_context
*ctx
)
840 ret
= config_writer_open_element(writer
, config_element_context
);
842 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
846 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
847 ret
= config_writer_open_element(writer
, config_element_perf
);
849 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
853 ret
= config_writer_write_element_unsigned_int(writer
,
854 config_element_type
, ctx
->u
.perf_counter
.type
);
856 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
860 ret
= config_writer_write_element_unsigned_int(writer
,
861 config_element_config
, ctx
->u
.perf_counter
.config
);
863 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
867 ret
= config_writer_write_element_string(writer
,
868 config_element_name
, ctx
->u
.perf_counter
.name
);
870 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
875 ret
= config_writer_close_element(writer
);
877 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
881 const char *context_type_string
=
882 get_kernel_context_type_string(ctx
->ctx
);
884 if (!context_type_string
) {
885 ERR("Unsupported kernel context type.");
886 ret
= LTTNG_ERR_INVALID
;
890 ret
= config_writer_write_element_string(writer
,
891 config_element_type
, context_type_string
);
893 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
899 ret
= config_writer_close_element(writer
);
901 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
910 int save_kernel_contexts(struct config_writer
*writer
,
911 struct ltt_kernel_channel
*kchan
)
914 struct ltt_kernel_context
*ctx
;
916 if (cds_list_empty(&kchan
->ctx_list
)) {
921 ret
= config_writer_open_element(writer
, config_element_contexts
);
923 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
927 cds_list_for_each_entry(ctx
, &kchan
->ctx_list
, list
) {
928 ret
= save_kernel_context(writer
, &ctx
->ctx
);
935 ret
= config_writer_close_element(writer
);
937 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
945 int save_ust_context(struct config_writer
*writer
,
946 struct cds_list_head
*ctx_list
)
949 struct ltt_ust_context
*ctx
;
954 ret
= config_writer_open_element(writer
, config_element_contexts
);
956 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
960 cds_list_for_each_entry(ctx
, ctx_list
, list
) {
961 const char *context_type_string
;
964 ret
= config_writer_open_element(writer
,
965 config_element_context
);
967 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
971 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
972 /* Perf contexts are saved as event_perf_context_type */
973 ret
= config_writer_open_element(writer
,
974 config_element_perf
);
976 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
980 ret
= config_writer_write_element_unsigned_int(writer
,
982 ctx
->ctx
.u
.perf_counter
.type
);
984 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
988 ret
= config_writer_write_element_unsigned_int(writer
,
989 config_element_config
,
990 ctx
->ctx
.u
.perf_counter
.config
);
992 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
996 ret
= config_writer_write_element_string(writer
,
998 ctx
->ctx
.u
.perf_counter
.name
);
1000 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1005 ret
= config_writer_close_element(writer
);
1007 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1011 /* Save context as event_context_type_type */
1012 context_type_string
= get_ust_context_type_string(
1014 if (!context_type_string
) {
1015 ERR("Unsupported UST context type.")
1016 ret
= LTTNG_ERR_INVALID
;
1020 ret
= config_writer_write_element_string(writer
,
1021 config_element_type
, context_type_string
);
1023 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1029 ret
= config_writer_close_element(writer
);
1031 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1037 ret
= config_writer_close_element(writer
);
1039 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1047 int save_kernel_channel(struct config_writer
*writer
,
1048 struct ltt_kernel_channel
*kchan
)
1055 ret
= config_writer_open_element(writer
, config_element_channel
);
1057 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1061 ret
= config_writer_write_element_string(writer
, config_element_name
,
1062 kchan
->channel
->name
);
1064 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1068 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1069 kchan
->channel
->enabled
);
1071 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1075 ret
= save_kernel_channel_attributes(writer
, &kchan
->channel
->attr
);
1080 ret
= save_kernel_events(writer
, kchan
);
1085 ret
= save_kernel_contexts(writer
, kchan
);
1091 ret
= config_writer_close_element(writer
);
1093 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1101 int save_ust_channel(struct config_writer
*writer
,
1102 struct ltt_ust_channel
*ust_chan
,
1103 struct ltt_ust_session
*session
)
1111 ret
= config_writer_open_element(writer
, config_element_channel
);
1113 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1117 ret
= config_writer_write_element_string(writer
, config_element_name
,
1120 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1124 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1127 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1131 ret
= save_ust_channel_attributes(writer
, &ust_chan
->attr
);
1136 ret
= config_writer_write_element_unsigned_int(writer
,
1137 config_element_tracefile_size
, ust_chan
->tracefile_size
);
1139 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1143 ret
= config_writer_write_element_unsigned_int(writer
,
1144 config_element_tracefile_count
, ust_chan
->tracefile_count
);
1146 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1150 ret
= config_writer_write_element_unsigned_int(writer
,
1151 config_element_live_timer_interval
,
1152 session
->live_timer_interval
);
1154 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1158 if (ust_chan
->domain
== LTTNG_DOMAIN_UST
) {
1159 ret
= save_ust_events(writer
, ust_chan
->events
);
1164 struct agent
*agent
= NULL
;
1166 agent
= trace_ust_find_agent(session
, ust_chan
->domain
);
1168 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1169 ERR("Could not find agent associated to UST subdomain");
1174 * Channels associated with a UST sub-domain (such as JUL, Log4j
1175 * or Python) don't have any non-internal events. We retrieve
1176 * the "agent" events associated with this channel and serialize
1179 ret
= save_agent_events(writer
, ust_chan
, agent
);
1185 ret
= save_ust_context(writer
, &ust_chan
->ctx_list
);
1191 ret
= config_writer_close_element(writer
);
1193 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1201 int save_kernel_session(struct config_writer
*writer
,
1202 struct ltt_session
*session
)
1205 struct ltt_kernel_channel
*kchan
;
1210 ret
= config_writer_write_element_string(writer
, config_element_type
,
1211 config_domain_type_kernel
);
1213 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1217 ret
= config_writer_write_element_string(writer
,
1218 config_element_buffer_type
, config_buffer_type_global
);
1220 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1224 ret
= config_writer_open_element(writer
,
1225 config_element_channels
);
1227 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1231 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
,
1233 ret
= save_kernel_channel(writer
, kchan
);
1240 ret
= config_writer_close_element(writer
);
1242 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1250 const char *get_config_domain_str(enum lttng_domain_type domain
)
1252 const char *str_dom
;
1255 case LTTNG_DOMAIN_KERNEL
:
1256 str_dom
= config_domain_type_kernel
;
1258 case LTTNG_DOMAIN_UST
:
1259 str_dom
= config_domain_type_ust
;
1261 case LTTNG_DOMAIN_JUL
:
1262 str_dom
= config_domain_type_jul
;
1264 case LTTNG_DOMAIN_LOG4J
:
1265 str_dom
= config_domain_type_log4j
;
1267 case LTTNG_DOMAIN_PYTHON
:
1268 str_dom
= config_domain_type_python
;
1278 int save_pid_tracker(struct config_writer
*writer
,
1279 struct ltt_session
*sess
, int domain
)
1282 ssize_t nr_pids
= 0, i
;
1283 int32_t *pids
= NULL
;
1286 case LTTNG_DOMAIN_KERNEL
:
1288 nr_pids
= kernel_list_tracker_pids(sess
->kernel_session
, &pids
);
1290 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1295 case LTTNG_DOMAIN_UST
:
1297 nr_pids
= trace_ust_list_tracker_pids(sess
->ust_session
, &pids
);
1299 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1304 case LTTNG_DOMAIN_JUL
:
1305 case LTTNG_DOMAIN_LOG4J
:
1306 case LTTNG_DOMAIN_PYTHON
:
1308 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1312 /* Only create a pid_tracker if enabled or untrack all */
1313 if (nr_pids
!= 1 || (nr_pids
== 1 && pids
[0] != -1)) {
1314 ret
= config_writer_open_element(writer
,
1315 config_element_pid_tracker
);
1317 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1321 ret
= config_writer_open_element(writer
,
1322 config_element_targets
);
1324 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1328 for (i
= 0; i
< nr_pids
; i
++) {
1329 ret
= config_writer_open_element(writer
,
1330 config_element_target_pid
);
1332 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1336 ret
= config_writer_write_element_unsigned_int(writer
,
1337 config_element_pid
, pids
[i
]);
1339 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1344 ret
= config_writer_close_element(writer
);
1346 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1352 ret
= config_writer_close_element(writer
);
1354 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1359 ret
= config_writer_close_element(writer
);
1361 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1371 int save_ust_domain(struct config_writer
*writer
,
1372 struct ltt_session
*session
, enum lttng_domain_type domain
)
1375 struct ltt_ust_channel
*ust_chan
;
1376 const char *buffer_type_string
;
1377 struct lttng_ht_node_str
*node
;
1378 struct lttng_ht_iter iter
;
1379 const char *config_domain_name
;
1384 ret
= config_writer_open_element(writer
,
1385 config_element_domain
);
1387 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1391 config_domain_name
= get_config_domain_str(domain
);
1392 if (!config_domain_name
) {
1393 ret
= LTTNG_ERR_INVALID
;
1397 ret
= config_writer_write_element_string(writer
,
1398 config_element_type
, config_domain_name
);
1400 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1404 buffer_type_string
= get_buffer_type_string(
1405 session
->ust_session
->buffer_type
);
1406 if (!buffer_type_string
) {
1407 ERR("Unsupported buffer type.");
1408 ret
= LTTNG_ERR_INVALID
;
1412 ret
= config_writer_write_element_string(writer
,
1413 config_element_buffer_type
, buffer_type_string
);
1415 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1419 ret
= config_writer_open_element(writer
, config_element_channels
);
1421 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1426 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
1427 &iter
.iter
, node
, node
) {
1428 ust_chan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1429 if (domain
== ust_chan
->domain
) {
1430 ret
= save_ust_channel(writer
, ust_chan
, session
->ust_session
);
1440 ret
= config_writer_close_element(writer
);
1442 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1446 if (domain
== LTTNG_DOMAIN_UST
) {
1447 ret
= config_writer_open_element(writer
,
1448 config_element_trackers
);
1450 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1454 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_UST
);
1460 ret
= config_writer_close_element(writer
);
1467 ret
= config_writer_close_element(writer
);
1469 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1478 int save_domains(struct config_writer
*writer
, struct ltt_session
*session
)
1485 if (!session
->kernel_session
&& !session
->ust_session
) {
1489 ret
= config_writer_open_element(writer
, config_element_domains
);
1491 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1496 if (session
->kernel_session
) {
1497 ret
= config_writer_open_element(writer
,
1498 config_element_domain
);
1500 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1504 ret
= save_kernel_session(writer
, session
);
1509 ret
= config_writer_open_element(writer
,
1510 config_element_trackers
);
1512 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1516 ret
= save_pid_tracker(writer
, session
, LTTNG_DOMAIN_KERNEL
);
1522 ret
= config_writer_close_element(writer
);
1524 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1528 ret
= config_writer_close_element(writer
);
1530 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1535 if (session
->ust_session
) {
1536 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_UST
);
1541 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_JUL
);
1546 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_LOG4J
);
1551 ret
= save_ust_domain(writer
, session
, LTTNG_DOMAIN_PYTHON
);
1558 ret
= config_writer_close_element(writer
);
1560 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1568 int save_consumer_output(struct config_writer
*writer
,
1569 struct consumer_output
*output
)
1576 ret
= config_writer_open_element(writer
, config_element_consumer_output
);
1578 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1582 ret
= config_writer_write_element_bool(writer
, config_element_enabled
,
1585 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1589 ret
= config_writer_open_element(writer
, config_element_destination
);
1591 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1595 switch (output
->type
) {
1596 case CONSUMER_DST_LOCAL
:
1597 ret
= config_writer_write_element_string(writer
,
1598 config_element_path
, output
->dst
.trace_path
);
1600 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1604 case CONSUMER_DST_NET
:
1608 uri
= zmalloc(PATH_MAX
);
1610 ret
= LTTNG_ERR_NOMEM
;
1614 ret
= config_writer_open_element(writer
, config_element_net_output
);
1616 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1617 goto end_net_output
;
1620 if (output
->dst
.net
.control_isset
&&
1621 output
->dst
.net
.data_isset
) {
1622 ret
= uri_to_str_url(&output
->dst
.net
.control
, uri
, PATH_MAX
);
1624 ret
= LTTNG_ERR_INVALID
;
1625 goto end_net_output
;
1628 ret
= config_writer_write_element_string(writer
,
1629 config_element_control_uri
, uri
);
1631 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1632 goto end_net_output
;
1635 ret
= uri_to_str_url(&output
->dst
.net
.data
, uri
, PATH_MAX
);
1637 ret
= LTTNG_ERR_INVALID
;
1638 goto end_net_output
;
1641 ret
= config_writer_write_element_string(writer
,
1642 config_element_data_uri
, uri
);
1644 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1645 goto end_net_output
;
1654 ret
= !output
->dst
.net
.control_isset
?
1655 LTTNG_ERR_URL_CTRL_MISS
:
1656 LTTNG_ERR_URL_DATA_MISS
;
1661 ret
= config_writer_close_element(writer
);
1663 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1669 ERR("Unsupported consumer output type.");
1670 ret
= LTTNG_ERR_INVALID
;
1675 ret
= config_writer_close_element(writer
);
1677 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1681 /* /consumer_output */
1682 ret
= config_writer_close_element(writer
);
1684 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1692 int save_snapshot_outputs(struct config_writer
*writer
,
1693 struct snapshot
*snapshot
)
1696 struct lttng_ht_iter iter
;
1697 struct snapshot_output
*output
;
1702 ret
= config_writer_open_element(writer
, config_element_snapshot_outputs
);
1704 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1709 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
1711 ret
= config_writer_open_element(writer
,
1712 config_element_output
);
1714 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1718 ret
= config_writer_write_element_string(writer
,
1719 config_element_name
, output
->name
);
1721 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1725 ret
= config_writer_write_element_unsigned_int(writer
,
1726 config_element_max_size
, output
->max_size
);
1728 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1732 ret
= save_consumer_output(writer
, output
->consumer
);
1738 ret
= config_writer_close_element(writer
);
1740 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1746 /* /snapshot_outputs */
1747 ret
= config_writer_close_element(writer
);
1749 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1761 int save_session_output(struct config_writer
*writer
,
1762 struct ltt_session
*session
)
1769 if ((session
->snapshot_mode
&& session
->snapshot
.nb_output
== 0) ||
1770 (!session
->snapshot_mode
&& !session
->consumer
)) {
1771 /* Session is in no output mode */
1776 ret
= config_writer_open_element(writer
, config_element_output
);
1778 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1782 if (session
->snapshot_mode
) {
1783 ret
= save_snapshot_outputs(writer
, &session
->snapshot
);
1788 if (session
->consumer
) {
1789 ret
= save_consumer_output(writer
, session
->consumer
);
1797 ret
= config_writer_close_element(writer
);
1799 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1807 * Save the given session.
1809 * Return 0 on success else a LTTNG_ERR* code.
1812 int save_session(struct ltt_session
*session
,
1813 struct lttng_save_session_attr
*attr
, lttng_sock_cred
*creds
)
1816 unsigned int file_opened
= 0; /* Indicate if the file has been opened */
1817 char config_file_path
[PATH_MAX
];
1819 struct config_writer
*writer
= NULL
;
1820 size_t session_name_len
;
1821 const char *provided_path
;
1827 session_name_len
= strlen(session
->name
);
1828 memset(config_file_path
, 0, sizeof(config_file_path
));
1830 if (!session_access_ok(session
,
1831 LTTNG_SOCK_GET_UID_CRED(creds
),
1832 LTTNG_SOCK_GET_GID_CRED(creds
))) {
1833 ret
= LTTNG_ERR_EPERM
;
1837 provided_path
= lttng_save_session_attr_get_output_url(attr
);
1838 if (provided_path
) {
1839 DBG3("Save session in provided path %s", provided_path
);
1840 len
= strlen(provided_path
);
1841 if (len
>= sizeof(config_file_path
)) {
1842 ret
= LTTNG_ERR_SET_URL
;
1845 strncpy(config_file_path
, provided_path
, len
);
1848 char *home_dir
= utils_get_user_home_dir(
1849 LTTNG_SOCK_GET_UID_CRED(creds
));
1851 ret
= LTTNG_ERR_SET_URL
;
1855 ret_len
= snprintf(config_file_path
, sizeof(config_file_path
),
1856 DEFAULT_SESSION_HOME_CONFIGPATH
, home_dir
);
1859 PERROR("snprintf save session");
1860 ret
= LTTNG_ERR_SET_URL
;
1867 * Check the path fits in the config file path dst including the '/'
1868 * followed by trailing .lttng extension and the NULL terminated string.
1870 if ((len
+ session_name_len
+ 2 +
1871 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
))
1872 > sizeof(config_file_path
)) {
1873 ret
= LTTNG_ERR_SET_URL
;
1877 ret
= run_as_mkdir_recursive(config_file_path
, S_IRWXU
| S_IRWXG
,
1878 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1880 ret
= LTTNG_ERR_SET_URL
;
1885 * At this point, we know that everything fits in the buffer. Validation
1886 * was done just above.
1888 config_file_path
[len
++] = '/';
1889 strncpy(config_file_path
+ len
, session
->name
, session_name_len
);
1890 len
+= session_name_len
;
1891 strcpy(config_file_path
+ len
, DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1892 len
+= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
);
1893 config_file_path
[len
] = '\0';
1895 if (!access(config_file_path
, F_OK
) && !attr
->overwrite
) {
1896 /* File exists, notify the user since the overwrite flag is off. */
1897 ret
= LTTNG_ERR_SAVE_FILE_EXIST
;
1901 fd
= run_as_open(config_file_path
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1902 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
1903 LTTNG_SOCK_GET_UID_CRED(creds
), LTTNG_SOCK_GET_GID_CRED(creds
));
1905 PERROR("Could not create configuration file");
1906 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1911 writer
= config_writer_create(fd
, 1);
1913 ret
= LTTNG_ERR_NOMEM
;
1917 ret
= config_writer_open_element(writer
, config_element_sessions
);
1919 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1923 ret
= config_writer_open_element(writer
, config_element_session
);
1925 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1929 ret
= config_writer_write_element_string(writer
, config_element_name
,
1932 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1936 if(session
->shm_path
[0] != '\0') {
1937 ret
= config_writer_write_element_string(writer
,
1938 config_element_shared_memory_path
,
1941 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1946 ret
= save_domains(writer
, session
);
1951 ret
= config_writer_write_element_bool(writer
, config_element_started
,
1954 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1958 if (session
->snapshot_mode
|| session
->live_timer
) {
1959 ret
= config_writer_open_element(writer
, config_element_attributes
);
1961 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1965 if (session
->snapshot_mode
) {
1966 ret
= config_writer_write_element_bool(writer
,
1967 config_element_snapshot_mode
, 1);
1969 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1973 ret
= config_writer_write_element_unsigned_int(writer
,
1974 config_element_live_timer_interval
, session
->live_timer
);
1976 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1982 ret
= config_writer_close_element(writer
);
1984 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
1989 ret
= save_session_output(writer
, session
);
1995 ret
= config_writer_close_element(writer
);
1997 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2002 ret
= config_writer_close_element(writer
);
2004 ret
= LTTNG_ERR_SAVE_IO_FAIL
;
2008 if (writer
&& config_writer_destroy(writer
)) {
2009 /* Preserve the original error code */
2010 ret
= ret
? ret
: LTTNG_ERR_SAVE_IO_FAIL
;
2013 /* Delete file in case of error */
2014 if (file_opened
&& unlink(config_file_path
)) {
2015 PERROR("Unlinking XML session configuration.");
2022 int cmd_save_sessions(struct lttng_save_session_attr
*attr
,
2023 lttng_sock_cred
*creds
)
2026 const char *session_name
;
2027 struct ltt_session
*session
;
2029 session_lock_list();
2031 session_name
= lttng_save_session_attr_get_session_name(attr
);
2033 session
= session_find_by_name(session_name
);
2035 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2039 session_lock(session
);
2040 ret
= save_session(session
, attr
, creds
);
2041 session_unlock(session
);
2046 struct ltt_session_list
*list
= session_get_list();
2048 cds_list_for_each_entry(session
, &list
->head
, list
) {
2049 session_lock(session
);
2050 ret
= save_session(session
, attr
, creds
);
2051 session_unlock(session
);
2053 /* Don't abort if we don't have the required permissions. */
2054 if (ret
&& ret
!= LTTNG_ERR_EPERM
) {
2062 session_unlock_list();