Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[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
086dc475
PP
32#include <babeltrace/assert-internal.h>
33#include <babeltrace/compat/uuid-internal.h>
34#include <babeltrace/compiler-internal.h>
ac0c6bdd 35#include <babeltrace/ctf-writer/clock-internal.h>
8deee039
PP
36#include <babeltrace/ctf-writer/field-types-internal.h>
37#include <babeltrace/ctf-writer/fields-internal.h>
273b65be 38#include <babeltrace/ctf-writer/functor-internal.h>
8deee039
PP
39#include <babeltrace/ctf-writer/stream-class-internal.h>
40#include <babeltrace/ctf-writer/stream-internal.h>
41#include <babeltrace/ctf-writer/trace-internal.h>
42#include <babeltrace/ctf-writer/writer-internal.h>
3d9990ac 43#include <babeltrace/endian-internal.h>
8138bfe1 44#include <babeltrace/object.h>
086dc475
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
83509119
JG
54void bt_ctf_writer_destroy(struct bt_object *obj);
55
488e09a7 56static
8deee039 57int init_trace_packet_header(struct bt_ctf_trace *trace)
488e09a7
PP
58{
59 int ret = 0;
8deee039 60 struct bt_ctf_field_type *_uint32_t =
488e09a7 61 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
8deee039 62 struct bt_ctf_field_type *_uint8_t =
488e09a7 63 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
8deee039
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
8deee039 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
8deee039 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
8deee039 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
8deee039 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:
8138bfe1
PP
98 bt_object_put_ref(uuid_array_type);
99 bt_object_put_ref(_uint32_t);
100 bt_object_put_ref(_uint8_t);
101 bt_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];
bb9c6e6a 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
bb9c6e6a
MJ
121 metadata_path = g_build_filename(path, "metadata", NULL);
122
1d7bf349 123 bt_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
8deee039 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
8deee039 146 ret = bt_ctf_trace_set_uuid(writer->trace, uuid);
4a32fda0
PP
147 if (ret) {
148 goto error_destroy;
149 }
150
1d7bf349 151 bt_object_set_parent(&writer->trace->common.base, &writer->base);
8138bfe1 152 bt_object_put_ref(writer->trace);
3f5808e5
PP
153
154 /* Default to little-endian */
8deee039 155 ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE);
8b45963b 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
bb9c6e6a
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
bb9c6e6a 172 g_free(metadata_path);
273b65be 173 return writer;
bc37ae52 174
273b65be 175error_destroy:
8138bfe1 176 BT_OBJECT_PUT_REF_AND_RESET(writer);
273b65be 177error:
bb9c6e6a 178 g_free(metadata_path);
273b65be
JG
179 return writer;
180}
181
83509119 182void bt_ctf_writer_destroy(struct bt_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
1d7bf349 198 bt_object_try_spec_release(&writer->trace->common.base);
273b65be
JG
199 g_free(writer);
200}
201
8deee039 202struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
a2540e85 203{
8deee039 204 struct bt_ctf_trace *trace = NULL;
a2540e85
JG
205
206 if (!writer) {
207 goto end;
208 }
209
210 trace = writer->trace;
8138bfe1 211 bt_object_get_ref(trace);
a2540e85
JG
212end:
213 return trace;
214}
215
8deee039
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{
8deee039 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 */
8deee039 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++) {
8deee039
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
8138bfe1 243 BT_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) {
8deee039 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
8deee039 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:
8138bfe1 267 BT_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
8deee039 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
8deee039 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
8deee039 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
8deee039 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
8deee039 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,
8deee039 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
8deee039 375 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
7b33a0e0 376 byte_order = BT_CTF_MY_BYTE_ORDER;
3f5808e5
PP
377 }
378
8deee039 379 ret = bt_ctf_trace_set_native_byte_order(writer->trace,
bc37ae52 380 byte_order);
273b65be
JG
381end:
382 return ret;
383}
384
8deee039
PP
385BT_HIDDEN
386void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
273b65be 387{
8deee039 388 writer->frozen = 1;
273b65be
JG
389}
390
8deee039
PP
391static
392const unsigned int field_type_aliases_alignments[] = {
393 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
394 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
395 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
396 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
397};
398
399static
400const unsigned int field_type_aliases_sizes[] = {
401 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
402 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
403 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
404 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
405 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
406 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
407};
408
409BT_HIDDEN
410struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
273b65be 411{
8deee039
PP
412 int ret;
413 unsigned int alignment, size;
414 struct bt_ctf_field_type *field_type = NULL;
415
416 if (alias >= NR_FIELD_TYPE_ALIAS) {
417 goto end;
418 }
419
420 alignment = field_type_aliases_alignments[alias];
421 size = field_type_aliases_sizes[alias];
422 field_type = bt_ctf_field_type_integer_create(size);
423 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
424 if (ret) {
8138bfe1 425 BT_OBJECT_PUT_REF_AND_RESET(field_type);
8deee039
PP
426 }
427end:
428 return field_type;
273b65be
JG
429}
430
319fd969 431BT_HIDDEN
086dc475 432const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order)
273b65be 433{
8deee039
PP
434 const char *string;
435
436 switch (byte_order) {
086dc475 437 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
8deee039
PP
438 string = "le";
439 break;
086dc475 440 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
8deee039
PP
441 string = "be";
442 break;
086dc475 443 case BT_CTF_BYTE_ORDER_NATIVE:
8deee039
PP
444 string = "native";
445 break;
446 default:
447 abort();
448 }
449
450 return string;
273b65be 451}
This page took 0.065058 seconds and 4 git commands to generate.