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
46 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
47 bt_ctf_object_release_func release_func
)
51 BT_LOGD_STR("Initializing common trace object.");
52 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_UNSPECIFIED
;
53 bt_ctf_object_init_shared_with_parent(&trace
->base
, release_func
);
54 trace
->clock_classes
= g_ptr_array_new_with_free_func(
55 (GDestroyNotify
) bt_ctf_object_put_ref
);
56 if (!trace
->clock_classes
) {
57 BT_LOGE_STR("Failed to allocate one GPtrArray.");
61 trace
->streams
= g_ptr_array_new_with_free_func(
62 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
63 if (!trace
->streams
) {
64 BT_LOGE_STR("Failed to allocate one GPtrArray.");
68 trace
->stream_classes
= g_ptr_array_new_with_free_func(
69 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
70 if (!trace
->stream_classes
) {
71 BT_LOGE_STR("Failed to allocate one GPtrArray.");
75 /* Create the environment array object */
76 trace
->environment
= bt_ctf_attributes_create();
77 if (!trace
->environment
) {
78 BT_LOGE_STR("Cannot create empty attributes object.");
82 BT_LOGD("Initialized common trace object: addr=%p", trace
);
92 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
)
94 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
95 trace
, bt_ctf_trace_common_get_name(trace
));
97 if (trace
->environment
) {
98 BT_LOGD_STR("Destroying environment attributes.");
99 bt_ctf_attributes_destroy(trace
->environment
);
103 g_string_free(trace
->name
, TRUE
);
106 if (trace
->clock_classes
) {
107 BT_LOGD_STR("Putting clock classes.");
108 g_ptr_array_free(trace
->clock_classes
, TRUE
);
111 if (trace
->streams
) {
112 BT_LOGD_STR("Destroying streams.");
113 g_ptr_array_free(trace
->streams
, TRUE
);
116 if (trace
->stream_classes
) {
117 BT_LOGD_STR("Destroying stream classes.");
118 g_ptr_array_free(trace
->stream_classes
, TRUE
);
121 BT_LOGD_STR("Putting packet header field type.");
122 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
125 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
)
130 BT_LOGW_STR("Invalid parameter: trace is NULL.");
136 BT_LOGW_STR("Invalid parameter: name is NULL.");
142 BT_LOGW("Invalid parameter: trace is frozen: "
143 "addr=%p, name=\"%s\"",
144 trace
, bt_ctf_trace_common_get_name(trace
));
149 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
152 BT_LOGE_STR("Failed to allocate one GString.");
157 BT_LOGT("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
163 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
,
169 BT_LOGW_STR("Invalid parameter: trace is NULL.");
175 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
181 BT_LOGW("Invalid parameter: trace is frozen: "
182 "addr=%p, name=\"%s\"",
183 trace
, bt_ctf_trace_common_get_name(trace
));
188 bt_uuid_copy(trace
->uuid
, uuid
);
189 trace
->uuid_set
= BT_CTF_TRUE
;
190 BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", "
191 "uuid=\"" BT_UUID_FMT
"\"",
192 trace
, bt_ctf_trace_common_get_name(trace
),
193 BT_UUID_FMT_VALUES(uuid
));
199 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
200 const char *name
, struct bt_ctf_private_value
*value
)
205 BT_LOGW_STR("Invalid parameter: trace is NULL.");
211 BT_LOGW_STR("Invalid parameter: name is NULL.");
217 BT_LOGW_STR("Invalid parameter: value is NULL.");
222 if (!bt_ctf_identifier_is_valid(name
)) {
223 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
224 "trace-addr=%p, trace-name=\"%s\", "
226 trace
, bt_ctf_trace_common_get_name(trace
), name
);
231 if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value
)) &&
232 !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value
))) {
233 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
234 "trace-addr=%p, trace-name=\"%s\", "
235 "env-name=\"%s\", env-value-type=%s",
236 trace
, bt_ctf_trace_common_get_name(trace
), name
,
237 bt_ctf_value_type_string(
238 bt_ctf_value_get_type(
239 bt_ctf_private_value_as_value(value
))));
246 * New environment fields may be added to a frozen trace,
247 * but existing fields may not be changed.
249 * The object passed is frozen like all other attributes.
251 struct bt_ctf_private_value
*attribute
=
252 bt_ctf_attributes_borrow_field_value_by_name(
253 trace
->environment
, name
);
256 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
257 "trace-addr=%p, trace-name=\"%s\", "
259 trace
, bt_ctf_trace_common_get_name(trace
), name
);
264 bt_ctf_value_freeze(bt_ctf_private_value_as_value(value
));
267 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
270 BT_LOGE("Cannot set environment field's value: "
271 "trace-addr=%p, trace-name=\"%s\", "
273 trace
, bt_ctf_trace_common_get_name(trace
), name
);
275 BT_LOGT("Set environment field's value: "
276 "trace-addr=%p, trace-name=\"%s\", "
277 "env-name=\"%s\", value-addr=%p",
278 trace
, bt_ctf_trace_common_get_name(trace
), name
, value
);
285 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
286 const char *name
, const char *value
)
289 struct bt_ctf_private_value
*env_value_string_obj
= NULL
;
292 BT_LOGW_STR("Invalid parameter: value is NULL.");
297 env_value_string_obj
= bt_ctf_private_value_string_create_init(value
);
298 if (!env_value_string_obj
) {
299 BT_LOGE_STR("Cannot create string value object.");
304 /* bt_ctf_trace_common_set_environment_field() logs errors */
305 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
306 env_value_string_obj
);
309 bt_ctf_object_put_ref(env_value_string_obj
);
313 int bt_ctf_trace_common_set_environment_field_integer(
314 struct bt_ctf_trace_common
*trace
, const char *name
, int64_t value
)
317 struct bt_ctf_private_value
*env_value_integer_obj
= NULL
;
319 env_value_integer_obj
= bt_ctf_private_value_integer_create_init(value
);
320 if (!env_value_integer_obj
) {
321 BT_LOGE_STR("Cannot create integer value object.");
326 /* bt_ctf_trace_common_set_environment_field() logs errors */
327 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
328 env_value_integer_obj
);
331 bt_ctf_object_put_ref(env_value_integer_obj
);
335 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
336 struct bt_ctf_clock_class
*clock_class
)
341 BT_LOGW_STR("Invalid parameter: trace is NULL.");
346 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
347 BT_LOGW("Invalid parameter: clock class is invalid: "
348 "trace-addr=%p, trace-name=\"%s\", "
349 "clock-class-addr=%p, clock-class-name=\"%s\"",
350 trace
, bt_ctf_trace_common_get_name(trace
),
351 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
356 /* Check for duplicate clock classes */
357 if (bt_ctf_trace_common_has_clock_class(trace
, clock_class
)) {
358 BT_LOGW("Invalid parameter: clock class already exists in trace: "
359 "trace-addr=%p, trace-name=\"%s\", "
360 "clock-class-addr=%p, clock-class-name=\"%s\"",
361 trace
, bt_ctf_trace_common_get_name(trace
),
362 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
367 bt_ctf_object_get_ref(clock_class
);
368 g_ptr_array_add(trace
->clock_classes
, clock_class
);
371 BT_LOGT_STR("Freezing added clock class because trace is frozen.");
372 bt_ctf_clock_class_freeze(clock_class
);
375 BT_LOGT("Added clock class to trace: "
376 "trace-addr=%p, trace-name=\"%s\", "
377 "clock-class-addr=%p, clock-class-name=\"%s\"",
378 trace
, bt_ctf_trace_common_get_name(trace
),
379 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
386 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
387 struct bt_ctf_field_type_common
*packet_header_type
)
390 bool is_valid
= true;
391 struct bt_ctf_field_type_common
*field_type
= NULL
;
393 if (!packet_header_type
) {
395 * No packet header field type: trace must have only
396 * one stream. At this point the stream class being
397 * added is not part of the trace yet, so we validate
398 * that the trace contains no stream classes yet.
400 if (trace
->stream_classes
->len
>= 1) {
401 BT_LOGW_STR("Invalid packet header field type: "
402 "packet header field type does not exist but there's more than one stream class in the trace.");
406 /* No packet header field type: valid at this point */
410 /* Packet header field type, if it exists, must be a structure */
411 if (packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
412 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
413 "ft-addr=%p, ft-id=%s",
415 bt_ctf_field_type_id_string(packet_header_type
->id
));
420 * If there's a `magic` field, it must be a 32-bit unsigned
421 * integer field type. Also it must be the first field of the
422 * packet header field type.
424 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
425 packet_header_type
, "magic");
427 const char *field_name
;
429 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
430 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
431 "magic-ft-addr=%p, magic-ft-id=%s",
433 bt_ctf_field_type_id_string(field_type
->id
));
437 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
438 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
439 "magic-ft-addr=%p", field_type
);
443 if (bt_ctf_field_type_common_integer_get_size(field_type
) != 32) {
444 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
445 "magic-ft-addr=%p, magic-ft-size=%u",
447 bt_ctf_field_type_common_integer_get_size(field_type
));
451 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
452 packet_header_type
, &field_name
, NULL
, 0);
453 BT_ASSERT_DBG(ret
== 0);
455 if (strcmp(field_name
, "magic") != 0) {
456 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
457 "magic-ft-addr=%p, first-field-name=\"%s\"",
458 field_type
, field_name
);
464 * If there's a `uuid` field, it must be an array field type of
465 * length 16 with an 8-bit unsigned integer element field type.
467 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
468 packet_header_type
, "uuid");
470 struct bt_ctf_field_type_common
*elem_ft
;
472 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
473 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
474 "uuid-ft-addr=%p, uuid-ft-id=%s",
476 bt_ctf_field_type_id_string(field_type
->id
));
480 if (bt_ctf_field_type_common_array_get_length(field_type
) != 16) {
481 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
482 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
484 bt_ctf_field_type_common_array_get_length(field_type
));
488 elem_ft
= bt_ctf_field_type_common_array_borrow_element_field_type(field_type
);
489 BT_ASSERT_DBG(elem_ft
);
491 if (elem_ft
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
492 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
493 "elem-ft-addr=%p, elem-ft-id=%s",
495 bt_ctf_field_type_id_string(elem_ft
->id
));
499 if (bt_ctf_field_type_common_integer_is_signed(elem_ft
)) {
500 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
501 "elem-ft-addr=%p", elem_ft
);
505 if (bt_ctf_field_type_common_integer_get_size(elem_ft
) != 8) {
506 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
507 "elem-ft-addr=%p, elem-ft-size=%u",
509 bt_ctf_field_type_common_integer_get_size(elem_ft
));
515 * The `stream_id` field must exist if there's more than one
516 * stream classes in the trace.
518 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
519 packet_header_type
, "stream_id");
521 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
522 BT_LOGW_STR("Invalid packet header field type: "
523 "`stream_id` field does not exist but there's more than one stream class in the trace.");
528 * If there's a `stream_id` field, it must be an unsigned
529 * integer field type.
532 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
533 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
534 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
536 bt_ctf_field_type_id_string(field_type
->id
));
540 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
541 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
542 "stream-id-ft-addr=%p", field_type
);
548 * If there's a `packet_seq_num` field, it must be an unsigned
549 * integer field type.
551 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
552 packet_header_type
, "packet_seq_num");
554 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
555 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
556 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
558 bt_ctf_field_type_id_string(field_type
->id
));
562 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
563 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
564 "packet-seq-num-ft-addr=%p", field_type
);
579 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
580 struct bt_ctf_stream_class_common
*stream_class
,
581 struct bt_ctf_field_type_common
*packet_context_type
,
582 bool check_ts_begin_end_mapped
)
584 bool is_valid
= true;
585 struct bt_ctf_field_type_common
*field_type
= NULL
;
587 if (!packet_context_type
) {
588 /* No packet context field type: valid at this point */
592 /* Packet context field type, if it exists, must be a structure */
593 if (packet_context_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
594 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
595 "ft-addr=%p, ft-id=%s",
597 bt_ctf_field_type_id_string(packet_context_type
->id
));
602 * If there's a `packet_size` field, it must be an unsigned
603 * integer field type.
605 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
606 packet_context_type
, "packet_size");
608 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
609 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
610 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
612 bt_ctf_field_type_id_string(field_type
->id
));
616 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
617 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
618 "packet-size-ft-addr=%p", field_type
);
624 * If there's a `content_size` field, it must be an unsigned
625 * integer field type.
627 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
628 packet_context_type
, "content_size");
630 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
631 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
632 "content-size-ft-addr=%p, content-size-ft-id=%s",
634 bt_ctf_field_type_id_string(field_type
->id
));
638 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
639 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
640 "content-size-ft-addr=%p", field_type
);
646 * If there's a `events_discarded` field, it must be an unsigned
647 * integer field type.
649 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
650 packet_context_type
, "events_discarded");
652 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
653 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
654 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
656 bt_ctf_field_type_id_string(field_type
->id
));
660 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
661 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
662 "events-discarded-ft-addr=%p", field_type
);
668 * If there's a `timestamp_begin` field, it must be an unsigned
669 * integer field type. Also, if the trace is not a CTF writer's
670 * trace, then we cannot automatically set the mapped clock
671 * class of this field, so it must have a mapped clock class.
673 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
674 packet_context_type
, "timestamp_begin");
676 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
677 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
678 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
680 bt_ctf_field_type_id_string(field_type
->id
));
684 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
685 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
686 "timestamp-begin-ft-addr=%p", field_type
);
690 if (check_ts_begin_end_mapped
) {
691 struct bt_ctf_clock_class
*clock_class
=
692 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
696 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
697 "timestamp-begin-ft-addr=%p", field_type
);
704 * If there's a `timestamp_end` field, it must be an unsigned
705 * integer field type. Also, if the trace is not a CTF writer's
706 * trace, then we cannot automatically set the mapped clock
707 * class of this field, so it must have a mapped clock class.
709 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
710 packet_context_type
, "timestamp_end");
712 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
713 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
714 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
716 bt_ctf_field_type_id_string(field_type
->id
));
720 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
721 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
722 "timestamp-end-ft-addr=%p", field_type
);
726 if (check_ts_begin_end_mapped
) {
727 struct bt_ctf_clock_class
*clock_class
=
728 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
732 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
733 "timestamp-end-ft-addr=%p", field_type
);
749 bool event_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
750 struct bt_ctf_stream_class_common
*stream_class
,
751 struct bt_ctf_field_type_common
*event_header_type
)
753 bool is_valid
= true;
754 struct bt_ctf_field_type_common
*field_type
= NULL
;
757 * We do not validate that the `timestamp` field exists here
758 * because CTF does not require this exact name to be mapped to
762 if (!event_header_type
) {
764 * No event header field type: stream class must have
765 * only one event class.
767 if (bt_ctf_stream_class_common_get_event_class_count(stream_class
) > 1) {
768 BT_LOGW_STR("Invalid event header field type: "
769 "event header field type does not exist but there's more than one event class in the stream class.");
773 /* No event header field type: valid at this point */
777 /* Event header field type, if it exists, must be a structure */
778 if (event_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
779 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
780 "ft-addr=%p, ft-id=%s",
782 bt_ctf_field_type_id_string(event_header_type
->id
));
787 * If there's an `id` field, it must be an unsigned integer
788 * field type or an enumeration field type with an unsigned
789 * integer container field type.
791 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
792 event_header_type
, "id");
794 struct bt_ctf_field_type_common
*int_ft
;
796 if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
798 } else if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
799 int_ft
= bt_ctf_field_type_common_enumeration_borrow_container_field_type(
802 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
803 "id-ft-addr=%p, id-ft-id=%s",
805 bt_ctf_field_type_id_string(field_type
->id
));
809 BT_ASSERT_DBG(int_ft
);
810 if (bt_ctf_field_type_common_integer_is_signed(int_ft
)) {
811 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
812 "id-ft-addr=%p", int_ft
);
827 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common
*trace
)
831 if (trace
->packet_header_field_type
) {
832 struct bt_ctf_clock_class
*clock_class
= NULL
;
834 ret
= bt_ctf_field_type_common_validate_single_clock_class(
835 trace
->packet_header_field_type
,
837 bt_ctf_object_put_ref(clock_class
);
838 if (ret
|| clock_class
) {
839 BT_LOGW("Trace's packet header field type cannot "
840 "contain a field type which is mapped to "
842 "trace-addr=%p, trace-name=\"%s\", "
843 "clock-class-name=\"%s\"",
844 trace
, bt_ctf_trace_common_get_name(trace
),
846 bt_ctf_clock_class_get_name(clock_class
) :
855 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
856 struct bt_ctf_stream_class_common
*stream_class
,
857 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
858 struct bt_ctf_clock_class
*init_expected_clock_class
,
859 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
860 struct bt_ctf_field_type_common
*packet_context_field_type
,
861 struct bt_ctf_field_type_common
*event_header_field_type
),
862 bool check_ts_begin_end_mapped
)
867 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
868 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
869 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
870 BT_CTF_VALIDATION_FLAG_TRACE
|
871 BT_CTF_VALIDATION_FLAG_STREAM
;
872 const enum bt_ctf_validation_flag ec_validation_flags
=
873 BT_CTF_VALIDATION_FLAG_EVENT
;
874 struct bt_ctf_field_type_common
*packet_header_type
= NULL
;
875 struct bt_ctf_field_type_common
*packet_context_type
= NULL
;
876 struct bt_ctf_field_type_common
*event_header_type
= NULL
;
877 struct bt_ctf_field_type_common
*stream_event_ctx_type
= NULL
;
878 int64_t event_class_count
= 0;
879 struct bt_ctf_trace_common
*current_parent_trace
= NULL
;
880 struct bt_ctf_clock_class
*expected_clock_class
=
881 bt_ctf_object_get_ref(init_expected_clock_class
);
883 BT_ASSERT_DBG(copy_field_type_func
);
886 BT_LOGW_STR("Invalid parameter: trace is NULL.");
892 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
897 BT_LOGD("Adding stream class to trace: "
898 "trace-addr=%p, trace-name=\"%s\", "
899 "stream-class-addr=%p, stream-class-name=\"%s\", "
900 "stream-class-id=%" PRId64
,
901 trace
, bt_ctf_trace_common_get_name(trace
),
902 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
903 bt_ctf_stream_class_common_get_id(stream_class
));
905 current_parent_trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
906 if (current_parent_trace
) {
907 /* Stream class is already associated to a trace, abort. */
908 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
909 "stream-class-trace-addr=%p, "
910 "stream-class-trace-name=\"%s\"",
911 current_parent_trace
,
912 bt_ctf_trace_common_get_name(current_parent_trace
));
918 bt_ctf_stream_class_common_get_event_class_count(stream_class
);
919 BT_ASSERT_DBG(event_class_count
>= 0);
921 if (!stream_class
->frozen
) {
923 * Stream class is not frozen yet. Validate that the
924 * stream class contains at most a single clock class
925 * because the previous
926 * bt_ctf_stream_class_common_add_event_class() calls did
927 * not make this validation since the stream class's
928 * direct field types (packet context, event header,
929 * event context) could change afterwards. This stream
930 * class is about to be frozen and those field types
931 * won't be changed if this function succeeds.
933 * At this point we're also sure that the stream class's
934 * clock, if any, has the same class as the stream
935 * class's expected clock class, if any. This is why, if
936 * bt_ctf_stream_class_common_validate_single_clock_class()
937 * succeeds below, the call to
938 * bt_ctf_stream_class_map_clock_class() at the end of this
939 * function is safe because it maps to the same, single
942 ret
= bt_ctf_stream_class_common_validate_single_clock_class(
943 stream_class
, &expected_clock_class
);
945 BT_LOGW("Invalid parameter: stream class or one of its "
946 "event classes contains a field type which is "
947 "not recursively mapped to the expected "
949 "stream-class-addr=%p, "
950 "stream-class-id=%" PRId64
", "
951 "stream-class-name=\"%s\", "
952 "expected-clock-class-addr=%p, "
953 "expected-clock-class-name=\"%s\"",
954 stream_class
, bt_ctf_stream_class_common_get_id(stream_class
),
955 bt_ctf_stream_class_common_get_name(stream_class
),
956 expected_clock_class
,
957 expected_clock_class
?
958 bt_ctf_clock_class_get_name(expected_clock_class
) :
964 ret
= check_packet_header_type_has_no_clock_class(trace
);
966 /* check_packet_header_type_has_no_clock_class() logs errors */
971 * We're about to freeze both the trace and the stream class.
972 * Also, each event class contained in this stream class are
975 * This trace, this stream class, and all its event classes
976 * should be valid at this point.
978 * Validate trace and stream class first, then each event
979 * class of this stream class can be validated individually.
982 bt_ctf_trace_common_borrow_packet_header_field_type(trace
);
983 packet_context_type
=
984 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class
);
986 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class
);
987 stream_event_ctx_type
=
988 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class
);
990 BT_LOGD("Validating trace and stream class field types.");
991 ret
= bt_ctf_validate_class_types(trace
->environment
,
992 packet_header_type
, packet_context_type
, event_header_type
,
993 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
994 stream_class
->valid
, 1, &trace_sc_validation_output
,
995 trace_sc_validation_flags
, copy_field_type_func
);
999 * This means something went wrong during the validation
1000 * process, not that the objects are invalid.
1002 BT_LOGE("Failed to validate trace and stream class field types: "
1007 if ((trace_sc_validation_output
.valid_flags
&
1008 trace_sc_validation_flags
) !=
1009 trace_sc_validation_flags
) {
1010 /* Invalid trace/stream class */
1011 BT_LOGW("Invalid trace or stream class field types: "
1013 trace_sc_validation_output
.valid_flags
);
1018 if (event_class_count
> 0) {
1019 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1021 if (!ec_validation_outputs
) {
1022 BT_LOGE_STR("Failed to allocate one validation output structure.");
1028 /* Validate each event class individually */
1029 for (i
= 0; i
< event_class_count
; i
++) {
1030 struct bt_ctf_event_class_common
*event_class
=
1031 bt_ctf_stream_class_common_borrow_event_class_by_index(
1033 struct bt_ctf_field_type_common
*event_context_type
= NULL
;
1034 struct bt_ctf_field_type_common
*event_payload_type
= NULL
;
1036 event_context_type
=
1037 bt_ctf_event_class_common_borrow_context_field_type(
1039 event_payload_type
=
1040 bt_ctf_event_class_common_borrow_payload_field_type(
1044 * It is important to use the field types returned by
1045 * the previous trace and stream class validation here
1046 * because copies could have been made.
1048 BT_LOGD("Validating event class's field types: "
1049 "addr=%p, name=\"%s\", id=%" PRId64
,
1050 event_class
, bt_ctf_event_class_common_get_name(event_class
),
1051 bt_ctf_event_class_common_get_id(event_class
));
1052 ret
= bt_ctf_validate_class_types(trace
->environment
,
1053 trace_sc_validation_output
.packet_header_type
,
1054 trace_sc_validation_output
.packet_context_type
,
1055 trace_sc_validation_output
.event_header_type
,
1056 trace_sc_validation_output
.stream_event_ctx_type
,
1057 event_context_type
, event_payload_type
,
1058 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1059 ec_validation_flags
, copy_field_type_func
);
1062 BT_LOGE("Failed to validate event class field types: "
1067 if ((ec_validation_outputs
[i
].valid_flags
&
1068 ec_validation_flags
) != ec_validation_flags
) {
1069 /* Invalid event class */
1070 BT_LOGW("Invalid event class field types: "
1072 ec_validation_outputs
[i
].valid_flags
);
1078 stream_id
= bt_ctf_stream_class_common_get_id(stream_class
);
1079 if (stream_id
< 0) {
1080 stream_id
= trace
->next_stream_id
++;
1081 if (stream_id
< 0) {
1082 BT_LOGE_STR("No more stream class IDs available.");
1087 /* Try to assign a new stream id */
1088 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1089 if (stream_id
== bt_ctf_stream_class_common_get_id(
1090 trace
->stream_classes
->pdata
[i
])) {
1091 /* Duplicate stream id found */
1092 BT_LOGW("Duplicate stream class ID: "
1093 "id=%" PRId64
, (int64_t) stream_id
);
1099 if (bt_ctf_stream_class_common_set_id_no_check(stream_class
,
1101 /* TODO Should retry with a different stream id */
1102 BT_LOGE("Cannot set stream class's ID: "
1103 "id=%" PRId64
, (int64_t) stream_id
);
1110 * At this point all the field types in the validation output
1111 * are valid. Validate the semantics of some scopes according to
1112 * the CTF specification.
1114 if (!packet_header_field_type_is_valid(trace
,
1115 trace_sc_validation_output
.packet_header_type
)) {
1116 BT_LOGW_STR("Invalid trace's packet header field type.");
1121 if (!packet_context_field_type_is_valid(trace
,
1123 trace_sc_validation_output
.packet_context_type
,
1124 check_ts_begin_end_mapped
)) {
1125 BT_LOGW_STR("Invalid stream class's packet context field type.");
1130 if (!event_header_field_type_is_valid(trace
,
1132 trace_sc_validation_output
.event_header_type
)) {
1133 BT_LOGW_STR("Invalid steam class's event header field type.");
1139 * Now is the time to automatically map specific field types of
1140 * the stream class's packet context and event header field
1141 * types to the stream class's clock's class if they are not
1142 * mapped to a clock class yet. We do it here because we know
1143 * that after this point, everything is frozen so it won't be
1144 * possible for the user to modify the stream class's clock, or
1145 * to map those field types to other clock classes.
1147 if (map_clock_classes_func
) {
1148 if (map_clock_classes_func(stream_class
,
1149 trace_sc_validation_output
.packet_context_type
,
1150 trace_sc_validation_output
.event_header_type
)) {
1151 /* map_clock_classes_func() logs errors */
1157 bt_ctf_object_set_parent(&stream_class
->base
, &trace
->base
);
1158 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1161 * At this point we know that the function will be successful.
1162 * Therefore we can replace the trace and stream class field
1163 * types with what's in their validation output structure and
1164 * mark them as valid. We can also replace the field types of
1165 * all the event classes of the stream class and mark them as
1168 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1169 &trace_sc_validation_output
, trace_sc_validation_flags
);
1171 stream_class
->valid
= 1;
1174 * Put what was not moved in bt_ctf_validation_replace_types().
1176 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1178 for (i
= 0; i
< event_class_count
; i
++) {
1179 struct bt_ctf_event_class_common
*event_class
=
1180 bt_ctf_stream_class_common_borrow_event_class_by_index(
1183 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1184 &ec_validation_outputs
[i
], ec_validation_flags
);
1185 event_class
->valid
= 1;
1188 * Put what was not moved in
1189 * bt_ctf_validation_replace_types().
1191 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1195 * Freeze the trace and the stream class.
1197 bt_ctf_stream_class_common_freeze(stream_class
);
1198 bt_ctf_trace_common_freeze(trace
);
1201 * It is safe to set the stream class's unique clock class
1202 * now because the stream class is frozen.
1204 if (expected_clock_class
) {
1205 BT_CTF_OBJECT_MOVE_REF(stream_class
->clock_class
, expected_clock_class
);
1208 BT_LOGD("Added stream class to trace: "
1209 "trace-addr=%p, trace-name=\"%s\", "
1210 "stream-class-addr=%p, stream-class-name=\"%s\", "
1211 "stream-class-id=%" PRId64
,
1212 trace
, bt_ctf_trace_common_get_name(trace
),
1213 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
1214 bt_ctf_stream_class_common_get_id(stream_class
));
1219 bt_ctf_object_set_parent(&stream_class
->base
, NULL
);
1222 if (ec_validation_outputs
) {
1223 for (i
= 0; i
< event_class_count
; i
++) {
1224 bt_ctf_validation_output_put_types(
1225 &ec_validation_outputs
[i
]);
1230 g_free(ec_validation_outputs
);
1231 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1232 bt_ctf_object_put_ref(expected_clock_class
);
1236 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
1237 struct bt_ctf_clock_class
*clock_class
)
1239 struct bt_ctf_search_query query
= { .value
= clock_class
, .found
= 0 };
1241 BT_ASSERT_DBG(trace
);
1242 BT_ASSERT_DBG(clock_class
);
1244 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1248 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
1249 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
)
1254 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1259 if (trace
->frozen
) {
1260 BT_LOGW("Invalid parameter: trace is frozen: "
1261 "addr=%p, name=\"%s\"",
1262 trace
, bt_ctf_trace_common_get_name(trace
));
1267 if (byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1268 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1269 "addr=%p, name=\"%s\"",
1270 trace
, bt_ctf_trace_common_get_name(trace
));
1275 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1276 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1277 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1278 BT_LOGW("Invalid parameter: invalid byte order: "
1279 "addr=%p, name=\"%s\", bo=%s",
1280 trace
, bt_ctf_trace_common_get_name(trace
),
1281 bt_ctf_byte_order_string(byte_order
));
1286 trace
->native_byte_order
= byte_order
;
1287 BT_LOGT("Set trace's native 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
));
1296 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
1297 struct bt_ctf_field_type_common
*packet_header_type
)
1302 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1307 if (trace
->frozen
) {
1308 BT_LOGW("Invalid parameter: trace is frozen: "
1309 "addr=%p, name=\"%s\"",
1310 trace
, bt_ctf_trace_common_get_name(trace
));
1315 /* packet_header_type must be a structure. */
1316 if (packet_header_type
&&
1317 packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1318 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1319 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1320 trace
, bt_ctf_trace_common_get_name(trace
),
1322 bt_ctf_field_type_id_string(packet_header_type
->id
));
1327 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
1328 trace
->packet_header_field_type
= bt_ctf_object_get_ref(packet_header_type
);
1329 BT_LOGT("Set trace's packet header field type: "
1330 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1331 trace
, bt_ctf_trace_common_get_name(trace
), packet_header_type
);
1337 int64_t get_stream_class_count(void *element
)
1339 return bt_ctf_trace_get_stream_class_count(
1340 (struct bt_ctf_trace
*) element
);
1344 void *get_stream_class(void *element
, int i
)
1346 return bt_ctf_trace_get_stream_class_by_index(
1347 (struct bt_ctf_trace
*) element
, i
);
1351 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1353 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1356 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1357 bt_ctf_visitor visitor
, void *data
)
1360 struct bt_ctf_visitor_object obj
= {
1362 .type
= BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1366 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1372 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1377 BT_LOGT("Visiting trace: addr=%p, name=\"%s\"",
1378 trace
, bt_ctf_trace_get_name(trace
));
1379 ret
= bt_ctf_visitor_helper(&obj
, get_stream_class_count
,
1380 get_stream_class
, visit_stream_class
, visitor
, data
);
1386 void bt_ctf_trace_destroy(struct bt_ctf_object
*obj
)
1388 struct bt_ctf_trace
*trace
= (void *) obj
;
1390 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1391 trace
, bt_ctf_trace_get_name(trace
));
1392 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace
));
1396 struct bt_ctf_trace
*bt_ctf_trace_create(void)
1398 struct bt_ctf_trace
*trace
= NULL
;
1401 BT_LOGD_STR("Creating CTF writer trace object.");
1402 trace
= g_new0(struct bt_ctf_trace
, 1);
1404 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1408 ret
= bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace
),
1409 bt_ctf_trace_destroy
);
1411 /* bt_ctf_trace_common_initialize() logs errors */
1415 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
1419 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1424 const uint8_t *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
1426 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace
));
1430 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
1431 const uint8_t *uuid
)
1433 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace
), uuid
);
1437 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
1438 const char *name
, const char *value
)
1440 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace
),
1445 int bt_ctf_trace_set_environment_field_integer(
1446 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
1448 return bt_ctf_trace_common_set_environment_field_integer(
1449 BT_CTF_TO_COMMON(trace
), name
, value
);
1452 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
1454 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace
));
1458 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
1461 return bt_ctf_trace_common_get_environment_field_name_by_index(
1462 BT_CTF_TO_COMMON(trace
), index
);
1465 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_index(
1466 struct bt_ctf_trace
*trace
, uint64_t index
)
1468 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1469 BT_CTF_TO_COMMON(trace
), index
));
1472 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_name(
1473 struct bt_ctf_trace
*trace
, const char *name
)
1475 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1476 BT_CTF_TO_COMMON(trace
), name
));
1479 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
1480 struct bt_ctf_clock_class
*clock_class
)
1482 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace
),
1483 (void *) clock_class
);
1486 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
1488 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace
));
1491 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
1492 struct bt_ctf_trace
*trace
, uint64_t index
)
1494 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index(
1495 BT_CTF_TO_COMMON(trace
), index
));
1499 int map_clock_classes_func(struct bt_ctf_stream_class_common
*stream_class
,
1500 struct bt_ctf_field_type_common
*packet_context_type
,
1501 struct bt_ctf_field_type_common
*event_header_type
)
1503 int ret
= bt_ctf_stream_class_map_clock_class(
1504 BT_CTF_FROM_COMMON(stream_class
),
1505 BT_CTF_FROM_COMMON(packet_context_type
),
1506 BT_CTF_FROM_COMMON(event_header_type
));
1509 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1516 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1517 struct bt_ctf_stream_class
*stream_class
)
1520 struct bt_ctf_clock_class
*expected_clock_class
= NULL
;
1523 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1528 if (!stream_class
) {
1529 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1534 if (stream_class
->clock
) {
1535 struct bt_ctf_clock_class
*stream_clock_class
=
1536 stream_class
->clock
->clock_class
;
1539 * Make sure this clock was also added to the
1540 * trace (potentially through its CTF writer
1545 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
1546 if (trace
->common
.clock_classes
->pdata
[i
] ==
1547 stream_clock_class
) {
1553 if (i
== trace
->common
.clock_classes
->len
) {
1555 BT_LOGW("Stream class's clock's class is not part of the trace: "
1556 "clock-class-addr=%p, clock-class-name=\"%s\"",
1558 bt_ctf_clock_class_get_name(stream_clock_class
));
1563 if (stream_class
->common
.clock_class
&&
1564 stream_class
->common
.clock_class
!=
1565 stream_class
->clock
->clock_class
) {
1567 * Stream class already has an expected clock
1568 * class, but it does not match its clock's
1571 BT_LOGW("Invalid parameter: stream class's clock's "
1572 "class does not match stream class's "
1573 "expected clock class: "
1574 "stream-class-addr=%p, "
1575 "stream-class-id=%" PRId64
", "
1576 "stream-class-name=\"%s\", "
1577 "expected-clock-class-addr=%p, "
1578 "expected-clock-class-name=\"%s\"",
1580 bt_ctf_stream_class_get_id(stream_class
),
1581 bt_ctf_stream_class_get_name(stream_class
),
1582 stream_class
->common
.clock_class
,
1583 bt_ctf_clock_class_get_name(stream_class
->common
.clock_class
));
1584 } else if (!stream_class
->common
.clock_class
) {
1586 * Set expected clock class to stream class's
1589 expected_clock_class
= stream_class
->clock
->clock_class
;
1594 ret
= bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace
),
1595 BT_CTF_TO_COMMON(stream_class
),
1596 (bt_ctf_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
1597 expected_clock_class
, map_clock_classes_func
,
1605 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1607 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace
));
1611 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1612 struct bt_ctf_trace
*trace
, uint64_t index
)
1614 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index(
1615 BT_CTF_TO_COMMON(trace
), index
));
1619 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1621 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace
));
1625 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1626 struct bt_ctf_trace
*trace
, uint64_t index
)
1628 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index(
1629 BT_CTF_TO_COMMON(trace
), index
));
1633 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1634 struct bt_ctf_trace
*trace
, uint64_t id
)
1636 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id(
1637 BT_CTF_TO_COMMON(trace
), id
));
1640 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1641 struct bt_ctf_trace
*trace
, const char *name
)
1643 return bt_ctf_object_get_ref(
1644 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace
),
1649 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1650 struct metadata_context
*context
)
1652 uint8_t *uuid
= trace
->common
.uuid
;
1655 if (trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1656 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1657 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1658 "set it with bt_ctf_trace_set_native_byte_order(): "
1659 "addr=%p, name=\"%s\"",
1660 trace
, bt_ctf_trace_get_name(trace
));
1665 g_string_append(context
->string
, "trace {\n");
1666 g_string_append(context
->string
, "\tmajor = 1;\n");
1667 g_string_append(context
->string
, "\tminor = 8;\n");
1668 BT_ASSERT_DBG(trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1669 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1670 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1672 if (trace
->common
.uuid_set
) {
1673 g_string_append_printf(context
->string
,
1674 "\tuuid = \"" BT_UUID_FMT
"\";\n",
1675 BT_UUID_FMT_VALUES(uuid
));
1678 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1679 bt_ctf_get_byte_order_string(trace
->common
.native_byte_order
));
1681 if (trace
->common
.packet_header_field_type
) {
1682 g_string_append(context
->string
, "\tpacket.header := ");
1683 context
->current_indentation_level
++;
1684 g_string_assign(context
->field_name
, "");
1685 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1686 ret
= bt_ctf_field_type_serialize_recursive(
1687 (void *) trace
->common
.packet_header_field_type
,
1692 context
->current_indentation_level
--;
1695 g_string_append(context
->string
, ";\n};\n\n");
1701 void append_env_metadata(struct bt_ctf_trace
*trace
,
1702 struct metadata_context
*context
)
1707 env_size
= bt_ctf_attributes_get_count(trace
->common
.environment
);
1708 if (env_size
<= 0) {
1712 g_string_append(context
->string
, "env {\n");
1714 for (i
= 0; i
< env_size
; i
++) {
1715 struct bt_ctf_private_value
*env_field_value_obj
= NULL
;
1716 const char *entry_name
;
1718 entry_name
= bt_ctf_attributes_get_field_name(
1719 trace
->common
.environment
, i
);
1720 env_field_value_obj
= bt_ctf_attributes_borrow_field_value(
1721 trace
->common
.environment
, i
);
1723 BT_ASSERT_DBG(entry_name
);
1724 BT_ASSERT_DBG(env_field_value_obj
);
1726 switch (bt_ctf_value_get_type(
1727 bt_ctf_private_value_as_value(env_field_value_obj
))) {
1728 case BT_CTF_VALUE_TYPE_INTEGER
:
1732 int_value
= bt_ctf_value_integer_get(
1733 bt_ctf_private_value_as_value(
1734 env_field_value_obj
));
1735 g_string_append_printf(context
->string
,
1736 "\t%s = %" PRId64
";\n", entry_name
,
1740 case BT_CTF_VALUE_TYPE_STRING
:
1742 const char *str_value
;
1743 char *escaped_str
= NULL
;
1745 str_value
= bt_ctf_value_string_get(
1746 bt_ctf_private_value_as_value(
1747 env_field_value_obj
));
1748 escaped_str
= g_strescape(str_value
, NULL
);
1750 BT_LOGE("Cannot escape string: string=\"%s\"",
1755 g_string_append_printf(context
->string
,
1756 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1765 g_string_append(context
->string
, "};\n\n");
1768 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1770 char *metadata
= NULL
;
1771 struct metadata_context
*context
= NULL
;
1776 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1780 context
= g_new0(struct metadata_context
, 1);
1782 BT_LOGE_STR("Failed to allocate one metadata context.");
1786 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1787 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1788 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1789 if (append_trace_metadata(trace
, context
)) {
1790 /* append_trace_metadata() logs errors */
1793 append_env_metadata(trace
, context
);
1794 g_ptr_array_foreach(trace
->common
.clock_classes
,
1795 (GFunc
) bt_ctf_clock_class_serialize
, context
);
1797 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
1798 /* bt_ctf_stream_class_serialize() logs details */
1799 err
= bt_ctf_stream_class_serialize(
1800 trace
->common
.stream_classes
->pdata
[i
], context
);
1802 /* bt_ctf_stream_class_serialize() logs errors */
1807 metadata
= context
->string
->str
;
1810 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1811 g_string_free(context
->field_name
, TRUE
);
1819 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1820 struct bt_ctf_trace
*trace
)
1822 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace
));
1826 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1827 enum bt_ctf_byte_order byte_order
)
1829 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace
),
1830 (int) byte_order
, false);
1834 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
1835 struct bt_ctf_trace
*trace
)
1837 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type(
1838 BT_CTF_TO_COMMON(trace
)));
1842 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
1843 struct bt_ctf_field_type
*packet_header_type
)
1845 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace
),
1846 (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
));