2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "CTF-WRITER/TRACE"
33 #include <babeltrace2-ctf-writer/event.h>
34 #include <babeltrace2-ctf-writer/object.h>
35 #include <babeltrace2-ctf-writer/utils.h>
36 #include <babeltrace2/types.h>
38 #include "common/assert.h"
39 #include "compat/compiler.h"
40 #include "compat/endian.h"
42 #include "attributes.h"
43 #include "clock-class.h"
45 #include "event-class.h"
47 #include "field-types.h"
48 #include "field-wrapper.h"
50 #include "stream-class.h"
54 #include "validation.h"
59 #define DEFAULT_IDENTIFIER_SIZE 128
60 #define DEFAULT_METADATA_STRING_SIZE 4096
63 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
64 bt_ctf_object_release_func release_func
)
68 BT_LOGD_STR("Initializing common trace object.");
69 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_UNSPECIFIED
;
70 bt_ctf_object_init_shared_with_parent(&trace
->base
, release_func
);
71 trace
->clock_classes
= g_ptr_array_new_with_free_func(
72 (GDestroyNotify
) bt_ctf_object_put_ref
);
73 if (!trace
->clock_classes
) {
74 BT_LOGE_STR("Failed to allocate one GPtrArray.");
78 trace
->streams
= g_ptr_array_new_with_free_func(
79 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
80 if (!trace
->streams
) {
81 BT_LOGE_STR("Failed to allocate one GPtrArray.");
85 trace
->stream_classes
= g_ptr_array_new_with_free_func(
86 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
87 if (!trace
->stream_classes
) {
88 BT_LOGE_STR("Failed to allocate one GPtrArray.");
92 /* Create the environment array object */
93 trace
->environment
= bt_ctf_attributes_create();
94 if (!trace
->environment
) {
95 BT_LOGE_STR("Cannot create empty attributes object.");
99 BT_LOGD("Initialized common trace object: addr=%p", trace
);
110 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
)
112 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
113 trace
, bt_ctf_trace_common_get_name(trace
));
115 if (trace
->environment
) {
116 BT_LOGD_STR("Destroying environment attributes.");
117 bt_ctf_attributes_destroy(trace
->environment
);
121 g_string_free(trace
->name
, TRUE
);
124 if (trace
->clock_classes
) {
125 BT_LOGD_STR("Putting clock classes.");
126 g_ptr_array_free(trace
->clock_classes
, TRUE
);
129 if (trace
->streams
) {
130 BT_LOGD_STR("Destroying streams.");
131 g_ptr_array_free(trace
->streams
, TRUE
);
134 if (trace
->stream_classes
) {
135 BT_LOGD_STR("Destroying stream classes.");
136 g_ptr_array_free(trace
->stream_classes
, TRUE
);
139 BT_LOGD_STR("Putting packet header field type.");
140 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
144 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
)
149 BT_LOGW_STR("Invalid parameter: trace is NULL.");
155 BT_LOGW_STR("Invalid parameter: name is NULL.");
161 BT_LOGW("Invalid parameter: trace is frozen: "
162 "addr=%p, name=\"%s\"",
163 trace
, bt_ctf_trace_common_get_name(trace
));
168 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
171 BT_LOGE_STR("Failed to allocate one GString.");
176 BT_LOGT("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
183 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
,
189 BT_LOGW_STR("Invalid parameter: trace is NULL.");
195 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
201 BT_LOGW("Invalid parameter: trace is frozen: "
202 "addr=%p, name=\"%s\"",
203 trace
, bt_ctf_trace_common_get_name(trace
));
208 bt_uuid_copy(trace
->uuid
, uuid
);
209 trace
->uuid_set
= BT_CTF_TRUE
;
210 BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", "
211 "uuid=\"" BT_UUID_FMT
"\"",
212 trace
, bt_ctf_trace_common_get_name(trace
),
213 BT_UUID_FMT_VALUES(uuid
));
220 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
221 const char *name
, struct bt_ctf_private_value
*value
)
226 BT_LOGW_STR("Invalid parameter: trace is NULL.");
232 BT_LOGW_STR("Invalid parameter: name is NULL.");
238 BT_LOGW_STR("Invalid parameter: value is NULL.");
243 if (!bt_ctf_identifier_is_valid(name
)) {
244 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
245 "trace-addr=%p, trace-name=\"%s\", "
247 trace
, bt_ctf_trace_common_get_name(trace
), name
);
252 if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value
)) &&
253 !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value
))) {
254 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
255 "trace-addr=%p, trace-name=\"%s\", "
256 "env-name=\"%s\", env-value-type=%s",
257 trace
, bt_ctf_trace_common_get_name(trace
), name
,
258 bt_ctf_value_type_string(
259 bt_ctf_value_get_type(
260 bt_ctf_private_value_as_value(value
))));
267 * New environment fields may be added to a frozen trace,
268 * but existing fields may not be changed.
270 * The object passed is frozen like all other attributes.
272 struct bt_ctf_private_value
*attribute
=
273 bt_ctf_attributes_borrow_field_value_by_name(
274 trace
->environment
, name
);
277 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
278 "trace-addr=%p, trace-name=\"%s\", "
280 trace
, bt_ctf_trace_common_get_name(trace
), name
);
285 bt_ctf_value_freeze(bt_ctf_private_value_as_value(value
));
288 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
291 BT_LOGE("Cannot set environment field's value: "
292 "trace-addr=%p, trace-name=\"%s\", "
294 trace
, bt_ctf_trace_common_get_name(trace
), name
);
296 BT_LOGT("Set environment field's value: "
297 "trace-addr=%p, trace-name=\"%s\", "
298 "env-name=\"%s\", value-addr=%p",
299 trace
, bt_ctf_trace_common_get_name(trace
), name
, value
);
307 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
308 const char *name
, const char *value
)
311 struct bt_ctf_private_value
*env_value_string_obj
= NULL
;
314 BT_LOGW_STR("Invalid parameter: value is NULL.");
319 env_value_string_obj
= bt_ctf_private_value_string_create_init(value
);
320 if (!env_value_string_obj
) {
321 BT_LOGE_STR("Cannot create string 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_string_obj
);
331 bt_ctf_object_put_ref(env_value_string_obj
);
336 int bt_ctf_trace_common_set_environment_field_integer(
337 struct bt_ctf_trace_common
*trace
, const char *name
, int64_t value
)
340 struct bt_ctf_private_value
*env_value_integer_obj
= NULL
;
342 env_value_integer_obj
= bt_ctf_private_value_integer_create_init(value
);
343 if (!env_value_integer_obj
) {
344 BT_LOGE_STR("Cannot create integer value object.");
349 /* bt_ctf_trace_common_set_environment_field() logs errors */
350 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
351 env_value_integer_obj
);
354 bt_ctf_object_put_ref(env_value_integer_obj
);
359 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
360 struct bt_ctf_clock_class
*clock_class
)
365 BT_LOGW_STR("Invalid parameter: trace is NULL.");
370 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
371 BT_LOGW("Invalid parameter: clock class is invalid: "
372 "trace-addr=%p, trace-name=\"%s\", "
373 "clock-class-addr=%p, clock-class-name=\"%s\"",
374 trace
, bt_ctf_trace_common_get_name(trace
),
375 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
380 /* Check for duplicate clock classes */
381 if (bt_ctf_trace_common_has_clock_class(trace
, clock_class
)) {
382 BT_LOGW("Invalid parameter: clock class already exists in trace: "
383 "trace-addr=%p, trace-name=\"%s\", "
384 "clock-class-addr=%p, clock-class-name=\"%s\"",
385 trace
, bt_ctf_trace_common_get_name(trace
),
386 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
391 bt_ctf_object_get_ref(clock_class
);
392 g_ptr_array_add(trace
->clock_classes
, clock_class
);
395 BT_LOGT_STR("Freezing added clock class because trace is frozen.");
396 bt_ctf_clock_class_freeze(clock_class
);
399 BT_LOGT("Added clock class to trace: "
400 "trace-addr=%p, trace-name=\"%s\", "
401 "clock-class-addr=%p, clock-class-name=\"%s\"",
402 trace
, bt_ctf_trace_common_get_name(trace
),
403 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
410 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
411 struct bt_ctf_field_type_common
*packet_header_type
)
414 bool is_valid
= true;
415 struct bt_ctf_field_type_common
*field_type
= NULL
;
417 if (!packet_header_type
) {
419 * No packet header field type: trace must have only
420 * one stream. At this point the stream class being
421 * added is not part of the trace yet, so we validate
422 * that the trace contains no stream classes yet.
424 if (trace
->stream_classes
->len
>= 1) {
425 BT_LOGW_STR("Invalid packet header field type: "
426 "packet header field type does not exist but there's more than one stream class in the trace.");
430 /* No packet header field type: valid at this point */
434 /* Packet header field type, if it exists, must be a structure */
435 if (packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
436 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
437 "ft-addr=%p, ft-id=%s",
439 bt_ctf_field_type_id_string(packet_header_type
->id
));
444 * If there's a `magic` field, it must be a 32-bit unsigned
445 * integer field type. Also it must be the first field of the
446 * packet header field type.
448 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
449 packet_header_type
, "magic");
451 const char *field_name
;
453 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
454 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
455 "magic-ft-addr=%p, magic-ft-id=%s",
457 bt_ctf_field_type_id_string(field_type
->id
));
461 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
462 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
463 "magic-ft-addr=%p", field_type
);
467 if (bt_ctf_field_type_common_integer_get_size(field_type
) != 32) {
468 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
469 "magic-ft-addr=%p, magic-ft-size=%u",
471 bt_ctf_field_type_common_integer_get_size(field_type
));
475 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
476 packet_header_type
, &field_name
, NULL
, 0);
477 BT_ASSERT_DBG(ret
== 0);
479 if (strcmp(field_name
, "magic") != 0) {
480 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
481 "magic-ft-addr=%p, first-field-name=\"%s\"",
482 field_type
, field_name
);
488 * If there's a `uuid` field, it must be an array field type of
489 * length 16 with an 8-bit unsigned integer element field type.
491 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
492 packet_header_type
, "uuid");
494 struct bt_ctf_field_type_common
*elem_ft
;
496 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
497 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
498 "uuid-ft-addr=%p, uuid-ft-id=%s",
500 bt_ctf_field_type_id_string(field_type
->id
));
504 if (bt_ctf_field_type_common_array_get_length(field_type
) != 16) {
505 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
506 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
508 bt_ctf_field_type_common_array_get_length(field_type
));
512 elem_ft
= bt_ctf_field_type_common_array_borrow_element_field_type(field_type
);
513 BT_ASSERT_DBG(elem_ft
);
515 if (elem_ft
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
516 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
517 "elem-ft-addr=%p, elem-ft-id=%s",
519 bt_ctf_field_type_id_string(elem_ft
->id
));
523 if (bt_ctf_field_type_common_integer_is_signed(elem_ft
)) {
524 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
525 "elem-ft-addr=%p", elem_ft
);
529 if (bt_ctf_field_type_common_integer_get_size(elem_ft
) != 8) {
530 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
531 "elem-ft-addr=%p, elem-ft-size=%u",
533 bt_ctf_field_type_common_integer_get_size(elem_ft
));
539 * The `stream_id` field must exist if there's more than one
540 * stream classes in the trace.
542 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
543 packet_header_type
, "stream_id");
545 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
546 BT_LOGW_STR("Invalid packet header field type: "
547 "`stream_id` field does not exist but there's more than one stream class in the trace.");
552 * If there's a `stream_id` field, it must be an unsigned
553 * integer field type.
556 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
557 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
558 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
560 bt_ctf_field_type_id_string(field_type
->id
));
564 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
565 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
566 "stream-id-ft-addr=%p", field_type
);
572 * If there's a `packet_seq_num` field, it must be an unsigned
573 * integer field type.
575 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
576 packet_header_type
, "packet_seq_num");
578 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
579 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
580 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
582 bt_ctf_field_type_id_string(field_type
->id
));
586 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
587 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
588 "packet-seq-num-ft-addr=%p", field_type
);
603 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
604 struct bt_ctf_stream_class_common
*stream_class
,
605 struct bt_ctf_field_type_common
*packet_context_type
,
606 bool check_ts_begin_end_mapped
)
608 bool is_valid
= true;
609 struct bt_ctf_field_type_common
*field_type
= NULL
;
611 if (!packet_context_type
) {
612 /* No packet context field type: valid at this point */
616 /* Packet context field type, if it exists, must be a structure */
617 if (packet_context_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
618 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
619 "ft-addr=%p, ft-id=%s",
621 bt_ctf_field_type_id_string(packet_context_type
->id
));
626 * If there's a `packet_size` field, it must be an unsigned
627 * integer field type.
629 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
630 packet_context_type
, "packet_size");
632 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
633 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
634 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
636 bt_ctf_field_type_id_string(field_type
->id
));
640 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
641 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
642 "packet-size-ft-addr=%p", field_type
);
648 * If there's a `content_size` field, it must be an unsigned
649 * integer field type.
651 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
652 packet_context_type
, "content_size");
654 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
655 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
656 "content-size-ft-addr=%p, content-size-ft-id=%s",
658 bt_ctf_field_type_id_string(field_type
->id
));
662 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
663 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
664 "content-size-ft-addr=%p", field_type
);
670 * If there's a `events_discarded` field, it must be an unsigned
671 * integer field type.
673 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
674 packet_context_type
, "events_discarded");
676 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
677 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
678 "events-discarded-ft-addr=%p, events-discarded-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: `events_discarded` field must be an unsigned integer field type: "
686 "events-discarded-ft-addr=%p", field_type
);
692 * If there's a `timestamp_begin` field, it must be an unsigned
693 * integer field type. Also, if the trace is not a CTF writer's
694 * trace, then we cannot automatically set the mapped clock
695 * class of this field, so it must have a mapped clock class.
697 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
698 packet_context_type
, "timestamp_begin");
700 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
701 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
702 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
704 bt_ctf_field_type_id_string(field_type
->id
));
708 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
709 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
710 "timestamp-begin-ft-addr=%p", field_type
);
714 if (check_ts_begin_end_mapped
) {
715 struct bt_ctf_clock_class
*clock_class
=
716 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
720 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
721 "timestamp-begin-ft-addr=%p", field_type
);
728 * If there's a `timestamp_end` field, it must be an unsigned
729 * integer field type. Also, if the trace is not a CTF writer's
730 * trace, then we cannot automatically set the mapped clock
731 * class of this field, so it must have a mapped clock class.
733 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
734 packet_context_type
, "timestamp_end");
736 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
737 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
738 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
740 bt_ctf_field_type_id_string(field_type
->id
));
744 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
745 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
746 "timestamp-end-ft-addr=%p", field_type
);
750 if (check_ts_begin_end_mapped
) {
751 struct bt_ctf_clock_class
*clock_class
=
752 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
756 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
757 "timestamp-end-ft-addr=%p", field_type
);
773 bool event_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
774 struct bt_ctf_stream_class_common
*stream_class
,
775 struct bt_ctf_field_type_common
*event_header_type
)
777 bool is_valid
= true;
778 struct bt_ctf_field_type_common
*field_type
= NULL
;
781 * We do not validate that the `timestamp` field exists here
782 * because CTF does not require this exact name to be mapped to
786 if (!event_header_type
) {
788 * No event header field type: stream class must have
789 * only one event class.
791 if (bt_ctf_stream_class_common_get_event_class_count(stream_class
) > 1) {
792 BT_LOGW_STR("Invalid event header field type: "
793 "event header field type does not exist but there's more than one event class in the stream class.");
797 /* No event header field type: valid at this point */
801 /* Event header field type, if it exists, must be a structure */
802 if (event_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
803 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
804 "ft-addr=%p, ft-id=%s",
806 bt_ctf_field_type_id_string(event_header_type
->id
));
811 * If there's an `id` field, it must be an unsigned integer
812 * field type or an enumeration field type with an unsigned
813 * integer container field type.
815 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
816 event_header_type
, "id");
818 struct bt_ctf_field_type_common
*int_ft
;
820 if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
822 } else if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
823 int_ft
= bt_ctf_field_type_common_enumeration_borrow_container_field_type(
826 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
827 "id-ft-addr=%p, id-ft-id=%s",
829 bt_ctf_field_type_id_string(field_type
->id
));
833 BT_ASSERT_DBG(int_ft
);
834 if (bt_ctf_field_type_common_integer_is_signed(int_ft
)) {
835 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
836 "id-ft-addr=%p", int_ft
);
851 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common
*trace
)
855 if (trace
->packet_header_field_type
) {
856 struct bt_ctf_clock_class
*clock_class
= NULL
;
858 ret
= bt_ctf_field_type_common_validate_single_clock_class(
859 trace
->packet_header_field_type
,
861 bt_ctf_object_put_ref(clock_class
);
862 if (ret
|| clock_class
) {
863 BT_LOGW("Trace's packet header field type cannot "
864 "contain a field type which is mapped to "
866 "trace-addr=%p, trace-name=\"%s\", "
867 "clock-class-name=\"%s\"",
868 trace
, bt_ctf_trace_common_get_name(trace
),
870 bt_ctf_clock_class_get_name(clock_class
) :
880 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
881 struct bt_ctf_stream_class_common
*stream_class
,
882 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
883 struct bt_ctf_clock_class
*init_expected_clock_class
,
884 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
885 struct bt_ctf_field_type_common
*packet_context_field_type
,
886 struct bt_ctf_field_type_common
*event_header_field_type
),
887 bool check_ts_begin_end_mapped
)
892 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
893 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
894 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
895 BT_CTF_VALIDATION_FLAG_TRACE
|
896 BT_CTF_VALIDATION_FLAG_STREAM
;
897 const enum bt_ctf_validation_flag ec_validation_flags
=
898 BT_CTF_VALIDATION_FLAG_EVENT
;
899 struct bt_ctf_field_type_common
*packet_header_type
= NULL
;
900 struct bt_ctf_field_type_common
*packet_context_type
= NULL
;
901 struct bt_ctf_field_type_common
*event_header_type
= NULL
;
902 struct bt_ctf_field_type_common
*stream_event_ctx_type
= NULL
;
903 int64_t event_class_count
= 0;
904 struct bt_ctf_trace_common
*current_parent_trace
= NULL
;
905 struct bt_ctf_clock_class
*expected_clock_class
=
906 bt_ctf_object_get_ref(init_expected_clock_class
);
908 BT_ASSERT_DBG(copy_field_type_func
);
911 BT_LOGW_STR("Invalid parameter: trace is NULL.");
917 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
922 BT_LOGD("Adding stream class to trace: "
923 "trace-addr=%p, trace-name=\"%s\", "
924 "stream-class-addr=%p, stream-class-name=\"%s\", "
925 "stream-class-id=%" PRId64
,
926 trace
, bt_ctf_trace_common_get_name(trace
),
927 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
928 bt_ctf_stream_class_common_get_id(stream_class
));
930 current_parent_trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
931 if (current_parent_trace
) {
932 /* Stream class is already associated to a trace, abort. */
933 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
934 "stream-class-trace-addr=%p, "
935 "stream-class-trace-name=\"%s\"",
936 current_parent_trace
,
937 bt_ctf_trace_common_get_name(current_parent_trace
));
943 bt_ctf_stream_class_common_get_event_class_count(stream_class
);
944 BT_ASSERT_DBG(event_class_count
>= 0);
946 if (!stream_class
->frozen
) {
948 * Stream class is not frozen yet. Validate that the
949 * stream class contains at most a single clock class
950 * because the previous
951 * bt_ctf_stream_class_common_add_event_class() calls did
952 * not make this validation since the stream class's
953 * direct field types (packet context, event header,
954 * event context) could change afterwards. This stream
955 * class is about to be frozen and those field types
956 * won't be changed if this function succeeds.
958 * At this point we're also sure that the stream class's
959 * clock, if any, has the same class as the stream
960 * class's expected clock class, if any. This is why, if
961 * bt_ctf_stream_class_common_validate_single_clock_class()
962 * succeeds below, the call to
963 * bt_ctf_stream_class_map_clock_class() at the end of this
964 * function is safe because it maps to the same, single
967 ret
= bt_ctf_stream_class_common_validate_single_clock_class(
968 stream_class
, &expected_clock_class
);
970 BT_LOGW("Invalid parameter: stream class or one of its "
971 "event classes contains a field type which is "
972 "not recursively mapped to the expected "
974 "stream-class-addr=%p, "
975 "stream-class-id=%" PRId64
", "
976 "stream-class-name=\"%s\", "
977 "expected-clock-class-addr=%p, "
978 "expected-clock-class-name=\"%s\"",
979 stream_class
, bt_ctf_stream_class_common_get_id(stream_class
),
980 bt_ctf_stream_class_common_get_name(stream_class
),
981 expected_clock_class
,
982 expected_clock_class
?
983 bt_ctf_clock_class_get_name(expected_clock_class
) :
989 ret
= check_packet_header_type_has_no_clock_class(trace
);
991 /* check_packet_header_type_has_no_clock_class() logs errors */
996 * We're about to freeze both the trace and the stream class.
997 * Also, each event class contained in this stream class are
1000 * This trace, this stream class, and all its event classes
1001 * should be valid at this point.
1003 * Validate trace and stream class first, then each event
1004 * class of this stream class can be validated individually.
1006 packet_header_type
=
1007 bt_ctf_trace_common_borrow_packet_header_field_type(trace
);
1008 packet_context_type
=
1009 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class
);
1011 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class
);
1012 stream_event_ctx_type
=
1013 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class
);
1015 BT_LOGD("Validating trace and stream class field types.");
1016 ret
= bt_ctf_validate_class_types(trace
->environment
,
1017 packet_header_type
, packet_context_type
, event_header_type
,
1018 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1019 stream_class
->valid
, 1, &trace_sc_validation_output
,
1020 trace_sc_validation_flags
, copy_field_type_func
);
1024 * This means something went wrong during the validation
1025 * process, not that the objects are invalid.
1027 BT_LOGE("Failed to validate trace and stream class field types: "
1032 if ((trace_sc_validation_output
.valid_flags
&
1033 trace_sc_validation_flags
) !=
1034 trace_sc_validation_flags
) {
1035 /* Invalid trace/stream class */
1036 BT_LOGW("Invalid trace or stream class field types: "
1038 trace_sc_validation_output
.valid_flags
);
1043 if (event_class_count
> 0) {
1044 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1046 if (!ec_validation_outputs
) {
1047 BT_LOGE_STR("Failed to allocate one validation output structure.");
1053 /* Validate each event class individually */
1054 for (i
= 0; i
< event_class_count
; i
++) {
1055 struct bt_ctf_event_class_common
*event_class
=
1056 bt_ctf_stream_class_common_borrow_event_class_by_index(
1058 struct bt_ctf_field_type_common
*event_context_type
= NULL
;
1059 struct bt_ctf_field_type_common
*event_payload_type
= NULL
;
1061 event_context_type
=
1062 bt_ctf_event_class_common_borrow_context_field_type(
1064 event_payload_type
=
1065 bt_ctf_event_class_common_borrow_payload_field_type(
1069 * It is important to use the field types returned by
1070 * the previous trace and stream class validation here
1071 * because copies could have been made.
1073 BT_LOGD("Validating event class's field types: "
1074 "addr=%p, name=\"%s\", id=%" PRId64
,
1075 event_class
, bt_ctf_event_class_common_get_name(event_class
),
1076 bt_ctf_event_class_common_get_id(event_class
));
1077 ret
= bt_ctf_validate_class_types(trace
->environment
,
1078 trace_sc_validation_output
.packet_header_type
,
1079 trace_sc_validation_output
.packet_context_type
,
1080 trace_sc_validation_output
.event_header_type
,
1081 trace_sc_validation_output
.stream_event_ctx_type
,
1082 event_context_type
, event_payload_type
,
1083 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1084 ec_validation_flags
, copy_field_type_func
);
1087 BT_LOGE("Failed to validate event class field types: "
1092 if ((ec_validation_outputs
[i
].valid_flags
&
1093 ec_validation_flags
) != ec_validation_flags
) {
1094 /* Invalid event class */
1095 BT_LOGW("Invalid event class field types: "
1097 ec_validation_outputs
[i
].valid_flags
);
1103 stream_id
= bt_ctf_stream_class_common_get_id(stream_class
);
1104 if (stream_id
< 0) {
1105 stream_id
= trace
->next_stream_id
++;
1106 if (stream_id
< 0) {
1107 BT_LOGE_STR("No more stream class IDs available.");
1112 /* Try to assign a new stream id */
1113 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1114 if (stream_id
== bt_ctf_stream_class_common_get_id(
1115 trace
->stream_classes
->pdata
[i
])) {
1116 /* Duplicate stream id found */
1117 BT_LOGW("Duplicate stream class ID: "
1118 "id=%" PRId64
, (int64_t) stream_id
);
1124 if (bt_ctf_stream_class_common_set_id_no_check(stream_class
,
1126 /* TODO Should retry with a different stream id */
1127 BT_LOGE("Cannot set stream class's ID: "
1128 "id=%" PRId64
, (int64_t) stream_id
);
1135 * At this point all the field types in the validation output
1136 * are valid. Validate the semantics of some scopes according to
1137 * the CTF specification.
1139 if (!packet_header_field_type_is_valid(trace
,
1140 trace_sc_validation_output
.packet_header_type
)) {
1141 BT_LOGW_STR("Invalid trace's packet header field type.");
1146 if (!packet_context_field_type_is_valid(trace
,
1148 trace_sc_validation_output
.packet_context_type
,
1149 check_ts_begin_end_mapped
)) {
1150 BT_LOGW_STR("Invalid stream class's packet context field type.");
1155 if (!event_header_field_type_is_valid(trace
,
1157 trace_sc_validation_output
.event_header_type
)) {
1158 BT_LOGW_STR("Invalid steam class's event header field type.");
1164 * Now is the time to automatically map specific field types of
1165 * the stream class's packet context and event header field
1166 * types to the stream class's clock's class if they are not
1167 * mapped to a clock class yet. We do it here because we know
1168 * that after this point, everything is frozen so it won't be
1169 * possible for the user to modify the stream class's clock, or
1170 * to map those field types to other clock classes.
1172 if (map_clock_classes_func
) {
1173 if (map_clock_classes_func(stream_class
,
1174 trace_sc_validation_output
.packet_context_type
,
1175 trace_sc_validation_output
.event_header_type
)) {
1176 /* map_clock_classes_func() logs errors */
1182 bt_ctf_object_set_parent(&stream_class
->base
, &trace
->base
);
1183 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1186 * At this point we know that the function will be successful.
1187 * Therefore we can replace the trace and stream class field
1188 * types with what's in their validation output structure and
1189 * mark them as valid. We can also replace the field types of
1190 * all the event classes of the stream class and mark them as
1193 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1194 &trace_sc_validation_output
, trace_sc_validation_flags
);
1196 stream_class
->valid
= 1;
1199 * Put what was not moved in bt_ctf_validation_replace_types().
1201 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1203 for (i
= 0; i
< event_class_count
; i
++) {
1204 struct bt_ctf_event_class_common
*event_class
=
1205 bt_ctf_stream_class_common_borrow_event_class_by_index(
1208 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1209 &ec_validation_outputs
[i
], ec_validation_flags
);
1210 event_class
->valid
= 1;
1213 * Put what was not moved in
1214 * bt_ctf_validation_replace_types().
1216 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1220 * Freeze the trace and the stream class.
1222 bt_ctf_stream_class_common_freeze(stream_class
);
1223 bt_ctf_trace_common_freeze(trace
);
1226 * It is safe to set the stream class's unique clock class
1227 * now because the stream class is frozen.
1229 if (expected_clock_class
) {
1230 BT_CTF_OBJECT_MOVE_REF(stream_class
->clock_class
, expected_clock_class
);
1233 BT_LOGD("Added stream class to trace: "
1234 "trace-addr=%p, trace-name=\"%s\", "
1235 "stream-class-addr=%p, stream-class-name=\"%s\", "
1236 "stream-class-id=%" PRId64
,
1237 trace
, bt_ctf_trace_common_get_name(trace
),
1238 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
1239 bt_ctf_stream_class_common_get_id(stream_class
));
1243 bt_ctf_object_set_parent(&stream_class
->base
, NULL
);
1245 if (ec_validation_outputs
) {
1246 for (i
= 0; i
< event_class_count
; i
++) {
1247 bt_ctf_validation_output_put_types(
1248 &ec_validation_outputs
[i
]);
1253 g_free(ec_validation_outputs
);
1254 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1255 bt_ctf_object_put_ref(expected_clock_class
);
1260 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
1261 struct bt_ctf_clock_class
*clock_class
)
1263 struct bt_ctf_search_query query
= { .value
= clock_class
, .found
= 0 };
1265 BT_ASSERT_DBG(trace
);
1266 BT_ASSERT_DBG(clock_class
);
1268 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1273 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
1274 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
)
1279 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1284 if (trace
->frozen
) {
1285 BT_LOGW("Invalid parameter: trace is frozen: "
1286 "addr=%p, name=\"%s\"",
1287 trace
, bt_ctf_trace_common_get_name(trace
));
1292 if (byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1293 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1294 "addr=%p, name=\"%s\"",
1295 trace
, bt_ctf_trace_common_get_name(trace
));
1300 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1301 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1302 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1303 BT_LOGW("Invalid parameter: invalid byte order: "
1304 "addr=%p, name=\"%s\", bo=%s",
1305 trace
, bt_ctf_trace_common_get_name(trace
),
1306 bt_ctf_byte_order_string(byte_order
));
1311 trace
->native_byte_order
= byte_order
;
1312 BT_LOGT("Set trace's native byte order: "
1313 "addr=%p, name=\"%s\", bo=%s",
1314 trace
, bt_ctf_trace_common_get_name(trace
),
1315 bt_ctf_byte_order_string(byte_order
));
1322 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
1323 struct bt_ctf_field_type_common
*packet_header_type
)
1328 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1333 if (trace
->frozen
) {
1334 BT_LOGW("Invalid parameter: trace is frozen: "
1335 "addr=%p, name=\"%s\"",
1336 trace
, bt_ctf_trace_common_get_name(trace
));
1341 /* packet_header_type must be a structure. */
1342 if (packet_header_type
&&
1343 packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1344 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1345 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1346 trace
, bt_ctf_trace_common_get_name(trace
),
1348 bt_ctf_field_type_id_string(packet_header_type
->id
));
1353 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
1354 trace
->packet_header_field_type
= bt_ctf_object_get_ref(packet_header_type
);
1355 BT_LOGT("Set trace's packet header field type: "
1356 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1357 trace
, bt_ctf_trace_common_get_name(trace
), packet_header_type
);
1363 int64_t get_stream_class_count(void *element
)
1365 return bt_ctf_trace_get_stream_class_count(
1366 (struct bt_ctf_trace
*) element
);
1370 void *get_stream_class(void *element
, int i
)
1372 return bt_ctf_trace_get_stream_class_by_index(
1373 (struct bt_ctf_trace
*) element
, i
);
1377 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1379 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1382 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1383 bt_ctf_visitor visitor
, void *data
)
1386 struct bt_ctf_visitor_object obj
= {
1388 .type
= BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1392 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1398 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1403 BT_LOGT("Visiting trace: addr=%p, name=\"%s\"",
1404 trace
, bt_ctf_trace_get_name(trace
));
1405 ret
= bt_ctf_visitor_helper(&obj
, get_stream_class_count
,
1406 get_stream_class
, visit_stream_class
, visitor
, data
);
1412 void bt_ctf_trace_destroy(struct bt_ctf_object
*obj
)
1414 struct bt_ctf_trace
*trace
= (void *) obj
;
1416 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1417 trace
, bt_ctf_trace_get_name(trace
));
1418 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace
));
1423 struct bt_ctf_trace
*bt_ctf_trace_create(void)
1425 struct bt_ctf_trace
*trace
= NULL
;
1428 BT_LOGD_STR("Creating CTF writer trace object.");
1429 trace
= g_new0(struct bt_ctf_trace
, 1);
1431 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1435 ret
= bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace
),
1436 bt_ctf_trace_destroy
);
1438 /* bt_ctf_trace_common_initialize() logs errors */
1442 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
1446 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1450 const uint8_t *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
1452 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace
));
1455 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
1456 const uint8_t *uuid
)
1458 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace
), uuid
);
1461 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
1462 const char *name
, const char *value
)
1464 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace
),
1468 int bt_ctf_trace_set_environment_field_integer(
1469 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
1471 return bt_ctf_trace_common_set_environment_field_integer(
1472 BT_CTF_TO_COMMON(trace
), name
, value
);
1475 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
1477 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace
));
1481 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
1484 return bt_ctf_trace_common_get_environment_field_name_by_index(
1485 BT_CTF_TO_COMMON(trace
), index
);
1488 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_index(
1489 struct bt_ctf_trace
*trace
, uint64_t index
)
1491 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1492 BT_CTF_TO_COMMON(trace
), index
));
1495 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_name(
1496 struct bt_ctf_trace
*trace
, const char *name
)
1498 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1499 BT_CTF_TO_COMMON(trace
), name
));
1503 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
1504 struct bt_ctf_clock_class
*clock_class
)
1506 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace
),
1507 (void *) clock_class
);
1511 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
1513 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace
));
1517 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
1518 struct bt_ctf_trace
*trace
, uint64_t index
)
1520 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index(
1521 BT_CTF_TO_COMMON(trace
), index
));
1525 int map_clock_classes_func(struct bt_ctf_stream_class_common
*stream_class
,
1526 struct bt_ctf_field_type_common
*packet_context_type
,
1527 struct bt_ctf_field_type_common
*event_header_type
)
1529 int ret
= bt_ctf_stream_class_map_clock_class(
1530 BT_CTF_FROM_COMMON(stream_class
),
1531 BT_CTF_FROM_COMMON(packet_context_type
),
1532 BT_CTF_FROM_COMMON(event_header_type
));
1535 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1541 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1542 struct bt_ctf_stream_class
*stream_class
)
1545 struct bt_ctf_clock_class
*expected_clock_class
= NULL
;
1548 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1553 if (!stream_class
) {
1554 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1559 if (stream_class
->clock
) {
1560 struct bt_ctf_clock_class
*stream_clock_class
=
1561 stream_class
->clock
->clock_class
;
1564 * Make sure this clock was also added to the
1565 * trace (potentially through its CTF writer
1570 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
1571 if (trace
->common
.clock_classes
->pdata
[i
] ==
1572 stream_clock_class
) {
1578 if (i
== trace
->common
.clock_classes
->len
) {
1580 BT_LOGW("Stream class's clock's class is not part of the trace: "
1581 "clock-class-addr=%p, clock-class-name=\"%s\"",
1583 bt_ctf_clock_class_get_name(stream_clock_class
));
1588 if (stream_class
->common
.clock_class
&&
1589 stream_class
->common
.clock_class
!=
1590 stream_class
->clock
->clock_class
) {
1592 * Stream class already has an expected clock
1593 * class, but it does not match its clock's
1596 BT_LOGW("Invalid parameter: stream class's clock's "
1597 "class does not match stream class's "
1598 "expected clock class: "
1599 "stream-class-addr=%p, "
1600 "stream-class-id=%" PRId64
", "
1601 "stream-class-name=\"%s\", "
1602 "expected-clock-class-addr=%p, "
1603 "expected-clock-class-name=\"%s\"",
1605 bt_ctf_stream_class_get_id(stream_class
),
1606 bt_ctf_stream_class_get_name(stream_class
),
1607 stream_class
->common
.clock_class
,
1608 bt_ctf_clock_class_get_name(stream_class
->common
.clock_class
));
1609 } else if (!stream_class
->common
.clock_class
) {
1611 * Set expected clock class to stream class's
1614 expected_clock_class
= stream_class
->clock
->clock_class
;
1619 ret
= bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace
),
1620 BT_CTF_TO_COMMON(stream_class
),
1621 (bt_ctf_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
1622 expected_clock_class
, map_clock_classes_func
,
1629 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1631 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace
));
1634 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1635 struct bt_ctf_trace
*trace
, uint64_t index
)
1637 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index(
1638 BT_CTF_TO_COMMON(trace
), index
));
1641 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1643 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace
));
1646 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1647 struct bt_ctf_trace
*trace
, uint64_t index
)
1649 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index(
1650 BT_CTF_TO_COMMON(trace
), index
));
1653 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1654 struct bt_ctf_trace
*trace
, uint64_t id
)
1656 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id(
1657 BT_CTF_TO_COMMON(trace
), id
));
1661 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1662 struct bt_ctf_trace
*trace
, const char *name
)
1664 return bt_ctf_object_get_ref(
1665 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace
),
1670 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1671 struct metadata_context
*context
)
1673 uint8_t *uuid
= trace
->common
.uuid
;
1676 if (trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1677 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1678 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1679 "set it with bt_ctf_trace_set_native_byte_order(): "
1680 "addr=%p, name=\"%s\"",
1681 trace
, bt_ctf_trace_get_name(trace
));
1686 g_string_append(context
->string
, "trace {\n");
1687 g_string_append(context
->string
, "\tmajor = 1;\n");
1688 g_string_append(context
->string
, "\tminor = 8;\n");
1689 BT_ASSERT_DBG(trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1690 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1691 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1693 if (trace
->common
.uuid_set
) {
1694 g_string_append_printf(context
->string
,
1695 "\tuuid = \"" BT_UUID_FMT
"\";\n",
1696 BT_UUID_FMT_VALUES(uuid
));
1699 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1700 bt_ctf_get_byte_order_string(trace
->common
.native_byte_order
));
1702 if (trace
->common
.packet_header_field_type
) {
1703 g_string_append(context
->string
, "\tpacket.header := ");
1704 context
->current_indentation_level
++;
1705 g_string_assign(context
->field_name
, "");
1706 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1707 ret
= bt_ctf_field_type_serialize_recursive(
1708 (void *) trace
->common
.packet_header_field_type
,
1713 context
->current_indentation_level
--;
1716 g_string_append(context
->string
, ";\n};\n\n");
1722 void append_env_metadata(struct bt_ctf_trace
*trace
,
1723 struct metadata_context
*context
)
1728 env_size
= bt_ctf_attributes_get_count(trace
->common
.environment
);
1729 if (env_size
<= 0) {
1733 g_string_append(context
->string
, "env {\n");
1735 for (i
= 0; i
< env_size
; i
++) {
1736 struct bt_ctf_private_value
*env_field_value_obj
= NULL
;
1737 const char *entry_name
;
1739 entry_name
= bt_ctf_attributes_get_field_name(
1740 trace
->common
.environment
, i
);
1741 env_field_value_obj
= bt_ctf_attributes_borrow_field_value(
1742 trace
->common
.environment
, i
);
1744 BT_ASSERT_DBG(entry_name
);
1745 BT_ASSERT_DBG(env_field_value_obj
);
1747 switch (bt_ctf_value_get_type(
1748 bt_ctf_private_value_as_value(env_field_value_obj
))) {
1749 case BT_CTF_VALUE_TYPE_INTEGER
:
1753 int_value
= bt_ctf_value_integer_get(
1754 bt_ctf_private_value_as_value(
1755 env_field_value_obj
));
1756 g_string_append_printf(context
->string
,
1757 "\t%s = %" PRId64
";\n", entry_name
,
1761 case BT_CTF_VALUE_TYPE_STRING
:
1763 const char *str_value
;
1764 char *escaped_str
= NULL
;
1766 str_value
= bt_ctf_value_string_get(
1767 bt_ctf_private_value_as_value(
1768 env_field_value_obj
));
1769 escaped_str
= g_strescape(str_value
, NULL
);
1771 BT_LOGE("Cannot escape string: string=\"%s\"",
1776 g_string_append_printf(context
->string
,
1777 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1786 g_string_append(context
->string
, "};\n\n");
1789 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1791 char *metadata
= NULL
;
1792 struct metadata_context
*context
= NULL
;
1797 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1801 context
= g_new0(struct metadata_context
, 1);
1803 BT_LOGE_STR("Failed to allocate one metadata context.");
1807 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1808 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1809 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1810 if (append_trace_metadata(trace
, context
)) {
1811 /* append_trace_metadata() logs errors */
1814 append_env_metadata(trace
, context
);
1815 g_ptr_array_foreach(trace
->common
.clock_classes
,
1816 (GFunc
) bt_ctf_clock_class_serialize
, context
);
1818 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
1819 /* bt_ctf_stream_class_serialize() logs details */
1820 err
= bt_ctf_stream_class_serialize(
1821 trace
->common
.stream_classes
->pdata
[i
], context
);
1823 /* bt_ctf_stream_class_serialize() logs errors */
1828 metadata
= context
->string
->str
;
1831 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1832 g_string_free(context
->field_name
, TRUE
);
1839 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1840 struct bt_ctf_trace
*trace
)
1842 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace
));
1845 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1846 enum bt_ctf_byte_order byte_order
)
1848 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace
),
1849 (int) byte_order
, false);
1852 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
1853 struct bt_ctf_trace
*trace
)
1855 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type(
1856 BT_CTF_TO_COMMON(trace
)));
1859 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
1860 struct bt_ctf_field_type
*packet_header_type
)
1862 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace
),
1863 (void *) packet_header_type
);
1866 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
1868 return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace
));