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