4 * Babeltrace CTF Writer
6 * Copyright 2013, 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 #define BT_LOG_TAG "CTF-WRITER"
40 #include <babeltrace2/ctf-writer/object.h>
42 #include "common/assert.h"
43 #include "compat/compiler.h"
44 #include "compat/endian.h"
45 #include "compat/uuid.h"
49 #include "field-types.h"
51 #include "stream-class.h"
57 void bt_ctf_writer_destroy(struct bt_ctf_object
*obj
);
60 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
63 struct bt_ctf_field_type
*_uint32_t
=
64 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
65 struct bt_ctf_field_type
*_uint8_t
=
66 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
67 struct bt_ctf_field_type
*trace_packet_header_type
=
68 bt_ctf_field_type_structure_create();
69 struct bt_ctf_field_type
*uuid_array_type
=
70 bt_ctf_field_type_array_create(_uint8_t
, 16);
72 if (!trace_packet_header_type
|| !uuid_array_type
) {
77 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
83 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
84 uuid_array_type
, "uuid");
89 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
90 _uint32_t
, "stream_id");
95 ret
= bt_ctf_trace_set_packet_header_field_type(trace
,
96 trace_packet_header_type
);
101 bt_ctf_object_put_ref(uuid_array_type
);
102 bt_ctf_object_put_ref(_uint32_t
);
103 bt_ctf_object_put_ref(_uint8_t
);
104 bt_ctf_object_put_ref(trace_packet_header_type
);
108 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
111 struct bt_ctf_writer
*writer
= NULL
;
112 unsigned char uuid
[16];
113 char *metadata_path
= NULL
;
119 writer
= g_new0(struct bt_ctf_writer
, 1);
124 metadata_path
= g_build_filename(path
, "metadata", NULL
);
126 bt_ctf_object_init_shared(&writer
->base
, bt_ctf_writer_destroy
);
127 writer
->path
= g_string_new(path
);
132 writer
->trace
= bt_ctf_trace_create();
133 if (!writer
->trace
) {
137 ret
= init_trace_packet_header(writer
->trace
);
142 /* Generate a UUID for this writer's trace */
143 ret
= bt_uuid_generate(uuid
);
145 BT_LOGE_STR("Cannot generate UUID for CTF writer's trace.");
149 ret
= bt_ctf_trace_set_uuid(writer
->trace
, uuid
);
154 bt_ctf_object_set_parent(&writer
->trace
->common
.base
, &writer
->base
);
155 bt_ctf_object_put_ref(writer
->trace
);
157 /* Default to little-endian */
158 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_NATIVE
);
161 /* Create trace directory if necessary and open a metadata file */
162 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
163 perror("g_mkdir_with_parents");
167 writer
->metadata_fd
= open(metadata_path
,
168 O_WRONLY
| O_CREAT
| O_TRUNC
,
169 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
170 if (writer
->metadata_fd
< 0) {
175 g_free(metadata_path
);
179 BT_CTF_OBJECT_PUT_REF_AND_RESET(writer
);
181 g_free(metadata_path
);
185 void bt_ctf_writer_destroy(struct bt_ctf_object
*obj
)
187 struct bt_ctf_writer
*writer
;
189 writer
= container_of(obj
, struct bt_ctf_writer
, base
);
190 bt_ctf_writer_flush_metadata(writer
);
192 g_string_free(writer
->path
, TRUE
);
195 if (writer
->metadata_fd
> 0) {
196 if (close(writer
->metadata_fd
)) {
201 bt_ctf_object_try_spec_release(&writer
->trace
->common
.base
);
205 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
207 struct bt_ctf_trace
*trace
= NULL
;
213 trace
= writer
->trace
;
214 bt_ctf_object_get_ref(trace
);
219 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
220 struct bt_ctf_stream_class
*stream_class
)
222 struct bt_ctf_stream
*stream
= NULL
;
223 int stream_class_count
;
224 bt_bool stream_class_found
= BT_FALSE
;
227 if (!writer
|| !stream_class
) {
231 /* Make sure the stream class is part of the writer's trace */
232 stream_class_count
= bt_ctf_trace_get_stream_class_count(writer
->trace
);
233 if (stream_class_count
< 0) {
237 for (i
= 0; i
< stream_class_count
; i
++) {
238 struct bt_ctf_stream_class
*existing_stream_class
=
239 bt_ctf_trace_get_stream_class_by_index(
242 if (existing_stream_class
== stream_class
) {
243 stream_class_found
= BT_TRUE
;
246 BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class
);
248 if (stream_class_found
) {
253 if (!stream_class_found
) {
254 int ret
= bt_ctf_trace_add_stream_class(writer
->trace
,
262 stream
= bt_ctf_stream_create_with_id(stream_class
, NULL
, -1ULL);
270 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
274 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
280 if (!writer
|| !name
|| !value
) {
284 ret
= bt_ctf_trace_set_environment_field_string(writer
->trace
,
290 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer
*writer
,
291 const char *name
, int64_t value
)
295 if (!writer
|| !name
) {
299 ret
= bt_ctf_trace_set_environment_field_integer(writer
->trace
, name
,
305 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
306 struct bt_ctf_clock
*clock
)
310 if (!writer
|| !clock
) {
314 ret
= bt_ctf_trace_add_clock_class(writer
->trace
, clock
->clock_class
);
319 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
321 char *metadata_string
= NULL
;
327 metadata_string
= bt_ctf_trace_get_metadata_string(
330 return metadata_string
;
333 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
336 char *metadata_string
= NULL
;
342 metadata_string
= bt_ctf_trace_get_metadata_string(
344 if (!metadata_string
) {
348 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
353 if (ftruncate(writer
->metadata_fd
, 0)) {
358 ret
= write(writer
->metadata_fd
, metadata_string
,
359 strlen(metadata_string
));
365 g_free(metadata_string
);
368 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
369 enum bt_ctf_byte_order byte_order
)
373 if (!writer
|| writer
->frozen
) {
378 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
379 if (BYTE_ORDER
== LITTLE_ENDIAN
) {
380 byte_order
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
382 byte_order
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
386 ret
= bt_ctf_trace_set_native_byte_order(writer
->trace
,
393 void bt_ctf_writer_freeze(struct bt_ctf_writer
*writer
)
399 const unsigned int field_type_aliases_alignments
[] = {
400 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
401 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
402 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
403 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
407 const unsigned int field_type_aliases_sizes
[] = {
408 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
409 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
410 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
411 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
412 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
413 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
417 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
420 unsigned int alignment
, size
;
421 struct bt_ctf_field_type
*field_type
= NULL
;
423 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
427 alignment
= field_type_aliases_alignments
[alias
];
428 size
= field_type_aliases_sizes
[alias
];
429 field_type
= bt_ctf_field_type_integer_create(size
);
430 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
432 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type
);
439 const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order
)
443 switch (byte_order
) {
444 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
447 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
450 case BT_CTF_BYTE_ORDER_NATIVE
: