tap-driver.sh: flush stdout after each test result
[babeltrace.git] / 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"
67d2ce02 30#include "logging.h"
20eee76e 31
3fadfbc0
MJ
32#include <babeltrace2/assert-internal.h>
33#include <babeltrace2/compat/uuid-internal.h>
34#include <babeltrace2/compiler-internal.h>
35#include <babeltrace2/ctf-writer/clock-internal.h>
36#include <babeltrace2/ctf-writer/field-types-internal.h>
37#include <babeltrace2/ctf-writer/fields-internal.h>
38#include <babeltrace2/ctf-writer/functor-internal.h>
39#include <babeltrace2/ctf-writer/stream-class-internal.h>
40#include <babeltrace2/ctf-writer/stream-internal.h>
41#include <babeltrace2/ctf-writer/trace-internal.h>
42#include <babeltrace2/ctf-writer/writer-internal.h>
43#include <babeltrace2/endian-internal.h>
44#include <babeltrace2/ctf-writer/object.h>
16ca5ff0
PP
45#include <errno.h>
46#include <fcntl.h>
47#include <inttypes.h>
273b65be
JG
48#include <stdio.h>
49#include <stdlib.h>
50#include <sys/stat.h>
273b65be 51#include <unistd.h>
273b65be 52
273b65be 53static
e1e02a22 54void bt_ctf_writer_destroy(struct bt_ctf_object *obj);
83509119 55
488e09a7 56static
3dca2276 57int init_trace_packet_header(struct bt_ctf_trace *trace)
488e09a7
PP
58{
59 int ret = 0;
3dca2276 60 struct bt_ctf_field_type *_uint32_t =
488e09a7 61 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
3dca2276 62 struct bt_ctf_field_type *_uint8_t =
488e09a7 63 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
3dca2276
PP
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);
488e09a7
PP
68
69 if (!trace_packet_header_type || !uuid_array_type) {
70 ret = -1;
71 goto end;
72 }
73
3dca2276 74 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
488e09a7
PP
75 _uint32_t, "magic");
76 if (ret) {
77 goto end;
78 }
79
3dca2276 80 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
488e09a7
PP
81 uuid_array_type, "uuid");
82 if (ret) {
83 goto end;
84 }
85
3dca2276 86 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
488e09a7
PP
87 _uint32_t, "stream_id");
88 if (ret) {
89 goto end;
90 }
91
3dca2276 92 ret = bt_ctf_trace_set_packet_header_field_type(trace,
488e09a7
PP
93 trace_packet_header_type);
94 if (ret) {
95 goto end;
96 }
97end:
e1e02a22
PP
98 bt_ctf_object_put_ref(uuid_array_type);
99 bt_ctf_object_put_ref(_uint32_t);
100 bt_ctf_object_put_ref(_uint8_t);
101 bt_ctf_object_put_ref(trace_packet_header_type);
488e09a7
PP
102 return ret;
103}
104
273b65be
JG
105struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
106{
3f5808e5 107 int ret;
273b65be 108 struct bt_ctf_writer *writer = NULL;
4a32fda0 109 unsigned char uuid[16];
ebd04048 110 char *metadata_path = NULL;
273b65be
JG
111
112 if (!path) {
113 goto error;
114 }
115
116 writer = g_new0(struct bt_ctf_writer, 1);
117 if (!writer) {
118 goto error;
119 }
120
ebd04048
MJ
121 metadata_path = g_build_filename(path, "metadata", NULL);
122
e1e02a22 123 bt_ctf_object_init_shared(&writer->base, bt_ctf_writer_destroy);
273b65be
JG
124 writer->path = g_string_new(path);
125 if (!writer->path) {
126 goto error_destroy;
127 }
128
3dca2276 129 writer->trace = bt_ctf_trace_create();
bc37ae52
JG
130 if (!writer->trace) {
131 goto error_destroy;
132 }
133
488e09a7
PP
134 ret = init_trace_packet_header(writer->trace);
135 if (ret) {
136 goto error_destroy;
137 }
138
4a32fda0 139 /* Generate a UUID for this writer's trace */
20eee76e
MJ
140 ret = bt_uuid_generate(uuid);
141 if (ret) {
142 BT_LOGE_STR("Cannot generate UUID for CTF writer's trace.");
143 goto error_destroy;
144 }
145
3dca2276 146 ret = bt_ctf_trace_set_uuid(writer->trace, uuid);
4a32fda0
PP
147 if (ret) {
148 goto error_destroy;
149 }
150
e1e02a22
PP
151 bt_ctf_object_set_parent(&writer->trace->common.base, &writer->base);
152 bt_ctf_object_put_ref(writer->trace);
3f5808e5
PP
153
154 /* Default to little-endian */
3dca2276 155 ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE);
f6ccaed9 156 BT_ASSERT(ret == 0);
3f5808e5 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
ebd04048
MJ
164 writer->metadata_fd = open(metadata_path,
165 O_WRONLY | O_CREAT | O_TRUNC,
166 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
167 if (writer->metadata_fd < 0) {
273b65be
JG
168 perror("open");
169 goto error_destroy;
170 }
171
ebd04048 172 g_free(metadata_path);
273b65be 173 return writer;
bc37ae52 174
273b65be 175error_destroy:
e1e02a22 176 BT_CTF_OBJECT_PUT_REF_AND_RESET(writer);
273b65be 177error:
ebd04048 178 g_free(metadata_path);
273b65be
JG
179 return writer;
180}
181
e1e02a22 182void bt_ctf_writer_destroy(struct bt_ctf_object *obj)
273b65be
JG
183{
184 struct bt_ctf_writer *writer;
273b65be 185
83509119 186 writer = container_of(obj, struct bt_ctf_writer, base);
273b65be
JG
187 bt_ctf_writer_flush_metadata(writer);
188 if (writer->path) {
189 g_string_free(writer->path, TRUE);
190 }
191
273b65be 192 if (writer->metadata_fd > 0) {
9bb7e58b
MD
193 if (close(writer->metadata_fd)) {
194 perror("close");
9bb7e58b 195 }
273b65be
JG
196 }
197
e1e02a22 198 bt_ctf_object_try_spec_release(&writer->trace->common.base);
273b65be
JG
199 g_free(writer);
200}
201
3dca2276 202struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
a2540e85 203{
3dca2276 204 struct bt_ctf_trace *trace = NULL;
a2540e85
JG
205
206 if (!writer) {
207 goto end;
208 }
209
210 trace = writer->trace;
e1e02a22 211 bt_ctf_object_get_ref(trace);
a2540e85
JG
212end:
213 return trace;
214}
215
3dca2276
PP
216struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
217 struct bt_ctf_stream_class *stream_class)
273b65be 218{
3dca2276 219 struct bt_ctf_stream *stream = NULL;
319fd969 220 int stream_class_count;
c55a9f58 221 bt_bool stream_class_found = BT_FALSE;
319fd969 222 int i;
273b65be
JG
223
224 if (!writer || !stream_class) {
225 goto error;
226 }
227
319fd969 228 /* Make sure the stream class is part of the writer's trace */
3dca2276 229 stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace);
319fd969 230 if (stream_class_count < 0) {
273b65be
JG
231 goto error;
232 }
233
319fd969 234 for (i = 0; i < stream_class_count; i++) {
3dca2276
PP
235 struct bt_ctf_stream_class *existing_stream_class =
236 bt_ctf_trace_get_stream_class_by_index(
9ac68eb1 237 writer->trace, i);
319fd969
PP
238
239 if (existing_stream_class == stream_class) {
c55a9f58 240 stream_class_found = BT_TRUE;
319fd969
PP
241 }
242
e1e02a22 243 BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class);
319fd969
PP
244
245 if (stream_class_found) {
246 break;
247 }
248 }
249
250 if (!stream_class_found) {
3dca2276 251 int ret = bt_ctf_trace_add_stream_class(writer->trace,
319fd969
PP
252 stream_class);
253
254 if (ret) {
255 goto error;
256 }
257 }
258
3dca2276 259 stream = bt_ctf_stream_create_with_id(stream_class, NULL, -1ULL);
319fd969 260 if (!stream) {
273b65be
JG
261 goto error;
262 }
263
273b65be 264 return stream;
bc37ae52 265
273b65be 266error:
e1e02a22 267 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream);
83509119 268 return stream;
273b65be
JG
269}
270
271int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
272 const char *name,
273 const char *value)
274{
bc37ae52 275 int ret = -1;
273b65be 276
bc37ae52
JG
277 if (!writer || !name || !value) {
278 goto end;
273b65be
JG
279 }
280
3dca2276 281 ret = bt_ctf_trace_set_environment_field_string(writer->trace,
bc37ae52
JG
282 name, value);
283end:
273b65be
JG
284 return ret;
285}
286
d7503815 287int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer,
9ac68eb1 288 const char *name, int64_t value)
d7503815
SM
289{
290 int ret = -1;
291
292 if (!writer || !name) {
293 goto end;
294 }
295
3dca2276 296 ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name,
d7503815
SM
297 value);
298end:
299 return ret;
300}
301
273b65be
JG
302int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
303 struct bt_ctf_clock *clock)
304{
bc37ae52 305 int ret = -1;
273b65be
JG
306
307 if (!writer || !clock) {
12af6048
JG
308 goto end;
309 }
273b65be 310
3dca2276 311 ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class);
12af6048
JG
312end:
313 return ret;
273b65be
JG
314}
315
273b65be
JG
316char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
317{
bc37ae52 318 char *metadata_string = NULL;
273b65be
JG
319
320 if (!writer) {
321 goto end;
322 }
323
3dca2276 324 metadata_string = bt_ctf_trace_get_metadata_string(
bc37ae52 325 writer->trace);
273b65be 326end:
bc37ae52 327 return metadata_string;
273b65be
JG
328}
329
330void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
331{
332 int ret;
333 char *metadata_string = NULL;
334
335 if (!writer) {
336 goto end;
337 }
338
3dca2276 339 metadata_string = bt_ctf_trace_get_metadata_string(
bc37ae52 340 writer->trace);
273b65be
JG
341 if (!metadata_string) {
342 goto end;
343 }
344
345 if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
346 perror("lseek");
347 goto end;
348 }
349
350 if (ftruncate(writer->metadata_fd, 0)) {
351 perror("ftruncate");
352 goto end;
353 }
354
355 ret = write(writer->metadata_fd, metadata_string,
356 strlen(metadata_string));
357 if (ret < 0) {
358 perror("write");
359 goto end;
360 }
361end:
362 g_free(metadata_string);
363}
364
365int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
3dca2276 366 enum bt_ctf_byte_order byte_order)
273b65be
JG
367{
368 int ret = 0;
273b65be
JG
369
370 if (!writer || writer->frozen) {
371 ret = -1;
372 goto end;
373 }
374
3dca2276 375 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
013f35c6
PP
376 if (BYTE_ORDER == LITTLE_ENDIAN) {
377 byte_order = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
378 } else {
379 byte_order = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
380 }
3f5808e5
PP
381 }
382
3dca2276 383 ret = bt_ctf_trace_set_native_byte_order(writer->trace,
bc37ae52 384 byte_order);
273b65be
JG
385end:
386 return ret;
387}
388
3dca2276
PP
389BT_HIDDEN
390void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
273b65be 391{
3dca2276 392 writer->frozen = 1;
273b65be
JG
393}
394
3dca2276
PP
395static
396const unsigned int field_type_aliases_alignments[] = {
397 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
398 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
399 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
400 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
401};
402
403static
404const unsigned int field_type_aliases_sizes[] = {
405 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
406 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
407 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
408 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
409 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
410 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
411};
412
413BT_HIDDEN
414struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
273b65be 415{
3dca2276
PP
416 int ret;
417 unsigned int alignment, size;
418 struct bt_ctf_field_type *field_type = NULL;
419
420 if (alias >= NR_FIELD_TYPE_ALIAS) {
421 goto end;
422 }
423
424 alignment = field_type_aliases_alignments[alias];
425 size = field_type_aliases_sizes[alias];
426 field_type = bt_ctf_field_type_integer_create(size);
427 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
428 if (ret) {
e1e02a22 429 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type);
3dca2276
PP
430 }
431end:
432 return field_type;
273b65be
JG
433}
434
319fd969 435BT_HIDDEN
16ca5ff0 436const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order)
273b65be 437{
3dca2276
PP
438 const char *string;
439
440 switch (byte_order) {
16ca5ff0 441 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
3dca2276
PP
442 string = "le";
443 break;
16ca5ff0 444 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
3dca2276
PP
445 string = "be";
446 break;
16ca5ff0 447 case BT_CTF_BYTE_ORDER_NATIVE:
3dca2276
PP
448 string = "native";
449 break;
450 default:
451 abort();
452 }
453
454 return string;
273b65be 455}
This page took 0.072531 seconds and 4 git commands to generate.