Commit | Line | Data |
---|---|---|
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 | ||
ac0c6bdd | 29 | #include <babeltrace/ctf-writer/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> |
3d9990ac PP |
38 | #include <babeltrace/endian-internal.h> |
39 | #include <babeltrace/compiler-internal.h> | |
273b65be JG |
40 | #include <stdio.h> |
41 | #include <stdlib.h> | |
42 | #include <sys/stat.h> | |
43 | #include <errno.h> | |
44 | #include <unistd.h> | |
45 | #include <fcntl.h> | |
46 | #include <inttypes.h> | |
47 | ||
273b65be | 48 | static |
83509119 JG |
49 | void bt_ctf_writer_destroy(struct bt_object *obj); |
50 | ||
273b65be JG |
51 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path) |
52 | { | |
3f5808e5 | 53 | int ret; |
273b65be JG |
54 | struct bt_ctf_writer *writer = NULL; |
55 | ||
56 | if (!path) { | |
57 | goto error; | |
58 | } | |
59 | ||
60 | writer = g_new0(struct bt_ctf_writer, 1); | |
61 | if (!writer) { | |
62 | goto error; | |
63 | } | |
64 | ||
83509119 | 65 | bt_object_init(writer, bt_ctf_writer_destroy); |
273b65be JG |
66 | writer->path = g_string_new(path); |
67 | if (!writer->path) { | |
68 | goto error_destroy; | |
69 | } | |
70 | ||
bc37ae52 JG |
71 | writer->trace = bt_ctf_trace_create(); |
72 | if (!writer->trace) { | |
73 | goto error_destroy; | |
74 | } | |
75 | ||
319fd969 | 76 | writer->trace->is_created_by_writer = 1; |
e6a8e8e4 JG |
77 | bt_object_set_parent(writer->trace, writer); |
78 | bt_put(writer->trace); | |
3f5808e5 PP |
79 | |
80 | /* Default to little-endian */ | |
81 | ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE); | |
82 | assert(ret == 0); | |
83 | ||
273b65be JG |
84 | /* Create trace directory if necessary and open a metadata file */ |
85 | if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { | |
86 | perror("g_mkdir_with_parents"); | |
87 | goto error_destroy; | |
88 | } | |
89 | ||
1b8180b9 | 90 | writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); |
273b65be JG |
91 | if (writer->trace_dir_fd < 0) { |
92 | perror("open"); | |
93 | goto error_destroy; | |
94 | } | |
95 | ||
96 | writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", | |
97 | O_WRONLY | O_CREAT | O_TRUNC, | |
98 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
273b65be JG |
99 | |
100 | return writer; | |
bc37ae52 | 101 | |
273b65be JG |
102 | error_destroy: |
103 | unlinkat(writer->trace_dir_fd, "metadata", 0); | |
83509119 | 104 | BT_PUT(writer); |
273b65be JG |
105 | error: |
106 | return writer; | |
107 | } | |
108 | ||
83509119 | 109 | void bt_ctf_writer_destroy(struct bt_object *obj) |
273b65be JG |
110 | { |
111 | struct bt_ctf_writer *writer; | |
273b65be | 112 | |
83509119 | 113 | writer = container_of(obj, struct bt_ctf_writer, base); |
273b65be JG |
114 | bt_ctf_writer_flush_metadata(writer); |
115 | if (writer->path) { | |
116 | g_string_free(writer->path, TRUE); | |
117 | } | |
118 | ||
119 | if (writer->trace_dir_fd > 0) { | |
9bb7e58b MD |
120 | if (close(writer->trace_dir_fd)) { |
121 | perror("close"); | |
9bb7e58b | 122 | } |
273b65be JG |
123 | } |
124 | ||
125 | if (writer->metadata_fd > 0) { | |
9bb7e58b MD |
126 | if (close(writer->metadata_fd)) { |
127 | perror("close"); | |
9bb7e58b | 128 | } |
273b65be JG |
129 | } |
130 | ||
e6a8e8e4 | 131 | bt_object_release(writer->trace); |
273b65be JG |
132 | g_free(writer); |
133 | } | |
134 | ||
a2540e85 JG |
135 | struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) |
136 | { | |
137 | struct bt_ctf_trace *trace = NULL; | |
138 | ||
139 | if (!writer) { | |
140 | goto end; | |
141 | } | |
142 | ||
143 | trace = writer->trace; | |
83509119 | 144 | bt_get(trace); |
a2540e85 JG |
145 | end: |
146 | return trace; | |
147 | } | |
148 | ||
273b65be JG |
149 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, |
150 | struct bt_ctf_stream_class *stream_class) | |
151 | { | |
273b65be | 152 | struct bt_ctf_stream *stream = NULL; |
319fd969 PP |
153 | int stream_class_count; |
154 | bool stream_class_found = false; | |
155 | int i; | |
273b65be JG |
156 | |
157 | if (!writer || !stream_class) { | |
158 | goto error; | |
159 | } | |
160 | ||
319fd969 PP |
161 | /* Make sure the stream class is part of the writer's trace */ |
162 | stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace); | |
163 | if (stream_class_count < 0) { | |
273b65be JG |
164 | goto error; |
165 | } | |
166 | ||
319fd969 PP |
167 | for (i = 0; i < stream_class_count; i++) { |
168 | struct bt_ctf_stream_class *existing_stream_class = | |
169 | bt_ctf_trace_get_stream_class(writer->trace, i); | |
170 | ||
171 | if (existing_stream_class == stream_class) { | |
172 | stream_class_found = true; | |
173 | } | |
174 | ||
175 | BT_PUT(existing_stream_class); | |
176 | ||
177 | if (stream_class_found) { | |
178 | break; | |
179 | } | |
180 | } | |
181 | ||
182 | if (!stream_class_found) { | |
183 | int ret = bt_ctf_trace_add_stream_class(writer->trace, | |
184 | stream_class); | |
185 | ||
186 | if (ret) { | |
187 | goto error; | |
188 | } | |
189 | } | |
190 | ||
b71d7298 | 191 | stream = bt_ctf_stream_create(stream_class, NULL); |
319fd969 | 192 | if (!stream) { |
273b65be JG |
193 | goto error; |
194 | } | |
195 | ||
273b65be | 196 | return stream; |
bc37ae52 | 197 | |
273b65be | 198 | error: |
83509119 JG |
199 | BT_PUT(stream); |
200 | return stream; | |
273b65be JG |
201 | } |
202 | ||
203 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, | |
204 | const char *name, | |
205 | const char *value) | |
206 | { | |
bc37ae52 | 207 | int ret = -1; |
273b65be | 208 | |
bc37ae52 JG |
209 | if (!writer || !name || !value) { |
210 | goto end; | |
273b65be JG |
211 | } |
212 | ||
7f800dc7 | 213 | ret = bt_ctf_trace_set_environment_field_string(writer->trace, |
bc37ae52 JG |
214 | name, value); |
215 | end: | |
273b65be JG |
216 | return ret; |
217 | } | |
218 | ||
d7503815 SM |
219 | int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, |
220 | const char *name, | |
221 | int64_t value) | |
222 | { | |
223 | int ret = -1; | |
224 | ||
225 | if (!writer || !name) { | |
226 | goto end; | |
227 | } | |
228 | ||
229 | ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name, | |
230 | value); | |
231 | end: | |
232 | return ret; | |
233 | } | |
234 | ||
273b65be JG |
235 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, |
236 | struct bt_ctf_clock *clock) | |
237 | { | |
bc37ae52 | 238 | int ret = -1; |
273b65be JG |
239 | |
240 | if (!writer || !clock) { | |
12af6048 JG |
241 | goto end; |
242 | } | |
273b65be | 243 | |
ac0c6bdd | 244 | ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class); |
12af6048 JG |
245 | end: |
246 | return ret; | |
273b65be JG |
247 | } |
248 | ||
273b65be JG |
249 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) |
250 | { | |
bc37ae52 | 251 | char *metadata_string = NULL; |
273b65be JG |
252 | |
253 | if (!writer) { | |
254 | goto end; | |
255 | } | |
256 | ||
bc37ae52 JG |
257 | metadata_string = bt_ctf_trace_get_metadata_string( |
258 | writer->trace); | |
273b65be | 259 | end: |
bc37ae52 | 260 | return metadata_string; |
273b65be JG |
261 | } |
262 | ||
263 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) | |
264 | { | |
265 | int ret; | |
266 | char *metadata_string = NULL; | |
267 | ||
268 | if (!writer) { | |
269 | goto end; | |
270 | } | |
271 | ||
bc37ae52 JG |
272 | metadata_string = bt_ctf_trace_get_metadata_string( |
273 | writer->trace); | |
273b65be JG |
274 | if (!metadata_string) { |
275 | goto end; | |
276 | } | |
277 | ||
278 | if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { | |
279 | perror("lseek"); | |
280 | goto end; | |
281 | } | |
282 | ||
283 | if (ftruncate(writer->metadata_fd, 0)) { | |
284 | perror("ftruncate"); | |
285 | goto end; | |
286 | } | |
287 | ||
288 | ret = write(writer->metadata_fd, metadata_string, | |
289 | strlen(metadata_string)); | |
290 | if (ret < 0) { | |
291 | perror("write"); | |
292 | goto end; | |
293 | } | |
294 | end: | |
295 | g_free(metadata_string); | |
296 | } | |
297 | ||
298 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, | |
299 | enum bt_ctf_byte_order byte_order) | |
300 | { | |
301 | int ret = 0; | |
273b65be JG |
302 | |
303 | if (!writer || writer->frozen) { | |
304 | ret = -1; | |
305 | goto end; | |
306 | } | |
307 | ||
3f5808e5 PP |
308 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
309 | byte_order = BT_CTF_MY_BYTE_ORDER; | |
310 | } | |
311 | ||
391c8f0d | 312 | ret = bt_ctf_trace_set_native_byte_order(writer->trace, |
bc37ae52 | 313 | byte_order); |
273b65be JG |
314 | end: |
315 | return ret; | |
316 | } | |
317 | ||
318 | void bt_ctf_writer_get(struct bt_ctf_writer *writer) | |
319 | { | |
83509119 | 320 | bt_get(writer); |
273b65be JG |
321 | } |
322 | ||
323 | void bt_ctf_writer_put(struct bt_ctf_writer *writer) | |
324 | { | |
83509119 | 325 | bt_put(writer); |
273b65be JG |
326 | } |
327 | ||
319fd969 PP |
328 | BT_HIDDEN |
329 | void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) | |
273b65be | 330 | { |
319fd969 | 331 | writer->frozen = 1; |
273b65be | 332 | } |