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"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-internal.h>
28 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/ctf-writer/attributes-internal.h>
30 #include <babeltrace/ctf-writer/clock-class-internal.h>
31 #include <babeltrace/ctf-writer/clock-internal.h>
32 #include <babeltrace/ctf-writer/event-class-internal.h>
33 #include <babeltrace/ctf-writer/event.h>
34 #include <babeltrace/ctf-writer/event-internal.h>
35 #include <babeltrace/ctf-writer/field-types-internal.h>
36 #include <babeltrace/ctf-writer/field-wrapper-internal.h>
37 #include <babeltrace/ctf-writer/functor-internal.h>
38 #include <babeltrace/ctf-writer/stream-class-internal.h>
39 #include <babeltrace/ctf-writer/stream-internal.h>
40 #include <babeltrace/ctf-writer/trace-internal.h>
41 #include <babeltrace/ctf-writer/utils-internal.h>
42 #include <babeltrace/ctf-writer/utils.h>
43 #include <babeltrace/ctf-writer/validation-internal.h>
44 #include <babeltrace/ctf-writer/visitor-internal.h>
45 #include <babeltrace/ctf-writer/writer-internal.h>
46 #include <babeltrace/endian-internal.h>
47 #include <babeltrace/ref.h>
48 #include <babeltrace/types.h>
49 #include <babeltrace/values-internal.h>
50 #include <babeltrace/values.h>
56 #define DEFAULT_IDENTIFIER_SIZE 128
57 #define DEFAULT_METADATA_STRING_SIZE 4096
60 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
61 bt_object_release_func release_func
)
65 BT_LOGD_STR("Initializing common trace object.");
66 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_UNSPECIFIED
;
67 bt_object_init_shared_with_parent(&trace
->base
, release_func
);
68 trace
->clock_classes
= g_ptr_array_new_with_free_func(
69 (GDestroyNotify
) bt_put
);
70 if (!trace
->clock_classes
) {
71 BT_LOGE_STR("Failed to allocate one GPtrArray.");
75 trace
->streams
= g_ptr_array_new_with_free_func(
76 (GDestroyNotify
) bt_object_try_spec_release
);
77 if (!trace
->streams
) {
78 BT_LOGE_STR("Failed to allocate one GPtrArray.");
82 trace
->stream_classes
= g_ptr_array_new_with_free_func(
83 (GDestroyNotify
) bt_object_try_spec_release
);
84 if (!trace
->stream_classes
) {
85 BT_LOGE_STR("Failed to allocate one GPtrArray.");
89 /* Create the environment array object */
90 trace
->environment
= bt_ctf_attributes_create();
91 if (!trace
->environment
) {
92 BT_LOGE_STR("Cannot create empty attributes object.");
96 BT_LOGD("Initialized common trace object: addr=%p", trace
);
107 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
)
109 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
110 trace
, bt_ctf_trace_common_get_name(trace
));
112 if (trace
->environment
) {
113 BT_LOGD_STR("Destroying environment attributes.");
114 bt_ctf_attributes_destroy(trace
->environment
);
118 g_string_free(trace
->name
, TRUE
);
121 if (trace
->clock_classes
) {
122 BT_LOGD_STR("Putting clock classes.");
123 g_ptr_array_free(trace
->clock_classes
, TRUE
);
126 if (trace
->streams
) {
127 BT_LOGD_STR("Destroying streams.");
128 g_ptr_array_free(trace
->streams
, TRUE
);
131 if (trace
->stream_classes
) {
132 BT_LOGD_STR("Destroying stream classes.");
133 g_ptr_array_free(trace
->stream_classes
, TRUE
);
136 BT_LOGD_STR("Putting packet header field type.");
137 bt_put(trace
->packet_header_field_type
);
141 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
)
146 BT_LOGW_STR("Invalid parameter: trace is NULL.");
152 BT_LOGW_STR("Invalid parameter: name is NULL.");
158 BT_LOGW("Invalid parameter: trace is frozen: "
159 "addr=%p, name=\"%s\"",
160 trace
, bt_ctf_trace_common_get_name(trace
));
165 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
168 BT_LOGE_STR("Failed to allocate one GString.");
173 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
180 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
,
181 const unsigned char *uuid
)
186 BT_LOGW_STR("Invalid parameter: trace is NULL.");
192 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
198 BT_LOGW("Invalid parameter: trace is frozen: "
199 "addr=%p, name=\"%s\"",
200 trace
, bt_ctf_trace_common_get_name(trace
));
205 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
206 trace
->uuid_set
= BT_TRUE
;
207 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
208 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
209 trace
, bt_ctf_trace_common_get_name(trace
),
210 (unsigned int) uuid
[0],
211 (unsigned int) uuid
[1],
212 (unsigned int) uuid
[2],
213 (unsigned int) uuid
[3],
214 (unsigned int) uuid
[4],
215 (unsigned int) uuid
[5],
216 (unsigned int) uuid
[6],
217 (unsigned int) uuid
[7],
218 (unsigned int) uuid
[8],
219 (unsigned int) uuid
[9],
220 (unsigned int) uuid
[10],
221 (unsigned int) uuid
[11],
222 (unsigned int) uuid
[12],
223 (unsigned int) uuid
[13],
224 (unsigned int) uuid
[14],
225 (unsigned int) uuid
[15]);
232 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
233 const char *name
, struct bt_value
*value
)
238 BT_LOGW_STR("Invalid parameter: trace is NULL.");
244 BT_LOGW_STR("Invalid parameter: name is NULL.");
250 BT_LOGW_STR("Invalid parameter: value is NULL.");
255 if (!bt_ctf_identifier_is_valid(name
)) {
256 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
257 "trace-addr=%p, trace-name=\"%s\", "
259 trace
, bt_ctf_trace_common_get_name(trace
), name
);
264 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
265 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
266 "trace-addr=%p, trace-name=\"%s\", "
267 "env-name=\"%s\", env-value-type=%s",
268 trace
, bt_ctf_trace_common_get_name(trace
), name
,
269 bt_value_type_string(bt_value_get_type(value
)));
276 * New environment fields may be added to a frozen trace,
277 * but existing fields may not be changed.
279 * The object passed is frozen like all other attributes.
281 struct bt_value
*attribute
=
282 bt_ctf_attributes_borrow_field_value_by_name(
283 trace
->environment
, name
);
286 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
287 "trace-addr=%p, trace-name=\"%s\", "
289 trace
, bt_ctf_trace_common_get_name(trace
), name
);
294 bt_value_freeze(value
);
297 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
300 BT_LOGE("Cannot set environment field's value: "
301 "trace-addr=%p, trace-name=\"%s\", "
303 trace
, bt_ctf_trace_common_get_name(trace
), name
);
305 BT_LOGV("Set environment field's value: "
306 "trace-addr=%p, trace-name=\"%s\", "
307 "env-name=\"%s\", value-addr=%p",
308 trace
, bt_ctf_trace_common_get_name(trace
), name
, value
);
316 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
317 const char *name
, const char *value
)
320 struct bt_value
*env_value_string_obj
= NULL
;
323 BT_LOGW_STR("Invalid parameter: value is NULL.");
328 env_value_string_obj
= bt_value_string_create_init(value
);
329 if (!env_value_string_obj
) {
330 BT_LOGE_STR("Cannot create string value object.");
335 /* bt_ctf_trace_common_set_environment_field() logs errors */
336 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
337 env_value_string_obj
);
340 bt_put(env_value_string_obj
);
345 int bt_ctf_trace_common_set_environment_field_integer(
346 struct bt_ctf_trace_common
*trace
, const char *name
, int64_t value
)
349 struct bt_value
*env_value_integer_obj
= NULL
;
351 env_value_integer_obj
= bt_value_integer_create_init(value
);
352 if (!env_value_integer_obj
) {
353 BT_LOGE_STR("Cannot create integer value object.");
358 /* bt_ctf_trace_common_set_environment_field() logs errors */
359 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
360 env_value_integer_obj
);
363 bt_put(env_value_integer_obj
);
368 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
369 struct bt_ctf_clock_class
*clock_class
)
374 BT_LOGW_STR("Invalid parameter: trace is NULL.");
379 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
380 BT_LOGW("Invalid parameter: clock class is invalid: "
381 "trace-addr=%p, trace-name=\"%s\", "
382 "clock-class-addr=%p, clock-class-name=\"%s\"",
383 trace
, bt_ctf_trace_common_get_name(trace
),
384 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
389 /* Check for duplicate clock classes */
390 if (bt_ctf_trace_common_has_clock_class(trace
, clock_class
)) {
391 BT_LOGW("Invalid parameter: clock class already exists in trace: "
392 "trace-addr=%p, trace-name=\"%s\", "
393 "clock-class-addr=%p, clock-class-name=\"%s\"",
394 trace
, bt_ctf_trace_common_get_name(trace
),
395 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
401 g_ptr_array_add(trace
->clock_classes
, clock_class
);
404 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
405 bt_ctf_clock_class_freeze(clock_class
);
408 BT_LOGV("Added clock class to trace: "
409 "trace-addr=%p, trace-name=\"%s\", "
410 "clock-class-addr=%p, clock-class-name=\"%s\"",
411 trace
, bt_ctf_trace_common_get_name(trace
),
412 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
419 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
420 struct bt_ctf_field_type_common
*packet_header_type
)
423 bool is_valid
= true;
424 struct bt_ctf_field_type_common
*field_type
= NULL
;
426 if (!packet_header_type
) {
428 * No packet header field type: trace must have only
429 * one stream. At this point the stream class being
430 * added is not part of the trace yet, so we validate
431 * that the trace contains no stream classes yet.
433 if (trace
->stream_classes
->len
>= 1) {
434 BT_LOGW_STR("Invalid packet header field type: "
435 "packet header field type does not exist but there's more than one stream class in the trace.");
439 /* No packet header field type: valid at this point */
443 /* Packet header field type, if it exists, must be a structure */
444 if (packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
445 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
446 "ft-addr=%p, ft-id=%s",
448 bt_ctf_field_type_id_string(packet_header_type
->id
));
453 * If there's a `magic` field, it must be a 32-bit unsigned
454 * integer field type. Also it must be the first field of the
455 * packet header field type.
457 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
458 packet_header_type
, "magic");
460 const char *field_name
;
462 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
463 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
464 "magic-ft-addr=%p, magic-ft-id=%s",
466 bt_ctf_field_type_id_string(field_type
->id
));
470 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
471 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
472 "magic-ft-addr=%p", field_type
);
476 if (bt_ctf_field_type_common_integer_get_size(field_type
) != 32) {
477 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
478 "magic-ft-addr=%p, magic-ft-size=%u",
480 bt_ctf_field_type_common_integer_get_size(field_type
));
484 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
485 packet_header_type
, &field_name
, NULL
, 0);
488 if (strcmp(field_name
, "magic") != 0) {
489 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
490 "magic-ft-addr=%p, first-field-name=\"%s\"",
491 field_type
, field_name
);
497 * If there's a `uuid` field, it must be an array field type of
498 * length 16 with an 8-bit unsigned integer element field type.
500 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
501 packet_header_type
, "uuid");
503 struct bt_ctf_field_type_common
*elem_ft
;
505 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
506 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
507 "uuid-ft-addr=%p, uuid-ft-id=%s",
509 bt_ctf_field_type_id_string(field_type
->id
));
513 if (bt_ctf_field_type_common_array_get_length(field_type
) != 16) {
514 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
515 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
517 bt_ctf_field_type_common_array_get_length(field_type
));
521 elem_ft
= bt_ctf_field_type_common_array_borrow_element_field_type(field_type
);
524 if (elem_ft
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
525 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
526 "elem-ft-addr=%p, elem-ft-id=%s",
528 bt_ctf_field_type_id_string(elem_ft
->id
));
532 if (bt_ctf_field_type_common_integer_is_signed(elem_ft
)) {
533 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
534 "elem-ft-addr=%p", elem_ft
);
538 if (bt_ctf_field_type_common_integer_get_size(elem_ft
) != 8) {
539 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
540 "elem-ft-addr=%p, elem-ft-size=%u",
542 bt_ctf_field_type_common_integer_get_size(elem_ft
));
548 * The `stream_id` field must exist if there's more than one
549 * stream classes in the trace.
551 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
552 packet_header_type
, "stream_id");
554 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
555 BT_LOGW_STR("Invalid packet header field type: "
556 "`stream_id` field does not exist but there's more than one stream class in the trace.");
561 * If there's a `stream_id` field, it must be an unsigned
562 * integer field type.
565 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
566 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
567 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
569 bt_ctf_field_type_id_string(field_type
->id
));
573 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
574 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
575 "stream-id-ft-addr=%p", field_type
);
581 * If there's a `packet_seq_num` field, it must be an unsigned
582 * integer field type.
584 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
585 packet_header_type
, "packet_seq_num");
587 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
588 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
589 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
591 bt_ctf_field_type_id_string(field_type
->id
));
595 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
596 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
597 "packet-seq-num-ft-addr=%p", field_type
);
612 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
613 struct bt_ctf_stream_class_common
*stream_class
,
614 struct bt_ctf_field_type_common
*packet_context_type
,
615 bool check_ts_begin_end_mapped
)
617 bool is_valid
= true;
618 struct bt_ctf_field_type_common
*field_type
= NULL
;
620 if (!packet_context_type
) {
621 /* No packet context field type: valid at this point */
625 /* Packet context field type, if it exists, must be a structure */
626 if (packet_context_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
627 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
628 "ft-addr=%p, ft-id=%s",
630 bt_ctf_field_type_id_string(packet_context_type
->id
));
635 * If there's a `packet_size` field, it must be an unsigned
636 * integer field type.
638 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
639 packet_context_type
, "packet_size");
641 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
642 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
643 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
645 bt_ctf_field_type_id_string(field_type
->id
));
649 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
650 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
651 "packet-size-ft-addr=%p", field_type
);
657 * If there's a `content_size` field, it must be an unsigned
658 * integer field type.
660 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
661 packet_context_type
, "content_size");
663 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
664 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
665 "content-size-ft-addr=%p, content-size-ft-id=%s",
667 bt_ctf_field_type_id_string(field_type
->id
));
671 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
672 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
673 "content-size-ft-addr=%p", field_type
);
679 * If there's a `events_discarded` field, it must be an unsigned
680 * integer field type.
682 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
683 packet_context_type
, "events_discarded");
685 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
686 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
687 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
689 bt_ctf_field_type_id_string(field_type
->id
));
693 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
694 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
695 "events-discarded-ft-addr=%p", field_type
);
701 * If there's a `timestamp_begin` field, it must be an unsigned
702 * integer field type. Also, if the trace is not a CTF writer's
703 * trace, then we cannot automatically set the mapped clock
704 * class of this field, so it must have a mapped clock class.
706 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
707 packet_context_type
, "timestamp_begin");
709 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
710 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
711 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
713 bt_ctf_field_type_id_string(field_type
->id
));
717 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
718 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
719 "timestamp-begin-ft-addr=%p", field_type
);
723 if (check_ts_begin_end_mapped
) {
724 struct bt_ctf_clock_class
*clock_class
=
725 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
729 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
730 "timestamp-begin-ft-addr=%p", field_type
);
737 * If there's a `timestamp_end` field, it must be an unsigned
738 * integer field type. Also, if the trace is not a CTF writer's
739 * trace, then we cannot automatically set the mapped clock
740 * class of this field, so it must have a mapped clock class.
742 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
743 packet_context_type
, "timestamp_end");
745 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
746 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
747 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
749 bt_ctf_field_type_id_string(field_type
->id
));
753 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
754 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
755 "timestamp-end-ft-addr=%p", field_type
);
759 if (check_ts_begin_end_mapped
) {
760 struct bt_ctf_clock_class
*clock_class
=
761 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
765 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
766 "timestamp-end-ft-addr=%p", field_type
);
782 bool event_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
783 struct bt_ctf_stream_class_common
*stream_class
,
784 struct bt_ctf_field_type_common
*event_header_type
)
786 bool is_valid
= true;
787 struct bt_ctf_field_type_common
*field_type
= NULL
;
790 * We do not validate that the `timestamp` field exists here
791 * because CTF does not require this exact name to be mapped to
795 if (!event_header_type
) {
797 * No event header field type: stream class must have
798 * only one event class.
800 if (bt_ctf_stream_class_common_get_event_class_count(stream_class
) > 1) {
801 BT_LOGW_STR("Invalid event header field type: "
802 "event header field type does not exist but there's more than one event class in the stream class.");
806 /* No event header field type: valid at this point */
810 /* Event header field type, if it exists, must be a structure */
811 if (event_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
812 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
813 "ft-addr=%p, ft-id=%s",
815 bt_ctf_field_type_id_string(event_header_type
->id
));
820 * If there's an `id` field, it must be an unsigned integer
821 * field type or an enumeration field type with an unsigned
822 * integer container field type.
824 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
825 event_header_type
, "id");
827 struct bt_ctf_field_type_common
*int_ft
;
829 if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
831 } else if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
832 int_ft
= bt_ctf_field_type_common_enumeration_borrow_container_field_type(
835 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
836 "id-ft-addr=%p, id-ft-id=%s",
838 bt_ctf_field_type_id_string(field_type
->id
));
843 if (bt_ctf_field_type_common_integer_is_signed(int_ft
)) {
844 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
845 "id-ft-addr=%p", int_ft
);
860 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common
*trace
)
864 if (trace
->packet_header_field_type
) {
865 struct bt_ctf_clock_class
*clock_class
= NULL
;
867 ret
= bt_ctf_field_type_common_validate_single_clock_class(
868 trace
->packet_header_field_type
,
871 if (ret
|| clock_class
) {
872 BT_LOGW("Trace's packet header field type cannot "
873 "contain a field type which is mapped to "
875 "trace-addr=%p, trace-name=\"%s\", "
876 "clock-class-name=\"%s\"",
877 trace
, bt_ctf_trace_common_get_name(trace
),
879 bt_ctf_clock_class_get_name(clock_class
) :
889 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
890 struct bt_ctf_stream_class_common
*stream_class
,
891 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
892 struct bt_ctf_clock_class
*init_expected_clock_class
,
893 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
894 struct bt_ctf_field_type_common
*packet_context_field_type
,
895 struct bt_ctf_field_type_common
*event_header_field_type
),
896 bool check_ts_begin_end_mapped
)
901 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
902 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
903 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
904 BT_CTF_VALIDATION_FLAG_TRACE
|
905 BT_CTF_VALIDATION_FLAG_STREAM
;
906 const enum bt_ctf_validation_flag ec_validation_flags
=
907 BT_CTF_VALIDATION_FLAG_EVENT
;
908 struct bt_ctf_field_type_common
*packet_header_type
= NULL
;
909 struct bt_ctf_field_type_common
*packet_context_type
= NULL
;
910 struct bt_ctf_field_type_common
*event_header_type
= NULL
;
911 struct bt_ctf_field_type_common
*stream_event_ctx_type
= NULL
;
912 int64_t event_class_count
;
913 struct bt_ctf_trace_common
*current_parent_trace
= NULL
;
914 struct bt_ctf_clock_class
*expected_clock_class
=
915 bt_get(init_expected_clock_class
);
917 BT_ASSERT(copy_field_type_func
);
920 BT_LOGW_STR("Invalid parameter: trace is NULL.");
926 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
931 BT_LOGD("Adding stream class to trace: "
932 "trace-addr=%p, trace-name=\"%s\", "
933 "stream-class-addr=%p, stream-class-name=\"%s\", "
934 "stream-class-id=%" PRId64
,
935 trace
, bt_ctf_trace_common_get_name(trace
),
936 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
937 bt_ctf_stream_class_common_get_id(stream_class
));
939 current_parent_trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
940 if (current_parent_trace
) {
941 /* Stream class is already associated to a trace, abort. */
942 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
943 "stream-class-trace-addr=%p, "
944 "stream-class-trace-name=\"%s\"",
945 current_parent_trace
,
946 bt_ctf_trace_common_get_name(current_parent_trace
));
952 bt_ctf_stream_class_common_get_event_class_count(stream_class
);
953 BT_ASSERT(event_class_count
>= 0);
955 if (!stream_class
->frozen
) {
957 * Stream class is not frozen yet. Validate that the
958 * stream class contains at most a single clock class
959 * because the previous
960 * bt_ctf_stream_class_common_add_event_class() calls did
961 * not make this validation since the stream class's
962 * direct field types (packet context, event header,
963 * event context) could change afterwards. This stream
964 * class is about to be frozen and those field types
965 * won't be changed if this function succeeds.
967 * At this point we're also sure that the stream class's
968 * clock, if any, has the same class as the stream
969 * class's expected clock class, if any. This is why, if
970 * bt_ctf_stream_class_common_validate_single_clock_class()
971 * succeeds below, the call to
972 * bt_ctf_stream_class_map_clock_class() at the end of this
973 * function is safe because it maps to the same, single
976 ret
= bt_ctf_stream_class_common_validate_single_clock_class(
977 stream_class
, &expected_clock_class
);
979 BT_LOGW("Invalid parameter: stream class or one of its "
980 "event classes contains a field type which is "
981 "not recursively mapped to the expected "
983 "stream-class-addr=%p, "
984 "stream-class-id=%" PRId64
", "
985 "stream-class-name=\"%s\", "
986 "expected-clock-class-addr=%p, "
987 "expected-clock-class-name=\"%s\"",
988 stream_class
, bt_ctf_stream_class_common_get_id(stream_class
),
989 bt_ctf_stream_class_common_get_name(stream_class
),
990 expected_clock_class
,
991 expected_clock_class
?
992 bt_ctf_clock_class_get_name(expected_clock_class
) :
998 ret
= check_packet_header_type_has_no_clock_class(trace
);
1000 /* check_packet_header_type_has_no_clock_class() logs errors */
1005 * We're about to freeze both the trace and the stream class.
1006 * Also, each event class contained in this stream class are
1009 * This trace, this stream class, and all its event classes
1010 * should be valid at this point.
1012 * Validate trace and stream class first, then each event
1013 * class of this stream class can be validated individually.
1015 packet_header_type
=
1016 bt_ctf_trace_common_borrow_packet_header_field_type(trace
);
1017 packet_context_type
=
1018 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class
);
1020 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class
);
1021 stream_event_ctx_type
=
1022 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class
);
1024 BT_LOGD("Validating trace and stream class field types.");
1025 ret
= bt_ctf_validate_class_types(trace
->environment
,
1026 packet_header_type
, packet_context_type
, event_header_type
,
1027 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1028 stream_class
->valid
, 1, &trace_sc_validation_output
,
1029 trace_sc_validation_flags
, copy_field_type_func
);
1033 * This means something went wrong during the validation
1034 * process, not that the objects are invalid.
1036 BT_LOGE("Failed to validate trace and stream class field types: "
1041 if ((trace_sc_validation_output
.valid_flags
&
1042 trace_sc_validation_flags
) !=
1043 trace_sc_validation_flags
) {
1044 /* Invalid trace/stream class */
1045 BT_LOGW("Invalid trace or stream class field types: "
1047 trace_sc_validation_output
.valid_flags
);
1052 if (event_class_count
> 0) {
1053 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1055 if (!ec_validation_outputs
) {
1056 BT_LOGE_STR("Failed to allocate one validation output structure.");
1062 /* Validate each event class individually */
1063 for (i
= 0; i
< event_class_count
; i
++) {
1064 struct bt_ctf_event_class_common
*event_class
=
1065 bt_ctf_stream_class_common_borrow_event_class_by_index(
1067 struct bt_ctf_field_type_common
*event_context_type
= NULL
;
1068 struct bt_ctf_field_type_common
*event_payload_type
= NULL
;
1070 event_context_type
=
1071 bt_ctf_event_class_common_borrow_context_field_type(
1073 event_payload_type
=
1074 bt_ctf_event_class_common_borrow_payload_field_type(
1078 * It is important to use the field types returned by
1079 * the previous trace and stream class validation here
1080 * because copies could have been made.
1082 BT_LOGD("Validating event class's field types: "
1083 "addr=%p, name=\"%s\", id=%" PRId64
,
1084 event_class
, bt_ctf_event_class_common_get_name(event_class
),
1085 bt_ctf_event_class_common_get_id(event_class
));
1086 ret
= bt_ctf_validate_class_types(trace
->environment
,
1087 trace_sc_validation_output
.packet_header_type
,
1088 trace_sc_validation_output
.packet_context_type
,
1089 trace_sc_validation_output
.event_header_type
,
1090 trace_sc_validation_output
.stream_event_ctx_type
,
1091 event_context_type
, event_payload_type
,
1092 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1093 ec_validation_flags
, copy_field_type_func
);
1096 BT_LOGE("Failed to validate event class field types: "
1101 if ((ec_validation_outputs
[i
].valid_flags
&
1102 ec_validation_flags
) != ec_validation_flags
) {
1103 /* Invalid event class */
1104 BT_LOGW("Invalid event class field types: "
1106 ec_validation_outputs
[i
].valid_flags
);
1112 stream_id
= bt_ctf_stream_class_common_get_id(stream_class
);
1113 if (stream_id
< 0) {
1114 stream_id
= trace
->next_stream_id
++;
1115 if (stream_id
< 0) {
1116 BT_LOGE_STR("No more stream class IDs available.");
1121 /* Try to assign a new stream id */
1122 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1123 if (stream_id
== bt_ctf_stream_class_common_get_id(
1124 trace
->stream_classes
->pdata
[i
])) {
1125 /* Duplicate stream id found */
1126 BT_LOGW("Duplicate stream class ID: "
1127 "id=%" PRId64
, (int64_t) stream_id
);
1133 if (bt_ctf_stream_class_common_set_id_no_check(stream_class
,
1135 /* TODO Should retry with a different stream id */
1136 BT_LOGE("Cannot set stream class's ID: "
1137 "id=%" PRId64
, (int64_t) stream_id
);
1144 * At this point all the field types in the validation output
1145 * are valid. Validate the semantics of some scopes according to
1146 * the CTF specification.
1148 if (!packet_header_field_type_is_valid(trace
,
1149 trace_sc_validation_output
.packet_header_type
)) {
1150 BT_LOGW_STR("Invalid trace's packet header field type.");
1155 if (!packet_context_field_type_is_valid(trace
,
1157 trace_sc_validation_output
.packet_context_type
,
1158 check_ts_begin_end_mapped
)) {
1159 BT_LOGW_STR("Invalid stream class's packet context field type.");
1164 if (!event_header_field_type_is_valid(trace
,
1166 trace_sc_validation_output
.event_header_type
)) {
1167 BT_LOGW_STR("Invalid steam class's event header field type.");
1173 * Now is the time to automatically map specific field types of
1174 * the stream class's packet context and event header field
1175 * types to the stream class's clock's class if they are not
1176 * mapped to a clock class yet. We do it here because we know
1177 * that after this point, everything is frozen so it won't be
1178 * possible for the user to modify the stream class's clock, or
1179 * to map those field types to other clock classes.
1181 if (map_clock_classes_func
) {
1182 if (map_clock_classes_func(stream_class
,
1183 trace_sc_validation_output
.packet_context_type
,
1184 trace_sc_validation_output
.event_header_type
)) {
1185 /* map_clock_classes_func() logs errors */
1191 bt_object_set_parent(&stream_class
->base
, &trace
->base
);
1192 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1195 * At this point we know that the function will be successful.
1196 * Therefore we can replace the trace and stream class field
1197 * types with what's in their validation output structure and
1198 * mark them as valid. We can also replace the field types of
1199 * all the event classes of the stream class and mark them as
1202 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1203 &trace_sc_validation_output
, trace_sc_validation_flags
);
1205 stream_class
->valid
= 1;
1208 * Put what was not moved in bt_ctf_validation_replace_types().
1210 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1212 for (i
= 0; i
< event_class_count
; i
++) {
1213 struct bt_ctf_event_class_common
*event_class
=
1214 bt_ctf_stream_class_common_borrow_event_class_by_index(
1217 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1218 &ec_validation_outputs
[i
], ec_validation_flags
);
1219 event_class
->valid
= 1;
1222 * Put what was not moved in
1223 * bt_ctf_validation_replace_types().
1225 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1229 * Freeze the trace and the stream class.
1231 bt_ctf_stream_class_common_freeze(stream_class
);
1232 bt_ctf_trace_common_freeze(trace
);
1235 * It is safe to set the stream class's unique clock class
1236 * now because the stream class is frozen.
1238 if (expected_clock_class
) {
1239 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
1242 BT_LOGD("Added stream class to trace: "
1243 "trace-addr=%p, trace-name=\"%s\", "
1244 "stream-class-addr=%p, stream-class-name=\"%s\", "
1245 "stream-class-id=%" PRId64
,
1246 trace
, bt_ctf_trace_common_get_name(trace
),
1247 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
1248 bt_ctf_stream_class_common_get_id(stream_class
));
1252 bt_object_set_parent(&stream_class
->base
, NULL
);
1254 if (ec_validation_outputs
) {
1255 for (i
= 0; i
< event_class_count
; i
++) {
1256 bt_ctf_validation_output_put_types(
1257 &ec_validation_outputs
[i
]);
1262 g_free(ec_validation_outputs
);
1263 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1264 bt_put(expected_clock_class
);
1269 bt_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
1270 struct bt_ctf_clock_class
*clock_class
)
1272 struct bt_ctf_search_query query
= { .value
= clock_class
, .found
= 0 };
1275 BT_ASSERT(clock_class
);
1277 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1282 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
1283 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
)
1288 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1293 if (trace
->frozen
) {
1294 BT_LOGW("Invalid parameter: trace is frozen: "
1295 "addr=%p, name=\"%s\"",
1296 trace
, bt_ctf_trace_common_get_name(trace
));
1301 if (byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1302 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1303 "addr=%p, name=\"%s\"",
1304 trace
, bt_ctf_trace_common_get_name(trace
));
1309 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1310 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1311 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1312 BT_LOGW("Invalid parameter: invalid 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
));
1320 trace
->native_byte_order
= byte_order
;
1321 BT_LOGV("Set trace's native byte order: "
1322 "addr=%p, name=\"%s\", bo=%s",
1323 trace
, bt_ctf_trace_common_get_name(trace
),
1324 bt_ctf_byte_order_string(byte_order
));
1331 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
1332 struct bt_ctf_field_type_common
*packet_header_type
)
1337 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1342 if (trace
->frozen
) {
1343 BT_LOGW("Invalid parameter: trace is frozen: "
1344 "addr=%p, name=\"%s\"",
1345 trace
, bt_ctf_trace_common_get_name(trace
));
1350 /* packet_header_type must be a structure. */
1351 if (packet_header_type
&&
1352 packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1353 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1354 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1355 trace
, bt_ctf_trace_common_get_name(trace
),
1357 bt_ctf_field_type_id_string(packet_header_type
->id
));
1362 bt_put(trace
->packet_header_field_type
);
1363 trace
->packet_header_field_type
= bt_get(packet_header_type
);
1364 BT_LOGV("Set trace's packet header field type: "
1365 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1366 trace
, bt_ctf_trace_common_get_name(trace
), packet_header_type
);
1372 int64_t get_stream_class_count(void *element
)
1374 return bt_ctf_trace_get_stream_class_count(
1375 (struct bt_ctf_trace
*) element
);
1379 void *get_stream_class(void *element
, int i
)
1381 return bt_ctf_trace_get_stream_class_by_index(
1382 (struct bt_ctf_trace
*) element
, i
);
1386 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1388 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1391 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1392 bt_ctf_visitor visitor
, void *data
)
1395 struct bt_ctf_visitor_object obj
= {
1397 .type
= BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1401 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1407 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1412 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1413 trace
, bt_ctf_trace_get_name(trace
));
1414 ret
= visitor_helper(&obj
, get_stream_class_count
,
1415 get_stream_class
, visit_stream_class
, visitor
, data
);
1421 void bt_ctf_trace_destroy(struct bt_object
*obj
)
1423 struct bt_ctf_trace
*trace
= (void *) obj
;
1425 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1426 trace
, bt_ctf_trace_get_name(trace
));
1427 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace
));
1432 struct bt_ctf_trace
*bt_ctf_trace_create(void)
1434 struct bt_ctf_trace
*trace
= NULL
;
1437 BT_LOGD_STR("Creating CTF writer trace object.");
1438 trace
= g_new0(struct bt_ctf_trace
, 1);
1440 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1444 ret
= bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace
),
1445 bt_ctf_trace_destroy
);
1447 /* bt_ctf_trace_common_initialize() logs errors */
1451 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
1459 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
1461 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace
));
1464 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
1465 const unsigned char *uuid
)
1467 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace
), uuid
);
1470 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
1471 const char *name
, struct bt_value
*value
)
1473 return bt_ctf_trace_common_set_environment_field(BT_CTF_TO_COMMON(trace
),
1477 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
1478 const char *name
, const char *value
)
1480 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace
),
1484 int bt_ctf_trace_set_environment_field_integer(
1485 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
1487 return bt_ctf_trace_common_set_environment_field_integer(
1488 BT_CTF_TO_COMMON(trace
), name
, value
);
1491 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
1493 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace
));
1497 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
1500 return bt_ctf_trace_common_get_environment_field_name_by_index(
1501 BT_CTF_TO_COMMON(trace
), index
);
1504 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
1505 struct bt_ctf_trace
*trace
, uint64_t index
)
1507 return bt_get(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1508 BT_CTF_TO_COMMON(trace
), index
));
1511 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
1512 struct bt_ctf_trace
*trace
, const char *name
)
1514 return bt_get(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1515 BT_CTF_TO_COMMON(trace
), name
));
1519 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
1520 struct bt_ctf_clock_class
*clock_class
)
1522 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace
),
1523 (void *) clock_class
);
1527 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
1529 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace
));
1533 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
1534 struct bt_ctf_trace
*trace
, uint64_t index
)
1536 return bt_get(bt_ctf_trace_common_borrow_clock_class_by_index(
1537 BT_CTF_TO_COMMON(trace
), index
));
1541 int map_clock_classes_func(struct bt_ctf_stream_class_common
*stream_class
,
1542 struct bt_ctf_field_type_common
*packet_context_type
,
1543 struct bt_ctf_field_type_common
*event_header_type
)
1545 int ret
= bt_ctf_stream_class_map_clock_class(
1546 BT_CTF_FROM_COMMON(stream_class
),
1547 BT_CTF_FROM_COMMON(packet_context_type
),
1548 BT_CTF_FROM_COMMON(event_header_type
));
1551 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1557 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1558 struct bt_ctf_stream_class
*stream_class
)
1561 struct bt_ctf_clock_class
*expected_clock_class
= NULL
;
1564 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1569 if (!stream_class
) {
1570 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1575 if (stream_class
->clock
) {
1576 struct bt_ctf_clock_class
*stream_clock_class
=
1577 stream_class
->clock
->clock_class
;
1580 * Make sure this clock was also added to the
1581 * trace (potentially through its CTF writer
1586 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
1587 if (trace
->common
.clock_classes
->pdata
[i
] ==
1588 stream_clock_class
) {
1594 if (i
== trace
->common
.clock_classes
->len
) {
1596 BT_LOGW("Stream class's clock's class is not part of the trace: "
1597 "clock-class-addr=%p, clock-class-name=\"%s\"",
1599 bt_ctf_clock_class_get_name(stream_clock_class
));
1604 if (stream_class
->common
.clock_class
&&
1605 stream_class
->common
.clock_class
!=
1606 stream_class
->clock
->clock_class
) {
1608 * Stream class already has an expected clock
1609 * class, but it does not match its clock's
1612 BT_LOGW("Invalid parameter: stream class's clock's "
1613 "class does not match stream class's "
1614 "expected clock class: "
1615 "stream-class-addr=%p, "
1616 "stream-class-id=%" PRId64
", "
1617 "stream-class-name=\"%s\", "
1618 "expected-clock-class-addr=%p, "
1619 "expected-clock-class-name=\"%s\"",
1621 bt_ctf_stream_class_get_id(stream_class
),
1622 bt_ctf_stream_class_get_name(stream_class
),
1623 stream_class
->common
.clock_class
,
1624 bt_ctf_clock_class_get_name(stream_class
->common
.clock_class
));
1625 } else if (!stream_class
->common
.clock_class
) {
1627 * Set expected clock class to stream class's
1630 expected_clock_class
= stream_class
->clock
->clock_class
;
1635 ret
= bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace
),
1636 BT_CTF_TO_COMMON(stream_class
),
1637 (bt_ctf_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
1638 expected_clock_class
, map_clock_classes_func
,
1645 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1647 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace
));
1650 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1651 struct bt_ctf_trace
*trace
, uint64_t index
)
1653 return bt_get(bt_ctf_trace_common_borrow_stream_by_index(
1654 BT_CTF_TO_COMMON(trace
), index
));
1657 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1659 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace
));
1662 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1663 struct bt_ctf_trace
*trace
, uint64_t index
)
1665 return bt_get(bt_ctf_trace_common_borrow_stream_class_by_index(
1666 BT_CTF_TO_COMMON(trace
), index
));
1669 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1670 struct bt_ctf_trace
*trace
, uint64_t id
)
1672 return bt_get(bt_ctf_trace_common_borrow_stream_class_by_id(
1673 BT_CTF_TO_COMMON(trace
), id
));
1677 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1678 struct bt_ctf_trace
*trace
, const char *name
)
1681 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace
),
1686 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1687 struct metadata_context
*context
)
1689 unsigned char *uuid
= trace
->common
.uuid
;
1692 if (trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1693 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1694 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1695 "set it with bt_ctf_trace_set_native_byte_order(): "
1696 "addr=%p, name=\"%s\"",
1697 trace
, bt_ctf_trace_get_name(trace
));
1702 g_string_append(context
->string
, "trace {\n");
1703 g_string_append(context
->string
, "\tmajor = 1;\n");
1704 g_string_append(context
->string
, "\tminor = 8;\n");
1705 BT_ASSERT(trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1706 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1707 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1709 if (trace
->common
.uuid_set
) {
1710 g_string_append_printf(context
->string
,
1711 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1712 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1713 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1714 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1715 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1718 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1719 bt_ctf_get_byte_order_string(trace
->common
.native_byte_order
));
1721 if (trace
->common
.packet_header_field_type
) {
1722 g_string_append(context
->string
, "\tpacket.header := ");
1723 context
->current_indentation_level
++;
1724 g_string_assign(context
->field_name
, "");
1725 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1726 ret
= bt_ctf_field_type_serialize_recursive(
1727 (void *) trace
->common
.packet_header_field_type
,
1732 context
->current_indentation_level
--;
1735 g_string_append(context
->string
, ";\n};\n\n");
1741 void append_env_metadata(struct bt_ctf_trace
*trace
,
1742 struct metadata_context
*context
)
1747 env_size
= bt_ctf_attributes_get_count(trace
->common
.environment
);
1748 if (env_size
<= 0) {
1752 g_string_append(context
->string
, "env {\n");
1754 for (i
= 0; i
< env_size
; i
++) {
1755 struct bt_value
*env_field_value_obj
= NULL
;
1756 const char *entry_name
;
1758 entry_name
= bt_ctf_attributes_get_field_name(
1759 trace
->common
.environment
, i
);
1760 env_field_value_obj
= bt_ctf_attributes_borrow_field_value(
1761 trace
->common
.environment
, i
);
1763 BT_ASSERT(entry_name
);
1764 BT_ASSERT(env_field_value_obj
);
1766 switch (bt_value_get_type(env_field_value_obj
)) {
1767 case BT_VALUE_TYPE_INTEGER
:
1772 ret
= bt_value_integer_get(env_field_value_obj
,
1774 BT_ASSERT(ret
== 0);
1775 g_string_append_printf(context
->string
,
1776 "\t%s = %" PRId64
";\n", entry_name
,
1780 case BT_VALUE_TYPE_STRING
:
1783 const char *str_value
;
1784 char *escaped_str
= NULL
;
1786 ret
= bt_value_string_get(env_field_value_obj
,
1788 BT_ASSERT(ret
== 0);
1789 escaped_str
= g_strescape(str_value
, NULL
);
1791 BT_LOGE("Cannot escape string: string=\"%s\"",
1796 g_string_append_printf(context
->string
,
1797 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1806 g_string_append(context
->string
, "};\n\n");
1809 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1811 char *metadata
= NULL
;
1812 struct metadata_context
*context
= NULL
;
1817 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1821 context
= g_new0(struct metadata_context
, 1);
1823 BT_LOGE_STR("Failed to allocate one metadata context.");
1827 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1828 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1829 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1830 if (append_trace_metadata(trace
, context
)) {
1831 /* append_trace_metadata() logs errors */
1834 append_env_metadata(trace
, context
);
1835 g_ptr_array_foreach(trace
->common
.clock_classes
,
1836 (GFunc
) bt_ctf_clock_class_serialize
, context
);
1838 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
1839 /* bt_ctf_stream_class_serialize() logs details */
1840 err
= bt_ctf_stream_class_serialize(
1841 trace
->common
.stream_classes
->pdata
[i
], context
);
1843 /* bt_ctf_stream_class_serialize() logs errors */
1848 metadata
= context
->string
->str
;
1851 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1852 g_string_free(context
->field_name
, TRUE
);
1859 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1860 struct bt_ctf_trace
*trace
)
1862 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace
));
1865 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1866 enum bt_ctf_byte_order byte_order
)
1868 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace
),
1869 (int) byte_order
, false);
1872 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
1873 struct bt_ctf_trace
*trace
)
1875 return bt_get(bt_ctf_trace_common_borrow_packet_header_field_type(
1876 BT_CTF_TO_COMMON(trace
)));
1879 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
1880 struct bt_ctf_field_type
*packet_header_type
)
1882 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace
),
1883 (void *) packet_header_type
);
1886 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
1888 return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace
));