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