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 | ||
488e09a7 PP |
52 | static |
53 | int init_trace_packet_header(struct bt_ctf_trace *trace) | |
54 | { | |
55 | int ret = 0; | |
56 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; | |
57 | struct bt_ctf_field_type *_uint32_t = | |
58 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
59 | struct bt_ctf_field_type *_uint8_t = | |
60 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
61 | struct bt_ctf_field_type *trace_packet_header_type = | |
62 | bt_ctf_field_type_structure_create(); | |
63 | struct bt_ctf_field_type *uuid_array_type = | |
64 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
65 | ||
66 | if (!trace_packet_header_type || !uuid_array_type) { | |
67 | ret = -1; | |
68 | goto end; | |
69 | } | |
70 | ||
71 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
72 | _uint32_t, "magic"); | |
73 | if (ret) { | |
74 | goto end; | |
75 | } | |
76 | ||
77 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
78 | uuid_array_type, "uuid"); | |
79 | if (ret) { | |
80 | goto end; | |
81 | } | |
82 | ||
83 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
84 | _uint32_t, "stream_id"); | |
85 | if (ret) { | |
86 | goto end; | |
87 | } | |
88 | ||
89 | ret = bt_ctf_trace_set_packet_header_type(trace, | |
90 | trace_packet_header_type); | |
91 | if (ret) { | |
92 | goto end; | |
93 | } | |
94 | end: | |
95 | bt_put(uuid_array_type); | |
96 | bt_put(_uint32_t); | |
97 | bt_put(_uint8_t); | |
98 | bt_put(magic); | |
99 | bt_put(uuid_array); | |
100 | bt_put(trace_packet_header_type); | |
101 | return ret; | |
102 | } | |
103 | ||
273b65be JG |
104 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path) |
105 | { | |
3f5808e5 | 106 | int ret; |
273b65be | 107 | struct bt_ctf_writer *writer = NULL; |
4a32fda0 | 108 | unsigned char uuid[16]; |
273b65be JG |
109 | |
110 | if (!path) { | |
111 | goto error; | |
112 | } | |
113 | ||
114 | writer = g_new0(struct bt_ctf_writer, 1); | |
115 | if (!writer) { | |
116 | goto error; | |
117 | } | |
118 | ||
83509119 | 119 | bt_object_init(writer, bt_ctf_writer_destroy); |
273b65be JG |
120 | writer->path = g_string_new(path); |
121 | if (!writer->path) { | |
122 | goto error_destroy; | |
123 | } | |
124 | ||
bc37ae52 JG |
125 | writer->trace = bt_ctf_trace_create(); |
126 | if (!writer->trace) { | |
127 | goto error_destroy; | |
128 | } | |
129 | ||
488e09a7 PP |
130 | ret = init_trace_packet_header(writer->trace); |
131 | if (ret) { | |
132 | goto error_destroy; | |
133 | } | |
134 | ||
4a32fda0 PP |
135 | /* Generate a UUID for this writer's trace */ |
136 | uuid_generate(uuid); | |
137 | ret = bt_ctf_trace_set_uuid(writer->trace, uuid); | |
138 | if (ret) { | |
139 | goto error_destroy; | |
140 | } | |
141 | ||
319fd969 | 142 | writer->trace->is_created_by_writer = 1; |
e6a8e8e4 JG |
143 | bt_object_set_parent(writer->trace, writer); |
144 | bt_put(writer->trace); | |
3f5808e5 PP |
145 | |
146 | /* Default to little-endian */ | |
147 | ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE); | |
148 | assert(ret == 0); | |
149 | ||
273b65be JG |
150 | /* Create trace directory if necessary and open a metadata file */ |
151 | if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { | |
152 | perror("g_mkdir_with_parents"); | |
153 | goto error_destroy; | |
154 | } | |
155 | ||
1b8180b9 | 156 | writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); |
273b65be JG |
157 | if (writer->trace_dir_fd < 0) { |
158 | perror("open"); | |
159 | goto error_destroy; | |
160 | } | |
161 | ||
162 | writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", | |
163 | O_WRONLY | O_CREAT | O_TRUNC, | |
164 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
273b65be JG |
165 | |
166 | return writer; | |
bc37ae52 | 167 | |
273b65be JG |
168 | error_destroy: |
169 | unlinkat(writer->trace_dir_fd, "metadata", 0); | |
83509119 | 170 | BT_PUT(writer); |
273b65be JG |
171 | error: |
172 | return writer; | |
173 | } | |
174 | ||
83509119 | 175 | void bt_ctf_writer_destroy(struct bt_object *obj) |
273b65be JG |
176 | { |
177 | struct bt_ctf_writer *writer; | |
273b65be | 178 | |
83509119 | 179 | writer = container_of(obj, struct bt_ctf_writer, base); |
273b65be JG |
180 | bt_ctf_writer_flush_metadata(writer); |
181 | if (writer->path) { | |
182 | g_string_free(writer->path, TRUE); | |
183 | } | |
184 | ||
185 | if (writer->trace_dir_fd > 0) { | |
9bb7e58b MD |
186 | if (close(writer->trace_dir_fd)) { |
187 | perror("close"); | |
9bb7e58b | 188 | } |
273b65be JG |
189 | } |
190 | ||
191 | if (writer->metadata_fd > 0) { | |
9bb7e58b MD |
192 | if (close(writer->metadata_fd)) { |
193 | perror("close"); | |
9bb7e58b | 194 | } |
273b65be JG |
195 | } |
196 | ||
e6a8e8e4 | 197 | bt_object_release(writer->trace); |
273b65be JG |
198 | g_free(writer); |
199 | } | |
200 | ||
a2540e85 JG |
201 | struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) |
202 | { | |
203 | struct bt_ctf_trace *trace = NULL; | |
204 | ||
205 | if (!writer) { | |
206 | goto end; | |
207 | } | |
208 | ||
209 | trace = writer->trace; | |
83509119 | 210 | bt_get(trace); |
a2540e85 JG |
211 | end: |
212 | return trace; | |
213 | } | |
214 | ||
273b65be JG |
215 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, |
216 | struct bt_ctf_stream_class *stream_class) | |
217 | { | |
273b65be | 218 | struct bt_ctf_stream *stream = NULL; |
319fd969 | 219 | int stream_class_count; |
c55a9f58 | 220 | bt_bool stream_class_found = BT_FALSE; |
319fd969 | 221 | int i; |
273b65be JG |
222 | |
223 | if (!writer || !stream_class) { | |
224 | goto error; | |
225 | } | |
226 | ||
319fd969 PP |
227 | /* Make sure the stream class is part of the writer's trace */ |
228 | stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace); | |
229 | if (stream_class_count < 0) { | |
273b65be JG |
230 | goto error; |
231 | } | |
232 | ||
319fd969 PP |
233 | for (i = 0; i < stream_class_count; i++) { |
234 | struct bt_ctf_stream_class *existing_stream_class = | |
9ac68eb1 PP |
235 | bt_ctf_trace_get_stream_class_by_index( |
236 | writer->trace, i); | |
319fd969 PP |
237 | |
238 | if (existing_stream_class == stream_class) { | |
c55a9f58 | 239 | stream_class_found = BT_TRUE; |
319fd969 PP |
240 | } |
241 | ||
242 | BT_PUT(existing_stream_class); | |
243 | ||
244 | if (stream_class_found) { | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
249 | if (!stream_class_found) { | |
250 | int ret = bt_ctf_trace_add_stream_class(writer->trace, | |
251 | stream_class); | |
252 | ||
253 | if (ret) { | |
254 | goto error; | |
255 | } | |
256 | } | |
257 | ||
b71d7298 | 258 | stream = bt_ctf_stream_create(stream_class, NULL); |
319fd969 | 259 | if (!stream) { |
273b65be JG |
260 | goto error; |
261 | } | |
262 | ||
273b65be | 263 | return stream; |
bc37ae52 | 264 | |
273b65be | 265 | error: |
83509119 JG |
266 | BT_PUT(stream); |
267 | return stream; | |
273b65be JG |
268 | } |
269 | ||
270 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, | |
271 | const char *name, | |
272 | const char *value) | |
273 | { | |
bc37ae52 | 274 | int ret = -1; |
273b65be | 275 | |
bc37ae52 JG |
276 | if (!writer || !name || !value) { |
277 | goto end; | |
273b65be JG |
278 | } |
279 | ||
7f800dc7 | 280 | ret = bt_ctf_trace_set_environment_field_string(writer->trace, |
bc37ae52 JG |
281 | name, value); |
282 | end: | |
273b65be JG |
283 | return ret; |
284 | } | |
285 | ||
d7503815 | 286 | int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, |
9ac68eb1 | 287 | const char *name, int64_t value) |
d7503815 SM |
288 | { |
289 | int ret = -1; | |
290 | ||
291 | if (!writer || !name) { | |
292 | goto end; | |
293 | } | |
294 | ||
295 | ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name, | |
296 | value); | |
297 | end: | |
298 | return ret; | |
299 | } | |
300 | ||
273b65be JG |
301 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, |
302 | struct bt_ctf_clock *clock) | |
303 | { | |
bc37ae52 | 304 | int ret = -1; |
273b65be JG |
305 | |
306 | if (!writer || !clock) { | |
12af6048 JG |
307 | goto end; |
308 | } | |
273b65be | 309 | |
ac0c6bdd | 310 | ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class); |
12af6048 JG |
311 | end: |
312 | return ret; | |
273b65be JG |
313 | } |
314 | ||
273b65be JG |
315 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) |
316 | { | |
bc37ae52 | 317 | char *metadata_string = NULL; |
273b65be JG |
318 | |
319 | if (!writer) { | |
320 | goto end; | |
321 | } | |
322 | ||
bc37ae52 JG |
323 | metadata_string = bt_ctf_trace_get_metadata_string( |
324 | writer->trace); | |
273b65be | 325 | end: |
bc37ae52 | 326 | return metadata_string; |
273b65be JG |
327 | } |
328 | ||
329 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) | |
330 | { | |
331 | int ret; | |
332 | char *metadata_string = NULL; | |
333 | ||
334 | if (!writer) { | |
335 | goto end; | |
336 | } | |
337 | ||
bc37ae52 JG |
338 | metadata_string = bt_ctf_trace_get_metadata_string( |
339 | writer->trace); | |
273b65be JG |
340 | if (!metadata_string) { |
341 | goto end; | |
342 | } | |
343 | ||
344 | if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { | |
345 | perror("lseek"); | |
346 | goto end; | |
347 | } | |
348 | ||
349 | if (ftruncate(writer->metadata_fd, 0)) { | |
350 | perror("ftruncate"); | |
351 | goto end; | |
352 | } | |
353 | ||
354 | ret = write(writer->metadata_fd, metadata_string, | |
355 | strlen(metadata_string)); | |
356 | if (ret < 0) { | |
357 | perror("write"); | |
358 | goto end; | |
359 | } | |
360 | end: | |
361 | g_free(metadata_string); | |
362 | } | |
363 | ||
364 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, | |
365 | enum bt_ctf_byte_order byte_order) | |
366 | { | |
367 | int ret = 0; | |
273b65be JG |
368 | |
369 | if (!writer || writer->frozen) { | |
370 | ret = -1; | |
371 | goto end; | |
372 | } | |
373 | ||
3f5808e5 PP |
374 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
375 | byte_order = BT_CTF_MY_BYTE_ORDER; | |
376 | } | |
377 | ||
391c8f0d | 378 | ret = bt_ctf_trace_set_native_byte_order(writer->trace, |
bc37ae52 | 379 | byte_order); |
273b65be JG |
380 | end: |
381 | return ret; | |
382 | } | |
383 | ||
384 | void bt_ctf_writer_get(struct bt_ctf_writer *writer) | |
385 | { | |
83509119 | 386 | bt_get(writer); |
273b65be JG |
387 | } |
388 | ||
389 | void bt_ctf_writer_put(struct bt_ctf_writer *writer) | |
390 | { | |
83509119 | 391 | bt_put(writer); |
273b65be JG |
392 | } |
393 | ||
319fd969 PP |
394 | BT_HIDDEN |
395 | void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) | |
273b65be | 396 | { |
319fd969 | 397 | writer->frozen = 1; |
273b65be | 398 | } |