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-class-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-writer/clock-internal.h>
38 #include <babeltrace/ctf-ir/field-types-internal.h>
39 #include <babeltrace/ctf-ir/attributes-internal.h>
40 #include <babeltrace/ctf-ir/validation-internal.h>
41 #include <babeltrace/ctf-ir/visitor-internal.h>
42 #include <babeltrace/ctf-ir/utils.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/values.h>
45 #include <babeltrace/ref.h>
46 #include <babeltrace/types.h>
47 #include <babeltrace/endian-internal.h>
52 #define DEFAULT_IDENTIFIER_SIZE 128
53 #define DEFAULT_METADATA_STRING_SIZE 4096
55 struct listener_wrapper
{
56 bt_ctf_listener_cb listener
;
61 void bt_ctf_trace_destroy(struct bt_object
*obj
);
63 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
66 const unsigned int field_type_aliases_alignments
[] = {
67 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
68 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
69 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
70 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
74 const unsigned int field_type_aliases_sizes
[] = {
75 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
76 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
77 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
78 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
79 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
80 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
83 struct bt_ctf_trace
*bt_ctf_trace_create(void)
85 struct bt_ctf_trace
*trace
= NULL
;
86 struct bt_ctf_field_type
*packet_header_type
= NULL
;
88 trace
= g_new0(struct bt_ctf_trace
, 1);
93 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_NATIVE
;
94 bt_object_init(trace
, bt_ctf_trace_destroy
);
95 trace
->clocks
= g_ptr_array_new_with_free_func(
96 (GDestroyNotify
) bt_put
);
97 trace
->streams
= g_ptr_array_new_with_free_func(
98 (GDestroyNotify
) bt_object_release
);
99 trace
->stream_classes
= g_ptr_array_new_with_free_func(
100 (GDestroyNotify
) bt_object_release
);
101 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
105 packet_header_type
= bt_ctf_field_type_structure_create();
106 if (!packet_header_type
) {
110 BT_MOVE(trace
->packet_header_type
, packet_header_type
);
112 /* Create the environment array object */
113 trace
->environment
= bt_ctf_attributes_create();
114 if (!trace
->environment
) {
118 trace
->listeners
= g_ptr_array_new_with_free_func(
119 (GDestroyNotify
) g_free
);
120 if (!trace
->listeners
) {
128 bt_put(packet_header_type
);
132 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
134 const char *name
= NULL
;
136 if (!trace
|| !trace
->name
) {
140 name
= trace
->name
->str
;
145 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
149 if (!trace
|| !name
|| trace
->frozen
) {
154 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
164 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
166 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
169 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
, const unsigned char *uuid
)
173 if (!trace
|| !uuid
|| trace
->frozen
) {
178 memcpy(trace
->uuid
, uuid
, sizeof(uuid_t
));
179 trace
->uuid_set
= BT_TRUE
;
185 void bt_ctf_trace_destroy(struct bt_object
*obj
)
187 struct bt_ctf_trace
*trace
;
189 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
190 if (trace
->environment
) {
191 bt_ctf_attributes_destroy(trace
->environment
);
195 g_string_free(trace
->name
, TRUE
);
199 g_ptr_array_free(trace
->clocks
, TRUE
);
202 if (trace
->streams
) {
203 g_ptr_array_free(trace
->streams
, TRUE
);
206 if (trace
->stream_classes
) {
207 g_ptr_array_free(trace
->stream_classes
, TRUE
);
210 if (trace
->listeners
) {
211 g_ptr_array_free(trace
->listeners
, TRUE
);
214 bt_put(trace
->packet_header_type
);
218 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
219 const char *name
, struct bt_value
*value
)
223 if (!trace
|| !name
|| !value
||
224 bt_ctf_validate_identifier(name
) ||
225 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
230 if (strchr(name
, ' ')) {
237 * New environment fields may be added to a frozen trace,
238 * but existing fields may not be changed.
240 * The object passed is frozen like all other attributes.
242 struct bt_value
*attribute
=
243 bt_ctf_attributes_get_field_value_by_name(
244 trace
->environment
, name
);
252 bt_value_freeze(value
);
255 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
262 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
263 const char *name
, const char *value
)
266 struct bt_value
*env_value_string_obj
= NULL
;
268 if (!trace
|| !name
|| !value
) {
275 * New environment fields may be added to a frozen trace,
276 * but existing fields may not be changed.
278 struct bt_value
*attribute
=
279 bt_ctf_attributes_get_field_value_by_name(
280 trace
->environment
, name
);
289 env_value_string_obj
= bt_value_string_create_init(value
);
291 if (!env_value_string_obj
) {
297 bt_value_freeze(env_value_string_obj
);
299 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
300 env_value_string_obj
);
303 BT_PUT(env_value_string_obj
);
307 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
308 const char *name
, int64_t value
)
311 struct bt_value
*env_value_integer_obj
= NULL
;
313 if (!trace
|| !name
) {
320 * New environment fields may be added to a frozen trace,
321 * but existing fields may not be changed.
323 struct bt_value
*attribute
=
324 bt_ctf_attributes_get_field_value_by_name(
325 trace
->environment
, name
);
334 env_value_integer_obj
= bt_value_integer_create_init(value
);
335 if (!env_value_integer_obj
) {
340 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
341 env_value_integer_obj
);
343 bt_value_freeze(env_value_integer_obj
);
346 BT_PUT(env_value_integer_obj
);
350 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
359 ret
= bt_ctf_attributes_get_count(trace
->environment
);
366 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
369 const char *ret
= NULL
;
375 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
381 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
382 struct bt_ctf_trace
*trace
, uint64_t index
)
384 struct bt_value
*ret
= NULL
;
390 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
396 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
397 struct bt_ctf_trace
*trace
, const char *name
)
399 struct bt_value
*ret
= NULL
;
401 if (!trace
|| !name
) {
405 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
412 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
413 struct bt_ctf_clock_class
*clock_class
)
417 if (!trace
|| trace
->is_static
||
418 !bt_ctf_clock_class_is_valid(clock_class
)) {
423 /* Check for duplicate clock classes */
424 if (bt_ctf_trace_has_clock_class(trace
, clock_class
)) {
430 g_ptr_array_add(trace
->clocks
, clock_class
);
433 bt_ctf_clock_class_freeze(clock_class
);
439 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
441 int64_t ret
= (int64_t) -1;
447 ret
= trace
->clocks
->len
;
452 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
453 struct bt_ctf_trace
*trace
, uint64_t index
)
455 struct bt_ctf_clock_class
*clock_class
= NULL
;
457 if (!trace
|| index
>= trace
->clocks
->len
) {
461 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
467 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
468 struct bt_ctf_stream_class
*stream_class
)
473 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
474 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
475 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
476 BT_CTF_VALIDATION_FLAG_TRACE
|
477 BT_CTF_VALIDATION_FLAG_STREAM
;
478 const enum bt_ctf_validation_flag ec_validation_flags
=
479 BT_CTF_VALIDATION_FLAG_EVENT
;
480 struct bt_ctf_field_type
*packet_header_type
= NULL
;
481 struct bt_ctf_field_type
*packet_context_type
= NULL
;
482 struct bt_ctf_field_type
*event_header_type
= NULL
;
483 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
484 int64_t event_class_count
;
485 struct bt_ctf_trace
*current_parent_trace
= NULL
;
487 if (!trace
|| !stream_class
|| trace
->is_static
) {
493 * At the end of this function we freeze the trace, so its
494 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
496 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
501 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
502 if (current_parent_trace
) {
503 /* Stream class is already associated to a trace, abort. */
509 bt_ctf_stream_class_get_event_class_count(stream_class
);
510 assert(event_class_count
>= 0);
512 /* Check for duplicate stream classes */
513 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
514 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
515 /* Stream class already registered to the trace */
521 if (stream_class
->clock
) {
522 struct bt_ctf_clock_class
*stream_clock_class
=
523 stream_class
->clock
->clock_class
;
525 if (trace
->is_created_by_writer
) {
527 * Make sure this clock was also added to the
528 * trace (potentially through its CTF writer
533 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
534 if (trace
->clocks
->pdata
[i
] ==
535 stream_clock_class
) {
541 if (i
== trace
->clocks
->len
) {
548 * This trace was NOT created by a CTF writer,
549 * thus do not allow the stream class to add to
550 * have a clock at all. Those are two
551 * independent APIs (non-writer and writer
552 * APIs), and isolating them simplifies things.
560 * We're about to freeze both the trace and the stream class.
561 * Also, each event class contained in this stream class are
564 * This trace, this stream class, and all its event classes
565 * should be valid at this point.
567 * Validate trace and stream class first, then each event
568 * class of this stream class can be validated individually.
571 bt_ctf_trace_get_packet_header_type(trace
);
572 packet_context_type
=
573 bt_ctf_stream_class_get_packet_context_type(stream_class
);
575 bt_ctf_stream_class_get_event_header_type(stream_class
);
576 stream_event_ctx_type
=
577 bt_ctf_stream_class_get_event_context_type(stream_class
);
578 ret
= bt_ctf_validate_class_types(trace
->environment
,
579 packet_header_type
, packet_context_type
, event_header_type
,
580 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
581 stream_class
->valid
, 1, &trace_sc_validation_output
,
582 trace_sc_validation_flags
);
583 BT_PUT(packet_header_type
);
584 BT_PUT(packet_context_type
);
585 BT_PUT(event_header_type
);
586 BT_PUT(stream_event_ctx_type
);
590 * This means something went wrong during the validation
591 * process, not that the objects are invalid.
596 if ((trace_sc_validation_output
.valid_flags
&
597 trace_sc_validation_flags
) !=
598 trace_sc_validation_flags
) {
599 /* Invalid trace/stream class */
604 if (event_class_count
> 0) {
605 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
607 if (!ec_validation_outputs
) {
613 /* Validate each event class individually */
614 for (i
= 0; i
< event_class_count
; i
++) {
615 struct bt_ctf_event_class
*event_class
=
616 bt_ctf_stream_class_get_event_class_by_index(
618 struct bt_ctf_field_type
*event_context_type
= NULL
;
619 struct bt_ctf_field_type
*event_payload_type
= NULL
;
622 bt_ctf_event_class_get_context_type(event_class
);
624 bt_ctf_event_class_get_payload_type(event_class
);
627 * It is important to use the field types returned by
628 * the previous trace and stream class validation here
629 * because copies could have been made.
631 ret
= bt_ctf_validate_class_types(trace
->environment
,
632 trace_sc_validation_output
.packet_header_type
,
633 trace_sc_validation_output
.packet_context_type
,
634 trace_sc_validation_output
.event_header_type
,
635 trace_sc_validation_output
.stream_event_ctx_type
,
636 event_context_type
, event_payload_type
,
637 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
638 ec_validation_flags
);
639 BT_PUT(event_context_type
);
640 BT_PUT(event_payload_type
);
647 if ((ec_validation_outputs
[i
].valid_flags
&
648 ec_validation_flags
) != ec_validation_flags
) {
649 /* Invalid event class */
655 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
657 stream_id
= trace
->next_stream_id
++;
663 /* Try to assign a new stream id */
664 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
665 if (stream_id
== bt_ctf_stream_class_get_id(
666 trace
->stream_classes
->pdata
[i
])) {
667 /* Duplicate stream id found */
673 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
675 /* TODO Should retry with a different stream id */
681 bt_object_set_parent(stream_class
, trace
);
682 g_ptr_array_add(trace
->stream_classes
, stream_class
);
685 * At this point we know that the function will be successful.
686 * Therefore we can replace the trace and stream class field
687 * types with what's in their validation output structure and
688 * mark them as valid. We can also replace the field types of
689 * all the event classes of the stream class and mark them as
692 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
693 &trace_sc_validation_output
, trace_sc_validation_flags
);
695 stream_class
->valid
= 1;
698 * Put what was not moved in bt_ctf_validation_replace_types().
700 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
702 for (i
= 0; i
< event_class_count
; i
++) {
703 struct bt_ctf_event_class
*event_class
=
704 bt_ctf_stream_class_get_event_class_by_index(
707 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
708 &ec_validation_outputs
[i
], ec_validation_flags
);
709 event_class
->valid
= 1;
713 * Put what was not moved in
714 * bt_ctf_validation_replace_types().
716 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
720 * Freeze the trace and the stream class.
722 bt_ctf_stream_class_freeze(stream_class
);
723 bt_ctf_trace_freeze(trace
);
725 /* Notifiy listeners of the trace's schema modification. */
726 bt_ctf_stream_class_visit(stream_class
,
727 bt_ctf_trace_object_modification
, trace
);
730 bt_object_set_parent(stream_class
, NULL
);
732 if (ec_validation_outputs
) {
733 for (i
= 0; i
< event_class_count
; i
++) {
734 bt_ctf_validation_output_put_types(
735 &ec_validation_outputs
[i
]);
740 g_free(ec_validation_outputs
);
741 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
742 bt_put(current_parent_trace
);
743 assert(!packet_header_type
);
744 assert(!packet_context_type
);
745 assert(!event_header_type
);
746 assert(!stream_event_ctx_type
);
751 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
760 ret
= (int64_t) trace
->streams
->len
;
766 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
767 struct bt_ctf_trace
*trace
,
770 struct bt_ctf_stream
*stream
= NULL
;
772 if (!trace
|| index
>= trace
->streams
->len
) {
776 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
782 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
791 ret
= (int64_t) trace
->stream_classes
->len
;
796 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
797 struct bt_ctf_trace
*trace
, uint64_t index
)
799 struct bt_ctf_stream_class
*stream_class
= NULL
;
801 if (!trace
|| index
>= trace
->stream_classes
->len
) {
805 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
806 bt_get(stream_class
);
811 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
812 struct bt_ctf_trace
*trace
, uint64_t id_param
)
815 struct bt_ctf_stream_class
*stream_class
= NULL
;
816 int64_t id
= (int64_t) id_param
;
818 if (!trace
|| id
< 0) {
822 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
823 struct bt_ctf_stream_class
*stream_class_candidate
;
825 stream_class_candidate
=
826 g_ptr_array_index(trace
->stream_classes
, i
);
828 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
830 stream_class
= stream_class_candidate
;
831 bt_get(stream_class
);
840 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
841 struct bt_ctf_trace
*trace
, const char *name
)
844 struct bt_ctf_clock_class
*clock_class
= NULL
;
846 if (!trace
|| !name
) {
850 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
851 struct bt_ctf_clock_class
*cur_clk
=
852 g_ptr_array_index(trace
->clocks
, i
);
853 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
859 if (!strcmp(cur_clk_name
, name
)) {
860 clock_class
= cur_clk
;
871 bt_bool
bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
872 struct bt_ctf_clock_class
*clock_class
)
874 struct search_query query
= { .value
= clock_class
, .found
= 0 };
879 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
884 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
888 switch (byte_order
) {
889 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
892 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
895 case BT_CTF_BYTE_ORDER_NATIVE
:
906 int append_trace_metadata(struct bt_ctf_trace
*trace
,
907 struct metadata_context
*context
)
909 unsigned char *uuid
= trace
->uuid
;
912 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
917 g_string_append(context
->string
, "trace {\n");
918 g_string_append(context
->string
, "\tmajor = 1;\n");
919 g_string_append(context
->string
, "\tminor = 8;\n");
920 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
921 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
922 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
924 if (trace
->uuid_set
) {
925 g_string_append_printf(context
->string
,
926 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
927 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
928 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
929 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
930 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
933 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
934 get_byte_order_string(trace
->native_byte_order
));
936 g_string_append(context
->string
, "\tpacket.header := ");
937 context
->current_indentation_level
++;
938 g_string_assign(context
->field_name
, "");
939 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
944 context
->current_indentation_level
--;
946 g_string_append(context
->string
, ";\n};\n\n");
952 void append_env_metadata(struct bt_ctf_trace
*trace
,
953 struct metadata_context
*context
)
958 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
964 g_string_append(context
->string
, "env {\n");
966 for (i
= 0; i
< env_size
; i
++) {
967 struct bt_value
*env_field_value_obj
= NULL
;
968 const char *entry_name
;
970 entry_name
= bt_ctf_attributes_get_field_name(
971 trace
->environment
, i
);
972 env_field_value_obj
= bt_ctf_attributes_get_field_value(
973 trace
->environment
, i
);
975 if (!entry_name
|| !env_field_value_obj
) {
979 switch (bt_value_get_type(env_field_value_obj
)) {
980 case BT_VALUE_TYPE_INTEGER
:
985 ret
= bt_value_integer_get(env_field_value_obj
,
992 g_string_append_printf(context
->string
,
993 "\t%s = %" PRId64
";\n", entry_name
,
997 case BT_VALUE_TYPE_STRING
:
1000 const char *str_value
;
1001 char *escaped_str
= NULL
;
1003 ret
= bt_value_string_get(env_field_value_obj
,
1010 escaped_str
= g_strescape(str_value
, NULL
);
1016 g_string_append_printf(context
->string
,
1017 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1027 BT_PUT(env_field_value_obj
);
1030 g_string_append(context
->string
, "};\n\n");
1033 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1035 char *metadata
= NULL
;
1036 struct metadata_context
*context
= NULL
;
1044 context
= g_new0(struct metadata_context
, 1);
1049 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1050 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1051 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1052 if (append_trace_metadata(trace
, context
)) {
1055 append_env_metadata(trace
, context
);
1056 g_ptr_array_foreach(trace
->clocks
,
1057 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1059 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1060 err
= bt_ctf_stream_class_serialize(
1061 trace
->stream_classes
->pdata
[i
], context
);
1067 metadata
= context
->string
->str
;
1069 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1070 g_string_free(context
->field_name
, TRUE
);
1076 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1077 struct bt_ctf_trace
*trace
)
1079 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1085 ret
= trace
->native_byte_order
;
1091 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1092 enum bt_ctf_byte_order byte_order
)
1096 if (!trace
|| trace
->frozen
) {
1101 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1102 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1103 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1108 trace
->native_byte_order
= byte_order
;
1114 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1115 struct bt_ctf_trace
*trace
)
1117 struct bt_ctf_field_type
*field_type
= NULL
;
1123 bt_get(trace
->packet_header_type
);
1124 field_type
= trace
->packet_header_type
;
1129 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1130 struct bt_ctf_field_type
*packet_header_type
)
1134 if (!trace
|| trace
->frozen
) {
1139 /* packet_header_type must be a structure. */
1140 if (packet_header_type
&&
1141 bt_ctf_field_type_get_type_id(packet_header_type
) !=
1142 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1147 bt_put(trace
->packet_header_type
);
1148 trace
->packet_header_type
= bt_get(packet_header_type
);
1154 int64_t get_stream_class_count(void *element
)
1156 return bt_ctf_trace_get_stream_class_count(
1157 (struct bt_ctf_trace
*) element
);
1161 void *get_stream_class(void *element
, int i
)
1163 return bt_ctf_trace_get_stream_class_by_index(
1164 (struct bt_ctf_trace
*) element
, i
);
1168 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1170 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1173 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1174 bt_ctf_visitor visitor
, void *data
)
1177 struct bt_ctf_object obj
=
1178 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1180 if (!trace
|| !visitor
) {
1185 ret
= visitor_helper(&obj
, get_stream_class_count
,
1186 get_stream_class
, visit_stream_class
, visitor
, data
);
1192 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1194 struct listener_wrapper
*listener_wrapper
= data
;
1196 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1200 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1201 bt_ctf_listener_cb listener
, void *listener_data
)
1204 struct listener_wrapper
*listener_wrapper
=
1205 g_new0(struct listener_wrapper
, 1);
1207 if (!trace
|| !listener
|| !listener_wrapper
) {
1212 listener_wrapper
->listener
= listener
;
1213 listener_wrapper
->data
= listener_data
;
1215 /* Visit the current schema. */
1216 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
1222 * Add listener to the array of callbacks which will be invoked on
1225 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
1228 g_free(listener_wrapper
);
1233 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
1237 struct bt_ctf_trace
*trace
= trace_ptr
;
1242 if (trace
->listeners
->len
== 0) {
1246 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
1247 struct listener_wrapper
*listener
=
1248 g_ptr_array_index(trace
->listeners
, i
);
1250 listener
->listener(object
, listener
->data
);
1257 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1260 unsigned int alignment
, size
;
1261 struct bt_ctf_field_type
*field_type
= NULL
;
1263 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1267 alignment
= field_type_aliases_alignments
[alias
];
1268 size
= field_type_aliases_sizes
[alias
];
1269 field_type
= bt_ctf_field_type_integer_create(size
);
1270 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
1279 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1283 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1284 bt_ctf_attributes_freeze(trace
->environment
);
1286 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1287 struct bt_ctf_clock_class
*clock_class
=
1288 g_ptr_array_index(trace
->clocks
, i
);
1290 bt_ctf_clock_class_freeze(clock_class
);
1296 bt_bool
bt_ctf_trace_is_static(struct bt_ctf_trace
*trace
)
1298 bt_bool is_static
= BT_FALSE
;
1304 is_static
= trace
->is_static
;
1310 int bt_ctf_trace_set_is_static(struct bt_ctf_trace
*trace
)
1319 trace
->is_static
= BT_TRUE
;
1320 bt_ctf_trace_freeze(trace
);