2 * SPDX-License-Identifier: MIT
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
8 #define BT_LOG_TAG "CTF-WRITER/TRACE"
17 #include <babeltrace2-ctf-writer/event.h>
18 #include <babeltrace2-ctf-writer/object.h>
19 #include <babeltrace2-ctf-writer/utils.h>
20 #include <babeltrace2/types.h>
22 #include "common/assert.h"
23 #include "compat/compiler.h"
24 #include "compat/endian.h"
26 #include "attributes.h"
27 #include "clock-class.h"
29 #include "event-class.h"
31 #include "field-types.h"
32 #include "field-wrapper.h"
34 #include "stream-class.h"
38 #include "validation.h"
43 #define DEFAULT_IDENTIFIER_SIZE 128
44 #define DEFAULT_METADATA_STRING_SIZE 4096
47 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
48 bt_ctf_object_release_func release_func
)
52 BT_LOGD_STR("Initializing common trace object.");
53 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_UNSPECIFIED
;
54 bt_ctf_object_init_shared_with_parent(&trace
->base
, release_func
);
55 trace
->clock_classes
= g_ptr_array_new_with_free_func(
56 (GDestroyNotify
) bt_ctf_object_put_ref
);
57 if (!trace
->clock_classes
) {
58 BT_LOGE_STR("Failed to allocate one GPtrArray.");
62 trace
->streams
= g_ptr_array_new_with_free_func(
63 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
64 if (!trace
->streams
) {
65 BT_LOGE_STR("Failed to allocate one GPtrArray.");
69 trace
->stream_classes
= g_ptr_array_new_with_free_func(
70 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
71 if (!trace
->stream_classes
) {
72 BT_LOGE_STR("Failed to allocate one GPtrArray.");
76 /* Create the environment array object */
77 trace
->environment
= bt_ctf_attributes_create();
78 if (!trace
->environment
) {
79 BT_LOGE_STR("Cannot create empty attributes object.");
83 BT_LOGD("Initialized common trace object: addr=%p", trace
);
94 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
)
96 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
97 trace
, bt_ctf_trace_common_get_name(trace
));
99 if (trace
->environment
) {
100 BT_LOGD_STR("Destroying environment attributes.");
101 bt_ctf_attributes_destroy(trace
->environment
);
105 g_string_free(trace
->name
, TRUE
);
108 if (trace
->clock_classes
) {
109 BT_LOGD_STR("Putting clock classes.");
110 g_ptr_array_free(trace
->clock_classes
, TRUE
);
113 if (trace
->streams
) {
114 BT_LOGD_STR("Destroying streams.");
115 g_ptr_array_free(trace
->streams
, TRUE
);
118 if (trace
->stream_classes
) {
119 BT_LOGD_STR("Destroying stream classes.");
120 g_ptr_array_free(trace
->stream_classes
, TRUE
);
123 BT_LOGD_STR("Putting packet header field type.");
124 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
128 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
)
133 BT_LOGW_STR("Invalid parameter: trace is NULL.");
139 BT_LOGW_STR("Invalid parameter: name is NULL.");
145 BT_LOGW("Invalid parameter: trace is frozen: "
146 "addr=%p, name=\"%s\"",
147 trace
, bt_ctf_trace_common_get_name(trace
));
152 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
155 BT_LOGE_STR("Failed to allocate one GString.");
160 BT_LOGT("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
167 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
,
173 BT_LOGW_STR("Invalid parameter: trace is NULL.");
179 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
185 BT_LOGW("Invalid parameter: trace is frozen: "
186 "addr=%p, name=\"%s\"",
187 trace
, bt_ctf_trace_common_get_name(trace
));
192 bt_uuid_copy(trace
->uuid
, uuid
);
193 trace
->uuid_set
= BT_CTF_TRUE
;
194 BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", "
195 "uuid=\"" BT_UUID_FMT
"\"",
196 trace
, bt_ctf_trace_common_get_name(trace
),
197 BT_UUID_FMT_VALUES(uuid
));
204 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
205 const char *name
, struct bt_ctf_private_value
*value
)
210 BT_LOGW_STR("Invalid parameter: trace is NULL.");
216 BT_LOGW_STR("Invalid parameter: name is NULL.");
222 BT_LOGW_STR("Invalid parameter: value is NULL.");
227 if (!bt_ctf_identifier_is_valid(name
)) {
228 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
229 "trace-addr=%p, trace-name=\"%s\", "
231 trace
, bt_ctf_trace_common_get_name(trace
), name
);
236 if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value
)) &&
237 !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value
))) {
238 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
239 "trace-addr=%p, trace-name=\"%s\", "
240 "env-name=\"%s\", env-value-type=%s",
241 trace
, bt_ctf_trace_common_get_name(trace
), name
,
242 bt_ctf_value_type_string(
243 bt_ctf_value_get_type(
244 bt_ctf_private_value_as_value(value
))));
251 * New environment fields may be added to a frozen trace,
252 * but existing fields may not be changed.
254 * The object passed is frozen like all other attributes.
256 struct bt_ctf_private_value
*attribute
=
257 bt_ctf_attributes_borrow_field_value_by_name(
258 trace
->environment
, name
);
261 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
262 "trace-addr=%p, trace-name=\"%s\", "
264 trace
, bt_ctf_trace_common_get_name(trace
), name
);
269 bt_ctf_value_freeze(bt_ctf_private_value_as_value(value
));
272 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
275 BT_LOGE("Cannot set environment field's value: "
276 "trace-addr=%p, trace-name=\"%s\", "
278 trace
, bt_ctf_trace_common_get_name(trace
), name
);
280 BT_LOGT("Set environment field's value: "
281 "trace-addr=%p, trace-name=\"%s\", "
282 "env-name=\"%s\", value-addr=%p",
283 trace
, bt_ctf_trace_common_get_name(trace
), name
, value
);
291 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
292 const char *name
, const char *value
)
295 struct bt_ctf_private_value
*env_value_string_obj
= NULL
;
298 BT_LOGW_STR("Invalid parameter: value is NULL.");
303 env_value_string_obj
= bt_ctf_private_value_string_create_init(value
);
304 if (!env_value_string_obj
) {
305 BT_LOGE_STR("Cannot create string value object.");
310 /* bt_ctf_trace_common_set_environment_field() logs errors */
311 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
312 env_value_string_obj
);
315 bt_ctf_object_put_ref(env_value_string_obj
);
320 int bt_ctf_trace_common_set_environment_field_integer(
321 struct bt_ctf_trace_common
*trace
, const char *name
, int64_t value
)
324 struct bt_ctf_private_value
*env_value_integer_obj
= NULL
;
326 env_value_integer_obj
= bt_ctf_private_value_integer_create_init(value
);
327 if (!env_value_integer_obj
) {
328 BT_LOGE_STR("Cannot create integer value object.");
333 /* bt_ctf_trace_common_set_environment_field() logs errors */
334 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
335 env_value_integer_obj
);
338 bt_ctf_object_put_ref(env_value_integer_obj
);
343 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
344 struct bt_ctf_clock_class
*clock_class
)
349 BT_LOGW_STR("Invalid parameter: trace is NULL.");
354 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
355 BT_LOGW("Invalid parameter: clock class is invalid: "
356 "trace-addr=%p, trace-name=\"%s\", "
357 "clock-class-addr=%p, clock-class-name=\"%s\"",
358 trace
, bt_ctf_trace_common_get_name(trace
),
359 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
364 /* Check for duplicate clock classes */
365 if (bt_ctf_trace_common_has_clock_class(trace
, clock_class
)) {
366 BT_LOGW("Invalid parameter: clock class already exists in trace: "
367 "trace-addr=%p, trace-name=\"%s\", "
368 "clock-class-addr=%p, clock-class-name=\"%s\"",
369 trace
, bt_ctf_trace_common_get_name(trace
),
370 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
375 bt_ctf_object_get_ref(clock_class
);
376 g_ptr_array_add(trace
->clock_classes
, clock_class
);
379 BT_LOGT_STR("Freezing added clock class because trace is frozen.");
380 bt_ctf_clock_class_freeze(clock_class
);
383 BT_LOGT("Added clock class to trace: "
384 "trace-addr=%p, trace-name=\"%s\", "
385 "clock-class-addr=%p, clock-class-name=\"%s\"",
386 trace
, bt_ctf_trace_common_get_name(trace
),
387 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
394 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
395 struct bt_ctf_field_type_common
*packet_header_type
)
398 bool is_valid
= true;
399 struct bt_ctf_field_type_common
*field_type
= NULL
;
401 if (!packet_header_type
) {
403 * No packet header field type: trace must have only
404 * one stream. At this point the stream class being
405 * added is not part of the trace yet, so we validate
406 * that the trace contains no stream classes yet.
408 if (trace
->stream_classes
->len
>= 1) {
409 BT_LOGW_STR("Invalid packet header field type: "
410 "packet header field type does not exist but there's more than one stream class in the trace.");
414 /* No packet header field type: valid at this point */
418 /* Packet header field type, if it exists, must be a structure */
419 if (packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
420 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
421 "ft-addr=%p, ft-id=%s",
423 bt_ctf_field_type_id_string(packet_header_type
->id
));
428 * If there's a `magic` field, it must be a 32-bit unsigned
429 * integer field type. Also it must be the first field of the
430 * packet header field type.
432 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
433 packet_header_type
, "magic");
435 const char *field_name
;
437 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
438 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
439 "magic-ft-addr=%p, magic-ft-id=%s",
441 bt_ctf_field_type_id_string(field_type
->id
));
445 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
446 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
447 "magic-ft-addr=%p", field_type
);
451 if (bt_ctf_field_type_common_integer_get_size(field_type
) != 32) {
452 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
453 "magic-ft-addr=%p, magic-ft-size=%u",
455 bt_ctf_field_type_common_integer_get_size(field_type
));
459 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
460 packet_header_type
, &field_name
, NULL
, 0);
461 BT_ASSERT_DBG(ret
== 0);
463 if (strcmp(field_name
, "magic") != 0) {
464 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
465 "magic-ft-addr=%p, first-field-name=\"%s\"",
466 field_type
, field_name
);
472 * If there's a `uuid` field, it must be an array field type of
473 * length 16 with an 8-bit unsigned integer element field type.
475 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
476 packet_header_type
, "uuid");
478 struct bt_ctf_field_type_common
*elem_ft
;
480 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
481 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
482 "uuid-ft-addr=%p, uuid-ft-id=%s",
484 bt_ctf_field_type_id_string(field_type
->id
));
488 if (bt_ctf_field_type_common_array_get_length(field_type
) != 16) {
489 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
490 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
492 bt_ctf_field_type_common_array_get_length(field_type
));
496 elem_ft
= bt_ctf_field_type_common_array_borrow_element_field_type(field_type
);
497 BT_ASSERT_DBG(elem_ft
);
499 if (elem_ft
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
500 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
501 "elem-ft-addr=%p, elem-ft-id=%s",
503 bt_ctf_field_type_id_string(elem_ft
->id
));
507 if (bt_ctf_field_type_common_integer_is_signed(elem_ft
)) {
508 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
509 "elem-ft-addr=%p", elem_ft
);
513 if (bt_ctf_field_type_common_integer_get_size(elem_ft
) != 8) {
514 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
515 "elem-ft-addr=%p, elem-ft-size=%u",
517 bt_ctf_field_type_common_integer_get_size(elem_ft
));
523 * The `stream_id` field must exist if there's more than one
524 * stream classes in the trace.
526 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
527 packet_header_type
, "stream_id");
529 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
530 BT_LOGW_STR("Invalid packet header field type: "
531 "`stream_id` field does not exist but there's more than one stream class in the trace.");
536 * If there's a `stream_id` field, it must be an unsigned
537 * integer field type.
540 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
541 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
542 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
544 bt_ctf_field_type_id_string(field_type
->id
));
548 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
549 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
550 "stream-id-ft-addr=%p", field_type
);
556 * If there's a `packet_seq_num` field, it must be an unsigned
557 * integer field type.
559 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
560 packet_header_type
, "packet_seq_num");
562 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
563 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
564 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
566 bt_ctf_field_type_id_string(field_type
->id
));
570 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
571 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
572 "packet-seq-num-ft-addr=%p", field_type
);
587 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
588 struct bt_ctf_stream_class_common
*stream_class
,
589 struct bt_ctf_field_type_common
*packet_context_type
,
590 bool check_ts_begin_end_mapped
)
592 bool is_valid
= true;
593 struct bt_ctf_field_type_common
*field_type
= NULL
;
595 if (!packet_context_type
) {
596 /* No packet context field type: valid at this point */
600 /* Packet context field type, if it exists, must be a structure */
601 if (packet_context_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
602 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
603 "ft-addr=%p, ft-id=%s",
605 bt_ctf_field_type_id_string(packet_context_type
->id
));
610 * If there's a `packet_size` field, it must be an unsigned
611 * integer field type.
613 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
614 packet_context_type
, "packet_size");
616 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
617 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
618 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
620 bt_ctf_field_type_id_string(field_type
->id
));
624 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
625 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
626 "packet-size-ft-addr=%p", field_type
);
632 * If there's a `content_size` field, it must be an unsigned
633 * integer field type.
635 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
636 packet_context_type
, "content_size");
638 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
639 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
640 "content-size-ft-addr=%p, content-size-ft-id=%s",
642 bt_ctf_field_type_id_string(field_type
->id
));
646 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
647 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
648 "content-size-ft-addr=%p", field_type
);
654 * If there's a `events_discarded` field, it must be an unsigned
655 * integer field type.
657 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
658 packet_context_type
, "events_discarded");
660 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
661 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
662 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
664 bt_ctf_field_type_id_string(field_type
->id
));
668 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
669 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
670 "events-discarded-ft-addr=%p", field_type
);
676 * If there's a `timestamp_begin` field, it must be an unsigned
677 * integer field type. Also, if the trace is not a CTF writer's
678 * trace, then we cannot automatically set the mapped clock
679 * class of this field, so it must have a mapped clock class.
681 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
682 packet_context_type
, "timestamp_begin");
684 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
685 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
686 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
688 bt_ctf_field_type_id_string(field_type
->id
));
692 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
693 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
694 "timestamp-begin-ft-addr=%p", field_type
);
698 if (check_ts_begin_end_mapped
) {
699 struct bt_ctf_clock_class
*clock_class
=
700 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
704 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
705 "timestamp-begin-ft-addr=%p", field_type
);
712 * If there's a `timestamp_end` field, it must be an unsigned
713 * integer field type. Also, if the trace is not a CTF writer's
714 * trace, then we cannot automatically set the mapped clock
715 * class of this field, so it must have a mapped clock class.
717 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
718 packet_context_type
, "timestamp_end");
720 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
721 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
722 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
724 bt_ctf_field_type_id_string(field_type
->id
));
728 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
729 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
730 "timestamp-end-ft-addr=%p", field_type
);
734 if (check_ts_begin_end_mapped
) {
735 struct bt_ctf_clock_class
*clock_class
=
736 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
740 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
741 "timestamp-end-ft-addr=%p", field_type
);
757 bool event_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
758 struct bt_ctf_stream_class_common
*stream_class
,
759 struct bt_ctf_field_type_common
*event_header_type
)
761 bool is_valid
= true;
762 struct bt_ctf_field_type_common
*field_type
= NULL
;
765 * We do not validate that the `timestamp` field exists here
766 * because CTF does not require this exact name to be mapped to
770 if (!event_header_type
) {
772 * No event header field type: stream class must have
773 * only one event class.
775 if (bt_ctf_stream_class_common_get_event_class_count(stream_class
) > 1) {
776 BT_LOGW_STR("Invalid event header field type: "
777 "event header field type does not exist but there's more than one event class in the stream class.");
781 /* No event header field type: valid at this point */
785 /* Event header field type, if it exists, must be a structure */
786 if (event_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
787 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
788 "ft-addr=%p, ft-id=%s",
790 bt_ctf_field_type_id_string(event_header_type
->id
));
795 * If there's an `id` field, it must be an unsigned integer
796 * field type or an enumeration field type with an unsigned
797 * integer container field type.
799 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
800 event_header_type
, "id");
802 struct bt_ctf_field_type_common
*int_ft
;
804 if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
806 } else if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
807 int_ft
= bt_ctf_field_type_common_enumeration_borrow_container_field_type(
810 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
811 "id-ft-addr=%p, id-ft-id=%s",
813 bt_ctf_field_type_id_string(field_type
->id
));
817 BT_ASSERT_DBG(int_ft
);
818 if (bt_ctf_field_type_common_integer_is_signed(int_ft
)) {
819 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
820 "id-ft-addr=%p", int_ft
);
835 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common
*trace
)
839 if (trace
->packet_header_field_type
) {
840 struct bt_ctf_clock_class
*clock_class
= NULL
;
842 ret
= bt_ctf_field_type_common_validate_single_clock_class(
843 trace
->packet_header_field_type
,
845 bt_ctf_object_put_ref(clock_class
);
846 if (ret
|| clock_class
) {
847 BT_LOGW("Trace's packet header field type cannot "
848 "contain a field type which is mapped to "
850 "trace-addr=%p, trace-name=\"%s\", "
851 "clock-class-name=\"%s\"",
852 trace
, bt_ctf_trace_common_get_name(trace
),
854 bt_ctf_clock_class_get_name(clock_class
) :
864 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
865 struct bt_ctf_stream_class_common
*stream_class
,
866 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
867 struct bt_ctf_clock_class
*init_expected_clock_class
,
868 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
869 struct bt_ctf_field_type_common
*packet_context_field_type
,
870 struct bt_ctf_field_type_common
*event_header_field_type
),
871 bool check_ts_begin_end_mapped
)
876 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
877 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
878 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
879 BT_CTF_VALIDATION_FLAG_TRACE
|
880 BT_CTF_VALIDATION_FLAG_STREAM
;
881 const enum bt_ctf_validation_flag ec_validation_flags
=
882 BT_CTF_VALIDATION_FLAG_EVENT
;
883 struct bt_ctf_field_type_common
*packet_header_type
= NULL
;
884 struct bt_ctf_field_type_common
*packet_context_type
= NULL
;
885 struct bt_ctf_field_type_common
*event_header_type
= NULL
;
886 struct bt_ctf_field_type_common
*stream_event_ctx_type
= NULL
;
887 int64_t event_class_count
= 0;
888 struct bt_ctf_trace_common
*current_parent_trace
= NULL
;
889 struct bt_ctf_clock_class
*expected_clock_class
=
890 bt_ctf_object_get_ref(init_expected_clock_class
);
892 BT_ASSERT_DBG(copy_field_type_func
);
895 BT_LOGW_STR("Invalid parameter: trace is NULL.");
901 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
906 BT_LOGD("Adding stream class to trace: "
907 "trace-addr=%p, trace-name=\"%s\", "
908 "stream-class-addr=%p, stream-class-name=\"%s\", "
909 "stream-class-id=%" PRId64
,
910 trace
, bt_ctf_trace_common_get_name(trace
),
911 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
912 bt_ctf_stream_class_common_get_id(stream_class
));
914 current_parent_trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
915 if (current_parent_trace
) {
916 /* Stream class is already associated to a trace, abort. */
917 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
918 "stream-class-trace-addr=%p, "
919 "stream-class-trace-name=\"%s\"",
920 current_parent_trace
,
921 bt_ctf_trace_common_get_name(current_parent_trace
));
927 bt_ctf_stream_class_common_get_event_class_count(stream_class
);
928 BT_ASSERT_DBG(event_class_count
>= 0);
930 if (!stream_class
->frozen
) {
932 * Stream class is not frozen yet. Validate that the
933 * stream class contains at most a single clock class
934 * because the previous
935 * bt_ctf_stream_class_common_add_event_class() calls did
936 * not make this validation since the stream class's
937 * direct field types (packet context, event header,
938 * event context) could change afterwards. This stream
939 * class is about to be frozen and those field types
940 * won't be changed if this function succeeds.
942 * At this point we're also sure that the stream class's
943 * clock, if any, has the same class as the stream
944 * class's expected clock class, if any. This is why, if
945 * bt_ctf_stream_class_common_validate_single_clock_class()
946 * succeeds below, the call to
947 * bt_ctf_stream_class_map_clock_class() at the end of this
948 * function is safe because it maps to the same, single
951 ret
= bt_ctf_stream_class_common_validate_single_clock_class(
952 stream_class
, &expected_clock_class
);
954 BT_LOGW("Invalid parameter: stream class or one of its "
955 "event classes contains a field type which is "
956 "not recursively mapped to the expected "
958 "stream-class-addr=%p, "
959 "stream-class-id=%" PRId64
", "
960 "stream-class-name=\"%s\", "
961 "expected-clock-class-addr=%p, "
962 "expected-clock-class-name=\"%s\"",
963 stream_class
, bt_ctf_stream_class_common_get_id(stream_class
),
964 bt_ctf_stream_class_common_get_name(stream_class
),
965 expected_clock_class
,
966 expected_clock_class
?
967 bt_ctf_clock_class_get_name(expected_clock_class
) :
973 ret
= check_packet_header_type_has_no_clock_class(trace
);
975 /* check_packet_header_type_has_no_clock_class() logs errors */
980 * We're about to freeze both the trace and the stream class.
981 * Also, each event class contained in this stream class are
984 * This trace, this stream class, and all its event classes
985 * should be valid at this point.
987 * Validate trace and stream class first, then each event
988 * class of this stream class can be validated individually.
991 bt_ctf_trace_common_borrow_packet_header_field_type(trace
);
992 packet_context_type
=
993 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class
);
995 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class
);
996 stream_event_ctx_type
=
997 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class
);
999 BT_LOGD("Validating trace and stream class field types.");
1000 ret
= bt_ctf_validate_class_types(trace
->environment
,
1001 packet_header_type
, packet_context_type
, event_header_type
,
1002 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1003 stream_class
->valid
, 1, &trace_sc_validation_output
,
1004 trace_sc_validation_flags
, copy_field_type_func
);
1008 * This means something went wrong during the validation
1009 * process, not that the objects are invalid.
1011 BT_LOGE("Failed to validate trace and stream class field types: "
1016 if ((trace_sc_validation_output
.valid_flags
&
1017 trace_sc_validation_flags
) !=
1018 trace_sc_validation_flags
) {
1019 /* Invalid trace/stream class */
1020 BT_LOGW("Invalid trace or stream class field types: "
1022 trace_sc_validation_output
.valid_flags
);
1027 if (event_class_count
> 0) {
1028 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1030 if (!ec_validation_outputs
) {
1031 BT_LOGE_STR("Failed to allocate one validation output structure.");
1037 /* Validate each event class individually */
1038 for (i
= 0; i
< event_class_count
; i
++) {
1039 struct bt_ctf_event_class_common
*event_class
=
1040 bt_ctf_stream_class_common_borrow_event_class_by_index(
1042 struct bt_ctf_field_type_common
*event_context_type
= NULL
;
1043 struct bt_ctf_field_type_common
*event_payload_type
= NULL
;
1045 event_context_type
=
1046 bt_ctf_event_class_common_borrow_context_field_type(
1048 event_payload_type
=
1049 bt_ctf_event_class_common_borrow_payload_field_type(
1053 * It is important to use the field types returned by
1054 * the previous trace and stream class validation here
1055 * because copies could have been made.
1057 BT_LOGD("Validating event class's field types: "
1058 "addr=%p, name=\"%s\", id=%" PRId64
,
1059 event_class
, bt_ctf_event_class_common_get_name(event_class
),
1060 bt_ctf_event_class_common_get_id(event_class
));
1061 ret
= bt_ctf_validate_class_types(trace
->environment
,
1062 trace_sc_validation_output
.packet_header_type
,
1063 trace_sc_validation_output
.packet_context_type
,
1064 trace_sc_validation_output
.event_header_type
,
1065 trace_sc_validation_output
.stream_event_ctx_type
,
1066 event_context_type
, event_payload_type
,
1067 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1068 ec_validation_flags
, copy_field_type_func
);
1071 BT_LOGE("Failed to validate event class field types: "
1076 if ((ec_validation_outputs
[i
].valid_flags
&
1077 ec_validation_flags
) != ec_validation_flags
) {
1078 /* Invalid event class */
1079 BT_LOGW("Invalid event class field types: "
1081 ec_validation_outputs
[i
].valid_flags
);
1087 stream_id
= bt_ctf_stream_class_common_get_id(stream_class
);
1088 if (stream_id
< 0) {
1089 stream_id
= trace
->next_stream_id
++;
1090 if (stream_id
< 0) {
1091 BT_LOGE_STR("No more stream class IDs available.");
1096 /* Try to assign a new stream id */
1097 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1098 if (stream_id
== bt_ctf_stream_class_common_get_id(
1099 trace
->stream_classes
->pdata
[i
])) {
1100 /* Duplicate stream id found */
1101 BT_LOGW("Duplicate stream class ID: "
1102 "id=%" PRId64
, (int64_t) stream_id
);
1108 if (bt_ctf_stream_class_common_set_id_no_check(stream_class
,
1110 /* TODO Should retry with a different stream id */
1111 BT_LOGE("Cannot set stream class's ID: "
1112 "id=%" PRId64
, (int64_t) stream_id
);
1119 * At this point all the field types in the validation output
1120 * are valid. Validate the semantics of some scopes according to
1121 * the CTF specification.
1123 if (!packet_header_field_type_is_valid(trace
,
1124 trace_sc_validation_output
.packet_header_type
)) {
1125 BT_LOGW_STR("Invalid trace's packet header field type.");
1130 if (!packet_context_field_type_is_valid(trace
,
1132 trace_sc_validation_output
.packet_context_type
,
1133 check_ts_begin_end_mapped
)) {
1134 BT_LOGW_STR("Invalid stream class's packet context field type.");
1139 if (!event_header_field_type_is_valid(trace
,
1141 trace_sc_validation_output
.event_header_type
)) {
1142 BT_LOGW_STR("Invalid steam class's event header field type.");
1148 * Now is the time to automatically map specific field types of
1149 * the stream class's packet context and event header field
1150 * types to the stream class's clock's class if they are not
1151 * mapped to a clock class yet. We do it here because we know
1152 * that after this point, everything is frozen so it won't be
1153 * possible for the user to modify the stream class's clock, or
1154 * to map those field types to other clock classes.
1156 if (map_clock_classes_func
) {
1157 if (map_clock_classes_func(stream_class
,
1158 trace_sc_validation_output
.packet_context_type
,
1159 trace_sc_validation_output
.event_header_type
)) {
1160 /* map_clock_classes_func() logs errors */
1166 bt_ctf_object_set_parent(&stream_class
->base
, &trace
->base
);
1167 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1170 * At this point we know that the function will be successful.
1171 * Therefore we can replace the trace and stream class field
1172 * types with what's in their validation output structure and
1173 * mark them as valid. We can also replace the field types of
1174 * all the event classes of the stream class and mark them as
1177 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1178 &trace_sc_validation_output
, trace_sc_validation_flags
);
1180 stream_class
->valid
= 1;
1183 * Put what was not moved in bt_ctf_validation_replace_types().
1185 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1187 for (i
= 0; i
< event_class_count
; i
++) {
1188 struct bt_ctf_event_class_common
*event_class
=
1189 bt_ctf_stream_class_common_borrow_event_class_by_index(
1192 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1193 &ec_validation_outputs
[i
], ec_validation_flags
);
1194 event_class
->valid
= 1;
1197 * Put what was not moved in
1198 * bt_ctf_validation_replace_types().
1200 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1204 * Freeze the trace and the stream class.
1206 bt_ctf_stream_class_common_freeze(stream_class
);
1207 bt_ctf_trace_common_freeze(trace
);
1210 * It is safe to set the stream class's unique clock class
1211 * now because the stream class is frozen.
1213 if (expected_clock_class
) {
1214 BT_CTF_OBJECT_MOVE_REF(stream_class
->clock_class
, expected_clock_class
);
1217 BT_LOGD("Added stream class to trace: "
1218 "trace-addr=%p, trace-name=\"%s\", "
1219 "stream-class-addr=%p, stream-class-name=\"%s\", "
1220 "stream-class-id=%" PRId64
,
1221 trace
, bt_ctf_trace_common_get_name(trace
),
1222 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
1223 bt_ctf_stream_class_common_get_id(stream_class
));
1227 bt_ctf_object_set_parent(&stream_class
->base
, NULL
);
1229 if (ec_validation_outputs
) {
1230 for (i
= 0; i
< event_class_count
; i
++) {
1231 bt_ctf_validation_output_put_types(
1232 &ec_validation_outputs
[i
]);
1237 g_free(ec_validation_outputs
);
1238 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1239 bt_ctf_object_put_ref(expected_clock_class
);
1244 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
1245 struct bt_ctf_clock_class
*clock_class
)
1247 struct bt_ctf_search_query query
= { .value
= clock_class
, .found
= 0 };
1249 BT_ASSERT_DBG(trace
);
1250 BT_ASSERT_DBG(clock_class
);
1252 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1257 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
1258 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
)
1263 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1268 if (trace
->frozen
) {
1269 BT_LOGW("Invalid parameter: trace is frozen: "
1270 "addr=%p, name=\"%s\"",
1271 trace
, bt_ctf_trace_common_get_name(trace
));
1276 if (byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1277 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1278 "addr=%p, name=\"%s\"",
1279 trace
, bt_ctf_trace_common_get_name(trace
));
1284 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1285 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1286 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1287 BT_LOGW("Invalid parameter: invalid byte order: "
1288 "addr=%p, name=\"%s\", bo=%s",
1289 trace
, bt_ctf_trace_common_get_name(trace
),
1290 bt_ctf_byte_order_string(byte_order
));
1295 trace
->native_byte_order
= byte_order
;
1296 BT_LOGT("Set trace's native byte order: "
1297 "addr=%p, name=\"%s\", bo=%s",
1298 trace
, bt_ctf_trace_common_get_name(trace
),
1299 bt_ctf_byte_order_string(byte_order
));
1306 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
1307 struct bt_ctf_field_type_common
*packet_header_type
)
1312 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1317 if (trace
->frozen
) {
1318 BT_LOGW("Invalid parameter: trace is frozen: "
1319 "addr=%p, name=\"%s\"",
1320 trace
, bt_ctf_trace_common_get_name(trace
));
1325 /* packet_header_type must be a structure. */
1326 if (packet_header_type
&&
1327 packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1328 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1329 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1330 trace
, bt_ctf_trace_common_get_name(trace
),
1332 bt_ctf_field_type_id_string(packet_header_type
->id
));
1337 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
1338 trace
->packet_header_field_type
= bt_ctf_object_get_ref(packet_header_type
);
1339 BT_LOGT("Set trace's packet header field type: "
1340 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1341 trace
, bt_ctf_trace_common_get_name(trace
), packet_header_type
);
1347 int64_t get_stream_class_count(void *element
)
1349 return bt_ctf_trace_get_stream_class_count(
1350 (struct bt_ctf_trace
*) element
);
1354 void *get_stream_class(void *element
, int i
)
1356 return bt_ctf_trace_get_stream_class_by_index(
1357 (struct bt_ctf_trace
*) element
, i
);
1361 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1363 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1366 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1367 bt_ctf_visitor visitor
, void *data
)
1370 struct bt_ctf_visitor_object obj
= {
1372 .type
= BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1376 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1382 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1387 BT_LOGT("Visiting trace: addr=%p, name=\"%s\"",
1388 trace
, bt_ctf_trace_get_name(trace
));
1389 ret
= bt_ctf_visitor_helper(&obj
, get_stream_class_count
,
1390 get_stream_class
, visit_stream_class
, visitor
, data
);
1396 void bt_ctf_trace_destroy(struct bt_ctf_object
*obj
)
1398 struct bt_ctf_trace
*trace
= (void *) obj
;
1400 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1401 trace
, bt_ctf_trace_get_name(trace
));
1402 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace
));
1407 struct bt_ctf_trace
*bt_ctf_trace_create(void)
1409 struct bt_ctf_trace
*trace
= NULL
;
1412 BT_LOGD_STR("Creating CTF writer trace object.");
1413 trace
= g_new0(struct bt_ctf_trace
, 1);
1415 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1419 ret
= bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace
),
1420 bt_ctf_trace_destroy
);
1422 /* bt_ctf_trace_common_initialize() logs errors */
1426 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
1430 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1434 const uint8_t *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
1436 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace
));
1439 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
1440 const uint8_t *uuid
)
1442 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace
), uuid
);
1445 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
1446 const char *name
, const char *value
)
1448 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace
),
1452 int bt_ctf_trace_set_environment_field_integer(
1453 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
1455 return bt_ctf_trace_common_set_environment_field_integer(
1456 BT_CTF_TO_COMMON(trace
), name
, value
);
1459 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
1461 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace
));
1465 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
1468 return bt_ctf_trace_common_get_environment_field_name_by_index(
1469 BT_CTF_TO_COMMON(trace
), index
);
1472 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_index(
1473 struct bt_ctf_trace
*trace
, uint64_t index
)
1475 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1476 BT_CTF_TO_COMMON(trace
), index
));
1479 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_name(
1480 struct bt_ctf_trace
*trace
, const char *name
)
1482 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1483 BT_CTF_TO_COMMON(trace
), name
));
1487 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
1488 struct bt_ctf_clock_class
*clock_class
)
1490 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace
),
1491 (void *) clock_class
);
1495 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
1497 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace
));
1501 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
1502 struct bt_ctf_trace
*trace
, uint64_t index
)
1504 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index(
1505 BT_CTF_TO_COMMON(trace
), index
));
1509 int map_clock_classes_func(struct bt_ctf_stream_class_common
*stream_class
,
1510 struct bt_ctf_field_type_common
*packet_context_type
,
1511 struct bt_ctf_field_type_common
*event_header_type
)
1513 int ret
= bt_ctf_stream_class_map_clock_class(
1514 BT_CTF_FROM_COMMON(stream_class
),
1515 BT_CTF_FROM_COMMON(packet_context_type
),
1516 BT_CTF_FROM_COMMON(event_header_type
));
1519 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1525 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1526 struct bt_ctf_stream_class
*stream_class
)
1529 struct bt_ctf_clock_class
*expected_clock_class
= NULL
;
1532 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1537 if (!stream_class
) {
1538 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1543 if (stream_class
->clock
) {
1544 struct bt_ctf_clock_class
*stream_clock_class
=
1545 stream_class
->clock
->clock_class
;
1548 * Make sure this clock was also added to the
1549 * trace (potentially through its CTF writer
1554 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
1555 if (trace
->common
.clock_classes
->pdata
[i
] ==
1556 stream_clock_class
) {
1562 if (i
== trace
->common
.clock_classes
->len
) {
1564 BT_LOGW("Stream class's clock's class is not part of the trace: "
1565 "clock-class-addr=%p, clock-class-name=\"%s\"",
1567 bt_ctf_clock_class_get_name(stream_clock_class
));
1572 if (stream_class
->common
.clock_class
&&
1573 stream_class
->common
.clock_class
!=
1574 stream_class
->clock
->clock_class
) {
1576 * Stream class already has an expected clock
1577 * class, but it does not match its clock's
1580 BT_LOGW("Invalid parameter: stream class's clock's "
1581 "class does not match stream class's "
1582 "expected clock class: "
1583 "stream-class-addr=%p, "
1584 "stream-class-id=%" PRId64
", "
1585 "stream-class-name=\"%s\", "
1586 "expected-clock-class-addr=%p, "
1587 "expected-clock-class-name=\"%s\"",
1589 bt_ctf_stream_class_get_id(stream_class
),
1590 bt_ctf_stream_class_get_name(stream_class
),
1591 stream_class
->common
.clock_class
,
1592 bt_ctf_clock_class_get_name(stream_class
->common
.clock_class
));
1593 } else if (!stream_class
->common
.clock_class
) {
1595 * Set expected clock class to stream class's
1598 expected_clock_class
= stream_class
->clock
->clock_class
;
1603 ret
= bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace
),
1604 BT_CTF_TO_COMMON(stream_class
),
1605 (bt_ctf_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
1606 expected_clock_class
, map_clock_classes_func
,
1613 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1615 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace
));
1618 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1619 struct bt_ctf_trace
*trace
, uint64_t index
)
1621 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index(
1622 BT_CTF_TO_COMMON(trace
), index
));
1625 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1627 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace
));
1630 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1631 struct bt_ctf_trace
*trace
, uint64_t index
)
1633 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index(
1634 BT_CTF_TO_COMMON(trace
), index
));
1637 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1638 struct bt_ctf_trace
*trace
, uint64_t id
)
1640 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id(
1641 BT_CTF_TO_COMMON(trace
), id
));
1645 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1646 struct bt_ctf_trace
*trace
, const char *name
)
1648 return bt_ctf_object_get_ref(
1649 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace
),
1654 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1655 struct metadata_context
*context
)
1657 uint8_t *uuid
= trace
->common
.uuid
;
1660 if (trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1661 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1662 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1663 "set it with bt_ctf_trace_set_native_byte_order(): "
1664 "addr=%p, name=\"%s\"",
1665 trace
, bt_ctf_trace_get_name(trace
));
1670 g_string_append(context
->string
, "trace {\n");
1671 g_string_append(context
->string
, "\tmajor = 1;\n");
1672 g_string_append(context
->string
, "\tminor = 8;\n");
1673 BT_ASSERT_DBG(trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1674 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1675 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1677 if (trace
->common
.uuid_set
) {
1678 g_string_append_printf(context
->string
,
1679 "\tuuid = \"" BT_UUID_FMT
"\";\n",
1680 BT_UUID_FMT_VALUES(uuid
));
1683 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1684 bt_ctf_get_byte_order_string(trace
->common
.native_byte_order
));
1686 if (trace
->common
.packet_header_field_type
) {
1687 g_string_append(context
->string
, "\tpacket.header := ");
1688 context
->current_indentation_level
++;
1689 g_string_assign(context
->field_name
, "");
1690 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1691 ret
= bt_ctf_field_type_serialize_recursive(
1692 (void *) trace
->common
.packet_header_field_type
,
1697 context
->current_indentation_level
--;
1700 g_string_append(context
->string
, ";\n};\n\n");
1706 void append_env_metadata(struct bt_ctf_trace
*trace
,
1707 struct metadata_context
*context
)
1712 env_size
= bt_ctf_attributes_get_count(trace
->common
.environment
);
1713 if (env_size
<= 0) {
1717 g_string_append(context
->string
, "env {\n");
1719 for (i
= 0; i
< env_size
; i
++) {
1720 struct bt_ctf_private_value
*env_field_value_obj
= NULL
;
1721 const char *entry_name
;
1723 entry_name
= bt_ctf_attributes_get_field_name(
1724 trace
->common
.environment
, i
);
1725 env_field_value_obj
= bt_ctf_attributes_borrow_field_value(
1726 trace
->common
.environment
, i
);
1728 BT_ASSERT_DBG(entry_name
);
1729 BT_ASSERT_DBG(env_field_value_obj
);
1731 switch (bt_ctf_value_get_type(
1732 bt_ctf_private_value_as_value(env_field_value_obj
))) {
1733 case BT_CTF_VALUE_TYPE_INTEGER
:
1737 int_value
= bt_ctf_value_integer_get(
1738 bt_ctf_private_value_as_value(
1739 env_field_value_obj
));
1740 g_string_append_printf(context
->string
,
1741 "\t%s = %" PRId64
";\n", entry_name
,
1745 case BT_CTF_VALUE_TYPE_STRING
:
1747 const char *str_value
;
1748 char *escaped_str
= NULL
;
1750 str_value
= bt_ctf_value_string_get(
1751 bt_ctf_private_value_as_value(
1752 env_field_value_obj
));
1753 escaped_str
= g_strescape(str_value
, NULL
);
1755 BT_LOGE("Cannot escape string: string=\"%s\"",
1760 g_string_append_printf(context
->string
,
1761 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1770 g_string_append(context
->string
, "};\n\n");
1773 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1775 char *metadata
= NULL
;
1776 struct metadata_context
*context
= NULL
;
1781 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1785 context
= g_new0(struct metadata_context
, 1);
1787 BT_LOGE_STR("Failed to allocate one metadata context.");
1791 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1792 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1793 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1794 if (append_trace_metadata(trace
, context
)) {
1795 /* append_trace_metadata() logs errors */
1798 append_env_metadata(trace
, context
);
1799 g_ptr_array_foreach(trace
->common
.clock_classes
,
1800 (GFunc
) bt_ctf_clock_class_serialize
, context
);
1802 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
1803 /* bt_ctf_stream_class_serialize() logs details */
1804 err
= bt_ctf_stream_class_serialize(
1805 trace
->common
.stream_classes
->pdata
[i
], context
);
1807 /* bt_ctf_stream_class_serialize() logs errors */
1812 metadata
= context
->string
->str
;
1815 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1816 g_string_free(context
->field_name
, TRUE
);
1823 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1824 struct bt_ctf_trace
*trace
)
1826 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace
));
1829 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1830 enum bt_ctf_byte_order byte_order
)
1832 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace
),
1833 (int) byte_order
, false);
1836 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
1837 struct bt_ctf_trace
*trace
)
1839 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type(
1840 BT_CTF_TO_COMMON(trace
)));
1843 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
1844 struct bt_ctf_field_type
*packet_header_type
)
1846 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace
),
1847 (void *) packet_header_type
);
1850 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
1852 return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace
));