4 * Babeltrace CTF Writer
6 * Copyright 2013 EfficiOS Inc.
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-writer/writer.h>
30 #include <babeltrace/ctf-writer/clock.h>
31 #include <babeltrace/ctf-writer/clock-internal.h>
32 #include <babeltrace/ctf-writer/writer-internal.h>
33 #include <babeltrace/ctf-writer/event-types-internal.h>
34 #include <babeltrace/ctf-writer/event-fields-internal.h>
35 #include <babeltrace/ctf-writer/functor-internal.h>
36 #include <babeltrace/ctf-writer/stream-internal.h>
37 #include <babeltrace/ctf-writer/stream.h>
38 #include <babeltrace/compiler.h>
47 #define DEFAULT_IDENTIFIER_SIZE 128
48 #define DEFAULT_METADATA_STRING_SIZE 4096
51 void environment_variable_destroy(struct environment_variable
*var
);
53 void bt_ctf_writer_destroy(struct bt_ctf_ref
*ref
);
55 int init_trace_packet_header(struct bt_ctf_writer
*writer
);
57 int create_stream_file(struct bt_ctf_writer
*writer
,
58 struct bt_ctf_stream
*stream
);
60 void stream_flush_cb(struct bt_ctf_stream
*stream
,
61 struct bt_ctf_writer
*writer
);
64 const char * const reserved_keywords_str
[] = {"align", "callsite",
65 "const", "char", "clock", "double", "enum", "env", "event",
66 "floating_point", "float", "integer", "int", "long", "short", "signed",
67 "stream", "string", "struct", "trace", "typealias", "typedef",
68 "unsigned", "variant", "void" "_Bool", "_Complex", "_Imaginary"};
71 const unsigned int field_type_aliases_alignments
[] = {
72 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
73 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
74 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
75 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
79 const unsigned int field_type_aliases_sizes
[] = {
80 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
81 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
82 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
83 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
84 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
85 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
88 static GHashTable
*reserved_keywords_set
;
90 static int global_data_refcount
;
92 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
94 struct bt_ctf_writer
*writer
= NULL
;
100 writer
= g_new0(struct bt_ctf_writer
, 1);
105 bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_NATIVE
);
106 bt_ctf_ref_init(&writer
->ref_count
);
107 writer
->path
= g_string_new(path
);
112 /* Create trace directory if necessary and open a metadata file */
113 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
114 perror("g_mkdir_with_parents");
118 writer
->trace_dir_fd
= open(path
, O_RDONLY
| O_DIRECTORY
,
120 if (writer
->trace_dir_fd
< 0) {
125 writer
->metadata_fd
= openat(writer
->trace_dir_fd
, "metadata",
126 O_WRONLY
| O_CREAT
| O_TRUNC
,
127 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
128 writer
->environment
= g_ptr_array_new_with_free_func(
129 (GDestroyNotify
)environment_variable_destroy
);
130 writer
->clocks
= g_ptr_array_new_with_free_func(
131 (GDestroyNotify
)bt_ctf_clock_put
);
132 writer
->streams
= g_ptr_array_new_with_free_func(
133 (GDestroyNotify
)bt_ctf_stream_put
);
134 writer
->stream_classes
= g_ptr_array_new_with_free_func(
135 (GDestroyNotify
)bt_ctf_stream_class_put
);
136 if (!writer
->environment
|| !writer
->clocks
||
137 !writer
->stream_classes
|| !writer
->streams
) {
141 /* Generate a trace UUID */
142 uuid_generate(writer
->uuid
);
143 if (init_trace_packet_header(writer
)) {
149 unlinkat(writer
->trace_dir_fd
, "metadata", 0);
150 bt_ctf_writer_destroy(&writer
->ref_count
);
156 void bt_ctf_writer_destroy(struct bt_ctf_ref
*ref
)
158 struct bt_ctf_writer
*writer
;
164 writer
= container_of(ref
, struct bt_ctf_writer
, ref_count
);
165 bt_ctf_writer_flush_metadata(writer
);
167 g_string_free(writer
->path
, TRUE
);
170 if (writer
->trace_dir_fd
> 0) {
171 close(writer
->trace_dir_fd
);
174 if (writer
->metadata_fd
> 0) {
175 close(writer
->metadata_fd
);
178 if (writer
->environment
) {
179 g_ptr_array_free(writer
->environment
, TRUE
);
182 if (writer
->clocks
) {
183 g_ptr_array_free(writer
->clocks
, TRUE
);
186 if (writer
->streams
) {
187 g_ptr_array_free(writer
->streams
, TRUE
);
190 if (writer
->stream_classes
) {
191 g_ptr_array_free(writer
->stream_classes
, TRUE
);
194 bt_ctf_field_type_put(writer
->trace_packet_header_type
);
195 bt_ctf_field_put(writer
->trace_packet_header
);
199 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
200 struct bt_ctf_stream_class
*stream_class
)
203 int stream_class_found
= 0;
206 struct bt_ctf_stream
*stream
= NULL
;
208 if (!writer
|| !stream_class
) {
212 stream
= bt_ctf_stream_create(stream_class
);
217 stream_fd
= create_stream_file(writer
, stream
);
218 if (stream_fd
< 0 || bt_ctf_stream_set_fd(stream
, stream_fd
)) {
222 bt_ctf_stream_set_flush_callback(stream
, (flush_func
)stream_flush_cb
,
224 ret
= bt_ctf_stream_class_set_byte_order(stream
->stream_class
,
225 writer
->byte_order
== LITTLE_ENDIAN
?
226 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
: BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
231 for (i
= 0; i
< writer
->stream_classes
->len
; i
++) {
232 if (writer
->stream_classes
->pdata
[i
] == stream
->stream_class
) {
233 stream_class_found
= 1;
237 if (!stream_class_found
) {
238 if (bt_ctf_stream_class_set_id(stream
->stream_class
,
239 writer
->next_stream_id
++)) {
243 bt_ctf_stream_class_get(stream
->stream_class
);
244 g_ptr_array_add(writer
->stream_classes
, stream
->stream_class
);
247 bt_ctf_stream_get(stream
);
248 g_ptr_array_add(writer
->streams
, stream
);
252 bt_ctf_stream_put(stream
);
256 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
260 struct environment_variable
*var
= NULL
;
261 char *escaped_value
= NULL
;
264 if (!writer
|| !name
|| !value
|| validate_identifier(name
)) {
269 if (strchr(name
, ' ')) {
274 var
= g_new0(struct environment_variable
, 1);
280 escaped_value
= g_strescape(value
, NULL
);
281 if (!escaped_value
) {
286 var
->name
= g_string_new(name
);
287 var
->value
= g_string_new(escaped_value
);
288 g_free(escaped_value
);
289 if (!var
->name
|| !var
->value
) {
294 g_ptr_array_add(writer
->environment
, var
);
298 if (var
&& var
->name
) {
299 g_string_free(var
->name
, TRUE
);
302 if (var
&& var
->value
) {
303 g_string_free(var
->value
, TRUE
);
310 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
311 struct bt_ctf_clock
*clock
)
314 struct search_query query
= { .value
= clock
, .found
= 0 };
316 if (!writer
|| !clock
) {
321 /* Check for duplicate clocks */
322 g_ptr_array_foreach(writer
->clocks
, value_exists
, &query
);
328 bt_ctf_clock_get(clock
);
329 g_ptr_array_add(writer
->clocks
, clock
);
335 const char *get_byte_order_string(int byte_order
)
339 switch (byte_order
) {
355 void append_trace_metadata(struct bt_ctf_writer
*writer
,
356 struct metadata_context
*context
)
358 unsigned char *uuid
= writer
->uuid
;
360 g_string_append(context
->string
, "trace {\n");
362 g_string_append(context
->string
, "\tmajor = 1;\n");
363 g_string_append(context
->string
, "\tminor = 8;\n");
365 g_string_append_printf(context
->string
,
366 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
367 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
368 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
369 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
370 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
371 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
372 get_byte_order_string(writer
->byte_order
));
374 g_string_append(context
->string
, "\tpacket.header := ");
375 context
->current_indentation_level
++;
376 g_string_assign(context
->field_name
, "");
377 bt_ctf_field_type_serialize(writer
->trace_packet_header_type
, context
);
378 context
->current_indentation_level
--;
380 g_string_append(context
->string
, ";\n};\n\n");
384 void append_env_field_metadata(struct environment_variable
*var
,
385 struct metadata_context
*context
)
387 g_string_append_printf(context
->string
, "\t%s = \"%s\";\n",
388 var
->name
->str
, var
->value
->str
);
392 void append_env_metadata(struct bt_ctf_writer
*writer
,
393 struct metadata_context
*context
)
395 if (writer
->environment
->len
== 0) {
399 g_string_append(context
->string
, "env {\n");
400 g_ptr_array_foreach(writer
->environment
,
401 (GFunc
)append_env_field_metadata
, context
);
402 g_string_append(context
->string
, "};\n\n");
405 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
407 char *metadata
= NULL
;
408 struct metadata_context
*context
= NULL
;
416 context
= g_new0(struct metadata_context
, 1);
421 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
422 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
423 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
424 append_trace_metadata(writer
, context
);
425 append_env_metadata(writer
, context
);
426 g_ptr_array_foreach(writer
->clocks
,
427 (GFunc
)bt_ctf_clock_serialize
, context
);
429 for (i
= 0; i
< writer
->stream_classes
->len
; i
++) {
430 err
= bt_ctf_stream_class_serialize(
431 writer
->stream_classes
->pdata
[i
], context
);
437 metadata
= context
->string
->str
;
439 g_string_free(context
->string
, err
? TRUE
: FALSE
);
440 g_string_free(context
->field_name
, TRUE
);
446 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
449 char *metadata_string
= NULL
;
455 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
456 if (!metadata_string
) {
460 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
465 if (ftruncate(writer
->metadata_fd
, 0)) {
470 ret
= write(writer
->metadata_fd
, metadata_string
,
471 strlen(metadata_string
));
477 g_free(metadata_string
);
480 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
481 enum bt_ctf_byte_order byte_order
)
484 int internal_byte_order
;
486 if (!writer
|| writer
->frozen
) {
491 switch (byte_order
) {
492 case BT_CTF_BYTE_ORDER_NATIVE
:
493 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
) ?
494 LITTLE_ENDIAN
: BIG_ENDIAN
;
496 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
497 internal_byte_order
= LITTLE_ENDIAN
;
499 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
500 case BT_CTF_BYTE_ORDER_NETWORK
:
501 internal_byte_order
= BIG_ENDIAN
;
508 writer
->byte_order
= internal_byte_order
;
509 if (writer
->trace_packet_header_type
||
510 writer
->trace_packet_header
) {
511 init_trace_packet_header(writer
);
517 void bt_ctf_writer_get(struct bt_ctf_writer
*writer
)
523 bt_ctf_ref_get(&writer
->ref_count
);
526 void bt_ctf_writer_put(struct bt_ctf_writer
*writer
)
532 bt_ctf_ref_put(&writer
->ref_count
, bt_ctf_writer_destroy
);
536 int validate_identifier(const char *input_string
)
540 char *save_ptr
, *token
;
542 if (!input_string
|| input_string
[0] == '\0') {
547 string
= strdup(input_string
);
553 token
= strtok_r(string
, " ", &save_ptr
);
555 if (g_hash_table_contains(reserved_keywords_set
,
556 GINT_TO_POINTER(g_quark_from_string(token
)))) {
561 token
= strtok_r(NULL
, " ", &save_ptr
);
569 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
571 unsigned int alignment
, size
;
572 struct bt_ctf_field_type
*field_type
;
574 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
578 alignment
= field_type_aliases_alignments
[alias
];
579 size
= field_type_aliases_sizes
[alias
];
580 field_type
= bt_ctf_field_type_integer_create(size
);
581 bt_ctf_field_type_set_alignment(field_type
, alignment
);
586 int init_trace_packet_header(struct bt_ctf_writer
*writer
)
590 struct bt_ctf_field
*trace_packet_header
= NULL
,
591 *magic
= NULL
, *uuid_array
= NULL
;
592 struct bt_ctf_field_type
*_uint32_t
=
593 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
594 struct bt_ctf_field_type
*_uint8_t
=
595 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
596 struct bt_ctf_field_type
*trace_packet_header_type
=
597 bt_ctf_field_type_structure_create();
598 struct bt_ctf_field_type
*uuid_array_type
=
599 bt_ctf_field_type_array_create(_uint8_t
, 16);
601 if (!trace_packet_header_type
|| !uuid_array_type
) {
606 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
,
607 (writer
->byte_order
== LITTLE_ENDIAN
?
608 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
609 BT_CTF_BYTE_ORDER_BIG_ENDIAN
));
614 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
620 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
621 uuid_array_type
, "uuid");
626 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
627 _uint32_t
, "stream_id");
632 trace_packet_header
= bt_ctf_field_create(trace_packet_header_type
);
633 if (!trace_packet_header
) {
638 magic
= bt_ctf_field_structure_get_field(trace_packet_header
, "magic");
639 ret
= bt_ctf_field_unsigned_integer_set_value(magic
, 0xC1FC1FC1);
644 uuid_array
= bt_ctf_field_structure_get_field(trace_packet_header
,
646 for (i
= 0; i
< 16; i
++) {
647 struct bt_ctf_field
*uuid_element
=
648 bt_ctf_field_array_get_field(uuid_array
, i
);
649 ret
= bt_ctf_field_unsigned_integer_set_value(uuid_element
,
651 bt_ctf_field_put(uuid_element
);
657 bt_ctf_field_type_put(writer
->trace_packet_header_type
);
658 bt_ctf_field_put(writer
->trace_packet_header
);
659 writer
->trace_packet_header_type
= trace_packet_header_type
;
660 writer
->trace_packet_header
= trace_packet_header
;
662 bt_ctf_field_type_put(uuid_array_type
);
663 bt_ctf_field_type_put(_uint32_t
);
664 bt_ctf_field_type_put(_uint8_t
);
665 bt_ctf_field_put(magic
);
666 bt_ctf_field_put(uuid_array
);
668 bt_ctf_field_type_put(trace_packet_header_type
);
669 bt_ctf_field_put(trace_packet_header
);
676 void environment_variable_destroy(struct environment_variable
*var
)
678 g_string_free(var
->name
, TRUE
);
679 g_string_free(var
->value
, TRUE
);
684 int create_stream_file(struct bt_ctf_writer
*writer
,
685 struct bt_ctf_stream
*stream
)
688 GString
*filename
= g_string_new(stream
->stream_class
->name
->str
);
690 g_string_append_printf(filename
, "_%" PRIu32
, stream
->id
);
691 fd
= openat(writer
->trace_dir_fd
, filename
->str
,
692 O_RDWR
| O_CREAT
| O_TRUNC
,
693 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
694 g_string_free(filename
, TRUE
);
699 void stream_flush_cb(struct bt_ctf_stream
*stream
, struct bt_ctf_writer
*writer
)
701 struct bt_ctf_field
*stream_id
;
703 /* Start a new packet in the stream */
704 if (stream
->flushed_packet_count
) {
705 /* ctf_init_pos has already initialized the first packet */
706 ctf_packet_seek(&stream
->pos
.parent
, 0, SEEK_CUR
);
709 stream_id
= bt_ctf_field_structure_get_field(
710 writer
->trace_packet_header
, "stream_id");
711 bt_ctf_field_unsigned_integer_set_value(stream_id
, stream
->id
);
712 bt_ctf_field_put(stream_id
);
714 /* Write the trace_packet_header */
715 bt_ctf_field_serialize(writer
->trace_packet_header
, &stream
->pos
);
718 static __attribute__((constructor
))
719 void writer_init(void)
722 const size_t reserved_keywords_count
=
723 sizeof(reserved_keywords_str
) / sizeof(char *);
725 global_data_refcount
++;
730 reserved_keywords_set
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
731 for (i
= 0; i
< reserved_keywords_count
; i
++) {
732 g_hash_table_add(reserved_keywords_set
,
733 GINT_TO_POINTER(g_quark_from_string(reserved_keywords_str
[i
])));
739 static __attribute__((destructor
))
740 void writer_finalize(void)
742 if (--global_data_refcount
== 0) {
743 g_hash_table_destroy(reserved_keywords_set
);