Add --debug-info-target-prefix
[babeltrace.git] / formats / ctf / writer / writer.c
CommitLineData
273b65be
JG
1/*
2 * writer.c
3 *
4 * Babeltrace CTF Writer
5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
adc315b8 29#include <babeltrace/ctf-ir/clock-internal.h>
273b65be 30#include <babeltrace/ctf-writer/writer-internal.h>
2e33ac5a
PP
31#include <babeltrace/ctf-ir/field-types-internal.h>
32#include <babeltrace/ctf-ir/fields-internal.h>
273b65be 33#include <babeltrace/ctf-writer/functor-internal.h>
adc315b8 34#include <babeltrace/ctf-ir/stream-class-internal.h>
3f043b05 35#include <babeltrace/ctf-ir/stream-internal.h>
319fd969 36#include <babeltrace/ctf-ir/trace-internal.h>
83509119 37#include <babeltrace/ref.h>
273b65be
JG
38#include <babeltrace/compiler.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <sys/stat.h>
42#include <errno.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <inttypes.h>
46
273b65be 47static
83509119
JG
48void bt_ctf_writer_destroy(struct bt_object *obj);
49
273b65be
JG
50struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
51{
52 struct bt_ctf_writer *writer = NULL;
53
54 if (!path) {
55 goto error;
56 }
57
58 writer = g_new0(struct bt_ctf_writer, 1);
59 if (!writer) {
60 goto error;
61 }
62
83509119 63 bt_object_init(writer, bt_ctf_writer_destroy);
273b65be
JG
64 writer->path = g_string_new(path);
65 if (!writer->path) {
66 goto error_destroy;
67 }
68
bc37ae52
JG
69 writer->trace = bt_ctf_trace_create();
70 if (!writer->trace) {
71 goto error_destroy;
72 }
73
319fd969 74 writer->trace->is_created_by_writer = 1;
e6a8e8e4
JG
75 bt_object_set_parent(writer->trace, writer);
76 bt_put(writer->trace);
273b65be
JG
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");
80 goto error_destroy;
81 }
82
1b8180b9 83 writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
273b65be
JG
84 if (writer->trace_dir_fd < 0) {
85 perror("open");
86 goto error_destroy;
87 }
88
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);
273b65be
JG
92
93 return writer;
bc37ae52 94
273b65be
JG
95error_destroy:
96 unlinkat(writer->trace_dir_fd, "metadata", 0);
83509119 97 BT_PUT(writer);
273b65be
JG
98error:
99 return writer;
100}
101
83509119 102void bt_ctf_writer_destroy(struct bt_object *obj)
273b65be
JG
103{
104 struct bt_ctf_writer *writer;
273b65be 105
83509119 106 writer = container_of(obj, struct bt_ctf_writer, base);
273b65be
JG
107 bt_ctf_writer_flush_metadata(writer);
108 if (writer->path) {
109 g_string_free(writer->path, TRUE);
110 }
111
112 if (writer->trace_dir_fd > 0) {
9bb7e58b
MD
113 if (close(writer->trace_dir_fd)) {
114 perror("close");
9bb7e58b 115 }
273b65be
JG
116 }
117
118 if (writer->metadata_fd > 0) {
9bb7e58b
MD
119 if (close(writer->metadata_fd)) {
120 perror("close");
9bb7e58b 121 }
273b65be
JG
122 }
123
e6a8e8e4 124 bt_object_release(writer->trace);
273b65be
JG
125 g_free(writer);
126}
127
a2540e85
JG
128struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
129{
130 struct bt_ctf_trace *trace = NULL;
131
132 if (!writer) {
133 goto end;
134 }
135
136 trace = writer->trace;
83509119 137 bt_get(trace);
a2540e85
JG
138end:
139 return trace;
140}
141
273b65be
JG
142struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
143 struct bt_ctf_stream_class *stream_class)
144{
273b65be 145 struct bt_ctf_stream *stream = NULL;
319fd969
PP
146 int stream_class_count;
147 bool stream_class_found = false;
148 int i;
273b65be
JG
149
150 if (!writer || !stream_class) {
151 goto error;
152 }
153
319fd969
PP
154 /* Make sure the stream class is part of the writer's trace */
155 stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace);
156 if (stream_class_count < 0) {
273b65be
JG
157 goto error;
158 }
159
319fd969
PP
160 for (i = 0; i < stream_class_count; i++) {
161 struct bt_ctf_stream_class *existing_stream_class =
162 bt_ctf_trace_get_stream_class(writer->trace, i);
163
164 if (existing_stream_class == stream_class) {
165 stream_class_found = true;
166 }
167
168 BT_PUT(existing_stream_class);
169
170 if (stream_class_found) {
171 break;
172 }
173 }
174
175 if (!stream_class_found) {
176 int ret = bt_ctf_trace_add_stream_class(writer->trace,
177 stream_class);
178
179 if (ret) {
180 goto error;
181 }
182 }
183
b71d7298 184 stream = bt_ctf_stream_create(stream_class, NULL);
319fd969 185 if (!stream) {
273b65be
JG
186 goto error;
187 }
188
273b65be 189 return stream;
bc37ae52 190
273b65be 191error:
83509119
JG
192 BT_PUT(stream);
193 return stream;
273b65be
JG
194}
195
196int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
197 const char *name,
198 const char *value)
199{
bc37ae52 200 int ret = -1;
273b65be 201
bc37ae52
JG
202 if (!writer || !name || !value) {
203 goto end;
273b65be
JG
204 }
205
7f800dc7 206 ret = bt_ctf_trace_set_environment_field_string(writer->trace,
bc37ae52
JG
207 name, value);
208end:
273b65be
JG
209 return ret;
210}
211
212int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
213 struct bt_ctf_clock *clock)
214{
bc37ae52 215 int ret = -1;
273b65be
JG
216
217 if (!writer || !clock) {
12af6048
JG
218 goto end;
219 }
273b65be 220
bc37ae52 221 ret = bt_ctf_trace_add_clock(writer->trace, clock);
12af6048
JG
222end:
223 return ret;
273b65be
JG
224}
225
273b65be
JG
226char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
227{
bc37ae52 228 char *metadata_string = NULL;
273b65be
JG
229
230 if (!writer) {
231 goto end;
232 }
233
bc37ae52
JG
234 metadata_string = bt_ctf_trace_get_metadata_string(
235 writer->trace);
273b65be 236end:
bc37ae52 237 return metadata_string;
273b65be
JG
238}
239
240void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
241{
242 int ret;
243 char *metadata_string = NULL;
244
245 if (!writer) {
246 goto end;
247 }
248
bc37ae52
JG
249 metadata_string = bt_ctf_trace_get_metadata_string(
250 writer->trace);
273b65be
JG
251 if (!metadata_string) {
252 goto end;
253 }
254
255 if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
256 perror("lseek");
257 goto end;
258 }
259
260 if (ftruncate(writer->metadata_fd, 0)) {
261 perror("ftruncate");
262 goto end;
263 }
264
265 ret = write(writer->metadata_fd, metadata_string,
266 strlen(metadata_string));
267 if (ret < 0) {
268 perror("write");
269 goto end;
270 }
271end:
272 g_free(metadata_string);
273}
274
275int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
276 enum bt_ctf_byte_order byte_order)
277{
278 int ret = 0;
273b65be
JG
279
280 if (!writer || writer->frozen) {
281 ret = -1;
282 goto end;
283 }
284
bc37ae52
JG
285 ret = bt_ctf_trace_set_byte_order(writer->trace,
286 byte_order);
273b65be
JG
287end:
288 return ret;
289}
290
291void bt_ctf_writer_get(struct bt_ctf_writer *writer)
292{
83509119 293 bt_get(writer);
273b65be
JG
294}
295
296void bt_ctf_writer_put(struct bt_ctf_writer *writer)
297{
83509119 298 bt_put(writer);
273b65be
JG
299}
300
319fd969
PP
301BT_HIDDEN
302void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
273b65be 303{
319fd969 304 writer->frozen = 1;
273b65be 305}
This page took 0.043714 seconds and 4 git commands to generate.