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