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/utils.h>
36 #include <babeltrace/compiler.h>
38 #define DEFAULT_IDENTIFIER_SIZE 128
39 #define DEFAULT_METADATA_STRING_SIZE 4096
42 void environment_variable_destroy(struct environment_variable
*var
);
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,
66 struct bt_ctf_trace
*bt_ctf_trace_create(void)
68 struct bt_ctf_trace
*trace
= NULL
;
70 trace
= g_new0(struct bt_ctf_trace
, 1);
75 bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
);
76 bt_ctf_ref_init(&trace
->ref_count
);
77 trace
->environment
= g_ptr_array_new_with_free_func(
78 (GDestroyNotify
)environment_variable_destroy
);
79 trace
->clocks
= g_ptr_array_new_with_free_func(
80 (GDestroyNotify
)bt_ctf_clock_put
);
81 trace
->streams
= g_ptr_array_new_with_free_func(
82 (GDestroyNotify
)bt_ctf_stream_put
);
83 trace
->stream_classes
= g_ptr_array_new_with_free_func(
84 (GDestroyNotify
)bt_ctf_stream_class_put
);
85 if (!trace
->environment
|| !trace
->clocks
||
86 !trace
->stream_classes
|| !trace
->streams
) {
90 /* Generate a trace UUID */
91 uuid_generate(trace
->uuid
);
92 if (init_trace_packet_header(trace
)) {
99 bt_ctf_trace_destroy(&trace
->ref_count
);
105 void bt_ctf_trace_destroy(struct bt_ctf_ref
*ref
)
107 struct bt_ctf_trace
*trace
;
113 trace
= container_of(ref
, struct bt_ctf_trace
, ref_count
);
114 if (trace
->environment
) {
115 g_ptr_array_free(trace
->environment
, TRUE
);
119 g_ptr_array_free(trace
->clocks
, TRUE
);
122 if (trace
->streams
) {
123 g_ptr_array_free(trace
->streams
, TRUE
);
126 if (trace
->stream_classes
) {
127 g_ptr_array_free(trace
->stream_classes
, TRUE
);
130 bt_ctf_field_type_put(trace
->packet_header_type
);
134 struct bt_ctf_stream
*bt_ctf_trace_create_stream(struct bt_ctf_trace
*trace
,
135 struct bt_ctf_stream_class
*stream_class
)
138 int stream_class_found
= 0;
140 struct bt_ctf_stream
*stream
= NULL
;
142 if (!trace
|| !stream_class
) {
146 ret
= bt_ctf_stream_class_set_byte_order(stream_class
,
147 trace
->byte_order
== LITTLE_ENDIAN
?
148 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
: BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
153 stream
= bt_ctf_stream_create(stream_class
, trace
);
158 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
159 if (trace
->stream_classes
->pdata
[i
] == stream_class
) {
160 stream_class_found
= 1;
164 if (!stream_class_found
) {
165 int64_t stream_id
= bt_ctf_stream_class_get_id(stream_class
);
168 /* Try to assign a new stream id */
169 if (bt_ctf_stream_class_set_id(stream
->stream_class
,
170 trace
->next_stream_id
++)) {
175 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
176 if (stream_id
== bt_ctf_stream_class_get_id(
177 trace
->stream_classes
->pdata
[i
])) {
178 /* Duplicate stream id found */
182 bt_ctf_stream_class_get(stream
->stream_class
);
183 g_ptr_array_add(trace
->stream_classes
, stream
->stream_class
);
186 bt_ctf_stream_get(stream
);
187 g_ptr_array_add(trace
->streams
, stream
);
192 bt_ctf_stream_put(stream
);
196 int bt_ctf_trace_add_environment_field(struct bt_ctf_trace
*trace
,
200 struct environment_variable
*var
= NULL
;
201 char *escaped_value
= NULL
;
204 if (!trace
|| !name
|| !value
|| bt_ctf_validate_identifier(name
)) {
209 if (strchr(name
, ' ')) {
214 var
= g_new0(struct environment_variable
, 1);
220 escaped_value
= g_strescape(value
, NULL
);
221 if (!escaped_value
) {
226 var
->name
= g_string_new(name
);
227 var
->value
= g_string_new(escaped_value
);
228 g_free(escaped_value
);
229 if (!var
->name
|| !var
->value
) {
234 g_ptr_array_add(trace
->environment
, var
);
238 if (var
&& var
->name
) {
239 g_string_free(var
->name
, TRUE
);
242 if (var
&& var
->value
) {
243 g_string_free(var
->value
, TRUE
);
250 int bt_ctf_trace_add_clock(struct bt_ctf_trace
*trace
,
251 struct bt_ctf_clock
*clock
)
254 struct search_query query
= { .value
= clock
, .found
= 0 };
256 if (!trace
|| !clock
) {
261 /* Check for duplicate clocks */
262 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
268 bt_ctf_clock_get(clock
);
269 g_ptr_array_add(trace
->clocks
, clock
);
274 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace
*trace
)
282 ret
= trace
->clocks
->len
;
287 struct bt_ctf_clock
*bt_ctf_trace_get_clock(struct bt_ctf_trace
*trace
,
290 struct bt_ctf_clock
*clock
= NULL
;
292 if (!trace
|| index
< 0 || index
>= trace
->clocks
->len
) {
296 clock
= g_ptr_array_index(trace
->clocks
, index
);
297 bt_ctf_clock_get(clock
);
303 const char *get_byte_order_string(int byte_order
)
307 switch (byte_order
) {
323 int append_trace_metadata(struct bt_ctf_trace
*trace
,
324 struct metadata_context
*context
)
326 unsigned char *uuid
= trace
->uuid
;
329 g_string_append(context
->string
, "trace {\n");
331 g_string_append(context
->string
, "\tmajor = 1;\n");
332 g_string_append(context
->string
, "\tminor = 8;\n");
334 g_string_append_printf(context
->string
,
335 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
336 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
337 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
338 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
339 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
340 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
341 get_byte_order_string(trace
->byte_order
));
343 g_string_append(context
->string
, "\tpacket.header := ");
344 context
->current_indentation_level
++;
345 g_string_assign(context
->field_name
, "");
346 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
351 context
->current_indentation_level
--;
353 g_string_append(context
->string
, ";\n};\n\n");
359 void append_env_field_metadata(struct environment_variable
*var
,
360 struct metadata_context
*context
)
362 g_string_append_printf(context
->string
, "\t%s = \"%s\";\n",
363 var
->name
->str
, var
->value
->str
);
367 void append_env_metadata(struct bt_ctf_trace
*trace
,
368 struct metadata_context
*context
)
370 if (trace
->environment
->len
== 0) {
374 g_string_append(context
->string
, "env {\n");
375 g_ptr_array_foreach(trace
->environment
,
376 (GFunc
)append_env_field_metadata
, context
);
377 g_string_append(context
->string
, "};\n\n");
380 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
382 char *metadata
= NULL
;
383 struct metadata_context
*context
= NULL
;
391 context
= g_new0(struct metadata_context
, 1);
396 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
397 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
398 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
399 if (append_trace_metadata(trace
, context
)) {
402 append_env_metadata(trace
, context
);
403 g_ptr_array_foreach(trace
->clocks
,
404 (GFunc
)bt_ctf_clock_serialize
, context
);
406 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
407 err
= bt_ctf_stream_class_serialize(
408 trace
->stream_classes
->pdata
[i
], context
);
414 metadata
= context
->string
->str
;
416 g_string_free(context
->string
, err
? TRUE
: FALSE
);
417 g_string_free(context
->field_name
, TRUE
);
423 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace
*trace
,
424 enum bt_ctf_byte_order byte_order
)
427 int internal_byte_order
;
429 if (!trace
|| trace
->frozen
) {
434 switch (byte_order
) {
435 case BT_CTF_BYTE_ORDER_NATIVE
:
436 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
437 LITTLE_ENDIAN
: BIG_ENDIAN
;
439 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
440 internal_byte_order
= LITTLE_ENDIAN
;
442 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
443 case BT_CTF_BYTE_ORDER_NETWORK
:
444 internal_byte_order
= BIG_ENDIAN
;
451 trace
->byte_order
= internal_byte_order
;
452 if (trace
->packet_header_type
) {
453 init_trace_packet_header(trace
);
459 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
460 struct bt_ctf_trace
*trace
)
462 struct bt_ctf_field_type
*field_type
= NULL
;
468 bt_ctf_field_type_get(trace
->packet_header_type
);
469 field_type
= trace
->packet_header_type
;
474 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
475 struct bt_ctf_field_type
*packet_header_type
)
479 if (!trace
|| !packet_header_type
|| trace
->frozen
) {
484 /* packet_header_type must be a structure */
485 if (bt_ctf_field_type_get_type_id(packet_header_type
) !=
491 bt_ctf_field_type_get(packet_header_type
);
492 bt_ctf_field_type_put(trace
->packet_header_type
);
493 trace
->packet_header_type
= packet_header_type
;
498 void bt_ctf_trace_get(struct bt_ctf_trace
*trace
)
504 bt_ctf_ref_get(&trace
->ref_count
);
507 void bt_ctf_trace_put(struct bt_ctf_trace
*trace
)
513 bt_ctf_ref_put(&trace
->ref_count
, bt_ctf_trace_destroy
);
517 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
519 unsigned int alignment
, size
;
520 struct bt_ctf_field_type
*field_type
;
522 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
526 alignment
= field_type_aliases_alignments
[alias
];
527 size
= field_type_aliases_sizes
[alias
];
528 field_type
= bt_ctf_field_type_integer_create(size
);
529 bt_ctf_field_type_set_alignment(field_type
, alignment
);
534 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
537 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
538 struct bt_ctf_field_type
*_uint32_t
=
539 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
540 struct bt_ctf_field_type
*_uint8_t
=
541 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
542 struct bt_ctf_field_type
*trace_packet_header_type
=
543 bt_ctf_field_type_structure_create();
544 struct bt_ctf_field_type
*uuid_array_type
=
545 bt_ctf_field_type_array_create(_uint8_t
, 16);
547 if (!trace_packet_header_type
|| !uuid_array_type
) {
552 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
,
553 (trace
->byte_order
== LITTLE_ENDIAN
?
554 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
555 BT_CTF_BYTE_ORDER_BIG_ENDIAN
));
560 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
566 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
567 uuid_array_type
, "uuid");
572 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
573 _uint32_t
, "stream_id");
578 bt_ctf_field_type_put(trace
->packet_header_type
);
579 trace
->packet_header_type
= trace_packet_header_type
;
581 bt_ctf_field_type_put(uuid_array_type
);
582 bt_ctf_field_type_put(_uint32_t
);
583 bt_ctf_field_type_put(_uint8_t
);
584 bt_ctf_field_put(magic
);
585 bt_ctf_field_put(uuid_array
);
587 bt_ctf_field_type_put(trace_packet_header_type
);
594 void environment_variable_destroy(struct environment_variable
*var
)
596 g_string_free(var
->name
, TRUE
);
597 g_string_free(var
->value
, TRUE
);