Fix: keep active session state on redundant start command
[lttng-tools.git] / src / bin / lttng-sessiond / save.c
CommitLineData
fb198a11
JG
1/*
2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
fb198a11
JG
19#include <assert.h>
20#include <inttypes.h>
21#include <string.h>
22#include <urcu/uatomic.h>
23#include <unistd.h>
24
25#include <common/defaults.h>
26#include <common/error.h>
f40ef1d5 27#include <common/config/session-config.h>
fb198a11
JG
28#include <common/utils.h>
29#include <common/runas.h>
30#include <lttng/save-internal.h>
31
847a5916 32#include "kernel.h"
fb198a11
JG
33#include "save.h"
34#include "session.h"
0dbc2034 35#include "lttng-syscall.h"
fb198a11 36#include "trace-ust.h"
51755dc8 37#include "agent.h"
fb198a11 38
55c9e7ca 39/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
40static
41int save_kernel_channel_attributes(struct config_writer *writer,
42 struct lttng_channel_attr *attr)
43{
44 int ret;
45
46 ret = config_writer_write_element_string(writer,
47 config_element_overwrite_mode,
48 attr->overwrite ? config_overwrite_mode_overwrite :
49 config_overwrite_mode_discard);
50 if (ret) {
55c9e7ca 51 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
52 goto end;
53 }
54
55 ret = config_writer_write_element_unsigned_int(writer,
56 config_element_subbuf_size, attr->subbuf_size);
57 if (ret) {
55c9e7ca 58 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
59 goto end;
60 }
61
62 ret = config_writer_write_element_unsigned_int(writer,
63 config_element_num_subbuf,
64 attr->num_subbuf);
65 if (ret) {
55c9e7ca 66 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
67 goto end;
68 }
69
70 ret = config_writer_write_element_unsigned_int(writer,
71 config_element_switch_timer_interval,
72 attr->switch_timer_interval);
73 if (ret) {
55c9e7ca 74 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
75 goto end;
76 }
77
78 ret = config_writer_write_element_unsigned_int(writer,
79 config_element_read_timer_interval,
80 attr->read_timer_interval);
81 if (ret) {
55c9e7ca 82 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
83 goto end;
84 }
85
86 ret = config_writer_write_element_string(writer,
87 config_element_output_type,
88 attr->output == LTTNG_EVENT_SPLICE ?
89 config_output_type_splice : config_output_type_mmap);
90 if (ret) {
55c9e7ca 91 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
92 goto end;
93 }
94
95 ret = config_writer_write_element_unsigned_int(writer,
96 config_element_tracefile_size, attr->tracefile_size);
97 if (ret) {
55c9e7ca 98 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
99 goto end;
100 }
101
102 ret = config_writer_write_element_unsigned_int(writer,
103 config_element_tracefile_count,
104 attr->tracefile_count);
105 if (ret) {
55c9e7ca 106 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
107 goto end;
108 }
109
110 ret = config_writer_write_element_unsigned_int(writer,
111 config_element_live_timer_interval,
112 attr->live_timer_interval);
113 if (ret) {
55c9e7ca 114 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
115 goto end;
116 }
4fc2b126
JR
117
118 if (attr->extended.ptr) {
119 struct lttng_channel_extended *ext = NULL;
120
121 ext = (struct lttng_channel_extended *) attr->extended.ptr;
122 ret = config_writer_write_element_unsigned_int(writer,
123 config_element_monitor_timer_interval,
124 ext->monitor_timer_interval);
125 if (ret) {
55c9e7ca 126 ret = LTTNG_ERR_SAVE_IO_FAIL;
4fc2b126
JR
127 goto end;
128 }
275472aa
JR
129
130 ret = config_writer_write_element_signed_int(writer,
131 config_element_blocking_timeout,
132 ext->blocking_timeout);
133 if (ret) {
55c9e7ca 134 ret = LTTNG_ERR_SAVE_IO_FAIL;
275472aa
JR
135 goto end;
136 }
4fc2b126
JR
137 }
138
55c9e7ca 139 ret = LTTNG_OK;
fb198a11 140end:
55c9e7ca 141 return ret;
fb198a11
JG
142}
143
55c9e7ca 144/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
145static
146int save_ust_channel_attributes(struct config_writer *writer,
147 struct lttng_ust_channel_attr *attr)
148{
149 int ret;
4fc2b126 150 struct ltt_ust_channel *channel = NULL;
fb198a11
JG
151
152 ret = config_writer_write_element_string(writer,
153 config_element_overwrite_mode,
154 attr->overwrite ? config_overwrite_mode_overwrite :
155 config_overwrite_mode_discard);
156 if (ret) {
55c9e7ca 157 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
158 goto end;
159 }
160
161 ret = config_writer_write_element_unsigned_int(writer,
162 config_element_subbuf_size, attr->subbuf_size);
163 if (ret) {
55c9e7ca 164 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
165 goto end;
166 }
167
168 ret = config_writer_write_element_unsigned_int(writer,
169 config_element_num_subbuf,
170 attr->num_subbuf);
171 if (ret) {
55c9e7ca 172 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
173 goto end;
174 }
175
176 ret = config_writer_write_element_unsigned_int(writer,
177 config_element_switch_timer_interval,
178 attr->switch_timer_interval);
179 if (ret) {
55c9e7ca 180 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
181 goto end;
182 }
183
184 ret = config_writer_write_element_unsigned_int(writer,
185 config_element_read_timer_interval,
186 attr->read_timer_interval);
187 if (ret) {
55c9e7ca 188 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
189 goto end;
190 }
191
192 ret = config_writer_write_element_string(writer,
193 config_element_output_type,
194 attr->output == LTTNG_UST_MMAP ?
195 config_output_type_mmap : config_output_type_splice);
196 if (ret) {
55c9e7ca 197 ret = LTTNG_ERR_SAVE_IO_FAIL;
fb198a11
JG
198 goto end;
199 }
4fc2b126 200
275472aa
JR
201 ret = config_writer_write_element_signed_int(writer,
202 config_element_blocking_timeout,
203 attr->u.s.blocking_timeout);
204 if (ret) {
55c9e7ca 205 ret = LTTNG_ERR_SAVE_IO_FAIL;
275472aa
JR
206 goto end;
207 }
208
4fc2b126
JR
209 /*
210 * Fetch the monitor timer which is located in the parent of
211 * lttng_ust_channel_attr
212 */
213 channel = caa_container_of(attr, struct ltt_ust_channel, attr);
214 ret = config_writer_write_element_unsigned_int(writer,
215 config_element_monitor_timer_interval,
216 channel->monitor_timer_interval);
217 if (ret) {
55c9e7ca 218 ret = LTTNG_ERR_SAVE_IO_FAIL;
4fc2b126
JR
219 goto end;
220 }
221
55c9e7ca 222 ret = LTTNG_OK;
fb198a11 223end:
55c9e7ca 224 return ret;
fb198a11
JG
225}
226
227static
228const char *get_kernel_instrumentation_string(
229 enum lttng_kernel_instrumentation instrumentation)
230{
231 const char *instrumentation_string;
232
233 switch (instrumentation) {
234 case LTTNG_KERNEL_ALL:
235 instrumentation_string = config_event_type_all;
236 break;
237 case LTTNG_KERNEL_TRACEPOINT:
238 instrumentation_string = config_event_type_tracepoint;
239 break;
240 case LTTNG_KERNEL_KPROBE:
9d82c4c2 241 instrumentation_string = config_event_type_probe;
fb198a11 242 break;
c1e83fb4
FD
243 case LTTNG_KERNEL_UPROBE:
244 instrumentation_string = config_event_type_userspace_probe;
245 break;
fb198a11 246 case LTTNG_KERNEL_FUNCTION:
9d82c4c2 247 instrumentation_string = config_event_type_function_entry;
fb198a11
JG
248 break;
249 case LTTNG_KERNEL_KRETPROBE:
9d82c4c2 250 instrumentation_string = config_event_type_function;
fb198a11
JG
251 break;
252 case LTTNG_KERNEL_NOOP:
253 instrumentation_string = config_event_type_noop;
254 break;
255 case LTTNG_KERNEL_SYSCALL:
256 instrumentation_string = config_event_type_syscall;
257 break;
258 default:
259 instrumentation_string = NULL;
260 }
261
262 return instrumentation_string;
263}
264
265static
266const char *get_kernel_context_type_string(
267 enum lttng_kernel_context_type context_type)
268{
269 const char *context_type_string;
270
271 switch (context_type) {
272 case LTTNG_KERNEL_CONTEXT_PID:
273 context_type_string = config_event_context_pid;
274 break;
275 case LTTNG_KERNEL_CONTEXT_PROCNAME:
276 context_type_string = config_event_context_procname;
277 break;
278 case LTTNG_KERNEL_CONTEXT_PRIO:
279 context_type_string = config_event_context_prio;
280 break;
281 case LTTNG_KERNEL_CONTEXT_NICE:
282 context_type_string = config_event_context_nice;
283 break;
284 case LTTNG_KERNEL_CONTEXT_VPID:
285 context_type_string = config_event_context_vpid;
286 break;
287 case LTTNG_KERNEL_CONTEXT_TID:
288 context_type_string = config_event_context_tid;
289 break;
290 case LTTNG_KERNEL_CONTEXT_VTID:
291 context_type_string = config_event_context_vtid;
292 break;
293 case LTTNG_KERNEL_CONTEXT_PPID:
294 context_type_string = config_event_context_ppid;
295 break;
296 case LTTNG_KERNEL_CONTEXT_VPPID:
297 context_type_string = config_event_context_vppid;
298 break;
299 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
300 context_type_string = config_event_context_hostname;
301 break;
1ae5e83e
JD
302 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE:
303 context_type_string = config_event_context_interruptible;
304 break;
305 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE:
306 context_type_string = config_event_context_preemptible;
307 break;
308 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE:
309 context_type_string = config_event_context_need_reschedule;
310 break;
311 case LTTNG_KERNEL_CONTEXT_MIGRATABLE:
312 context_type_string = config_event_context_migratable;
313 break;
16c4c991
FD
314 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER:
315 context_type_string = config_event_context_callstack_user;
316 break;
317 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL:
318 context_type_string = config_event_context_callstack_kernel;
319 break;
40e14884
MJ
320 case LTTNG_KERNEL_CONTEXT_CGROUP_NS:
321 context_type_string = config_event_context_cgroup_ns;
322 break;
323 case LTTNG_KERNEL_CONTEXT_IPC_NS:
324 context_type_string = config_event_context_ipc_ns;
325 break;
326 case LTTNG_KERNEL_CONTEXT_MNT_NS:
327 context_type_string = config_event_context_mnt_ns;
328 break;
329 case LTTNG_KERNEL_CONTEXT_NET_NS:
330 context_type_string = config_event_context_net_ns;
331 break;
332 case LTTNG_KERNEL_CONTEXT_PID_NS:
333 context_type_string = config_event_context_pid_ns;
334 break;
335 case LTTNG_KERNEL_CONTEXT_USER_NS:
336 context_type_string = config_event_context_user_ns;
337 break;
338 case LTTNG_KERNEL_CONTEXT_UTS_NS:
339 context_type_string = config_event_context_uts_ns;
340 break;
499cbfa1
MJ
341 case LTTNG_KERNEL_CONTEXT_UID:
342 context_type_string = config_event_context_uid;
343 break;
344 case LTTNG_KERNEL_CONTEXT_EUID:
345 context_type_string = config_event_context_euid;
346 break;
347 case LTTNG_KERNEL_CONTEXT_SUID:
348 context_type_string = config_event_context_suid;
349 break;
350 case LTTNG_KERNEL_CONTEXT_GID:
351 context_type_string = config_event_context_gid;
352 break;
353 case LTTNG_KERNEL_CONTEXT_EGID:
354 context_type_string = config_event_context_egid;
355 break;
356 case LTTNG_KERNEL_CONTEXT_SGID:
357 context_type_string = config_event_context_sgid;
358 break;
359 case LTTNG_KERNEL_CONTEXT_VUID:
360 context_type_string = config_event_context_vuid;
361 break;
362 case LTTNG_KERNEL_CONTEXT_VEUID:
363 context_type_string = config_event_context_veuid;
364 break;
365 case LTTNG_KERNEL_CONTEXT_VSUID:
366 context_type_string = config_event_context_vsuid;
367 break;
368 case LTTNG_KERNEL_CONTEXT_VGID:
369 context_type_string = config_event_context_vgid;
370 break;
371 case LTTNG_KERNEL_CONTEXT_VEGID:
372 context_type_string = config_event_context_vegid;
373 break;
374 case LTTNG_KERNEL_CONTEXT_VSGID:
375 context_type_string = config_event_context_vsgid;
376 break;
fb198a11
JG
377 default:
378 context_type_string = NULL;
379 }
380
381 return context_type_string;
382}
383
384static
385const char *get_ust_context_type_string(
386 enum lttng_ust_context_type context_type)
387{
388 const char *context_type_string;
389
390 switch (context_type) {
391 case LTTNG_UST_CONTEXT_PROCNAME:
392 context_type_string = config_event_context_procname;
393 break;
394 case LTTNG_UST_CONTEXT_VPID:
395 context_type_string = config_event_context_vpid;
396 break;
397 case LTTNG_UST_CONTEXT_VTID:
398 context_type_string = config_event_context_vtid;
399 break;
400 case LTTNG_UST_CONTEXT_IP:
401 context_type_string = config_event_context_ip;
402 break;
403 case LTTNG_UST_CONTEXT_PTHREAD_ID:
404 context_type_string = config_event_context_pthread_id;
405 break;
045fc617
JG
406 case LTTNG_UST_CONTEXT_APP_CONTEXT:
407 context_type_string = config_event_context_app;
408 break;
f17b8732
MJ
409 case LTTNG_UST_CONTEXT_CGROUP_NS:
410 context_type_string = config_event_context_cgroup_ns;
411 break;
412 case LTTNG_UST_CONTEXT_IPC_NS:
413 context_type_string = config_event_context_ipc_ns;
414 break;
415 case LTTNG_UST_CONTEXT_MNT_NS:
416 context_type_string = config_event_context_mnt_ns;
417 break;
418 case LTTNG_UST_CONTEXT_NET_NS:
419 context_type_string = config_event_context_net_ns;
420 break;
421 case LTTNG_UST_CONTEXT_PID_NS:
422 context_type_string = config_event_context_pid_ns;
423 break;
424 case LTTNG_UST_CONTEXT_USER_NS:
425 context_type_string = config_event_context_user_ns;
426 break;
427 case LTTNG_UST_CONTEXT_UTS_NS:
428 context_type_string = config_event_context_uts_ns;
429 break;
4fc59cb8
MJ
430 case LTTNG_UST_CONTEXT_VUID:
431 context_type_string = config_event_context_vuid;
432 break;
433 case LTTNG_UST_CONTEXT_VEUID:
434 context_type_string = config_event_context_veuid;
435 break;
436 case LTTNG_UST_CONTEXT_VSUID:
437 context_type_string = config_event_context_vsuid;
438 break;
439 case LTTNG_UST_CONTEXT_VGID:
440 context_type_string = config_event_context_vgid;
441 break;
442 case LTTNG_UST_CONTEXT_VEGID:
443 context_type_string = config_event_context_vegid;
444 break;
445 case LTTNG_UST_CONTEXT_VSGID:
446 context_type_string = config_event_context_vsgid;
447 break;
e885a367 448 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
14ce5bd8
JG
449 /*
450 * Error, should not be stored in the XML, perf contexts
451 * are stored as a node of type event_perf_context_type.
452 */
fb198a11
JG
453 default:
454 context_type_string = NULL;
e885a367 455 break;
fb198a11
JG
456 }
457
458 return context_type_string;
459}
460
461static
462const char *get_buffer_type_string(
463 enum lttng_buffer_type buffer_type)
464{
465 const char *buffer_type_string;
466
467 switch (buffer_type) {
468 case LTTNG_BUFFER_PER_PID:
469 buffer_type_string = config_buffer_type_per_pid;
470 break;
471 case LTTNG_BUFFER_PER_UID:
472 buffer_type_string = config_buffer_type_per_uid;
473 break;
474 case LTTNG_BUFFER_GLOBAL:
475 buffer_type_string = config_buffer_type_global;
476 break;
477 default:
478 buffer_type_string = NULL;
479 }
480
481 return buffer_type_string;
482}
483
484static
485const char *get_loglevel_type_string(
486 enum lttng_ust_loglevel_type loglevel_type)
487{
488 const char *loglevel_type_string;
489
490 switch (loglevel_type) {
491 case LTTNG_UST_LOGLEVEL_ALL:
492 loglevel_type_string = config_loglevel_type_all;
493 break;
494 case LTTNG_UST_LOGLEVEL_RANGE:
495 loglevel_type_string = config_loglevel_type_range;
496 break;
497 case LTTNG_UST_LOGLEVEL_SINGLE:
498 loglevel_type_string = config_loglevel_type_single;
499 break;
500 default:
501 loglevel_type_string = NULL;
502 }
503
504 return loglevel_type_string;
505}
506
55c9e7ca 507/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11 508static
83712c39
FD
509int save_kernel_function_event(struct config_writer *writer,
510 struct ltt_kernel_event *event)
511{
512 int ret;
513
514 ret = config_writer_open_element(writer, config_element_function_attributes);
515 if (ret) {
516 ret = LTTNG_ERR_SAVE_IO_FAIL;
517 goto end;
518 }
519
520 ret = config_writer_write_element_string(writer, config_element_name,
521 event->event->u.ftrace.symbol_name);
522 if (ret) {
523 ret = LTTNG_ERR_SAVE_IO_FAIL;
524 goto end;
525 }
526
527 /* /function attributes */
528 ret = config_writer_close_element(writer);
529 if (ret) {
530 ret = LTTNG_ERR_SAVE_IO_FAIL;
531 goto end;
532 }
533end:
534 return ret;
535}
536
537static
538int save_kernel_kprobe_event(struct config_writer *writer,
539 struct ltt_kernel_event *event)
540{
541 int ret;
542 const char *symbol_name;
543 uint64_t addr;
544 uint64_t offset;
545
546 switch (event->event->instrumentation) {
547 case LTTNG_KERNEL_KPROBE:
548 /*
549 * Comments in lttng-kernel.h mention that
550 * either addr or symbol_name are set, not both.
551 */
552 addr = event->event->u.kprobe.addr;
553 offset = event->event->u.kprobe.offset;
554 symbol_name = addr ? NULL : event->event->u.kprobe.symbol_name;
555 break;
556 case LTTNG_KERNEL_KRETPROBE:
557 addr = event->event->u.kretprobe.addr;
558 offset = event->event->u.kretprobe.offset;
c1e83fb4 559 symbol_name = addr ? NULL : event->event->u.kretprobe.symbol_name;
83712c39
FD
560 break;
561 default:
562 assert(1);
c1e83fb4
FD
563 ERR("Unsupported kernel instrumentation type.");
564 ret = LTTNG_ERR_INVALID;
565 goto end;
83712c39
FD
566 }
567
568 ret = config_writer_open_element(writer, config_element_probe_attributes);
569 if (ret) {
570 ret = LTTNG_ERR_SAVE_IO_FAIL;
571 goto end;
572 }
573
c1e83fb4
FD
574 if (addr) {
575 ret = config_writer_write_element_unsigned_int( writer,
576 config_element_address, addr);
577 if (ret) {
578 ret = LTTNG_ERR_SAVE_IO_FAIL;
579 goto end;
580 }
581 } else if (symbol_name) {
83712c39
FD
582 ret = config_writer_write_element_string(writer,
583 config_element_symbol_name, symbol_name);
584 if (ret) {
585 ret = LTTNG_ERR_SAVE_IO_FAIL;
586 goto end;
587 }
c1e83fb4
FD
588 /* If the offset is non-zero, write it.*/
589 if (offset) {
590 ret = config_writer_write_element_unsigned_int(writer,
591 config_element_offset, offset);
592 if (ret) {
593 ret = LTTNG_ERR_SAVE_IO_FAIL;
594 goto end;
595 }
596 }
597 } else {
598 /*
599 * This really should not happen as we are either setting the
600 * address or the symbol above.
601 */
602 ERR("Invalid probe/function description.");
603 ret = LTTNG_ERR_INVALID;
604 goto end;
83712c39
FD
605 }
606
c1e83fb4
FD
607
608 ret = config_writer_close_element(writer);
609 if (ret) {
610 ret = LTTNG_ERR_SAVE_IO_FAIL;
611 goto end;
612 }
613end:
614 return ret;
615}
616
617/*
618 * Save the userspace probe tracepoint event associated with the event to the
619 * config writer.
620 */
621static
622int save_kernel_userspace_probe_tracepoint_event(struct config_writer *writer,
623 struct ltt_kernel_event *event)
624{
625 int ret = 0;
626 const char *probe_name, *provider_name, *binary_path;
87597c2c
JG
627 const struct lttng_userspace_probe_location *userspace_probe_location;
628 const struct lttng_userspace_probe_location_lookup_method *lookup_method;
c1e83fb4
FD
629 enum lttng_userspace_probe_location_lookup_method_type lookup_type;
630
631 /* Get userspace probe location from the event. */
632 userspace_probe_location = event->userspace_probe_location;
633 if (!userspace_probe_location) {
634 ret = LTTNG_ERR_SAVE_IO_FAIL;
635 goto end;
636 }
637
638 /* Get lookup method and lookup method type. */
639 lookup_method = lttng_userspace_probe_location_get_lookup_method(userspace_probe_location);
640 if (!lookup_method) {
641 ret = LTTNG_ERR_SAVE_IO_FAIL;
642 goto end;
643 }
644
645 lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
646
647 /* Get the binary path, probe name and provider name. */
648 binary_path =
649 lttng_userspace_probe_location_tracepoint_get_binary_path(
650 userspace_probe_location);
651 if (!binary_path) {
652 ret = LTTNG_ERR_SAVE_IO_FAIL;
653 goto end;
654 }
655
656 probe_name =
657 lttng_userspace_probe_location_tracepoint_get_probe_name(
658 userspace_probe_location);
659 if (!probe_name) {
660 ret = LTTNG_ERR_SAVE_IO_FAIL;
661 goto end;
662 }
663
664 provider_name =
665 lttng_userspace_probe_location_tracepoint_get_provider_name(
666 userspace_probe_location);
667 if (!provider_name) {
668 ret = LTTNG_ERR_SAVE_IO_FAIL;
669 goto end;
670 }
671
672 /* Open a userspace probe tracepoint attribute. */
673 ret = config_writer_open_element(writer, config_element_userspace_probe_tracepoint_attributes);
674 if (ret) {
675 ret = LTTNG_ERR_SAVE_IO_FAIL;
676 goto end;
677 }
678
679 switch (lookup_type) {
680 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
681 ret = config_writer_write_element_string(writer,
682 config_element_userspace_probe_lookup,
683 config_element_userspace_probe_lookup_tracepoint_sdt);
83712c39
FD
684 if (ret) {
685 ret = LTTNG_ERR_SAVE_IO_FAIL;
686 goto end;
687 }
c1e83fb4
FD
688 break;
689 default:
690 ERR("Unsupported kernel userspace probe tracepoint lookup method.");
691 ret = LTTNG_ERR_INVALID;
692 goto end;
83712c39
FD
693 }
694
c1e83fb4
FD
695 /* Write the binary path, provider name and the probe name. */
696 ret = config_writer_write_element_string(writer,
697 config_element_userspace_probe_location_binary_path,
698 binary_path);
699 if (ret) {
700 ret = LTTNG_ERR_SAVE_IO_FAIL;
701 goto end;
702 }
703
704 ret = config_writer_write_element_string(writer,
705 config_element_userspace_probe_tracepoint_location_provider_name,
706 provider_name);
707 if (ret) {
708 ret = LTTNG_ERR_SAVE_IO_FAIL;
709 goto end;
710 }
711
712 ret = config_writer_write_element_string(writer,
713 config_element_userspace_probe_tracepoint_location_probe_name,
714 probe_name);
715 if (ret) {
716 ret = LTTNG_ERR_SAVE_IO_FAIL;
717 goto end;
718 }
719
720 /* Close the userspace probe tracepoint attribute. */
721 ret = config_writer_close_element(writer);
722 if (ret) {
723 ret = LTTNG_ERR_SAVE_IO_FAIL;
724 goto end;
725 }
726
727end:
728 return ret;
729}
730
731/*
732 * Save the userspace probe function event associated with the event to the
733 * config writer.
734 */
735static
736int save_kernel_userspace_probe_function_event(struct config_writer *writer,
737 struct ltt_kernel_event *event)
738{
739 int ret = 0;
740 const char *function_name, *binary_path;
87597c2c
JG
741 const struct lttng_userspace_probe_location *userspace_probe_location;
742 const struct lttng_userspace_probe_location_lookup_method *lookup_method;
c1e83fb4
FD
743 enum lttng_userspace_probe_location_lookup_method_type lookup_type;
744
745 /* Get userspace probe location from the event. */
746 userspace_probe_location = event->userspace_probe_location;
747 if (!userspace_probe_location) {
748 ret = LTTNG_ERR_SAVE_IO_FAIL;
749 goto end;
750 }
751
752 /* Get lookup method and lookup method type. */
753 lookup_method = lttng_userspace_probe_location_get_lookup_method(
754 userspace_probe_location);
755 if (!lookup_method) {
756 ret = LTTNG_ERR_SAVE_IO_FAIL;
757 goto end;
758 }
759
760 /* Get the binary path and the function name. */
761 binary_path =
762 lttng_userspace_probe_location_function_get_binary_path(
763 userspace_probe_location);
764 if (!binary_path) {
765 ret = LTTNG_ERR_SAVE_IO_FAIL;
766 goto end;
767 }
768
769 function_name =
770 lttng_userspace_probe_location_function_get_function_name(
771 userspace_probe_location);
772 if (!function_name) {
773 ret = LTTNG_ERR_SAVE_IO_FAIL;
774 goto end;
775 }
776
777 /* Open a userspace probe function attribute. */
778 ret = config_writer_open_element(writer,
779 config_element_userspace_probe_function_attributes);
780 if (ret) {
781 ret = LTTNG_ERR_SAVE_IO_FAIL;
782 goto end;
783 }
784
785 lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
786 switch (lookup_type) {
787 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
788 ret = config_writer_write_element_string(writer,
789 config_element_userspace_probe_lookup,
790 config_element_userspace_probe_lookup_function_elf);
83712c39
FD
791 if (ret) {
792 ret = LTTNG_ERR_SAVE_IO_FAIL;
793 goto end;
794 }
c1e83fb4
FD
795 break;
796 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
797 ret = config_writer_write_element_string(writer,
798 config_element_userspace_probe_lookup,
799 config_element_userspace_probe_lookup_function_default);
800 if (ret) {
801 ret = LTTNG_ERR_SAVE_IO_FAIL;
802 goto end;
803 }
804 break;
805 default:
806 ERR("Unsupported kernel userspace probe function lookup method.");
807 ret = LTTNG_ERR_INVALID;
808 goto end;
83712c39
FD
809 }
810
c1e83fb4
FD
811 /* Write the binary path and the function name. */
812 ret = config_writer_write_element_string(writer,
813 config_element_userspace_probe_location_binary_path,
814 binary_path);
815 if (ret) {
816 ret = LTTNG_ERR_SAVE_IO_FAIL;
817 goto end;
818 }
819
820 ret = config_writer_write_element_string(writer,
821 config_element_userspace_probe_function_location_function_name,
822 function_name);
823 if (ret) {
824 ret = LTTNG_ERR_SAVE_IO_FAIL;
825 goto end;
826 }
827
828 /* Close the userspace probe function attribute. */
83712c39
FD
829 ret = config_writer_close_element(writer);
830 if (ret) {
831 ret = LTTNG_ERR_SAVE_IO_FAIL;
832 goto end;
833 }
c1e83fb4 834
83712c39
FD
835end:
836 return ret;
837}
c1e83fb4
FD
838
839static
840int save_kernel_userspace_probe_event(struct config_writer *writer,
841 struct ltt_kernel_event *event)
842{
843 int ret;
844 struct lttng_userspace_probe_location *userspace_probe_location;
845
846 /* Get userspace probe location from the event. */
847 userspace_probe_location = event->userspace_probe_location;
848 if (!userspace_probe_location) {
849 ret = LTTNG_ERR_SAVE_IO_FAIL;
850 goto end;
851 }
852
853 switch(lttng_userspace_probe_location_get_type(userspace_probe_location)) {
854 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
855 {
856 ret = save_kernel_userspace_probe_function_event(writer, event);
857 if (ret) {
858 ret = LTTNG_ERR_SAVE_IO_FAIL;
859 goto end;
860 }
861 break;
862 }
863 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
864 {
865 ret = save_kernel_userspace_probe_tracepoint_event(writer, event);
866 if (ret) {
867 ret = LTTNG_ERR_SAVE_IO_FAIL;
868 goto end;
869 }
870 break;
871 }
872 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN:
873 default:
874 ERR("Unsupported kernel userspace probe location type.");
875 ret = LTTNG_ERR_INVALID;
876 goto end;
877 }
878
879end:
880 return ret;
881}
882
883static
fb198a11 884int save_kernel_event(struct config_writer *writer,
83712c39 885 struct ltt_kernel_event *event)
fb198a11
JG
886{
887 int ret;
888 const char *instrumentation_type;
889
890 ret = config_writer_open_element(writer, config_element_event);
891 if (ret) {
892 ret = LTTNG_ERR_SAVE_IO_FAIL;
893 goto end;
894 }
895
896 if (event->event->name[0]) {
897 ret = config_writer_write_element_string(writer,
898 config_element_name, event->event->name);
899 if (ret) {
900 ret = LTTNG_ERR_SAVE_IO_FAIL;
901 goto end;
902 }
903 }
904
905 ret = config_writer_write_element_bool(writer, config_element_enabled,
906 event->enabled);
907 if (ret) {
908 ret = LTTNG_ERR_SAVE_IO_FAIL;
909 goto end;
910 }
911
912 instrumentation_type = get_kernel_instrumentation_string(
913 event->event->instrumentation);
914 if (!instrumentation_type) {
915 ret = LTTNG_ERR_INVALID;
916 goto end;
917 }
918
919 ret = config_writer_write_element_string(writer, config_element_type,
920 instrumentation_type);
921 if (ret) {
922 ret = LTTNG_ERR_SAVE_IO_FAIL;
923 goto end;
924 }
925
911d1560
JG
926 if (event->filter_expression) {
927 ret = config_writer_write_element_string(writer,
928 config_element_filter,
929 event->filter_expression);
930 if (ret) {
931 ret = LTTNG_ERR_SAVE_IO_FAIL;
932 goto end;
933 }
934 }
935
fb198a11
JG
936 if (event->event->instrumentation == LTTNG_KERNEL_FUNCTION ||
937 event->event->instrumentation == LTTNG_KERNEL_KPROBE ||
c1e83fb4 938 event->event->instrumentation == LTTNG_KERNEL_UPROBE ||
fb198a11
JG
939 event->event->instrumentation == LTTNG_KERNEL_KRETPROBE) {
940
941 ret = config_writer_open_element(writer,
942 config_element_attributes);
943 if (ret) {
944 ret = LTTNG_ERR_SAVE_IO_FAIL;
945 goto end;
946 }
947
948 switch (event->event->instrumentation) {
0de3eda1 949 case LTTNG_KERNEL_SYSCALL:
fb198a11 950 case LTTNG_KERNEL_FUNCTION:
83712c39 951 ret = save_kernel_function_event(writer, event);
fb198a11 952 if (ret) {
fb198a11
JG
953 goto end;
954 }
955 break;
956 case LTTNG_KERNEL_KPROBE:
957 case LTTNG_KERNEL_KRETPROBE:
83712c39 958 ret = save_kernel_kprobe_event(writer, event);
fb198a11 959 if (ret) {
fb198a11
JG
960 goto end;
961 }
962 break;
c1e83fb4
FD
963 case LTTNG_KERNEL_UPROBE:
964 ret = save_kernel_userspace_probe_event(writer, event);
965 if (ret) {
966 goto end;
967 }
968 break;
fb198a11
JG
969 default:
970 ERR("Unsupported kernel instrumentation type.");
971 ret = LTTNG_ERR_INVALID;
972 goto end;
973 }
974
975 /* /attributes */
976 ret = config_writer_close_element(writer);
977 if (ret) {
978 ret = LTTNG_ERR_SAVE_IO_FAIL;
979 goto end;
980 }
981 }
982
983 /* /event */
984 ret = config_writer_close_element(writer);
985 if (ret) {
986 ret = LTTNG_ERR_SAVE_IO_FAIL;
987 goto end;
988 }
55c9e7ca
JR
989
990 ret = LTTNG_OK;
fb198a11
JG
991end:
992 return ret;
993}
994
55c9e7ca 995/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
996static
997int save_kernel_events(struct config_writer *writer,
0de3eda1 998 struct ltt_kernel_channel *kchan)
fb198a11
JG
999{
1000 int ret;
1001 struct ltt_kernel_event *event;
1002
1003 ret = config_writer_open_element(writer, config_element_events);
1004 if (ret) {
1005 ret = LTTNG_ERR_SAVE_IO_FAIL;
1006 goto end;
1007 }
1008
0de3eda1 1009 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
fb198a11 1010 ret = save_kernel_event(writer, event);
55c9e7ca 1011 if (ret != LTTNG_OK) {
fb198a11
JG
1012 goto end;
1013 }
1014 }
1015
1016 /* /events */
1017 ret = config_writer_close_element(writer);
1018 if (ret) {
1019 ret = LTTNG_ERR_SAVE_IO_FAIL;
1020 goto end;
1021 }
55c9e7ca
JR
1022
1023 ret = LTTNG_OK;
fb198a11
JG
1024end:
1025 return ret;
1026}
1027
55c9e7ca 1028/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1029static
1030int save_ust_event(struct config_writer *writer,
1031 struct ltt_ust_event *event)
1032{
1033 int ret;
1034 const char *loglevel_type_string;
1035
1036 ret = config_writer_open_element(writer, config_element_event);
1037 if (ret) {
1038 ret = LTTNG_ERR_SAVE_IO_FAIL;
1039 goto end;
1040 }
1041
1042 if (event->attr.name[0]) {
1043 ret = config_writer_write_element_string(writer,
1044 config_element_name, event->attr.name);
1045 if (ret) {
1046 ret = LTTNG_ERR_SAVE_IO_FAIL;
1047 goto end;
1048 }
1049 }
1050
1051 ret = config_writer_write_element_bool(writer, config_element_enabled,
1052 event->enabled);
1053 if (ret) {
1054 ret = LTTNG_ERR_SAVE_IO_FAIL;
1055 goto end;
1056 }
1057
1058 if (event->attr.instrumentation != LTTNG_UST_TRACEPOINT) {
1059 ERR("Unsupported UST instrumentation type.");
1060 ret = LTTNG_ERR_INVALID;
1061 goto end;
1062 }
1063 ret = config_writer_write_element_string(writer, config_element_type,
1064 config_event_type_tracepoint);
1065 if (ret) {
1066 ret = LTTNG_ERR_SAVE_IO_FAIL;
1067 goto end;
1068 }
1069
1070 loglevel_type_string = get_loglevel_type_string(
1071 event->attr.loglevel_type);
1072 if (!loglevel_type_string) {
1073 ERR("Unsupported UST loglevel type.");
1074 ret = LTTNG_ERR_INVALID;
1075 goto end;
1076 }
1077
1078 ret = config_writer_write_element_string(writer,
1079 config_element_loglevel_type, loglevel_type_string);
1080 if (ret) {
1081 ret = LTTNG_ERR_SAVE_IO_FAIL;
1082 goto end;
1083 }
1084
1adbdb10
JG
1085 /* The log level is irrelevant if no "filtering" is enabled */
1086 if (event->attr.loglevel_type != LTTNG_UST_LOGLEVEL_ALL) {
1087 ret = config_writer_write_element_signed_int(writer,
1088 config_element_loglevel, event->attr.loglevel);
1089 if (ret) {
1090 ret = LTTNG_ERR_SAVE_IO_FAIL;
1091 goto end;
1092 }
fb198a11
JG
1093 }
1094
1095 if (event->filter_expression) {
1096 ret = config_writer_write_element_string(writer,
1097 config_element_filter, event->filter_expression);
1098 if (ret) {
1099 ret = LTTNG_ERR_SAVE_IO_FAIL;
1100 goto end;
1101 }
1102 }
1103
1104 if (event->exclusion && event->exclusion->count) {
1105 uint32_t i;
1106
1107 ret = config_writer_open_element(writer,
1108 config_element_exclusions);
1109 if (ret) {
1110 ret = LTTNG_ERR_SAVE_IO_FAIL;
1111 goto end;
1112 }
1113
1114 for (i = 0; i < event->exclusion->count; i++) {
1115 ret = config_writer_write_element_string(writer,
1116 config_element_exclusion,
d7af3565
PP
1117 LTTNG_EVENT_EXCLUSION_NAME_AT(
1118 event->exclusion, i));
fb198a11
JG
1119 if (ret) {
1120 ret = LTTNG_ERR_SAVE_IO_FAIL;
1121 goto end;
1122 }
1123 }
1124
1125 /* /exclusions */
1126 ret = config_writer_close_element(writer);
1127 if (ret) {
1128 ret = LTTNG_ERR_SAVE_IO_FAIL;
1129 goto end;
1130 }
1131 }
1132
1133 /* /event */
1134 ret = config_writer_close_element(writer);
1135 if (ret) {
1136 ret = LTTNG_ERR_SAVE_IO_FAIL;
1137 goto end;
1138 }
55c9e7ca
JR
1139
1140 ret = LTTNG_OK;
fb198a11
JG
1141end:
1142 return ret;
1143}
1144
55c9e7ca 1145/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1146static
1147int save_ust_events(struct config_writer *writer,
1148 struct lttng_ht *events)
1149{
1150 int ret;
1151 struct ltt_ust_event *event;
1152 struct lttng_ht_node_str *node;
1153 struct lttng_ht_iter iter;
1154
1155 ret = config_writer_open_element(writer, config_element_events);
1156 if (ret) {
1157 ret = LTTNG_ERR_SAVE_IO_FAIL;
1158 goto end;
1159 }
1160
1161 rcu_read_lock();
1162 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1163 event = caa_container_of(node, struct ltt_ust_event, node);
1164
af325ba1
JG
1165 if (event->internal) {
1166 /* Internal events must not be exposed to clients */
1167 continue;
1168 }
fb198a11 1169 ret = save_ust_event(writer, event);
55c9e7ca 1170 if (ret != LTTNG_OK) {
fb198a11
JG
1171 rcu_read_unlock();
1172 goto end;
1173 }
1174 }
1175 rcu_read_unlock();
1176
1177 /* /events */
1178 ret = config_writer_close_element(writer);
1179 if (ret) {
1180 ret = LTTNG_ERR_SAVE_IO_FAIL;
1181 goto end;
1182 }
55c9e7ca
JR
1183
1184 ret = LTTNG_OK;
fb198a11
JG
1185end:
1186 return ret;
1187}
1188
55c9e7ca 1189/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
51755dc8 1190static
0b35b846 1191int init_ust_event_from_agent_event(struct ltt_ust_event *ust_event,
51755dc8
JG
1192 struct agent_event *agent_event)
1193{
55c9e7ca 1194 int ret;
0b35b846
JG
1195 enum lttng_ust_loglevel_type ust_loglevel_type;
1196
51755dc8
JG
1197 ust_event->enabled = agent_event->enabled;
1198 ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT;
d333bdaa
MD
1199 if (lttng_strncpy(ust_event->attr.name, agent_event->name,
1200 LTTNG_SYMBOL_NAME_LEN)) {
55c9e7ca 1201 ret = LTTNG_ERR_INVALID;
d333bdaa
MD
1202 goto end;
1203 }
0b35b846
JG
1204 switch (agent_event->loglevel_type) {
1205 case LTTNG_EVENT_LOGLEVEL_ALL:
1206 ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
1207 break;
1208 case LTTNG_EVENT_LOGLEVEL_SINGLE:
1209 ust_loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
1210 break;
1211 case LTTNG_EVENT_LOGLEVEL_RANGE:
1212 ust_loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
1213 break;
1214 default:
1215 ERR("Invalid agent_event loglevel_type.");
55c9e7ca 1216 ret = LTTNG_ERR_INVALID;
0b35b846
JG
1217 goto end;
1218 }
1219
1220 ust_event->attr.loglevel_type = ust_loglevel_type;
2106efa0 1221 ust_event->attr.loglevel = agent_event->loglevel_value;
51755dc8
JG
1222 ust_event->filter_expression = agent_event->filter_expression;
1223 ust_event->exclusion = agent_event->exclusion;
55c9e7ca
JR
1224
1225 ret = LTTNG_OK;
0b35b846
JG
1226end:
1227 return ret;
51755dc8
JG
1228}
1229
55c9e7ca 1230/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
51755dc8
JG
1231static
1232int save_agent_events(struct config_writer *writer,
51755dc8
JG
1233 struct agent *agent)
1234{
1235 int ret;
1236 struct lttng_ht_iter iter;
1237 struct lttng_ht_node_str *node;
1238
1239 ret = config_writer_open_element(writer, config_element_events);
1240 if (ret) {
1241 ret = LTTNG_ERR_SAVE_IO_FAIL;
1242 goto end;
1243 }
1244
1245 rcu_read_lock();
1246 cds_lfht_for_each_entry(agent->events->ht, &iter.iter, node, node) {
1247 int ret;
1248 struct agent_event *agent_event;
1249 struct ltt_ust_event fake_event;
1250
1251 memset(&fake_event, 0, sizeof(fake_event));
1252 agent_event = caa_container_of(node, struct agent_event, node);
1253
1254 /*
1255 * Initialize a fake ust event to reuse the same serialization
1256 * function since UST and agent events contain the same info
1257 * (and one could wonder why they don't reuse the same
1258 * structures...).
1259 */
0b35b846 1260 ret = init_ust_event_from_agent_event(&fake_event, agent_event);
55c9e7ca 1261 if (ret != LTTNG_OK) {
0b35b846
JG
1262 rcu_read_unlock();
1263 goto end;
1264 }
51755dc8 1265 ret = save_ust_event(writer, &fake_event);
55c9e7ca 1266 if (ret != LTTNG_OK) {
51755dc8
JG
1267 rcu_read_unlock();
1268 goto end;
1269 }
1270 }
1271 rcu_read_unlock();
1272
1273 /* /events */
1274 ret = config_writer_close_element(writer);
1275 if (ret) {
1276 ret = LTTNG_ERR_SAVE_IO_FAIL;
1277 goto end;
1278 }
55c9e7ca
JR
1279
1280 ret = LTTNG_OK;
51755dc8
JG
1281end:
1282 return ret;
1283}
1284
55c9e7ca 1285/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1286static
1287int save_kernel_context(struct config_writer *writer,
1288 struct lttng_kernel_context *ctx)
1289{
55c9e7ca 1290 int ret = LTTNG_OK;
fb198a11
JG
1291
1292 if (!ctx) {
1293 goto end;
1294 }
1295
fb198a11
JG
1296 ret = config_writer_open_element(writer, config_element_context);
1297 if (ret) {
1298 ret = LTTNG_ERR_SAVE_IO_FAIL;
1299 goto end;
1300 }
1301
cba45eda 1302 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
045fc617
JG
1303 ret = config_writer_open_element(writer,
1304 config_element_context_perf);
fb198a11
JG
1305 if (ret) {
1306 ret = LTTNG_ERR_SAVE_IO_FAIL;
1307 goto end;
1308 }
1309
1310 ret = config_writer_write_element_unsigned_int(writer,
1311 config_element_type, ctx->u.perf_counter.type);
1312 if (ret) {
1313 ret = LTTNG_ERR_SAVE_IO_FAIL;
1314 goto end;
1315 }
1316
1317 ret = config_writer_write_element_unsigned_int(writer,
1318 config_element_config, ctx->u.perf_counter.config);
1319 if (ret) {
1320 ret = LTTNG_ERR_SAVE_IO_FAIL;
1321 goto end;
1322 }
1323
1324 ret = config_writer_write_element_string(writer,
1325 config_element_name, ctx->u.perf_counter.name);
1326 if (ret) {
1327 ret = LTTNG_ERR_SAVE_IO_FAIL;
1328 goto end;
1329 }
1330
1331 /* /perf */
1332 ret = config_writer_close_element(writer);
1333 if (ret) {
1334 ret = LTTNG_ERR_SAVE_IO_FAIL;
1335 goto end;
1336 }
1337 } else {
1338 const char *context_type_string =
1339 get_kernel_context_type_string(ctx->ctx);
1340
1341 if (!context_type_string) {
1342 ERR("Unsupported kernel context type.");
1343 ret = LTTNG_ERR_INVALID;
1344 goto end;
1345 }
1346
1347 ret = config_writer_write_element_string(writer,
1348 config_element_type, context_type_string);
1349 if (ret) {
1350 ret = LTTNG_ERR_SAVE_IO_FAIL;
1351 goto end;
1352 }
1353 }
1354
1355 /* /context */
1356 ret = config_writer_close_element(writer);
1357 if (ret) {
1358 ret = LTTNG_ERR_SAVE_IO_FAIL;
1359 goto end;
1360 }
1361
55c9e7ca 1362 ret = LTTNG_OK;
645328ae
DG
1363end:
1364 return ret;
1365}
1366
55c9e7ca 1367/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
645328ae
DG
1368static
1369int save_kernel_contexts(struct config_writer *writer,
1370 struct ltt_kernel_channel *kchan)
1371{
1372 int ret;
1373 struct ltt_kernel_context *ctx;
1374
2aa64052 1375 if (cds_list_empty(&kchan->ctx_list)) {
55c9e7ca 1376 ret = LTTNG_OK;
2aa64052
JG
1377 goto end;
1378 }
1379
645328ae
DG
1380 ret = config_writer_open_element(writer, config_element_contexts);
1381 if (ret) {
1382 ret = LTTNG_ERR_SAVE_IO_FAIL;
1383 goto end;
1384 }
1385
1386 cds_list_for_each_entry(ctx, &kchan->ctx_list, list) {
1387 ret = save_kernel_context(writer, &ctx->ctx);
55c9e7ca 1388 if (ret != LTTNG_OK) {
645328ae
DG
1389 goto end;
1390 }
1391 }
1392
fb198a11
JG
1393 /* /contexts */
1394 ret = config_writer_close_element(writer);
1395 if (ret) {
1396 ret = LTTNG_ERR_SAVE_IO_FAIL;
1397 goto end;
1398 }
55c9e7ca
JR
1399
1400 ret = LTTNG_OK;
fb198a11
JG
1401end:
1402 return ret;
1403}
1404
55c9e7ca 1405/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
045fc617
JG
1406static
1407int save_ust_context_perf_thread_counter(struct config_writer *writer,
1408 struct ltt_ust_context *ctx)
1409{
1410 int ret;
1411
1412 assert(writer);
1413 assert(ctx);
1414
1415 /* Perf contexts are saved as event_perf_context_type */
1416 ret = config_writer_open_element(writer, config_element_context_perf);
1417 if (ret) {
1418 ret = LTTNG_ERR_SAVE_IO_FAIL;
1419 goto end;
1420 }
1421
1422 ret = config_writer_write_element_unsigned_int(writer,
1423 config_element_type, ctx->ctx.u.perf_counter.type);
1424 if (ret) {
1425 ret = LTTNG_ERR_SAVE_IO_FAIL;
1426 goto end;
1427 }
1428
1429 ret = config_writer_write_element_unsigned_int(writer,
1430 config_element_config, ctx->ctx.u.perf_counter.config);
1431 if (ret) {
1432 ret = LTTNG_ERR_SAVE_IO_FAIL;
1433 goto end;
1434 }
1435
1436 ret = config_writer_write_element_string(writer, config_element_name,
1437 ctx->ctx.u.perf_counter.name);
1438 if (ret) {
1439 ret = LTTNG_ERR_SAVE_IO_FAIL;
1440 goto end;
1441 }
1442
1443 /* /perf */
1444 ret = config_writer_close_element(writer);
1445 if (ret) {
1446 ret = LTTNG_ERR_SAVE_IO_FAIL;
1447 goto end;
1448 }
55c9e7ca
JR
1449
1450 ret = LTTNG_OK;
045fc617
JG
1451end:
1452 return ret;
1453}
1454
55c9e7ca 1455/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
045fc617
JG
1456static
1457int save_ust_context_app_ctx(struct config_writer *writer,
1458 struct ltt_ust_context *ctx)
1459{
1460 int ret;
1461
1462 assert(writer);
1463 assert(ctx);
1464
1465 /* Application contexts are saved as application_context_type */
1466 ret = config_writer_open_element(writer, config_element_context_app);
1467 if (ret) {
1468 ret = LTTNG_ERR_SAVE_IO_FAIL;
1469 goto end;
1470 }
1471
1472 ret = config_writer_write_element_string(writer,
1473 config_element_context_app_provider_name,
1474 ctx->ctx.u.app_ctx.provider_name);
1475 if (ret) {
1476 ret = LTTNG_ERR_SAVE_IO_FAIL;
1477 goto end;
1478 }
1479
1480 ret = config_writer_write_element_string(writer,
1481 config_element_context_app_ctx_name,
1482 ctx->ctx.u.app_ctx.ctx_name);
1483 if (ret) {
1484 ret = LTTNG_ERR_SAVE_IO_FAIL;
1485 goto end;
1486 }
1487
1488 /* /app */
1489 ret = config_writer_close_element(writer);
1490 if (ret) {
1491 ret = LTTNG_ERR_SAVE_IO_FAIL;
1492 goto end;
1493 }
55c9e7ca
JR
1494
1495 ret = LTTNG_OK;
045fc617
JG
1496end:
1497 return ret;
1498}
1499
55c9e7ca 1500/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
045fc617
JG
1501static
1502int save_ust_context_generic(struct config_writer *writer,
1503 struct ltt_ust_context *ctx)
1504{
1505 int ret;
1506 const char *context_type_string;
1507
1508 assert(writer);
1509 assert(ctx);
1510
1511 /* Save context as event_context_type_type */
1512 context_type_string = get_ust_context_type_string(
1513 ctx->ctx.ctx);
1514 if (!context_type_string) {
1515 ERR("Unsupported UST context type.");
1516 ret = LTTNG_ERR_SAVE_IO_FAIL;
1517 goto end;
1518 }
1519
1520 ret = config_writer_write_element_string(writer,
1521 config_element_type, context_type_string);
1522 if (ret) {
1523 ret = LTTNG_ERR_SAVE_IO_FAIL;
1524 goto end;
1525 }
55c9e7ca
JR
1526
1527 ret = LTTNG_OK;
045fc617
JG
1528end:
1529 return ret;
1530}
1531
55c9e7ca 1532/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1533static
1534int save_ust_context(struct config_writer *writer,
1535 struct cds_list_head *ctx_list)
1536{
1537 int ret;
1538 struct ltt_ust_context *ctx;
1539
1540 assert(writer);
1541 assert(ctx_list);
1542
1543 ret = config_writer_open_element(writer, config_element_contexts);
1544 if (ret) {
1545 ret = LTTNG_ERR_SAVE_IO_FAIL;
1546 goto end;
1547 }
1548
1549 cds_list_for_each_entry(ctx, ctx_list, list) {
fb198a11
JG
1550 ret = config_writer_open_element(writer,
1551 config_element_context);
1552 if (ret) {
1553 ret = LTTNG_ERR_SAVE_IO_FAIL;
1554 goto end;
1555 }
1556
045fc617
JG
1557 switch (ctx->ctx.ctx) {
1558 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
1559 ret = save_ust_context_perf_thread_counter(writer, ctx);
1560 break;
1561 case LTTNG_UST_CONTEXT_APP_CONTEXT:
1562 ret = save_ust_context_app_ctx(writer, ctx);
1563 break;
1564 default:
1565 /* Save generic context. */
1566 ret = save_ust_context_generic(writer, ctx);
1567 }
55c9e7ca 1568 if (ret != LTTNG_OK) {
045fc617 1569 goto end;
fb198a11
JG
1570 }
1571
1572 /* /context */
1573 ret = config_writer_close_element(writer);
1574 if (ret) {
1575 ret = LTTNG_ERR_SAVE_IO_FAIL;
1576 goto end;
1577 }
1578 }
1579
1580 /* /contexts */
1581 ret = config_writer_close_element(writer);
1582 if (ret) {
1583 ret = LTTNG_ERR_SAVE_IO_FAIL;
1584 goto end;
1585 }
55c9e7ca
JR
1586
1587 ret = LTTNG_OK;
fb198a11
JG
1588end:
1589 return ret;
1590}
1591
55c9e7ca 1592/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1593static
1594int save_kernel_channel(struct config_writer *writer,
1595 struct ltt_kernel_channel *kchan)
1596{
1597 int ret;
1598
1599 assert(writer);
1600 assert(kchan);
1601
1602 ret = config_writer_open_element(writer, config_element_channel);
1603 if (ret) {
1604 ret = LTTNG_ERR_SAVE_IO_FAIL;
1605 goto end;
1606 }
1607
1608 ret = config_writer_write_element_string(writer, config_element_name,
1609 kchan->channel->name);
1610 if (ret) {
1611 ret = LTTNG_ERR_SAVE_IO_FAIL;
1612 goto end;
1613 }
1614
1615 ret = config_writer_write_element_bool(writer, config_element_enabled,
1616 kchan->channel->enabled);
1617 if (ret) {
1618 ret = LTTNG_ERR_SAVE_IO_FAIL;
1619 goto end;
1620 }
1621
1622 ret = save_kernel_channel_attributes(writer, &kchan->channel->attr);
55c9e7ca 1623 if (ret != LTTNG_OK) {
fb198a11
JG
1624 goto end;
1625 }
1626
0de3eda1 1627 ret = save_kernel_events(writer, kchan);
55c9e7ca 1628 if (ret != LTTNG_OK) {
fb198a11
JG
1629 goto end;
1630 }
1631
645328ae 1632 ret = save_kernel_contexts(writer, kchan);
55c9e7ca 1633 if (ret != LTTNG_OK) {
fb198a11
JG
1634 goto end;
1635 }
1636
1637 /* /channel */
1638 ret = config_writer_close_element(writer);
1639 if (ret) {
1640 ret = LTTNG_ERR_SAVE_IO_FAIL;
1641 goto end;
1642 }
55c9e7ca
JR
1643
1644 ret = LTTNG_OK;
fb198a11
JG
1645end:
1646 return ret;
1647}
1648
55c9e7ca 1649/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1650static
1651int save_ust_channel(struct config_writer *writer,
1652 struct ltt_ust_channel *ust_chan,
1653 struct ltt_ust_session *session)
1654{
1655 int ret;
1656
1657 assert(writer);
1658 assert(ust_chan);
1659 assert(session);
1660
1661 ret = config_writer_open_element(writer, config_element_channel);
1662 if (ret) {
1663 ret = LTTNG_ERR_SAVE_IO_FAIL;
1664 goto end;
1665 }
1666
1667 ret = config_writer_write_element_string(writer, config_element_name,
1668 ust_chan->name);
1669 if (ret) {
1670 ret = LTTNG_ERR_SAVE_IO_FAIL;
1671 goto end;
1672 }
1673
1674 ret = config_writer_write_element_bool(writer, config_element_enabled,
1675 ust_chan->enabled);
1676 if (ret) {
1677 ret = LTTNG_ERR_SAVE_IO_FAIL;
1678 goto end;
1679 }
1680
1681 ret = save_ust_channel_attributes(writer, &ust_chan->attr);
55c9e7ca 1682 if (ret != LTTNG_OK) {
fb198a11
JG
1683 goto end;
1684 }
1685
1686 ret = config_writer_write_element_unsigned_int(writer,
1687 config_element_tracefile_size, ust_chan->tracefile_size);
1688 if (ret) {
1689 ret = LTTNG_ERR_SAVE_IO_FAIL;
1690 goto end;
1691 }
1692
1693 ret = config_writer_write_element_unsigned_int(writer,
1694 config_element_tracefile_count, ust_chan->tracefile_count);
1695 if (ret) {
1696 ret = LTTNG_ERR_SAVE_IO_FAIL;
1697 goto end;
1698 }
1699
1700 ret = config_writer_write_element_unsigned_int(writer,
1701 config_element_live_timer_interval,
1702 session->live_timer_interval);
1703 if (ret) {
1704 ret = LTTNG_ERR_SAVE_IO_FAIL;
1705 goto end;
1706 }
1707
51755dc8
JG
1708 if (ust_chan->domain == LTTNG_DOMAIN_UST) {
1709 ret = save_ust_events(writer, ust_chan->events);
55c9e7ca 1710 if (ret != LTTNG_OK) {
51755dc8
JG
1711 goto end;
1712 }
1713 } else {
1714 struct agent *agent = NULL;
1715
1716 agent = trace_ust_find_agent(session, ust_chan->domain);
1717 if (!agent) {
1718 ret = LTTNG_ERR_SAVE_IO_FAIL;
1719 ERR("Could not find agent associated to UST subdomain");
1720 goto end;
1721 }
1722
1723 /*
1724 * Channels associated with a UST sub-domain (such as JUL, Log4j
1725 * or Python) don't have any non-internal events. We retrieve
1726 * the "agent" events associated with this channel and serialize
1727 * them.
1728 */
8cd0a98d 1729 ret = save_agent_events(writer, agent);
55c9e7ca 1730 if (ret != LTTNG_OK) {
51755dc8
JG
1731 goto end;
1732 }
fb198a11
JG
1733 }
1734
1735 ret = save_ust_context(writer, &ust_chan->ctx_list);
55c9e7ca 1736 if (ret != LTTNG_OK) {
fb198a11
JG
1737 goto end;
1738 }
1739
1740 /* /channel */
1741 ret = config_writer_close_element(writer);
1742 if (ret) {
1743 ret = LTTNG_ERR_SAVE_IO_FAIL;
1744 goto end;
1745 }
55c9e7ca
JR
1746
1747 ret = LTTNG_OK;
fb198a11
JG
1748end:
1749 return ret;
1750}
1751
55c9e7ca 1752/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
1753static
1754int save_kernel_session(struct config_writer *writer,
1755 struct ltt_session *session)
1756{
1757 int ret;
1758 struct ltt_kernel_channel *kchan;
1759
1760 assert(writer);
1761 assert(session);
1762
1763 ret = config_writer_write_element_string(writer, config_element_type,
1764 config_domain_type_kernel);
1765 if (ret) {
1766 ret = LTTNG_ERR_SAVE_IO_FAIL;
1767 goto end;
1768 }
1769
1770 ret = config_writer_write_element_string(writer,
1771 config_element_buffer_type, config_buffer_type_global);
1772 if (ret) {
1773 ret = LTTNG_ERR_SAVE_IO_FAIL;
1774 goto end;
1775 }
1776
1777 ret = config_writer_open_element(writer,
1778 config_element_channels);
1779 if (ret) {
1780 ret = LTTNG_ERR_SAVE_IO_FAIL;
1781 goto end;
1782 }
1783
1784 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head,
1785 list) {
1786 ret = save_kernel_channel(writer, kchan);
55c9e7ca 1787 if (ret != LTTNG_OK) {
fb198a11
JG
1788 goto end;
1789 }
1790 }
1791
1792 /* /channels */
1793 ret = config_writer_close_element(writer);
1794 if (ret) {
1795 ret = LTTNG_ERR_SAVE_IO_FAIL;
1796 goto end;
1797 }
55c9e7ca
JR
1798
1799 ret = LTTNG_OK;
fb198a11
JG
1800end:
1801 return ret;
1802}
1803
1804static
51755dc8
JG
1805const char *get_config_domain_str(enum lttng_domain_type domain)
1806{
1807 const char *str_dom;
1808
1809 switch (domain) {
1810 case LTTNG_DOMAIN_KERNEL:
1811 str_dom = config_domain_type_kernel;
1812 break;
1813 case LTTNG_DOMAIN_UST:
1814 str_dom = config_domain_type_ust;
1815 break;
1816 case LTTNG_DOMAIN_JUL:
1817 str_dom = config_domain_type_jul;
1818 break;
1819 case LTTNG_DOMAIN_LOG4J:
1820 str_dom = config_domain_type_log4j;
1821 break;
1822 case LTTNG_DOMAIN_PYTHON:
1823 str_dom = config_domain_type_python;
1824 break;
1825 default:
1826 assert(0);
1827 }
1828
1829 return str_dom;
1830}
1831
55c9e7ca
JR
1832/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
1833static int save_id_tracker(struct config_writer *writer,
1834 struct ltt_session *sess,
1835 int domain,
1836 enum lttng_tracker_type tracker_type)
e8fcabef 1837{
55c9e7ca 1838 int ret = LTTNG_OK;
e283e4a0 1839 unsigned int nr_ids, i;
a7a533cd 1840 struct lttng_tracker_ids *ids = NULL;
55c9e7ca 1841 const char *element_id_tracker, *element_target_id, *element_id;
a7a533cd 1842 const struct lttng_tracker_id *id;
2d97a006
JR
1843 enum lttng_tracker_id_status status;
1844 int value;
1845 const char *string;
55c9e7ca
JR
1846
1847 switch (tracker_type) {
1848 case LTTNG_TRACKER_PID:
1849 element_id_tracker = config_element_pid_tracker;
1850 element_target_id = config_element_target_pid;
1851 element_id = config_element_pid;
1852 break;
1853 case LTTNG_TRACKER_VPID:
1854 element_id_tracker = config_element_vpid_tracker;
1855 element_target_id = config_element_target_vpid;
1856 element_id = config_element_id;
1857 break;
1858 case LTTNG_TRACKER_UID:
1859 element_id_tracker = config_element_uid_tracker;
1860 element_target_id = config_element_target_uid;
1861 element_id = config_element_id;
1862 break;
1863 case LTTNG_TRACKER_VUID:
1864 element_id_tracker = config_element_vuid_tracker;
1865 element_target_id = config_element_target_vuid;
1866 element_id = config_element_id;
1867 break;
1868 case LTTNG_TRACKER_GID:
1869 element_id_tracker = config_element_gid_tracker;
1870 element_target_id = config_element_target_gid;
1871 element_id = config_element_id;
1872 break;
1873 case LTTNG_TRACKER_VGID:
1874 element_id_tracker = config_element_vgid_tracker;
1875 element_target_id = config_element_target_vgid;
1876 element_id = config_element_id;
1877 break;
1878 default:
1879 ret = LTTNG_ERR_SAVE_IO_FAIL;
1880 goto end;
1881 }
e8fcabef
JG
1882
1883 switch (domain) {
1884 case LTTNG_DOMAIN_KERNEL:
1885 {
a7a533cd 1886 ret = kernel_list_tracker_ids(
55c9e7ca 1887 tracker_type, sess->kernel_session, &ids);
a7a533cd 1888 if (ret != LTTNG_OK) {
e8fcabef
JG
1889 ret = LTTNG_ERR_KERN_LIST_FAIL;
1890 goto end;
1891 }
1892 break;
1893 }
1894 case LTTNG_DOMAIN_UST:
1895 {
a7a533cd 1896 ret = trace_ust_list_tracker_ids(
55c9e7ca 1897 tracker_type, sess->ust_session, &ids);
a7a533cd 1898 if (ret != LTTNG_OK) {
e8fcabef
JG
1899 ret = LTTNG_ERR_UST_LIST_FAIL;
1900 goto end;
1901 }
1902 break;
1903 }
1904 case LTTNG_DOMAIN_JUL:
1905 case LTTNG_DOMAIN_LOG4J:
1906 case LTTNG_DOMAIN_PYTHON:
1907 default:
1908 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1909 goto end;
1910 }
1911
e283e4a0
JR
1912 status = lttng_tracker_ids_get_count(ids, &nr_ids);
1913 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
1914 ret = LTTNG_ERR_INVALID;
1915 goto end;
1916 }
a7a533cd
JR
1917
1918 if (nr_ids == 1) {
1919 id = lttng_tracker_ids_get_at_index(ids, 0);
1920 if (id && lttng_tracker_id_get_type(id) == LTTNG_ID_ALL) {
1921 /* Tracking all, nothing to output. */
1922 ret = LTTNG_OK;
1923 goto end;
1924 }
55c9e7ca
JR
1925 }
1926
1927 ret = config_writer_open_element(writer, element_id_tracker);
1928 if (ret) {
1929 ret = LTTNG_ERR_SAVE_IO_FAIL;
1930 goto end;
1931 }
1932
1933 ret = config_writer_open_element(writer, config_element_targets);
1934 if (ret) {
1935 ret = LTTNG_ERR_SAVE_IO_FAIL;
1936 goto end;
1937 }
1938
1939 if (nr_ids == 0) {
1940 /* Tracking none: empty list. */
1941 ret = config_writer_open_element(writer, element_target_id);
e8fcabef
JG
1942 if (ret) {
1943 ret = LTTNG_ERR_SAVE_IO_FAIL;
1944 goto end;
1945 }
1946
55c9e7ca
JR
1947 /* /$element_target_id */
1948 ret = config_writer_close_element(writer);
e8fcabef
JG
1949 if (ret) {
1950 ret = LTTNG_ERR_SAVE_IO_FAIL;
1951 goto end;
1952 }
55c9e7ca
JR
1953 } else {
1954 /* Tracking list. */
1955 for (i = 0; i < nr_ids; i++) {
a7a533cd
JR
1956 id = lttng_tracker_ids_get_at_index(ids, i);
1957 if (!id) {
1958 ret = LTTNG_ERR_SAVE_IO_FAIL;
1959 goto end;
1960 }
2d97a006 1961 switch (lttng_tracker_id_get_type(id)) {
55c9e7ca
JR
1962 case LTTNG_ID_VALUE:
1963 ret = config_writer_open_element(
1964 writer, element_target_id);
1965 if (ret) {
1966 ret = LTTNG_ERR_SAVE_IO_FAIL;
1967 goto end;
1968 }
2d97a006 1969 status = lttng_tracker_id_get_value(id, &value);
55c9e7ca 1970 ret = config_writer_write_element_unsigned_int(
2d97a006 1971 writer, element_id, value);
55c9e7ca
JR
1972 break;
1973 case LTTNG_ID_STRING:
1974 ret = config_writer_open_element(
1975 writer, element_target_id);
1976 if (ret) {
1977 ret = LTTNG_ERR_SAVE_IO_FAIL;
1978 goto end;
1979 }
2d97a006
JR
1980 status = lttng_tracker_id_get_string(
1981 id, &string);
55c9e7ca 1982 ret = config_writer_write_element_string(writer,
2d97a006 1983 config_element_name, string);
55c9e7ca
JR
1984 break;
1985 default:
1986 /* Unexpected. */
e8fcabef
JG
1987 ret = LTTNG_ERR_SAVE_IO_FAIL;
1988 goto end;
1989 }
e8fcabef
JG
1990 if (ret) {
1991 ret = LTTNG_ERR_SAVE_IO_FAIL;
1992 goto end;
1993 }
2d97a006
JR
1994 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
1995 ret = LTTNG_ERR_SAVE_IO_FAIL;
1996 goto end;
1997 }
e8fcabef 1998
55c9e7ca 1999 /* /$element_target_id */
e8fcabef
JG
2000 ret = config_writer_close_element(writer);
2001 if (ret) {
2002 ret = LTTNG_ERR_SAVE_IO_FAIL;
2003 goto end;
2004 }
2005 }
55c9e7ca 2006 }
e8fcabef 2007
55c9e7ca
JR
2008 /* /targets */
2009 ret = config_writer_close_element(writer);
2010 if (ret) {
2011 ret = LTTNG_ERR_SAVE_IO_FAIL;
2012 goto end;
2013 }
e8fcabef 2014
55c9e7ca
JR
2015 /* /$element_id_tracker */
2016 ret = config_writer_close_element(writer);
2017 if (ret) {
2018 ret = LTTNG_ERR_SAVE_IO_FAIL;
2019 goto end;
e8fcabef 2020 }
55c9e7ca
JR
2021
2022 ret = LTTNG_OK;
e8fcabef 2023end:
a7a533cd 2024 lttng_tracker_ids_destroy(ids);
e8fcabef
JG
2025 return ret;
2026}
2027
55c9e7ca
JR
2028/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
2029static int save_id_trackers(struct config_writer *writer,
2030 struct ltt_session *sess,
2031 int domain)
2032{
2033 int ret;
2034
2035 switch (domain) {
2036 case LTTNG_DOMAIN_KERNEL:
2037 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_PID);
2038 if (ret != LTTNG_OK)
2039 return ret;
2040 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_VPID);
2041 if (ret != LTTNG_OK)
2042 return ret;
2043 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_UID);
2044 if (ret != LTTNG_OK)
2045 return ret;
2046 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_VUID);
2047 if (ret != LTTNG_OK)
2048 return ret;
2049 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_GID);
2050 if (ret != LTTNG_OK)
2051 return ret;
2052 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_VGID);
2053 if (ret != LTTNG_OK)
2054 return ret;
2055 break;
2056 case LTTNG_DOMAIN_UST:
2057 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_VPID);
2058 if (ret != LTTNG_OK)
2059 return ret;
2060 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_VUID);
2061 if (ret != LTTNG_OK)
2062 return ret;
2063 ret = save_id_tracker(writer, sess, domain, LTTNG_TRACKER_VGID);
2064 if (ret != LTTNG_OK)
2065 return ret;
2066 break;
2067 default:
2068 return LTTNG_ERR_INVALID;
2069 }
2070 return LTTNG_OK;
2071}
2072
2073/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
51755dc8
JG
2074static
2075int save_ust_domain(struct config_writer *writer,
2076 struct ltt_session *session, enum lttng_domain_type domain)
fb198a11
JG
2077{
2078 int ret;
2079 struct ltt_ust_channel *ust_chan;
2080 const char *buffer_type_string;
2081 struct lttng_ht_node_str *node;
2082 struct lttng_ht_iter iter;
51755dc8 2083 const char *config_domain_name;
fb198a11
JG
2084
2085 assert(writer);
2086 assert(session);
2087
51755dc8
JG
2088 ret = config_writer_open_element(writer,
2089 config_element_domain);
2090 if (ret) {
2091 ret = LTTNG_ERR_SAVE_IO_FAIL;
2092 goto end;
2093 }
2094
2095 config_domain_name = get_config_domain_str(domain);
2096 if (!config_domain_name) {
2097 ret = LTTNG_ERR_INVALID;
2098 goto end;
2099 }
2100
2101 ret = config_writer_write_element_string(writer,
2102 config_element_type, config_domain_name);
fb198a11
JG
2103 if (ret) {
2104 ret = LTTNG_ERR_SAVE_IO_FAIL;
2105 goto end;
2106 }
2107
2108 buffer_type_string = get_buffer_type_string(
2109 session->ust_session->buffer_type);
2110 if (!buffer_type_string) {
2111 ERR("Unsupported buffer type.");
2112 ret = LTTNG_ERR_INVALID;
2113 goto end;
2114 }
2115
2116 ret = config_writer_write_element_string(writer,
2117 config_element_buffer_type, buffer_type_string);
2118 if (ret) {
2119 ret = LTTNG_ERR_SAVE_IO_FAIL;
2120 goto end;
2121 }
2122
2123 ret = config_writer_open_element(writer, config_element_channels);
2124 if (ret) {
2125 ret = LTTNG_ERR_SAVE_IO_FAIL;
2126 goto end;
2127 }
2128
2129 rcu_read_lock();
2130 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
2131 &iter.iter, node, node) {
fb198a11 2132 ust_chan = caa_container_of(node, struct ltt_ust_channel, node);
51755dc8 2133 if (domain == ust_chan->domain) {
fb198a11 2134 ret = save_ust_channel(writer, ust_chan, session->ust_session);
55c9e7ca 2135 if (ret != LTTNG_OK) {
fb198a11
JG
2136 rcu_read_unlock();
2137 goto end;
2138 }
2139 }
2140 }
2141 rcu_read_unlock();
2142
2143 /* /channels */
2144 ret = config_writer_close_element(writer);
2145 if (ret) {
2146 ret = LTTNG_ERR_SAVE_IO_FAIL;
2147 goto end;
2148 }
51755dc8 2149
e8fcabef 2150 if (domain == LTTNG_DOMAIN_UST) {
847a5916 2151 ret = config_writer_open_element(writer,
e8fcabef 2152 config_element_trackers);
847a5916
JR
2153 if (ret) {
2154 ret = LTTNG_ERR_SAVE_IO_FAIL;
2155 goto end;
2156 }
2157
55c9e7ca
JR
2158 ret = save_id_trackers(writer, session, LTTNG_DOMAIN_UST);
2159 if (ret != LTTNG_OK) {
847a5916
JR
2160 goto end;
2161 }
2162
e8fcabef 2163 /* /trackers */
847a5916
JR
2164 ret = config_writer_close_element(writer);
2165 if (ret) {
55c9e7ca 2166 ret = LTTNG_ERR_SAVE_IO_FAIL;
847a5916
JR
2167 goto end;
2168 }
e8fcabef 2169 }
847a5916 2170
e8fcabef
JG
2171 /* /domain */
2172 ret = config_writer_close_element(writer);
2173 if (ret) {
2174 ret = LTTNG_ERR_SAVE_IO_FAIL;
2175 goto end;
847a5916 2176 }
e8fcabef 2177
55c9e7ca 2178 ret = LTTNG_OK;
847a5916 2179end:
847a5916
JR
2180 return ret;
2181}
2182
55c9e7ca 2183/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
2184static
2185int save_domains(struct config_writer *writer, struct ltt_session *session)
2186{
55c9e7ca 2187 int ret = LTTNG_OK;
fb198a11
JG
2188
2189 assert(writer);
2190 assert(session);
2191
2192 if (!session->kernel_session && !session->ust_session) {
2193 goto end;
2194 }
2195
2196 ret = config_writer_open_element(writer, config_element_domains);
2197 if (ret) {
2198 ret = LTTNG_ERR_SAVE_IO_FAIL;
2199 goto end;
2200 }
2201
fb198a11
JG
2202 if (session->kernel_session) {
2203 ret = config_writer_open_element(writer,
2204 config_element_domain);
2205 if (ret) {
2206 ret = LTTNG_ERR_SAVE_IO_FAIL;
2207 goto end;
2208 }
2209
2210 ret = save_kernel_session(writer, session);
55c9e7ca 2211 if (ret != LTTNG_OK) {
fb198a11
JG
2212 goto end;
2213 }
2214
847a5916
JR
2215 ret = config_writer_open_element(writer,
2216 config_element_trackers);
2217 if (ret) {
2218 ret = LTTNG_ERR_SAVE_IO_FAIL;
2219 goto end;
2220 }
2221
55c9e7ca
JR
2222 ret = save_id_trackers(writer, session, LTTNG_DOMAIN_KERNEL);
2223 if (ret != LTTNG_OK) {
847a5916
JR
2224 goto end;
2225 }
2226
2227 /* /trackers */
2228 ret = config_writer_close_element(writer);
2229 if (ret) {
2230 ret = LTTNG_ERR_SAVE_IO_FAIL;
2231 goto end;
2232 }
fb198a11
JG
2233 /* /domain */
2234 ret = config_writer_close_element(writer);
2235 if (ret) {
2236 ret = LTTNG_ERR_SAVE_IO_FAIL;
2237 goto end;
2238 }
2239 }
2240
2241 if (session->ust_session) {
51755dc8 2242 ret = save_ust_domain(writer, session, LTTNG_DOMAIN_UST);
55c9e7ca 2243 if (ret != LTTNG_OK) {
fb198a11
JG
2244 goto end;
2245 }
2246
51755dc8 2247 ret = save_ust_domain(writer, session, LTTNG_DOMAIN_JUL);
55c9e7ca 2248 if (ret != LTTNG_OK) {
fb198a11
JG
2249 goto end;
2250 }
fb198a11 2251
51755dc8 2252 ret = save_ust_domain(writer, session, LTTNG_DOMAIN_LOG4J);
55c9e7ca 2253 if (ret != LTTNG_OK) {
51755dc8
JG
2254 goto end;
2255 }
65d72c41 2256
51755dc8 2257 ret = save_ust_domain(writer, session, LTTNG_DOMAIN_PYTHON);
55c9e7ca 2258 if (ret != LTTNG_OK) {
51755dc8 2259 goto end;
fb198a11
JG
2260 }
2261 }
2262
2263 /* /domains */
2264 ret = config_writer_close_element(writer);
2265 if (ret) {
2266 ret = LTTNG_ERR_SAVE_IO_FAIL;
2267 goto end;
2268 }
55c9e7ca
JR
2269
2270 ret = LTTNG_OK;
fb198a11
JG
2271end:
2272 return ret;
2273}
2274
55c9e7ca 2275/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
2276static
2277int save_consumer_output(struct config_writer *writer,
2278 struct consumer_output *output)
2279{
2280 int ret;
2281
2282 assert(writer);
2283 assert(output);
2284
2285 ret = config_writer_open_element(writer, config_element_consumer_output);
2286 if (ret) {
2287 ret = LTTNG_ERR_SAVE_IO_FAIL;
2288 goto end;
2289 }
2290
2291 ret = config_writer_write_element_bool(writer, config_element_enabled,
2292 output->enabled);
2293 if (ret) {
2294 ret = LTTNG_ERR_SAVE_IO_FAIL;
2295 goto end;
2296 }
2297
2298 ret = config_writer_open_element(writer, config_element_destination);
2299 if (ret) {
2300 ret = LTTNG_ERR_SAVE_IO_FAIL;
2301 goto end;
2302 }
2303
2304 switch (output->type) {
2305 case CONSUMER_DST_LOCAL:
2306 ret = config_writer_write_element_string(writer,
366a9222 2307 config_element_path, output->dst.session_root_path);
fb198a11
JG
2308 if (ret) {
2309 ret = LTTNG_ERR_SAVE_IO_FAIL;
2310 goto end;
2311 }
2312 break;
2313 case CONSUMER_DST_NET:
2314 {
2315 char *uri;
2316
2317 uri = zmalloc(PATH_MAX);
2318 if (!uri) {
2319 ret = LTTNG_ERR_NOMEM;
2320 goto end;
2321 }
2322
2323 ret = config_writer_open_element(writer, config_element_net_output);
2324 if (ret) {
2325 ret = LTTNG_ERR_SAVE_IO_FAIL;
2326 goto end_net_output;
2327 }
2328
2329 if (output->dst.net.control_isset &&
2330 output->dst.net.data_isset) {
2331 ret = uri_to_str_url(&output->dst.net.control, uri, PATH_MAX);
2332 if (ret < 0) {
2333 ret = LTTNG_ERR_INVALID;
2334 goto end_net_output;
2335 }
2336
2337 ret = config_writer_write_element_string(writer,
2338 config_element_control_uri, uri);
2339 if (ret) {
2340 ret = LTTNG_ERR_SAVE_IO_FAIL;
2341 goto end_net_output;
2342 }
2343
2344 ret = uri_to_str_url(&output->dst.net.data, uri, PATH_MAX);
2345 if (ret < 0) {
2346 ret = LTTNG_ERR_INVALID;
2347 goto end_net_output;
2348 }
2349
2350 ret = config_writer_write_element_string(writer,
2351 config_element_data_uri, uri);
2352 if (ret) {
2353 ret = LTTNG_ERR_SAVE_IO_FAIL;
2354 goto end_net_output;
2355 }
55c9e7ca 2356 ret = LTTNG_OK;
fb198a11
JG
2357end_net_output:
2358 free(uri);
55c9e7ca 2359 if (ret != LTTNG_OK) {
fb198a11
JG
2360 goto end;
2361 }
2362 } else {
2363 ret = !output->dst.net.control_isset ?
2364 LTTNG_ERR_URL_CTRL_MISS :
2365 LTTNG_ERR_URL_DATA_MISS;
c39270e5 2366 free(uri);
fb198a11
JG
2367 goto end;
2368 }
2369
2370 ret = config_writer_close_element(writer);
2371 if (ret) {
2372 ret = LTTNG_ERR_SAVE_IO_FAIL;
2373 goto end;
2374 }
2375 break;
2376 }
2377 default:
2378 ERR("Unsupported consumer output type.");
2379 ret = LTTNG_ERR_INVALID;
2380 goto end;
2381 }
2382
2383 /* /destination */
2384 ret = config_writer_close_element(writer);
2385 if (ret) {
2386 ret = LTTNG_ERR_SAVE_IO_FAIL;
2387 goto end;
2388 }
2389
2390 /* /consumer_output */
2391 ret = config_writer_close_element(writer);
2392 if (ret) {
2393 ret = LTTNG_ERR_SAVE_IO_FAIL;
2394 goto end;
2395 }
55c9e7ca
JR
2396
2397 ret = LTTNG_OK;
fb198a11
JG
2398end:
2399 return ret;
2400}
2401
55c9e7ca 2402/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
2403static
2404int save_snapshot_outputs(struct config_writer *writer,
2405 struct snapshot *snapshot)
2406{
2407 int ret;
2408 struct lttng_ht_iter iter;
2409 struct snapshot_output *output;
2410
2411 assert(writer);
2412 assert(snapshot);
2413
2414 ret = config_writer_open_element(writer, config_element_snapshot_outputs);
2415 if (ret) {
2416 ret = LTTNG_ERR_SAVE_IO_FAIL;
2417 goto end;
2418 }
2419
2420 rcu_read_lock();
2421 cds_lfht_for_each_entry(snapshot->output_ht->ht, &iter.iter, output,
2422 node.node) {
2423 ret = config_writer_open_element(writer,
2424 config_element_output);
2425 if (ret) {
2426 ret = LTTNG_ERR_SAVE_IO_FAIL;
2427 goto end_unlock;
2428 }
2429
2430 ret = config_writer_write_element_string(writer,
2431 config_element_name, output->name);
2432 if (ret) {
2433 ret = LTTNG_ERR_SAVE_IO_FAIL;
2434 goto end_unlock;
2435 }
2436
2437 ret = config_writer_write_element_unsigned_int(writer,
2438 config_element_max_size, output->max_size);
2439 if (ret) {
2440 ret = LTTNG_ERR_SAVE_IO_FAIL;
2441 goto end_unlock;
2442 }
2443
2444 ret = save_consumer_output(writer, output->consumer);
55c9e7ca 2445 if (ret != LTTNG_OK) {
fb198a11
JG
2446 goto end_unlock;
2447 }
2448
2449 /* /output */
2450 ret = config_writer_close_element(writer);
2451 if (ret) {
2452 ret = LTTNG_ERR_SAVE_IO_FAIL;
2453 goto end_unlock;
2454 }
2455 }
2456 rcu_read_unlock();
2457
2458 /* /snapshot_outputs */
2459 ret = config_writer_close_element(writer);
2460 if (ret) {
2461 ret = LTTNG_ERR_SAVE_IO_FAIL;
2462 goto end;
2463 }
2464
55c9e7ca 2465 ret = LTTNG_OK;
fb198a11
JG
2466end:
2467 return ret;
2468end_unlock:
2469 rcu_read_unlock();
2470 return ret;
2471}
2472
55c9e7ca 2473/* Return LTTNG_OK on success else a LTTNG_ERR* code. */
fb198a11
JG
2474static
2475int save_session_output(struct config_writer *writer,
2476 struct ltt_session *session)
2477{
2478 int ret;
2479
2480 assert(writer);
2481 assert(session);
2482
2483 if ((session->snapshot_mode && session->snapshot.nb_output == 0) ||
2484 (!session->snapshot_mode && !session->consumer)) {
2485 /* Session is in no output mode */
55c9e7ca 2486 ret = LTTNG_OK;
fb198a11
JG
2487 goto end;
2488 }
2489
2490 ret = config_writer_open_element(writer, config_element_output);
2491 if (ret) {
2492 ret = LTTNG_ERR_SAVE_IO_FAIL;
2493 goto end;
2494 }
2495
2496 if (session->snapshot_mode) {
2497 ret = save_snapshot_outputs(writer, &session->snapshot);
55c9e7ca 2498 if (ret != LTTNG_OK) {
fb198a11
JG
2499 goto end;
2500 }
2501 } else {
2502 if (session->consumer) {
2503 ret = save_consumer_output(writer, session->consumer);
55c9e7ca 2504 if (ret != LTTNG_OK) {
fb198a11
JG
2505 goto end;
2506 }
2507 }
2508 }
2509
2510 /* /output */
2511 ret = config_writer_close_element(writer);
2512 if (ret) {
2513 ret = LTTNG_ERR_SAVE_IO_FAIL;
2514 goto end;
2515 }
55c9e7ca 2516 ret = LTTNG_OK;
fb198a11
JG
2517end:
2518 return ret;
2519}
2520
ce6176f2
JG
2521static
2522int save_session_rotation_schedule(struct config_writer *writer,
2523 enum lttng_rotation_schedule_type type, uint64_t value)
2524{
2525 int ret = 0;
2526 const char *element_name;
2527 const char *value_name;
2528
2529 switch (type) {
2530 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
2531 element_name = config_element_rotation_schedule_periodic;
2532 value_name = config_element_rotation_schedule_periodic_time_us;
2533 break;
2534 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
2535 element_name = config_element_rotation_schedule_size_threshold;
2536 value_name = config_element_rotation_schedule_size_threshold_bytes;
2537 break;
2538 default:
2539 ret = -1;
2540 goto end;
2541 }
2542
2543 ret = config_writer_open_element(writer, element_name);
2544 if (ret) {
2545 goto end;
2546 }
2547
2548 ret = config_writer_write_element_unsigned_int(writer,
2549 value_name, value);
2550 if (ret) {
2551 goto end;
2552 }
2553
2554 /* Close schedule descriptor element. */
2555 ret = config_writer_close_element(writer);
2556 if (ret) {
2557 goto end;
2558 }
2559end:
2560 return ret;
2561}
2562
2563static
2564int save_session_rotation_schedules(struct config_writer *writer,
2565 struct ltt_session *session)
2566{
2567 int ret;
2568
2569 ret = config_writer_open_element(writer,
2570 config_element_rotation_schedules);
f829d17a
JG
2571 if (ret) {
2572 goto end;
2573 }
ce6176f2
JG
2574 if (session->rotate_timer_period) {
2575 ret = save_session_rotation_schedule(writer,
2576 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC,
2577 session->rotate_timer_period);
2578 if (ret) {
2579 goto close_schedules;
2580 }
2581 }
2582 if (session->rotate_size) {
2583 ret = save_session_rotation_schedule(writer,
2584 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD,
2585 session->rotate_size);
2586 if (ret) {
2587 goto close_schedules;
2588 }
2589 }
2590
2591close_schedules:
2592 /* Close rotation schedules element. */
2593 ret = config_writer_close_element(writer);
2594 if (ret) {
2595 goto end;
2596 }
2597end:
2598 return ret;
2599}
2600
fb198a11
JG
2601/*
2602 * Save the given session.
2603 *
55c9e7ca 2604 * Return LTTNG_OK on success else a LTTNG_ERR* code.
fb198a11
JG
2605 */
2606static
2607int save_session(struct ltt_session *session,
2608 struct lttng_save_session_attr *attr, lttng_sock_cred *creds)
2609{
b45f9ad2 2610 int ret, fd = -1;
511653c3 2611 char config_file_path[LTTNG_PATH_MAX];
fb198a11
JG
2612 size_t len;
2613 struct config_writer *writer = NULL;
2614 size_t session_name_len;
2615 const char *provided_path;
f376ad9c 2616 int file_open_flags = O_CREAT | O_WRONLY | O_TRUNC;
fb198a11
JG
2617
2618 assert(session);
2619 assert(attr);
2620 assert(creds);
2621
2622 session_name_len = strlen(session->name);
95a29ab8 2623 memset(config_file_path, 0, sizeof(config_file_path));
fb198a11
JG
2624
2625 if (!session_access_ok(session,
2626 LTTNG_SOCK_GET_UID_CRED(creds),
e32d7f27 2627 LTTNG_SOCK_GET_GID_CRED(creds)) || session->destroyed) {
fb198a11
JG
2628 ret = LTTNG_ERR_EPERM;
2629 goto end;
2630 }
2631
2632 provided_path = lttng_save_session_attr_get_output_url(attr);
2633 if (provided_path) {
95a29ab8 2634 DBG3("Save session in provided path %s", provided_path);
fb198a11 2635 len = strlen(provided_path);
d2992717 2636 if (len >= sizeof(config_file_path)) {
fb198a11
JG
2637 ret = LTTNG_ERR_SET_URL;
2638 goto end;
2639 }
511653c3 2640 strncpy(config_file_path, provided_path, sizeof(config_file_path));
fb198a11 2641 } else {
7e078ad1 2642 ssize_t ret_len;
fb198a11
JG
2643 char *home_dir = utils_get_user_home_dir(
2644 LTTNG_SOCK_GET_UID_CRED(creds));
2645 if (!home_dir) {
2646 ret = LTTNG_ERR_SET_URL;
2647 goto end;
2648 }
2649
d2992717 2650 ret_len = snprintf(config_file_path, sizeof(config_file_path),
fb198a11
JG
2651 DEFAULT_SESSION_HOME_CONFIGPATH, home_dir);
2652 free(home_dir);
7e078ad1 2653 if (ret_len < 0) {
fb198a11
JG
2654 PERROR("snprintf save session");
2655 ret = LTTNG_ERR_SET_URL;
2656 goto end;
2657 }
7e078ad1 2658 len = ret_len;
fb198a11
JG
2659 }
2660
2661 /*
d2992717
DG
2662 * Check the path fits in the config file path dst including the '/'
2663 * followed by trailing .lttng extension and the NULL terminated string.
fb198a11 2664 */
d2992717
DG
2665 if ((len + session_name_len + 2 +
2666 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION))
2667 > sizeof(config_file_path)) {
fb198a11
JG
2668 ret = LTTNG_ERR_SET_URL;
2669 goto end;
2670 }
2671
2672 ret = run_as_mkdir_recursive(config_file_path, S_IRWXU | S_IRWXG,
2673 LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds));
2674 if (ret) {
2675 ret = LTTNG_ERR_SET_URL;
2676 goto end;
2677 }
2678
d2992717
DG
2679 /*
2680 * At this point, we know that everything fits in the buffer. Validation
2681 * was done just above.
2682 */
fb198a11 2683 config_file_path[len++] = '/';
511653c3 2684 strncpy(config_file_path + len, session->name, sizeof(config_file_path) - len);
fb198a11
JG
2685 len += session_name_len;
2686 strcpy(config_file_path + len, DEFAULT_SESSION_CONFIG_FILE_EXTENSION);
95a29ab8
DG
2687 len += sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION);
2688 config_file_path[len] = '\0';
fb198a11 2689
f376ad9c
JG
2690 if (!attr->overwrite) {
2691 file_open_flags |= O_EXCL;
fb198a11
JG
2692 }
2693
f376ad9c 2694 fd = run_as_open(config_file_path, file_open_flags,
fb198a11
JG
2695 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2696 LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds));
2697 if (fd < 0) {
2698 PERROR("Could not create configuration file");
f376ad9c
JG
2699 switch (errno) {
2700 case EEXIST:
2701 ret = LTTNG_ERR_SAVE_FILE_EXIST;
2702 break;
2703 case EACCES:
2704 ret = LTTNG_ERR_EPERM;
2705 break;
2706 default:
2707 ret = LTTNG_ERR_SAVE_IO_FAIL;
2708 break;
2709 }
fb198a11
JG
2710 goto end;
2711 }
2712
705bb62f 2713 writer = config_writer_create(fd, 1);
fb198a11
JG
2714 if (!writer) {
2715 ret = LTTNG_ERR_NOMEM;
2716 goto end;
2717 }
2718
2719 ret = config_writer_open_element(writer, config_element_sessions);
2720 if (ret) {
2721 ret = LTTNG_ERR_SAVE_IO_FAIL;
2722 goto end;
2723 }
2724
2725 ret = config_writer_open_element(writer, config_element_session);
2726 if (ret) {
2727 ret = LTTNG_ERR_SAVE_IO_FAIL;
2728 goto end;
2729 }
2730
2731 ret = config_writer_write_element_string(writer, config_element_name,
2732 session->name);
2733 if (ret) {
2734 ret = LTTNG_ERR_SAVE_IO_FAIL;
2735 goto end;
2736 }
2737
55c9e7ca 2738 if (session->shm_path[0] != '\0') {
9e7c9f56
JR
2739 ret = config_writer_write_element_string(writer,
2740 config_element_shared_memory_path,
2741 session->shm_path);
2742 if (ret) {
2743 ret = LTTNG_ERR_SAVE_IO_FAIL;
2744 goto end;
2745 }
2746 }
2747
fb198a11 2748 ret = save_domains(writer, session);
55c9e7ca 2749 if (ret != LTTNG_OK) {
fb198a11
JG
2750 goto end;
2751 }
2752
2753 ret = config_writer_write_element_bool(writer, config_element_started,
8382cf6f 2754 session->active);
fb198a11
JG
2755 if (ret) {
2756 ret = LTTNG_ERR_SAVE_IO_FAIL;
2757 goto end;
2758 }
2759
329f3443
JD
2760 if (session->snapshot_mode || session->live_timer ||
2761 session->rotate_timer_period || session->rotate_size) {
fb198a11
JG
2762 ret = config_writer_open_element(writer, config_element_attributes);
2763 if (ret) {
2764 ret = LTTNG_ERR_SAVE_IO_FAIL;
2765 goto end;
2766 }
2767
2768 if (session->snapshot_mode) {
2769 ret = config_writer_write_element_bool(writer,
2770 config_element_snapshot_mode, 1);
2771 if (ret) {
2772 ret = LTTNG_ERR_SAVE_IO_FAIL;
2773 goto end;
2774 }
329f3443 2775 } else if (session->live_timer) {
d98ad589 2776 ret = config_writer_write_element_unsigned_int(writer,
fb198a11
JG
2777 config_element_live_timer_interval, session->live_timer);
2778 if (ret) {
2779 ret = LTTNG_ERR_SAVE_IO_FAIL;
2780 goto end;
2781 }
2782 }
ce6176f2
JG
2783 if (session->rotate_timer_period || session->rotate_size) {
2784 ret = save_session_rotation_schedules(writer,
2785 session);
329f3443
JD
2786 if (ret) {
2787 ret = LTTNG_ERR_SAVE_IO_FAIL;
2788 goto end;
2789 }
2790 }
fb198a11
JG
2791
2792 /* /attributes */
2793 ret = config_writer_close_element(writer);
2794 if (ret) {
2795 ret = LTTNG_ERR_SAVE_IO_FAIL;
2796 goto end;
2797 }
2798 }
2799
2800 ret = save_session_output(writer, session);
55c9e7ca 2801 if (ret != LTTNG_OK) {
fb198a11
JG
2802 goto end;
2803 }
2804
2805 /* /session */
2806 ret = config_writer_close_element(writer);
2807 if (ret) {
2808 ret = LTTNG_ERR_SAVE_IO_FAIL;
2809 goto end;
2810 }
2811
2812 /* /sessions */
2813 ret = config_writer_close_element(writer);
2814 if (ret) {
2815 ret = LTTNG_ERR_SAVE_IO_FAIL;
2816 goto end;
2817 }
55c9e7ca
JR
2818
2819 ret = LTTNG_OK;
fb198a11
JG
2820end:
2821 if (writer && config_writer_destroy(writer)) {
2822 /* Preserve the original error code */
55c9e7ca 2823 ret = ret != LTTNG_OK ? ret : LTTNG_ERR_SAVE_IO_FAIL;
fb198a11 2824 }
55c9e7ca 2825 if (ret != LTTNG_OK) {
fb198a11 2826 /* Delete file in case of error */
b45f9ad2 2827 if ((fd >= 0) && unlink(config_file_path)) {
fb198a11
JG
2828 PERROR("Unlinking XML session configuration.");
2829 }
2830 }
2831
b45f9ad2 2832 if (fd >= 0) {
55c9e7ca
JR
2833 int closeret;
2834
2835 closeret = close(fd);
2836 if (closeret) {
1d12100d
JR
2837 PERROR("Closing XML session configuration");
2838 }
2839 }
2840
fb198a11
JG
2841 return ret;
2842}
2843
2844int cmd_save_sessions(struct lttng_save_session_attr *attr,
2845 lttng_sock_cred *creds)
2846{
2847 int ret;
2848 const char *session_name;
2849 struct ltt_session *session;
2850
2851 session_lock_list();
2852
2853 session_name = lttng_save_session_attr_get_session_name(attr);
2854 if (session_name) {
2855 session = session_find_by_name(session_name);
2856 if (!session) {
2857 ret = LTTNG_ERR_SESS_NOT_FOUND;
2858 goto end;
2859 }
2860
2861 session_lock(session);
2862 ret = save_session(session, attr, creds);
2863 session_unlock(session);
e32d7f27 2864 session_put(session);
55c9e7ca 2865 if (ret != LTTNG_OK) {
fb198a11
JG
2866 goto end;
2867 }
2868 } else {
2869 struct ltt_session_list *list = session_get_list();
2870
2871 cds_list_for_each_entry(session, &list->head, list) {
e32d7f27
JG
2872 if (!session_get(session)) {
2873 continue;
2874 }
fb198a11
JG
2875 session_lock(session);
2876 ret = save_session(session, attr, creds);
2877 session_unlock(session);
e32d7f27 2878 session_put(session);
fb198a11 2879 /* Don't abort if we don't have the required permissions. */
55c9e7ca 2880 if (ret != LTTNG_OK && ret != LTTNG_ERR_EPERM) {
fb198a11
JG
2881 goto end;
2882 }
2883 }
2884 }
2885 ret = LTTNG_OK;
2886
2887end:
2888 session_unlock_list();
2889 return ret;
2890}
This page took 0.192529 seconds and 4 git commands to generate.