2 * SPDX-License-Identifier: MIT
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Babeltrace CTF Writer
9 #define BT_LOG_TAG "CTF-WRITER"
20 #include <babeltrace2-ctf-writer/object.h>
22 #include "common/assert.h"
23 #include "compat/compiler.h"
24 #include "compat/endian.h"
25 #include "common/uuid.h"
29 #include "field-types.h"
31 #include "stream-class.h"
37 void bt_ctf_writer_destroy(struct bt_ctf_object
*obj
);
40 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
43 struct bt_ctf_field_type
*_uint32_t
=
44 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
45 struct bt_ctf_field_type
*_uint8_t
=
46 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
47 struct bt_ctf_field_type
*trace_packet_header_type
=
48 bt_ctf_field_type_structure_create();
49 struct bt_ctf_field_type
*uuid_array_type
=
50 bt_ctf_field_type_array_create(_uint8_t
, 16);
52 if (!trace_packet_header_type
|| !uuid_array_type
) {
57 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
63 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
64 uuid_array_type
, "uuid");
69 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
70 _uint32_t
, "stream_id");
75 ret
= bt_ctf_trace_set_packet_header_field_type(trace
,
76 trace_packet_header_type
);
81 bt_ctf_object_put_ref(uuid_array_type
);
82 bt_ctf_object_put_ref(_uint32_t
);
83 bt_ctf_object_put_ref(_uint8_t
);
84 bt_ctf_object_put_ref(trace_packet_header_type
);
89 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
92 struct bt_ctf_writer
*writer
= NULL
;
94 char *metadata_path
= NULL
;
100 writer
= g_new0(struct bt_ctf_writer
, 1);
105 metadata_path
= g_build_filename(path
, "metadata", NULL
);
107 bt_ctf_object_init_shared(&writer
->base
, bt_ctf_writer_destroy
);
108 writer
->path
= g_string_new(path
);
113 writer
->trace
= bt_ctf_trace_create();
114 if (!writer
->trace
) {
118 ret
= init_trace_packet_header(writer
->trace
);
123 /* Generate a UUID for this writer's trace */
124 bt_uuid_generate(uuid
);
126 ret
= bt_ctf_trace_set_uuid(writer
->trace
, uuid
);
131 bt_ctf_object_set_parent(&writer
->trace
->common
.base
, &writer
->base
);
132 bt_ctf_object_put_ref(writer
->trace
);
134 /* Default to little-endian */
135 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_NATIVE
);
136 BT_ASSERT_DBG(ret
== 0);
138 /* Create trace directory if necessary and open a metadata file */
139 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
140 perror("g_mkdir_with_parents");
144 writer
->metadata_fd
= open(metadata_path
,
145 O_WRONLY
| O_CREAT
| O_TRUNC
,
146 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
147 if (writer
->metadata_fd
< 0) {
152 g_free(metadata_path
);
156 BT_CTF_OBJECT_PUT_REF_AND_RESET(writer
);
158 g_free(metadata_path
);
163 void bt_ctf_writer_destroy(struct bt_ctf_object
*obj
)
165 struct bt_ctf_writer
*writer
;
167 writer
= container_of(obj
, struct bt_ctf_writer
, base
);
168 bt_ctf_writer_flush_metadata(writer
);
170 g_string_free(writer
->path
, TRUE
);
173 if (writer
->metadata_fd
> 0) {
174 if (close(writer
->metadata_fd
)) {
179 bt_ctf_object_try_spec_release(&writer
->trace
->common
.base
);
184 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
186 struct bt_ctf_trace
*trace
= NULL
;
192 trace
= writer
->trace
;
193 bt_ctf_object_get_ref(trace
);
199 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
200 struct bt_ctf_stream_class
*stream_class
)
202 struct bt_ctf_stream
*stream
= NULL
;
203 int stream_class_count
;
204 bt_ctf_bool stream_class_found
= BT_CTF_FALSE
;
207 if (!writer
|| !stream_class
) {
211 /* Make sure the stream class is part of the writer's trace */
212 stream_class_count
= bt_ctf_trace_get_stream_class_count(writer
->trace
);
213 if (stream_class_count
< 0) {
217 for (i
= 0; i
< stream_class_count
; i
++) {
218 struct bt_ctf_stream_class
*existing_stream_class
=
219 bt_ctf_trace_get_stream_class_by_index(
222 if (existing_stream_class
== stream_class
) {
223 stream_class_found
= BT_CTF_TRUE
;
226 BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class
);
228 if (stream_class_found
) {
233 if (!stream_class_found
) {
234 int ret
= bt_ctf_trace_add_stream_class(writer
->trace
,
242 stream
= bt_ctf_stream_create_with_id(stream_class
, NULL
, -1ULL);
250 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
255 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
261 if (!writer
|| !name
|| !value
) {
265 ret
= bt_ctf_trace_set_environment_field_string(writer
->trace
,
272 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer
*writer
,
273 const char *name
, int64_t value
)
277 if (!writer
|| !name
) {
281 ret
= bt_ctf_trace_set_environment_field_integer(writer
->trace
, name
,
288 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
289 struct bt_ctf_clock
*clock
)
293 if (!writer
|| !clock
) {
297 ret
= bt_ctf_trace_add_clock_class(writer
->trace
, clock
->clock_class
);
303 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
305 char *metadata_string
= NULL
;
311 metadata_string
= bt_ctf_trace_get_metadata_string(
314 return metadata_string
;
318 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
321 char *metadata_string
= NULL
;
327 metadata_string
= bt_ctf_trace_get_metadata_string(
329 if (!metadata_string
) {
333 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
338 if (ftruncate(writer
->metadata_fd
, 0)) {
343 ret
= write(writer
->metadata_fd
, metadata_string
,
344 strlen(metadata_string
));
350 g_free(metadata_string
);
354 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
355 enum bt_ctf_byte_order byte_order
)
359 if (!writer
|| writer
->frozen
) {
364 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
365 if (BYTE_ORDER
== LITTLE_ENDIAN
) {
366 byte_order
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
368 byte_order
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
372 ret
= bt_ctf_trace_set_native_byte_order(writer
->trace
,
378 void bt_ctf_writer_freeze(struct bt_ctf_writer
*writer
)
384 const unsigned int field_type_aliases_alignments
[] = {
385 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
386 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
387 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
388 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
392 const unsigned int field_type_aliases_sizes
[] = {
393 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
394 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
395 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
396 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
397 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
398 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
401 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
404 unsigned int alignment
, size
;
405 struct bt_ctf_field_type
*field_type
= NULL
;
407 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
411 alignment
= field_type_aliases_alignments
[alias
];
412 size
= field_type_aliases_sizes
[alias
];
413 field_type
= bt_ctf_field_type_integer_create(size
);
414 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
416 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type
);
422 const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order
)
426 switch (byte_order
) {
427 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
430 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
433 case BT_CTF_BYTE_ORDER_NATIVE
: