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
);
49 const unsigned int field_type_aliases_alignments
[] = {
50 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
51 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
52 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
53 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
57 const unsigned int field_type_aliases_sizes
[] = {
58 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
59 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
60 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
61 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
62 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
63 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
67 void put_stream_class(struct bt_ctf_stream_class
*stream_class
)
69 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
70 bt_ctf_stream_class_put(stream_class
);
73 struct bt_ctf_trace
*bt_ctf_trace_create(void)
75 struct bt_ctf_trace
*trace
= NULL
;
77 trace
= g_new0(struct bt_ctf_trace
, 1);
82 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
83 bt_ctf_ref_init(&trace
->ref_count
);
84 trace
->clocks
= g_ptr_array_new_with_free_func(
85 (GDestroyNotify
) bt_ctf_clock_put
);
86 trace
->streams
= g_ptr_array_new_with_free_func(
87 (GDestroyNotify
) bt_ctf_stream_put
);
88 trace
->stream_classes
= g_ptr_array_new_with_free_func(
89 (GDestroyNotify
) put_stream_class
);
90 if (!trace
->clocks
|| !trace
->stream_classes
|| !trace
->streams
) {
94 /* Generate a trace UUID */
95 uuid_generate(trace
->uuid
);
96 if (init_trace_packet_header(trace
)) {
100 /* Create the environment array object */
101 trace
->environment
= bt_ctf_attributes_create();
102 if (!trace
->environment
) {
109 bt_ctf_trace_destroy(&trace
->ref_count
);
115 void bt_ctf_trace_destroy(struct bt_ctf_ref
*ref
)
117 struct bt_ctf_trace
*trace
;
123 trace
= container_of(ref
, struct bt_ctf_trace
, ref_count
);
124 if (trace
->environment
) {
125 bt_ctf_attributes_destroy(trace
->environment
);
129 g_ptr_array_free(trace
->clocks
, TRUE
);
132 if (trace
->streams
) {
133 g_ptr_array_free(trace
->streams
, TRUE
);
136 if (trace
->stream_classes
) {
137 g_ptr_array_free(trace
->stream_classes
, TRUE
);
140 bt_ctf_field_type_put(trace
->packet_header_type
);
144 struct bt_ctf_stream
*bt_ctf_trace_create_stream(struct bt_ctf_trace
*trace
,
145 struct bt_ctf_stream_class
*stream_class
)
148 int stream_class_found
= 0;
150 struct bt_ctf_stream
*stream
= NULL
;
152 if (!trace
|| !stream_class
) {
156 stream
= bt_ctf_stream_create(stream_class
, trace
);
161 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
162 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
163 stream_class_found
= 1;
167 if (!stream_class_found
) {
168 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
174 bt_ctf_stream_get(stream
);
175 g_ptr_array_add(trace
->streams
, stream
);
179 bt_ctf_stream_put(stream
);
183 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
184 const char *name
, struct bt_object
*value
)
188 if (!trace
|| trace
->frozen
|| !name
|| !value
||
189 bt_ctf_validate_identifier(name
) ||
190 !(bt_object_is_integer(value
) || bt_object_is_string(value
))) {
195 if (strchr(name
, ' ')) {
200 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
207 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
208 const char *name
, const char *value
)
211 struct bt_object
*env_value_string_obj
= NULL
;
213 if (!trace
|| !name
|| !value
) {
218 env_value_string_obj
= bt_object_string_create_init(value
);
220 if (!env_value_string_obj
) {
225 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
226 env_value_string_obj
);
229 BT_OBJECT_PUT(env_value_string_obj
);
234 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
235 const char *name
, int64_t value
)
238 struct bt_object
*env_value_integer_obj
= NULL
;
240 if (!trace
|| !name
) {
245 env_value_integer_obj
= bt_object_integer_create_init(value
);
246 if (!env_value_integer_obj
) {
251 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
252 env_value_integer_obj
);
255 BT_OBJECT_PUT(env_value_integer_obj
);
260 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
269 ret
= bt_ctf_attributes_get_count(trace
->environment
);
276 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace
*trace
,
279 const char *ret
= NULL
;
285 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
291 struct bt_object
*bt_ctf_trace_get_environment_field_value(
292 struct bt_ctf_trace
*trace
, int index
)
294 struct bt_object
*ret
= NULL
;
300 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
306 struct bt_object
*bt_ctf_trace_get_environment_field_value_by_name(
307 struct bt_ctf_trace
*trace
, const char *name
)
309 struct bt_object
*ret
= NULL
;
311 if (!trace
|| !name
) {
315 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
322 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
323 struct bt_ctf_clock
*clock
)
326 struct search_query query
= { .value
= clock
, .found
= 0 };
328 if (!trace
|| !clock
) {
333 /* Check for duplicate clocks */
334 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
340 bt_ctf_clock_get(clock
);
341 g_ptr_array_add(trace
->clocks
, clock
);
346 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
354 ret
= trace
->clocks
->len
;
359 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
362 struct bt_ctf_clock
*clock
= NULL
;
364 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
368 clock
= g_ptr_array_index(trace
->clocks
, index
);
369 bt_ctf_clock_get(clock
);
374 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
375 struct bt_ctf_stream_class
*stream_class
)
380 if (!trace
|| !stream_class
) {
385 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
386 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
392 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
394 stream_id
= trace
->next_stream_id
++;
396 /* Try to assign a new stream id */
397 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
398 if (stream_id
== bt_ctf_stream_class_get_id(
399 trace
->stream_classes
->pdata
[i
])) {
400 /* Duplicate stream id found */
406 if (_bt_ctf_stream_class_set_id(stream_class
,
408 /* TODO Should retry with a different stream id */
414 /* Set weak reference to trace in stream class */
415 ret
= bt_ctf_stream_class_set_trace(stream_class
, trace
);
417 /* Stream class already part of another trace */
421 bt_ctf_stream_class_get(stream_class
);
422 g_ptr_array_add(trace
->stream_classes
, stream_class
);
425 * Freeze the trace and its packet header.
427 * All field type byte orders set as "native" byte ordering can now be
428 * safely set to trace's own endianness, including the stream class'.
430 bt_ctf_field_type_set_native_byte_order(trace
->packet_header_type
,
432 ret
= bt_ctf_stream_class_set_byte_order(stream_class
,
433 trace
->byte_order
== LITTLE_ENDIAN
?
434 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
: BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
438 bt_ctf_stream_class_freeze(stream_class
);
440 bt_ctf_attributes_freeze(trace
->environment
);
444 (void) bt_ctf_stream_class_set_trace(stream_class
, NULL
);
449 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
458 ret
= trace
->stream_classes
->len
;
463 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class(
464 struct bt_ctf_trace
*trace
, int index
)
466 struct bt_ctf_stream_class
*stream_class
= NULL
;
468 if (!trace
|| index
< 0 || index
>= trace
->stream_classes
->len
) {
472 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
473 bt_ctf_stream_class_get(stream_class
);
478 struct bt_ctf_clock
*bt_ctf_trace_get_clock_by_name(
479 struct bt_ctf_trace
*trace
, const char *name
)
482 struct bt_ctf_clock
*clock
= NULL
;
484 if (!trace
|| !name
) {
488 for (i
= 0; i
< trace
->clocks
->len
; ++i
) {
489 struct bt_ctf_clock
*cur_clk
=
490 g_ptr_array_index(trace
->clocks
, i
);
491 const char *cur_clk_name
= bt_ctf_clock_get_name(cur_clk
);
497 if (!strcmp(cur_clk_name
, name
)) {
499 bt_ctf_clock_get(clock
);
509 const char *get_byte_order_string(int byte_order
)
513 switch (byte_order
) {
529 int append_trace_metadata(struct bt_ctf_trace
*trace
,
530 struct metadata_context
*context
)
532 unsigned char *uuid
= trace
->uuid
;
535 g_string_append(context
->string
, "trace {\n");
537 g_string_append(context
->string
, "\tmajor = 1;\n");
538 g_string_append(context
->string
, "\tminor = 8;\n");
540 g_string_append_printf(context
->string
,
541 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
542 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
543 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
544 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
545 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
546 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
547 get_byte_order_string(trace
->byte_order
));
549 g_string_append(context
->string
, "\tpacket.header := ");
550 context
->current_indentation_level
++;
551 g_string_assign(context
->field_name
, "");
552 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
557 context
->current_indentation_level
--;
559 g_string_append(context
->string
, ";\n};\n\n");
565 void append_env_metadata(struct bt_ctf_trace
*trace
,
566 struct metadata_context
*context
)
571 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
577 g_string_append(context
->string
, "env {\n");
579 for (i
= 0; i
< env_size
; ++i
) {
580 struct bt_object
*env_field_value_obj
= NULL
;
581 const char *entry_name
;
583 entry_name
= bt_ctf_attributes_get_field_name(
584 trace
->environment
, i
);
585 env_field_value_obj
= bt_ctf_attributes_get_field_value(
586 trace
->environment
, i
);
588 if (!entry_name
|| !env_field_value_obj
) {
592 switch (bt_object_get_type(env_field_value_obj
)) {
593 case BT_OBJECT_TYPE_INTEGER
:
598 ret
= bt_object_integer_get(env_field_value_obj
,
605 g_string_append_printf(context
->string
,
606 "\t%s = %" PRId64
";\n", entry_name
,
610 case BT_OBJECT_TYPE_STRING
:
613 const char *str_value
;
614 char *escaped_str
= NULL
;
616 ret
= bt_object_string_get(env_field_value_obj
,
623 escaped_str
= g_strescape(str_value
, NULL
);
629 g_string_append_printf(context
->string
,
630 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
640 BT_OBJECT_PUT(env_field_value_obj
);
643 g_string_append(context
->string
, "};\n\n");
646 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
648 char *metadata
= NULL
;
649 struct metadata_context
*context
= NULL
;
657 context
= g_new0(struct metadata_context
, 1);
662 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
663 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
664 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
665 if (append_trace_metadata(trace
, context
)) {
668 append_env_metadata(trace
, context
);
669 g_ptr_array_foreach(trace
->clocks
,
670 (GFunc
)bt_ctf_clock_serialize
, context
);
672 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
673 err
= bt_ctf_stream_class_serialize(
674 trace
->stream_classes
->pdata
[i
], context
);
680 metadata
= context
->string
->str
;
682 g_string_free(context
->string
, err
? TRUE
: FALSE
);
683 g_string_free(context
->field_name
, TRUE
);
689 enum bt_ctf_byte_order
bt_ctf_trace_get_byte_order(struct bt_ctf_trace
*trace
)
691 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
697 switch (trace
->byte_order
) {
699 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
702 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
711 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
712 enum bt_ctf_byte_order byte_order
)
715 int internal_byte_order
;
717 if (!trace
|| trace
->frozen
) {
722 switch (byte_order
) {
723 case BT_CTF_BYTE_ORDER_NATIVE
:
725 * This doesn't make sense since the CTF specification defines
726 * the "native" byte order as "the byte order described in the
727 * trace description". However, this behavior had been
728 * implemented as part of v1.2 and is kept to maintain
731 * This may be changed on a major version bump only.
733 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
734 LITTLE_ENDIAN
: BIG_ENDIAN
;
736 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
737 internal_byte_order
= LITTLE_ENDIAN
;
739 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
740 case BT_CTF_BYTE_ORDER_NETWORK
:
741 internal_byte_order
= BIG_ENDIAN
;
748 trace
->byte_order
= internal_byte_order
;
753 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
754 struct bt_ctf_trace
*trace
)
756 struct bt_ctf_field_type
*field_type
= NULL
;
762 bt_ctf_field_type_get(trace
->packet_header_type
);
763 field_type
= trace
->packet_header_type
;
768 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
769 struct bt_ctf_field_type
*packet_header_type
)
773 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
778 /* packet_header_type must be a structure */
779 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
785 bt_ctf_field_type_get(packet_header_type
);
786 bt_ctf_field_type_put(trace
->packet_header_type
);
787 trace
->packet_header_type
= packet_header_type
;
792 void bt_ctf_trace_get(struct bt_ctf_trace
*trace
)
798 bt_ctf_ref_get(&trace
->ref_count
);
801 void bt_ctf_trace_put(struct bt_ctf_trace
*trace
)
807 bt_ctf_ref_put(&trace
->ref_count
, bt_ctf_trace_destroy
);
811 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
813 unsigned int alignment
, size
;
814 struct bt_ctf_field_type
*field_type
;
816 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
820 alignment
= field_type_aliases_alignments
[alias
];
821 size
= field_type_aliases_sizes
[alias
];
822 field_type
= bt_ctf_field_type_integer_create(size
);
823 bt_ctf_field_type_set_alignment(field_type
, alignment
);
828 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
831 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
832 struct bt_ctf_field_type
*_uint32_t
=
833 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
834 struct bt_ctf_field_type
*_uint8_t
=
835 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
836 struct bt_ctf_field_type
*trace_packet_header_type
=
837 bt_ctf_field_type_structure_create();
838 struct bt_ctf_field_type
*uuid_array_type
=
839 bt_ctf_field_type_array_create(_uint8_t
, 16);
841 if (!trace_packet_header_type
|| !uuid_array_type
) {
846 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
852 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
853 uuid_array_type
, "uuid");
858 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
859 _uint32_t
, "stream_id");
864 ret
= bt_ctf_trace_set_packet_header_type(trace
,
865 trace_packet_header_type
);
870 bt_ctf_field_type_put(uuid_array_type
);
871 bt_ctf_field_type_put(_uint32_t
);
872 bt_ctf_field_type_put(_uint8_t
);
873 bt_ctf_field_put(magic
);
874 bt_ctf_field_put(uuid_array
);
875 bt_ctf_field_type_put(trace_packet_header_type
);