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 #include <babeltrace/ctf-writer/clock-internal.h>
30 #include <babeltrace/ctf-writer/writer-internal.h>
31 #include <babeltrace/ctf-ir/field-types-internal.h>
32 #include <babeltrace/ctf-ir/fields-internal.h>
33 #include <babeltrace/ctf-writer/functor-internal.h>
34 #include <babeltrace/ctf-ir/stream-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-internal.h>
36 #include <babeltrace/ctf-ir/trace-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/endian-internal.h>
39 #include <babeltrace/compiler-internal.h>
40 #include <babeltrace/compat/uuid-internal.h>
50 void bt_ctf_writer_destroy(struct bt_object
*obj
);
52 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
55 struct bt_ctf_writer
*writer
= NULL
;
56 unsigned char uuid
[16];
62 writer
= g_new0(struct bt_ctf_writer
, 1);
67 bt_object_init(writer
, bt_ctf_writer_destroy
);
68 writer
->path
= g_string_new(path
);
73 writer
->trace
= bt_ctf_trace_create();
78 /* Generate a UUID for this writer's trace */
80 ret
= bt_ctf_trace_set_uuid(writer
->trace
, uuid
);
85 writer
->trace
->is_created_by_writer
= 1;
86 bt_object_set_parent(writer
->trace
, writer
);
87 bt_put(writer
->trace
);
89 /* Default to little-endian */
90 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_NATIVE
);
93 /* Create trace directory if necessary and open a metadata file */
94 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
95 perror("g_mkdir_with_parents");
99 writer
->trace_dir_fd
= open(path
, O_RDONLY
, S_IRWXU
| S_IRWXG
);
100 if (writer
->trace_dir_fd
< 0) {
105 writer
->metadata_fd
= openat(writer
->trace_dir_fd
, "metadata",
106 O_WRONLY
| O_CREAT
| O_TRUNC
,
107 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
112 unlinkat(writer
->trace_dir_fd
, "metadata", 0);
118 void bt_ctf_writer_destroy(struct bt_object
*obj
)
120 struct bt_ctf_writer
*writer
;
122 writer
= container_of(obj
, struct bt_ctf_writer
, base
);
123 bt_ctf_writer_flush_metadata(writer
);
125 g_string_free(writer
->path
, TRUE
);
128 if (writer
->trace_dir_fd
> 0) {
129 if (close(writer
->trace_dir_fd
)) {
134 if (writer
->metadata_fd
> 0) {
135 if (close(writer
->metadata_fd
)) {
140 bt_object_release(writer
->trace
);
144 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
146 struct bt_ctf_trace
*trace
= NULL
;
152 trace
= writer
->trace
;
158 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
159 struct bt_ctf_stream_class
*stream_class
)
161 struct bt_ctf_stream
*stream
= NULL
;
162 int stream_class_count
;
163 bool stream_class_found
= false;
166 if (!writer
|| !stream_class
) {
170 /* Make sure the stream class is part of the writer's trace */
171 stream_class_count
= bt_ctf_trace_get_stream_class_count(writer
->trace
);
172 if (stream_class_count
< 0) {
176 for (i
= 0; i
< stream_class_count
; i
++) {
177 struct bt_ctf_stream_class
*existing_stream_class
=
178 bt_ctf_trace_get_stream_class_by_index(
181 if (existing_stream_class
== stream_class
) {
182 stream_class_found
= true;
185 BT_PUT(existing_stream_class
);
187 if (stream_class_found
) {
192 if (!stream_class_found
) {
193 int ret
= bt_ctf_trace_add_stream_class(writer
->trace
,
201 stream
= bt_ctf_stream_create(stream_class
, NULL
);
213 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
219 if (!writer
|| !name
|| !value
) {
223 ret
= bt_ctf_trace_set_environment_field_string(writer
->trace
,
229 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer
*writer
,
230 const char *name
, int64_t value
)
234 if (!writer
|| !name
) {
238 ret
= bt_ctf_trace_set_environment_field_integer(writer
->trace
, name
,
244 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
245 struct bt_ctf_clock
*clock
)
249 if (!writer
|| !clock
) {
253 ret
= bt_ctf_trace_add_clock_class(writer
->trace
, clock
->clock_class
);
258 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
260 char *metadata_string
= NULL
;
266 metadata_string
= bt_ctf_trace_get_metadata_string(
269 return metadata_string
;
272 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
275 char *metadata_string
= NULL
;
281 metadata_string
= bt_ctf_trace_get_metadata_string(
283 if (!metadata_string
) {
287 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
292 if (ftruncate(writer
->metadata_fd
, 0)) {
297 ret
= write(writer
->metadata_fd
, metadata_string
,
298 strlen(metadata_string
));
304 g_free(metadata_string
);
307 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
308 enum bt_ctf_byte_order byte_order
)
312 if (!writer
|| writer
->frozen
) {
317 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
318 byte_order
= BT_CTF_MY_BYTE_ORDER
;
321 ret
= bt_ctf_trace_set_native_byte_order(writer
->trace
,
327 void bt_ctf_writer_get(struct bt_ctf_writer
*writer
)
332 void bt_ctf_writer_put(struct bt_ctf_writer
*writer
)
338 void bt_ctf_writer_freeze(struct bt_ctf_writer
*writer
)