return ret;
}
+/*
+ * One side-effect of this function is to unmap pos mmap base if one is
+ * mapped.
+ */
+static
+int find_data_offset(struct ctf_stream_pos *pos,
+ struct ctf_file_stream *file_stream,
+ struct packet_index *packet_index)
+{
+ uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
+ struct stat filestats;
+ size_t filesize;
+ int ret;
+
+ pos = &file_stream->pos;
+
+ ret = fstat(pos->fd, &filestats);
+ if (ret < 0)
+ return ret;
+ filesize = filestats.st_size;
+
+ /* Deal with empty files */
+ if (!filesize) {
+ return 0;
+ }
+
+begin:
+ if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
+ packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
+ }
+
+ if (pos->base_mma) {
+ /* unmap old base */
+ ret = munmap_align(pos->base_mma);
+ if (ret) {
+ fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
+ strerror(errno));
+ return ret;
+ }
+ pos->base_mma = NULL;
+ }
+ /* map new base. Need mapping length from header. */
+ pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
+ MAP_PRIVATE, pos->fd, pos->mmap_offset);
+ assert(pos->base_mma != MAP_FAILED);
+
+ pos->content_size = packet_map_len;
+ pos->packet_size = packet_map_len;
+ pos->offset = 0; /* Position of the packet header */
+
+ /* update trace_packet_header and stream_packet_context */
+ if (pos->prot == PROT_READ && file_stream->parent.trace_packet_header) {
+ /* Read packet header */
+ ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
+ if (ret) {
+ if (ret == -EFAULT)
+ goto retry;
+ }
+ }
+ if (pos->prot == PROT_READ && file_stream->parent.stream_packet_context) {
+ /* Read packet context */
+ ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
+ if (ret) {
+ if (ret == -EFAULT)
+ goto retry;
+ }
+ }
+ packet_index->data_offset = pos->offset;
+
+ /* unmap old base */
+ ret = munmap_align(pos->base_mma);
+ if (ret) {
+ fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
+ strerror(errno));
+ return ret;
+ }
+ pos->base_mma = NULL;
+
+ return 0;
+
+ /* Retry with larger mapping */
+retry:
+ if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
+ /*
+ * Reached EOF, but still expecting header/context data.
+ */
+ fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
+ return -EFAULT;
+ }
+ /* Double the mapping len, and retry */
+ tmp_map_len = packet_map_len << 1;
+ if (tmp_map_len >> 1 != packet_map_len) {
+ /* Overflow */
+ fprintf(stderr, "[error] Packet mapping length overflow\n");
+ return -EFAULT;
+ }
+ packet_map_len = tmp_map_len;
+ goto begin;
+}
+
+
int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
int fd, int open_flags)
{
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 */
+ if (packet_index->data_offset == -1) {
+ ret = find_data_offset(pos, file_stream, packet_index);
+ if (ret < 0) {
+ return;
+ }
+ }
pos->content_size = packet_index->content_size;
pos->packet_size = packet_index->packet_size;
+ pos->mmap_offset = packet_index->offset;
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) {
index.timestamp_end = be64toh(ctf_index.timestamp_end);
index.events_discarded = be64toh(ctf_index.events_discarded);
index.events_discarded_len = 64;
+ index.data_offset = -1;
stream_id = be64toh(ctf_index.stream_id);
if (!first_packet) {