lib: rename include dir to babeltrace2
[babeltrace.git] / lib / ctf-writer / writer.c
1 /*
2 * writer.c
3 *
4 * Babeltrace CTF Writer
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #define BT_LOG_TAG "CTF-WRITER"
30 #include <babeltrace2/lib-logging-internal.h>
31
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>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <inttypes.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <sys/stat.h>
51 #include <unistd.h>
52
53 static
54 void bt_ctf_writer_destroy(struct bt_ctf_object *obj);
55
56 static
57 int init_trace_packet_header(struct bt_ctf_trace *trace)
58 {
59 int ret = 0;
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_field_type(trace,
93 trace_packet_header_type);
94 if (ret) {
95 goto end;
96 }
97 end:
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);
102 return ret;
103 }
104
105 struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
106 {
107 int ret;
108 struct bt_ctf_writer *writer = NULL;
109 unsigned char uuid[16];
110 char *metadata_path = NULL;
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
121 metadata_path = g_build_filename(path, "metadata", NULL);
122
123 bt_ctf_object_init_shared(&writer->base, bt_ctf_writer_destroy);
124 writer->path = g_string_new(path);
125 if (!writer->path) {
126 goto error_destroy;
127 }
128
129 writer->trace = bt_ctf_trace_create();
130 if (!writer->trace) {
131 goto error_destroy;
132 }
133
134 ret = init_trace_packet_header(writer->trace);
135 if (ret) {
136 goto error_destroy;
137 }
138
139 /* Generate a UUID for this writer's trace */
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
146 ret = bt_ctf_trace_set_uuid(writer->trace, uuid);
147 if (ret) {
148 goto error_destroy;
149 }
150
151 bt_ctf_object_set_parent(&writer->trace->common.base, &writer->base);
152 bt_ctf_object_put_ref(writer->trace);
153
154 /* Default to little-endian */
155 ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE);
156 BT_ASSERT(ret == 0);
157
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
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) {
168 perror("open");
169 goto error_destroy;
170 }
171
172 g_free(metadata_path);
173 return writer;
174
175 error_destroy:
176 BT_CTF_OBJECT_PUT_REF_AND_RESET(writer);
177 error:
178 g_free(metadata_path);
179 return writer;
180 }
181
182 void bt_ctf_writer_destroy(struct bt_ctf_object *obj)
183 {
184 struct bt_ctf_writer *writer;
185
186 writer = container_of(obj, struct bt_ctf_writer, base);
187 bt_ctf_writer_flush_metadata(writer);
188 if (writer->path) {
189 g_string_free(writer->path, TRUE);
190 }
191
192 if (writer->metadata_fd > 0) {
193 if (close(writer->metadata_fd)) {
194 perror("close");
195 }
196 }
197
198 bt_ctf_object_try_spec_release(&writer->trace->common.base);
199 g_free(writer);
200 }
201
202 struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
203 {
204 struct bt_ctf_trace *trace = NULL;
205
206 if (!writer) {
207 goto end;
208 }
209
210 trace = writer->trace;
211 bt_ctf_object_get_ref(trace);
212 end:
213 return trace;
214 }
215
216 struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
217 struct bt_ctf_stream_class *stream_class)
218 {
219 struct bt_ctf_stream *stream = NULL;
220 int stream_class_count;
221 bt_bool stream_class_found = BT_FALSE;
222 int i;
223
224 if (!writer || !stream_class) {
225 goto error;
226 }
227
228 /* Make sure the stream class is part of the writer's trace */
229 stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace);
230 if (stream_class_count < 0) {
231 goto error;
232 }
233
234 for (i = 0; i < stream_class_count; i++) {
235 struct bt_ctf_stream_class *existing_stream_class =
236 bt_ctf_trace_get_stream_class_by_index(
237 writer->trace, i);
238
239 if (existing_stream_class == stream_class) {
240 stream_class_found = BT_TRUE;
241 }
242
243 BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class);
244
245 if (stream_class_found) {
246 break;
247 }
248 }
249
250 if (!stream_class_found) {
251 int ret = bt_ctf_trace_add_stream_class(writer->trace,
252 stream_class);
253
254 if (ret) {
255 goto error;
256 }
257 }
258
259 stream = bt_ctf_stream_create_with_id(stream_class, NULL, -1ULL);
260 if (!stream) {
261 goto error;
262 }
263
264 return stream;
265
266 error:
267 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream);
268 return stream;
269 }
270
271 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
272 const char *name,
273 const char *value)
274 {
275 int ret = -1;
276
277 if (!writer || !name || !value) {
278 goto end;
279 }
280
281 ret = bt_ctf_trace_set_environment_field_string(writer->trace,
282 name, value);
283 end:
284 return ret;
285 }
286
287 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer,
288 const char *name, int64_t value)
289 {
290 int ret = -1;
291
292 if (!writer || !name) {
293 goto end;
294 }
295
296 ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name,
297 value);
298 end:
299 return ret;
300 }
301
302 int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
303 struct bt_ctf_clock *clock)
304 {
305 int ret = -1;
306
307 if (!writer || !clock) {
308 goto end;
309 }
310
311 ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class);
312 end:
313 return ret;
314 }
315
316 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
317 {
318 char *metadata_string = NULL;
319
320 if (!writer) {
321 goto end;
322 }
323
324 metadata_string = bt_ctf_trace_get_metadata_string(
325 writer->trace);
326 end:
327 return metadata_string;
328 }
329
330 void 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
339 metadata_string = bt_ctf_trace_get_metadata_string(
340 writer->trace);
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 }
361 end:
362 g_free(metadata_string);
363 }
364
365 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
366 enum bt_ctf_byte_order byte_order)
367 {
368 int ret = 0;
369
370 if (!writer || writer->frozen) {
371 ret = -1;
372 goto end;
373 }
374
375 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
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 }
381 }
382
383 ret = bt_ctf_trace_set_native_byte_order(writer->trace,
384 byte_order);
385 end:
386 return ret;
387 }
388
389 BT_HIDDEN
390 void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
391 {
392 writer->frozen = 1;
393 }
394
395 static
396 const 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
403 static
404 const 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
413 BT_HIDDEN
414 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
415 {
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) {
429 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type);
430 }
431 end:
432 return field_type;
433 }
434
435 BT_HIDDEN
436 const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order)
437 {
438 const char *string;
439
440 switch (byte_order) {
441 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
442 string = "le";
443 break;
444 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
445 string = "be";
446 break;
447 case BT_CTF_BYTE_ORDER_NATIVE:
448 string = "native";
449 break;
450 default:
451 abort();
452 }
453
454 return string;
455 }
This page took 0.041452 seconds and 4 git commands to generate.