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 #include <babeltrace/ctf-ir/trace-internal.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-ir/stream-internal.h>
32 #include <babeltrace/ctf-ir/stream-class-internal.h>
33 #include <babeltrace/ctf-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/event-class.h>
35 #include <babeltrace/ctf-ir/event-class-internal.h>
36 #include <babeltrace/ctf-writer/functor-internal.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/attributes-internal.h>
39 #include <babeltrace/ctf-ir/validation-internal.h>
40 #include <babeltrace/ctf-ir/utils.h>
41 #include <babeltrace/compiler.h>
42 #include <babeltrace/values.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/endian.h>
46 #define DEFAULT_IDENTIFIER_SIZE 128
47 #define DEFAULT_METADATA_STRING_SIZE 4096
50 void bt_ctf_trace_destroy(struct bt_object
*obj
);
52 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
54 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
57 const unsigned int field_type_aliases_alignments
[] = {
58 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
59 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
60 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
61 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
65 const unsigned int field_type_aliases_sizes
[] = {
66 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
67 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
68 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
69 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
70 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
71 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
74 struct bt_ctf_trace
*bt_ctf_trace_create(void)
76 struct bt_ctf_trace
*trace
= NULL
;
78 trace
= g_new0(struct bt_ctf_trace
, 1);
83 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
84 bt_object_init(trace
, bt_ctf_trace_destroy
);
85 trace
->clocks
= g_ptr_array_new_with_free_func(
86 (GDestroyNotify
) bt_put
);
87 trace
->streams
= g_ptr_array_new_with_free_func(
88 (GDestroyNotify
) bt_object_release
);
89 trace
->stream_classes
= g_ptr_array_new_with_free_func(
90 (GDestroyNotify
) bt_object_release
);
91 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
95 /* Generate a trace UUID */
96 uuid_generate(trace
->uuid
);
97 if (init_trace_packet_header(trace
)) {
101 /* Create the environment array object */
102 trace
->environment
= bt_ctf_attributes_create();
103 if (!trace
->environment
) {
114 void bt_ctf_trace_destroy(struct bt_object
*obj
)
116 struct bt_ctf_trace
*trace
;
118 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
119 if (trace
->environment
) {
120 bt_ctf_attributes_destroy(trace
->environment
);
124 g_ptr_array_free(trace
->clocks
, TRUE
);
127 if (trace
->streams
) {
128 g_ptr_array_free(trace
->streams
, TRUE
);
131 if (trace
->stream_classes
) {
132 g_ptr_array_free(trace
->stream_classes
, TRUE
);
135 bt_put(trace
->packet_header_type
);
139 struct bt_ctf_stream
*bt_ctf_trace_create_stream(struct bt_ctf_trace
*trace
,
140 struct bt_ctf_stream_class
*stream_class
)
143 int stream_class_found
= 0;
145 struct bt_ctf_stream
*stream
= NULL
;
147 if (!trace
|| !stream_class
) {
151 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
152 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
153 stream_class_found
= 1;
157 if (!stream_class_found
) {
158 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
164 stream
= bt_ctf_stream_create(stream_class
, trace
);
169 g_ptr_array_add(trace
->streams
, stream
);
176 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
177 const char *name
, struct bt_value
*value
)
181 if (!trace
|| !name
|| !value
||
182 bt_ctf_validate_identifier(name
) ||
183 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
188 if (strchr(name
, ' ')) {
195 * New environment fields may be added to a frozen trace,
196 * but existing fields may not be changed.
198 * The object passed is frozen like all other attributes.
200 struct bt_value
*attribute
=
201 bt_ctf_attributes_get_field_value_by_name(
202 trace
->environment
, name
);
210 bt_value_freeze(value
);
213 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
220 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
221 const char *name
, const char *value
)
224 struct bt_value
*env_value_string_obj
= NULL
;
226 if (!trace
|| !name
|| !value
) {
233 * New environment fields may be added to a frozen trace,
234 * but existing fields may not be changed.
236 struct bt_value
*attribute
=
237 bt_ctf_attributes_get_field_value_by_name(
238 trace
->environment
, name
);
247 env_value_string_obj
= bt_value_string_create_init(value
);
249 if (!env_value_string_obj
) {
255 bt_value_freeze(env_value_string_obj
);
257 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
258 env_value_string_obj
);
261 BT_PUT(env_value_string_obj
);
265 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
266 const char *name
, int64_t value
)
269 struct bt_value
*env_value_integer_obj
= NULL
;
271 if (!trace
|| !name
) {
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_integer_obj
= bt_value_integer_create_init(value
);
293 if (!env_value_integer_obj
) {
298 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
299 env_value_integer_obj
);
301 bt_value_freeze(env_value_integer_obj
);
304 BT_PUT(env_value_integer_obj
);
308 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
317 ret
= bt_ctf_attributes_get_count(trace
->environment
);
324 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
327 const char *ret
= NULL
;
333 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
339 struct bt_value
*bt_ctf_trace_get_environment_field_value(
340 struct bt_ctf_trace
*trace
, int index
)
342 struct bt_value
*ret
= NULL
;
348 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
354 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
355 struct bt_ctf_trace
*trace
, const char *name
)
357 struct bt_value
*ret
= NULL
;
359 if (!trace
|| !name
) {
363 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
370 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
371 struct bt_ctf_clock
*clock
)
374 struct search_query query
= { .value
= clock
, .found
= 0 };
376 if (!trace
|| !clock
) {
381 /* Check for duplicate clocks */
382 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
389 g_ptr_array_add(trace
->clocks
, clock
);
392 bt_ctf_clock_freeze(clock
);
398 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
406 ret
= trace
->clocks
->len
;
411 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
414 struct bt_ctf_clock
*clock
= NULL
;
416 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
420 clock
= g_ptr_array_index(trace
->clocks
, index
);
426 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
427 struct bt_ctf_stream_class
*stream_class
)
431 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
432 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
433 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
434 BT_CTF_VALIDATION_FLAG_TRACE
|
435 BT_CTF_VALIDATION_FLAG_STREAM
;
436 const enum bt_ctf_validation_flag ec_validation_flags
=
437 BT_CTF_VALIDATION_FLAG_EVENT
;
438 struct bt_ctf_field_type
*packet_header_type
= NULL
;
439 struct bt_ctf_field_type
*packet_context_type
= NULL
;
440 struct bt_ctf_field_type
*event_header_type
= NULL
;
441 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
442 int event_class_count
;
443 struct bt_ctf_clock
*clock_to_add_to_trace
= NULL
;
445 if (!trace
|| !stream_class
) {
451 bt_ctf_stream_class_get_event_class_count(stream_class
);
452 assert(event_class_count
>= 0);
454 /* Check for duplicate stream classes */
455 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
456 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
457 /* Stream class already registered to the trace */
464 * If the stream class has a clock, register this clock to this
465 * trace if not already done.
467 if (stream_class
->clock
) {
468 const char *clock_name
=
469 bt_ctf_clock_get_name(stream_class
->clock
);
470 struct bt_ctf_clock
*trace_clock
;
473 trace_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
476 if (trace_clock
!= stream_class
->clock
) {
478 * Error: two different clocks in the
479 * trace would share the same name.
485 clock_to_add_to_trace
= bt_get(stream_class
->clock
);
490 * We're about to freeze both the trace and the stream class.
491 * Also, each event class contained in this stream class are
494 * This trace, this stream class, and all its event classes
495 * should be valid at this point.
497 * Validate trace and stream class first, then each event
498 * class of this stream class can be validated individually.
501 bt_ctf_trace_get_packet_header_type(trace
);
502 packet_context_type
=
503 bt_ctf_stream_class_get_packet_context_type(stream_class
);
505 bt_ctf_stream_class_get_event_header_type(stream_class
);
506 stream_event_ctx_type
=
507 bt_ctf_stream_class_get_event_context_type(stream_class
);
508 ret
= bt_ctf_validate_class_types(trace
->environment
,
509 packet_header_type
, packet_context_type
, event_header_type
,
510 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
511 stream_class
->valid
, 1, &trace_sc_validation_output
,
512 trace_sc_validation_flags
);
513 BT_PUT(packet_header_type
);
514 BT_PUT(packet_context_type
);
515 BT_PUT(event_header_type
);
516 BT_PUT(stream_event_ctx_type
);
520 * This means something went wrong during the validation
521 * process, not that the objects are invalid.
526 if ((trace_sc_validation_output
.valid_flags
&
527 trace_sc_validation_flags
) !=
528 trace_sc_validation_flags
) {
529 /* Invalid trace/stream class */
534 if (event_class_count
> 0) {
535 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
537 if (!ec_validation_outputs
) {
543 /* Validate each event class individually */
544 for (i
= 0; i
< event_class_count
; i
++) {
545 struct bt_ctf_event_class
*event_class
=
546 bt_ctf_stream_class_get_event_class(stream_class
, i
);
547 struct bt_ctf_field_type
*event_context_type
= NULL
;
548 struct bt_ctf_field_type
*event_payload_type
= NULL
;
551 bt_ctf_event_class_get_context_type(event_class
);
553 bt_ctf_event_class_get_payload_type(event_class
);
556 * It is important to use the field types returned by
557 * the previous trace and stream class validation here
558 * because copies could have been made.
560 ret
= bt_ctf_validate_class_types(trace
->environment
,
561 trace_sc_validation_output
.packet_header_type
,
562 trace_sc_validation_output
.packet_context_type
,
563 trace_sc_validation_output
.event_header_type
,
564 trace_sc_validation_output
.stream_event_ctx_type
,
565 event_context_type
, event_payload_type
,
566 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
567 ec_validation_flags
);
568 BT_PUT(event_context_type
);
569 BT_PUT(event_payload_type
);
576 if ((ec_validation_outputs
[i
].valid_flags
&
577 ec_validation_flags
) != ec_validation_flags
) {
578 /* Invalid event class */
584 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
586 stream_id
= trace
->next_stream_id
++;
588 /* Try to assign a new stream id */
589 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
590 if (stream_id
== bt_ctf_stream_class_get_id(
591 trace
->stream_classes
->pdata
[i
])) {
592 /* Duplicate stream id found */
598 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
600 /* TODO Should retry with a different stream id */
606 bt_object_set_parent(stream_class
, trace
);
607 g_ptr_array_add(trace
->stream_classes
, stream_class
);
610 * At this point we know that the function will be successful.
611 * Therefore we can replace the trace and stream class field
612 * types with what's in their validation output structure and
613 * mark them as valid. We can also replace the field types of
614 * all the event classes of the stream class and mark them as
617 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
618 &trace_sc_validation_output
, trace_sc_validation_flags
);
620 stream_class
->valid
= 1;
623 * Put what was not moved in bt_ctf_validation_replace_types().
625 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
627 for (i
= 0; i
< event_class_count
; i
++) {
628 struct bt_ctf_event_class
*event_class
=
629 bt_ctf_stream_class_get_event_class(stream_class
, i
);
631 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
632 &ec_validation_outputs
[i
], ec_validation_flags
);
633 event_class
->valid
= 1;
637 * Put what was not moved in
638 * bt_ctf_validation_replace_types().
640 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
644 * All field type byte orders set as "native" byte ordering can now be
645 * safely set to trace's own endianness, including the stream class'.
647 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
649 bt_ctf_stream_class_set_byte_order(stream_class
, trace
->byte_order
);
651 /* Add stream class's clock if it exists */
652 if (clock_to_add_to_trace
) {
654 bt_ctf_trace_add_clock(trace
, clock_to_add_to_trace
);
655 assert(add_clock_ret
== 0);
659 * Freeze the trace and the stream class.
661 bt_ctf_stream_class_freeze(stream_class
);
662 bt_ctf_trace_freeze(trace
);
666 bt_object_set_parent(stream_class
, NULL
);
668 if (ec_validation_outputs
) {
669 for (i
= 0; i
< event_class_count
; i
++) {
670 bt_ctf_validation_output_put_types(
671 &ec_validation_outputs
[i
]);
676 g_free(ec_validation_outputs
);
677 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
678 BT_PUT(clock_to_add_to_trace
);
679 assert(!packet_header_type
);
680 assert(!packet_context_type
);
681 assert(!event_header_type
);
682 assert(!stream_event_ctx_type
);
687 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
696 ret
= trace
->stream_classes
->len
;
701 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
702 struct bt_ctf_trace
*trace
, int index
)
704 struct bt_ctf_stream_class
*stream_class
= NULL
;
706 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
710 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
711 bt_get(stream_class
);
716 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
717 struct bt_ctf_trace
*trace
, uint32_t id
)
720 struct bt_ctf_stream_class
*stream_class
= NULL
;
726 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
727 struct bt_ctf_stream_class
*stream_class_candidate
;
729 stream_class_candidate
=
730 g_ptr_array_index(trace
->stream_classes
, i
);
732 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
734 stream_class
= stream_class_candidate
;
735 bt_get(stream_class
);
744 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
745 struct bt_ctf_trace
*trace
, const char *name
)
748 struct bt_ctf_clock
*clock
= NULL
;
750 if (!trace
|| !name
) {
754 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
755 struct bt_ctf_clock
*cur_clk
=
756 g_ptr_array_index(trace
->clocks
, i
);
757 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
763 if (!strcmp(cur_clk_name
, name
)) {
775 const char *get_byte_order_string(int byte_order
)
779 switch (byte_order
) {
795 int append_trace_metadata(struct bt_ctf_trace
*trace
,
796 struct metadata_context
*context
)
798 unsigned char *uuid
= trace
->uuid
;
801 g_string_append(context
->string
, "trace {\n");
803 g_string_append(context
->string
, "\tmajor = 1;\n");
804 g_string_append(context
->string
, "\tminor = 8;\n");
806 g_string_append_printf(context
->string
,
807 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
808 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
809 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
810 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
811 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
812 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
813 get_byte_order_string(trace
->byte_order
));
815 g_string_append(context
->string
, "\tpacket.header := ");
816 context
->current_indentation_level
++;
817 g_string_assign(context
->field_name
, "");
818 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
823 context
->current_indentation_level
--;
825 g_string_append(context
->string
, ";\n};\n\n");
831 void append_env_metadata(struct bt_ctf_trace
*trace
,
832 struct metadata_context
*context
)
837 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
843 g_string_append(context
->string
, "env {\n");
845 for (i
= 0; i
< env_size
; i
++) {
846 struct bt_value
*env_field_value_obj
= NULL
;
847 const char *entry_name
;
849 entry_name
= bt_ctf_attributes_get_field_name(
850 trace
->environment
, i
);
851 env_field_value_obj
= bt_ctf_attributes_get_field_value(
852 trace
->environment
, i
);
854 if (!entry_name
|| !env_field_value_obj
) {
858 switch (bt_value_get_type(env_field_value_obj
)) {
859 case BT_VALUE_TYPE_INTEGER
:
864 ret
= bt_value_integer_get(env_field_value_obj
,
871 g_string_append_printf(context
->string
,
872 "\t%s = %" PRId64
";\n", entry_name
,
876 case BT_VALUE_TYPE_STRING
:
879 const char *str_value
;
880 char *escaped_str
= NULL
;
882 ret
= bt_value_string_get(env_field_value_obj
,
889 escaped_str
= g_strescape(str_value
, NULL
);
895 g_string_append_printf(context
->string
,
896 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
906 BT_PUT(env_field_value_obj
);
909 g_string_append(context
->string
, "};\n\n");
912 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
914 char *metadata
= NULL
;
915 struct metadata_context
*context
= NULL
;
923 context
= g_new0(struct metadata_context
, 1);
928 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
929 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
930 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
931 if (append_trace_metadata(trace
, context
)) {
934 append_env_metadata(trace
, context
);
935 g_ptr_array_foreach(trace
->clocks
,
936 (GFunc
)bt_ctf_clock_serialize
, context
);
938 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
939 err
= bt_ctf_stream_class_serialize(
940 trace
->stream_classes
->pdata
[i
], context
);
946 metadata
= context
->string
->str
;
948 g_string_free(context
->string
, err
? TRUE
: FALSE
);
949 g_string_free(context
->field_name
, TRUE
);
955 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
957 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
963 switch (trace
->byte_order
) {
965 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
968 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
977 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
978 enum bt_ctf_byte_order byte_order
)
981 int internal_byte_order
;
983 if (!trace
|| trace
->frozen
) {
988 switch (byte_order
) {
989 case BT_CTF_BYTE_ORDER_NATIVE
:
991 * This doesn't make sense since the CTF specification defines
992 * the "native" byte order as "the byte order described in the
993 * trace description". However, this behavior had been
994 * implemented as part of v1.2 and is kept to maintain
997 * This may be changed on a major version bump only.
999 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
1000 LITTLE_ENDIAN
: BIG_ENDIAN
;
1002 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1003 internal_byte_order
= LITTLE_ENDIAN
;
1005 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1006 case BT_CTF_BYTE_ORDER_NETWORK
:
1007 internal_byte_order
= BIG_ENDIAN
;
1014 trace
->byte_order
= internal_byte_order
;
1019 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1020 struct bt_ctf_trace
*trace
)
1022 struct bt_ctf_field_type
*field_type
= NULL
;
1028 bt_get(trace
->packet_header_type
);
1029 field_type
= trace
->packet_header_type
;
1034 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1035 struct bt_ctf_field_type
*packet_header_type
)
1039 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
1044 /* packet_header_type must be a structure */
1045 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
1046 BT_CTF_TYPE_ID_STRUCT
) {
1051 bt_get(packet_header_type
);
1052 bt_put(trace
->packet_header_type
);
1053 trace
->packet_header_type
= packet_header_type
;
1059 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1061 unsigned int alignment
, size
;
1062 struct bt_ctf_field_type
*field_type
= NULL
;
1064 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1068 alignment
= field_type_aliases_alignments
[alias
];
1069 size
= field_type_aliases_sizes
[alias
];
1070 field_type
= bt_ctf_field_type_integer_create(size
);
1071 bt_ctf_field_type_set_alignment(field_type
, alignment
);
1077 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1081 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1082 bt_ctf_attributes_freeze(trace
->environment
);
1084 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1085 struct bt_ctf_clock
*clock
=
1086 g_ptr_array_index(trace
->clocks
, i
);
1088 bt_ctf_clock_freeze(clock
);
1095 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
1098 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
1099 struct bt_ctf_field_type
*_uint32_t
=
1100 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1101 struct bt_ctf_field_type
*_uint8_t
=
1102 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1103 struct bt_ctf_field_type
*trace_packet_header_type
=
1104 bt_ctf_field_type_structure_create();
1105 struct bt_ctf_field_type
*uuid_array_type
=
1106 bt_ctf_field_type_array_create(_uint8_t
, 16);
1108 if (!trace_packet_header_type
|| !uuid_array_type
) {
1113 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1114 _uint32_t
, "magic");
1119 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1120 uuid_array_type
, "uuid");
1125 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1126 _uint32_t
, "stream_id");
1131 ret
= bt_ctf_trace_set_packet_header_type(trace
,
1132 trace_packet_header_type
);
1137 bt_put(uuid_array_type
);
1142 bt_put(trace_packet_header_type
);