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-ir/clock-internal.h>
30 #include <babeltrace/ctf-writer/writer-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/ctf-ir/event-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/ref.h>
37 #include <babeltrace/ctf-ir/common-internal.h>
38 #include <babeltrace/compiler.h>
48 void bt_ctf_writer_destroy(struct bt_ref
*ref
);
50 int create_stream_file(struct bt_ctf_writer
*writer
,
51 struct bt_ctf_stream
*stream
);
53 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
55 struct bt_ctf_writer
*writer
= NULL
;
61 writer
= g_new0(struct bt_ctf_writer
, 1);
66 bt_ctf_base_init(writer
, bt_ctf_writer_destroy
);
67 writer
->path
= g_string_new(path
);
72 writer
->trace
= bt_ctf_trace_create();
77 /* Create trace directory if necessary and open a metadata file */
78 if (g_mkdir_with_parents(path
, S_IRWXU
| S_IRWXG
)) {
79 perror("g_mkdir_with_parents");
83 writer
->trace_dir_fd
= open(path
, O_RDONLY
, S_IRWXU
| S_IRWXG
);
84 if (writer
->trace_dir_fd
< 0) {
89 writer
->metadata_fd
= openat(writer
->trace_dir_fd
, "metadata",
90 O_WRONLY
| O_CREAT
| O_TRUNC
,
91 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
96 unlinkat(writer
->trace_dir_fd
, "metadata", 0);
97 bt_ctf_writer_destroy(&writer
->base
.ref_count
);
103 void bt_ctf_writer_destroy(struct bt_ref
*ref
)
105 struct bt_ctf_writer
*writer
;
106 struct bt_ctf_base
*base
;
112 base
= container_of(ref
, struct bt_ctf_base
, ref_count
);
113 writer
= container_of(base
, struct bt_ctf_writer
, base
);
114 bt_ctf_writer_flush_metadata(writer
);
116 g_string_free(writer
->path
, TRUE
);
119 if (writer
->trace_dir_fd
> 0) {
120 if (close(writer
->trace_dir_fd
)) {
125 if (writer
->metadata_fd
> 0) {
126 if (close(writer
->metadata_fd
)) {
131 bt_ctf_trace_put(writer
->trace
);
135 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
137 struct bt_ctf_trace
*trace
= NULL
;
143 trace
= writer
->trace
;
144 bt_ctf_trace_get(trace
);
149 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
150 struct bt_ctf_stream_class
*stream_class
)
153 struct bt_ctf_stream
*stream
= NULL
;
155 if (!writer
|| !stream_class
) {
159 stream
= bt_ctf_trace_create_stream(writer
->trace
, stream_class
);
164 stream_fd
= create_stream_file(writer
, stream
);
165 if (stream_fd
< 0 || bt_ctf_stream_set_fd(stream
, stream_fd
)) {
173 bt_ctf_stream_put(stream
);
177 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
183 if (!writer
|| !name
|| !value
) {
187 ret
= bt_ctf_trace_set_environment_field_string(writer
->trace
,
193 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
194 struct bt_ctf_clock
*clock
)
198 if (!writer
|| !clock
) {
202 ret
= bt_ctf_trace_add_clock(writer
->trace
, clock
);
207 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
209 char *metadata_string
= NULL
;
215 metadata_string
= bt_ctf_trace_get_metadata_string(
218 return metadata_string
;
221 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
224 char *metadata_string
= NULL
;
230 metadata_string
= bt_ctf_trace_get_metadata_string(
232 if (!metadata_string
) {
236 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
241 if (ftruncate(writer
->metadata_fd
, 0)) {
246 ret
= write(writer
->metadata_fd
, metadata_string
,
247 strlen(metadata_string
));
253 g_free(metadata_string
);
256 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
257 enum bt_ctf_byte_order byte_order
)
261 if (!writer
|| writer
->frozen
) {
266 ret
= bt_ctf_trace_set_byte_order(writer
->trace
,
272 void bt_ctf_writer_get(struct bt_ctf_writer
*writer
)
277 void bt_ctf_writer_put(struct bt_ctf_writer
*writer
)
283 int create_stream_file(struct bt_ctf_writer
*writer
,
284 struct bt_ctf_stream
*stream
)
287 GString
*filename
= g_string_new(stream
->stream_class
->name
->str
);
289 if (stream
->stream_class
->name
->len
== 0) {
292 ret
= bt_ctf_stream_class_get_id(stream
->stream_class
);
298 g_string_printf(filename
, "stream_%" PRId64
, ret
);
301 g_string_append_printf(filename
, "_%" PRIu32
, stream
->id
);
302 fd
= openat(writer
->trace_dir_fd
, filename
->str
,
303 O_RDWR
| O_CREAT
| O_TRUNC
,
304 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
306 g_string_free(filename
, TRUE
);