Free the callbacks on iter_destroy
[babeltrace.git] / formats / ctf / ctf.c
CommitLineData
fc93b2bd
MD
1/*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 9 *
ccd7e1c8
MD
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:
fc93b2bd 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
fc93b2bd
MD
19 */
20
21#include <babeltrace/format.h>
22#include <babeltrace/ctf/types.h>
bbefb8dd 23#include <babeltrace/ctf/metadata.h>
70bd0a12 24#include <babeltrace/babeltrace-internal.h>
0f980a35 25#include <inttypes.h>
b4c19c1e 26#include <stdio.h>
0f980a35
MD
27#include <uuid/uuid.h>
28#include <sys/mman.h>
bbefb8dd 29#include <errno.h>
b4c19c1e 30#include <endian.h>
bbefb8dd 31#include <sys/types.h>
65102a8c 32#include <sys/stat.h>
bbefb8dd 33#include <fcntl.h>
65102a8c 34#include <dirent.h>
bbefb8dd 35#include <glib.h>
65102a8c
MD
36#include <unistd.h>
37#include <stdlib.h>
38
65102a8c
MD
39#include "metadata/ctf-scanner.h"
40#include "metadata/ctf-parser.h"
41#include "metadata/ctf-ast.h"
42
0f980a35
MD
43/*
44 * We currently simply map a page to read the packet header and packet
8c572eba 45 * context to get the packet length and content length. (in bits)
0f980a35 46 */
8c572eba
MD
47#define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35
MD
49#define UUID_LEN 16 /* uuid by value len */
50
a0fe7d97
MD
51#ifndef min
52#define min(a, b) (((a) < (b)) ? (a) : (b))
53#endif
54
65102a8c 55extern int yydebug;
bbefb8dd 56
e9378815 57static
bbefb8dd 58struct trace_descriptor *ctf_open_trace(const char *path, int flags);
e9378815 59static
bbefb8dd 60void ctf_close_trace(struct trace_descriptor *descriptor);
fc93b2bd 61
1ae19169
MD
62static
63rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
64 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
65 [ CTF_TYPE_FLOAT ] = ctf_float_read,
66 [ CTF_TYPE_ENUM ] = ctf_enum_read,
67 [ CTF_TYPE_STRING ] = ctf_string_read,
68 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
69 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
70 [ CTF_TYPE_ARRAY ] = ctf_array_read,
71 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
72};
73
1ae19169
MD
74static
75rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
76 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
77 [ CTF_TYPE_FLOAT ] = ctf_float_write,
78 [ CTF_TYPE_ENUM ] = ctf_enum_write,
79 [ CTF_TYPE_STRING ] = ctf_string_write,
80 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
81 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
82 [ CTF_TYPE_ARRAY ] = ctf_array_write,
83 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
84};
85
1ae19169 86static
d11e9c49 87struct format ctf_format = {
bbefb8dd
MD
88 .open_trace = ctf_open_trace,
89 .close_trace = ctf_close_trace,
fc93b2bd
MD
90};
91
2b9a764d
MD
92static
93void ctf_update_timestamp(struct ctf_stream *stream,
94 struct definition_integer *integer_definition)
95{
96 struct declaration_integer *integer_declaration =
97 integer_definition->declaration;
98 uint64_t oldval, newval, updateval;
99
7f3f572b 100 if (unlikely(integer_declaration->len == 64)) {
2b9a764d
MD
101 stream->timestamp = integer_definition->value._unsigned;
102 return;
103 }
104 /* keep low bits */
105 oldval = stream->timestamp;
106 oldval &= (1ULL << integer_declaration->len) - 1;
107 newval = integer_definition->value._unsigned;
108 /* Test for overflow by comparing low bits */
109 if (newval < oldval)
110 newval += 1ULL << integer_declaration->len;
111 /* updateval contains old high bits, and new low bits (sum) */
112 updateval = stream->timestamp;
113 updateval &= ~((1ULL << integer_declaration->len) - 1);
114 updateval += newval;
115 stream->timestamp = updateval;
116}
117
31262354 118static
764af3f4 119int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
31262354
MD
120{
121 struct ctf_stream_pos *pos =
122 container_of(ppos, struct ctf_stream_pos, parent);
764af3f4 123 struct ctf_stream_class *stream_class = stream->stream_class;
42dc00b7 124 struct ctf_stream_event *event;
31262354 125 uint64_t id = 0;
31262354
MD
126 int ret;
127
5f643ed7
MD
128 ctf_pos_get_event(pos);
129
7f3f572b 130 if (unlikely(pos->offset == EOF))
31262354 131 return EOF;
5f643ed7 132 assert(pos->offset < pos->content_size);
31262354
MD
133
134 /* Read event header */
7f3f572b 135 if (likely(stream->stream_event_header)) {
a35173fe 136 struct definition_integer *integer_definition;
ccdb988e 137 struct definition *variant;
a35173fe 138
e28d4618 139 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 140 if (unlikely(ret))
31262354
MD
141 goto error;
142 /* lookup event id */
e28d4618 143 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
144 if (integer_definition) {
145 id = integer_definition->value._unsigned;
146 } else {
147 struct definition_enum *enum_definition;
148
e28d4618 149 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
150 if (enum_definition) {
151 id = enum_definition->integer->value._unsigned;
152 }
31262354 153 }
764af3f4 154
e28d4618 155 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
156 if (variant) {
157 integer_definition = lookup_integer(variant, "id", FALSE);
158 if (integer_definition) {
159 id = integer_definition->value._unsigned;
160 }
161 }
c87a8eb2 162 stream->event_id = id;
ccdb988e 163
764af3f4 164 /* lookup timestamp */
5e2eb0ae 165 stream->has_timestamp = 0;
e28d4618 166 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 167 if (integer_definition) {
2b9a764d 168 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 169 stream->has_timestamp = 1;
a35173fe 170 } else {
ccdb988e
MD
171 if (variant) {
172 integer_definition = lookup_integer(variant, "timestamp", FALSE);
a35173fe 173 if (integer_definition) {
2b9a764d 174 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 175 stream->has_timestamp = 1;
a35173fe
MD
176 }
177 }
764af3f4 178 }
31262354
MD
179 }
180
181 /* Read stream-declared event context */
e28d4618
MD
182 if (stream->stream_event_context) {
183 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
184 if (ret)
185 goto error;
186 }
187
7f3f572b 188 if (unlikely(id >= stream_class->events_by_id->len)) {
31262354
MD
189 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
190 return -EINVAL;
191 }
e28d4618 192 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 193 if (unlikely(!event)) {
31262354
MD
194 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
195 return -EINVAL;
196 }
197
198 /* Read event-declared event context */
e28d4618
MD
199 if (event->event_context) {
200 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
201 if (ret)
202 goto error;
203 }
204
205 /* Read event payload */
7f3f572b 206 if (likely(event->event_fields)) {
e28d4618 207 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
208 if (ret)
209 goto error;
210 }
211
212 return 0;
213
214error:
215 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
216 return ret;
217}
218
219static
764af3f4 220int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
31262354 221{
764af3f4 222 struct ctf_stream_class *stream_class = stream->stream_class;
42dc00b7 223 struct ctf_stream_event *event;
c87a8eb2 224 uint64_t id;
31262354
MD
225 int ret;
226
c87a8eb2
MD
227 id = stream->event_id;
228
31262354 229 /* print event header */
7f3f572b 230 if (likely(stream->stream_event_header)) {
e28d4618 231 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
232 if (ret)
233 goto error;
234 }
235
236 /* print stream-declared event context */
e28d4618
MD
237 if (stream->stream_event_context) {
238 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
239 if (ret)
240 goto error;
241 }
242
7f3f572b 243 if (unlikely(id >= stream_class->events_by_id->len)) {
31262354
MD
244 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
245 return -EINVAL;
246 }
e28d4618 247 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 248 if (unlikely(!event)) {
31262354
MD
249 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
250 return -EINVAL;
251 }
252
253 /* print event-declared event context */
e28d4618
MD
254 if (event->event_context) {
255 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
256 if (ret)
257 goto error;
258 }
259
260 /* Read and print event payload */
7f3f572b 261 if (likely(event->event_fields)) {
e28d4618 262 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
263 if (ret)
264 goto error;
265 }
266
267 return 0;
268
269error:
270 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
271 return ret;
272}
273
8563e754 274void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
275{
276 pos->fd = fd;
277 pos->mmap_offset = 0;
278 pos->packet_size = 0;
279 pos->content_size = 0;
280 pos->content_size_loc = NULL;
281 pos->base = NULL;
282 pos->offset = 0;
283 pos->dummy = false;
8c572eba 284 pos->cur_index = 0;
8563e754
MD
285 if (fd >= 0)
286 pos->packet_index = g_array_new(FALSE, TRUE,
287 sizeof(struct packet_index));
288 else
289 pos->packet_index = NULL;
8563e754
MD
290 switch (open_flags & O_ACCMODE) {
291 case O_RDONLY:
292 pos->prot = PROT_READ;
293 pos->flags = MAP_PRIVATE;
294 pos->parent.rw_table = read_dispatch_table;
31262354 295 pos->parent.event_cb = ctf_read_event;
8563e754 296 break;
8563e754
MD
297 case O_RDWR:
298 pos->prot = PROT_WRITE; /* Write has priority */
299 pos->flags = MAP_SHARED;
300 pos->parent.rw_table = write_dispatch_table;
31262354 301 pos->parent.event_cb = ctf_write_event;
8563e754 302 if (fd >= 0)
847bf71a 303 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
8563e754
MD
304 break;
305 default:
306 assert(0);
8c572eba
MD
307 }
308}
309
46322b33 310void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
311{
312 int ret;
313
314 if (pos->prot == PROT_WRITE && pos->content_size_loc)
315 *pos->content_size_loc = pos->offset;
316 if (pos->base) {
317 /* unmap old base */
318 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
319 if (ret) {
46322b33 320 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
321 strerror(errno));
322 assert(0);
323 }
324 }
325 (void) g_array_free(pos->packet_index, TRUE);
326}
327
847bf71a 328void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
0f980a35 329{
75cc2c35
MD
330 struct ctf_file_stream *file_stream =
331 container_of(pos, struct ctf_file_stream, pos);
0f980a35 332 int ret;
8c572eba
MD
333 off_t off;
334 struct packet_index *index;
0f980a35 335
8c572eba
MD
336 if (pos->prot == PROT_WRITE && pos->content_size_loc)
337 *pos->content_size_loc = pos->offset;
0f980a35
MD
338
339 if (pos->base) {
340 /* unmap old base */
8c572eba 341 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 342 if (ret) {
46322b33 343 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
344 strerror(errno));
345 assert(0);
346 }
847bf71a 347 pos->base = NULL;
0f980a35
MD
348 }
349
8c572eba 350 /*
46322b33 351 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
352 * except to get exactly at the beginning of the next packet.
353 */
354 if (pos->prot == PROT_WRITE) {
989c73bc
MD
355 switch (whence) {
356 case SEEK_CUR:
357 /* The writer will add padding */
358 assert(pos->offset + offset == pos->packet_size);
8c572eba 359 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
360 break;
361 case SEEK_SET:
362 assert(offset == 0); /* only seek supported for now */
363 pos->cur_index = 0;
364 break;
365 default:
366 assert(0);
367 }
8c572eba
MD
368 pos->content_size = -1U; /* Unknown at this point */
369 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
370 off = posix_fallocate(pos->fd, pos->mmap_offset,
371 pos->packet_size / CHAR_BIT);
8c572eba 372 assert(off >= 0);
847bf71a 373 pos->offset = 0;
8c572eba 374 } else {
5f643ed7 375 read_next_packet:
847bf71a
MD
376 switch (whence) {
377 case SEEK_CUR:
5f643ed7
MD
378 if (pos->offset == EOF)
379 return;
847bf71a
MD
380 /* The reader will expect us to skip padding */
381 assert(pos->offset + offset == pos->content_size);
8c572eba 382 ++pos->cur_index;
847bf71a
MD
383 break;
384 case SEEK_SET:
385 assert(offset == 0); /* only seek supported for now */
386 pos->cur_index = 0;
387 break;
388 default:
389 assert(0);
390 }
391 if (pos->cur_index >= pos->packet_index->len) {
670977d3 392 pos->offset = EOF;
847bf71a
MD
393 return;
394 }
8c572eba
MD
395 index = &g_array_index(pos->packet_index, struct packet_index,
396 pos->cur_index);
397 pos->mmap_offset = index->offset;
398
399 /* Lookup context/packet size in index */
2d0bea29 400 file_stream->parent.timestamp = index->timestamp_begin;
8c572eba
MD
401 pos->content_size = index->content_size;
402 pos->packet_size = index->packet_size;
5f643ed7 403 if (index->data_offset < index->content_size) {
75cc2c35 404 pos->offset = 0; /* will read headers */
5f643ed7
MD
405 } else if (index->data_offset == index->content_size) {
406 /* empty packet */
407 pos->offset = index->data_offset;
408 offset = 0;
409 goto read_next_packet;
7eda6fc7 410 } else {
2b9a764d
MD
411 pos->offset = EOF;
412 return;
413 }
8c572eba 414 }
0f980a35 415 /* map new base. Need mapping length from header. */
8c572eba
MD
416 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
417 pos->flags, pos->fd, pos->mmap_offset);
847bf71a
MD
418 if (pos->base == MAP_FAILED) {
419 fprintf(stdout, "[error] mmap error %s.\n",
420 strerror(errno));
421 assert(0);
422 }
75cc2c35
MD
423
424 /* update trace_packet_header and stream_packet_context */
2d0bea29 425 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
75cc2c35 426 /* Read packet header */
2d0bea29 427 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
428 assert(!ret);
429 }
2d0bea29 430 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
75cc2c35 431 /* Read packet context */
2d0bea29 432 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
433 assert(!ret);
434 }
0f980a35
MD
435}
436
b4c19c1e
MD
437static
438int packet_metadata(struct ctf_trace *td, FILE *fp)
439{
440 uint32_t magic;
441 size_t len;
442 int ret = 0;
443
444 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 445 if (len != 1) {
b4c19c1e
MD
446 goto end;
447 }
448 if (magic == TSDL_MAGIC) {
449 ret = 1;
450 td->byte_order = BYTE_ORDER;
0d336fdf 451 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
452 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
453 ret = 1;
454 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
455 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 456 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
457 }
458end:
459 rewind(fp);
460 return ret;
461}
462
5c262147
MD
463/*
464 * Returns 0 on success, -1 on error.
465 */
466static
467int check_version(unsigned int major, unsigned int minor)
468{
469 switch (major) {
470 case 1:
471 switch (minor) {
472 case 8:
473 return 0;
474 default:
475 goto warning;
476 }
477 default:
478 goto warning;
479
480 }
481
482 /* eventually return an error instead of warning */
483warning:
484 fprintf(stdout, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
485 major, minor);
486 return 0;
487}
488
b4c19c1e
MD
489static
490int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
491 FILE *out)
492{
493 struct metadata_packet_header header;
a0fe7d97 494 size_t readlen, writelen, toread;
f8254867 495 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
496 int ret = 0;
497
a91a962e 498 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 499 if (readlen < 1)
b4c19c1e
MD
500 return -EINVAL;
501
502 if (td->byte_order != BYTE_ORDER) {
503 header.magic = GUINT32_SWAP_LE_BE(header.magic);
504 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
505 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
506 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
507 }
508 if (header.checksum)
509 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
510 if (header.compression_scheme) {
511 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
512 header.compression_scheme);
513 return -EINVAL;
514 }
515 if (header.encryption_scheme) {
516 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
517 header.encryption_scheme);
518 return -EINVAL;
519 }
520 if (header.checksum_scheme) {
521 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
522 header.checksum_scheme);
523 return -EINVAL;
524 }
5c262147
MD
525 if (check_version(header.major, header.minor) < 0)
526 return -EINVAL;
b4c19c1e
MD
527 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
528 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
529 CTF_TRACE_SET_FIELD(td, uuid);
530 } else {
531 if (uuid_compare(header.uuid, td->uuid))
532 return -EINVAL;
533 }
534
255b2138 535 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
536
537 for (;;) {
f8254867 538 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
539 if (ferror(in)) {
540 ret = -EINVAL;
541 break;
542 }
4152822b 543 if (babeltrace_debug) {
f8254867 544 buf[readlen] = '\0';
4152822b
MD
545 fprintf(stdout, "[debug] metadata packet read: %s\n",
546 buf);
547 }
548
b4c19c1e
MD
549 writelen = fwrite(buf, sizeof(char), readlen, out);
550 if (writelen < readlen) {
551 ret = -EIO;
552 break;
553 }
554 if (ferror(out)) {
555 ret = -EINVAL;
556 break;
557 }
a0fe7d97
MD
558 toread -= readlen;
559 if (!toread) {
7f4b5c4d 560 ret = 0; /* continue reading next packet */
ddbc52af 561 goto read_padding;
a0fe7d97 562 }
b4c19c1e
MD
563 }
564 return ret;
ddbc52af
MD
565
566read_padding:
567 toread = (header.packet_size - header.content_size) / CHAR_BIT;
568 ret = fseek(in, toread, SEEK_CUR);
569 if (ret < 0) {
570 fprintf(stdout, "[warning] Missing padding at end of file\n");
571 ret = 0;
572 }
573 return ret;
b4c19c1e
MD
574}
575
576static
577int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
578 char **buf)
579{
580 FILE *in, *out;
581 size_t size;
582 int ret;
583
584 in = *fp;
c4f5487e
MD
585 /*
586 * Using strlen on *buf instead of size of open_memstream
587 * because its size includes garbage at the end (after final
588 * \0). This is the allocated size, not the actual string size.
589 */
b4c19c1e
MD
590 out = open_memstream(buf, &size);
591 if (out == NULL)
592 return -errno;
593
594 for (;;) {
595 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 596 if (ret) {
b4c19c1e 597 break;
7f4b5c4d
MD
598 }
599 if (feof(in)) {
600 ret = 0;
b4c19c1e
MD
601 break;
602 }
603 }
604 fclose(out); /* flush the buffer */
605 fclose(in);
606 /* open for reading */
c4f5487e 607 *fp = fmemopen(*buf, strlen(*buf), "rb");
b4c19c1e
MD
608 return 0;
609}
610
65102a8c 611static
46322b33 612int ctf_open_trace_metadata_read(struct ctf_trace *td)
65102a8c
MD
613{
614 struct ctf_scanner *scanner;
2d0bea29 615 struct ctf_file_stream *metadata_stream;
65102a8c 616 FILE *fp;
b4c19c1e 617 char *buf = NULL;
65102a8c
MD
618 int ret = 0;
619
2d0bea29
MD
620 metadata_stream = g_new0(struct ctf_file_stream, 1);
621 td->metadata = &metadata_stream->parent;
622 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
623 if (metadata_stream->pos.fd < 0) {
65102a8c 624 fprintf(stdout, "Unable to open metadata.\n");
2d0bea29 625 return metadata_stream->pos.fd;
65102a8c
MD
626 }
627
628 if (babeltrace_debug)
629 yydebug = 1;
630
2d0bea29 631 fp = fdopen(metadata_stream->pos.fd, "r");
65102a8c 632 if (!fp) {
46322b33 633 fprintf(stdout, "[error] Unable to open metadata stream.\n");
65102a8c
MD
634 ret = -errno;
635 goto end_stream;
636 }
637
b4c19c1e
MD
638 if (packet_metadata(td, fp)) {
639 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
640 if (ret)
641 goto end_packet_read;
da75b0f7 642 } else {
5c262147
MD
643 unsigned int major, minor;
644 ssize_t nr_items;
8c2df3f5 645
da75b0f7 646 td->byte_order = BYTE_ORDER;
8c2df3f5 647
5c262147
MD
648 /* Check text-only metadata header and version */
649 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
650 if (nr_items < 2)
651 fprintf(stdout, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
652 if (check_version(major, minor) < 0) {
653 ret = -EINVAL;
654 goto end_packet_read;
655 }
8c2df3f5 656 rewind(fp);
b4c19c1e
MD
657 }
658
65102a8c
MD
659 scanner = ctf_scanner_alloc(fp);
660 if (!scanner) {
46322b33 661 fprintf(stdout, "[error] Error allocating scanner\n");
65102a8c
MD
662 ret = -ENOMEM;
663 goto end_scanner_alloc;
664 }
665 ret = ctf_scanner_append_ast(scanner);
666 if (ret) {
46322b33 667 fprintf(stdout, "[error] Error creating AST\n");
65102a8c
MD
668 goto end;
669 }
670
671 if (babeltrace_debug) {
672 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
673 if (ret) {
46322b33 674 fprintf(stdout, "[error] Error visiting AST for XML output\n");
65102a8c
MD
675 goto end;
676 }
677 }
678
679 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
680 if (ret) {
46322b33 681 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
682 goto end;
683 }
684 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
ac5c6ca0 685 td, td->byte_order);
65102a8c 686 if (ret) {
46322b33 687 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
688 goto end;
689 }
690end:
691 ctf_scanner_free(scanner);
692end_scanner_alloc:
b4c19c1e 693end_packet_read:
65102a8c 694 fclose(fp);
b4c19c1e 695 free(buf);
65102a8c 696end_stream:
2d0bea29
MD
697 close(metadata_stream->pos.fd);
698 if (ret)
699 g_free(metadata_stream);
0f980a35
MD
700 return ret;
701}
702
e28d4618 703static
42dc00b7
MD
704struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
705 struct ctf_stream *stream,
706 struct ctf_event *event)
e28d4618 707{
42dc00b7 708 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
e28d4618
MD
709
710 if (event->context_decl) {
711 struct definition *definition =
712 event->context_decl->p.definition_new(&event->context_decl->p,
713 stream->parent_def_scope, 0, 0, "event.context");
714 if (!definition) {
715 goto error;
716 }
42dc00b7 717 stream_event->event_context = container_of(definition,
e28d4618 718 struct definition_struct, p);
42dc00b7 719 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
720 }
721 if (event->fields_decl) {
722 struct definition *definition =
723 event->fields_decl->p.definition_new(&event->fields_decl->p,
724 stream->parent_def_scope, 0, 0, "event.fields");
725 if (!definition) {
726 goto error;
727 }
42dc00b7 728 stream_event->event_fields = container_of(definition,
e28d4618 729 struct definition_struct, p);
42dc00b7 730 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 731 }
42dc00b7 732 return stream_event;
e28d4618
MD
733
734error:
42dc00b7
MD
735 if (stream_event->event_fields)
736 definition_unref(&stream_event->event_fields->p);
737 if (stream_event->event_context)
738 definition_unref(&stream_event->event_context->p);
e28d4618
MD
739 return NULL;
740}
741
742static
743int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
744{
745 struct ctf_stream_class *stream_class;
746 int ret;
747 int i;
748
749 if (stream->stream_definitions_created)
750 return 0;
751
752 stream_class = stream->stream_class;
753
754 if (stream_class->packet_context_decl) {
755 struct definition *definition =
756 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
757 stream->parent_def_scope, 0, 0, "stream.packet.context");
758 if (!definition) {
759 ret = -EINVAL;
760 goto error;
761 }
762 stream->stream_packet_context = container_of(definition,
763 struct definition_struct, p);
764 stream->parent_def_scope = stream->stream_packet_context->p.scope;
765 }
766 if (stream_class->event_header_decl) {
767 struct definition *definition =
768 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
769 stream->parent_def_scope, 0, 0, "stream.event.header");
770 if (!definition) {
771 ret = -EINVAL;
772 goto error;
773 }
774 stream->stream_event_header =
775 container_of(definition, struct definition_struct, p);
776 stream->parent_def_scope = stream->stream_event_header->p.scope;
777 }
778 if (stream_class->event_context_decl) {
779 struct definition *definition =
780 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
781 stream->parent_def_scope, 0, 0, "stream.event.context");
782 if (!definition) {
783 ret = -EINVAL;
784 goto error;
785 }
786 stream->stream_event_context =
787 container_of(definition, struct definition_struct, p);
788 stream->parent_def_scope = stream->stream_event_context->p.scope;
789 }
790 stream->events_by_id = g_ptr_array_new();
791 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
792 for (i = 0; i < stream->events_by_id->len; i++) {
793 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
42dc00b7 794 struct ctf_stream_event *stream_event;
e28d4618
MD
795
796 if (!event)
797 continue;
42dc00b7
MD
798 stream_event = create_event_definitions(td, stream, event);
799 if (!stream_event)
e28d4618 800 goto error_event;
42dc00b7 801 g_ptr_array_index(stream->events_by_id, i) = stream_event;
e28d4618
MD
802 }
803 return 0;
804
805error_event:
806 for (i = 0; i < stream->events_by_id->len; i++) {
42dc00b7
MD
807 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
808 if (stream_event)
809 g_free(stream_event);
e28d4618
MD
810 }
811 g_ptr_array_free(stream->events_by_id, TRUE);
812error:
813 if (stream->stream_event_context)
814 definition_unref(&stream->stream_event_context->p);
815 if (stream->stream_event_header)
816 definition_unref(&stream->stream_event_header->p);
817 if (stream->stream_packet_context)
818 definition_unref(&stream->stream_packet_context->p);
819 return ret;
820}
821
0f980a35
MD
822
823static
46322b33 824int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
825 struct ctf_file_stream *file_stream)
826{
aa6bffae 827 struct ctf_stream_class *stream;
0f980a35 828 int len_index;
46322b33 829 struct ctf_stream_pos *pos;
0f980a35
MD
830 struct stat filestats;
831 struct packet_index packet_index;
832 int first_packet = 1;
833 int ret;
834
835 pos = &file_stream->pos;
836
837 ret = fstat(pos->fd, &filestats);
838 if (ret < 0)
839 return ret;
840
8895362d
MD
841 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
842 return -EINVAL;
843
0f980a35
MD
844 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
845 uint64_t stream_id = 0;
846
847 if (pos->base) {
848 /* unmap old base */
8c572eba 849 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 850 if (ret) {
46322b33 851 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
852 strerror(errno));
853 return ret;
854 }
8c572eba 855 pos->base = NULL;
0f980a35
MD
856 }
857 /* map new base. Need mapping length from header. */
8c572eba 858 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 859 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
860 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
861 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
862 pos->offset = 0; /* Position of the packet header */
863
8c572eba
MD
864 packet_index.offset = pos->mmap_offset;
865 packet_index.content_size = 0;
866 packet_index.packet_size = 0;
75cc2c35
MD
867 packet_index.timestamp_begin = 0;
868 packet_index.timestamp_end = 0;
8c572eba 869
0f980a35 870 /* read and check header, set stream id (and check) */
2d0bea29 871 if (file_stream->parent.trace_packet_header) {
0f980a35 872 /* Read packet header */
2d0bea29 873 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
c5e74408
MD
874 if (ret)
875 return ret;
2d0bea29 876 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
877 if (len_index >= 0) {
878 struct definition_integer *defint;
b1a2f580 879 struct definition *field;
0f980a35 880
2d0bea29 881 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
882 assert(field->declaration->id == CTF_TYPE_INTEGER);
883 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
884 assert(defint->declaration->signedness == FALSE);
885 if (defint->value._unsigned != CTF_MAGIC) {
d8ea2d29 886 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
8c572eba
MD
887 defint->value._unsigned,
888 file_stream->pos.packet_index->len,
889 (ssize_t) pos->mmap_offset);
0f980a35
MD
890 return -EINVAL;
891 }
892 }
893
894 /* check uuid */
2d0bea29 895 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
0f980a35
MD
896 if (len_index >= 0) {
897 struct definition_array *defarray;
b1a2f580 898 struct definition *field;
0f980a35
MD
899 uint64_t i;
900 uint8_t uuidval[UUID_LEN];
901
2d0bea29 902 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
903 assert(field->declaration->id == CTF_TYPE_ARRAY);
904 defarray = container_of(field, struct definition_array, p);
3838df27 905 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
906 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
907
908 for (i = 0; i < UUID_LEN; i++) {
909 struct definition *elem;
910 struct definition_integer *defint;
911
912 elem = array_index(defarray, i);
913 assert(elem);
914 defint = container_of(elem, struct definition_integer, p);
915 uuidval[i] = defint->value._unsigned;
916 }
46322b33 917 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
918 if (ret) {
919 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
920 return -EINVAL;
921 }
922 }
923
924
2d0bea29 925 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
926 if (len_index >= 0) {
927 struct definition_integer *defint;
b1a2f580 928 struct definition *field;
0f980a35 929
2d0bea29 930 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
931 assert(field->declaration->id == CTF_TYPE_INTEGER);
932 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
933 assert(defint->declaration->signedness == FALSE);
934 stream_id = defint->value._unsigned;
935 }
936 }
937
2d0bea29 938 if (!first_packet && file_stream->parent.stream_id != stream_id) {
0f980a35
MD
939 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
940 return -EINVAL;
941 }
942 if (first_packet) {
2d0bea29 943 file_stream->parent.stream_id = stream_id;
46322b33 944 if (stream_id >= td->streams->len) {
0f980a35
MD
945 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
946 return -EINVAL;
947 }
46322b33 948 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
949 if (!stream) {
950 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
951 return -EINVAL;
952 }
2d0bea29 953 file_stream->parent.stream_class = stream;
01c76b24 954 ret = create_stream_definitions(td, &file_stream->parent);
862434ec
MD
955 if (ret)
956 return ret;
0f980a35
MD
957 }
958 first_packet = 0;
959
2d0bea29 960 if (file_stream->parent.stream_packet_context) {
dc48ecad 961 /* Read packet context */
2d0bea29 962 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
c5e74408
MD
963 if (ret)
964 return ret;
dc48ecad 965 /* read content size from header */
2d0bea29 966 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
dc48ecad
MD
967 if (len_index >= 0) {
968 struct definition_integer *defint;
b1a2f580 969 struct definition *field;
dc48ecad 970
2d0bea29 971 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
b1a2f580
MD
972 assert(field->declaration->id == CTF_TYPE_INTEGER);
973 defint = container_of(field, struct definition_integer, p);
dc48ecad 974 assert(defint->declaration->signedness == FALSE);
8c572eba 975 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
976 } else {
977 /* Use file size for packet size */
8c572eba 978 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
979 }
980
981 /* read packet size from header */
2d0bea29 982 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
dc48ecad
MD
983 if (len_index >= 0) {
984 struct definition_integer *defint;
b1a2f580 985 struct definition *field;
dc48ecad 986
2d0bea29 987 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
b1a2f580
MD
988 assert(field->declaration->id == CTF_TYPE_INTEGER);
989 defint = container_of(field, struct definition_integer, p);
dc48ecad 990 assert(defint->declaration->signedness == FALSE);
8c572eba 991 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
992 } else {
993 /* Use content size if non-zero, else file size */
8c572eba 994 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 995 }
75cc2c35
MD
996
997 /* read timestamp begin from header */
2d0bea29 998 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
75cc2c35
MD
999 if (len_index >= 0) {
1000 struct definition_integer *defint;
1001 struct definition *field;
1002
2d0bea29 1003 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
75cc2c35
MD
1004 assert(field->declaration->id == CTF_TYPE_INTEGER);
1005 defint = container_of(field, struct definition_integer, p);
1006 assert(defint->declaration->signedness == FALSE);
1007 packet_index.timestamp_begin = defint->value._unsigned;
1008 }
1009
1010 /* read timestamp end from header */
2d0bea29 1011 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
75cc2c35
MD
1012 if (len_index >= 0) {
1013 struct definition_integer *defint;
1014 struct definition *field;
1015
2d0bea29 1016 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
75cc2c35
MD
1017 assert(field->declaration->id == CTF_TYPE_INTEGER);
1018 defint = container_of(field, struct definition_integer, p);
1019 assert(defint->declaration->signedness == FALSE);
1020 packet_index.timestamp_end = defint->value._unsigned;
1021 }
0f980a35
MD
1022 } else {
1023 /* Use file size for packet size */
8c572eba 1024 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 1025 /* Use content size if non-zero, else file size */
8c572eba 1026 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 1027 }
546293fa
MD
1028
1029 /* Validate content size and packet size values */
1030 if (packet_index.content_size > packet_index.packet_size) {
1031 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1032 packet_index.content_size, packet_index.packet_size);
1033 return -EINVAL;
1034 }
1035
58b0b883
MD
1036 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
1037 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
5aebb2ee 1038 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
1039 return -EINVAL;
1040 }
1041
847bf71a
MD
1042 /* Save position after header and context */
1043 packet_index.data_offset = pos->offset;
0f980a35 1044
0f980a35
MD
1045 /* add index to packet array */
1046 g_array_append_val(file_stream->pos.packet_index, packet_index);
1047
8c572eba 1048 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
1049 }
1050
847bf71a
MD
1051 /* Move pos back to beginning of file */
1052 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1053
0f980a35
MD
1054 return 0;
1055}
1056
e28d4618
MD
1057static
1058int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1059{
1060 int ret;
1061
1062 if (td->packet_header_decl) {
1063 struct definition *definition =
1064 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1065 stream->parent_def_scope, 0, 0, "trace.packet.header");
1066 if (!definition) {
1067 ret = -EINVAL;
1068 goto error;
1069 }
1070 stream->trace_packet_header =
1071 container_of(definition, struct definition_struct, p);
1072 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1073 }
1074
1075 return 0;
1076
1077error:
1078 return ret;
1079}
1080
0f980a35
MD
1081/*
1082 * Note: many file streams can inherit from the same stream class
1083 * description (metadata).
1084 */
1085static
46322b33 1086int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
1087{
1088 int ret;
1089 struct ctf_file_stream *file_stream;
1090
46322b33 1091 ret = openat(td->dirfd, path, flags);
0f980a35
MD
1092 if (ret < 0)
1093 goto error;
1094 file_stream = g_new0(struct ctf_file_stream, 1);
8563e754 1095 ctf_init_pos(&file_stream->pos, ret, flags);
2d0bea29 1096 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1097 if (ret)
1098 goto error_def;
0f980a35
MD
1099 ret = create_stream_packet_index(td, file_stream);
1100 if (ret)
1101 goto error_index;
1102 /* Add stream file to stream class */
2d0bea29
MD
1103 g_ptr_array_add(file_stream->parent.stream_class->streams,
1104 &file_stream->parent);
0f980a35
MD
1105 return 0;
1106
1107error_index:
2d0bea29
MD
1108 if (file_stream->parent.trace_packet_header)
1109 definition_unref(&file_stream->parent.trace_packet_header->p);
e28d4618 1110error_def:
46322b33 1111 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1112 close(file_stream->pos.fd);
1113 g_free(file_stream);
1114error:
65102a8c
MD
1115 return ret;
1116}
1117
bbefb8dd 1118static
46322b33 1119int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
1120{
1121 int ret;
65102a8c
MD
1122 struct dirent *dirent;
1123 struct dirent *diriter;
1124 size_t dirent_len;
bbefb8dd 1125
46322b33 1126 td->flags = flags;
bbefb8dd
MD
1127
1128 /* Open trace directory */
46322b33
MD
1129 td->dir = opendir(path);
1130 if (!td->dir) {
dc48ecad 1131 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
1132 ret = -ENOENT;
1133 goto error;
1134 }
1135
46322b33
MD
1136 td->dirfd = open(path, 0);
1137 if (td->dirfd < 0) {
dc48ecad 1138 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
1139 ret = -ENOENT;
1140 goto error_dirfd;
1141 }
0f980a35 1142
65102a8c
MD
1143 /*
1144 * Keep the metadata file separate.
1145 */
bbefb8dd 1146
65102a8c
MD
1147 ret = ctf_open_trace_metadata_read(td);
1148 if (ret) {
1149 goto error_metadata;
1150 }
bbefb8dd
MD
1151
1152 /*
1153 * Open each stream: for each file, try to open, check magic
1154 * number, and get the stream ID to add to the right location in
1155 * the stream array.
bbefb8dd
MD
1156 */
1157
65102a8c 1158 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1159 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1160
65102a8c 1161 dirent = malloc(dirent_len);
bbefb8dd 1162
65102a8c 1163 for (;;) {
46322b33 1164 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1165 if (ret) {
dc48ecad 1166 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 1167 goto readdir_error;
65102a8c
MD
1168 }
1169 if (!diriter)
1170 break;
d8ea2d29
MD
1171 /* Ignore hidden files, ., .. and metadata. */
1172 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1173 || !strcmp(diriter->d_name, "..")
1174 || !strcmp(diriter->d_name, "metadata"))
1175 continue;
dc48ecad
MD
1176 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
1177 if (ret) {
1178 fprintf(stdout, "[error] Open file stream error.\n");
1179 goto readdir_error;
1180 }
65102a8c 1181 }
bbefb8dd 1182
65102a8c 1183 free(dirent);
bbefb8dd 1184 return 0;
65102a8c
MD
1185
1186readdir_error:
1187 free(dirent);
1188error_metadata:
46322b33 1189 close(td->dirfd);
65102a8c 1190error_dirfd:
46322b33 1191 closedir(td->dir);
bbefb8dd
MD
1192error:
1193 return ret;
1194}
1195
e9378815 1196static
bbefb8dd
MD
1197struct trace_descriptor *ctf_open_trace(const char *path, int flags)
1198{
46322b33 1199 struct ctf_trace *td;
bbefb8dd
MD
1200 int ret;
1201
46322b33 1202 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1203
8c572eba 1204 switch (flags & O_ACCMODE) {
bbefb8dd 1205 case O_RDONLY:
b61922b5
MD
1206 if (!path) {
1207 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1208 goto error;
1209 }
bbefb8dd
MD
1210 ret = ctf_open_trace_read(td, path, flags);
1211 if (ret)
1212 goto error;
1213 break;
989c73bc 1214 case O_RDWR:
46322b33
MD
1215 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1216 goto error;
bbefb8dd 1217 default:
46322b33 1218 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1219 goto error;
1220 }
1221
46322b33 1222 return &td->parent;
bbefb8dd
MD
1223error:
1224 g_free(td);
1225 return NULL;
1226}
1227
0f980a35
MD
1228static
1229void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1230{
46322b33 1231 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1232 close(file_stream->pos.fd);
1233}
1234
e9378815 1235static
46322b33 1236void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 1237{
46322b33 1238 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
1239 int i;
1240
46322b33
MD
1241 if (td->streams) {
1242 for (i = 0; i < td->streams->len; i++) {
aa6bffae 1243 struct ctf_stream_class *stream;
0f980a35 1244 int j;
e9378815 1245
46322b33 1246 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
1247 if (!stream)
1248 continue;
2d0bea29 1249 for (j = 0; j < stream->streams->len; j++) {
0f980a35 1250 struct ctf_file_stream *file_stream;
2d0bea29 1251 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
0f980a35
MD
1252 ctf_close_file_stream(file_stream);
1253 }
1254
1255 }
46322b33 1256 g_ptr_array_free(td->streams, TRUE);
0f980a35 1257 }
46322b33 1258 closedir(td->dir);
bbefb8dd
MD
1259 g_free(td);
1260}
1261
7fb21036 1262void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
1263{
1264 int ret;
1265
4c8bfb7e 1266 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
1267 ret = bt_register_format(&ctf_format);
1268 assert(!ret);
1269}
698f0fe4
MD
1270
1271/* TODO: finalize */
This page took 0.08965 seconds and 4 git commands to generate.