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-writer/functor-internal.h>
34 #include <babeltrace/ctf-ir/event-types-internal.h>
35 #include <babeltrace/ctf-ir/attributes-internal.h>
36 #include <babeltrace/ctf-ir/visitor-internal.h>
37 #include <babeltrace/ctf-ir/utils.h>
38 #include <babeltrace/ctf-ir/ref.h>
39 #include <babeltrace/ctf-ir/common-internal.h>
40 #include <babeltrace/compiler.h>
41 #include <babeltrace/values.h>
43 #define DEFAULT_IDENTIFIER_SIZE 128
44 #define DEFAULT_METADATA_STRING_SIZE 4096
47 void bt_ctf_trace_destroy(struct bt_ref
*ref
);
49 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
51 int bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
54 const unsigned int field_type_aliases_alignments
[] = {
55 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
56 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
57 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
58 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
62 const unsigned int field_type_aliases_sizes
[] = {
63 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
64 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
65 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
66 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
67 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
68 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
72 void put_stream_class(struct bt_ctf_stream_class
*stream_class
)
74 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
75 bt_ctf_stream_class_put(stream_class
);
78 struct bt_ctf_trace
*bt_ctf_trace_create(void)
80 struct bt_ctf_trace
*trace
= NULL
;
82 trace
= g_new0(struct bt_ctf_trace
, 1);
87 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
88 bt_ctf_base_init(trace
, bt_ctf_trace_destroy
);
89 trace
->clocks
= g_ptr_array_new_with_free_func(
90 (GDestroyNotify
) bt_ctf_clock_put
);
91 trace
->streams
= g_ptr_array_new_with_free_func(
92 (GDestroyNotify
) bt_ctf_stream_put
);
93 trace
->stream_classes
= g_ptr_array_new_with_free_func(
94 (GDestroyNotify
) put_stream_class
);
95 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
99 /* Generate a trace UUID */
100 uuid_generate(trace
->uuid
);
101 if (init_trace_packet_header(trace
)) {
105 /* Create the environment array object */
106 trace
->environment
= bt_ctf_attributes_create();
107 if (!trace
->environment
) {
114 bt_ctf_trace_destroy(&trace
->base
.ref_count
);
120 void bt_ctf_trace_destroy(struct bt_ref
*ref
)
122 struct bt_ctf_trace
*trace
;
123 struct bt_ctf_base
*base
;
129 base
= container_of(ref
, struct bt_ctf_base
, ref_count
);
130 trace
= container_of(base
, struct bt_ctf_trace
, base
);
131 if (trace
->environment
) {
132 bt_ctf_attributes_destroy(trace
->environment
);
136 g_ptr_array_free(trace
->clocks
, TRUE
);
139 if (trace
->streams
) {
140 g_ptr_array_free(trace
->streams
, TRUE
);
143 if (trace
->stream_classes
) {
144 g_ptr_array_free(trace
->stream_classes
, TRUE
);
147 bt_ctf_field_type_put(trace
->packet_header_type
);
151 struct bt_ctf_stream
*bt_ctf_trace_create_stream(struct bt_ctf_trace
*trace
,
152 struct bt_ctf_stream_class
*stream_class
)
155 int stream_class_found
= 0;
157 struct bt_ctf_stream
*stream
= NULL
;
159 if (!trace
|| !stream_class
) {
163 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
164 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
165 stream_class_found
= 1;
169 if (!stream_class_found
) {
170 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
176 stream
= bt_ctf_stream_create(stream_class
, trace
);
181 bt_ctf_stream_get(stream
);
182 g_ptr_array_add(trace
->streams
, stream
);
186 bt_ctf_stream_put(stream
);
190 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
191 const char *name
, struct bt_value
*value
)
195 if (!trace
|| !name
|| !value
||
196 bt_ctf_validate_identifier(name
) ||
197 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
202 if (strchr(name
, ' ')) {
209 * New environment fields may be added to a frozen trace,
210 * but existing fields may not be changed.
212 * The object passed is frozen like all other attributes.
214 struct bt_value
*attribute
=
215 bt_ctf_attributes_get_field_value_by_name(
216 trace
->environment
, name
);
219 BT_VALUE_PUT(attribute
);
224 bt_value_freeze(value
);
227 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
234 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
235 const char *name
, const char *value
)
238 struct bt_value
*env_value_string_obj
= NULL
;
240 if (!trace
|| !name
|| !value
) {
247 * New environment fields may be added to a frozen trace,
248 * but existing fields may not be changed.
250 struct bt_value
*attribute
=
251 bt_ctf_attributes_get_field_value_by_name(
252 trace
->environment
, name
);
255 BT_VALUE_PUT(attribute
);
261 env_value_string_obj
= bt_value_string_create_init(value
);
263 if (!env_value_string_obj
) {
269 bt_value_freeze(env_value_string_obj
);
271 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
272 env_value_string_obj
);
275 BT_VALUE_PUT(env_value_string_obj
);
280 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
281 const char *name
, int64_t value
)
284 struct bt_value
*env_value_integer_obj
= NULL
;
286 if (!trace
|| !name
) {
293 * New environment fields may be added to a frozen trace,
294 * but existing fields may not be changed.
296 struct bt_value
*attribute
=
297 bt_ctf_attributes_get_field_value_by_name(
298 trace
->environment
, name
);
301 BT_VALUE_PUT(attribute
);
307 env_value_integer_obj
= bt_value_integer_create_init(value
);
308 if (!env_value_integer_obj
) {
313 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
314 env_value_integer_obj
);
316 bt_value_freeze(env_value_integer_obj
);
319 BT_VALUE_PUT(env_value_integer_obj
);
324 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
333 ret
= bt_ctf_attributes_get_count(trace
->environment
);
340 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
343 const char *ret
= NULL
;
349 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
355 struct bt_value
*bt_ctf_trace_get_environment_field_value(
356 struct bt_ctf_trace
*trace
, int index
)
358 struct bt_value
*ret
= NULL
;
364 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
370 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
371 struct bt_ctf_trace
*trace
, const char *name
)
373 struct bt_value
*ret
= NULL
;
375 if (!trace
|| !name
) {
379 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
386 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
387 struct bt_ctf_clock
*clock
)
390 struct search_query query
= { .value
= clock
, .found
= 0 };
392 if (!trace
|| !clock
) {
397 /* Check for duplicate clocks */
398 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
404 bt_ctf_clock_get(clock
);
405 g_ptr_array_add(trace
->clocks
, clock
);
410 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
418 ret
= trace
->clocks
->len
;
423 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
426 struct bt_ctf_clock
*clock
= NULL
;
428 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
432 clock
= g_ptr_array_index(trace
->clocks
, index
);
433 bt_ctf_clock_get(clock
);
438 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
439 struct bt_ctf_stream_class
*stream_class
)
444 if (!trace
|| !stream_class
) {
449 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
450 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
451 /* Stream already registered to the trace */
457 ret
= bt_ctf_stream_class_resolve_types(stream_class
, trace
);
462 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
464 stream_id
= trace
->next_stream_id
++;
466 /* Try to assign a new stream id */
467 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
468 if (stream_id
== bt_ctf_stream_class_get_id(
469 trace
->stream_classes
->pdata
[i
])) {
470 /* Duplicate stream id found */
476 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
478 /* TODO Should retry with a different stream id */
484 /* Set weak reference to trace in stream class */
485 ret
= bt_ctf_stream_class_set_trace(stream_class
, trace
);
487 /* Stream class already part of another trace */
491 bt_ctf_stream_class_get(stream_class
);
492 g_ptr_array_add(trace
->stream_classes
, stream_class
);
495 * Freeze the trace and its packet header.
497 * All field type byte orders set as "native" byte ordering can now be
498 * safely set to trace's own endianness, including the stream class'.
500 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
502 ret
= bt_ctf_stream_class_set_byte_order(stream_class
,
503 trace
->byte_order
== LITTLE_ENDIAN
?
504 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
: BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
509 bt_ctf_stream_class_freeze(stream_class
);
510 if (!trace
->frozen
) {
511 ret
= bt_ctf_trace_freeze(trace
);
516 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
521 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
530 ret
= trace
->stream_classes
->len
;
535 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
536 struct bt_ctf_trace
*trace
, int index
)
538 struct bt_ctf_stream_class
*stream_class
= NULL
;
540 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
544 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
545 bt_ctf_stream_class_get(stream_class
);
550 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
551 struct bt_ctf_trace
*trace
, const char *name
)
554 struct bt_ctf_clock
*clock
= NULL
;
556 if (!trace
|| !name
) {
560 for (i
= 0; i
< trace
->clocks
->len
; ++i
) {
561 struct bt_ctf_clock
*cur_clk
=
562 g_ptr_array_index(trace
->clocks
, i
);
563 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
569 if (!strcmp(cur_clk_name
, name
)) {
571 bt_ctf_clock_get(clock
);
581 const char *get_byte_order_string(int byte_order
)
585 switch (byte_order
) {
601 int append_trace_metadata(struct bt_ctf_trace
*trace
,
602 struct metadata_context
*context
)
604 unsigned char *uuid
= trace
->uuid
;
607 g_string_append(context
->string
, "trace {\n");
609 g_string_append(context
->string
, "\tmajor = 1;\n");
610 g_string_append(context
->string
, "\tminor = 8;\n");
612 g_string_append_printf(context
->string
,
613 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
614 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
615 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
616 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
617 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
618 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
619 get_byte_order_string(trace
->byte_order
));
621 g_string_append(context
->string
, "\tpacket.header := ");
622 context
->current_indentation_level
++;
623 g_string_assign(context
->field_name
, "");
624 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
629 context
->current_indentation_level
--;
631 g_string_append(context
->string
, ";\n};\n\n");
637 void append_env_metadata(struct bt_ctf_trace
*trace
,
638 struct metadata_context
*context
)
643 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
649 g_string_append(context
->string
, "env {\n");
651 for (i
= 0; i
< env_size
; ++i
) {
652 struct bt_value
*env_field_value_obj
= NULL
;
653 const char *entry_name
;
655 entry_name
= bt_ctf_attributes_get_field_name(
656 trace
->environment
, i
);
657 env_field_value_obj
= bt_ctf_attributes_get_field_value(
658 trace
->environment
, i
);
660 if (!entry_name
|| !env_field_value_obj
) {
664 switch (bt_value_get_type(env_field_value_obj
)) {
665 case BT_VALUE_TYPE_INTEGER
:
670 ret
= bt_value_integer_get(env_field_value_obj
,
677 g_string_append_printf(context
->string
,
678 "\t%s = %" PRId64
";\n", entry_name
,
682 case BT_VALUE_TYPE_STRING
:
685 const char *str_value
;
686 char *escaped_str
= NULL
;
688 ret
= bt_value_string_get(env_field_value_obj
,
695 escaped_str
= g_strescape(str_value
, NULL
);
701 g_string_append_printf(context
->string
,
702 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
712 BT_VALUE_PUT(env_field_value_obj
);
715 g_string_append(context
->string
, "};\n\n");
718 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
720 char *metadata
= NULL
;
721 struct metadata_context
*context
= NULL
;
729 context
= g_new0(struct metadata_context
, 1);
734 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
735 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
736 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
737 if (append_trace_metadata(trace
, context
)) {
740 append_env_metadata(trace
, context
);
741 g_ptr_array_foreach(trace
->clocks
,
742 (GFunc
)bt_ctf_clock_serialize
, context
);
744 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
745 err
= bt_ctf_stream_class_serialize(
746 trace
->stream_classes
->pdata
[i
], context
);
752 metadata
= context
->string
->str
;
754 g_string_free(context
->string
, err
? TRUE
: FALSE
);
755 g_string_free(context
->field_name
, TRUE
);
761 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
763 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
769 switch (trace
->byte_order
) {
771 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
774 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
783 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
784 enum bt_ctf_byte_order byte_order
)
787 int internal_byte_order
;
789 if (!trace
|| trace
->frozen
) {
794 switch (byte_order
) {
795 case BT_CTF_BYTE_ORDER_NATIVE
:
797 * This doesn't make sense since the CTF specification defines
798 * the "native" byte order as "the byte order described in the
799 * trace description". However, this behavior had been
800 * implemented as part of v1.2 and is kept to maintain
803 * This may be changed on a major version bump only.
805 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
806 LITTLE_ENDIAN
: BIG_ENDIAN
;
808 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
809 internal_byte_order
= LITTLE_ENDIAN
;
811 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
812 case BT_CTF_BYTE_ORDER_NETWORK
:
813 internal_byte_order
= BIG_ENDIAN
;
820 trace
->byte_order
= internal_byte_order
;
825 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
826 struct bt_ctf_trace
*trace
)
828 struct bt_ctf_field_type
*field_type
= NULL
;
834 bt_ctf_field_type_get(trace
->packet_header_type
);
835 field_type
= trace
->packet_header_type
;
840 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
841 struct bt_ctf_field_type
*packet_header_type
)
845 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
850 /* packet_header_type must be a structure */
851 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
857 bt_ctf_field_type_get(packet_header_type
);
858 bt_ctf_field_type_put(trace
->packet_header_type
);
859 trace
->packet_header_type
= packet_header_type
;
864 void bt_ctf_trace_get(struct bt_ctf_trace
*trace
)
869 void bt_ctf_trace_put(struct bt_ctf_trace
*trace
)
876 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
878 unsigned int alignment
, size
;
879 struct bt_ctf_field_type
*field_type
= NULL
;
881 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
885 alignment
= field_type_aliases_alignments
[alias
];
886 size
= field_type_aliases_sizes
[alias
];
887 field_type
= bt_ctf_field_type_integer_create(size
);
888 bt_ctf_field_type_set_alignment(field_type
, alignment
);
894 int bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
898 ret
= bt_ctf_trace_resolve_types(trace
);
903 bt_ctf_attributes_freeze(trace
->environment
);
910 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
913 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
914 struct bt_ctf_field_type
*_uint32_t
=
915 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
916 struct bt_ctf_field_type
*_uint8_t
=
917 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
918 struct bt_ctf_field_type
*trace_packet_header_type
=
919 bt_ctf_field_type_structure_create();
920 struct bt_ctf_field_type
*uuid_array_type
=
921 bt_ctf_field_type_array_create(_uint8_t
, 16);
923 if (!trace_packet_header_type
|| !uuid_array_type
) {
928 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
934 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
935 uuid_array_type
, "uuid");
940 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
941 _uint32_t
, "stream_id");
946 ret
= bt_ctf_trace_set_packet_header_type(trace
,
947 trace_packet_header_type
);
952 bt_ctf_field_type_put(uuid_array_type
);
953 bt_ctf_field_type_put(_uint32_t
);
954 bt_ctf_field_type_put(_uint8_t
);
955 bt_ctf_field_put(magic
);
956 bt_ctf_field_put(uuid_array
);
957 bt_ctf_field_type_put(trace_packet_header_type
);