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>
53 void bt_ctf_writer_destroy(struct bt_object
*obj
);
56 int init_trace_packet_header(struct bt_ctf_trace
*trace
)
59 struct bt_ctf_field
*magic
= NULL
, *uuid_array
= NULL
;
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_type(trace
,
93 trace_packet_header_type
);
98 bt_put(uuid_array_type
);
103 bt_put(trace_packet_header_type
);
107 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
110 struct bt_ctf_writer
*writer
= NULL
;
111 unsigned char uuid
[16];
117 writer
= g_new0(struct bt_ctf_writer
, 1);
122 bt_object_init(writer
, bt_ctf_writer_destroy
);
123 writer
->path
= g_string_new(path
);
128 writer
->trace
= bt_ctf_trace_create();
129 if (!writer
->trace
) {
133 ret
= init_trace_packet_header(writer
->trace
);
138 /* Generate a UUID for this writer's trace */
139 ret
= bt_uuid_generate(uuid
);
141 BT_LOGE_STR("Cannot generate UUID for CTF writer's trace.");
145 ret
= bt_ctf_trace_set_uuid(writer
->trace
, uuid
);
150 writer
->trace
->is_created_by_writer
= 1;
151 bt_object_set_parent(writer
->trace
, writer
);
152 bt_put(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
->trace_dir_fd
= open(path
, O_RDONLY
, S_IRWXU
| S_IRWXG
);
165 if (writer
->trace_dir_fd
< 0) {
170 writer
->metadata_fd
= openat(writer
->trace_dir_fd
, "metadata",
171 O_WRONLY
| O_CREAT
| O_TRUNC
,
172 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
177 unlinkat(writer
->trace_dir_fd
, "metadata", 0);
183 void bt_ctf_writer_destroy(struct bt_object
*obj
)
185 struct bt_ctf_writer
*writer
;
187 writer
= container_of(obj
, struct bt_ctf_writer
, base
);
188 bt_ctf_writer_flush_metadata(writer
);
190 g_string_free(writer
->path
, TRUE
);
193 if (writer
->trace_dir_fd
> 0) {
194 if (close(writer
->trace_dir_fd
)) {
199 if (writer
->metadata_fd
> 0) {
200 if (close(writer
->metadata_fd
)) {
205 bt_object_release(writer
->trace
);
209 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
211 struct bt_ctf_trace
*trace
= NULL
;
217 trace
= writer
->trace
;
223 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
224 struct bt_ctf_stream_class
*stream_class
)
226 struct bt_ctf_stream
*stream
= NULL
;
227 int stream_class_count
;
228 bt_bool stream_class_found
= BT_FALSE
;
231 if (!writer
|| !stream_class
) {
235 /* Make sure the stream class is part of the writer's trace */
236 stream_class_count
= bt_ctf_trace_get_stream_class_count(writer
->trace
);
237 if (stream_class_count
< 0) {
241 for (i
= 0; i
< stream_class_count
; i
++) {
242 struct bt_ctf_stream_class
*existing_stream_class
=
243 bt_ctf_trace_get_stream_class_by_index(
246 if (existing_stream_class
== stream_class
) {
247 stream_class_found
= BT_TRUE
;
250 BT_PUT(existing_stream_class
);
252 if (stream_class_found
) {
257 if (!stream_class_found
) {
258 int ret
= bt_ctf_trace_add_stream_class(writer
->trace
,
266 stream
= bt_ctf_stream_create(stream_class
, NULL
);
278 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
284 if (!writer
|| !name
|| !value
) {
288 ret
= bt_ctf_trace_set_environment_field_string(writer
->trace
,
294 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer
*writer
,
295 const char *name
, int64_t value
)
299 if (!writer
|| !name
) {
303 ret
= bt_ctf_trace_set_environment_field_integer(writer
->trace
, name
,
309 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
310 struct bt_ctf_clock
*clock
)
314 if (!writer
|| !clock
) {
318 ret
= bt_ctf_trace_add_clock_class(writer
->trace
, clock
->clock_class
);
323 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
325 char *metadata_string
= NULL
;
331 metadata_string
= bt_ctf_trace_get_metadata_string(
334 return metadata_string
;
337 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
340 char *metadata_string
= NULL
;
346 metadata_string
= bt_ctf_trace_get_metadata_string(
348 if (!metadata_string
) {
352 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
357 if (ftruncate(writer
->metadata_fd
, 0)) {
362 ret
= write(writer
->metadata_fd
, metadata_string
,
363 strlen(metadata_string
));
369 g_free(metadata_string
);
372 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
373 enum bt_ctf_byte_order byte_order
)
377 if (!writer
|| writer
->frozen
) {
382 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
383 byte_order
= BT_CTF_MY_BYTE_ORDER
;
386 ret
= bt_ctf_trace_set_native_byte_order(writer
->trace
,
392 void bt_ctf_writer_get(struct bt_ctf_writer
*writer
)
397 void bt_ctf_writer_put(struct bt_ctf_writer
*writer
)
403 void bt_ctf_writer_freeze(struct bt_ctf_writer
*writer
)