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"
32 #include <babeltrace2/ctf-writer/event.h>
33 #include <babeltrace2/ctf-writer/object.h>
34 #include <babeltrace2/ctf-writer/utils.h>
35 #include <babeltrace2/types.h>
37 #include "common/assert.h"
38 #include "compat/compiler.h"
39 #include "compat/endian.h"
41 #include "attributes.h"
42 #include "clock-class.h"
44 #include "event-class.h"
46 #include "field-types.h"
47 #include "field-wrapper.h"
49 #include "stream-class.h"
53 #include "validation.h"
58 #define DEFAULT_IDENTIFIER_SIZE 128
59 #define DEFAULT_METADATA_STRING_SIZE 4096
62 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
63 bt_ctf_object_release_func release_func
)
67 BT_LOGD_STR("Initializing common trace object.");
68 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_UNSPECIFIED
;
69 bt_ctf_object_init_shared_with_parent(&trace
->base
, release_func
);
70 trace
->clock_classes
= g_ptr_array_new_with_free_func(
71 (GDestroyNotify
) bt_ctf_object_put_ref
);
72 if (!trace
->clock_classes
) {
73 BT_LOGE_STR("Failed to allocate one GPtrArray.");
77 trace
->streams
= g_ptr_array_new_with_free_func(
78 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
79 if (!trace
->streams
) {
80 BT_LOGE_STR("Failed to allocate one GPtrArray.");
84 trace
->stream_classes
= g_ptr_array_new_with_free_func(
85 (GDestroyNotify
) bt_ctf_object_try_spec_release
);
86 if (!trace
->stream_classes
) {
87 BT_LOGE_STR("Failed to allocate one GPtrArray.");
91 /* Create the environment array object */
92 trace
->environment
= bt_ctf_attributes_create();
93 if (!trace
->environment
) {
94 BT_LOGE_STR("Cannot create empty attributes object.");
98 BT_LOGD("Initialized common trace object: addr=%p", trace
);
109 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
)
111 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
112 trace
, bt_ctf_trace_common_get_name(trace
));
114 if (trace
->environment
) {
115 BT_LOGD_STR("Destroying environment attributes.");
116 bt_ctf_attributes_destroy(trace
->environment
);
120 g_string_free(trace
->name
, TRUE
);
123 if (trace
->clock_classes
) {
124 BT_LOGD_STR("Putting clock classes.");
125 g_ptr_array_free(trace
->clock_classes
, TRUE
);
128 if (trace
->streams
) {
129 BT_LOGD_STR("Destroying streams.");
130 g_ptr_array_free(trace
->streams
, TRUE
);
133 if (trace
->stream_classes
) {
134 BT_LOGD_STR("Destroying stream classes.");
135 g_ptr_array_free(trace
->stream_classes
, TRUE
);
138 BT_LOGD_STR("Putting packet header field type.");
139 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
143 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
)
148 BT_LOGW_STR("Invalid parameter: trace is NULL.");
154 BT_LOGW_STR("Invalid parameter: name is NULL.");
160 BT_LOGW("Invalid parameter: trace is frozen: "
161 "addr=%p, name=\"%s\"",
162 trace
, bt_ctf_trace_common_get_name(trace
));
167 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
170 BT_LOGE_STR("Failed to allocate one GString.");
175 BT_LOGT("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
182 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
,
188 BT_LOGW_STR("Invalid parameter: trace is NULL.");
194 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
200 BT_LOGW("Invalid parameter: trace is frozen: "
201 "addr=%p, name=\"%s\"",
202 trace
, bt_ctf_trace_common_get_name(trace
));
207 bt_uuid_copy(trace
->uuid
, uuid
);
208 trace
->uuid_set
= BT_TRUE
;
209 BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", "
210 "uuid=\"" BT_UUID_FMT
"\"",
211 trace
, bt_ctf_trace_common_get_name(trace
),
212 BT_UUID_FMT_VALUES(uuid
));
219 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
220 const char *name
, struct bt_ctf_private_value
*value
)
225 BT_LOGW_STR("Invalid parameter: trace is NULL.");
231 BT_LOGW_STR("Invalid parameter: name is NULL.");
237 BT_LOGW_STR("Invalid parameter: value is NULL.");
242 if (!bt_ctf_identifier_is_valid(name
)) {
243 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
244 "trace-addr=%p, trace-name=\"%s\", "
246 trace
, bt_ctf_trace_common_get_name(trace
), name
);
251 if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value
)) &&
252 !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value
))) {
253 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
254 "trace-addr=%p, trace-name=\"%s\", "
255 "env-name=\"%s\", env-value-type=%s",
256 trace
, bt_ctf_trace_common_get_name(trace
), name
,
257 bt_ctf_value_type_string(
258 bt_ctf_value_get_type(
259 bt_ctf_private_value_as_value(value
))));
266 * New environment fields may be added to a frozen trace,
267 * but existing fields may not be changed.
269 * The object passed is frozen like all other attributes.
271 struct bt_ctf_private_value
*attribute
=
272 bt_ctf_attributes_borrow_field_value_by_name(
273 trace
->environment
, name
);
276 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
277 "trace-addr=%p, trace-name=\"%s\", "
279 trace
, bt_ctf_trace_common_get_name(trace
), name
);
284 bt_ctf_value_freeze(bt_ctf_private_value_as_value(value
));
287 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
290 BT_LOGE("Cannot set environment field's value: "
291 "trace-addr=%p, trace-name=\"%s\", "
293 trace
, bt_ctf_trace_common_get_name(trace
), name
);
295 BT_LOGT("Set environment field's value: "
296 "trace-addr=%p, trace-name=\"%s\", "
297 "env-name=\"%s\", value-addr=%p",
298 trace
, bt_ctf_trace_common_get_name(trace
), name
, value
);
306 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
307 const char *name
, const char *value
)
310 struct bt_ctf_private_value
*env_value_string_obj
= NULL
;
313 BT_LOGW_STR("Invalid parameter: value is NULL.");
318 env_value_string_obj
= bt_ctf_private_value_string_create_init(value
);
319 if (!env_value_string_obj
) {
320 BT_LOGE_STR("Cannot create string value object.");
325 /* bt_ctf_trace_common_set_environment_field() logs errors */
326 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
327 env_value_string_obj
);
330 bt_ctf_object_put_ref(env_value_string_obj
);
335 int bt_ctf_trace_common_set_environment_field_integer(
336 struct bt_ctf_trace_common
*trace
, const char *name
, int64_t value
)
339 struct bt_ctf_private_value
*env_value_integer_obj
= NULL
;
341 env_value_integer_obj
= bt_ctf_private_value_integer_create_init(value
);
342 if (!env_value_integer_obj
) {
343 BT_LOGE_STR("Cannot create integer value object.");
348 /* bt_ctf_trace_common_set_environment_field() logs errors */
349 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
350 env_value_integer_obj
);
353 bt_ctf_object_put_ref(env_value_integer_obj
);
358 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
359 struct bt_ctf_clock_class
*clock_class
)
364 BT_LOGW_STR("Invalid parameter: trace is NULL.");
369 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
370 BT_LOGW("Invalid parameter: clock class is invalid: "
371 "trace-addr=%p, trace-name=\"%s\", "
372 "clock-class-addr=%p, clock-class-name=\"%s\"",
373 trace
, bt_ctf_trace_common_get_name(trace
),
374 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
379 /* Check for duplicate clock classes */
380 if (bt_ctf_trace_common_has_clock_class(trace
, clock_class
)) {
381 BT_LOGW("Invalid parameter: clock class already exists in trace: "
382 "trace-addr=%p, trace-name=\"%s\", "
383 "clock-class-addr=%p, clock-class-name=\"%s\"",
384 trace
, bt_ctf_trace_common_get_name(trace
),
385 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
390 bt_ctf_object_get_ref(clock_class
);
391 g_ptr_array_add(trace
->clock_classes
, clock_class
);
394 BT_LOGT_STR("Freezing added clock class because trace is frozen.");
395 bt_ctf_clock_class_freeze(clock_class
);
398 BT_LOGT("Added clock class to trace: "
399 "trace-addr=%p, trace-name=\"%s\", "
400 "clock-class-addr=%p, clock-class-name=\"%s\"",
401 trace
, bt_ctf_trace_common_get_name(trace
),
402 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
409 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
410 struct bt_ctf_field_type_common
*packet_header_type
)
413 bool is_valid
= true;
414 struct bt_ctf_field_type_common
*field_type
= NULL
;
416 if (!packet_header_type
) {
418 * No packet header field type: trace must have only
419 * one stream. At this point the stream class being
420 * added is not part of the trace yet, so we validate
421 * that the trace contains no stream classes yet.
423 if (trace
->stream_classes
->len
>= 1) {
424 BT_LOGW_STR("Invalid packet header field type: "
425 "packet header field type does not exist but there's more than one stream class in the trace.");
429 /* No packet header field type: valid at this point */
433 /* Packet header field type, if it exists, must be a structure */
434 if (packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
435 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
436 "ft-addr=%p, ft-id=%s",
438 bt_ctf_field_type_id_string(packet_header_type
->id
));
443 * If there's a `magic` field, it must be a 32-bit unsigned
444 * integer field type. Also it must be the first field of the
445 * packet header field type.
447 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
448 packet_header_type
, "magic");
450 const char *field_name
;
452 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
453 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
454 "magic-ft-addr=%p, magic-ft-id=%s",
456 bt_ctf_field_type_id_string(field_type
->id
));
460 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
461 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
462 "magic-ft-addr=%p", field_type
);
466 if (bt_ctf_field_type_common_integer_get_size(field_type
) != 32) {
467 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
468 "magic-ft-addr=%p, magic-ft-size=%u",
470 bt_ctf_field_type_common_integer_get_size(field_type
));
474 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
475 packet_header_type
, &field_name
, NULL
, 0);
478 if (strcmp(field_name
, "magic") != 0) {
479 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
480 "magic-ft-addr=%p, first-field-name=\"%s\"",
481 field_type
, field_name
);
487 * If there's a `uuid` field, it must be an array field type of
488 * length 16 with an 8-bit unsigned integer element field type.
490 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
491 packet_header_type
, "uuid");
493 struct bt_ctf_field_type_common
*elem_ft
;
495 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
496 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
497 "uuid-ft-addr=%p, uuid-ft-id=%s",
499 bt_ctf_field_type_id_string(field_type
->id
));
503 if (bt_ctf_field_type_common_array_get_length(field_type
) != 16) {
504 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
505 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
507 bt_ctf_field_type_common_array_get_length(field_type
));
511 elem_ft
= bt_ctf_field_type_common_array_borrow_element_field_type(field_type
);
514 if (elem_ft
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
515 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
516 "elem-ft-addr=%p, elem-ft-id=%s",
518 bt_ctf_field_type_id_string(elem_ft
->id
));
522 if (bt_ctf_field_type_common_integer_is_signed(elem_ft
)) {
523 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
524 "elem-ft-addr=%p", elem_ft
);
528 if (bt_ctf_field_type_common_integer_get_size(elem_ft
) != 8) {
529 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
530 "elem-ft-addr=%p, elem-ft-size=%u",
532 bt_ctf_field_type_common_integer_get_size(elem_ft
));
538 * The `stream_id` field must exist if there's more than one
539 * stream classes in the trace.
541 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
542 packet_header_type
, "stream_id");
544 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
545 BT_LOGW_STR("Invalid packet header field type: "
546 "`stream_id` field does not exist but there's more than one stream class in the trace.");
551 * If there's a `stream_id` field, it must be an unsigned
552 * integer field type.
555 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
556 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
557 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
559 bt_ctf_field_type_id_string(field_type
->id
));
563 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
564 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
565 "stream-id-ft-addr=%p", field_type
);
571 * If there's a `packet_seq_num` field, it must be an unsigned
572 * integer field type.
574 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
575 packet_header_type
, "packet_seq_num");
577 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
578 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
579 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
581 bt_ctf_field_type_id_string(field_type
->id
));
585 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
586 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
587 "packet-seq-num-ft-addr=%p", field_type
);
602 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
603 struct bt_ctf_stream_class_common
*stream_class
,
604 struct bt_ctf_field_type_common
*packet_context_type
,
605 bool check_ts_begin_end_mapped
)
607 bool is_valid
= true;
608 struct bt_ctf_field_type_common
*field_type
= NULL
;
610 if (!packet_context_type
) {
611 /* No packet context field type: valid at this point */
615 /* Packet context field type, if it exists, must be a structure */
616 if (packet_context_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
617 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
618 "ft-addr=%p, ft-id=%s",
620 bt_ctf_field_type_id_string(packet_context_type
->id
));
625 * If there's a `packet_size` field, it must be an unsigned
626 * integer field type.
628 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
629 packet_context_type
, "packet_size");
631 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
632 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
633 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
635 bt_ctf_field_type_id_string(field_type
->id
));
639 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
640 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
641 "packet-size-ft-addr=%p", field_type
);
647 * If there's a `content_size` field, it must be an unsigned
648 * integer field type.
650 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
651 packet_context_type
, "content_size");
653 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
654 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
655 "content-size-ft-addr=%p, content-size-ft-id=%s",
657 bt_ctf_field_type_id_string(field_type
->id
));
661 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
662 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
663 "content-size-ft-addr=%p", field_type
);
669 * If there's a `events_discarded` field, it must be an unsigned
670 * integer field type.
672 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
673 packet_context_type
, "events_discarded");
675 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
676 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
677 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
679 bt_ctf_field_type_id_string(field_type
->id
));
683 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
684 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
685 "events-discarded-ft-addr=%p", field_type
);
691 * If there's a `timestamp_begin` field, it must be an unsigned
692 * integer field type. Also, if the trace is not a CTF writer's
693 * trace, then we cannot automatically set the mapped clock
694 * class of this field, so it must have a mapped clock class.
696 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
697 packet_context_type
, "timestamp_begin");
699 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
700 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
701 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
703 bt_ctf_field_type_id_string(field_type
->id
));
707 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
708 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
709 "timestamp-begin-ft-addr=%p", field_type
);
713 if (check_ts_begin_end_mapped
) {
714 struct bt_ctf_clock_class
*clock_class
=
715 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
719 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
720 "timestamp-begin-ft-addr=%p", field_type
);
727 * If there's a `timestamp_end` field, it must be an unsigned
728 * integer field type. Also, if the trace is not a CTF writer's
729 * trace, then we cannot automatically set the mapped clock
730 * class of this field, so it must have a mapped clock class.
732 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
733 packet_context_type
, "timestamp_end");
735 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
736 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
737 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
739 bt_ctf_field_type_id_string(field_type
->id
));
743 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
744 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
745 "timestamp-end-ft-addr=%p", field_type
);
749 if (check_ts_begin_end_mapped
) {
750 struct bt_ctf_clock_class
*clock_class
=
751 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
755 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
756 "timestamp-end-ft-addr=%p", field_type
);
772 bool event_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
773 struct bt_ctf_stream_class_common
*stream_class
,
774 struct bt_ctf_field_type_common
*event_header_type
)
776 bool is_valid
= true;
777 struct bt_ctf_field_type_common
*field_type
= NULL
;
780 * We do not validate that the `timestamp` field exists here
781 * because CTF does not require this exact name to be mapped to
785 if (!event_header_type
) {
787 * No event header field type: stream class must have
788 * only one event class.
790 if (bt_ctf_stream_class_common_get_event_class_count(stream_class
) > 1) {
791 BT_LOGW_STR("Invalid event header field type: "
792 "event header field type does not exist but there's more than one event class in the stream class.");
796 /* No event header field type: valid at this point */
800 /* Event header field type, if it exists, must be a structure */
801 if (event_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
802 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
803 "ft-addr=%p, ft-id=%s",
805 bt_ctf_field_type_id_string(event_header_type
->id
));
810 * If there's an `id` field, it must be an unsigned integer
811 * field type or an enumeration field type with an unsigned
812 * integer container field type.
814 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
815 event_header_type
, "id");
817 struct bt_ctf_field_type_common
*int_ft
;
819 if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
821 } else if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
822 int_ft
= bt_ctf_field_type_common_enumeration_borrow_container_field_type(
825 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
826 "id-ft-addr=%p, id-ft-id=%s",
828 bt_ctf_field_type_id_string(field_type
->id
));
833 if (bt_ctf_field_type_common_integer_is_signed(int_ft
)) {
834 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
835 "id-ft-addr=%p", int_ft
);
850 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common
*trace
)
854 if (trace
->packet_header_field_type
) {
855 struct bt_ctf_clock_class
*clock_class
= NULL
;
857 ret
= bt_ctf_field_type_common_validate_single_clock_class(
858 trace
->packet_header_field_type
,
860 bt_ctf_object_put_ref(clock_class
);
861 if (ret
|| clock_class
) {
862 BT_LOGW("Trace's packet header field type cannot "
863 "contain a field type which is mapped to "
865 "trace-addr=%p, trace-name=\"%s\", "
866 "clock-class-name=\"%s\"",
867 trace
, bt_ctf_trace_common_get_name(trace
),
869 bt_ctf_clock_class_get_name(clock_class
) :
879 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
880 struct bt_ctf_stream_class_common
*stream_class
,
881 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
882 struct bt_ctf_clock_class
*init_expected_clock_class
,
883 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
884 struct bt_ctf_field_type_common
*packet_context_field_type
,
885 struct bt_ctf_field_type_common
*event_header_field_type
),
886 bool check_ts_begin_end_mapped
)
891 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
892 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
893 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
894 BT_CTF_VALIDATION_FLAG_TRACE
|
895 BT_CTF_VALIDATION_FLAG_STREAM
;
896 const enum bt_ctf_validation_flag ec_validation_flags
=
897 BT_CTF_VALIDATION_FLAG_EVENT
;
898 struct bt_ctf_field_type_common
*packet_header_type
= NULL
;
899 struct bt_ctf_field_type_common
*packet_context_type
= NULL
;
900 struct bt_ctf_field_type_common
*event_header_type
= NULL
;
901 struct bt_ctf_field_type_common
*stream_event_ctx_type
= NULL
;
902 int64_t event_class_count
;
903 struct bt_ctf_trace_common
*current_parent_trace
= NULL
;
904 struct bt_ctf_clock_class
*expected_clock_class
=
905 bt_ctf_object_get_ref(init_expected_clock_class
);
907 BT_ASSERT(copy_field_type_func
);
910 BT_LOGW_STR("Invalid parameter: trace is NULL.");
916 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
921 BT_LOGD("Adding stream class to trace: "
922 "trace-addr=%p, trace-name=\"%s\", "
923 "stream-class-addr=%p, stream-class-name=\"%s\", "
924 "stream-class-id=%" PRId64
,
925 trace
, bt_ctf_trace_common_get_name(trace
),
926 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
927 bt_ctf_stream_class_common_get_id(stream_class
));
929 current_parent_trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
930 if (current_parent_trace
) {
931 /* Stream class is already associated to a trace, abort. */
932 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
933 "stream-class-trace-addr=%p, "
934 "stream-class-trace-name=\"%s\"",
935 current_parent_trace
,
936 bt_ctf_trace_common_get_name(current_parent_trace
));
942 bt_ctf_stream_class_common_get_event_class_count(stream_class
);
943 BT_ASSERT(event_class_count
>= 0);
945 if (!stream_class
->frozen
) {
947 * Stream class is not frozen yet. Validate that the
948 * stream class contains at most a single clock class
949 * because the previous
950 * bt_ctf_stream_class_common_add_event_class() calls did
951 * not make this validation since the stream class's
952 * direct field types (packet context, event header,
953 * event context) could change afterwards. This stream
954 * class is about to be frozen and those field types
955 * won't be changed if this function succeeds.
957 * At this point we're also sure that the stream class's
958 * clock, if any, has the same class as the stream
959 * class's expected clock class, if any. This is why, if
960 * bt_ctf_stream_class_common_validate_single_clock_class()
961 * succeeds below, the call to
962 * bt_ctf_stream_class_map_clock_class() at the end of this
963 * function is safe because it maps to the same, single
966 ret
= bt_ctf_stream_class_common_validate_single_clock_class(
967 stream_class
, &expected_clock_class
);
969 BT_LOGW("Invalid parameter: stream class or one of its "
970 "event classes contains a field type which is "
971 "not recursively mapped to the expected "
973 "stream-class-addr=%p, "
974 "stream-class-id=%" PRId64
", "
975 "stream-class-name=\"%s\", "
976 "expected-clock-class-addr=%p, "
977 "expected-clock-class-name=\"%s\"",
978 stream_class
, bt_ctf_stream_class_common_get_id(stream_class
),
979 bt_ctf_stream_class_common_get_name(stream_class
),
980 expected_clock_class
,
981 expected_clock_class
?
982 bt_ctf_clock_class_get_name(expected_clock_class
) :
988 ret
= check_packet_header_type_has_no_clock_class(trace
);
990 /* check_packet_header_type_has_no_clock_class() logs errors */
995 * We're about to freeze both the trace and the stream class.
996 * Also, each event class contained in this stream class are
999 * This trace, this stream class, and all its event classes
1000 * should be valid at this point.
1002 * Validate trace and stream class first, then each event
1003 * class of this stream class can be validated individually.
1005 packet_header_type
=
1006 bt_ctf_trace_common_borrow_packet_header_field_type(trace
);
1007 packet_context_type
=
1008 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class
);
1010 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class
);
1011 stream_event_ctx_type
=
1012 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class
);
1014 BT_LOGD("Validating trace and stream class field types.");
1015 ret
= bt_ctf_validate_class_types(trace
->environment
,
1016 packet_header_type
, packet_context_type
, event_header_type
,
1017 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1018 stream_class
->valid
, 1, &trace_sc_validation_output
,
1019 trace_sc_validation_flags
, copy_field_type_func
);
1023 * This means something went wrong during the validation
1024 * process, not that the objects are invalid.
1026 BT_LOGE("Failed to validate trace and stream class field types: "
1031 if ((trace_sc_validation_output
.valid_flags
&
1032 trace_sc_validation_flags
) !=
1033 trace_sc_validation_flags
) {
1034 /* Invalid trace/stream class */
1035 BT_LOGW("Invalid trace or stream class field types: "
1037 trace_sc_validation_output
.valid_flags
);
1042 if (event_class_count
> 0) {
1043 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1045 if (!ec_validation_outputs
) {
1046 BT_LOGE_STR("Failed to allocate one validation output structure.");
1052 /* Validate each event class individually */
1053 for (i
= 0; i
< event_class_count
; i
++) {
1054 struct bt_ctf_event_class_common
*event_class
=
1055 bt_ctf_stream_class_common_borrow_event_class_by_index(
1057 struct bt_ctf_field_type_common
*event_context_type
= NULL
;
1058 struct bt_ctf_field_type_common
*event_payload_type
= NULL
;
1060 event_context_type
=
1061 bt_ctf_event_class_common_borrow_context_field_type(
1063 event_payload_type
=
1064 bt_ctf_event_class_common_borrow_payload_field_type(
1068 * It is important to use the field types returned by
1069 * the previous trace and stream class validation here
1070 * because copies could have been made.
1072 BT_LOGD("Validating event class's field types: "
1073 "addr=%p, name=\"%s\", id=%" PRId64
,
1074 event_class
, bt_ctf_event_class_common_get_name(event_class
),
1075 bt_ctf_event_class_common_get_id(event_class
));
1076 ret
= bt_ctf_validate_class_types(trace
->environment
,
1077 trace_sc_validation_output
.packet_header_type
,
1078 trace_sc_validation_output
.packet_context_type
,
1079 trace_sc_validation_output
.event_header_type
,
1080 trace_sc_validation_output
.stream_event_ctx_type
,
1081 event_context_type
, event_payload_type
,
1082 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1083 ec_validation_flags
, copy_field_type_func
);
1086 BT_LOGE("Failed to validate event class field types: "
1091 if ((ec_validation_outputs
[i
].valid_flags
&
1092 ec_validation_flags
) != ec_validation_flags
) {
1093 /* Invalid event class */
1094 BT_LOGW("Invalid event class field types: "
1096 ec_validation_outputs
[i
].valid_flags
);
1102 stream_id
= bt_ctf_stream_class_common_get_id(stream_class
);
1103 if (stream_id
< 0) {
1104 stream_id
= trace
->next_stream_id
++;
1105 if (stream_id
< 0) {
1106 BT_LOGE_STR("No more stream class IDs available.");
1111 /* Try to assign a new stream id */
1112 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1113 if (stream_id
== bt_ctf_stream_class_common_get_id(
1114 trace
->stream_classes
->pdata
[i
])) {
1115 /* Duplicate stream id found */
1116 BT_LOGW("Duplicate stream class ID: "
1117 "id=%" PRId64
, (int64_t) stream_id
);
1123 if (bt_ctf_stream_class_common_set_id_no_check(stream_class
,
1125 /* TODO Should retry with a different stream id */
1126 BT_LOGE("Cannot set stream class's ID: "
1127 "id=%" PRId64
, (int64_t) stream_id
);
1134 * At this point all the field types in the validation output
1135 * are valid. Validate the semantics of some scopes according to
1136 * the CTF specification.
1138 if (!packet_header_field_type_is_valid(trace
,
1139 trace_sc_validation_output
.packet_header_type
)) {
1140 BT_LOGW_STR("Invalid trace's packet header field type.");
1145 if (!packet_context_field_type_is_valid(trace
,
1147 trace_sc_validation_output
.packet_context_type
,
1148 check_ts_begin_end_mapped
)) {
1149 BT_LOGW_STR("Invalid stream class's packet context field type.");
1154 if (!event_header_field_type_is_valid(trace
,
1156 trace_sc_validation_output
.event_header_type
)) {
1157 BT_LOGW_STR("Invalid steam class's event header field type.");
1163 * Now is the time to automatically map specific field types of
1164 * the stream class's packet context and event header field
1165 * types to the stream class's clock's class if they are not
1166 * mapped to a clock class yet. We do it here because we know
1167 * that after this point, everything is frozen so it won't be
1168 * possible for the user to modify the stream class's clock, or
1169 * to map those field types to other clock classes.
1171 if (map_clock_classes_func
) {
1172 if (map_clock_classes_func(stream_class
,
1173 trace_sc_validation_output
.packet_context_type
,
1174 trace_sc_validation_output
.event_header_type
)) {
1175 /* map_clock_classes_func() logs errors */
1181 bt_ctf_object_set_parent(&stream_class
->base
, &trace
->base
);
1182 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1185 * At this point we know that the function will be successful.
1186 * Therefore we can replace the trace and stream class field
1187 * types with what's in their validation output structure and
1188 * mark them as valid. We can also replace the field types of
1189 * all the event classes of the stream class and mark them as
1192 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1193 &trace_sc_validation_output
, trace_sc_validation_flags
);
1195 stream_class
->valid
= 1;
1198 * Put what was not moved in bt_ctf_validation_replace_types().
1200 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1202 for (i
= 0; i
< event_class_count
; i
++) {
1203 struct bt_ctf_event_class_common
*event_class
=
1204 bt_ctf_stream_class_common_borrow_event_class_by_index(
1207 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1208 &ec_validation_outputs
[i
], ec_validation_flags
);
1209 event_class
->valid
= 1;
1212 * Put what was not moved in
1213 * bt_ctf_validation_replace_types().
1215 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1219 * Freeze the trace and the stream class.
1221 bt_ctf_stream_class_common_freeze(stream_class
);
1222 bt_ctf_trace_common_freeze(trace
);
1225 * It is safe to set the stream class's unique clock class
1226 * now because the stream class is frozen.
1228 if (expected_clock_class
) {
1229 BT_CTF_OBJECT_MOVE_REF(stream_class
->clock_class
, expected_clock_class
);
1232 BT_LOGD("Added stream class to trace: "
1233 "trace-addr=%p, trace-name=\"%s\", "
1234 "stream-class-addr=%p, stream-class-name=\"%s\", "
1235 "stream-class-id=%" PRId64
,
1236 trace
, bt_ctf_trace_common_get_name(trace
),
1237 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
1238 bt_ctf_stream_class_common_get_id(stream_class
));
1242 bt_ctf_object_set_parent(&stream_class
->base
, NULL
);
1244 if (ec_validation_outputs
) {
1245 for (i
= 0; i
< event_class_count
; i
++) {
1246 bt_ctf_validation_output_put_types(
1247 &ec_validation_outputs
[i
]);
1252 g_free(ec_validation_outputs
);
1253 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1254 bt_ctf_object_put_ref(expected_clock_class
);
1259 bt_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
1260 struct bt_ctf_clock_class
*clock_class
)
1262 struct bt_ctf_search_query query
= { .value
= clock_class
, .found
= 0 };
1265 BT_ASSERT(clock_class
);
1267 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1272 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
1273 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
)
1278 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1283 if (trace
->frozen
) {
1284 BT_LOGW("Invalid parameter: trace is frozen: "
1285 "addr=%p, name=\"%s\"",
1286 trace
, bt_ctf_trace_common_get_name(trace
));
1291 if (byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1292 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1293 "addr=%p, name=\"%s\"",
1294 trace
, bt_ctf_trace_common_get_name(trace
));
1299 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1300 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1301 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1302 BT_LOGW("Invalid parameter: invalid byte order: "
1303 "addr=%p, name=\"%s\", bo=%s",
1304 trace
, bt_ctf_trace_common_get_name(trace
),
1305 bt_ctf_byte_order_string(byte_order
));
1310 trace
->native_byte_order
= byte_order
;
1311 BT_LOGT("Set trace's native byte order: "
1312 "addr=%p, name=\"%s\", bo=%s",
1313 trace
, bt_ctf_trace_common_get_name(trace
),
1314 bt_ctf_byte_order_string(byte_order
));
1321 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
1322 struct bt_ctf_field_type_common
*packet_header_type
)
1327 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1332 if (trace
->frozen
) {
1333 BT_LOGW("Invalid parameter: trace is frozen: "
1334 "addr=%p, name=\"%s\"",
1335 trace
, bt_ctf_trace_common_get_name(trace
));
1340 /* packet_header_type must be a structure. */
1341 if (packet_header_type
&&
1342 packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1343 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1344 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1345 trace
, bt_ctf_trace_common_get_name(trace
),
1347 bt_ctf_field_type_id_string(packet_header_type
->id
));
1352 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
1353 trace
->packet_header_field_type
= bt_ctf_object_get_ref(packet_header_type
);
1354 BT_LOGT("Set trace's packet header field type: "
1355 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1356 trace
, bt_ctf_trace_common_get_name(trace
), packet_header_type
);
1362 int64_t get_stream_class_count(void *element
)
1364 return bt_ctf_trace_get_stream_class_count(
1365 (struct bt_ctf_trace
*) element
);
1369 void *get_stream_class(void *element
, int i
)
1371 return bt_ctf_trace_get_stream_class_by_index(
1372 (struct bt_ctf_trace
*) element
, i
);
1376 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1378 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1381 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1382 bt_ctf_visitor visitor
, void *data
)
1385 struct bt_ctf_visitor_object obj
= {
1387 .type
= BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1391 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1397 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1402 BT_LOGT("Visiting trace: addr=%p, name=\"%s\"",
1403 trace
, bt_ctf_trace_get_name(trace
));
1404 ret
= bt_ctf_visitor_helper(&obj
, get_stream_class_count
,
1405 get_stream_class
, visit_stream_class
, visitor
, data
);
1411 void bt_ctf_trace_destroy(struct bt_ctf_object
*obj
)
1413 struct bt_ctf_trace
*trace
= (void *) obj
;
1415 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1416 trace
, bt_ctf_trace_get_name(trace
));
1417 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace
));
1422 struct bt_ctf_trace
*bt_ctf_trace_create(void)
1424 struct bt_ctf_trace
*trace
= NULL
;
1427 BT_LOGD_STR("Creating CTF writer trace object.");
1428 trace
= g_new0(struct bt_ctf_trace
, 1);
1430 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1434 ret
= bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace
),
1435 bt_ctf_trace_destroy
);
1437 /* bt_ctf_trace_common_initialize() logs errors */
1441 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
1445 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1449 const uint8_t *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
1451 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace
));
1454 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
1455 const uint8_t *uuid
)
1457 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace
), uuid
);
1460 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
1461 const char *name
, const char *value
)
1463 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace
),
1467 int bt_ctf_trace_set_environment_field_integer(
1468 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
1470 return bt_ctf_trace_common_set_environment_field_integer(
1471 BT_CTF_TO_COMMON(trace
), name
, value
);
1474 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
1476 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace
));
1480 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
1483 return bt_ctf_trace_common_get_environment_field_name_by_index(
1484 BT_CTF_TO_COMMON(trace
), index
);
1487 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_index(
1488 struct bt_ctf_trace
*trace
, uint64_t index
)
1490 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1491 BT_CTF_TO_COMMON(trace
), index
));
1494 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_name(
1495 struct bt_ctf_trace
*trace
, const char *name
)
1497 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1498 BT_CTF_TO_COMMON(trace
), name
));
1502 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
1503 struct bt_ctf_clock_class
*clock_class
)
1505 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace
),
1506 (void *) clock_class
);
1510 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
1512 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace
));
1516 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
1517 struct bt_ctf_trace
*trace
, uint64_t index
)
1519 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index(
1520 BT_CTF_TO_COMMON(trace
), index
));
1524 int map_clock_classes_func(struct bt_ctf_stream_class_common
*stream_class
,
1525 struct bt_ctf_field_type_common
*packet_context_type
,
1526 struct bt_ctf_field_type_common
*event_header_type
)
1528 int ret
= bt_ctf_stream_class_map_clock_class(
1529 BT_CTF_FROM_COMMON(stream_class
),
1530 BT_CTF_FROM_COMMON(packet_context_type
),
1531 BT_CTF_FROM_COMMON(event_header_type
));
1534 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1540 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1541 struct bt_ctf_stream_class
*stream_class
)
1544 struct bt_ctf_clock_class
*expected_clock_class
= NULL
;
1547 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1552 if (!stream_class
) {
1553 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1558 if (stream_class
->clock
) {
1559 struct bt_ctf_clock_class
*stream_clock_class
=
1560 stream_class
->clock
->clock_class
;
1563 * Make sure this clock was also added to the
1564 * trace (potentially through its CTF writer
1569 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
1570 if (trace
->common
.clock_classes
->pdata
[i
] ==
1571 stream_clock_class
) {
1577 if (i
== trace
->common
.clock_classes
->len
) {
1579 BT_LOGW("Stream class's clock's class is not part of the trace: "
1580 "clock-class-addr=%p, clock-class-name=\"%s\"",
1582 bt_ctf_clock_class_get_name(stream_clock_class
));
1587 if (stream_class
->common
.clock_class
&&
1588 stream_class
->common
.clock_class
!=
1589 stream_class
->clock
->clock_class
) {
1591 * Stream class already has an expected clock
1592 * class, but it does not match its clock's
1595 BT_LOGW("Invalid parameter: stream class's clock's "
1596 "class does not match stream class's "
1597 "expected clock class: "
1598 "stream-class-addr=%p, "
1599 "stream-class-id=%" PRId64
", "
1600 "stream-class-name=\"%s\", "
1601 "expected-clock-class-addr=%p, "
1602 "expected-clock-class-name=\"%s\"",
1604 bt_ctf_stream_class_get_id(stream_class
),
1605 bt_ctf_stream_class_get_name(stream_class
),
1606 stream_class
->common
.clock_class
,
1607 bt_ctf_clock_class_get_name(stream_class
->common
.clock_class
));
1608 } else if (!stream_class
->common
.clock_class
) {
1610 * Set expected clock class to stream class's
1613 expected_clock_class
= stream_class
->clock
->clock_class
;
1618 ret
= bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace
),
1619 BT_CTF_TO_COMMON(stream_class
),
1620 (bt_ctf_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
1621 expected_clock_class
, map_clock_classes_func
,
1628 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1630 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace
));
1633 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1634 struct bt_ctf_trace
*trace
, uint64_t index
)
1636 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index(
1637 BT_CTF_TO_COMMON(trace
), index
));
1640 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1642 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace
));
1645 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1646 struct bt_ctf_trace
*trace
, uint64_t index
)
1648 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index(
1649 BT_CTF_TO_COMMON(trace
), index
));
1652 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1653 struct bt_ctf_trace
*trace
, uint64_t id
)
1655 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id(
1656 BT_CTF_TO_COMMON(trace
), id
));
1660 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1661 struct bt_ctf_trace
*trace
, const char *name
)
1663 return bt_ctf_object_get_ref(
1664 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace
),
1669 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1670 struct metadata_context
*context
)
1672 uint8_t *uuid
= trace
->common
.uuid
;
1675 if (trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1676 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1677 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1678 "set it with bt_ctf_trace_set_native_byte_order(): "
1679 "addr=%p, name=\"%s\"",
1680 trace
, bt_ctf_trace_get_name(trace
));
1685 g_string_append(context
->string
, "trace {\n");
1686 g_string_append(context
->string
, "\tmajor = 1;\n");
1687 g_string_append(context
->string
, "\tminor = 8;\n");
1688 BT_ASSERT(trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1689 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1690 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1692 if (trace
->common
.uuid_set
) {
1693 g_string_append_printf(context
->string
,
1694 "\tuuid = \"" BT_UUID_FMT
"\";\n",
1695 BT_UUID_FMT_VALUES(uuid
));
1698 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1699 bt_ctf_get_byte_order_string(trace
->common
.native_byte_order
));
1701 if (trace
->common
.packet_header_field_type
) {
1702 g_string_append(context
->string
, "\tpacket.header := ");
1703 context
->current_indentation_level
++;
1704 g_string_assign(context
->field_name
, "");
1705 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1706 ret
= bt_ctf_field_type_serialize_recursive(
1707 (void *) trace
->common
.packet_header_field_type
,
1712 context
->current_indentation_level
--;
1715 g_string_append(context
->string
, ";\n};\n\n");
1721 void append_env_metadata(struct bt_ctf_trace
*trace
,
1722 struct metadata_context
*context
)
1727 env_size
= bt_ctf_attributes_get_count(trace
->common
.environment
);
1728 if (env_size
<= 0) {
1732 g_string_append(context
->string
, "env {\n");
1734 for (i
= 0; i
< env_size
; i
++) {
1735 struct bt_ctf_private_value
*env_field_value_obj
= NULL
;
1736 const char *entry_name
;
1738 entry_name
= bt_ctf_attributes_get_field_name(
1739 trace
->common
.environment
, i
);
1740 env_field_value_obj
= bt_ctf_attributes_borrow_field_value(
1741 trace
->common
.environment
, i
);
1743 BT_ASSERT(entry_name
);
1744 BT_ASSERT(env_field_value_obj
);
1746 switch (bt_ctf_value_get_type(
1747 bt_ctf_private_value_as_value(env_field_value_obj
))) {
1748 case BT_CTF_VALUE_TYPE_INTEGER
:
1752 int_value
= bt_ctf_value_integer_get(
1753 bt_ctf_private_value_as_value(
1754 env_field_value_obj
));
1755 g_string_append_printf(context
->string
,
1756 "\t%s = %" PRId64
";\n", entry_name
,
1760 case BT_CTF_VALUE_TYPE_STRING
:
1762 const char *str_value
;
1763 char *escaped_str
= NULL
;
1765 str_value
= bt_ctf_value_string_get(
1766 bt_ctf_private_value_as_value(
1767 env_field_value_obj
));
1768 escaped_str
= g_strescape(str_value
, NULL
);
1770 BT_LOGE("Cannot escape string: string=\"%s\"",
1775 g_string_append_printf(context
->string
,
1776 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1785 g_string_append(context
->string
, "};\n\n");
1788 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1790 char *metadata
= NULL
;
1791 struct metadata_context
*context
= NULL
;
1796 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1800 context
= g_new0(struct metadata_context
, 1);
1802 BT_LOGE_STR("Failed to allocate one metadata context.");
1806 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1807 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1808 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1809 if (append_trace_metadata(trace
, context
)) {
1810 /* append_trace_metadata() logs errors */
1813 append_env_metadata(trace
, context
);
1814 g_ptr_array_foreach(trace
->common
.clock_classes
,
1815 (GFunc
) bt_ctf_clock_class_serialize
, context
);
1817 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
1818 /* bt_ctf_stream_class_serialize() logs details */
1819 err
= bt_ctf_stream_class_serialize(
1820 trace
->common
.stream_classes
->pdata
[i
], context
);
1822 /* bt_ctf_stream_class_serialize() logs errors */
1827 metadata
= context
->string
->str
;
1830 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1831 g_string_free(context
->field_name
, TRUE
);
1838 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1839 struct bt_ctf_trace
*trace
)
1841 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace
));
1844 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1845 enum bt_ctf_byte_order byte_order
)
1847 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace
),
1848 (int) byte_order
, false);
1851 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
1852 struct bt_ctf_trace
*trace
)
1854 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type(
1855 BT_CTF_TO_COMMON(trace
)));
1858 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
1859 struct bt_ctf_field_type
*packet_header_type
)
1861 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace
),
1862 (void *) packet_header_type
);
1865 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
1867 return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace
));