4 * Babeltrace CTF IR - Trace
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "TRACE"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-ir/trace-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/stream-internal.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/event-class.h>
38 #include <babeltrace/ctf-ir/event-class-internal.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/ctf-writer/clock-internal.h>
41 #include <babeltrace/ctf-ir/field-types-internal.h>
42 #include <babeltrace/ctf-ir/attributes-internal.h>
43 #include <babeltrace/ctf-ir/validation-internal.h>
44 #include <babeltrace/ctf-ir/visitor-internal.h>
45 #include <babeltrace/ctf-ir/utils.h>
46 #include <babeltrace/compiler-internal.h>
47 #include <babeltrace/values.h>
48 #include <babeltrace/ref.h>
49 #include <babeltrace/types.h>
50 #include <babeltrace/endian-internal.h>
55 #define DEFAULT_IDENTIFIER_SIZE 128
56 #define DEFAULT_METADATA_STRING_SIZE 4096
58 struct listener_wrapper
{
59 bt_ctf_listener_cb listener
;
64 void bt_ctf_trace_destroy(struct bt_object
*obj
);
66 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
69 const unsigned int field_type_aliases_alignments
[] = {
70 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
71 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
72 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
73 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
77 const unsigned int field_type_aliases_sizes
[] = {
78 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
79 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
80 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
81 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
82 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
83 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
86 struct bt_ctf_trace
*bt_ctf_trace_create(void)
88 struct bt_ctf_trace
*trace
= NULL
;
89 struct bt_ctf_field_type
*packet_header_type
= NULL
;
91 trace
= g_new0(struct bt_ctf_trace
, 1);
96 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_NATIVE
;
97 bt_object_init(trace
, bt_ctf_trace_destroy
);
98 trace
->clocks
= g_ptr_array_new_with_free_func(
99 (GDestroyNotify
) bt_put
);
100 trace
->streams
= g_ptr_array_new_with_free_func(
101 (GDestroyNotify
) bt_object_release
);
102 trace
->stream_classes
= g_ptr_array_new_with_free_func(
103 (GDestroyNotify
) bt_object_release
);
104 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
108 packet_header_type
= bt_ctf_field_type_structure_create();
109 if (!packet_header_type
) {
113 BT_MOVE(trace
->packet_header_type
, packet_header_type
);
115 /* Create the environment array object */
116 trace
->environment
= bt_ctf_attributes_create();
117 if (!trace
->environment
) {
121 trace
->listeners
= g_ptr_array_new_with_free_func(
122 (GDestroyNotify
) g_free
);
123 if (!trace
->listeners
) {
131 bt_put(packet_header_type
);
135 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
137 const char *name
= NULL
;
139 if (!trace
|| !trace
->name
) {
143 name
= trace
->name
->str
;
148 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
152 if (!trace
|| !name
|| trace
->frozen
) {
157 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
167 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
169 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
172 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
, const unsigned char *uuid
)
176 if (!trace
|| !uuid
|| trace
->frozen
) {
181 memcpy(trace
->uuid
, uuid
, sizeof(uuid_t
));
182 trace
->uuid_set
= BT_TRUE
;
188 void bt_ctf_trace_destroy(struct bt_object
*obj
)
190 struct bt_ctf_trace
*trace
;
192 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
193 if (trace
->environment
) {
194 bt_ctf_attributes_destroy(trace
->environment
);
198 g_string_free(trace
->name
, TRUE
);
202 g_ptr_array_free(trace
->clocks
, TRUE
);
205 if (trace
->streams
) {
206 g_ptr_array_free(trace
->streams
, TRUE
);
209 if (trace
->stream_classes
) {
210 g_ptr_array_free(trace
->stream_classes
, TRUE
);
213 if (trace
->listeners
) {
214 g_ptr_array_free(trace
->listeners
, TRUE
);
217 bt_put(trace
->packet_header_type
);
221 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
222 const char *name
, struct bt_value
*value
)
226 if (!trace
|| !name
|| !value
||
227 bt_ctf_validate_identifier(name
) ||
228 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
233 if (strchr(name
, ' ')) {
240 * New environment fields may be added to a frozen trace,
241 * but existing fields may not be changed.
243 * The object passed is frozen like all other attributes.
245 struct bt_value
*attribute
=
246 bt_ctf_attributes_get_field_value_by_name(
247 trace
->environment
, name
);
255 bt_value_freeze(value
);
258 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
265 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
266 const char *name
, const char *value
)
269 struct bt_value
*env_value_string_obj
= NULL
;
271 if (!trace
|| !name
|| !value
) {
278 * New environment fields may be added to a frozen trace,
279 * but existing fields may not be changed.
281 struct bt_value
*attribute
=
282 bt_ctf_attributes_get_field_value_by_name(
283 trace
->environment
, name
);
292 env_value_string_obj
= bt_value_string_create_init(value
);
294 if (!env_value_string_obj
) {
300 bt_value_freeze(env_value_string_obj
);
302 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
303 env_value_string_obj
);
306 BT_PUT(env_value_string_obj
);
310 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
311 const char *name
, int64_t value
)
314 struct bt_value
*env_value_integer_obj
= NULL
;
316 if (!trace
|| !name
) {
323 * New environment fields may be added to a frozen trace,
324 * but existing fields may not be changed.
326 struct bt_value
*attribute
=
327 bt_ctf_attributes_get_field_value_by_name(
328 trace
->environment
, name
);
337 env_value_integer_obj
= bt_value_integer_create_init(value
);
338 if (!env_value_integer_obj
) {
343 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
344 env_value_integer_obj
);
346 bt_value_freeze(env_value_integer_obj
);
349 BT_PUT(env_value_integer_obj
);
353 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
362 ret
= bt_ctf_attributes_get_count(trace
->environment
);
369 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
372 const char *ret
= NULL
;
378 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
384 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
385 struct bt_ctf_trace
*trace
, uint64_t index
)
387 struct bt_value
*ret
= NULL
;
393 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
399 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
400 struct bt_ctf_trace
*trace
, const char *name
)
402 struct bt_value
*ret
= NULL
;
404 if (!trace
|| !name
) {
408 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
415 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
416 struct bt_ctf_clock_class
*clock_class
)
420 if (!trace
|| trace
->is_static
||
421 !bt_ctf_clock_class_is_valid(clock_class
)) {
426 /* Check for duplicate clock classes */
427 if (bt_ctf_trace_has_clock_class(trace
, clock_class
)) {
433 g_ptr_array_add(trace
->clocks
, clock_class
);
436 bt_ctf_clock_class_freeze(clock_class
);
442 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
444 int64_t ret
= (int64_t) -1;
450 ret
= trace
->clocks
->len
;
455 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
456 struct bt_ctf_trace
*trace
, uint64_t index
)
458 struct bt_ctf_clock_class
*clock_class
= NULL
;
460 if (!trace
|| index
>= trace
->clocks
->len
) {
464 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
471 bool packet_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
472 struct bt_ctf_field_type
*packet_header_type
)
475 bool is_valid
= true;
476 struct bt_ctf_field_type
*field_type
= NULL
;
478 if (!packet_header_type
) {
480 * No packet header field type: trace must have only
481 * one stream. At this point the stream class being
482 * added is not part of the trace yet, so we validate
483 * that the trace contains no stream classes yet.
485 if (trace
->stream_classes
->len
>= 1) {
486 BT_LOGW_STR("Invalid packet header field type: "
487 "packet header field type does not exist but there's more than one stream class in the trace.");
491 /* No packet header field type: valid at this point */
495 /* Packet header field type, if it exists, must be a structure */
496 if (!bt_ctf_field_type_is_structure(packet_header_type
)) {
497 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
498 "ft-addr=%p, ft-id=%s",
500 bt_ctf_field_type_id_string(packet_header_type
->id
));
505 * If there's a `magic` field, it must be a 32-bit unsigned
506 * integer field type. Also it must be the first field of the
507 * packet header field type.
509 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
510 packet_header_type
, "magic");
512 const char *field_name
;
514 if (!bt_ctf_field_type_is_integer(field_type
)) {
515 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
516 "magic-ft-addr=%p, magic-ft-id=%s",
518 bt_ctf_field_type_id_string(field_type
->id
));
522 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
523 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
524 "magic-ft-addr=%p", field_type
);
528 if (bt_ctf_field_type_integer_get_size(field_type
) != 32) {
529 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
530 "magic-ft-addr=%p, magic-ft-size=%u",
532 bt_ctf_field_type_integer_get_size(field_type
));
536 ret
= bt_ctf_field_type_structure_get_field_by_index(
537 packet_header_type
, &field_name
, NULL
, 0);
540 if (strcmp(field_name
, "magic") != 0) {
541 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
542 "magic-ft-addr=%p, first-field-name=\"%s\"",
543 field_type
, field_name
);
551 * If there's a `uuid` field, it must be an array field type of
552 * length 16 with an 8-bit unsigned integer element field type.
554 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
555 packet_header_type
, "uuid");
557 struct bt_ctf_field_type
*elem_ft
;
559 if (!bt_ctf_field_type_is_array(field_type
)) {
560 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
561 "uuid-ft-addr=%p, uuid-ft-id=%s",
563 bt_ctf_field_type_id_string(field_type
->id
));
567 if (bt_ctf_field_type_array_get_length(field_type
) != 16) {
568 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
569 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
571 bt_ctf_field_type_array_get_length(field_type
));
575 elem_ft
= bt_ctf_field_type_array_get_element_type(field_type
);
578 if (!bt_ctf_field_type_is_integer(elem_ft
)) {
579 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
580 "elem-ft-addr=%p, elem-ft-id=%s",
582 bt_ctf_field_type_id_string(elem_ft
->id
));
587 if (bt_ctf_field_type_integer_is_signed(elem_ft
)) {
588 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
589 "elem-ft-addr=%p", elem_ft
);
594 if (bt_ctf_field_type_integer_get_size(elem_ft
) != 8) {
595 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
596 "elem-ft-addr=%p, elem-ft-size=%u",
598 bt_ctf_field_type_integer_get_size(elem_ft
));
608 * The `stream_id` field must exist if there's more than one
609 * stream classes in the trace.
611 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
612 packet_header_type
, "stream_id");
614 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
615 BT_LOGW_STR("Invalid packet header field type: "
616 "`stream_id` field does not exist but there's more than one stream class in the trace.");
621 * If there's a `stream_id` field, it must be an unsigned
622 * integer field type.
625 if (!bt_ctf_field_type_is_integer(field_type
)) {
626 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
627 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
629 bt_ctf_field_type_id_string(field_type
->id
));
633 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
634 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
635 "stream-id-ft-addr=%p", field_type
);
653 bool packet_context_field_type_is_valid(struct bt_ctf_trace
*trace
,
654 struct bt_ctf_stream_class
*stream_class
,
655 struct bt_ctf_field_type
*packet_context_type
)
657 bool is_valid
= true;
658 struct bt_ctf_field_type
*field_type
= NULL
;
660 if (!packet_context_type
) {
661 /* No packet context field type: valid at this point */
665 /* Packet context field type, if it exists, must be a structure */
666 if (!bt_ctf_field_type_is_structure(packet_context_type
)) {
667 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
668 "ft-addr=%p, ft-id=%s",
670 bt_ctf_field_type_id_string(packet_context_type
->id
));
675 * If there's a `packet_size` field, it must be an unsigned
676 * integer field type.
678 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
679 packet_context_type
, "packet_size");
681 if (!bt_ctf_field_type_is_integer(field_type
)) {
682 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
683 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
685 bt_ctf_field_type_id_string(field_type
->id
));
689 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
690 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
691 "packet-size-ft-addr=%p", field_type
);
699 * If there's a `content_size` field, it must be an unsigned
700 * integer field type.
702 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
703 packet_context_type
, "content_size");
705 if (!bt_ctf_field_type_is_integer(field_type
)) {
706 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
707 "content-size-ft-addr=%p, content-size-ft-id=%s",
709 bt_ctf_field_type_id_string(field_type
->id
));
713 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
714 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
715 "content-size-ft-addr=%p", field_type
);
723 * If there's a `events_discarded` field, it must be an unsigned
724 * integer field type.
726 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
727 packet_context_type
, "events_discarded");
729 if (!bt_ctf_field_type_is_integer(field_type
)) {
730 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
731 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
733 bt_ctf_field_type_id_string(field_type
->id
));
737 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
738 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
739 "events-discarded-ft-addr=%p", field_type
);
747 * If there's a `timestamp_begin` field, it must be an unsigned
748 * integer field type. Also, if the trace is not a CTF writer's
749 * trace, then we cannot automatically set the mapped clock
750 * class of this field, so it must have a mapped clock class.
752 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
753 packet_context_type
, "timestamp_begin");
755 if (!bt_ctf_field_type_is_integer(field_type
)) {
756 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
757 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
759 bt_ctf_field_type_id_string(field_type
->id
));
763 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
764 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
765 "timestamp-begin-ft-addr=%p", field_type
);
769 if (!trace
->is_created_by_writer
) {
770 struct bt_ctf_clock_class
*clock_class
=
771 bt_ctf_field_type_integer_get_mapped_clock_class(
776 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
777 "timestamp-begin-ft-addr=%p", field_type
);
786 * If there's a `timestamp_end` field, it must be an unsigned
787 * integer field type. Also, if the trace is not a CTF writer's
788 * trace, then we cannot automatically set the mapped clock
789 * class of this field, so it must have a mapped clock class.
791 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
792 packet_context_type
, "timestamp_end");
794 if (!bt_ctf_field_type_is_integer(field_type
)) {
795 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
796 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
798 bt_ctf_field_type_id_string(field_type
->id
));
802 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
803 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
804 "timestamp-end-ft-addr=%p", field_type
);
808 if (!trace
->is_created_by_writer
) {
809 struct bt_ctf_clock_class
*clock_class
=
810 bt_ctf_field_type_integer_get_mapped_clock_class(
815 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
816 "timestamp-end-ft-addr=%p", field_type
);
835 bool event_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
836 struct bt_ctf_stream_class
*stream_class
,
837 struct bt_ctf_field_type
*event_header_type
)
839 bool is_valid
= true;
840 struct bt_ctf_field_type
*field_type
= NULL
;
843 * We do not validate that the `timestamp` field exists here
844 * because CTF does not require this exact name to be mapped to
848 if (!event_header_type
) {
850 * No event header field type: stream class must have
851 * only one event class.
853 if (bt_ctf_stream_class_get_event_class_count(stream_class
) > 1) {
854 BT_LOGW_STR("Invalid event header field type: "
855 "event header field type does not exist but there's more than one event class in the stream class.");
859 /* No event header field type: valid at this point */
863 /* Event header field type, if it exists, must be a structure */
864 if (!bt_ctf_field_type_is_structure(event_header_type
)) {
865 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
866 "ft-addr=%p, ft-id=%s",
868 bt_ctf_field_type_id_string(event_header_type
->id
));
873 * If there's an `id` field, it must be an unsigned integer
874 * field type or an enumeration field type with an unsigned
875 * integer container field type.
877 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
878 event_header_type
, "id");
880 struct bt_ctf_field_type
*int_ft
;
882 if (bt_ctf_field_type_is_integer(field_type
)) {
883 int_ft
= bt_get(field_type
);
884 } else if (bt_ctf_field_type_is_enumeration(field_type
)) {
885 int_ft
= bt_ctf_field_type_enumeration_get_container_type(
888 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
889 "id-ft-addr=%p, id-ft-id=%s",
891 bt_ctf_field_type_id_string(field_type
->id
));
896 if (bt_ctf_field_type_integer_is_signed(int_ft
)) {
897 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
898 "id-ft-addr=%p", int_ft
);
916 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
917 struct bt_ctf_stream_class
*stream_class
)
922 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
923 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
924 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
925 BT_CTF_VALIDATION_FLAG_TRACE
|
926 BT_CTF_VALIDATION_FLAG_STREAM
;
927 const enum bt_ctf_validation_flag ec_validation_flags
=
928 BT_CTF_VALIDATION_FLAG_EVENT
;
929 struct bt_ctf_field_type
*packet_header_type
= NULL
;
930 struct bt_ctf_field_type
*packet_context_type
= NULL
;
931 struct bt_ctf_field_type
*event_header_type
= NULL
;
932 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
933 int64_t event_class_count
;
934 struct bt_ctf_trace
*current_parent_trace
= NULL
;
936 if (!trace
|| !stream_class
|| trace
->is_static
) {
942 * At the end of this function we freeze the trace, so its
943 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
945 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
950 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
951 if (current_parent_trace
) {
952 /* Stream class is already associated to a trace, abort. */
958 bt_ctf_stream_class_get_event_class_count(stream_class
);
959 assert(event_class_count
>= 0);
961 /* Check for duplicate stream classes */
962 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
963 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
964 /* Stream class already registered to the trace */
970 if (stream_class
->clock
) {
971 struct bt_ctf_clock_class
*stream_clock_class
=
972 stream_class
->clock
->clock_class
;
974 if (trace
->is_created_by_writer
) {
976 * Make sure this clock was also added to the
977 * trace (potentially through its CTF writer
982 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
983 if (trace
->clocks
->pdata
[i
] ==
984 stream_clock_class
) {
990 if (i
== trace
->clocks
->len
) {
997 * This trace was NOT created by a CTF writer,
998 * thus do not allow the stream class to add to
999 * have a clock at all. Those are two
1000 * independent APIs (non-writer and writer
1001 * APIs), and isolating them simplifies things.
1009 * We're about to freeze both the trace and the stream class.
1010 * Also, each event class contained in this stream class are
1013 * This trace, this stream class, and all its event classes
1014 * should be valid at this point.
1016 * Validate trace and stream class first, then each event
1017 * class of this stream class can be validated individually.
1019 packet_header_type
=
1020 bt_ctf_trace_get_packet_header_type(trace
);
1021 packet_context_type
=
1022 bt_ctf_stream_class_get_packet_context_type(stream_class
);
1024 bt_ctf_stream_class_get_event_header_type(stream_class
);
1025 stream_event_ctx_type
=
1026 bt_ctf_stream_class_get_event_context_type(stream_class
);
1027 ret
= bt_ctf_validate_class_types(trace
->environment
,
1028 packet_header_type
, packet_context_type
, event_header_type
,
1029 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1030 stream_class
->valid
, 1, &trace_sc_validation_output
,
1031 trace_sc_validation_flags
);
1032 BT_PUT(packet_header_type
);
1033 BT_PUT(packet_context_type
);
1034 BT_PUT(event_header_type
);
1035 BT_PUT(stream_event_ctx_type
);
1039 * This means something went wrong during the validation
1040 * process, not that the objects are invalid.
1045 if ((trace_sc_validation_output
.valid_flags
&
1046 trace_sc_validation_flags
) !=
1047 trace_sc_validation_flags
) {
1048 /* Invalid trace/stream class */
1053 if (event_class_count
> 0) {
1054 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1056 if (!ec_validation_outputs
) {
1062 /* Validate each event class individually */
1063 for (i
= 0; i
< event_class_count
; i
++) {
1064 struct bt_ctf_event_class
*event_class
=
1065 bt_ctf_stream_class_get_event_class_by_index(
1067 struct bt_ctf_field_type
*event_context_type
= NULL
;
1068 struct bt_ctf_field_type
*event_payload_type
= NULL
;
1070 event_context_type
=
1071 bt_ctf_event_class_get_context_type(event_class
);
1072 event_payload_type
=
1073 bt_ctf_event_class_get_payload_type(event_class
);
1076 * It is important to use the field types returned by
1077 * the previous trace and stream class validation here
1078 * because copies could have been made.
1080 ret
= bt_ctf_validate_class_types(trace
->environment
,
1081 trace_sc_validation_output
.packet_header_type
,
1082 trace_sc_validation_output
.packet_context_type
,
1083 trace_sc_validation_output
.event_header_type
,
1084 trace_sc_validation_output
.stream_event_ctx_type
,
1085 event_context_type
, event_payload_type
,
1086 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1087 ec_validation_flags
);
1088 BT_PUT(event_context_type
);
1089 BT_PUT(event_payload_type
);
1090 BT_PUT(event_class
);
1096 if ((ec_validation_outputs
[i
].valid_flags
&
1097 ec_validation_flags
) != ec_validation_flags
) {
1098 /* Invalid event class */
1104 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
1105 if (stream_id
< 0) {
1106 stream_id
= trace
->next_stream_id
++;
1107 if (stream_id
< 0) {
1112 /* Try to assign a new stream id */
1113 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1114 if (stream_id
== bt_ctf_stream_class_get_id(
1115 trace
->stream_classes
->pdata
[i
])) {
1116 /* Duplicate stream id found */
1122 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
1124 /* TODO Should retry with a different stream id */
1131 * At this point all the field types in the validation output
1132 * are valid. Validate the semantics of some scopes according to
1133 * the CTF specification.
1135 if (!packet_header_field_type_is_valid(trace
,
1136 trace_sc_validation_output
.packet_header_type
)) {
1141 if (!packet_context_field_type_is_valid(trace
,
1143 trace_sc_validation_output
.packet_context_type
)) {
1148 if (!event_header_field_type_is_valid(trace
,
1150 trace_sc_validation_output
.event_header_type
)) {
1156 * Now is the time to automatically map specific field types of
1157 * the stream class's packet context and event header field
1158 * types to the stream class's clock's class if they are not
1159 * mapped to a clock class yet. We do it here because we know
1160 * that after this point, everything is frozen so it won't be
1161 * possible for the user to modify the stream class's clock, or
1162 * to map those field types to other clock classes.
1164 if (trace
->is_created_by_writer
) {
1165 if (bt_ctf_stream_class_map_clock_class(stream_class
,
1166 trace_sc_validation_output
.packet_context_type
,
1167 trace_sc_validation_output
.event_header_type
)) {
1173 bt_object_set_parent(stream_class
, trace
);
1174 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1177 * At this point we know that the function will be successful.
1178 * Therefore we can replace the trace and stream class field
1179 * types with what's in their validation output structure and
1180 * mark them as valid. We can also replace the field types of
1181 * all the event classes of the stream class and mark them as
1184 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1185 &trace_sc_validation_output
, trace_sc_validation_flags
);
1187 stream_class
->valid
= 1;
1190 * Put what was not moved in bt_ctf_validation_replace_types().
1192 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1194 for (i
= 0; i
< event_class_count
; i
++) {
1195 struct bt_ctf_event_class
*event_class
=
1196 bt_ctf_stream_class_get_event_class_by_index(
1199 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1200 &ec_validation_outputs
[i
], ec_validation_flags
);
1201 event_class
->valid
= 1;
1202 BT_PUT(event_class
);
1205 * Put what was not moved in
1206 * bt_ctf_validation_replace_types().
1208 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1212 * Freeze the trace and the stream class.
1214 bt_ctf_stream_class_freeze(stream_class
);
1215 bt_ctf_trace_freeze(trace
);
1217 /* Notifiy listeners of the trace's schema modification. */
1218 bt_ctf_stream_class_visit(stream_class
,
1219 bt_ctf_trace_object_modification
, trace
);
1222 bt_object_set_parent(stream_class
, NULL
);
1224 if (ec_validation_outputs
) {
1225 for (i
= 0; i
< event_class_count
; i
++) {
1226 bt_ctf_validation_output_put_types(
1227 &ec_validation_outputs
[i
]);
1232 g_free(ec_validation_outputs
);
1233 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1234 bt_put(current_parent_trace
);
1235 assert(!packet_header_type
);
1236 assert(!packet_context_type
);
1237 assert(!event_header_type
);
1238 assert(!stream_event_ctx_type
);
1243 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1252 ret
= (int64_t) trace
->streams
->len
;
1258 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1259 struct bt_ctf_trace
*trace
,
1262 struct bt_ctf_stream
*stream
= NULL
;
1264 if (!trace
|| index
>= trace
->streams
->len
) {
1268 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1274 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1283 ret
= (int64_t) trace
->stream_classes
->len
;
1288 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1289 struct bt_ctf_trace
*trace
, uint64_t index
)
1291 struct bt_ctf_stream_class
*stream_class
= NULL
;
1293 if (!trace
|| index
>= trace
->stream_classes
->len
) {
1297 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1298 bt_get(stream_class
);
1300 return stream_class
;
1303 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1304 struct bt_ctf_trace
*trace
, uint64_t id_param
)
1307 struct bt_ctf_stream_class
*stream_class
= NULL
;
1308 int64_t id
= (int64_t) id_param
;
1310 if (!trace
|| id
< 0) {
1314 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1315 struct bt_ctf_stream_class
*stream_class_candidate
;
1317 stream_class_candidate
=
1318 g_ptr_array_index(trace
->stream_classes
, i
);
1320 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
1322 stream_class
= stream_class_candidate
;
1323 bt_get(stream_class
);
1329 return stream_class
;
1332 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1333 struct bt_ctf_trace
*trace
, const char *name
)
1336 struct bt_ctf_clock_class
*clock_class
= NULL
;
1338 if (!trace
|| !name
) {
1342 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1343 struct bt_ctf_clock_class
*cur_clk
=
1344 g_ptr_array_index(trace
->clocks
, i
);
1345 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
1347 if (!cur_clk_name
) {
1351 if (!strcmp(cur_clk_name
, name
)) {
1352 clock_class
= cur_clk
;
1353 bt_get(clock_class
);
1363 bt_bool
bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
1364 struct bt_ctf_clock_class
*clock_class
)
1366 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1369 assert(clock_class
);
1371 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1376 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
1380 switch (byte_order
) {
1381 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1384 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1387 case BT_CTF_BYTE_ORDER_NATIVE
:
1398 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1399 struct metadata_context
*context
)
1401 unsigned char *uuid
= trace
->uuid
;
1404 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
1409 g_string_append(context
->string
, "trace {\n");
1410 g_string_append(context
->string
, "\tmajor = 1;\n");
1411 g_string_append(context
->string
, "\tminor = 8;\n");
1412 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1413 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1414 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1416 if (trace
->uuid_set
) {
1417 g_string_append_printf(context
->string
,
1418 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1419 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1420 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1421 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1422 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1425 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1426 get_byte_order_string(trace
->native_byte_order
));
1428 if (trace
->packet_header_type
) {
1429 g_string_append(context
->string
, "\tpacket.header := ");
1430 context
->current_indentation_level
++;
1431 g_string_assign(context
->field_name
, "");
1432 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
1437 context
->current_indentation_level
--;
1440 g_string_append(context
->string
, ";\n};\n\n");
1446 void append_env_metadata(struct bt_ctf_trace
*trace
,
1447 struct metadata_context
*context
)
1452 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
1454 if (env_size
<= 0) {
1458 g_string_append(context
->string
, "env {\n");
1460 for (i
= 0; i
< env_size
; i
++) {
1461 struct bt_value
*env_field_value_obj
= NULL
;
1462 const char *entry_name
;
1464 entry_name
= bt_ctf_attributes_get_field_name(
1465 trace
->environment
, i
);
1466 env_field_value_obj
= bt_ctf_attributes_get_field_value(
1467 trace
->environment
, i
);
1469 if (!entry_name
|| !env_field_value_obj
) {
1473 switch (bt_value_get_type(env_field_value_obj
)) {
1474 case BT_VALUE_TYPE_INTEGER
:
1479 ret
= bt_value_integer_get(env_field_value_obj
,
1486 g_string_append_printf(context
->string
,
1487 "\t%s = %" PRId64
";\n", entry_name
,
1491 case BT_VALUE_TYPE_STRING
:
1494 const char *str_value
;
1495 char *escaped_str
= NULL
;
1497 ret
= bt_value_string_get(env_field_value_obj
,
1504 escaped_str
= g_strescape(str_value
, NULL
);
1510 g_string_append_printf(context
->string
,
1511 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1521 BT_PUT(env_field_value_obj
);
1524 g_string_append(context
->string
, "};\n\n");
1527 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1529 char *metadata
= NULL
;
1530 struct metadata_context
*context
= NULL
;
1538 context
= g_new0(struct metadata_context
, 1);
1543 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1544 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1545 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1546 if (append_trace_metadata(trace
, context
)) {
1549 append_env_metadata(trace
, context
);
1550 g_ptr_array_foreach(trace
->clocks
,
1551 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1553 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1554 err
= bt_ctf_stream_class_serialize(
1555 trace
->stream_classes
->pdata
[i
], context
);
1561 metadata
= context
->string
->str
;
1563 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1564 g_string_free(context
->field_name
, TRUE
);
1570 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1571 struct bt_ctf_trace
*trace
)
1573 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1579 ret
= trace
->native_byte_order
;
1585 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1586 enum bt_ctf_byte_order byte_order
)
1590 if (!trace
|| trace
->frozen
) {
1595 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1596 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1597 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1602 trace
->native_byte_order
= byte_order
;
1608 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1609 struct bt_ctf_trace
*trace
)
1611 struct bt_ctf_field_type
*field_type
= NULL
;
1617 bt_get(trace
->packet_header_type
);
1618 field_type
= trace
->packet_header_type
;
1623 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1624 struct bt_ctf_field_type
*packet_header_type
)
1628 if (!trace
|| trace
->frozen
) {
1633 /* packet_header_type must be a structure. */
1634 if (packet_header_type
&&
1635 bt_ctf_field_type_get_type_id(packet_header_type
) !=
1636 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1641 bt_put(trace
->packet_header_type
);
1642 trace
->packet_header_type
= bt_get(packet_header_type
);
1648 int64_t get_stream_class_count(void *element
)
1650 return bt_ctf_trace_get_stream_class_count(
1651 (struct bt_ctf_trace
*) element
);
1655 void *get_stream_class(void *element
, int i
)
1657 return bt_ctf_trace_get_stream_class_by_index(
1658 (struct bt_ctf_trace
*) element
, i
);
1662 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1664 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1667 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1668 bt_ctf_visitor visitor
, void *data
)
1671 struct bt_ctf_object obj
=
1672 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1674 if (!trace
|| !visitor
) {
1679 ret
= visitor_helper(&obj
, get_stream_class_count
,
1680 get_stream_class
, visit_stream_class
, visitor
, data
);
1686 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1688 struct listener_wrapper
*listener_wrapper
= data
;
1690 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1694 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1695 bt_ctf_listener_cb listener
, void *listener_data
)
1698 struct listener_wrapper
*listener_wrapper
=
1699 g_new0(struct listener_wrapper
, 1);
1701 if (!trace
|| !listener
|| !listener_wrapper
) {
1706 listener_wrapper
->listener
= listener
;
1707 listener_wrapper
->data
= listener_data
;
1709 /* Visit the current schema. */
1710 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
1716 * Add listener to the array of callbacks which will be invoked on
1719 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
1722 g_free(listener_wrapper
);
1727 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
1731 struct bt_ctf_trace
*trace
= trace_ptr
;
1736 if (trace
->listeners
->len
== 0) {
1740 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
1741 struct listener_wrapper
*listener
=
1742 g_ptr_array_index(trace
->listeners
, i
);
1744 listener
->listener(object
, listener
->data
);
1751 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1754 unsigned int alignment
, size
;
1755 struct bt_ctf_field_type
*field_type
= NULL
;
1757 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1761 alignment
= field_type_aliases_alignments
[alias
];
1762 size
= field_type_aliases_sizes
[alias
];
1763 field_type
= bt_ctf_field_type_integer_create(size
);
1764 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
1773 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1777 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1778 bt_ctf_attributes_freeze(trace
->environment
);
1780 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1781 struct bt_ctf_clock_class
*clock_class
=
1782 g_ptr_array_index(trace
->clocks
, i
);
1784 bt_ctf_clock_class_freeze(clock_class
);
1790 bt_bool
bt_ctf_trace_is_static(struct bt_ctf_trace
*trace
)
1792 bt_bool is_static
= BT_FALSE
;
1798 is_static
= trace
->is_static
;
1804 int bt_ctf_trace_set_is_static(struct bt_ctf_trace
*trace
)
1813 trace
->is_static
= BT_TRUE
;
1814 bt_ctf_trace_freeze(trace
);