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/endian-internal.h>
50 #define DEFAULT_IDENTIFIER_SIZE 128
51 #define DEFAULT_METADATA_STRING_SIZE 4096
53 struct listener_wrapper
{
54 bt_ctf_listener_cb listener
;
59 void bt_ctf_trace_destroy(struct bt_object
*obj
);
61 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
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
;
87 trace
= g_new0(struct bt_ctf_trace
, 1);
92 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_NATIVE
;
93 bt_object_init(trace
, bt_ctf_trace_destroy
);
94 trace
->clocks
= g_ptr_array_new_with_free_func(
95 (GDestroyNotify
) bt_put
);
96 trace
->streams
= g_ptr_array_new_with_free_func(
97 (GDestroyNotify
) bt_object_release
);
98 trace
->stream_classes
= g_ptr_array_new_with_free_func(
99 (GDestroyNotify
) bt_object_release
);
100 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
104 /* Generate a trace UUID */
105 uuid_generate(trace
->uuid
);
106 if (init_trace_packet_header(trace
)) {
110 /* Create the environment array object */
111 trace
->environment
= bt_ctf_attributes_create();
112 if (!trace
->environment
) {
116 trace
->listeners
= g_ptr_array_new_with_free_func(
117 (GDestroyNotify
) g_free
);
118 if (!trace
->listeners
) {
129 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
131 const char *name
= NULL
;
133 if (!trace
|| !trace
->name
) {
137 name
= trace
->name
->str
;
142 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
146 if (!trace
|| !name
|| trace
->frozen
) {
151 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
161 void bt_ctf_trace_destroy(struct bt_object
*obj
)
163 struct bt_ctf_trace
*trace
;
165 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
166 if (trace
->environment
) {
167 bt_ctf_attributes_destroy(trace
->environment
);
171 g_string_free(trace
->name
, TRUE
);
175 g_ptr_array_free(trace
->clocks
, TRUE
);
178 if (trace
->streams
) {
179 g_ptr_array_free(trace
->streams
, TRUE
);
182 if (trace
->stream_classes
) {
183 g_ptr_array_free(trace
->stream_classes
, TRUE
);
186 if (trace
->listeners
) {
187 g_ptr_array_free(trace
->listeners
, TRUE
);
190 bt_put(trace
->packet_header_type
);
194 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
195 const char *name
, struct bt_value
*value
)
199 if (!trace
|| !name
|| !value
||
200 bt_ctf_validate_identifier(name
) ||
201 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
206 if (strchr(name
, ' ')) {
213 * New environment fields may be added to a frozen trace,
214 * but existing fields may not be changed.
216 * The object passed is frozen like all other attributes.
218 struct bt_value
*attribute
=
219 bt_ctf_attributes_get_field_value_by_name(
220 trace
->environment
, name
);
228 bt_value_freeze(value
);
231 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
238 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
239 const char *name
, const char *value
)
242 struct bt_value
*env_value_string_obj
= NULL
;
244 if (!trace
|| !name
|| !value
) {
251 * New environment fields may be added to a frozen trace,
252 * but existing fields may not be changed.
254 struct bt_value
*attribute
=
255 bt_ctf_attributes_get_field_value_by_name(
256 trace
->environment
, name
);
265 env_value_string_obj
= bt_value_string_create_init(value
);
267 if (!env_value_string_obj
) {
273 bt_value_freeze(env_value_string_obj
);
275 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
276 env_value_string_obj
);
279 BT_PUT(env_value_string_obj
);
283 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
284 const char *name
, int64_t value
)
287 struct bt_value
*env_value_integer_obj
= NULL
;
289 if (!trace
|| !name
) {
296 * New environment fields may be added to a frozen trace,
297 * but existing fields may not be changed.
299 struct bt_value
*attribute
=
300 bt_ctf_attributes_get_field_value_by_name(
301 trace
->environment
, name
);
310 env_value_integer_obj
= bt_value_integer_create_init(value
);
311 if (!env_value_integer_obj
) {
316 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
317 env_value_integer_obj
);
319 bt_value_freeze(env_value_integer_obj
);
322 BT_PUT(env_value_integer_obj
);
326 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
335 ret
= bt_ctf_attributes_get_count(trace
->environment
);
342 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
345 const char *ret
= NULL
;
351 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
357 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
358 struct bt_ctf_trace
*trace
, uint64_t index
)
360 struct bt_value
*ret
= NULL
;
366 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
372 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
373 struct bt_ctf_trace
*trace
, const char *name
)
375 struct bt_value
*ret
= NULL
;
377 if (!trace
|| !name
) {
381 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
388 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
389 struct bt_ctf_clock_class
*clock_class
)
393 if (!trace
|| trace
->is_static
||
394 !bt_ctf_clock_class_is_valid(clock_class
)) {
399 /* Check for duplicate clock classes */
400 if (bt_ctf_trace_has_clock_class(trace
, clock_class
)) {
406 g_ptr_array_add(trace
->clocks
, clock_class
);
409 bt_ctf_clock_class_freeze(clock_class
);
415 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
417 int64_t ret
= (int64_t) -1;
423 ret
= trace
->clocks
->len
;
428 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
429 struct bt_ctf_trace
*trace
, uint64_t index
)
431 struct bt_ctf_clock_class
*clock_class
= NULL
;
433 if (!trace
|| index
>= trace
->clocks
->len
) {
437 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
443 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
444 struct bt_ctf_stream_class
*stream_class
)
449 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
450 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
451 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
452 BT_CTF_VALIDATION_FLAG_TRACE
|
453 BT_CTF_VALIDATION_FLAG_STREAM
;
454 const enum bt_ctf_validation_flag ec_validation_flags
=
455 BT_CTF_VALIDATION_FLAG_EVENT
;
456 struct bt_ctf_field_type
*packet_header_type
= NULL
;
457 struct bt_ctf_field_type
*packet_context_type
= NULL
;
458 struct bt_ctf_field_type
*event_header_type
= NULL
;
459 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
460 int64_t event_class_count
;
461 struct bt_ctf_trace
*current_parent_trace
= NULL
;
463 if (!trace
|| !stream_class
|| trace
->is_static
) {
469 * At the end of this function we freeze the trace, so its
470 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
472 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
477 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
478 if (current_parent_trace
) {
479 /* Stream class is already associated to a trace, abort. */
485 bt_ctf_stream_class_get_event_class_count(stream_class
);
486 assert(event_class_count
>= 0);
488 /* Check for duplicate stream classes */
489 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
490 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
491 /* Stream class already registered to the trace */
497 if (stream_class
->clock
) {
498 struct bt_ctf_clock_class
*stream_clock_class
=
499 stream_class
->clock
->clock_class
;
501 if (trace
->is_created_by_writer
) {
503 * Make sure this clock was also added to the
504 * trace (potentially through its CTF writer
509 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
510 if (trace
->clocks
->pdata
[i
] ==
511 stream_clock_class
) {
517 if (i
== trace
->clocks
->len
) {
524 * This trace was NOT created by a CTF writer,
525 * thus do not allow the stream class to add to
526 * have a clock at all. Those are two
527 * independent APIs (non-writer and writer
528 * APIs), and isolating them simplifies things.
536 * We're about to freeze both the trace and the stream class.
537 * Also, each event class contained in this stream class are
540 * This trace, this stream class, and all its event classes
541 * should be valid at this point.
543 * Validate trace and stream class first, then each event
544 * class of this stream class can be validated individually.
547 bt_ctf_trace_get_packet_header_type(trace
);
548 packet_context_type
=
549 bt_ctf_stream_class_get_packet_context_type(stream_class
);
551 bt_ctf_stream_class_get_event_header_type(stream_class
);
552 stream_event_ctx_type
=
553 bt_ctf_stream_class_get_event_context_type(stream_class
);
554 ret
= bt_ctf_validate_class_types(trace
->environment
,
555 packet_header_type
, packet_context_type
, event_header_type
,
556 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
557 stream_class
->valid
, 1, &trace_sc_validation_output
,
558 trace_sc_validation_flags
);
559 BT_PUT(packet_header_type
);
560 BT_PUT(packet_context_type
);
561 BT_PUT(event_header_type
);
562 BT_PUT(stream_event_ctx_type
);
566 * This means something went wrong during the validation
567 * process, not that the objects are invalid.
572 if ((trace_sc_validation_output
.valid_flags
&
573 trace_sc_validation_flags
) !=
574 trace_sc_validation_flags
) {
575 /* Invalid trace/stream class */
580 if (event_class_count
> 0) {
581 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
583 if (!ec_validation_outputs
) {
589 /* Validate each event class individually */
590 for (i
= 0; i
< event_class_count
; i
++) {
591 struct bt_ctf_event_class
*event_class
=
592 bt_ctf_stream_class_get_event_class_by_index(
594 struct bt_ctf_field_type
*event_context_type
= NULL
;
595 struct bt_ctf_field_type
*event_payload_type
= NULL
;
598 bt_ctf_event_class_get_context_type(event_class
);
600 bt_ctf_event_class_get_payload_type(event_class
);
603 * It is important to use the field types returned by
604 * the previous trace and stream class validation here
605 * because copies could have been made.
607 ret
= bt_ctf_validate_class_types(trace
->environment
,
608 trace_sc_validation_output
.packet_header_type
,
609 trace_sc_validation_output
.packet_context_type
,
610 trace_sc_validation_output
.event_header_type
,
611 trace_sc_validation_output
.stream_event_ctx_type
,
612 event_context_type
, event_payload_type
,
613 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
614 ec_validation_flags
);
615 BT_PUT(event_context_type
);
616 BT_PUT(event_payload_type
);
623 if ((ec_validation_outputs
[i
].valid_flags
&
624 ec_validation_flags
) != ec_validation_flags
) {
625 /* Invalid event class */
631 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
633 stream_id
= trace
->next_stream_id
++;
639 /* Try to assign a new stream id */
640 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
641 if (stream_id
== bt_ctf_stream_class_get_id(
642 trace
->stream_classes
->pdata
[i
])) {
643 /* Duplicate stream id found */
649 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
651 /* TODO Should retry with a different stream id */
657 bt_object_set_parent(stream_class
, trace
);
658 g_ptr_array_add(trace
->stream_classes
, stream_class
);
661 * At this point we know that the function will be successful.
662 * Therefore we can replace the trace and stream class field
663 * types with what's in their validation output structure and
664 * mark them as valid. We can also replace the field types of
665 * all the event classes of the stream class and mark them as
668 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
669 &trace_sc_validation_output
, trace_sc_validation_flags
);
671 stream_class
->valid
= 1;
674 * Put what was not moved in bt_ctf_validation_replace_types().
676 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
678 for (i
= 0; i
< event_class_count
; i
++) {
679 struct bt_ctf_event_class
*event_class
=
680 bt_ctf_stream_class_get_event_class_by_index(
683 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
684 &ec_validation_outputs
[i
], ec_validation_flags
);
685 event_class
->valid
= 1;
689 * Put what was not moved in
690 * bt_ctf_validation_replace_types().
692 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
696 * Freeze the trace and the stream class.
698 bt_ctf_stream_class_freeze(stream_class
);
699 bt_ctf_trace_freeze(trace
);
701 /* Notifiy listeners of the trace's schema modification. */
702 bt_ctf_stream_class_visit(stream_class
,
703 bt_ctf_trace_object_modification
, trace
);
706 bt_object_set_parent(stream_class
, NULL
);
708 if (ec_validation_outputs
) {
709 for (i
= 0; i
< event_class_count
; i
++) {
710 bt_ctf_validation_output_put_types(
711 &ec_validation_outputs
[i
]);
716 g_free(ec_validation_outputs
);
717 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
718 bt_put(current_parent_trace
);
719 assert(!packet_header_type
);
720 assert(!packet_context_type
);
721 assert(!event_header_type
);
722 assert(!stream_event_ctx_type
);
727 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
736 ret
= (int64_t) trace
->streams
->len
;
742 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
743 struct bt_ctf_trace
*trace
,
746 struct bt_ctf_stream
*stream
= NULL
;
748 if (!trace
|| index
>= trace
->streams
->len
) {
752 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
758 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
767 ret
= (int64_t) trace
->stream_classes
->len
;
772 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
773 struct bt_ctf_trace
*trace
, uint64_t index
)
775 struct bt_ctf_stream_class
*stream_class
= NULL
;
777 if (!trace
|| index
>= trace
->stream_classes
->len
) {
781 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
782 bt_get(stream_class
);
787 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
788 struct bt_ctf_trace
*trace
, uint64_t id_param
)
791 struct bt_ctf_stream_class
*stream_class
= NULL
;
792 int64_t id
= (int64_t) id_param
;
794 if (!trace
|| id
< 0) {
798 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
799 struct bt_ctf_stream_class
*stream_class_candidate
;
801 stream_class_candidate
=
802 g_ptr_array_index(trace
->stream_classes
, i
);
804 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
806 stream_class
= stream_class_candidate
;
807 bt_get(stream_class
);
816 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
817 struct bt_ctf_trace
*trace
, const char *name
)
820 struct bt_ctf_clock_class
*clock_class
= NULL
;
822 if (!trace
|| !name
) {
826 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
827 struct bt_ctf_clock_class
*cur_clk
=
828 g_ptr_array_index(trace
->clocks
, i
);
829 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
835 if (!strcmp(cur_clk_name
, name
)) {
836 clock_class
= cur_clk
;
847 bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
848 struct bt_ctf_clock_class
*clock_class
)
850 struct search_query query
= { .value
= clock_class
, .found
= 0 };
855 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
860 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
864 switch (byte_order
) {
865 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
868 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
871 case BT_CTF_BYTE_ORDER_NATIVE
:
882 int append_trace_metadata(struct bt_ctf_trace
*trace
,
883 struct metadata_context
*context
)
885 unsigned char *uuid
= trace
->uuid
;
888 g_string_append(context
->string
, "trace {\n");
890 g_string_append(context
->string
, "\tmajor = 1;\n");
891 g_string_append(context
->string
, "\tminor = 8;\n");
892 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
893 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
894 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
895 g_string_append_printf(context
->string
,
896 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
897 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
898 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
899 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
900 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
901 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
902 get_byte_order_string(trace
->native_byte_order
));
904 g_string_append(context
->string
, "\tpacket.header := ");
905 context
->current_indentation_level
++;
906 g_string_assign(context
->field_name
, "");
907 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
912 context
->current_indentation_level
--;
914 g_string_append(context
->string
, ";\n};\n\n");
920 void append_env_metadata(struct bt_ctf_trace
*trace
,
921 struct metadata_context
*context
)
926 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
932 g_string_append(context
->string
, "env {\n");
934 for (i
= 0; i
< env_size
; i
++) {
935 struct bt_value
*env_field_value_obj
= NULL
;
936 const char *entry_name
;
938 entry_name
= bt_ctf_attributes_get_field_name(
939 trace
->environment
, i
);
940 env_field_value_obj
= bt_ctf_attributes_get_field_value(
941 trace
->environment
, i
);
943 if (!entry_name
|| !env_field_value_obj
) {
947 switch (bt_value_get_type(env_field_value_obj
)) {
948 case BT_VALUE_TYPE_INTEGER
:
953 ret
= bt_value_integer_get(env_field_value_obj
,
960 g_string_append_printf(context
->string
,
961 "\t%s = %" PRId64
";\n", entry_name
,
965 case BT_VALUE_TYPE_STRING
:
968 const char *str_value
;
969 char *escaped_str
= NULL
;
971 ret
= bt_value_string_get(env_field_value_obj
,
978 escaped_str
= g_strescape(str_value
, NULL
);
984 g_string_append_printf(context
->string
,
985 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
995 BT_PUT(env_field_value_obj
);
998 g_string_append(context
->string
, "};\n\n");
1001 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1003 char *metadata
= NULL
;
1004 struct metadata_context
*context
= NULL
;
1012 context
= g_new0(struct metadata_context
, 1);
1017 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1018 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1019 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1020 if (append_trace_metadata(trace
, context
)) {
1023 append_env_metadata(trace
, context
);
1024 g_ptr_array_foreach(trace
->clocks
,
1025 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1027 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1028 err
= bt_ctf_stream_class_serialize(
1029 trace
->stream_classes
->pdata
[i
], context
);
1035 metadata
= context
->string
->str
;
1037 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1038 g_string_free(context
->field_name
, TRUE
);
1044 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
1046 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1052 ret
= trace
->native_byte_order
;
1058 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1059 enum bt_ctf_byte_order byte_order
)
1063 if (!trace
|| trace
->frozen
) {
1068 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1069 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1070 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1075 trace
->native_byte_order
= byte_order
;
1081 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1082 struct bt_ctf_trace
*trace
)
1084 struct bt_ctf_field_type
*field_type
= NULL
;
1090 bt_get(trace
->packet_header_type
);
1091 field_type
= trace
->packet_header_type
;
1096 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1097 struct bt_ctf_field_type
*packet_header_type
)
1101 if (!trace
|| trace
->frozen
) {
1106 /* packet_header_type must be a structure. */
1107 if (packet_header_type
&&
1108 bt_ctf_field_type_get_type_id(packet_header_type
) !=
1109 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1114 bt_put(trace
->packet_header_type
);
1115 trace
->packet_header_type
= bt_get(packet_header_type
);
1121 int64_t get_stream_class_count(void *element
)
1123 return bt_ctf_trace_get_stream_class_count(
1124 (struct bt_ctf_trace
*) element
);
1128 void *get_stream_class(void *element
, int i
)
1130 return bt_ctf_trace_get_stream_class_by_index(
1131 (struct bt_ctf_trace
*) element
, i
);
1135 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1137 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1140 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1141 bt_ctf_visitor visitor
, void *data
)
1144 struct bt_ctf_object obj
=
1145 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1147 if (!trace
|| !visitor
) {
1152 ret
= visitor_helper(&obj
, get_stream_class_count
,
1153 get_stream_class
, visit_stream_class
, visitor
, data
);
1159 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1161 struct listener_wrapper
*listener_wrapper
= data
;
1163 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1167 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1168 bt_ctf_listener_cb listener
, void *listener_data
)
1171 struct listener_wrapper
*listener_wrapper
=
1172 g_new0(struct listener_wrapper
, 1);
1174 if (!trace
|| !listener
|| !listener_wrapper
) {
1179 listener_wrapper
->listener
= listener
;
1180 listener_wrapper
->data
= listener_data
;
1182 /* Visit the current schema. */
1183 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
1189 * Add listener to the array of callbacks which will be invoked on
1192 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
1195 g_free(listener_wrapper
);
1200 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
1204 struct bt_ctf_trace
*trace
= trace_ptr
;
1209 if (trace
->listeners
->len
== 0) {
1213 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
1214 struct listener_wrapper
*listener
=
1215 g_ptr_array_index(trace
->listeners
, i
);
1217 listener
->listener(object
, listener
->data
);
1224 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1227 unsigned int alignment
, size
;
1228 struct bt_ctf_field_type
*field_type
= NULL
;
1230 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1234 alignment
= field_type_aliases_alignments
[alias
];
1235 size
= field_type_aliases_sizes
[alias
];
1236 field_type
= bt_ctf_field_type_integer_create(size
);
1237 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
1246 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1250 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1251 bt_ctf_attributes_freeze(trace
->environment
);
1253 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1254 struct bt_ctf_clock_class
*clock_class
=
1255 g_ptr_array_index(trace
->clocks
, i
);
1257 bt_ctf_clock_class_freeze(clock_class
);
1264 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
1267 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
1268 struct bt_ctf_field_type
*_uint32_t
=
1269 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1270 struct bt_ctf_field_type
*_uint8_t
=
1271 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1272 struct bt_ctf_field_type
*trace_packet_header_type
=
1273 bt_ctf_field_type_structure_create();
1274 struct bt_ctf_field_type
*uuid_array_type
=
1275 bt_ctf_field_type_array_create(_uint8_t
, 16);
1277 if (!trace_packet_header_type
|| !uuid_array_type
) {
1282 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1283 _uint32_t
, "magic");
1288 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1289 uuid_array_type
, "uuid");
1294 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1295 _uint32_t
, "stream_id");
1300 ret
= bt_ctf_trace_set_packet_header_type(trace
,
1301 trace_packet_header_type
);
1306 bt_put(uuid_array_type
);
1311 bt_put(trace_packet_header_type
);
1315 bool bt_ctf_trace_is_static(struct bt_ctf_trace
*trace
)
1317 bool is_static
= false;
1323 is_static
= trace
->is_static
;
1329 int bt_ctf_trace_set_is_static(struct bt_ctf_trace
*trace
)
1338 trace
->is_static
= true;
1339 bt_ctf_trace_freeze(trace
);