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 notification_handler
{
52 bt_ctf_notification_cb func
;
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
) {
121 void bt_ctf_trace_destroy(struct bt_object
*obj
)
123 struct bt_ctf_trace
*trace
;
125 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
126 if (trace
->environment
) {
127 bt_ctf_attributes_destroy(trace
->environment
);
131 g_ptr_array_free(trace
->clocks
, TRUE
);
134 if (trace
->streams
) {
135 g_ptr_array_free(trace
->streams
, TRUE
);
138 if (trace
->stream_classes
) {
139 g_ptr_array_free(trace
->stream_classes
, TRUE
);
142 bt_put(trace
->packet_header_type
);
146 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
147 const char *name
, struct bt_value
*value
)
151 if (!trace
|| !name
|| !value
||
152 bt_ctf_validate_identifier(name
) ||
153 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
158 if (strchr(name
, ' ')) {
165 * New environment fields may be added to a frozen trace,
166 * but existing fields may not be changed.
168 * The object passed is frozen like all other attributes.
170 struct bt_value
*attribute
=
171 bt_ctf_attributes_get_field_value_by_name(
172 trace
->environment
, name
);
180 bt_value_freeze(value
);
183 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
190 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
191 const char *name
, const char *value
)
194 struct bt_value
*env_value_string_obj
= NULL
;
196 if (!trace
|| !name
|| !value
) {
203 * New environment fields may be added to a frozen trace,
204 * but existing fields may not be changed.
206 struct bt_value
*attribute
=
207 bt_ctf_attributes_get_field_value_by_name(
208 trace
->environment
, name
);
217 env_value_string_obj
= bt_value_string_create_init(value
);
219 if (!env_value_string_obj
) {
225 bt_value_freeze(env_value_string_obj
);
227 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
228 env_value_string_obj
);
231 BT_PUT(env_value_string_obj
);
235 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
236 const char *name
, int64_t value
)
239 struct bt_value
*env_value_integer_obj
= NULL
;
241 if (!trace
|| !name
) {
248 * New environment fields may be added to a frozen trace,
249 * but existing fields may not be changed.
251 struct bt_value
*attribute
=
252 bt_ctf_attributes_get_field_value_by_name(
253 trace
->environment
, name
);
262 env_value_integer_obj
= bt_value_integer_create_init(value
);
263 if (!env_value_integer_obj
) {
268 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
269 env_value_integer_obj
);
271 bt_value_freeze(env_value_integer_obj
);
274 BT_PUT(env_value_integer_obj
);
278 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
287 ret
= bt_ctf_attributes_get_count(trace
->environment
);
294 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
297 const char *ret
= NULL
;
303 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
309 struct bt_value
*bt_ctf_trace_get_environment_field_value(
310 struct bt_ctf_trace
*trace
, int index
)
312 struct bt_value
*ret
= NULL
;
318 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
324 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
325 struct bt_ctf_trace
*trace
, const char *name
)
327 struct bt_value
*ret
= NULL
;
329 if (!trace
|| !name
) {
333 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
340 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
341 struct bt_ctf_clock
*clock
)
344 struct search_query query
= { .value
= clock
, .found
= 0 };
346 if (!trace
|| !clock
) {
351 /* Check for duplicate clocks */
352 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
359 g_ptr_array_add(trace
->clocks
, clock
);
361 if (!trace
->is_created_by_writer
) {
363 * Non-writer mode trace: disable clock value functions
364 * because clock values are per-stream in that
367 clock
->has_value
= 0;
371 bt_ctf_clock_freeze(clock
);
377 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
385 ret
= trace
->clocks
->len
;
390 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
393 struct bt_ctf_clock
*clock
= NULL
;
395 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
399 clock
= g_ptr_array_index(trace
->clocks
, index
);
405 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
406 struct bt_ctf_stream_class
*stream_class
)
410 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
411 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
412 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
413 BT_CTF_VALIDATION_FLAG_TRACE
|
414 BT_CTF_VALIDATION_FLAG_STREAM
;
415 const enum bt_ctf_validation_flag ec_validation_flags
=
416 BT_CTF_VALIDATION_FLAG_EVENT
;
417 struct bt_ctf_field_type
*packet_header_type
= NULL
;
418 struct bt_ctf_field_type
*packet_context_type
= NULL
;
419 struct bt_ctf_field_type
*event_header_type
= NULL
;
420 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
421 int event_class_count
;
422 struct bt_ctf_clock
*clock_to_add_to_trace
= NULL
;
424 if (!trace
|| !stream_class
) {
430 bt_ctf_stream_class_get_event_class_count(stream_class
);
431 assert(event_class_count
>= 0);
433 /* Check for duplicate stream classes */
434 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
435 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
436 /* Stream class already registered to the trace */
443 * If the stream class has a clock, register this clock to this
444 * trace if not already done.
446 if (stream_class
->clock
) {
447 const char *clock_name
=
448 bt_ctf_clock_get_name(stream_class
->clock
);
449 struct bt_ctf_clock
*trace_clock
;
452 trace_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
455 if (trace_clock
!= stream_class
->clock
) {
457 * Error: two different clocks in the
458 * trace would share the same name.
464 clock_to_add_to_trace
= bt_get(stream_class
->clock
);
469 * We're about to freeze both the trace and the stream class.
470 * Also, each event class contained in this stream class are
473 * This trace, this stream class, and all its event classes
474 * should be valid at this point.
476 * Validate trace and stream class first, then each event
477 * class of this stream class can be validated individually.
480 bt_ctf_trace_get_packet_header_type(trace
);
481 packet_context_type
=
482 bt_ctf_stream_class_get_packet_context_type(stream_class
);
484 bt_ctf_stream_class_get_event_header_type(stream_class
);
485 stream_event_ctx_type
=
486 bt_ctf_stream_class_get_event_context_type(stream_class
);
487 ret
= bt_ctf_validate_class_types(trace
->environment
,
488 packet_header_type
, packet_context_type
, event_header_type
,
489 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
490 stream_class
->valid
, 1, &trace_sc_validation_output
,
491 trace_sc_validation_flags
);
492 BT_PUT(packet_header_type
);
493 BT_PUT(packet_context_type
);
494 BT_PUT(event_header_type
);
495 BT_PUT(stream_event_ctx_type
);
499 * This means something went wrong during the validation
500 * process, not that the objects are invalid.
505 if ((trace_sc_validation_output
.valid_flags
&
506 trace_sc_validation_flags
) !=
507 trace_sc_validation_flags
) {
508 /* Invalid trace/stream class */
513 if (event_class_count
> 0) {
514 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
516 if (!ec_validation_outputs
) {
522 /* Validate each event class individually */
523 for (i
= 0; i
< event_class_count
; i
++) {
524 struct bt_ctf_event_class
*event_class
=
525 bt_ctf_stream_class_get_event_class(stream_class
, i
);
526 struct bt_ctf_field_type
*event_context_type
= NULL
;
527 struct bt_ctf_field_type
*event_payload_type
= NULL
;
530 bt_ctf_event_class_get_context_type(event_class
);
532 bt_ctf_event_class_get_payload_type(event_class
);
535 * It is important to use the field types returned by
536 * the previous trace and stream class validation here
537 * because copies could have been made.
539 ret
= bt_ctf_validate_class_types(trace
->environment
,
540 trace_sc_validation_output
.packet_header_type
,
541 trace_sc_validation_output
.packet_context_type
,
542 trace_sc_validation_output
.event_header_type
,
543 trace_sc_validation_output
.stream_event_ctx_type
,
544 event_context_type
, event_payload_type
,
545 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
546 ec_validation_flags
);
547 BT_PUT(event_context_type
);
548 BT_PUT(event_payload_type
);
555 if ((ec_validation_outputs
[i
].valid_flags
&
556 ec_validation_flags
) != ec_validation_flags
) {
557 /* Invalid event class */
563 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
565 stream_id
= trace
->next_stream_id
++;
567 /* Try to assign a new stream id */
568 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
569 if (stream_id
== bt_ctf_stream_class_get_id(
570 trace
->stream_classes
->pdata
[i
])) {
571 /* Duplicate stream id found */
577 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
579 /* TODO Should retry with a different stream id */
585 bt_object_set_parent(stream_class
, trace
);
586 g_ptr_array_add(trace
->stream_classes
, stream_class
);
589 * At this point we know that the function will be successful.
590 * Therefore we can replace the trace and stream class field
591 * types with what's in their validation output structure and
592 * mark them as valid. We can also replace the field types of
593 * all the event classes of the stream class and mark them as
596 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
597 &trace_sc_validation_output
, trace_sc_validation_flags
);
599 stream_class
->valid
= 1;
602 * Put what was not moved in bt_ctf_validation_replace_types().
604 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
606 for (i
= 0; i
< event_class_count
; i
++) {
607 struct bt_ctf_event_class
*event_class
=
608 bt_ctf_stream_class_get_event_class(stream_class
, i
);
610 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
611 &ec_validation_outputs
[i
], ec_validation_flags
);
612 event_class
->valid
= 1;
616 * Put what was not moved in
617 * bt_ctf_validation_replace_types().
619 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
623 * All field type byte orders set as "native" byte ordering can now be
624 * safely set to trace's own endianness, including the stream class'.
626 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
628 bt_ctf_stream_class_set_byte_order(stream_class
, trace
->byte_order
);
630 /* Add stream class's clock if it exists */
631 if (clock_to_add_to_trace
) {
633 bt_ctf_trace_add_clock(trace
, clock_to_add_to_trace
);
634 assert(add_clock_ret
== 0);
638 * Freeze the trace and the stream class.
640 bt_ctf_stream_class_freeze(stream_class
);
641 bt_ctf_trace_freeze(trace
);
645 bt_object_set_parent(stream_class
, NULL
);
647 if (ec_validation_outputs
) {
648 for (i
= 0; i
< event_class_count
; i
++) {
649 bt_ctf_validation_output_put_types(
650 &ec_validation_outputs
[i
]);
655 g_free(ec_validation_outputs
);
656 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
657 BT_PUT(clock_to_add_to_trace
);
658 assert(!packet_header_type
);
659 assert(!packet_context_type
);
660 assert(!event_header_type
);
661 assert(!stream_event_ctx_type
);
666 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
675 ret
= trace
->stream_classes
->len
;
680 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
681 struct bt_ctf_trace
*trace
, int index
)
683 struct bt_ctf_stream_class
*stream_class
= NULL
;
685 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
689 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
690 bt_get(stream_class
);
695 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
696 struct bt_ctf_trace
*trace
, uint32_t id
)
699 struct bt_ctf_stream_class
*stream_class
= NULL
;
705 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
706 struct bt_ctf_stream_class
*stream_class_candidate
;
708 stream_class_candidate
=
709 g_ptr_array_index(trace
->stream_classes
, i
);
711 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
713 stream_class
= stream_class_candidate
;
714 bt_get(stream_class
);
723 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
724 struct bt_ctf_trace
*trace
, const char *name
)
727 struct bt_ctf_clock
*clock
= NULL
;
729 if (!trace
|| !name
) {
733 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
734 struct bt_ctf_clock
*cur_clk
=
735 g_ptr_array_index(trace
->clocks
, i
);
736 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
742 if (!strcmp(cur_clk_name
, name
)) {
754 const char *get_byte_order_string(int byte_order
)
758 switch (byte_order
) {
774 int append_trace_metadata(struct bt_ctf_trace
*trace
,
775 struct metadata_context
*context
)
777 unsigned char *uuid
= trace
->uuid
;
780 g_string_append(context
->string
, "trace {\n");
782 g_string_append(context
->string
, "\tmajor = 1;\n");
783 g_string_append(context
->string
, "\tminor = 8;\n");
785 g_string_append_printf(context
->string
,
786 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
787 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
788 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
789 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
790 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
791 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
792 get_byte_order_string(trace
->byte_order
));
794 g_string_append(context
->string
, "\tpacket.header := ");
795 context
->current_indentation_level
++;
796 g_string_assign(context
->field_name
, "");
797 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
802 context
->current_indentation_level
--;
804 g_string_append(context
->string
, ";\n};\n\n");
810 void append_env_metadata(struct bt_ctf_trace
*trace
,
811 struct metadata_context
*context
)
816 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
822 g_string_append(context
->string
, "env {\n");
824 for (i
= 0; i
< env_size
; i
++) {
825 struct bt_value
*env_field_value_obj
= NULL
;
826 const char *entry_name
;
828 entry_name
= bt_ctf_attributes_get_field_name(
829 trace
->environment
, i
);
830 env_field_value_obj
= bt_ctf_attributes_get_field_value(
831 trace
->environment
, i
);
833 if (!entry_name
|| !env_field_value_obj
) {
837 switch (bt_value_get_type(env_field_value_obj
)) {
838 case BT_VALUE_TYPE_INTEGER
:
843 ret
= bt_value_integer_get(env_field_value_obj
,
850 g_string_append_printf(context
->string
,
851 "\t%s = %" PRId64
";\n", entry_name
,
855 case BT_VALUE_TYPE_STRING
:
858 const char *str_value
;
859 char *escaped_str
= NULL
;
861 ret
= bt_value_string_get(env_field_value_obj
,
868 escaped_str
= g_strescape(str_value
, NULL
);
874 g_string_append_printf(context
->string
,
875 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
885 BT_PUT(env_field_value_obj
);
888 g_string_append(context
->string
, "};\n\n");
891 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
893 char *metadata
= NULL
;
894 struct metadata_context
*context
= NULL
;
902 context
= g_new0(struct metadata_context
, 1);
907 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
908 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
909 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
910 if (append_trace_metadata(trace
, context
)) {
913 append_env_metadata(trace
, context
);
914 g_ptr_array_foreach(trace
->clocks
,
915 (GFunc
)bt_ctf_clock_serialize
, context
);
917 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
918 err
= bt_ctf_stream_class_serialize(
919 trace
->stream_classes
->pdata
[i
], context
);
925 metadata
= context
->string
->str
;
927 g_string_free(context
->string
, err
? TRUE
: FALSE
);
928 g_string_free(context
->field_name
, TRUE
);
934 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
936 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
942 switch (trace
->byte_order
) {
944 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
947 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
956 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
957 enum bt_ctf_byte_order byte_order
)
960 int internal_byte_order
;
962 if (!trace
|| trace
->frozen
) {
967 switch (byte_order
) {
968 case BT_CTF_BYTE_ORDER_NATIVE
:
970 * This doesn't make sense since the CTF specification defines
971 * the "native" byte order as "the byte order described in the
972 * trace description". However, this behavior had been
973 * implemented as part of v1.2 and is kept to maintain
976 * This may be changed on a major version bump only.
978 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
979 LITTLE_ENDIAN
: BIG_ENDIAN
;
981 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
982 internal_byte_order
= LITTLE_ENDIAN
;
984 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
985 case BT_CTF_BYTE_ORDER_NETWORK
:
986 internal_byte_order
= BIG_ENDIAN
;
993 trace
->byte_order
= internal_byte_order
;
998 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
999 struct bt_ctf_trace
*trace
)
1001 struct bt_ctf_field_type
*field_type
= NULL
;
1007 bt_get(trace
->packet_header_type
);
1008 field_type
= trace
->packet_header_type
;
1013 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1014 struct bt_ctf_field_type
*packet_header_type
)
1018 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
1023 /* packet_header_type must be a structure */
1024 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
1025 BT_CTF_TYPE_ID_STRUCT
) {
1030 bt_get(packet_header_type
);
1031 bt_put(trace
->packet_header_type
);
1032 trace
->packet_header_type
= packet_header_type
;
1038 int get_stream_class_count(void *element
)
1040 return bt_ctf_trace_get_stream_class_count(
1041 (struct bt_ctf_trace
*) element
);
1045 void *get_stream_class(void *element
, int i
)
1047 return bt_ctf_trace_get_stream_class(
1048 (struct bt_ctf_trace
*) element
, i
);
1052 int visit_stream_class(void *element
, bt_ctf_ir_visitor visitor
,void *data
)
1054 return bt_ctf_stream_class_visit(element
, visitor
, data
);
1057 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1058 bt_ctf_ir_visitor visitor
, void *data
)
1061 struct bt_ctf_ir_element element
=
1062 { .element
= trace
, .type
= BT_CTF_IR_TYPE_TRACE
};
1064 if (!trace
|| !visitor
) {
1069 ret
= visitor_helper(&element
, get_stream_class_count
,
1070 get_stream_class
, visit_stream_class
, visitor
, data
);
1076 int ir_visitor(struct bt_ctf_ir_element
*element
, void *data
)
1080 switch (element
->type
) {
1081 case BT_CTF_IR_TYPE_TRACE
:
1085 case BT_CTF_IR_TYPE_STREAM_CLASS
:
1089 case BT_CTF_IR_TYPE_EVENT_CLASS
:
1102 int bt_ctf_trace_add_notification_handler_cb(struct bt_ctf_trace
*trace
,
1103 bt_ctf_notification_cb handler
, void *handler_data
)
1106 struct notification_handler
*handler_wrapper
=
1107 g_new0(struct notification_handler
, 1);
1109 if (!trace
|| !handler
|| !handler_wrapper
) {
1114 handler_wrapper
->func
= handler
;
1115 handler_wrapper
->data
= handler_data
;
1117 /* Emit notifications describing the current schema. */
1118 ret
= bt_ctf_trace_visit(trace
, ir_visitor
, handler_wrapper
);
1123 g_ptr_array_add(trace
->notification_handlers
, handler_wrapper
);
1126 g_free(handler_wrapper
);
1131 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
1134 unsigned int alignment
, size
;
1135 struct bt_ctf_field_type
*field_type
= NULL
;
1137 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
1141 alignment
= field_type_aliases_alignments
[alias
];
1142 size
= field_type_aliases_sizes
[alias
];
1143 field_type
= bt_ctf_field_type_integer_create(size
);
1144 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
1153 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
1157 bt_ctf_field_type_freeze(trace
->packet_header_type
);
1158 bt_ctf_attributes_freeze(trace
->environment
);
1160 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1161 struct bt_ctf_clock
*clock
=
1162 g_ptr_array_index(trace
->clocks
, i
);
1164 bt_ctf_clock_freeze(clock
);
1171 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
1174 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
1175 struct bt_ctf_field_type
*_uint32_t
=
1176 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1177 struct bt_ctf_field_type
*_uint8_t
=
1178 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1179 struct bt_ctf_field_type
*trace_packet_header_type
=
1180 bt_ctf_field_type_structure_create();
1181 struct bt_ctf_field_type
*uuid_array_type
=
1182 bt_ctf_field_type_array_create(_uint8_t
, 16);
1184 if (!trace_packet_header_type
|| !uuid_array_type
) {
1189 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1190 _uint32_t
, "magic");
1195 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1196 uuid_array_type
, "uuid");
1201 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
1202 _uint32_t
, "stream_id");
1207 ret
= bt_ctf_trace_set_packet_header_type(trace
,
1208 trace_packet_header_type
);
1213 bt_put(uuid_array_type
);
1218 bt_put(trace_packet_header_type
);