Fix support for empty streams
[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
MD
902 if (len_index >= 0) {
903 struct definition_integer *defint;
b1a2f580 904 struct definition *field;
0f980a35 905
2d0bea29 906 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
907 assert(field->declaration->id == CTF_TYPE_INTEGER);
908 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
909 assert(defint->declaration->signedness == FALSE);
910 if (defint->value._unsigned != CTF_MAGIC) {
d8ea2d29 911 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
8c572eba
MD
912 defint->value._unsigned,
913 file_stream->pos.packet_index->len,
914 (ssize_t) pos->mmap_offset);
0f980a35
MD
915 return -EINVAL;
916 }
917 }
918
919 /* check uuid */
2d0bea29 920 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
0f980a35
MD
921 if (len_index >= 0) {
922 struct definition_array *defarray;
b1a2f580 923 struct definition *field;
0f980a35
MD
924 uint64_t i;
925 uint8_t uuidval[UUID_LEN];
926
2d0bea29 927 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
928 assert(field->declaration->id == CTF_TYPE_ARRAY);
929 defarray = container_of(field, struct definition_array, p);
3838df27 930 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
931 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
932
933 for (i = 0; i < UUID_LEN; i++) {
934 struct definition *elem;
935 struct definition_integer *defint;
936
937 elem = array_index(defarray, i);
938 assert(elem);
939 defint = container_of(elem, struct definition_integer, p);
940 uuidval[i] = defint->value._unsigned;
941 }
46322b33 942 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
943 if (ret) {
944 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
945 return -EINVAL;
946 }
947 }
948
949
2d0bea29 950 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
951 if (len_index >= 0) {
952 struct definition_integer *defint;
b1a2f580 953 struct definition *field;
0f980a35 954
2d0bea29 955 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
956 assert(field->declaration->id == CTF_TYPE_INTEGER);
957 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
958 assert(defint->declaration->signedness == FALSE);
959 stream_id = defint->value._unsigned;
960 }
961 }
962
2d0bea29 963 if (!first_packet && file_stream->parent.stream_id != stream_id) {
0f980a35
MD
964 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
965 return -EINVAL;
966 }
967 if (first_packet) {
2d0bea29 968 file_stream->parent.stream_id = stream_id;
46322b33 969 if (stream_id >= td->streams->len) {
0f980a35
MD
970 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
971 return -EINVAL;
972 }
46322b33 973 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
974 if (!stream) {
975 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
976 return -EINVAL;
977 }
2d0bea29 978 file_stream->parent.stream_class = stream;
01c76b24 979 ret = create_stream_definitions(td, &file_stream->parent);
862434ec
MD
980 if (ret)
981 return ret;
0f980a35
MD
982 }
983 first_packet = 0;
984
2d0bea29 985 if (file_stream->parent.stream_packet_context) {
dc48ecad 986 /* Read packet context */
2d0bea29 987 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
c5e74408
MD
988 if (ret)
989 return ret;
dc48ecad 990 /* read content size from header */
2d0bea29 991 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
dc48ecad
MD
992 if (len_index >= 0) {
993 struct definition_integer *defint;
b1a2f580 994 struct definition *field;
dc48ecad 995
2d0bea29 996 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
b1a2f580
MD
997 assert(field->declaration->id == CTF_TYPE_INTEGER);
998 defint = container_of(field, struct definition_integer, p);
dc48ecad 999 assert(defint->declaration->signedness == FALSE);
8c572eba 1000 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
1001 } else {
1002 /* Use file size for packet size */
8c572eba 1003 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
1004 }
1005
1006 /* read packet size from header */
2d0bea29 1007 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
dc48ecad
MD
1008 if (len_index >= 0) {
1009 struct definition_integer *defint;
b1a2f580 1010 struct definition *field;
dc48ecad 1011
2d0bea29 1012 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
b1a2f580
MD
1013 assert(field->declaration->id == CTF_TYPE_INTEGER);
1014 defint = container_of(field, struct definition_integer, p);
dc48ecad 1015 assert(defint->declaration->signedness == FALSE);
8c572eba 1016 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
1017 } else {
1018 /* Use content size if non-zero, else file size */
8c572eba 1019 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 1020 }
75cc2c35
MD
1021
1022 /* read timestamp begin from header */
2d0bea29 1023 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
75cc2c35
MD
1024 if (len_index >= 0) {
1025 struct definition_integer *defint;
1026 struct definition *field;
1027
2d0bea29 1028 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
75cc2c35
MD
1029 assert(field->declaration->id == CTF_TYPE_INTEGER);
1030 defint = container_of(field, struct definition_integer, p);
1031 assert(defint->declaration->signedness == FALSE);
1032 packet_index.timestamp_begin = defint->value._unsigned;
1033 }
1034
1035 /* read timestamp end from header */
2d0bea29 1036 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
75cc2c35
MD
1037 if (len_index >= 0) {
1038 struct definition_integer *defint;
1039 struct definition *field;
1040
2d0bea29 1041 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
75cc2c35
MD
1042 assert(field->declaration->id == CTF_TYPE_INTEGER);
1043 defint = container_of(field, struct definition_integer, p);
1044 assert(defint->declaration->signedness == FALSE);
1045 packet_index.timestamp_end = defint->value._unsigned;
1046 }
0f980a35
MD
1047 } else {
1048 /* Use file size for packet size */
8c572eba 1049 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 1050 /* Use content size if non-zero, else file size */
8c572eba 1051 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 1052 }
546293fa
MD
1053
1054 /* Validate content size and packet size values */
1055 if (packet_index.content_size > packet_index.packet_size) {
1056 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1057 packet_index.content_size, packet_index.packet_size);
1058 return -EINVAL;
1059 }
1060
58b0b883
MD
1061 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
1062 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
5aebb2ee 1063 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
1064 return -EINVAL;
1065 }
1066
847bf71a
MD
1067 /* Save position after header and context */
1068 packet_index.data_offset = pos->offset;
0f980a35 1069
0f980a35
MD
1070 /* add index to packet array */
1071 g_array_append_val(file_stream->pos.packet_index, packet_index);
1072
8c572eba 1073 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
1074 }
1075
847bf71a
MD
1076 /* Move pos back to beginning of file */
1077 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1078
0f980a35
MD
1079 return 0;
1080}
1081
e28d4618
MD
1082static
1083int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1084{
1085 int ret;
1086
1087 if (td->packet_header_decl) {
1088 struct definition *definition =
1089 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1090 stream->parent_def_scope, 0, 0, "trace.packet.header");
1091 if (!definition) {
1092 ret = -EINVAL;
1093 goto error;
1094 }
1095 stream->trace_packet_header =
1096 container_of(definition, struct definition_struct, p);
1097 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1098 }
1099
1100 return 0;
1101
1102error:
1103 return ret;
1104}
1105
0f980a35
MD
1106/*
1107 * Note: many file streams can inherit from the same stream class
1108 * description (metadata).
1109 */
1110static
b086c01a
JD
1111int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1112 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1113 int whence))
0f980a35
MD
1114{
1115 int ret;
1116 struct ctf_file_stream *file_stream;
1117
46322b33 1118 ret = openat(td->dirfd, path, flags);
0f980a35
MD
1119 if (ret < 0)
1120 goto error;
1121 file_stream = g_new0(struct ctf_file_stream, 1);
b086c01a
JD
1122
1123 if (move_pos_slow) {
1124 file_stream->pos.move_pos_slow = move_pos_slow;
1125 } else {
1126 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1127 ret = -1;
1128 goto error_def;
1129 }
1130
8563e754 1131 ctf_init_pos(&file_stream->pos, ret, flags);
2d0bea29 1132 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1133 if (ret)
1134 goto error_def;
0f980a35
MD
1135 ret = create_stream_packet_index(td, file_stream);
1136 if (ret)
1137 goto error_index;
1138 /* Add stream file to stream class */
2d0bea29
MD
1139 g_ptr_array_add(file_stream->parent.stream_class->streams,
1140 &file_stream->parent);
0f980a35
MD
1141 return 0;
1142
1143error_index:
2d0bea29
MD
1144 if (file_stream->parent.trace_packet_header)
1145 definition_unref(&file_stream->parent.trace_packet_header->p);
e28d4618 1146error_def:
46322b33 1147 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1148 close(file_stream->pos.fd);
1149 g_free(file_stream);
1150error:
65102a8c
MD
1151 return ret;
1152}
1153
bbefb8dd 1154static
b086c01a
JD
1155int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags,
1156 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 1157 int whence), FILE *metadata_fp)
bbefb8dd
MD
1158{
1159 int ret;
65102a8c
MD
1160 struct dirent *dirent;
1161 struct dirent *diriter;
1162 size_t dirent_len;
bbefb8dd 1163
46322b33 1164 td->flags = flags;
bbefb8dd
MD
1165
1166 /* Open trace directory */
46322b33
MD
1167 td->dir = opendir(path);
1168 if (!td->dir) {
dc48ecad 1169 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
1170 ret = -ENOENT;
1171 goto error;
1172 }
1173
46322b33
MD
1174 td->dirfd = open(path, 0);
1175 if (td->dirfd < 0) {
dc48ecad 1176 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
1177 ret = -ENOENT;
1178 goto error_dirfd;
1179 }
0f980a35 1180
65102a8c
MD
1181 /*
1182 * Keep the metadata file separate.
1183 */
bbefb8dd 1184
ae23d232 1185 ret = ctf_open_trace_metadata_read(td, move_pos_slow, metadata_fp);
65102a8c
MD
1186 if (ret) {
1187 goto error_metadata;
1188 }
bbefb8dd
MD
1189
1190 /*
1191 * Open each stream: for each file, try to open, check magic
1192 * number, and get the stream ID to add to the right location in
1193 * the stream array.
bbefb8dd
MD
1194 */
1195
65102a8c 1196 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1197 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1198
65102a8c 1199 dirent = malloc(dirent_len);
bbefb8dd 1200
65102a8c 1201 for (;;) {
46322b33 1202 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1203 if (ret) {
dc48ecad 1204 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 1205 goto readdir_error;
65102a8c
MD
1206 }
1207 if (!diriter)
1208 break;
d8ea2d29
MD
1209 /* Ignore hidden files, ., .. and metadata. */
1210 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1211 || !strcmp(diriter->d_name, "..")
1212 || !strcmp(diriter->d_name, "metadata"))
1213 continue;
b086c01a 1214 ret = ctf_open_file_stream_read(td, diriter->d_name, flags, move_pos_slow);
dc48ecad
MD
1215 if (ret) {
1216 fprintf(stdout, "[error] Open file stream error.\n");
1217 goto readdir_error;
1218 }
65102a8c 1219 }
bbefb8dd 1220
65102a8c 1221 free(dirent);
bbefb8dd 1222 return 0;
65102a8c
MD
1223
1224readdir_error:
1225 free(dirent);
1226error_metadata:
46322b33 1227 close(td->dirfd);
65102a8c 1228error_dirfd:
46322b33 1229 closedir(td->dir);
bbefb8dd
MD
1230error:
1231 return ret;
1232}
1233
e9378815 1234static
b086c01a
JD
1235struct trace_descriptor *ctf_open_trace(const char *path, int flags,
1236 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 1237 int whence), FILE *metadata_fp)
bbefb8dd 1238{
46322b33 1239 struct ctf_trace *td;
bbefb8dd
MD
1240 int ret;
1241
46322b33 1242 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1243
8c572eba 1244 switch (flags & O_ACCMODE) {
bbefb8dd 1245 case O_RDONLY:
b61922b5
MD
1246 if (!path) {
1247 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1248 goto error;
1249 }
ae23d232 1250 ret = ctf_open_trace_read(td, path, flags, move_pos_slow, metadata_fp);
bbefb8dd
MD
1251 if (ret)
1252 goto error;
1253 break;
989c73bc 1254 case O_RDWR:
46322b33
MD
1255 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1256 goto error;
bbefb8dd 1257 default:
46322b33 1258 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1259 goto error;
1260 }
1261
46322b33 1262 return &td->parent;
bbefb8dd
MD
1263error:
1264 g_free(td);
1265 return NULL;
1266}
1267
0f980a35
MD
1268static
1269void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1270{
46322b33 1271 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1272 close(file_stream->pos.fd);
1273}
1274
e9378815 1275static
46322b33 1276void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 1277{
46322b33 1278 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
1279 int i;
1280
46322b33
MD
1281 if (td->streams) {
1282 for (i = 0; i < td->streams->len; i++) {
aa6bffae 1283 struct ctf_stream_class *stream;
0f980a35 1284 int j;
e9378815 1285
46322b33 1286 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
1287 if (!stream)
1288 continue;
2d0bea29 1289 for (j = 0; j < stream->streams->len; j++) {
0f980a35 1290 struct ctf_file_stream *file_stream;
2d0bea29 1291 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
0f980a35
MD
1292 ctf_close_file_stream(file_stream);
1293 }
1294
1295 }
46322b33 1296 g_ptr_array_free(td->streams, TRUE);
0f980a35 1297 }
46322b33 1298 closedir(td->dir);
bbefb8dd
MD
1299 g_free(td);
1300}
1301
7fb21036 1302void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
1303{
1304 int ret;
1305
4c8bfb7e 1306 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
1307 ret = bt_register_format(&ctf_format);
1308 assert(!ret);
1309}
698f0fe4
MD
1310
1311/* TODO: finalize */
This page took 0.092037 seconds and 4 git commands to generate.