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/compiler.h>
39 #include <babeltrace/values.h>
40 #include <babeltrace/ref.h>
42 #define DEFAULT_IDENTIFIER_SIZE 128
43 #define DEFAULT_METADATA_STRING_SIZE 4096
46 void bt_ctf_trace_destroy(struct bt_object
*obj
);
48 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
50 int bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
53 const unsigned int field_type_aliases_alignments
[] = {
54 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
55 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
56 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
57 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
61 const unsigned int field_type_aliases_sizes
[] = {
62 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
63 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
64 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
65 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
66 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
67 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
71 void put_stream_class(struct bt_ctf_stream_class
*stream_class
)
73 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
77 struct bt_ctf_trace
*bt_ctf_trace_create(void)
79 struct bt_ctf_trace
*trace
= NULL
;
81 trace
= g_new0(struct bt_ctf_trace
, 1);
86 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
87 bt_object_init(trace
, bt_ctf_trace_destroy
);
88 trace
->clocks
= g_ptr_array_new_with_free_func(
89 (GDestroyNotify
) bt_put
);
90 trace
->streams
= g_ptr_array_new_with_free_func(
91 (GDestroyNotify
) bt_put
);
92 trace
->stream_classes
= g_ptr_array_new_with_free_func(
93 (GDestroyNotify
) put_stream_class
);
94 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
98 /* Generate a trace UUID */
99 uuid_generate(trace
->uuid
);
100 if (init_trace_packet_header(trace
)) {
104 /* Create the environment array object */
105 trace
->environment
= bt_ctf_attributes_create();
106 if (!trace
->environment
) {
117 void bt_ctf_trace_destroy(struct bt_object
*obj
)
119 struct bt_ctf_trace
*trace
;
121 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
122 if (trace
->environment
) {
123 bt_ctf_attributes_destroy(trace
->environment
);
127 g_ptr_array_free(trace
->clocks
, TRUE
);
130 if (trace
->streams
) {
131 g_ptr_array_free(trace
->streams
, TRUE
);
134 if (trace
->stream_classes
) {
135 g_ptr_array_free(trace
->stream_classes
, TRUE
);
138 bt_put(trace
->packet_header_type
);
142 struct bt_ctf_stream
*bt_ctf_trace_create_stream(struct bt_ctf_trace
*trace
,
143 struct bt_ctf_stream_class
*stream_class
)
146 int stream_class_found
= 0;
148 struct bt_ctf_stream
*stream
= NULL
;
150 if (!trace
|| !stream_class
) {
154 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
155 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
156 stream_class_found
= 1;
160 if (!stream_class_found
) {
161 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
167 stream
= bt_ctf_stream_create(stream_class
, trace
);
173 g_ptr_array_add(trace
->streams
, stream
);
181 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
182 const char *name
, struct bt_value
*value
)
186 if (!trace
|| !name
|| !value
||
187 bt_ctf_validate_identifier(name
) ||
188 !(bt_value_is_integer(value
) || bt_value_is_string(value
))) {
193 if (strchr(name
, ' ')) {
200 * New environment fields may be added to a frozen trace,
201 * but existing fields may not be changed.
203 * The object passed is frozen like all other attributes.
205 struct bt_value
*attribute
=
206 bt_ctf_attributes_get_field_value_by_name(
207 trace
->environment
, name
);
215 bt_value_freeze(value
);
218 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
225 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
226 const char *name
, const char *value
)
229 struct bt_value
*env_value_string_obj
= NULL
;
231 if (!trace
|| !name
|| !value
) {
238 * New environment fields may be added to a frozen trace,
239 * but existing fields may not be changed.
241 struct bt_value
*attribute
=
242 bt_ctf_attributes_get_field_value_by_name(
243 trace
->environment
, name
);
252 env_value_string_obj
= bt_value_string_create_init(value
);
254 if (!env_value_string_obj
) {
260 bt_value_freeze(env_value_string_obj
);
262 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
263 env_value_string_obj
);
266 BT_PUT(env_value_string_obj
);
270 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
271 const char *name
, int64_t value
)
274 struct bt_value
*env_value_integer_obj
= NULL
;
276 if (!trace
|| !name
) {
283 * New environment fields may be added to a frozen trace,
284 * but existing fields may not be changed.
286 struct bt_value
*attribute
=
287 bt_ctf_attributes_get_field_value_by_name(
288 trace
->environment
, name
);
297 env_value_integer_obj
= bt_value_integer_create_init(value
);
298 if (!env_value_integer_obj
) {
303 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
304 env_value_integer_obj
);
306 bt_value_freeze(env_value_integer_obj
);
309 BT_PUT(env_value_integer_obj
);
313 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
322 ret
= bt_ctf_attributes_get_count(trace
->environment
);
329 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
332 const char *ret
= NULL
;
338 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
344 struct bt_value
*bt_ctf_trace_get_environment_field_value(
345 struct bt_ctf_trace
*trace
, int index
)
347 struct bt_value
*ret
= NULL
;
353 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
359 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
360 struct bt_ctf_trace
*trace
, const char *name
)
362 struct bt_value
*ret
= NULL
;
364 if (!trace
|| !name
) {
368 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
375 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
376 struct bt_ctf_clock
*clock
)
379 struct search_query query
= { .value
= clock
, .found
= 0 };
381 if (!trace
|| !clock
) {
386 /* Check for duplicate clocks */
387 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
394 g_ptr_array_add(trace
->clocks
, clock
);
399 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
407 ret
= trace
->clocks
->len
;
412 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
415 struct bt_ctf_clock
*clock
= NULL
;
417 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
421 clock
= g_ptr_array_index(trace
->clocks
, index
);
427 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
428 struct bt_ctf_stream_class
*stream_class
)
433 if (!trace
|| !stream_class
) {
438 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
439 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
440 /* Stream already registered to the trace */
446 ret
= bt_ctf_stream_class_resolve_types(stream_class
, trace
);
451 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
453 stream_id
= trace
->next_stream_id
++;
455 /* Try to assign a new stream id */
456 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
457 if (stream_id
== bt_ctf_stream_class_get_id(
458 trace
->stream_classes
->pdata
[i
])) {
459 /* Duplicate stream id found */
465 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
467 /* TODO Should retry with a different stream id */
473 /* Set weak reference to trace in stream class */
474 ret
= bt_ctf_stream_class_set_trace(stream_class
, trace
);
476 /* Stream class already part of another trace */
480 bt_get(stream_class
);
481 g_ptr_array_add(trace
->stream_classes
, stream_class
);
484 * Freeze the trace and its packet header.
486 * All field type byte orders set as "native" byte ordering can now be
487 * safely set to trace's own endianness, including the stream class'.
489 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
491 ret
= bt_ctf_stream_class_set_byte_order(stream_class
,
492 trace
->byte_order
== LITTLE_ENDIAN
?
493 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
: BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
498 bt_ctf_stream_class_freeze(stream_class
);
499 if (!trace
->frozen
) {
500 ret
= bt_ctf_trace_freeze(trace
);
505 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
510 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
519 ret
= trace
->stream_classes
->len
;
524 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
525 struct bt_ctf_trace
*trace
, int index
)
527 struct bt_ctf_stream_class
*stream_class
= NULL
;
529 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
533 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
534 bt_get(stream_class
);
539 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
540 struct bt_ctf_trace
*trace
, uint32_t id
)
543 struct bt_ctf_stream_class
*stream_class
= NULL
;
549 for (i
= 0; i
< trace
->stream_classes
->len
; ++i
) {
550 struct bt_ctf_stream_class
*stream_class_candidate
;
552 stream_class_candidate
=
553 g_ptr_array_index(trace
->stream_classes
, i
);
555 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
557 stream_class
= stream_class_candidate
;
558 bt_get(stream_class
);
567 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
568 struct bt_ctf_trace
*trace
, const char *name
)
571 struct bt_ctf_clock
*clock
= NULL
;
573 if (!trace
|| !name
) {
577 for (i
= 0; i
< trace
->clocks
->len
; ++i
) {
578 struct bt_ctf_clock
*cur_clk
=
579 g_ptr_array_index(trace
->clocks
, i
);
580 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
586 if (!strcmp(cur_clk_name
, name
)) {
598 const char *get_byte_order_string(int byte_order
)
602 switch (byte_order
) {
618 int append_trace_metadata(struct bt_ctf_trace
*trace
,
619 struct metadata_context
*context
)
621 unsigned char *uuid
= trace
->uuid
;
624 g_string_append(context
->string
, "trace {\n");
626 g_string_append(context
->string
, "\tmajor = 1;\n");
627 g_string_append(context
->string
, "\tminor = 8;\n");
629 g_string_append_printf(context
->string
,
630 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
631 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
632 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
633 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
634 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
635 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
636 get_byte_order_string(trace
->byte_order
));
638 g_string_append(context
->string
, "\tpacket.header := ");
639 context
->current_indentation_level
++;
640 g_string_assign(context
->field_name
, "");
641 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
646 context
->current_indentation_level
--;
648 g_string_append(context
->string
, ";\n};\n\n");
654 void append_env_metadata(struct bt_ctf_trace
*trace
,
655 struct metadata_context
*context
)
660 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
666 g_string_append(context
->string
, "env {\n");
668 for (i
= 0; i
< env_size
; ++i
) {
669 struct bt_value
*env_field_value_obj
= NULL
;
670 const char *entry_name
;
672 entry_name
= bt_ctf_attributes_get_field_name(
673 trace
->environment
, i
);
674 env_field_value_obj
= bt_ctf_attributes_get_field_value(
675 trace
->environment
, i
);
677 if (!entry_name
|| !env_field_value_obj
) {
681 switch (bt_value_get_type(env_field_value_obj
)) {
682 case BT_VALUE_TYPE_INTEGER
:
687 ret
= bt_value_integer_get(env_field_value_obj
,
694 g_string_append_printf(context
->string
,
695 "\t%s = %" PRId64
";\n", entry_name
,
699 case BT_VALUE_TYPE_STRING
:
702 const char *str_value
;
703 char *escaped_str
= NULL
;
705 ret
= bt_value_string_get(env_field_value_obj
,
712 escaped_str
= g_strescape(str_value
, NULL
);
718 g_string_append_printf(context
->string
,
719 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
729 BT_PUT(env_field_value_obj
);
732 g_string_append(context
->string
, "};\n\n");
735 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
737 char *metadata
= NULL
;
738 struct metadata_context
*context
= NULL
;
746 context
= g_new0(struct metadata_context
, 1);
751 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
752 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
753 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
754 if (append_trace_metadata(trace
, context
)) {
757 append_env_metadata(trace
, context
);
758 g_ptr_array_foreach(trace
->clocks
,
759 (GFunc
)bt_ctf_clock_serialize
, context
);
761 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
762 err
= bt_ctf_stream_class_serialize(
763 trace
->stream_classes
->pdata
[i
], context
);
769 metadata
= context
->string
->str
;
771 g_string_free(context
->string
, err
? TRUE
: FALSE
);
772 g_string_free(context
->field_name
, TRUE
);
778 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
780 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
786 switch (trace
->byte_order
) {
788 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
791 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
800 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
801 enum bt_ctf_byte_order byte_order
)
804 int internal_byte_order
;
806 if (!trace
|| trace
->frozen
) {
811 switch (byte_order
) {
812 case BT_CTF_BYTE_ORDER_NATIVE
:
814 * This doesn't make sense since the CTF specification defines
815 * the "native" byte order as "the byte order described in the
816 * trace description". However, this behavior had been
817 * implemented as part of v1.2 and is kept to maintain
820 * This may be changed on a major version bump only.
822 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
823 LITTLE_ENDIAN
: BIG_ENDIAN
;
825 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
826 internal_byte_order
= LITTLE_ENDIAN
;
828 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
829 case BT_CTF_BYTE_ORDER_NETWORK
:
830 internal_byte_order
= BIG_ENDIAN
;
837 trace
->byte_order
= internal_byte_order
;
842 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
843 struct bt_ctf_trace
*trace
)
845 struct bt_ctf_field_type
*field_type
= NULL
;
851 bt_get(trace
->packet_header_type
);
852 field_type
= trace
->packet_header_type
;
857 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
858 struct bt_ctf_field_type
*packet_header_type
)
862 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
867 /* packet_header_type must be a structure */
868 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
874 bt_get(packet_header_type
);
875 bt_put(trace
->packet_header_type
);
876 trace
->packet_header_type
= packet_header_type
;
881 void bt_ctf_trace_get(struct bt_ctf_trace
*trace
)
886 void bt_ctf_trace_put(struct bt_ctf_trace
*trace
)
893 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
895 unsigned int alignment
, size
;
896 struct bt_ctf_field_type
*field_type
= NULL
;
898 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
902 alignment
= field_type_aliases_alignments
[alias
];
903 size
= field_type_aliases_sizes
[alias
];
904 field_type
= bt_ctf_field_type_integer_create(size
);
905 bt_ctf_field_type_set_alignment(field_type
, alignment
);
911 int bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
915 ret
= bt_ctf_trace_resolve_types(trace
);
920 bt_ctf_attributes_freeze(trace
->environment
);
927 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
930 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
931 struct bt_ctf_field_type
*_uint32_t
=
932 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
933 struct bt_ctf_field_type
*_uint8_t
=
934 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
935 struct bt_ctf_field_type
*trace_packet_header_type
=
936 bt_ctf_field_type_structure_create();
937 struct bt_ctf_field_type
*uuid_array_type
=
938 bt_ctf_field_type_array_create(_uint8_t
, 16);
940 if (!trace_packet_header_type
|| !uuid_array_type
) {
945 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
951 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
952 uuid_array_type
, "uuid");
957 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
958 _uint32_t
, "stream_id");
963 ret
= bt_ctf_trace_set_packet_header_type(trace
,
964 trace_packet_header_type
);
969 bt_put(uuid_array_type
);
974 bt_put(trace_packet_header_type
);