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