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
,
183 const unsigned char *uuid
)
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 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
208 trace
->uuid_set
= BT_TRUE
;
209 BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", "
210 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
211 trace
, bt_ctf_trace_common_get_name(trace
),
212 (unsigned int) uuid
[0],
213 (unsigned int) uuid
[1],
214 (unsigned int) uuid
[2],
215 (unsigned int) uuid
[3],
216 (unsigned int) uuid
[4],
217 (unsigned int) uuid
[5],
218 (unsigned int) uuid
[6],
219 (unsigned int) uuid
[7],
220 (unsigned int) uuid
[8],
221 (unsigned int) uuid
[9],
222 (unsigned int) uuid
[10],
223 (unsigned int) uuid
[11],
224 (unsigned int) uuid
[12],
225 (unsigned int) uuid
[13],
226 (unsigned int) uuid
[14],
227 (unsigned int) uuid
[15]);
234 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
235 const char *name
, struct bt_ctf_private_value
*value
)
240 BT_LOGW_STR("Invalid parameter: trace is NULL.");
246 BT_LOGW_STR("Invalid parameter: name is NULL.");
252 BT_LOGW_STR("Invalid parameter: value is NULL.");
257 if (!bt_ctf_identifier_is_valid(name
)) {
258 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
259 "trace-addr=%p, trace-name=\"%s\", "
261 trace
, bt_ctf_trace_common_get_name(trace
), name
);
266 if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value
)) &&
267 !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value
))) {
268 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
269 "trace-addr=%p, trace-name=\"%s\", "
270 "env-name=\"%s\", env-value-type=%s",
271 trace
, bt_ctf_trace_common_get_name(trace
), name
,
272 bt_ctf_value_type_string(
273 bt_ctf_value_get_type(
274 bt_ctf_private_value_as_value(value
))));
281 * New environment fields may be added to a frozen trace,
282 * but existing fields may not be changed.
284 * The object passed is frozen like all other attributes.
286 struct bt_ctf_private_value
*attribute
=
287 bt_ctf_attributes_borrow_field_value_by_name(
288 trace
->environment
, name
);
291 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
292 "trace-addr=%p, trace-name=\"%s\", "
294 trace
, bt_ctf_trace_common_get_name(trace
), name
);
299 bt_ctf_value_freeze(bt_ctf_private_value_as_value(value
));
302 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
305 BT_LOGE("Cannot set environment field's value: "
306 "trace-addr=%p, trace-name=\"%s\", "
308 trace
, bt_ctf_trace_common_get_name(trace
), name
);
310 BT_LOGT("Set environment field's value: "
311 "trace-addr=%p, trace-name=\"%s\", "
312 "env-name=\"%s\", value-addr=%p",
313 trace
, bt_ctf_trace_common_get_name(trace
), name
, value
);
321 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
322 const char *name
, const char *value
)
325 struct bt_ctf_private_value
*env_value_string_obj
= NULL
;
328 BT_LOGW_STR("Invalid parameter: value is NULL.");
333 env_value_string_obj
= bt_ctf_private_value_string_create_init(value
);
334 if (!env_value_string_obj
) {
335 BT_LOGE_STR("Cannot create string value object.");
340 /* bt_ctf_trace_common_set_environment_field() logs errors */
341 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
342 env_value_string_obj
);
345 bt_ctf_object_put_ref(env_value_string_obj
);
350 int bt_ctf_trace_common_set_environment_field_integer(
351 struct bt_ctf_trace_common
*trace
, const char *name
, int64_t value
)
354 struct bt_ctf_private_value
*env_value_integer_obj
= NULL
;
356 env_value_integer_obj
= bt_ctf_private_value_integer_create_init(value
);
357 if (!env_value_integer_obj
) {
358 BT_LOGE_STR("Cannot create integer value object.");
363 /* bt_ctf_trace_common_set_environment_field() logs errors */
364 ret
= bt_ctf_trace_common_set_environment_field(trace
, name
,
365 env_value_integer_obj
);
368 bt_ctf_object_put_ref(env_value_integer_obj
);
373 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
374 struct bt_ctf_clock_class
*clock_class
)
379 BT_LOGW_STR("Invalid parameter: trace is NULL.");
384 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
385 BT_LOGW("Invalid parameter: clock class is invalid: "
386 "trace-addr=%p, trace-name=\"%s\", "
387 "clock-class-addr=%p, clock-class-name=\"%s\"",
388 trace
, bt_ctf_trace_common_get_name(trace
),
389 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
394 /* Check for duplicate clock classes */
395 if (bt_ctf_trace_common_has_clock_class(trace
, clock_class
)) {
396 BT_LOGW("Invalid parameter: clock class already exists in trace: "
397 "trace-addr=%p, trace-name=\"%s\", "
398 "clock-class-addr=%p, clock-class-name=\"%s\"",
399 trace
, bt_ctf_trace_common_get_name(trace
),
400 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
405 bt_ctf_object_get_ref(clock_class
);
406 g_ptr_array_add(trace
->clock_classes
, clock_class
);
409 BT_LOGT_STR("Freezing added clock class because trace is frozen.");
410 bt_ctf_clock_class_freeze(clock_class
);
413 BT_LOGT("Added clock class to trace: "
414 "trace-addr=%p, trace-name=\"%s\", "
415 "clock-class-addr=%p, clock-class-name=\"%s\"",
416 trace
, bt_ctf_trace_common_get_name(trace
),
417 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
424 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
425 struct bt_ctf_field_type_common
*packet_header_type
)
428 bool is_valid
= true;
429 struct bt_ctf_field_type_common
*field_type
= NULL
;
431 if (!packet_header_type
) {
433 * No packet header field type: trace must have only
434 * one stream. At this point the stream class being
435 * added is not part of the trace yet, so we validate
436 * that the trace contains no stream classes yet.
438 if (trace
->stream_classes
->len
>= 1) {
439 BT_LOGW_STR("Invalid packet header field type: "
440 "packet header field type does not exist but there's more than one stream class in the trace.");
444 /* No packet header field type: valid at this point */
448 /* Packet header field type, if it exists, must be a structure */
449 if (packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
450 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
451 "ft-addr=%p, ft-id=%s",
453 bt_ctf_field_type_id_string(packet_header_type
->id
));
458 * If there's a `magic` field, it must be a 32-bit unsigned
459 * integer field type. Also it must be the first field of the
460 * packet header field type.
462 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
463 packet_header_type
, "magic");
465 const char *field_name
;
467 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
468 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
469 "magic-ft-addr=%p, magic-ft-id=%s",
471 bt_ctf_field_type_id_string(field_type
->id
));
475 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
476 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
477 "magic-ft-addr=%p", field_type
);
481 if (bt_ctf_field_type_common_integer_get_size(field_type
) != 32) {
482 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
483 "magic-ft-addr=%p, magic-ft-size=%u",
485 bt_ctf_field_type_common_integer_get_size(field_type
));
489 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
490 packet_header_type
, &field_name
, NULL
, 0);
493 if (strcmp(field_name
, "magic") != 0) {
494 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
495 "magic-ft-addr=%p, first-field-name=\"%s\"",
496 field_type
, field_name
);
502 * If there's a `uuid` field, it must be an array field type of
503 * length 16 with an 8-bit unsigned integer element field type.
505 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
506 packet_header_type
, "uuid");
508 struct bt_ctf_field_type_common
*elem_ft
;
510 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
511 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
512 "uuid-ft-addr=%p, uuid-ft-id=%s",
514 bt_ctf_field_type_id_string(field_type
->id
));
518 if (bt_ctf_field_type_common_array_get_length(field_type
) != 16) {
519 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
520 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
522 bt_ctf_field_type_common_array_get_length(field_type
));
526 elem_ft
= bt_ctf_field_type_common_array_borrow_element_field_type(field_type
);
529 if (elem_ft
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
530 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
531 "elem-ft-addr=%p, elem-ft-id=%s",
533 bt_ctf_field_type_id_string(elem_ft
->id
));
537 if (bt_ctf_field_type_common_integer_is_signed(elem_ft
)) {
538 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
539 "elem-ft-addr=%p", elem_ft
);
543 if (bt_ctf_field_type_common_integer_get_size(elem_ft
) != 8) {
544 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
545 "elem-ft-addr=%p, elem-ft-size=%u",
547 bt_ctf_field_type_common_integer_get_size(elem_ft
));
553 * The `stream_id` field must exist if there's more than one
554 * stream classes in the trace.
556 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
557 packet_header_type
, "stream_id");
559 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
560 BT_LOGW_STR("Invalid packet header field type: "
561 "`stream_id` field does not exist but there's more than one stream class in the trace.");
566 * If there's a `stream_id` field, it must be an unsigned
567 * integer field type.
570 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
571 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
572 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
574 bt_ctf_field_type_id_string(field_type
->id
));
578 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
579 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
580 "stream-id-ft-addr=%p", field_type
);
586 * If there's a `packet_seq_num` field, it must be an unsigned
587 * integer field type.
589 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
590 packet_header_type
, "packet_seq_num");
592 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
593 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
594 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
596 bt_ctf_field_type_id_string(field_type
->id
));
600 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
601 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
602 "packet-seq-num-ft-addr=%p", field_type
);
617 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
618 struct bt_ctf_stream_class_common
*stream_class
,
619 struct bt_ctf_field_type_common
*packet_context_type
,
620 bool check_ts_begin_end_mapped
)
622 bool is_valid
= true;
623 struct bt_ctf_field_type_common
*field_type
= NULL
;
625 if (!packet_context_type
) {
626 /* No packet context field type: valid at this point */
630 /* Packet context field type, if it exists, must be a structure */
631 if (packet_context_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
632 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
633 "ft-addr=%p, ft-id=%s",
635 bt_ctf_field_type_id_string(packet_context_type
->id
));
640 * If there's a `packet_size` field, it must be an unsigned
641 * integer field type.
643 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
644 packet_context_type
, "packet_size");
646 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
647 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
648 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
650 bt_ctf_field_type_id_string(field_type
->id
));
654 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
655 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
656 "packet-size-ft-addr=%p", field_type
);
662 * If there's a `content_size` field, it must be an unsigned
663 * integer field type.
665 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
666 packet_context_type
, "content_size");
668 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
669 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
670 "content-size-ft-addr=%p, content-size-ft-id=%s",
672 bt_ctf_field_type_id_string(field_type
->id
));
676 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
677 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
678 "content-size-ft-addr=%p", field_type
);
684 * If there's a `events_discarded` field, it must be an unsigned
685 * integer field type.
687 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
688 packet_context_type
, "events_discarded");
690 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
691 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
692 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
694 bt_ctf_field_type_id_string(field_type
->id
));
698 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
699 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
700 "events-discarded-ft-addr=%p", field_type
);
706 * If there's a `timestamp_begin` field, it must be an unsigned
707 * integer field type. Also, if the trace is not a CTF writer's
708 * trace, then we cannot automatically set the mapped clock
709 * class of this field, so it must have a mapped clock class.
711 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
712 packet_context_type
, "timestamp_begin");
714 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
715 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
716 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
718 bt_ctf_field_type_id_string(field_type
->id
));
722 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
723 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
724 "timestamp-begin-ft-addr=%p", field_type
);
728 if (check_ts_begin_end_mapped
) {
729 struct bt_ctf_clock_class
*clock_class
=
730 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
734 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
735 "timestamp-begin-ft-addr=%p", field_type
);
742 * If there's a `timestamp_end` field, it must be an unsigned
743 * integer field type. Also, if the trace is not a CTF writer's
744 * trace, then we cannot automatically set the mapped clock
745 * class of this field, so it must have a mapped clock class.
747 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
748 packet_context_type
, "timestamp_end");
750 if (field_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
751 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
752 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
754 bt_ctf_field_type_id_string(field_type
->id
));
758 if (bt_ctf_field_type_common_integer_is_signed(field_type
)) {
759 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
760 "timestamp-end-ft-addr=%p", field_type
);
764 if (check_ts_begin_end_mapped
) {
765 struct bt_ctf_clock_class
*clock_class
=
766 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
770 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
771 "timestamp-end-ft-addr=%p", field_type
);
787 bool event_header_field_type_is_valid(struct bt_ctf_trace_common
*trace
,
788 struct bt_ctf_stream_class_common
*stream_class
,
789 struct bt_ctf_field_type_common
*event_header_type
)
791 bool is_valid
= true;
792 struct bt_ctf_field_type_common
*field_type
= NULL
;
795 * We do not validate that the `timestamp` field exists here
796 * because CTF does not require this exact name to be mapped to
800 if (!event_header_type
) {
802 * No event header field type: stream class must have
803 * only one event class.
805 if (bt_ctf_stream_class_common_get_event_class_count(stream_class
) > 1) {
806 BT_LOGW_STR("Invalid event header field type: "
807 "event header field type does not exist but there's more than one event class in the stream class.");
811 /* No event header field type: valid at this point */
815 /* Event header field type, if it exists, must be a structure */
816 if (event_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
817 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
818 "ft-addr=%p, ft-id=%s",
820 bt_ctf_field_type_id_string(event_header_type
->id
));
825 * If there's an `id` field, it must be an unsigned integer
826 * field type or an enumeration field type with an unsigned
827 * integer container field type.
829 field_type
= bt_ctf_field_type_common_structure_borrow_field_type_by_name(
830 event_header_type
, "id");
832 struct bt_ctf_field_type_common
*int_ft
;
834 if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
836 } else if (field_type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
837 int_ft
= bt_ctf_field_type_common_enumeration_borrow_container_field_type(
840 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
841 "id-ft-addr=%p, id-ft-id=%s",
843 bt_ctf_field_type_id_string(field_type
->id
));
848 if (bt_ctf_field_type_common_integer_is_signed(int_ft
)) {
849 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
850 "id-ft-addr=%p", int_ft
);
865 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common
*trace
)
869 if (trace
->packet_header_field_type
) {
870 struct bt_ctf_clock_class
*clock_class
= NULL
;
872 ret
= bt_ctf_field_type_common_validate_single_clock_class(
873 trace
->packet_header_field_type
,
875 bt_ctf_object_put_ref(clock_class
);
876 if (ret
|| clock_class
) {
877 BT_LOGW("Trace's packet header field type cannot "
878 "contain a field type which is mapped to "
880 "trace-addr=%p, trace-name=\"%s\", "
881 "clock-class-name=\"%s\"",
882 trace
, bt_ctf_trace_common_get_name(trace
),
884 bt_ctf_clock_class_get_name(clock_class
) :
894 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
895 struct bt_ctf_stream_class_common
*stream_class
,
896 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
897 struct bt_ctf_clock_class
*init_expected_clock_class
,
898 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
899 struct bt_ctf_field_type_common
*packet_context_field_type
,
900 struct bt_ctf_field_type_common
*event_header_field_type
),
901 bool check_ts_begin_end_mapped
)
906 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
907 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
908 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
909 BT_CTF_VALIDATION_FLAG_TRACE
|
910 BT_CTF_VALIDATION_FLAG_STREAM
;
911 const enum bt_ctf_validation_flag ec_validation_flags
=
912 BT_CTF_VALIDATION_FLAG_EVENT
;
913 struct bt_ctf_field_type_common
*packet_header_type
= NULL
;
914 struct bt_ctf_field_type_common
*packet_context_type
= NULL
;
915 struct bt_ctf_field_type_common
*event_header_type
= NULL
;
916 struct bt_ctf_field_type_common
*stream_event_ctx_type
= NULL
;
917 int64_t event_class_count
;
918 struct bt_ctf_trace_common
*current_parent_trace
= NULL
;
919 struct bt_ctf_clock_class
*expected_clock_class
=
920 bt_ctf_object_get_ref(init_expected_clock_class
);
922 BT_ASSERT(copy_field_type_func
);
925 BT_LOGW_STR("Invalid parameter: trace is NULL.");
931 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
936 BT_LOGD("Adding stream class to trace: "
937 "trace-addr=%p, trace-name=\"%s\", "
938 "stream-class-addr=%p, stream-class-name=\"%s\", "
939 "stream-class-id=%" PRId64
,
940 trace
, bt_ctf_trace_common_get_name(trace
),
941 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
942 bt_ctf_stream_class_common_get_id(stream_class
));
944 current_parent_trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
945 if (current_parent_trace
) {
946 /* Stream class is already associated to a trace, abort. */
947 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
948 "stream-class-trace-addr=%p, "
949 "stream-class-trace-name=\"%s\"",
950 current_parent_trace
,
951 bt_ctf_trace_common_get_name(current_parent_trace
));
957 bt_ctf_stream_class_common_get_event_class_count(stream_class
);
958 BT_ASSERT(event_class_count
>= 0);
960 if (!stream_class
->frozen
) {
962 * Stream class is not frozen yet. Validate that the
963 * stream class contains at most a single clock class
964 * because the previous
965 * bt_ctf_stream_class_common_add_event_class() calls did
966 * not make this validation since the stream class's
967 * direct field types (packet context, event header,
968 * event context) could change afterwards. This stream
969 * class is about to be frozen and those field types
970 * won't be changed if this function succeeds.
972 * At this point we're also sure that the stream class's
973 * clock, if any, has the same class as the stream
974 * class's expected clock class, if any. This is why, if
975 * bt_ctf_stream_class_common_validate_single_clock_class()
976 * succeeds below, the call to
977 * bt_ctf_stream_class_map_clock_class() at the end of this
978 * function is safe because it maps to the same, single
981 ret
= bt_ctf_stream_class_common_validate_single_clock_class(
982 stream_class
, &expected_clock_class
);
984 BT_LOGW("Invalid parameter: stream class or one of its "
985 "event classes contains a field type which is "
986 "not recursively mapped to the expected "
988 "stream-class-addr=%p, "
989 "stream-class-id=%" PRId64
", "
990 "stream-class-name=\"%s\", "
991 "expected-clock-class-addr=%p, "
992 "expected-clock-class-name=\"%s\"",
993 stream_class
, bt_ctf_stream_class_common_get_id(stream_class
),
994 bt_ctf_stream_class_common_get_name(stream_class
),
995 expected_clock_class
,
996 expected_clock_class
?
997 bt_ctf_clock_class_get_name(expected_clock_class
) :
1003 ret
= check_packet_header_type_has_no_clock_class(trace
);
1005 /* check_packet_header_type_has_no_clock_class() logs errors */
1010 * We're about to freeze both the trace and the stream class.
1011 * Also, each event class contained in this stream class are
1014 * This trace, this stream class, and all its event classes
1015 * should be valid at this point.
1017 * Validate trace and stream class first, then each event
1018 * class of this stream class can be validated individually.
1020 packet_header_type
=
1021 bt_ctf_trace_common_borrow_packet_header_field_type(trace
);
1022 packet_context_type
=
1023 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class
);
1025 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class
);
1026 stream_event_ctx_type
=
1027 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class
);
1029 BT_LOGD("Validating trace and stream class field types.");
1030 ret
= bt_ctf_validate_class_types(trace
->environment
,
1031 packet_header_type
, packet_context_type
, event_header_type
,
1032 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1033 stream_class
->valid
, 1, &trace_sc_validation_output
,
1034 trace_sc_validation_flags
, copy_field_type_func
);
1038 * This means something went wrong during the validation
1039 * process, not that the objects are invalid.
1041 BT_LOGE("Failed to validate trace and stream class field types: "
1046 if ((trace_sc_validation_output
.valid_flags
&
1047 trace_sc_validation_flags
) !=
1048 trace_sc_validation_flags
) {
1049 /* Invalid trace/stream class */
1050 BT_LOGW("Invalid trace or stream class field types: "
1052 trace_sc_validation_output
.valid_flags
);
1057 if (event_class_count
> 0) {
1058 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1060 if (!ec_validation_outputs
) {
1061 BT_LOGE_STR("Failed to allocate one validation output structure.");
1067 /* Validate each event class individually */
1068 for (i
= 0; i
< event_class_count
; i
++) {
1069 struct bt_ctf_event_class_common
*event_class
=
1070 bt_ctf_stream_class_common_borrow_event_class_by_index(
1072 struct bt_ctf_field_type_common
*event_context_type
= NULL
;
1073 struct bt_ctf_field_type_common
*event_payload_type
= NULL
;
1075 event_context_type
=
1076 bt_ctf_event_class_common_borrow_context_field_type(
1078 event_payload_type
=
1079 bt_ctf_event_class_common_borrow_payload_field_type(
1083 * It is important to use the field types returned by
1084 * the previous trace and stream class validation here
1085 * because copies could have been made.
1087 BT_LOGD("Validating event class's field types: "
1088 "addr=%p, name=\"%s\", id=%" PRId64
,
1089 event_class
, bt_ctf_event_class_common_get_name(event_class
),
1090 bt_ctf_event_class_common_get_id(event_class
));
1091 ret
= bt_ctf_validate_class_types(trace
->environment
,
1092 trace_sc_validation_output
.packet_header_type
,
1093 trace_sc_validation_output
.packet_context_type
,
1094 trace_sc_validation_output
.event_header_type
,
1095 trace_sc_validation_output
.stream_event_ctx_type
,
1096 event_context_type
, event_payload_type
,
1097 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1098 ec_validation_flags
, copy_field_type_func
);
1101 BT_LOGE("Failed to validate event class field types: "
1106 if ((ec_validation_outputs
[i
].valid_flags
&
1107 ec_validation_flags
) != ec_validation_flags
) {
1108 /* Invalid event class */
1109 BT_LOGW("Invalid event class field types: "
1111 ec_validation_outputs
[i
].valid_flags
);
1117 stream_id
= bt_ctf_stream_class_common_get_id(stream_class
);
1118 if (stream_id
< 0) {
1119 stream_id
= trace
->next_stream_id
++;
1120 if (stream_id
< 0) {
1121 BT_LOGE_STR("No more stream class IDs available.");
1126 /* Try to assign a new stream id */
1127 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1128 if (stream_id
== bt_ctf_stream_class_common_get_id(
1129 trace
->stream_classes
->pdata
[i
])) {
1130 /* Duplicate stream id found */
1131 BT_LOGW("Duplicate stream class ID: "
1132 "id=%" PRId64
, (int64_t) stream_id
);
1138 if (bt_ctf_stream_class_common_set_id_no_check(stream_class
,
1140 /* TODO Should retry with a different stream id */
1141 BT_LOGE("Cannot set stream class's ID: "
1142 "id=%" PRId64
, (int64_t) stream_id
);
1149 * At this point all the field types in the validation output
1150 * are valid. Validate the semantics of some scopes according to
1151 * the CTF specification.
1153 if (!packet_header_field_type_is_valid(trace
,
1154 trace_sc_validation_output
.packet_header_type
)) {
1155 BT_LOGW_STR("Invalid trace's packet header field type.");
1160 if (!packet_context_field_type_is_valid(trace
,
1162 trace_sc_validation_output
.packet_context_type
,
1163 check_ts_begin_end_mapped
)) {
1164 BT_LOGW_STR("Invalid stream class's packet context field type.");
1169 if (!event_header_field_type_is_valid(trace
,
1171 trace_sc_validation_output
.event_header_type
)) {
1172 BT_LOGW_STR("Invalid steam class's event header field type.");
1178 * Now is the time to automatically map specific field types of
1179 * the stream class's packet context and event header field
1180 * types to the stream class's clock's class if they are not
1181 * mapped to a clock class yet. We do it here because we know
1182 * that after this point, everything is frozen so it won't be
1183 * possible for the user to modify the stream class's clock, or
1184 * to map those field types to other clock classes.
1186 if (map_clock_classes_func
) {
1187 if (map_clock_classes_func(stream_class
,
1188 trace_sc_validation_output
.packet_context_type
,
1189 trace_sc_validation_output
.event_header_type
)) {
1190 /* map_clock_classes_func() logs errors */
1196 bt_ctf_object_set_parent(&stream_class
->base
, &trace
->base
);
1197 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1200 * At this point we know that the function will be successful.
1201 * Therefore we can replace the trace and stream class field
1202 * types with what's in their validation output structure and
1203 * mark them as valid. We can also replace the field types of
1204 * all the event classes of the stream class and mark them as
1207 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1208 &trace_sc_validation_output
, trace_sc_validation_flags
);
1210 stream_class
->valid
= 1;
1213 * Put what was not moved in bt_ctf_validation_replace_types().
1215 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1217 for (i
= 0; i
< event_class_count
; i
++) {
1218 struct bt_ctf_event_class_common
*event_class
=
1219 bt_ctf_stream_class_common_borrow_event_class_by_index(
1222 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1223 &ec_validation_outputs
[i
], ec_validation_flags
);
1224 event_class
->valid
= 1;
1227 * Put what was not moved in
1228 * bt_ctf_validation_replace_types().
1230 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1234 * Freeze the trace and the stream class.
1236 bt_ctf_stream_class_common_freeze(stream_class
);
1237 bt_ctf_trace_common_freeze(trace
);
1240 * It is safe to set the stream class's unique clock class
1241 * now because the stream class is frozen.
1243 if (expected_clock_class
) {
1244 BT_CTF_OBJECT_MOVE_REF(stream_class
->clock_class
, expected_clock_class
);
1247 BT_LOGD("Added stream class to trace: "
1248 "trace-addr=%p, trace-name=\"%s\", "
1249 "stream-class-addr=%p, stream-class-name=\"%s\", "
1250 "stream-class-id=%" PRId64
,
1251 trace
, bt_ctf_trace_common_get_name(trace
),
1252 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
1253 bt_ctf_stream_class_common_get_id(stream_class
));
1257 bt_ctf_object_set_parent(&stream_class
->base
, NULL
);
1259 if (ec_validation_outputs
) {
1260 for (i
= 0; i
< event_class_count
; i
++) {
1261 bt_ctf_validation_output_put_types(
1262 &ec_validation_outputs
[i
]);
1267 g_free(ec_validation_outputs
);
1268 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1269 bt_ctf_object_put_ref(expected_clock_class
);
1274 bt_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
1275 struct bt_ctf_clock_class
*clock_class
)
1277 struct bt_ctf_search_query query
= { .value
= clock_class
, .found
= 0 };
1280 BT_ASSERT(clock_class
);
1282 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1287 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
1288 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
)
1293 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1298 if (trace
->frozen
) {
1299 BT_LOGW("Invalid parameter: trace is frozen: "
1300 "addr=%p, name=\"%s\"",
1301 trace
, bt_ctf_trace_common_get_name(trace
));
1306 if (byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1307 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1308 "addr=%p, name=\"%s\"",
1309 trace
, bt_ctf_trace_common_get_name(trace
));
1314 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1315 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1316 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1317 BT_LOGW("Invalid parameter: invalid byte order: "
1318 "addr=%p, name=\"%s\", bo=%s",
1319 trace
, bt_ctf_trace_common_get_name(trace
),
1320 bt_ctf_byte_order_string(byte_order
));
1325 trace
->native_byte_order
= byte_order
;
1326 BT_LOGT("Set trace's native byte order: "
1327 "addr=%p, name=\"%s\", bo=%s",
1328 trace
, bt_ctf_trace_common_get_name(trace
),
1329 bt_ctf_byte_order_string(byte_order
));
1336 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
1337 struct bt_ctf_field_type_common
*packet_header_type
)
1342 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1347 if (trace
->frozen
) {
1348 BT_LOGW("Invalid parameter: trace is frozen: "
1349 "addr=%p, name=\"%s\"",
1350 trace
, bt_ctf_trace_common_get_name(trace
));
1355 /* packet_header_type must be a structure. */
1356 if (packet_header_type
&&
1357 packet_header_type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1358 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1359 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1360 trace
, bt_ctf_trace_common_get_name(trace
),
1362 bt_ctf_field_type_id_string(packet_header_type
->id
));
1367 bt_ctf_object_put_ref(trace
->packet_header_field_type
);
1368 trace
->packet_header_field_type
= bt_ctf_object_get_ref(packet_header_type
);
1369 BT_LOGT("Set trace's packet header field type: "
1370 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1371 trace
, bt_ctf_trace_common_get_name(trace
), packet_header_type
);
1377 int64_t get_stream_class_count(void *element
)
1379 return bt_ctf_trace_get_stream_class_count(
1380 (struct bt_ctf_trace
*) element
);
1384 void *get_stream_class(void *element
, int i
)
1386 return bt_ctf_trace_get_stream_class_by_index(
1387 (struct bt_ctf_trace
*) element
, i
);
1391 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1393 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1396 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1397 bt_ctf_visitor visitor
, void *data
)
1400 struct bt_ctf_visitor_object obj
= {
1402 .type
= BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1406 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1412 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1417 BT_LOGT("Visiting trace: addr=%p, name=\"%s\"",
1418 trace
, bt_ctf_trace_get_name(trace
));
1419 ret
= bt_ctf_visitor_helper(&obj
, get_stream_class_count
,
1420 get_stream_class
, visit_stream_class
, visitor
, data
);
1426 void bt_ctf_trace_destroy(struct bt_ctf_object
*obj
)
1428 struct bt_ctf_trace
*trace
= (void *) obj
;
1430 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1431 trace
, bt_ctf_trace_get_name(trace
));
1432 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace
));
1437 struct bt_ctf_trace
*bt_ctf_trace_create(void)
1439 struct bt_ctf_trace
*trace
= NULL
;
1442 BT_LOGD_STR("Creating CTF writer trace object.");
1443 trace
= g_new0(struct bt_ctf_trace
, 1);
1445 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1449 ret
= bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace
),
1450 bt_ctf_trace_destroy
);
1452 /* bt_ctf_trace_common_initialize() logs errors */
1456 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
1460 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1464 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
1466 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace
));
1469 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
1470 const unsigned char *uuid
)
1472 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace
), uuid
);
1475 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
1476 const char *name
, const char *value
)
1478 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace
),
1482 int bt_ctf_trace_set_environment_field_integer(
1483 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
1485 return bt_ctf_trace_common_set_environment_field_integer(
1486 BT_CTF_TO_COMMON(trace
), name
, value
);
1489 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
1491 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace
));
1495 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
1498 return bt_ctf_trace_common_get_environment_field_name_by_index(
1499 BT_CTF_TO_COMMON(trace
), index
);
1502 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_index(
1503 struct bt_ctf_trace
*trace
, uint64_t index
)
1505 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1506 BT_CTF_TO_COMMON(trace
), index
));
1509 struct bt_ctf_value
*bt_ctf_trace_get_environment_field_value_by_name(
1510 struct bt_ctf_trace
*trace
, const char *name
)
1512 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1513 BT_CTF_TO_COMMON(trace
), name
));
1517 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
1518 struct bt_ctf_clock_class
*clock_class
)
1520 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace
),
1521 (void *) clock_class
);
1525 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
1527 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace
));
1531 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
1532 struct bt_ctf_trace
*trace
, uint64_t index
)
1534 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index(
1535 BT_CTF_TO_COMMON(trace
), index
));
1539 int map_clock_classes_func(struct bt_ctf_stream_class_common
*stream_class
,
1540 struct bt_ctf_field_type_common
*packet_context_type
,
1541 struct bt_ctf_field_type_common
*event_header_type
)
1543 int ret
= bt_ctf_stream_class_map_clock_class(
1544 BT_CTF_FROM_COMMON(stream_class
),
1545 BT_CTF_FROM_COMMON(packet_context_type
),
1546 BT_CTF_FROM_COMMON(event_header_type
));
1549 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1555 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1556 struct bt_ctf_stream_class
*stream_class
)
1559 struct bt_ctf_clock_class
*expected_clock_class
= NULL
;
1562 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1567 if (!stream_class
) {
1568 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1573 if (stream_class
->clock
) {
1574 struct bt_ctf_clock_class
*stream_clock_class
=
1575 stream_class
->clock
->clock_class
;
1578 * Make sure this clock was also added to the
1579 * trace (potentially through its CTF writer
1584 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
1585 if (trace
->common
.clock_classes
->pdata
[i
] ==
1586 stream_clock_class
) {
1592 if (i
== trace
->common
.clock_classes
->len
) {
1594 BT_LOGW("Stream class's clock's class is not part of the trace: "
1595 "clock-class-addr=%p, clock-class-name=\"%s\"",
1597 bt_ctf_clock_class_get_name(stream_clock_class
));
1602 if (stream_class
->common
.clock_class
&&
1603 stream_class
->common
.clock_class
!=
1604 stream_class
->clock
->clock_class
) {
1606 * Stream class already has an expected clock
1607 * class, but it does not match its clock's
1610 BT_LOGW("Invalid parameter: stream class's clock's "
1611 "class does not match stream class's "
1612 "expected clock class: "
1613 "stream-class-addr=%p, "
1614 "stream-class-id=%" PRId64
", "
1615 "stream-class-name=\"%s\", "
1616 "expected-clock-class-addr=%p, "
1617 "expected-clock-class-name=\"%s\"",
1619 bt_ctf_stream_class_get_id(stream_class
),
1620 bt_ctf_stream_class_get_name(stream_class
),
1621 stream_class
->common
.clock_class
,
1622 bt_ctf_clock_class_get_name(stream_class
->common
.clock_class
));
1623 } else if (!stream_class
->common
.clock_class
) {
1625 * Set expected clock class to stream class's
1628 expected_clock_class
= stream_class
->clock
->clock_class
;
1633 ret
= bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace
),
1634 BT_CTF_TO_COMMON(stream_class
),
1635 (bt_ctf_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
1636 expected_clock_class
, map_clock_classes_func
,
1643 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1645 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace
));
1648 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1649 struct bt_ctf_trace
*trace
, uint64_t index
)
1651 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index(
1652 BT_CTF_TO_COMMON(trace
), index
));
1655 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1657 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace
));
1660 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1661 struct bt_ctf_trace
*trace
, uint64_t index
)
1663 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index(
1664 BT_CTF_TO_COMMON(trace
), index
));
1667 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1668 struct bt_ctf_trace
*trace
, uint64_t id
)
1670 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id(
1671 BT_CTF_TO_COMMON(trace
), id
));
1675 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1676 struct bt_ctf_trace
*trace
, const char *name
)
1678 return bt_ctf_object_get_ref(
1679 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace
),
1684 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1685 struct metadata_context
*context
)
1687 unsigned char *uuid
= trace
->common
.uuid
;
1690 if (trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1691 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1692 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1693 "set it with bt_ctf_trace_set_native_byte_order(): "
1694 "addr=%p, name=\"%s\"",
1695 trace
, bt_ctf_trace_get_name(trace
));
1700 g_string_append(context
->string
, "trace {\n");
1701 g_string_append(context
->string
, "\tmajor = 1;\n");
1702 g_string_append(context
->string
, "\tminor = 8;\n");
1703 BT_ASSERT(trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1704 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1705 trace
->common
.native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1707 if (trace
->common
.uuid_set
) {
1708 g_string_append_printf(context
->string
,
1709 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1710 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1711 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1712 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1713 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1716 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1717 bt_ctf_get_byte_order_string(trace
->common
.native_byte_order
));
1719 if (trace
->common
.packet_header_field_type
) {
1720 g_string_append(context
->string
, "\tpacket.header := ");
1721 context
->current_indentation_level
++;
1722 g_string_assign(context
->field_name
, "");
1723 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1724 ret
= bt_ctf_field_type_serialize_recursive(
1725 (void *) trace
->common
.packet_header_field_type
,
1730 context
->current_indentation_level
--;
1733 g_string_append(context
->string
, ";\n};\n\n");
1739 void append_env_metadata(struct bt_ctf_trace
*trace
,
1740 struct metadata_context
*context
)
1745 env_size
= bt_ctf_attributes_get_count(trace
->common
.environment
);
1746 if (env_size
<= 0) {
1750 g_string_append(context
->string
, "env {\n");
1752 for (i
= 0; i
< env_size
; i
++) {
1753 struct bt_ctf_private_value
*env_field_value_obj
= NULL
;
1754 const char *entry_name
;
1756 entry_name
= bt_ctf_attributes_get_field_name(
1757 trace
->common
.environment
, i
);
1758 env_field_value_obj
= bt_ctf_attributes_borrow_field_value(
1759 trace
->common
.environment
, i
);
1761 BT_ASSERT(entry_name
);
1762 BT_ASSERT(env_field_value_obj
);
1764 switch (bt_ctf_value_get_type(
1765 bt_ctf_private_value_as_value(env_field_value_obj
))) {
1766 case BT_CTF_VALUE_TYPE_INTEGER
:
1770 int_value
= bt_ctf_value_integer_get(
1771 bt_ctf_private_value_as_value(
1772 env_field_value_obj
));
1773 g_string_append_printf(context
->string
,
1774 "\t%s = %" PRId64
";\n", entry_name
,
1778 case BT_CTF_VALUE_TYPE_STRING
:
1780 const char *str_value
;
1781 char *escaped_str
= NULL
;
1783 str_value
= bt_ctf_value_string_get(
1784 bt_ctf_private_value_as_value(
1785 env_field_value_obj
));
1786 escaped_str
= g_strescape(str_value
, NULL
);
1788 BT_LOGE("Cannot escape string: string=\"%s\"",
1793 g_string_append_printf(context
->string
,
1794 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1803 g_string_append(context
->string
, "};\n\n");
1806 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1808 char *metadata
= NULL
;
1809 struct metadata_context
*context
= NULL
;
1814 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1818 context
= g_new0(struct metadata_context
, 1);
1820 BT_LOGE_STR("Failed to allocate one metadata context.");
1824 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1825 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1826 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1827 if (append_trace_metadata(trace
, context
)) {
1828 /* append_trace_metadata() logs errors */
1831 append_env_metadata(trace
, context
);
1832 g_ptr_array_foreach(trace
->common
.clock_classes
,
1833 (GFunc
) bt_ctf_clock_class_serialize
, context
);
1835 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
1836 /* bt_ctf_stream_class_serialize() logs details */
1837 err
= bt_ctf_stream_class_serialize(
1838 trace
->common
.stream_classes
->pdata
[i
], context
);
1840 /* bt_ctf_stream_class_serialize() logs errors */
1845 metadata
= context
->string
->str
;
1848 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1849 g_string_free(context
->field_name
, TRUE
);
1856 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1857 struct bt_ctf_trace
*trace
)
1859 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace
));
1862 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1863 enum bt_ctf_byte_order byte_order
)
1865 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace
),
1866 (int) byte_order
, false);
1869 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
1870 struct bt_ctf_trace
*trace
)
1872 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type(
1873 BT_CTF_TO_COMMON(trace
)));
1876 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
1877 struct bt_ctf_field_type
*packet_header_type
)
1879 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace
),
1880 (void *) packet_header_type
);
1883 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
1885 return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace
));