+ break;
+ }
+ case SEEK_SET:
+ packet_index = &g_array_index(pos->packet_cycles_index,
+ struct packet_index, index);
+ pos->last_events_discarded = packet_index->events_discarded;
+ pos->cur_index = index;
+ file_stream->parent.prev_real_timestamp = 0;
+ file_stream->parent.prev_real_timestamp_end = 0;
+ file_stream->parent.prev_cycles_timestamp = 0;
+ file_stream->parent.prev_cycles_timestamp_end = 0;
+ break;
+ default:
+ assert(0);
+ }
+ if (pos->cur_index >= pos->packet_real_index->len) {
+ /*
+ * When a stream reaches the end of the
+ * file, we need to show the number of
+ * events discarded ourselves, because
+ * there is no next event scheduled to
+ * be printed in the output.
+ */
+ if (file_stream->parent.events_discarded) {
+ /*
+ * We need to check if we are in trace
+ * read or called from packet indexing.
+ * In this last case, the collection is
+ * not there, so we cannot print the
+ * timestamps.
+ */
+ if ((&file_stream->parent)->stream_class->trace->collection) {
+ fflush(stdout);
+ fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events at end of stream between [",
+ file_stream->parent.events_discarded);
+ if (opt_clock_cycles) {
+ ctf_print_timestamp(stderr,
+ &file_stream->parent,
+ file_stream->parent.prev_cycles_timestamp);
+ fprintf(stderr, "] and [");
+ ctf_print_timestamp(stderr, &file_stream->parent,
+ file_stream->parent.prev_cycles_timestamp_end);
+ } else {
+ ctf_print_timestamp(stderr,
+ &file_stream->parent,
+ file_stream->parent.prev_real_timestamp);
+ fprintf(stderr, "] and [");
+ ctf_print_timestamp(stderr, &file_stream->parent,
+ file_stream->parent.prev_real_timestamp_end);
+ }
+ fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
+ fflush(stderr);
+ }
+ file_stream->parent.events_discarded = 0;
+ }
+ pos->offset = EOF;
+ return;
+ }
+ packet_index = &g_array_index(pos->packet_cycles_index,
+ struct packet_index,
+ pos->cur_index);
+ file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
+
+ packet_index = &g_array_index(pos->packet_real_index,
+ struct packet_index,
+ pos->cur_index);
+ file_stream->parent.real_timestamp = packet_index->timestamp_begin;
+ pos->mmap_offset = packet_index->offset;
+
+ /* Lookup context/packet size in index */
+ pos->content_size = packet_index->content_size;
+ pos->packet_size = packet_index->packet_size;
+ if (packet_index->data_offset < packet_index->content_size) {
+ pos->offset = 0; /* will read headers */
+ } else if (packet_index->data_offset == packet_index->content_size) {
+ /* empty packet */
+ pos->offset = packet_index->data_offset;
+ whence = SEEK_CUR;
+ goto read_next_packet;
+ } else {
+ pos->offset = EOF;
+ return;
+ }
+ }
+ /* map new base. Need mapping length from header. */
+ pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
+ pos->flags, pos->fd, pos->mmap_offset);
+ if (pos->base_mma == MAP_FAILED) {
+ fprintf(stderr, "[error] mmap error %s.\n",
+ strerror(errno));
+ assert(0);
+ }
+
+ /* update trace_packet_header and stream_packet_context */
+ if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
+ /* Read packet header */
+ ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
+ assert(!ret);
+ }
+ if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
+ /* Read packet context */
+ ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
+ assert(!ret);
+ }
+}
+
+static
+int packet_metadata(struct ctf_trace *td, FILE *fp)
+{
+ uint32_t magic;
+ size_t len;
+ int ret = 0;
+
+ len = fread(&magic, sizeof(magic), 1, fp);
+ if (len != 1) {
+ goto end;
+ }
+ if (magic == TSDL_MAGIC) {
+ ret = 1;
+ td->byte_order = BYTE_ORDER;
+ CTF_TRACE_SET_FIELD(td, byte_order);
+ } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
+ ret = 1;
+ td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
+ LITTLE_ENDIAN : BIG_ENDIAN;
+ CTF_TRACE_SET_FIELD(td, byte_order);
+ }
+end:
+ rewind(fp);
+ return ret;
+}
+
+/*
+ * Returns 0 on success, -1 on error.
+ */
+static
+int check_version(unsigned int major, unsigned int minor)
+{
+ switch (major) {
+ case 1:
+ switch (minor) {
+ case 8:
+ return 0;
+ default:
+ goto warning;
+ }
+ default:
+ goto warning;
+
+ }
+
+ /* eventually return an error instead of warning */
+warning:
+ fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
+ major, minor);
+ return 0;
+}
+
+static
+int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
+ FILE *out)
+{
+ struct metadata_packet_header header;
+ size_t readlen, writelen, toread;
+ char buf[4096 + 1]; /* + 1 for debug-mode \0 */
+ int ret = 0;
+
+ readlen = fread(&header, header_sizeof(header), 1, in);
+ if (readlen < 1)
+ return -EINVAL;
+
+ if (td->byte_order != BYTE_ORDER) {
+ header.magic = GUINT32_SWAP_LE_BE(header.magic);
+ header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
+ header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
+ header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
+ }
+ if (header.checksum)
+ fprintf(stderr, "[warning] checksum verification not supported yet.\n");
+ if (header.compression_scheme) {
+ fprintf(stderr, "[error] compression (%u) not supported yet.\n",
+ header.compression_scheme);
+ return -EINVAL;
+ }
+ if (header.encryption_scheme) {
+ fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
+ header.encryption_scheme);
+ return -EINVAL;
+ }
+ if (header.checksum_scheme) {
+ fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
+ header.checksum_scheme);
+ return -EINVAL;
+ }
+ if (check_version(header.major, header.minor) < 0)
+ return -EINVAL;
+ if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
+ memcpy(td->uuid, header.uuid, sizeof(header.uuid));
+ CTF_TRACE_SET_FIELD(td, uuid);
+ } else {
+ if (babeltrace_uuid_compare(header.uuid, td->uuid))
+ return -EINVAL;
+ }
+
+ toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
+
+ for (;;) {
+ readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
+ if (ferror(in)) {
+ ret = -EINVAL;
+ break;
+ }
+ if (babeltrace_debug) {
+ buf[readlen] = '\0';
+ fprintf(stderr, "[debug] metadata packet read: %s\n",
+ buf);
+ }
+
+ writelen = fwrite(buf, sizeof(char), readlen, out);
+ if (writelen < readlen) {
+ ret = -EIO;
+ break;
+ }
+ if (ferror(out)) {
+ ret = -EINVAL;
+ break;
+ }
+ toread -= readlen;
+ if (!toread) {
+ ret = 0; /* continue reading next packet */
+ goto read_padding;
+ }
+ }
+ return ret;
+
+read_padding:
+ toread = (header.packet_size - header.content_size) / CHAR_BIT;
+ ret = fseek(in, toread, SEEK_CUR);
+ if (ret < 0) {
+ fprintf(stderr, "[warning] Missing padding at end of file\n");
+ ret = 0;
+ }
+ return ret;
+}
+
+static
+int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
+ char **buf)
+{
+ FILE *in, *out;
+ size_t size;
+ int ret;