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/ctf-writer/clock-internal.h>
33 #include <babeltrace/ctf-writer/writer-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/fields-internal.h>
36 #include <babeltrace/ctf-writer/functor-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-ir/stream-internal.h>
39 #include <babeltrace/ctf-ir/trace-internal.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/endian-internal.h>
42 #include <babeltrace/compiler-internal.h>
43 #include <babeltrace/compat/uuid-internal.h>
44 #include <babeltrace/assert-internal.h>
54 void bt_ctf_writer_destroy(struct bt_object
*obj
);
57 int init_trace_packet_header(struct bt_trace
*trace
)
60 struct bt_field
*magic
= NULL
, *uuid_array
= NULL
;
61 struct bt_field_type
*_uint32_t
=
62 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
63 struct bt_field_type
*_uint8_t
=
64 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
65 struct bt_field_type
*trace_packet_header_type
=
66 bt_field_type_structure_create();
67 struct bt_field_type
*uuid_array_type
=
68 bt_field_type_array_create(_uint8_t
, 16);
70 if (!trace_packet_header_type
|| !uuid_array_type
) {
75 ret
= bt_field_type_structure_add_field(trace_packet_header_type
,
81 ret
= bt_field_type_structure_add_field(trace_packet_header_type
,
82 uuid_array_type
, "uuid");
87 ret
= bt_field_type_structure_add_field(trace_packet_header_type
,
88 _uint32_t
, "stream_id");
93 ret
= bt_trace_set_packet_header_type(trace
,
94 trace_packet_header_type
);
99 bt_put(uuid_array_type
);
104 bt_put(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_object_init(writer
, bt_ctf_writer_destroy
);
127 writer
->path
= g_string_new(path
);
132 writer
->trace
= bt_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_trace_set_uuid(writer
->trace
, uuid
);
154 writer
->trace
->is_created_by_writer
= 1;
155 bt_object_set_parent(writer
->trace
, writer
);
156 bt_put(writer
->trace
);
158 /* Default to little-endian */
159 ret
= bt_ctf_writer_set_byte_order(writer
, BT_BYTE_ORDER_NATIVE
);
162 /* Create trace directory if necessary and open a metadata file */
163 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
164 perror("g_mkdir_with_parents");
168 writer
->metadata_fd
= open(metadata_path
,
169 O_WRONLY
| O_CREAT
| O_TRUNC
,
170 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
171 if (writer
->metadata_fd
< 0) {
176 g_free(metadata_path
);
182 g_free(metadata_path
);
186 void bt_ctf_writer_destroy(struct bt_object
*obj
)
188 struct bt_ctf_writer
*writer
;
190 writer
= container_of(obj
, struct bt_ctf_writer
, base
);
191 bt_ctf_writer_flush_metadata(writer
);
193 g_string_free(writer
->path
, TRUE
);
196 if (writer
->metadata_fd
> 0) {
197 if (close(writer
->metadata_fd
)) {
202 bt_object_release(writer
->trace
);
206 struct bt_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
208 struct bt_trace
*trace
= NULL
;
214 trace
= writer
->trace
;
220 struct bt_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
221 struct bt_stream_class
*stream_class
)
223 struct bt_stream
*stream
= NULL
;
224 int stream_class_count
;
225 bt_bool stream_class_found
= BT_FALSE
;
228 if (!writer
|| !stream_class
) {
232 /* Make sure the stream class is part of the writer's trace */
233 stream_class_count
= bt_trace_get_stream_class_count(writer
->trace
);
234 if (stream_class_count
< 0) {
238 for (i
= 0; i
< stream_class_count
; i
++) {
239 struct bt_stream_class
*existing_stream_class
=
240 bt_trace_get_stream_class_by_index(
243 if (existing_stream_class
== stream_class
) {
244 stream_class_found
= BT_TRUE
;
247 BT_PUT(existing_stream_class
);
249 if (stream_class_found
) {
254 if (!stream_class_found
) {
255 int ret
= bt_trace_add_stream_class(writer
->trace
,
263 stream
= bt_stream_create(stream_class
, NULL
);
275 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
281 if (!writer
|| !name
|| !value
) {
285 ret
= bt_trace_set_environment_field_string(writer
->trace
,
291 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer
*writer
,
292 const char *name
, int64_t value
)
296 if (!writer
|| !name
) {
300 ret
= bt_trace_set_environment_field_integer(writer
->trace
, name
,
306 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
307 struct bt_ctf_clock
*clock
)
311 if (!writer
|| !clock
) {
315 ret
= bt_trace_add_clock_class(writer
->trace
, clock
->clock_class
);
320 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
322 char *metadata_string
= NULL
;
328 metadata_string
= bt_trace_get_metadata_string(
331 return metadata_string
;
334 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
337 char *metadata_string
= NULL
;
343 metadata_string
= bt_trace_get_metadata_string(
345 if (!metadata_string
) {
349 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
354 if (ftruncate(writer
->metadata_fd
, 0)) {
359 ret
= write(writer
->metadata_fd
, metadata_string
,
360 strlen(metadata_string
));
366 g_free(metadata_string
);
369 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
370 enum bt_byte_order byte_order
)
374 if (!writer
|| writer
->frozen
) {
379 if (byte_order
== BT_BYTE_ORDER_NATIVE
) {
380 byte_order
= BT_MY_BYTE_ORDER
;
383 ret
= bt_trace_set_native_byte_order(writer
->trace
,
389 void bt_ctf_writer_get(struct bt_ctf_writer
*writer
)
394 void bt_ctf_writer_put(struct bt_ctf_writer
*writer
)
400 void bt_ctf_writer_freeze(struct bt_ctf_writer
*writer
)