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"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-internal.h>
33 #include <babeltrace/compat/uuid-internal.h>
34 #include <babeltrace/compiler-internal.h>
35 #include <babeltrace/ctf-writer/clock-internal.h>
36 #include <babeltrace/ctf-writer/field-types-internal.h>
37 #include <babeltrace/ctf-writer/fields-internal.h>
38 #include <babeltrace/ctf-writer/functor-internal.h>
39 #include <babeltrace/ctf-writer/stream-class-internal.h>
40 #include <babeltrace/ctf-writer/stream-internal.h>
41 #include <babeltrace/ctf-writer/trace-internal.h>
42 #include <babeltrace/ctf-writer/writer-internal.h>
43 #include <babeltrace/endian-internal.h>
44 #include <babeltrace/ctf-writer/object.h>
54 void bt_ctf_writer_destroy(struct bt_ctf_object
*obj
);
57 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
60 struct bt_ctf_field_type
*_uint32_t
=
61 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
62 struct bt_ctf_field_type
*_uint8_t
=
63 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
64 struct bt_ctf_field_type
*trace_packet_header_type
=
65 bt_ctf_field_type_structure_create();
66 struct bt_ctf_field_type
*uuid_array_type
=
67 bt_ctf_field_type_array_create(_uint8_t
, 16);
69 if (!trace_packet_header_type
|| !uuid_array_type
) {
74 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
80 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
81 uuid_array_type
, "uuid");
86 ret
= bt_ctf_field_type_structure_add_field(trace_packet_header_type
,
87 _uint32_t
, "stream_id");
92 ret
= bt_ctf_trace_set_packet_header_field_type(trace
,
93 trace_packet_header_type
);
98 bt_ctf_object_put_ref(uuid_array_type
);
99 bt_ctf_object_put_ref(_uint32_t
);
100 bt_ctf_object_put_ref(_uint8_t
);
101 bt_ctf_object_put_ref(trace_packet_header_type
);
105 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
108 struct bt_ctf_writer
*writer
= NULL
;
109 unsigned char uuid
[16];
110 char *metadata_path
= NULL
;
116 writer
= g_new0(struct bt_ctf_writer
, 1);
121 metadata_path
= g_build_filename(path
, "metadata", NULL
);
123 bt_ctf_object_init_shared(&writer
->base
, bt_ctf_writer_destroy
);
124 writer
->path
= g_string_new(path
);
129 writer
->trace
= bt_ctf_trace_create();
130 if (!writer
->trace
) {
134 ret
= init_trace_packet_header(writer
->trace
);
139 /* Generate a UUID for this writer's trace */
140 ret
= bt_uuid_generate(uuid
);
142 BT_LOGE_STR("Cannot generate UUID for CTF writer's trace.");
146 ret
= bt_ctf_trace_set_uuid(writer
->trace
, uuid
);
151 bt_ctf_object_set_parent(&writer
->trace
->common
.base
, &writer
->base
);
152 bt_ctf_object_put_ref(writer
->trace
);
154 /* Default to little-endian */
155 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_NATIVE
);
158 /* Create trace directory if necessary and open a metadata file */
159 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
160 perror("g_mkdir_with_parents");
164 writer
->metadata_fd
= open(metadata_path
,
165 O_WRONLY
| O_CREAT
| O_TRUNC
,
166 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
167 if (writer
->metadata_fd
< 0) {
172 g_free(metadata_path
);
176 BT_CTF_OBJECT_PUT_REF_AND_RESET(writer
);
178 g_free(metadata_path
);
182 void bt_ctf_writer_destroy(struct bt_ctf_object
*obj
)
184 struct bt_ctf_writer
*writer
;
186 writer
= container_of(obj
, struct bt_ctf_writer
, base
);
187 bt_ctf_writer_flush_metadata(writer
);
189 g_string_free(writer
->path
, TRUE
);
192 if (writer
->metadata_fd
> 0) {
193 if (close(writer
->metadata_fd
)) {
198 bt_ctf_object_try_spec_release(&writer
->trace
->common
.base
);
202 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
204 struct bt_ctf_trace
*trace
= NULL
;
210 trace
= writer
->trace
;
211 bt_ctf_object_get_ref(trace
);
216 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
217 struct bt_ctf_stream_class
*stream_class
)
219 struct bt_ctf_stream
*stream
= NULL
;
220 int stream_class_count
;
221 bt_bool stream_class_found
= BT_FALSE
;
224 if (!writer
|| !stream_class
) {
228 /* Make sure the stream class is part of the writer's trace */
229 stream_class_count
= bt_ctf_trace_get_stream_class_count(writer
->trace
);
230 if (stream_class_count
< 0) {
234 for (i
= 0; i
< stream_class_count
; i
++) {
235 struct bt_ctf_stream_class
*existing_stream_class
=
236 bt_ctf_trace_get_stream_class_by_index(
239 if (existing_stream_class
== stream_class
) {
240 stream_class_found
= BT_TRUE
;
243 BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class
);
245 if (stream_class_found
) {
250 if (!stream_class_found
) {
251 int ret
= bt_ctf_trace_add_stream_class(writer
->trace
,
259 stream
= bt_ctf_stream_create_with_id(stream_class
, NULL
, -1ULL);
267 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
271 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
277 if (!writer
|| !name
|| !value
) {
281 ret
= bt_ctf_trace_set_environment_field_string(writer
->trace
,
287 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer
*writer
,
288 const char *name
, int64_t value
)
292 if (!writer
|| !name
) {
296 ret
= bt_ctf_trace_set_environment_field_integer(writer
->trace
, name
,
302 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
303 struct bt_ctf_clock
*clock
)
307 if (!writer
|| !clock
) {
311 ret
= bt_ctf_trace_add_clock_class(writer
->trace
, clock
->clock_class
);
316 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
318 char *metadata_string
= NULL
;
324 metadata_string
= bt_ctf_trace_get_metadata_string(
327 return metadata_string
;
330 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
333 char *metadata_string
= NULL
;
339 metadata_string
= bt_ctf_trace_get_metadata_string(
341 if (!metadata_string
) {
345 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
350 if (ftruncate(writer
->metadata_fd
, 0)) {
355 ret
= write(writer
->metadata_fd
, metadata_string
,
356 strlen(metadata_string
));
362 g_free(metadata_string
);
365 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
366 enum bt_ctf_byte_order byte_order
)
370 if (!writer
|| writer
->frozen
) {
375 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
376 if (BYTE_ORDER
== LITTLE_ENDIAN
) {
377 byte_order
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
379 byte_order
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
383 ret
= bt_ctf_trace_set_native_byte_order(writer
->trace
,
390 void bt_ctf_writer_freeze(struct bt_ctf_writer
*writer
)
396 const unsigned int field_type_aliases_alignments
[] = {
397 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
398 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
399 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
400 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
404 const unsigned int field_type_aliases_sizes
[] = {
405 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
406 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
407 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
408 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
409 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
410 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
414 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
417 unsigned int alignment
, size
;
418 struct bt_ctf_field_type
*field_type
= NULL
;
420 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
424 alignment
= field_type_aliases_alignments
[alias
];
425 size
= field_type_aliases_sizes
[alias
];
426 field_type
= bt_ctf_field_type_integer_create(size
);
427 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
429 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type
);
436 const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order
)
440 switch (byte_order
) {
441 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
444 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
447 case BT_CTF_BYTE_ORDER_NATIVE
: