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/utils.h>
37 #include <babeltrace/compiler.h>
38 #include <babeltrace/objects.h>
40 #define DEFAULT_IDENTIFIER_SIZE 128
41 #define DEFAULT_METADATA_STRING_SIZE 4096
44 void bt_ctf_trace_destroy(struct bt_ctf_ref
*ref
);
46 int init_trace_packet_header(struct bt_ctf_trace
*trace
);
48 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
51 const unsigned int field_type_aliases_alignments
[] = {
52 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
53 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
54 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
55 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
59 const unsigned int field_type_aliases_sizes
[] = {
60 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
61 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
62 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
63 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
64 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
65 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
69 void put_stream_class(struct bt_ctf_stream_class
*stream_class
)
71 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
72 bt_ctf_stream_class_put(stream_class
);
75 struct bt_ctf_trace
*bt_ctf_trace_create(void)
77 struct bt_ctf_trace
*trace
= NULL
;
79 trace
= g_new0(struct bt_ctf_trace
, 1);
84 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
85 bt_ctf_ref_init(&trace
->ref_count
);
86 trace
->clocks
= g_ptr_array_new_with_free_func(
87 (GDestroyNotify
) bt_ctf_clock_put
);
88 trace
->streams
= g_ptr_array_new_with_free_func(
89 (GDestroyNotify
) bt_ctf_stream_put
);
90 trace
->stream_classes
= g_ptr_array_new_with_free_func(
91 (GDestroyNotify
) put_stream_class
);
92 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
96 /* Generate a trace UUID */
97 uuid_generate(trace
->uuid
);
98 if (init_trace_packet_header(trace
)) {
102 /* Create the environment array object */
103 trace
->environment
= bt_ctf_attributes_create();
104 if (!trace
->environment
) {
111 bt_ctf_trace_destroy(&trace
->ref_count
);
117 void bt_ctf_trace_destroy(struct bt_ctf_ref
*ref
)
119 struct bt_ctf_trace
*trace
;
125 trace
= container_of(ref
, struct bt_ctf_trace
, ref_count
);
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_ctf_field_type_put(trace
->packet_header_type
);
146 struct bt_ctf_stream
*bt_ctf_trace_create_stream(struct bt_ctf_trace
*trace
,
147 struct bt_ctf_stream_class
*stream_class
)
150 int stream_class_found
= 0;
152 struct bt_ctf_stream
*stream
= NULL
;
154 if (!trace
|| !stream_class
) {
158 stream
= bt_ctf_stream_create(stream_class
, trace
);
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 bt_ctf_stream_get(stream
);
177 g_ptr_array_add(trace
->streams
, stream
);
181 bt_ctf_stream_put(stream
);
185 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
186 const char *name
, struct bt_object
*value
)
190 if (!trace
|| trace
->frozen
|| !name
|| !value
||
191 bt_ctf_validate_identifier(name
) ||
192 !(bt_object_is_integer(value
) || bt_object_is_string(value
))) {
197 if (strchr(name
, ' ')) {
202 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
209 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
210 const char *name
, const char *value
)
213 struct bt_object
*env_value_string_obj
= NULL
;
215 if (!trace
|| !name
|| !value
) {
220 env_value_string_obj
= bt_object_string_create_init(value
);
222 if (!env_value_string_obj
) {
227 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
228 env_value_string_obj
);
231 BT_OBJECT_PUT(env_value_string_obj
);
236 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
237 const char *name
, int64_t value
)
240 struct bt_object
*env_value_integer_obj
= NULL
;
242 if (!trace
|| !name
) {
247 env_value_integer_obj
= bt_object_integer_create_init(value
);
248 if (!env_value_integer_obj
) {
253 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
254 env_value_integer_obj
);
257 BT_OBJECT_PUT(env_value_integer_obj
);
262 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
271 ret
= bt_ctf_attributes_get_count(trace
->environment
);
278 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
281 const char *ret
= NULL
;
287 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
293 struct bt_object
*bt_ctf_trace_get_environment_field_value(
294 struct bt_ctf_trace
*trace
, int index
)
296 struct bt_object
*ret
= NULL
;
302 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
308 struct bt_object
*bt_ctf_trace_get_environment_field_value_by_name(
309 struct bt_ctf_trace
*trace
, const char *name
)
311 struct bt_object
*ret
= NULL
;
313 if (!trace
|| !name
) {
317 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
324 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
325 struct bt_ctf_clock
*clock
)
328 struct search_query query
= { .value
= clock
, .found
= 0 };
330 if (!trace
|| !clock
) {
335 /* Check for duplicate clocks */
336 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
342 bt_ctf_clock_get(clock
);
343 g_ptr_array_add(trace
->clocks
, clock
);
348 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
356 ret
= trace
->clocks
->len
;
361 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
364 struct bt_ctf_clock
*clock
= NULL
;
366 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
370 clock
= g_ptr_array_index(trace
->clocks
, index
);
371 bt_ctf_clock_get(clock
);
376 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
377 struct bt_ctf_stream_class
*stream_class
)
382 if (!trace
|| !stream_class
) {
387 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
388 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
394 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
396 stream_id
= trace
->next_stream_id
++;
398 /* Try to assign a new stream id */
399 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
400 if (stream_id
== bt_ctf_stream_class_get_id(
401 trace
->stream_classes
->pdata
[i
])) {
402 /* Duplicate stream id found */
408 if (_bt_ctf_stream_class_set_id(stream_class
,
410 /* TODO Should retry with a different stream id */
416 /* Set weak reference to trace in stream class */
417 ret
= bt_ctf_stream_class_set_trace(stream_class
, trace
);
419 /* Stream class already part of another trace */
423 bt_ctf_stream_class_get(stream_class
);
424 g_ptr_array_add(trace
->stream_classes
, stream_class
);
427 * Freeze the trace and its packet header.
429 * All field type byte orders set as "native" byte ordering can now be
430 * safely set to trace's own endianness, including the stream class'.
432 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
434 ret
= bt_ctf_stream_class_set_byte_order(stream_class
,
435 trace
->byte_order
== LITTLE_ENDIAN
?
436 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
: BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
441 bt_ctf_stream_class_freeze(stream_class
);
442 bt_ctf_trace_freeze(trace
);
445 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
450 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
459 ret
= trace
->stream_classes
->len
;
464 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
465 struct bt_ctf_trace
*trace
, int index
)
467 struct bt_ctf_stream_class
*stream_class
= NULL
;
469 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
473 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
474 bt_ctf_stream_class_get(stream_class
);
479 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
480 struct bt_ctf_trace
*trace
, const char *name
)
483 struct bt_ctf_clock
*clock
= NULL
;
485 if (!trace
|| !name
) {
489 for (i
= 0; i
< trace
->clocks
->len
; ++i
) {
490 struct bt_ctf_clock
*cur_clk
=
491 g_ptr_array_index(trace
->clocks
, i
);
492 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
498 if (!strcmp(cur_clk_name
, name
)) {
500 bt_ctf_clock_get(clock
);
510 const char *get_byte_order_string(int byte_order
)
514 switch (byte_order
) {
530 int append_trace_metadata(struct bt_ctf_trace
*trace
,
531 struct metadata_context
*context
)
533 unsigned char *uuid
= trace
->uuid
;
536 g_string_append(context
->string
, "trace {\n");
538 g_string_append(context
->string
, "\tmajor = 1;\n");
539 g_string_append(context
->string
, "\tminor = 8;\n");
541 g_string_append_printf(context
->string
,
542 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
543 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
544 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
545 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
546 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
547 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
548 get_byte_order_string(trace
->byte_order
));
550 g_string_append(context
->string
, "\tpacket.header := ");
551 context
->current_indentation_level
++;
552 g_string_assign(context
->field_name
, "");
553 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
558 context
->current_indentation_level
--;
560 g_string_append(context
->string
, ";\n};\n\n");
566 void append_env_metadata(struct bt_ctf_trace
*trace
,
567 struct metadata_context
*context
)
572 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
578 g_string_append(context
->string
, "env {\n");
580 for (i
= 0; i
< env_size
; ++i
) {
581 struct bt_object
*env_field_value_obj
= NULL
;
582 const char *entry_name
;
584 entry_name
= bt_ctf_attributes_get_field_name(
585 trace
->environment
, i
);
586 env_field_value_obj
= bt_ctf_attributes_get_field_value(
587 trace
->environment
, i
);
589 if (!entry_name
|| !env_field_value_obj
) {
593 switch (bt_object_get_type(env_field_value_obj
)) {
594 case BT_OBJECT_TYPE_INTEGER
:
599 ret
= bt_object_integer_get(env_field_value_obj
,
606 g_string_append_printf(context
->string
,
607 "\t%s = %" PRId64
";\n", entry_name
,
611 case BT_OBJECT_TYPE_STRING
:
614 const char *str_value
;
615 char *escaped_str
= NULL
;
617 ret
= bt_object_string_get(env_field_value_obj
,
624 escaped_str
= g_strescape(str_value
, NULL
);
630 g_string_append_printf(context
->string
,
631 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
641 BT_OBJECT_PUT(env_field_value_obj
);
644 g_string_append(context
->string
, "};\n\n");
647 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
649 char *metadata
= NULL
;
650 struct metadata_context
*context
= NULL
;
658 context
= g_new0(struct metadata_context
, 1);
663 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
664 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
665 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
666 if (append_trace_metadata(trace
, context
)) {
669 append_env_metadata(trace
, context
);
670 g_ptr_array_foreach(trace
->clocks
,
671 (GFunc
)bt_ctf_clock_serialize
, context
);
673 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
674 err
= bt_ctf_stream_class_serialize(
675 trace
->stream_classes
->pdata
[i
], context
);
681 metadata
= context
->string
->str
;
683 g_string_free(context
->string
, err
? TRUE
: FALSE
);
684 g_string_free(context
->field_name
, TRUE
);
690 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
692 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
698 switch (trace
->byte_order
) {
700 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
703 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
712 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
713 enum bt_ctf_byte_order byte_order
)
716 int internal_byte_order
;
718 if (!trace
|| trace
->frozen
) {
723 switch (byte_order
) {
724 case BT_CTF_BYTE_ORDER_NATIVE
:
726 * This doesn't make sense since the CTF specification defines
727 * the "native" byte order as "the byte order described in the
728 * trace description". However, this behavior had been
729 * implemented as part of v1.2 and is kept to maintain
732 * This may be changed on a major version bump only.
734 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
735 LITTLE_ENDIAN
: BIG_ENDIAN
;
737 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
738 internal_byte_order
= LITTLE_ENDIAN
;
740 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
741 case BT_CTF_BYTE_ORDER_NETWORK
:
742 internal_byte_order
= BIG_ENDIAN
;
749 trace
->byte_order
= internal_byte_order
;
754 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
755 struct bt_ctf_trace
*trace
)
757 struct bt_ctf_field_type
*field_type
= NULL
;
763 bt_ctf_field_type_get(trace
->packet_header_type
);
764 field_type
= trace
->packet_header_type
;
769 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
770 struct bt_ctf_field_type
*packet_header_type
)
774 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
779 /* packet_header_type must be a structure */
780 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
786 bt_ctf_field_type_get(packet_header_type
);
787 bt_ctf_field_type_put(trace
->packet_header_type
);
788 trace
->packet_header_type
= packet_header_type
;
793 void bt_ctf_trace_get(struct bt_ctf_trace
*trace
)
799 bt_ctf_ref_get(&trace
->ref_count
);
802 void bt_ctf_trace_put(struct bt_ctf_trace
*trace
)
808 bt_ctf_ref_put(&trace
->ref_count
, bt_ctf_trace_destroy
);
812 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
814 unsigned int alignment
, size
;
815 struct bt_ctf_field_type
*field_type
;
817 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
821 alignment
= field_type_aliases_alignments
[alias
];
822 size
= field_type_aliases_sizes
[alias
];
823 field_type
= bt_ctf_field_type_integer_create(size
);
824 bt_ctf_field_type_set_alignment(field_type
, alignment
);
829 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
831 bt_ctf_attributes_freeze(trace
->environment
);
836 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
839 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
840 struct bt_ctf_field_type
*_uint32_t
=
841 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
842 struct bt_ctf_field_type
*_uint8_t
=
843 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
844 struct bt_ctf_field_type
*trace_packet_header_type
=
845 bt_ctf_field_type_structure_create();
846 struct bt_ctf_field_type
*uuid_array_type
=
847 bt_ctf_field_type_array_create(_uint8_t
, 16);
849 if (!trace_packet_header_type
|| !uuid_array_type
) {
854 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
860 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
861 uuid_array_type
, "uuid");
866 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
867 _uint32_t
, "stream_id");
872 ret
= bt_ctf_trace_set_packet_header_type(trace
,
873 trace_packet_header_type
);
878 bt_ctf_field_type_put(uuid_array_type
);
879 bt_ctf_field_type_put(_uint32_t
);
880 bt_ctf_field_type_put(_uint8_t
);
881 bt_ctf_field_put(magic
);
882 bt_ctf_field_put(uuid_array
);
883 bt_ctf_field_type_put(trace_packet_header_type
);