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