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>
49 #define DEFAULT_IDENTIFIER_SIZE 128
50 #define DEFAULT_METADATA_STRING_SIZE 4096
52 struct listener_wrapper
{
53 bt_ctf_listener_cb listener
;
58 void bt_ctf_trace_destroy(struct bt_object
*obj
);
60 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
62 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
65 const unsigned int field_type_aliases_alignments
[] = {
66 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
67 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
68 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
69 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
73 const unsigned int field_type_aliases_sizes
[] = {
74 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
75 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
76 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
77 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
78 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
79 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
82 struct bt_ctf_trace
*bt_ctf_trace_create(void)
84 struct bt_ctf_trace
*trace
= NULL
;
86 trace
= g_new0(struct bt_ctf_trace
, 1);
91 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_NATIVE
;
92 bt_object_init(trace
, bt_ctf_trace_destroy
);
93 trace
->clocks
= g_ptr_array_new_with_free_func(
94 (GDestroyNotify
) bt_put
);
95 trace
->streams
= g_ptr_array_new_with_free_func(
96 (GDestroyNotify
) bt_object_release
);
97 trace
->stream_classes
= g_ptr_array_new_with_free_func(
98 (GDestroyNotify
) bt_object_release
);
99 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
103 /* Generate a trace UUID */
104 uuid_generate(trace
->uuid
);
105 if (init_trace_packet_header(trace
)) {
109 /* Create the environment array object */
110 trace
->environment
= bt_ctf_attributes_create();
111 if (!trace
->environment
) {
115 trace
->listeners
= g_ptr_array_new_with_free_func(
116 (GDestroyNotify
) g_free
);
117 if (!trace
->listeners
) {
128 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
130 const char *name
= NULL
;
132 if (!trace
|| !trace
->name
) {
136 name
= trace
->name
->str
;
141 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
145 if (!trace
|| !name
|| trace
->frozen
) {
150 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
160 void bt_ctf_trace_destroy(struct bt_object
*obj
)
162 struct bt_ctf_trace
*trace
;
164 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
165 if (trace
->environment
) {
166 bt_ctf_attributes_destroy(trace
->environment
);
170 g_string_free(trace
->name
, TRUE
);
174 g_ptr_array_free(trace
->clocks
, TRUE
);
177 if (trace
->streams
) {
178 g_ptr_array_free(trace
->streams
, TRUE
);
181 if (trace
->stream_classes
) {
182 g_ptr_array_free(trace
->stream_classes
, TRUE
);
185 if (trace
->listeners
) {
186 g_ptr_array_free(trace
->listeners
, TRUE
);
189 bt_put(trace
->packet_header_type
);
193 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
194 const char *name
, struct bt_value
*value
)
198 if (!trace
|| !name
|| !value
||
199 bt_ctf_validate_identifier(name
) ||
200 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
205 if (strchr(name
, ' ')) {
212 * New environment fields may be added to a frozen trace,
213 * but existing fields may not be changed.
215 * The object passed is frozen like all other attributes.
217 struct bt_value
*attribute
=
218 bt_ctf_attributes_get_field_value_by_name(
219 trace
->environment
, name
);
227 bt_value_freeze(value
);
230 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
237 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
238 const char *name
, const char *value
)
241 struct bt_value
*env_value_string_obj
= NULL
;
243 if (!trace
|| !name
|| !value
) {
250 * New environment fields may be added to a frozen trace,
251 * but existing fields may not be changed.
253 struct bt_value
*attribute
=
254 bt_ctf_attributes_get_field_value_by_name(
255 trace
->environment
, name
);
264 env_value_string_obj
= bt_value_string_create_init(value
);
266 if (!env_value_string_obj
) {
272 bt_value_freeze(env_value_string_obj
);
274 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
275 env_value_string_obj
);
278 BT_PUT(env_value_string_obj
);
282 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
283 const char *name
, int64_t value
)
286 struct bt_value
*env_value_integer_obj
= NULL
;
288 if (!trace
|| !name
) {
295 * New environment fields may be added to a frozen trace,
296 * but existing fields may not be changed.
298 struct bt_value
*attribute
=
299 bt_ctf_attributes_get_field_value_by_name(
300 trace
->environment
, name
);
309 env_value_integer_obj
= bt_value_integer_create_init(value
);
310 if (!env_value_integer_obj
) {
315 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
316 env_value_integer_obj
);
318 bt_value_freeze(env_value_integer_obj
);
321 BT_PUT(env_value_integer_obj
);
325 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
334 ret
= bt_ctf_attributes_get_count(trace
->environment
);
341 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
344 const char *ret
= NULL
;
350 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
356 struct bt_value
*bt_ctf_trace_get_environment_field_value(
357 struct bt_ctf_trace
*trace
, int index
)
359 struct bt_value
*ret
= NULL
;
365 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
371 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
372 struct bt_ctf_trace
*trace
, const char *name
)
374 struct bt_value
*ret
= NULL
;
376 if (!trace
|| !name
) {
380 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
387 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
388 struct bt_ctf_clock_class
*clock_class
)
392 if (!trace
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
397 /* Check for duplicate clock classes */
398 if (bt_ctf_trace_has_clock_class(trace
, clock_class
)) {
404 g_ptr_array_add(trace
->clocks
, clock_class
);
407 bt_ctf_clock_class_freeze(clock_class
);
413 int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
421 ret
= trace
->clocks
->len
;
426 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class(
427 struct bt_ctf_trace
*trace
, int index
)
429 struct bt_ctf_clock_class
*clock_class
= NULL
;
431 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
435 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
441 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
442 struct bt_ctf_stream_class
*stream_class
)
446 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
447 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
448 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
449 BT_CTF_VALIDATION_FLAG_TRACE
|
450 BT_CTF_VALIDATION_FLAG_STREAM
;
451 const enum bt_ctf_validation_flag ec_validation_flags
=
452 BT_CTF_VALIDATION_FLAG_EVENT
;
453 struct bt_ctf_field_type
*packet_header_type
= NULL
;
454 struct bt_ctf_field_type
*packet_context_type
= NULL
;
455 struct bt_ctf_field_type
*event_header_type
= NULL
;
456 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
457 int event_class_count
;
458 struct bt_ctf_trace
*current_parent_trace
= NULL
;
460 if (!trace
|| !stream_class
) {
466 * At the end of this function we freeze the trace, so its
467 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
469 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
474 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
475 if (current_parent_trace
) {
476 /* Stream class is already associated to a trace, abort. */
482 bt_ctf_stream_class_get_event_class_count(stream_class
);
483 assert(event_class_count
>= 0);
485 /* Check for duplicate stream classes */
486 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
487 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
488 /* Stream class already registered to the trace */
494 if (stream_class
->clock
) {
495 struct bt_ctf_clock_class
*stream_clock_class
=
496 stream_class
->clock
->clock_class
;
498 if (trace
->is_created_by_writer
) {
500 * Make sure this clock was also added to the
501 * trace (potentially through its CTF writer
506 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
507 if (trace
->clocks
->pdata
[i
] ==
508 stream_clock_class
) {
514 if (i
== trace
->clocks
->len
) {
521 * This trace was NOT created by a CTF writer,
522 * thus do not allow the stream class to add to
523 * have a clock at all. Those are two
524 * independent APIs (non-writer and writer
525 * APIs), and isolating them simplifies things.
533 * We're about to freeze both the trace and the stream class.
534 * Also, each event class contained in this stream class are
537 * This trace, this stream class, and all its event classes
538 * should be valid at this point.
540 * Validate trace and stream class first, then each event
541 * class of this stream class can be validated individually.
544 bt_ctf_trace_get_packet_header_type(trace
);
545 packet_context_type
=
546 bt_ctf_stream_class_get_packet_context_type(stream_class
);
548 bt_ctf_stream_class_get_event_header_type(stream_class
);
549 stream_event_ctx_type
=
550 bt_ctf_stream_class_get_event_context_type(stream_class
);
551 ret
= bt_ctf_validate_class_types(trace
->environment
,
552 packet_header_type
, packet_context_type
, event_header_type
,
553 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
554 stream_class
->valid
, 1, &trace_sc_validation_output
,
555 trace_sc_validation_flags
);
556 BT_PUT(packet_header_type
);
557 BT_PUT(packet_context_type
);
558 BT_PUT(event_header_type
);
559 BT_PUT(stream_event_ctx_type
);
563 * This means something went wrong during the validation
564 * process, not that the objects are invalid.
569 if ((trace_sc_validation_output
.valid_flags
&
570 trace_sc_validation_flags
) !=
571 trace_sc_validation_flags
) {
572 /* Invalid trace/stream class */
577 if (event_class_count
> 0) {
578 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
580 if (!ec_validation_outputs
) {
586 /* Validate each event class individually */
587 for (i
= 0; i
< event_class_count
; i
++) {
588 struct bt_ctf_event_class
*event_class
=
589 bt_ctf_stream_class_get_event_class(stream_class
, i
);
590 struct bt_ctf_field_type
*event_context_type
= NULL
;
591 struct bt_ctf_field_type
*event_payload_type
= NULL
;
594 bt_ctf_event_class_get_context_type(event_class
);
596 bt_ctf_event_class_get_payload_type(event_class
);
599 * It is important to use the field types returned by
600 * the previous trace and stream class validation here
601 * because copies could have been made.
603 ret
= bt_ctf_validate_class_types(trace
->environment
,
604 trace_sc_validation_output
.packet_header_type
,
605 trace_sc_validation_output
.packet_context_type
,
606 trace_sc_validation_output
.event_header_type
,
607 trace_sc_validation_output
.stream_event_ctx_type
,
608 event_context_type
, event_payload_type
,
609 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
610 ec_validation_flags
);
611 BT_PUT(event_context_type
);
612 BT_PUT(event_payload_type
);
619 if ((ec_validation_outputs
[i
].valid_flags
&
620 ec_validation_flags
) != ec_validation_flags
) {
621 /* Invalid event class */
627 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
629 stream_id
= trace
->next_stream_id
++;
631 /* Try to assign a new stream id */
632 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
633 if (stream_id
== bt_ctf_stream_class_get_id(
634 trace
->stream_classes
->pdata
[i
])) {
635 /* Duplicate stream id found */
641 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
643 /* TODO Should retry with a different stream id */
649 bt_object_set_parent(stream_class
, trace
);
650 g_ptr_array_add(trace
->stream_classes
, stream_class
);
653 * At this point we know that the function will be successful.
654 * Therefore we can replace the trace and stream class field
655 * types with what's in their validation output structure and
656 * mark them as valid. We can also replace the field types of
657 * all the event classes of the stream class and mark them as
660 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
661 &trace_sc_validation_output
, trace_sc_validation_flags
);
663 stream_class
->valid
= 1;
666 * Put what was not moved in bt_ctf_validation_replace_types().
668 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
670 for (i
= 0; i
< event_class_count
; i
++) {
671 struct bt_ctf_event_class
*event_class
=
672 bt_ctf_stream_class_get_event_class(stream_class
, i
);
674 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
675 &ec_validation_outputs
[i
], ec_validation_flags
);
676 event_class
->valid
= 1;
680 * Put what was not moved in
681 * bt_ctf_validation_replace_types().
683 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
687 * Freeze the trace and the stream class.
689 bt_ctf_stream_class_freeze(stream_class
);
690 bt_ctf_trace_freeze(trace
);
692 /* Notifiy listeners of the trace's schema modification. */
693 bt_ctf_stream_class_visit(stream_class
,
694 bt_ctf_trace_object_modification
, trace
);
697 bt_object_set_parent(stream_class
, NULL
);
699 if (ec_validation_outputs
) {
700 for (i
= 0; i
< event_class_count
; i
++) {
701 bt_ctf_validation_output_put_types(
702 &ec_validation_outputs
[i
]);
707 g_free(ec_validation_outputs
);
708 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
709 bt_put(current_parent_trace
);
710 assert(!packet_header_type
);
711 assert(!packet_context_type
);
712 assert(!event_header_type
);
713 assert(!stream_event_ctx_type
);
718 int bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
727 ret
= trace
->streams
->len
;
733 struct bt_ctf_stream
*bt_ctf_trace_get_stream(struct bt_ctf_trace
*trace
,
736 struct bt_ctf_stream
*stream
= NULL
;
738 if (!trace
|| index
>= trace
->streams
->len
) {
742 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
748 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
757 ret
= trace
->stream_classes
->len
;
762 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
763 struct bt_ctf_trace
*trace
, int index
)
765 struct bt_ctf_stream_class
*stream_class
= NULL
;
767 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
771 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
772 bt_get(stream_class
);
777 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
778 struct bt_ctf_trace
*trace
, uint32_t id
)
781 struct bt_ctf_stream_class
*stream_class
= NULL
;
787 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
788 struct bt_ctf_stream_class
*stream_class_candidate
;
790 stream_class_candidate
=
791 g_ptr_array_index(trace
->stream_classes
, i
);
793 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
795 stream_class
= stream_class_candidate
;
796 bt_get(stream_class
);
805 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
806 struct bt_ctf_trace
*trace
, const char *name
)
809 struct bt_ctf_clock_class
*clock_class
= NULL
;
811 if (!trace
|| !name
) {
815 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
816 struct bt_ctf_clock_class
*cur_clk
=
817 g_ptr_array_index(trace
->clocks
, i
);
818 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
824 if (!strcmp(cur_clk_name
, name
)) {
825 clock_class
= cur_clk
;
836 bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
837 struct bt_ctf_clock_class
*clock_class
)
839 struct search_query query
= { .value
= clock_class
, .found
= 0 };
844 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
849 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
853 switch (byte_order
) {
854 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
857 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
860 case BT_CTF_BYTE_ORDER_NATIVE
:
871 int append_trace_metadata(struct bt_ctf_trace
*trace
,
872 struct metadata_context
*context
)
874 unsigned char *uuid
= trace
->uuid
;
877 g_string_append(context
->string
, "trace {\n");
879 g_string_append(context
->string
, "\tmajor = 1;\n");
880 g_string_append(context
->string
, "\tminor = 8;\n");
881 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
882 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
883 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
884 g_string_append_printf(context
->string
,
885 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
886 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
887 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
888 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
889 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
890 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
891 get_byte_order_string(trace
->native_byte_order
));
893 g_string_append(context
->string
, "\tpacket.header := ");
894 context
->current_indentation_level
++;
895 g_string_assign(context
->field_name
, "");
896 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
901 context
->current_indentation_level
--;
903 g_string_append(context
->string
, ";\n};\n\n");
909 void append_env_metadata(struct bt_ctf_trace
*trace
,
910 struct metadata_context
*context
)
915 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
921 g_string_append(context
->string
, "env {\n");
923 for (i
= 0; i
< env_size
; i
++) {
924 struct bt_value
*env_field_value_obj
= NULL
;
925 const char *entry_name
;
927 entry_name
= bt_ctf_attributes_get_field_name(
928 trace
->environment
, i
);
929 env_field_value_obj
= bt_ctf_attributes_get_field_value(
930 trace
->environment
, i
);
932 if (!entry_name
|| !env_field_value_obj
) {
936 switch (bt_value_get_type(env_field_value_obj
)) {
937 case BT_VALUE_TYPE_INTEGER
:
942 ret
= bt_value_integer_get(env_field_value_obj
,
949 g_string_append_printf(context
->string
,
950 "\t%s = %" PRId64
";\n", entry_name
,
954 case BT_VALUE_TYPE_STRING
:
957 const char *str_value
;
958 char *escaped_str
= NULL
;
960 ret
= bt_value_string_get(env_field_value_obj
,
967 escaped_str
= g_strescape(str_value
, NULL
);
973 g_string_append_printf(context
->string
,
974 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
984 BT_PUT(env_field_value_obj
);
987 g_string_append(context
->string
, "};\n\n");
990 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
992 char *metadata
= NULL
;
993 struct metadata_context
*context
= NULL
;
1001 context
= g_new0(struct metadata_context
, 1);
1006 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1007 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1008 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1009 if (append_trace_metadata(trace
, context
)) {
1012 append_env_metadata(trace
, context
);
1013 g_ptr_array_foreach(trace
->clocks
,
1014 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1016 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1017 err
= bt_ctf_stream_class_serialize(
1018 trace
->stream_classes
->pdata
[i
], context
);
1024 metadata
= context
->string
->str
;
1026 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1027 g_string_free(context
->field_name
, TRUE
);
1033 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
1035 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1041 ret
= trace
->native_byte_order
;
1047 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1048 enum bt_ctf_byte_order byte_order
)
1052 if (!trace
|| trace
->frozen
) {
1057 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1058 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1059 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1064 trace
->native_byte_order
= byte_order
;
1070 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1071 struct bt_ctf_trace
*trace
)
1073 struct bt_ctf_field_type
*field_type
= NULL
;
1079 bt_get(trace
->packet_header_type
);
1080 field_type
= trace
->packet_header_type
;
1085 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1086 struct bt_ctf_field_type
*packet_header_type
)
1090 if (!trace
|| trace
->frozen
) {
1095 /* packet_header_type must be a structure. */
1096 if (packet_header_type
&&
1097 bt_ctf_field_type_get_type_id(packet_header_type
) !=
1098 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1103 bt_put(trace
->packet_header_type
);
1104 trace
->packet_header_type
= bt_get(packet_header_type
);
1110 int get_stream_class_count(void *element
)
1112 return bt_ctf_trace_get_stream_class_count(
1113 (struct bt_ctf_trace
*) element
);
1117 void *get_stream_class(void *element
, int i
)
1119 return bt_ctf_trace_get_stream_class(
1120 (struct bt_ctf_trace
*) element
, i
);
1124 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1126 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1129 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1130 bt_ctf_visitor visitor
, void *data
)
1133 struct bt_ctf_object obj
=
1134 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1136 if (!trace
|| !visitor
) {
1141 ret
= visitor_helper(&obj
, get_stream_class_count
,
1142 get_stream_class
, visit_stream_class
, visitor
, data
);
1148 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1150 struct listener_wrapper
*listener_wrapper
= data
;
1152 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1156 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1157 bt_ctf_listener_cb listener
, void *listener_data
)
1160 struct listener_wrapper
*listener_wrapper
=
1161 g_new0(struct listener_wrapper
, 1);
1163 if (!trace
|| !listener
|| !listener_wrapper
) {
1168 listener_wrapper
->listener
= listener
;
1169 listener_wrapper
->data
= listener_data
;
1171 /* Visit the current schema. */
1172 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
1178 * Add listener to the array of callbacks which will be invoked on
1181 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
1184 g_free(listener_wrapper
);
1189 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
1193 struct bt_ctf_trace
*trace
= trace_ptr
;
1198 if (trace
->listeners
->len
== 0) {
1202 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
1203 struct listener_wrapper
*listener
=
1204 g_ptr_array_index(trace
->listeners
, i
);
1206 listener
->listener(object
, listener
->data
);
1213 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1216 unsigned int alignment
, size
;
1217 struct bt_ctf_field_type
*field_type
= NULL
;
1219 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1223 alignment
= field_type_aliases_alignments
[alias
];
1224 size
= field_type_aliases_sizes
[alias
];
1225 field_type
= bt_ctf_field_type_integer_create(size
);
1226 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
1235 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1239 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1240 bt_ctf_attributes_freeze(trace
->environment
);
1242 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1243 struct bt_ctf_clock_class
*clock_class
=
1244 g_ptr_array_index(trace
->clocks
, i
);
1246 bt_ctf_clock_class_freeze(clock_class
);
1253 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
1256 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
1257 struct bt_ctf_field_type
*_uint32_t
=
1258 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1259 struct bt_ctf_field_type
*_uint8_t
=
1260 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1261 struct bt_ctf_field_type
*trace_packet_header_type
=
1262 bt_ctf_field_type_structure_create();
1263 struct bt_ctf_field_type
*uuid_array_type
=
1264 bt_ctf_field_type_array_create(_uint8_t
, 16);
1266 if (!trace_packet_header_type
|| !uuid_array_type
) {
1271 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1272 _uint32_t
, "magic");
1277 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1278 uuid_array_type
, "uuid");
1283 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1284 _uint32_t
, "stream_id");
1289 ret
= bt_ctf_trace_set_packet_header_type(trace
,
1290 trace_packet_header_type
);
1295 bt_put(uuid_array_type
);
1300 bt_put(trace_packet_header_type
);