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/visitor-internal.h>
41 #include <babeltrace/ctf-ir/utils.h>
42 #include <babeltrace/plugin/notification/schema.h>
43 #include <babeltrace/compiler.h>
44 #include <babeltrace/values.h>
45 #include <babeltrace/ref.h>
46 #include <babeltrace/endian.h>
48 #define DEFAULT_IDENTIFIER_SIZE 128
49 #define DEFAULT_METADATA_STRING_SIZE 4096
51 struct listener_wrapper
{
52 bt_ctf_listener_cb listener
;
57 void bt_ctf_trace_destroy(struct bt_object
*obj
);
59 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
61 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
64 const unsigned int field_type_aliases_alignments
[] = {
65 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
66 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
67 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
68 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
72 const unsigned int field_type_aliases_sizes
[] = {
73 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
74 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
75 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
76 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
77 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
78 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
81 struct bt_ctf_trace
*bt_ctf_trace_create(void)
83 struct bt_ctf_trace
*trace
= NULL
;
85 trace
= g_new0(struct bt_ctf_trace
, 1);
90 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
91 bt_object_init(trace
, bt_ctf_trace_destroy
);
92 trace
->clocks
= g_ptr_array_new_with_free_func(
93 (GDestroyNotify
) bt_put
);
94 trace
->streams
= g_ptr_array_new_with_free_func(
95 (GDestroyNotify
) bt_object_release
);
96 trace
->stream_classes
= g_ptr_array_new_with_free_func(
97 (GDestroyNotify
) bt_object_release
);
98 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
102 /* Generate a trace UUID */
103 uuid_generate(trace
->uuid
);
104 if (init_trace_packet_header(trace
)) {
108 /* Create the environment array object */
109 trace
->environment
= bt_ctf_attributes_create();
110 if (!trace
->environment
) {
114 trace
->listeners
= g_ptr_array_new_with_free_func(
115 (GDestroyNotify
) g_free
);
116 if (!trace
->listeners
) {
127 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
129 const char *name
= NULL
;
131 if (!trace
|| !trace
->name
) {
135 name
= trace
->name
->str
;
140 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
144 if (!trace
|| !name
|| trace
->frozen
) {
149 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
159 void bt_ctf_trace_destroy(struct bt_object
*obj
)
161 struct bt_ctf_trace
*trace
;
163 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
164 if (trace
->environment
) {
165 bt_ctf_attributes_destroy(trace
->environment
);
169 g_string_free(trace
->name
, TRUE
);
173 g_ptr_array_free(trace
->clocks
, TRUE
);
176 if (trace
->streams
) {
177 g_ptr_array_free(trace
->streams
, TRUE
);
180 if (trace
->stream_classes
) {
181 g_ptr_array_free(trace
->stream_classes
, TRUE
);
184 if (trace
->listeners
) {
185 g_ptr_array_free(trace
->listeners
, TRUE
);
188 bt_put(trace
->packet_header_type
);
192 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
193 const char *name
, struct bt_value
*value
)
197 if (!trace
|| !name
|| !value
||
198 bt_ctf_validate_identifier(name
) ||
199 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
204 if (strchr(name
, ' ')) {
211 * New environment fields may be added to a frozen trace,
212 * but existing fields may not be changed.
214 * The object passed is frozen like all other attributes.
216 struct bt_value
*attribute
=
217 bt_ctf_attributes_get_field_value_by_name(
218 trace
->environment
, name
);
226 bt_value_freeze(value
);
229 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
236 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
237 const char *name
, const char *value
)
240 struct bt_value
*env_value_string_obj
= NULL
;
242 if (!trace
|| !name
|| !value
) {
249 * New environment fields may be added to a frozen trace,
250 * but existing fields may not be changed.
252 struct bt_value
*attribute
=
253 bt_ctf_attributes_get_field_value_by_name(
254 trace
->environment
, name
);
263 env_value_string_obj
= bt_value_string_create_init(value
);
265 if (!env_value_string_obj
) {
271 bt_value_freeze(env_value_string_obj
);
273 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
274 env_value_string_obj
);
277 BT_PUT(env_value_string_obj
);
281 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
282 const char *name
, int64_t value
)
285 struct bt_value
*env_value_integer_obj
= NULL
;
287 if (!trace
|| !name
) {
294 * New environment fields may be added to a frozen trace,
295 * but existing fields may not be changed.
297 struct bt_value
*attribute
=
298 bt_ctf_attributes_get_field_value_by_name(
299 trace
->environment
, name
);
308 env_value_integer_obj
= bt_value_integer_create_init(value
);
309 if (!env_value_integer_obj
) {
314 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
315 env_value_integer_obj
);
317 bt_value_freeze(env_value_integer_obj
);
320 BT_PUT(env_value_integer_obj
);
324 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
333 ret
= bt_ctf_attributes_get_count(trace
->environment
);
340 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
343 const char *ret
= NULL
;
349 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
355 struct bt_value
*bt_ctf_trace_get_environment_field_value(
356 struct bt_ctf_trace
*trace
, int index
)
358 struct bt_value
*ret
= NULL
;
364 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
370 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
371 struct bt_ctf_trace
*trace
, const char *name
)
373 struct bt_value
*ret
= NULL
;
375 if (!trace
|| !name
) {
379 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
386 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
387 struct bt_ctf_clock
*clock
)
390 struct search_query query
= { .value
= clock
, .found
= 0 };
392 if (!trace
|| !bt_ctf_clock_is_valid(clock
)) {
397 /* Check for duplicate clocks */
398 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
405 g_ptr_array_add(trace
->clocks
, clock
);
407 if (!trace
->is_created_by_writer
) {
409 * Non-writer mode trace: disable clock value functions
410 * because clock values are per-stream in that
413 clock
->has_value
= 0;
417 bt_ctf_clock_freeze(clock
);
423 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
431 ret
= trace
->clocks
->len
;
436 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
439 struct bt_ctf_clock
*clock
= NULL
;
441 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
445 clock
= g_ptr_array_index(trace
->clocks
, index
);
451 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
452 struct bt_ctf_stream_class
*stream_class
)
456 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
457 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
458 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
459 BT_CTF_VALIDATION_FLAG_TRACE
|
460 BT_CTF_VALIDATION_FLAG_STREAM
;
461 const enum bt_ctf_validation_flag ec_validation_flags
=
462 BT_CTF_VALIDATION_FLAG_EVENT
;
463 struct bt_ctf_field_type
*packet_header_type
= NULL
;
464 struct bt_ctf_field_type
*packet_context_type
= NULL
;
465 struct bt_ctf_field_type
*event_header_type
= NULL
;
466 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
467 int event_class_count
;
468 struct bt_ctf_clock
*clock_to_add_to_trace
= NULL
;
469 struct bt_ctf_trace
*current_parent_trace
= NULL
;
471 if (!trace
|| !stream_class
) {
476 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
477 if (current_parent_trace
) {
478 /* Stream class is already associated to a trace, abort. */
484 bt_ctf_stream_class_get_event_class_count(stream_class
);
485 assert(event_class_count
>= 0);
487 /* Check for duplicate stream classes */
488 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
489 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
490 /* Stream class already registered to the trace */
497 * If the stream class has a clock, register this clock to this
498 * trace if not already done.
500 if (stream_class
->clock
) {
501 const char *clock_name
=
502 bt_ctf_clock_get_name(stream_class
->clock
);
503 struct bt_ctf_clock
*trace_clock
;
506 trace_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
509 if (trace_clock
!= stream_class
->clock
) {
511 * Error: two different clocks in the
512 * trace would share the same name.
518 clock_to_add_to_trace
= bt_get(stream_class
->clock
);
523 * We're about to freeze both the trace and the stream class.
524 * Also, each event class contained in this stream class are
527 * This trace, this stream class, and all its event classes
528 * should be valid at this point.
530 * Validate trace and stream class first, then each event
531 * class of this stream class can be validated individually.
534 bt_ctf_trace_get_packet_header_type(trace
);
535 packet_context_type
=
536 bt_ctf_stream_class_get_packet_context_type(stream_class
);
538 bt_ctf_stream_class_get_event_header_type(stream_class
);
539 stream_event_ctx_type
=
540 bt_ctf_stream_class_get_event_context_type(stream_class
);
541 ret
= bt_ctf_validate_class_types(trace
->environment
,
542 packet_header_type
, packet_context_type
, event_header_type
,
543 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
544 stream_class
->valid
, 1, &trace_sc_validation_output
,
545 trace_sc_validation_flags
);
546 BT_PUT(packet_header_type
);
547 BT_PUT(packet_context_type
);
548 BT_PUT(event_header_type
);
549 BT_PUT(stream_event_ctx_type
);
553 * This means something went wrong during the validation
554 * process, not that the objects are invalid.
559 if ((trace_sc_validation_output
.valid_flags
&
560 trace_sc_validation_flags
) !=
561 trace_sc_validation_flags
) {
562 /* Invalid trace/stream class */
567 if (event_class_count
> 0) {
568 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
570 if (!ec_validation_outputs
) {
576 /* Validate each event class individually */
577 for (i
= 0; i
< event_class_count
; i
++) {
578 struct bt_ctf_event_class
*event_class
=
579 bt_ctf_stream_class_get_event_class(stream_class
, i
);
580 struct bt_ctf_field_type
*event_context_type
= NULL
;
581 struct bt_ctf_field_type
*event_payload_type
= NULL
;
584 bt_ctf_event_class_get_context_type(event_class
);
586 bt_ctf_event_class_get_payload_type(event_class
);
589 * It is important to use the field types returned by
590 * the previous trace and stream class validation here
591 * because copies could have been made.
593 ret
= bt_ctf_validate_class_types(trace
->environment
,
594 trace_sc_validation_output
.packet_header_type
,
595 trace_sc_validation_output
.packet_context_type
,
596 trace_sc_validation_output
.event_header_type
,
597 trace_sc_validation_output
.stream_event_ctx_type
,
598 event_context_type
, event_payload_type
,
599 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
600 ec_validation_flags
);
601 BT_PUT(event_context_type
);
602 BT_PUT(event_payload_type
);
609 if ((ec_validation_outputs
[i
].valid_flags
&
610 ec_validation_flags
) != ec_validation_flags
) {
611 /* Invalid event class */
617 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
619 stream_id
= trace
->next_stream_id
++;
621 /* Try to assign a new stream id */
622 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
623 if (stream_id
== bt_ctf_stream_class_get_id(
624 trace
->stream_classes
->pdata
[i
])) {
625 /* Duplicate stream id found */
631 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
633 /* TODO Should retry with a different stream id */
639 bt_object_set_parent(stream_class
, trace
);
640 g_ptr_array_add(trace
->stream_classes
, stream_class
);
643 * At this point we know that the function will be successful.
644 * Therefore we can replace the trace and stream class field
645 * types with what's in their validation output structure and
646 * mark them as valid. We can also replace the field types of
647 * all the event classes of the stream class and mark them as
650 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
651 &trace_sc_validation_output
, trace_sc_validation_flags
);
653 stream_class
->valid
= 1;
656 * Put what was not moved in bt_ctf_validation_replace_types().
658 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
660 for (i
= 0; i
< event_class_count
; i
++) {
661 struct bt_ctf_event_class
*event_class
=
662 bt_ctf_stream_class_get_event_class(stream_class
, i
);
664 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
665 &ec_validation_outputs
[i
], ec_validation_flags
);
666 event_class
->valid
= 1;
670 * Put what was not moved in
671 * bt_ctf_validation_replace_types().
673 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
677 * All field type byte orders set as "native" byte ordering can now be
678 * safely set to trace's own endianness, including the stream class'.
680 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
682 bt_ctf_stream_class_set_byte_order(stream_class
, trace
->byte_order
);
684 /* Add stream class's clock if it exists */
685 if (clock_to_add_to_trace
) {
687 bt_ctf_trace_add_clock(trace
, clock_to_add_to_trace
);
688 assert(add_clock_ret
== 0);
692 * Freeze the trace and the stream class.
694 bt_ctf_stream_class_freeze(stream_class
);
695 bt_ctf_trace_freeze(trace
);
697 /* Notifiy listeners of the trace's schema modification. */
698 bt_ctf_stream_class_visit(stream_class
,
699 bt_ctf_trace_object_modification
, trace
);
702 bt_object_set_parent(stream_class
, NULL
);
704 if (ec_validation_outputs
) {
705 for (i
= 0; i
< event_class_count
; i
++) {
706 bt_ctf_validation_output_put_types(
707 &ec_validation_outputs
[i
]);
712 g_free(ec_validation_outputs
);
713 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
714 BT_PUT(clock_to_add_to_trace
);
715 bt_put(current_parent_trace
);
716 assert(!packet_header_type
);
717 assert(!packet_context_type
);
718 assert(!event_header_type
);
719 assert(!stream_event_ctx_type
);
724 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
733 ret
= trace
->stream_classes
->len
;
738 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
739 struct bt_ctf_trace
*trace
, int index
)
741 struct bt_ctf_stream_class
*stream_class
= NULL
;
743 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
747 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
748 bt_get(stream_class
);
753 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
754 struct bt_ctf_trace
*trace
, uint32_t id
)
757 struct bt_ctf_stream_class
*stream_class
= NULL
;
763 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
764 struct bt_ctf_stream_class
*stream_class_candidate
;
766 stream_class_candidate
=
767 g_ptr_array_index(trace
->stream_classes
, i
);
769 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
771 stream_class
= stream_class_candidate
;
772 bt_get(stream_class
);
781 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
782 struct bt_ctf_trace
*trace
, const char *name
)
785 struct bt_ctf_clock
*clock
= NULL
;
787 if (!trace
|| !name
) {
791 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
792 struct bt_ctf_clock
*cur_clk
=
793 g_ptr_array_index(trace
->clocks
, i
);
794 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
800 if (!strcmp(cur_clk_name
, name
)) {
812 const char *get_byte_order_string(int byte_order
)
816 switch (byte_order
) {
832 int append_trace_metadata(struct bt_ctf_trace
*trace
,
833 struct metadata_context
*context
)
835 unsigned char *uuid
= trace
->uuid
;
838 g_string_append(context
->string
, "trace {\n");
840 g_string_append(context
->string
, "\tmajor = 1;\n");
841 g_string_append(context
->string
, "\tminor = 8;\n");
843 g_string_append_printf(context
->string
,
844 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
845 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
846 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
847 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
848 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
849 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
850 get_byte_order_string(trace
->byte_order
));
852 g_string_append(context
->string
, "\tpacket.header := ");
853 context
->current_indentation_level
++;
854 g_string_assign(context
->field_name
, "");
855 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
860 context
->current_indentation_level
--;
862 g_string_append(context
->string
, ";\n};\n\n");
868 void append_env_metadata(struct bt_ctf_trace
*trace
,
869 struct metadata_context
*context
)
874 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
880 g_string_append(context
->string
, "env {\n");
882 for (i
= 0; i
< env_size
; i
++) {
883 struct bt_value
*env_field_value_obj
= NULL
;
884 const char *entry_name
;
886 entry_name
= bt_ctf_attributes_get_field_name(
887 trace
->environment
, i
);
888 env_field_value_obj
= bt_ctf_attributes_get_field_value(
889 trace
->environment
, i
);
891 if (!entry_name
|| !env_field_value_obj
) {
895 switch (bt_value_get_type(env_field_value_obj
)) {
896 case BT_VALUE_TYPE_INTEGER
:
901 ret
= bt_value_integer_get(env_field_value_obj
,
908 g_string_append_printf(context
->string
,
909 "\t%s = %" PRId64
";\n", entry_name
,
913 case BT_VALUE_TYPE_STRING
:
916 const char *str_value
;
917 char *escaped_str
= NULL
;
919 ret
= bt_value_string_get(env_field_value_obj
,
926 escaped_str
= g_strescape(str_value
, NULL
);
932 g_string_append_printf(context
->string
,
933 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
943 BT_PUT(env_field_value_obj
);
946 g_string_append(context
->string
, "};\n\n");
949 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
951 char *metadata
= NULL
;
952 struct metadata_context
*context
= NULL
;
960 context
= g_new0(struct metadata_context
, 1);
965 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
966 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
967 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
968 if (append_trace_metadata(trace
, context
)) {
971 append_env_metadata(trace
, context
);
972 g_ptr_array_foreach(trace
->clocks
,
973 (GFunc
)bt_ctf_clock_serialize
, context
);
975 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
976 err
= bt_ctf_stream_class_serialize(
977 trace
->stream_classes
->pdata
[i
], context
);
983 metadata
= context
->string
->str
;
985 g_string_free(context
->string
, err
? TRUE
: FALSE
);
986 g_string_free(context
->field_name
, TRUE
);
992 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
994 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1000 switch (trace
->byte_order
) {
1002 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1005 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1014 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
1015 enum bt_ctf_byte_order byte_order
)
1018 int internal_byte_order
;
1020 if (!trace
|| trace
->frozen
) {
1025 switch (byte_order
) {
1026 case BT_CTF_BYTE_ORDER_NATIVE
:
1028 * This doesn't make sense since the CTF specification defines
1029 * the "native" byte order as "the byte order described in the
1030 * trace description". However, this behavior had been
1031 * implemented as part of v1.2 and is kept to maintain
1034 * This may be changed on a major version bump only.
1036 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
1037 LITTLE_ENDIAN
: BIG_ENDIAN
;
1039 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1040 internal_byte_order
= LITTLE_ENDIAN
;
1042 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1043 case BT_CTF_BYTE_ORDER_NETWORK
:
1044 internal_byte_order
= BIG_ENDIAN
;
1051 trace
->byte_order
= internal_byte_order
;
1056 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1057 struct bt_ctf_trace
*trace
)
1059 struct bt_ctf_field_type
*field_type
= NULL
;
1065 bt_get(trace
->packet_header_type
);
1066 field_type
= trace
->packet_header_type
;
1071 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1072 struct bt_ctf_field_type
*packet_header_type
)
1076 if (!trace
|| trace
->frozen
) {
1081 /* packet_header_type must be a structure. */
1082 if (packet_header_type
&&
1083 bt_ctf_field_type_get_type_id(packet_header_type
) !=
1084 BT_CTF_TYPE_ID_STRUCT
) {
1089 bt_put(trace
->packet_header_type
);
1090 trace
->packet_header_type
= bt_get(packet_header_type
);
1096 int get_stream_class_count(void *element
)
1098 return bt_ctf_trace_get_stream_class_count(
1099 (struct bt_ctf_trace
*) element
);
1103 void *get_stream_class(void *element
, int i
)
1105 return bt_ctf_trace_get_stream_class(
1106 (struct bt_ctf_trace
*) element
, i
);
1110 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1112 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1115 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1116 bt_ctf_visitor visitor
, void *data
)
1119 struct bt_ctf_object obj
=
1120 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1122 if (!trace
|| !visitor
) {
1127 ret
= visitor_helper(&obj
, get_stream_class_count
,
1128 get_stream_class
, visit_stream_class
, visitor
, data
);
1134 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1136 struct listener_wrapper
*listener_wrapper
= data
;
1138 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1142 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1143 bt_ctf_listener_cb listener
, void *listener_data
)
1146 struct listener_wrapper
*listener_wrapper
=
1147 g_new0(struct listener_wrapper
, 1);
1149 if (!trace
|| !listener
|| !listener_wrapper
) {
1154 listener_wrapper
->listener
= listener
;
1155 listener_wrapper
->data
= listener_data
;
1157 /* Visit the current schema. */
1158 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
1164 * Add listener to the array of callbacks which will be invoked on
1167 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
1170 g_free(listener_wrapper
);
1175 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
1179 struct bt_ctf_trace
*trace
= trace_ptr
;
1184 if (trace
->listeners
->len
== 0) {
1188 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
1189 struct listener_wrapper
*listener
=
1190 g_ptr_array_index(trace
->listeners
, i
);
1192 listener
->listener(object
, listener
->data
);
1199 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1202 unsigned int alignment
, size
;
1203 struct bt_ctf_field_type
*field_type
= NULL
;
1205 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1209 alignment
= field_type_aliases_alignments
[alias
];
1210 size
= field_type_aliases_sizes
[alias
];
1211 field_type
= bt_ctf_field_type_integer_create(size
);
1212 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
1221 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1225 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1226 bt_ctf_attributes_freeze(trace
->environment
);
1228 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1229 struct bt_ctf_clock
*clock
=
1230 g_ptr_array_index(trace
->clocks
, i
);
1232 bt_ctf_clock_freeze(clock
);
1239 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
1242 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
1243 struct bt_ctf_field_type
*_uint32_t
=
1244 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1245 struct bt_ctf_field_type
*_uint8_t
=
1246 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1247 struct bt_ctf_field_type
*trace_packet_header_type
=
1248 bt_ctf_field_type_structure_create();
1249 struct bt_ctf_field_type
*uuid_array_type
=
1250 bt_ctf_field_type_array_create(_uint8_t
, 16);
1252 if (!trace_packet_header_type
|| !uuid_array_type
) {
1257 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1258 _uint32_t
, "magic");
1263 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1264 uuid_array_type
, "uuid");
1269 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1270 _uint32_t
, "stream_id");
1275 ret
= bt_ctf_trace_set_packet_header_type(trace
,
1276 trace_packet_header_type
);
1281 bt_put(uuid_array_type
);
1286 bt_put(trace_packet_header_type
);