2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
7 #include <common/defaults.hpp>
8 #include <common/error.hpp>
9 #include <common/macros.hpp>
10 #include <common/payload-view.hpp>
11 #include <common/payload.hpp>
12 #include <common/uri.hpp>
13 #include <lttng/session-descriptor-internal.hpp>
14 #include <lttng/trace-format-descriptor-internal.hpp>
15 #include <lttng/trace-format-descriptor.h>
21 struct lttng_session_descriptor_network_location
{
22 struct lttng_uri
*control
;
23 struct lttng_uri
*data
;
27 struct lttng_session_descriptor
{
28 enum lttng_session_descriptor_type type
;
30 * If an output type that is not OUTPUT_TYPE_NONE is specified,
31 * it means that an output of that type must be generated at
32 * session-creation time.
34 enum lttng_session_descriptor_output_type output_type
;
37 struct lttng_session_descriptor_network_location network
;
38 struct lttng_uri
*local
;
41 lttng::trace_format_descriptor
*format_descriptor
;
45 struct lttng_session_descriptor_snapshot
{
46 struct lttng_session_descriptor base
;
48 * Assumes at-most one snapshot output is supported. Uses
49 * the output field of the base class.
53 struct lttng_session_descriptor_live
{
54 struct lttng_session_descriptor base
;
55 unsigned long long live_timer_us
;
58 struct lttng_session_descriptor_comm
{
59 /* enum lttng_session_descriptor_type */
61 /* enum lttng_session_descriptor_output_type */
63 /* Includes trailing null. */
67 /* Size of lttng_trace_format_descriptor */
68 uint32_t trace_format_len
;
69 /* Name follows, followed by URIs, followed by lttng_trace_format */
72 struct lttng_session_descriptor_live_comm
{
73 struct lttng_session_descriptor_comm base
;
74 /* Live-specific parameters. */
75 uint64_t live_timer_us
;
80 struct lttng_uri
*uri_copy(const struct lttng_uri
*uri
)
82 struct lttng_uri
*new_uri
= NULL
;
88 new_uri
= zmalloc
<lttng_uri
>();
92 memcpy(new_uri
, uri
, sizeof(*new_uri
));
98 struct lttng_uri
*uri_from_path(const char *path
)
100 struct lttng_uri
*uris
= NULL
;
102 char local_protocol_string
[LTTNG_PATH_MAX
+ sizeof("file://")] =
105 if (strlen(path
) >= LTTNG_PATH_MAX
) {
109 if (path
[0] != '/') {
110 /* Not an absolute path. */
114 strncat(local_protocol_string
, path
, LTTNG_PATH_MAX
);
115 uri_count
= uri_parse(local_protocol_string
, &uris
);
116 if (uri_count
!= 1) {
119 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
131 void network_location_fini(
132 struct lttng_session_descriptor_network_location
*location
)
134 free(location
->control
);
135 free(location
->data
);
138 /* Assumes ownership of control and data. */
140 int network_location_set_from_lttng_uris(
141 struct lttng_session_descriptor_network_location
*location
,
142 struct lttng_uri
*control
, struct lttng_uri
*data
)
146 if (!control
&& !data
) {
150 if (!(control
&& data
)) {
151 /* None or both must be set. */
156 if (control
->stype
!= LTTNG_STREAM_CONTROL
||
157 data
->stype
!= LTTNG_STREAM_DATA
) {
162 free(location
->control
);
163 free(location
->data
);
164 location
->control
= control
;
165 location
->data
= data
;
175 int network_location_set_from_uri_strings(
176 struct lttng_session_descriptor_network_location
*location
,
177 const char *control
, const char *data
)
181 struct lttng_uri
*parsed_uris
= NULL
;
182 struct lttng_uri
*control_uri
= NULL
;
183 struct lttng_uri
*data_uri
= NULL
;
185 uri_count
= uri_parse_str_urls(control
, data
, &parsed_uris
);
186 if (uri_count
!= 2 && uri_count
!= 0) {
192 * uri_parse_str_urls returns a contiguous array of lttng_uris whereas
193 * session descriptors expect individually allocated lttng_uris.
195 if (uri_count
== 2) {
196 control_uri
= zmalloc
<lttng_uri
>();
197 data_uri
= zmalloc
<lttng_uri
>();
198 if (!control_uri
|| !data_uri
) {
202 memcpy(control_uri
, &parsed_uris
[0], sizeof(*control_uri
));
203 memcpy(data_uri
, &parsed_uris
[1], sizeof(*data_uri
));
206 /* Ownership of control and data uris is transferred. */
207 ret
= network_location_set_from_lttng_uris(
220 struct lttng_session_descriptor
*
221 lttng_session_descriptor_create(const char *name
)
223 struct lttng_session_descriptor
*descriptor
;
225 descriptor
= zmalloc
<lttng_session_descriptor
>();
230 descriptor
->type
= LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
;
231 descriptor
->output_type
=
232 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
;
233 if (lttng_session_descriptor_set_session_name(descriptor
, name
)) {
237 /* Default to ctf1 trace format */
239 descriptor
->format_descriptor
= new lttng::trace_format_descriptor_ctf1();
240 } catch (const std::exception
& ex
) {
241 descriptor
->format_descriptor
= nullptr;
247 lttng_session_descriptor_destroy(descriptor
);
251 /* Ownership of uri is transferred. */
253 struct lttng_session_descriptor
*
254 _lttng_session_descriptor_local_create(const char *name
,
255 struct lttng_uri
*uri
)
257 struct lttng_session_descriptor
*descriptor
;
259 descriptor
= lttng_session_descriptor_create(name
);
263 descriptor
->type
= LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
;
264 descriptor
->output_type
=
265 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
;
267 if (uri
->dtype
!= LTTNG_DST_PATH
) {
270 descriptor
->output
.local
= uri
;
276 lttng_session_descriptor_destroy(descriptor
);
280 struct lttng_session_descriptor
*
281 lttng_session_descriptor_local_create(const char *name
, const char *path
)
283 struct lttng_uri
*uri
= NULL
;
284 struct lttng_session_descriptor
*descriptor
;
287 uri
= uri_from_path(path
);
292 descriptor
= _lttng_session_descriptor_local_create(name
, uri
);
298 /* Assumes the ownership of both uris. */
300 struct lttng_session_descriptor
*
301 _lttng_session_descriptor_network_create(const char *name
,
302 struct lttng_uri
*control
, struct lttng_uri
*data
)
305 struct lttng_session_descriptor
*descriptor
;
307 descriptor
= lttng_session_descriptor_create(name
);
312 descriptor
->type
= LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
;
313 descriptor
->output_type
= LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
;
314 /* Assumes the ownership of both uris. */
315 ret
= network_location_set_from_lttng_uris(&descriptor
->output
.network
,
324 lttng_session_descriptor_destroy(descriptor
);
330 struct lttng_session_descriptor
*
331 lttng_session_descriptor_network_create(const char *name
,
332 const char *control_url
, const char *data_url
)
335 struct lttng_session_descriptor
*descriptor
;
337 descriptor
= _lttng_session_descriptor_network_create(name
,
343 ret
= network_location_set_from_uri_strings(&descriptor
->output
.network
,
344 control_url
, data_url
);
350 lttng_session_descriptor_destroy(descriptor
);
355 struct lttng_session_descriptor_snapshot
*
356 _lttng_session_descriptor_snapshot_create(const char *name
)
358 struct lttng_session_descriptor_snapshot
*descriptor
;
360 descriptor
= zmalloc
<lttng_session_descriptor_snapshot
>();
365 descriptor
->base
.type
= LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
;
366 descriptor
->base
.output_type
=
367 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
;
368 if (lttng_session_descriptor_set_session_name(&descriptor
->base
,
373 /* Default to ctf1 trace format */
375 descriptor
->base
.format_descriptor
= new lttng::trace_format_descriptor_ctf1();
376 } catch (const std::exception
& ex
) {
377 descriptor
->base
.format_descriptor
= nullptr;
383 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
387 /* Ownership of control and data is transferred. */
389 struct lttng_session_descriptor_snapshot
*
390 _lttng_session_descriptor_snapshot_network_create(const char *name
,
391 struct lttng_uri
*control
, struct lttng_uri
*data
)
394 struct lttng_session_descriptor_snapshot
*descriptor
;
396 descriptor
= _lttng_session_descriptor_snapshot_create(name
);
401 descriptor
->base
.output_type
=
402 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
;
403 /* Ownership of control and data is transferred. */
404 ret
= network_location_set_from_lttng_uris(
405 &descriptor
->base
.output
.network
,
416 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
420 struct lttng_session_descriptor
*
421 lttng_session_descriptor_snapshot_create(const char *name
)
423 struct lttng_session_descriptor_snapshot
*descriptor
;
425 descriptor
= _lttng_session_descriptor_snapshot_create(name
);
426 return descriptor
? &descriptor
->base
: NULL
;
429 struct lttng_session_descriptor
*
430 lttng_session_descriptor_snapshot_network_create(const char *name
,
431 const char *control_url
, const char *data_url
)
434 struct lttng_session_descriptor_snapshot
*descriptor
;
436 descriptor
= _lttng_session_descriptor_snapshot_network_create(name
,
442 ret
= network_location_set_from_uri_strings(
443 &descriptor
->base
.output
.network
,
444 control_url
, data_url
);
448 return &descriptor
->base
;
450 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
454 /* Ownership of uri is transferred. */
456 struct lttng_session_descriptor_snapshot
*
457 _lttng_session_descriptor_snapshot_local_create(const char *name
,
458 struct lttng_uri
*uri
)
460 struct lttng_session_descriptor_snapshot
*descriptor
;
462 descriptor
= _lttng_session_descriptor_snapshot_create(name
);
466 descriptor
->base
.output_type
=
467 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
;
469 if (uri
->dtype
!= LTTNG_DST_PATH
) {
472 descriptor
->base
.output
.local
= uri
;
478 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
482 struct lttng_session_descriptor
*
483 lttng_session_descriptor_snapshot_local_create(const char *name
,
486 struct lttng_uri
*path_uri
= NULL
;
487 struct lttng_session_descriptor_snapshot
*descriptor
;
490 path_uri
= uri_from_path(path
);
495 descriptor
= _lttng_session_descriptor_snapshot_local_create(name
,
497 return descriptor
? &descriptor
->base
: NULL
;
503 struct lttng_session_descriptor_live
*
504 _lttng_session_descriptor_live_create(const char *name
,
505 unsigned long long live_timer_interval_us
)
507 struct lttng_session_descriptor_live
*descriptor
= NULL
;
509 if (live_timer_interval_us
== 0) {
512 descriptor
= zmalloc
<lttng_session_descriptor_live
>();
517 descriptor
->base
.type
= LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
;
518 descriptor
->base
.output_type
=
519 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
;
521 descriptor
->live_timer_us
= live_timer_interval_us
;
522 if (lttng_session_descriptor_set_session_name(&descriptor
->base
,
527 /* Default to ctf1 trace format */
529 descriptor
->base
.format_descriptor
= new lttng::trace_format_descriptor_ctf1();
530 } catch (const std::exception
& ex
) {
531 descriptor
->base
.format_descriptor
= nullptr;
537 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
541 /* Ownership of control and data is transferred. */
543 struct lttng_session_descriptor_live
*
544 _lttng_session_descriptor_live_network_create(
546 struct lttng_uri
*control
, struct lttng_uri
*data
,
547 unsigned long long live_timer_interval_us
)
550 struct lttng_session_descriptor_live
*descriptor
;
552 descriptor
= _lttng_session_descriptor_live_create(name
,
553 live_timer_interval_us
);
558 descriptor
->base
.output_type
=
559 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
;
561 /* Ownerwhip of control and data is transferred. */
562 ret
= network_location_set_from_lttng_uris(
563 &descriptor
->base
.output
.network
,
574 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
578 struct lttng_session_descriptor
*
579 lttng_session_descriptor_live_create(
581 unsigned long long live_timer_us
)
583 struct lttng_session_descriptor_live
*descriptor
;
585 descriptor
= _lttng_session_descriptor_live_create(name
, live_timer_us
);
587 return descriptor
? &descriptor
->base
: NULL
;
590 struct lttng_session_descriptor
*
591 lttng_session_descriptor_live_network_create(
593 const char *control_url
, const char *data_url
,
594 unsigned long long live_timer_us
)
597 struct lttng_session_descriptor_live
*descriptor
;
599 descriptor
= _lttng_session_descriptor_live_network_create(name
,
600 NULL
, NULL
, live_timer_us
);
605 ret
= network_location_set_from_uri_strings(
606 &descriptor
->base
.output
.network
,
607 control_url
, data_url
);
611 return &descriptor
->base
;
613 lttng_session_descriptor_destroy(descriptor
? &descriptor
->base
: NULL
);
617 void lttng_session_descriptor_destroy(
618 struct lttng_session_descriptor
*descriptor
)
624 switch (descriptor
->output_type
) {
625 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
627 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
628 free(descriptor
->output
.local
);
630 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
631 network_location_fini(&descriptor
->output
.network
);
637 delete (descriptor
->format_descriptor
);
638 free(descriptor
->name
);
642 ssize_t
lttng_session_descriptor_create_from_buffer(
643 const struct lttng_buffer_view
*payload
,
644 struct lttng_session_descriptor
**descriptor
)
647 ssize_t offset
= 0, ret
;
648 struct lttng_buffer_view current_view
;
650 const char *name
= NULL
;
651 const struct lttng_session_descriptor_comm
*base_header
;
652 size_t max_expected_uri_count
;
653 uint64_t live_timer_us
= 0;
654 struct lttng_uri
*uris
[2] = {};
655 enum lttng_session_descriptor_type type
;
656 enum lttng_session_descriptor_output_type output_type
;
657 lttng::trace_format_descriptor::uptr trace_format
;
659 current_view
= lttng_buffer_view_from_view(payload
, offset
,
660 sizeof(*base_header
));
661 if (!lttng_buffer_view_is_valid(¤t_view
)) {
666 base_header
= (typeof(base_header
)) current_view
.data
;
667 switch (base_header
->type
) {
668 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
669 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
671 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
673 const struct lttng_session_descriptor_live_comm
*live_header
;
675 current_view
= lttng_buffer_view_from_view(payload
, offset
,
676 sizeof(*live_header
));
677 if (!lttng_buffer_view_is_valid(¤t_view
)) {
682 live_header
= (typeof(live_header
)) current_view
.data
;
683 live_timer_us
= live_header
->live_timer_us
;
690 /* type has been validated. */
691 type
= (lttng_session_descriptor_type
) base_header
->type
;
693 switch (base_header
->output_type
) {
694 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
695 max_expected_uri_count
= 0;
697 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
698 max_expected_uri_count
= 1;
700 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
701 max_expected_uri_count
= 2;
707 /* output_type has been validated. */
708 output_type
= (lttng_session_descriptor_output_type
) base_header
->output_type
;
710 /* Skip after header. */
711 offset
+= current_view
.size
;
712 if (!base_header
->name_len
) {
717 current_view
= lttng_buffer_view_from_view(payload
, offset
,
718 base_header
->name_len
);
719 if (!lttng_buffer_view_is_valid(¤t_view
)) {
724 name
= current_view
.data
;
725 if (base_header
->name_len
== 1 ||
726 name
[base_header
->name_len
- 1] ||
727 strlen(name
) != base_header
->name_len
- 1) {
729 * Check that the name is not NULL, is NULL-terminated, and
730 * does not contain a NULL before the last byte.
736 /* Skip after the name. */
737 offset
+= base_header
->name_len
;
739 if (base_header
->uri_count
> max_expected_uri_count
) {
744 for (i
= 0; i
< base_header
->uri_count
; i
++) {
745 struct lttng_uri
*uri
;
748 current_view
= lttng_buffer_view_from_view(payload
,
749 offset
, sizeof(*uri
));
750 if (!lttng_buffer_view_is_valid(¤t_view
)) {
755 uri
= (typeof(uri
)) current_view
.data
;
756 uris
[i
] = zmalloc
<lttng_uri
>();
761 memcpy(uris
[i
], uri
, sizeof(*uri
));
762 offset
+= sizeof(*uri
);
765 /* Deserialize trace format */
767 struct lttng_payload_view current_payload_view
=
768 lttng_payload_view_from_buffer_view(
769 payload
, offset
, base_header
->trace_format_len
);
770 if (!lttng_buffer_view_is_valid(¤t_view
)) {
774 ret
= lttng::trace_format_descriptor::create_from_payload(
775 ¤t_payload_view
, trace_format
);
776 if (ret
< 0 || ret
!= base_header
->trace_format_len
) {
783 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
784 switch (output_type
) {
785 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
786 *descriptor
= lttng_session_descriptor_create(name
);
788 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
789 *descriptor
= _lttng_session_descriptor_local_create(
792 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
793 *descriptor
= _lttng_session_descriptor_network_create(
794 name
, uris
[0], uris
[1]);
797 /* Already checked. */
801 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
803 struct lttng_session_descriptor_snapshot
*snapshot
;
804 switch (output_type
) {
805 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
806 snapshot
= _lttng_session_descriptor_snapshot_create(
809 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
810 snapshot
= _lttng_session_descriptor_snapshot_local_create(
813 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
814 snapshot
= _lttng_session_descriptor_snapshot_network_create(
815 name
, uris
[0], uris
[1]);
818 /* Already checked. */
821 *descriptor
= snapshot
? &snapshot
->base
: NULL
;
824 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
826 struct lttng_session_descriptor_live
*live
;
828 switch (output_type
) {
829 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
830 live
= _lttng_session_descriptor_live_create(
831 name
, live_timer_us
);
833 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
834 live
= _lttng_session_descriptor_live_network_create(
835 name
, uris
[0], uris
[1],
838 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
842 /* Already checked. */
845 *descriptor
= live
? &live
->base
: NULL
;
849 /* Already checked. */
852 memset(uris
, 0, sizeof(uris
));
858 (*descriptor
)->format_descriptor
= trace_format
.release();
867 int lttng_session_descriptor_serialize(
868 const struct lttng_session_descriptor
*descriptor
, lttng_payload
*payload
)
871 size_t header_offset
, size_before_payload
;
872 /* There are, at most, two URIs to serialize. */
873 struct lttng_uri
*uris
[2] = {};
874 size_t uri_count
= 0;
875 /* The live header is a superset of all headers. */
876 struct lttng_session_descriptor_live_comm header
= {
879 .type
= (uint8_t) descriptor
->type
,
880 .output_type
= (uint8_t) descriptor
882 .name_len
= (uint32_t) (descriptor
->name
?
883 strlen(descriptor
->name
) +
887 .trace_format_len
= 0,
892 struct lttng_session_descriptor_comm
*base_header
;
893 const void *header_ptr
= NULL
;
896 switch (descriptor
->output_type
) {
897 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
899 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
900 uris
[0] = descriptor
->output
.local
;
902 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
903 uris
[0] = descriptor
->output
.network
.control
;
904 uris
[1] = descriptor
->output
.network
.data
;
910 uri_count
+= !!uris
[0];
911 uri_count
+= !!uris
[1];
913 header
.base
.uri_count
= uri_count
;
914 if (descriptor
->type
== LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
) {
915 const struct lttng_session_descriptor_live
*live
= lttng::utils::container_of(
916 descriptor
, <tng_session_descriptor_live::base
);
918 header
.live_timer_us
= live
->live_timer_us
;
919 header_ptr
= &header
;
920 header_size
= sizeof(header
);
922 header_ptr
= &header
.base
;
923 header_size
= sizeof(header
.base
);
926 header_offset
= payload
->buffer
.size
;
927 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, header_ptr
, header_size
);
931 if (header
.base
.name_len
) {
932 ret
= lttng_dynamic_buffer_append(
933 &payload
->buffer
, descriptor
->name
, header
.base
.name_len
);
939 for (i
= 0; i
< uri_count
; i
++) {
940 ret
= lttng_dynamic_buffer_append(
941 &payload
->buffer
, uris
[i
], sizeof(struct lttng_uri
));
947 /* Serialize trace format */
948 size_before_payload
= payload
->buffer
.size
;
949 ret
= reinterpret_cast<lttng::trace_format_descriptor
*>(descriptor
->format_descriptor
)
950 ->serialize(payload
);
954 base_header
= (typeof(base_header
)) (payload
->buffer
.data
+ header_offset
);
955 base_header
->trace_format_len
= payload
->buffer
.size
- size_before_payload
;
960 enum lttng_session_descriptor_type
961 lttng_session_descriptor_get_type(
962 const struct lttng_session_descriptor
*descriptor
)
964 return descriptor
->type
;
967 enum lttng_session_descriptor_output_type
968 lttng_session_descriptor_get_output_type(
969 const struct lttng_session_descriptor
*descriptor
)
971 return descriptor
->output_type
;
974 void lttng_session_descriptor_get_local_output_uri(
975 const struct lttng_session_descriptor
*descriptor
,
976 struct lttng_uri
*local_uri
)
978 memcpy(local_uri
, descriptor
->output
.local
, sizeof(*local_uri
));
981 void lttng_session_descriptor_get_network_output_uris(
982 const struct lttng_session_descriptor
*descriptor
,
983 struct lttng_uri
*control
,
984 struct lttng_uri
*data
)
986 memcpy(control
, descriptor
->output
.network
.control
, sizeof(*control
));
987 memcpy(data
, descriptor
->output
.network
.data
, sizeof(*data
));
991 lttng_session_descriptor_live_get_timer_interval(
992 const struct lttng_session_descriptor
*descriptor
)
994 struct lttng_session_descriptor_live
*live
;
996 live
= lttng::utils::container_of(descriptor
, <tng_session_descriptor_live::base
);
997 return live
->live_timer_us
;
1000 enum lttng_session_descriptor_status
1001 lttng_session_descriptor_get_session_name(
1002 const struct lttng_session_descriptor
*descriptor
,
1003 const char **session_name
)
1005 enum lttng_session_descriptor_status status
;
1007 if (!descriptor
|| !session_name
) {
1008 status
= LTTNG_SESSION_DESCRIPTOR_STATUS_INVALID
;
1012 *session_name
= descriptor
->name
;
1013 status
= descriptor
->name
?
1014 LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
1015 LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
;
1020 int lttng_session_descriptor_set_session_name(
1021 struct lttng_session_descriptor
*descriptor
,
1030 if (strlen(name
) >= LTTNG_NAME_MAX
) {
1034 new_name
= strdup(name
);
1039 free(descriptor
->name
);
1040 descriptor
->name
= new_name
;
1045 bool lttng_session_descriptor_is_output_destination_initialized(
1046 const struct lttng_session_descriptor
*descriptor
)
1048 switch (descriptor
->output_type
) {
1049 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
1051 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
1052 return descriptor
->output
.local
;
1053 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
1054 return descriptor
->output
.network
.control
;
1060 bool lttng_session_descriptor_has_output_directory(
1061 const struct lttng_session_descriptor
*descriptor
)
1063 switch (descriptor
->output_type
) {
1064 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
1066 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
1067 if (descriptor
->output
.local
) {
1068 return *descriptor
->output
.local
->dst
.path
;
1071 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
1072 if (descriptor
->output
.network
.control
) {
1073 return *descriptor
->output
.network
.control
->subdir
;
1082 enum lttng_error_code
lttng_session_descriptor_set_default_output(
1083 struct lttng_session_descriptor
*descriptor
,
1084 time_t *session_creation_time
,
1085 const char *absolute_home_path
)
1087 enum lttng_error_code ret_code
= LTTNG_OK
;
1088 struct lttng_uri
*uris
= NULL
;
1090 switch (descriptor
->output_type
) {
1091 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
1093 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
1097 char local_uri
[LTTNG_PATH_MAX
];
1098 char creation_datetime_suffix
[17] = {};
1100 if (session_creation_time
) {
1101 size_t strftime_ret
;
1102 struct tm
*timeinfo
;
1104 timeinfo
= localtime(session_creation_time
);
1106 ret_code
= LTTNG_ERR_FATAL
;
1109 strftime_ret
= strftime(creation_datetime_suffix
,
1110 sizeof(creation_datetime_suffix
),
1111 "-%Y%m%d-%H%M%S", timeinfo
);
1112 if (strftime_ret
== 0) {
1113 ERR("Failed to format session creation timestamp while setting default local output destination");
1114 ret_code
= LTTNG_ERR_FATAL
;
1118 LTTNG_ASSERT(descriptor
->name
);
1119 ret
= snprintf(local_uri
, sizeof(local_uri
),
1120 "file://%s/%s/%s%s",
1122 DEFAULT_TRACE_DIR_NAME
, descriptor
->name
,
1123 creation_datetime_suffix
);
1124 if (ret
>= sizeof(local_uri
)) {
1125 ERR("Truncation occurred while setting default local output destination");
1126 ret_code
= LTTNG_ERR_SET_URL
;
1128 } else if (ret
< 0) {
1129 PERROR("Failed to format default local output URI");
1130 ret_code
= LTTNG_ERR_SET_URL
;
1134 uri_ret
= uri_parse(local_uri
, &uris
);
1136 ret_code
= LTTNG_ERR_SET_URL
;
1139 free(descriptor
->output
.local
);
1140 descriptor
->output
.local
= &uris
[0];
1144 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
1148 struct lttng_uri
*control
= NULL
, *data
= NULL
;
1150 uri_ret
= uri_parse_str_urls("net://127.0.0.1", NULL
, &uris
);
1152 ret_code
= LTTNG_ERR_SET_URL
;
1156 control
= uri_copy(&uris
[0]);
1157 data
= uri_copy(&uris
[1]);
1158 if (!control
|| !data
) {
1161 ret_code
= LTTNG_ERR_SET_URL
;
1165 /* Ownership of uris is transferred. */
1166 ret
= network_location_set_from_lttng_uris(
1167 &descriptor
->output
.network
,
1171 ret_code
= LTTNG_ERR_SET_URL
;
1185 * Note that only properties that can be populated by the session daemon
1186 * (output destination and name) are assigned.
1188 int lttng_session_descriptor_assign(
1189 struct lttng_session_descriptor
*dst
,
1190 const struct lttng_session_descriptor
*src
)
1194 if (dst
->type
!= src
->type
) {
1198 if (dst
->output_type
!= src
->output_type
) {
1202 ret
= lttng_session_descriptor_set_session_name(dst
, src
->name
);
1208 dst
->format_descriptor
= src
->format_descriptor
->clone().release();
1214 switch (dst
->output_type
) {
1215 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
1216 free(dst
->output
.local
);
1217 dst
->output
.local
= uri_copy(src
->output
.local
);
1218 if (!dst
->output
.local
) {
1223 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
1225 struct lttng_uri
*control_copy
= NULL
, *data_copy
= NULL
;
1227 control_copy
= uri_copy(dst
->output
.network
.control
);
1228 if (!control_copy
&& dst
->output
.network
.control
) {
1232 data_copy
= uri_copy(dst
->output
.network
.data
);
1233 if (!data_copy
&& dst
->output
.network
.data
) {
1238 ret
= network_location_set_from_lttng_uris(&dst
->output
.network
,
1239 control_copy
, data_copy
);
1242 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
: