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