Fix: babeltrace assert() triggered by directories within trace
[babeltrace.git] / formats / ctf / ctf.c
index 91fc20c3972626a59075b13dcf72df981b30f2a3..b3f7699afc925bca91b6fbf0dce5c38aca091ce5 100644 (file)
@@ -87,6 +87,12 @@ void ctf_set_handle(struct trace_descriptor *descriptor,
 
 static
 void ctf_close_trace(struct trace_descriptor *descriptor);
+static
+uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
+               struct bt_trace_handle *handle);
+static
+uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
+               struct bt_trace_handle *handle);
 
 static
 rw_dispatch read_dispatch_table[] = {
@@ -119,8 +125,101 @@ struct format ctf_format = {
        .close_trace = ctf_close_trace,
        .set_context = ctf_set_context,
        .set_handle = ctf_set_handle,
+       .timestamp_begin = ctf_timestamp_begin,
+       .timestamp_end = ctf_timestamp_end,
 };
 
+static
+uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
+               struct bt_trace_handle *handle)
+{
+       struct ctf_trace *tin;
+       uint64_t begin = ULLONG_MAX;
+       int i, j;
+
+       tin = container_of(descriptor, struct ctf_trace, parent);
+
+       if (!tin)
+               goto error;
+
+       /* for each stream_class */
+       for (i = 0; i < tin->streams->len; i++) {
+               struct ctf_stream_declaration *stream_class;
+
+               stream_class = g_ptr_array_index(tin->streams, i);
+               if (!stream_class)
+                       continue;
+               /* for each file_stream */
+               for (j = 0; j < stream_class->streams->len; j++) {
+                       struct ctf_stream_definition *stream;
+                       struct ctf_file_stream *cfs;
+                       struct ctf_stream_pos *stream_pos;
+                       struct packet_index *index;
+
+                       stream = g_ptr_array_index(stream_class->streams, j);
+                       cfs = container_of(stream, struct ctf_file_stream,
+                                       parent);
+                       stream_pos = &cfs->pos;
+
+                       index = &g_array_index(stream_pos->packet_index,
+                                       struct packet_index, 0);
+                       if (index->timestamp_begin < begin)
+                               begin = index->timestamp_begin;
+               }
+       }
+
+       return begin;
+
+error:
+       return -1ULL;
+}
+
+static
+uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
+               struct bt_trace_handle *handle)
+{
+       struct ctf_trace *tin;
+       uint64_t end = 0;
+       int i, j;
+
+       tin = container_of(descriptor, struct ctf_trace, parent);
+
+       if (!tin)
+               goto error;
+
+       /* for each stream_class */
+       for (i = 0; i < tin->streams->len; i++) {
+               struct ctf_stream_declaration *stream_class;
+
+               stream_class = g_ptr_array_index(tin->streams, i);
+               if (!stream_class)
+                       continue;
+               /* for each file_stream */
+               for (j = 0; j < stream_class->streams->len; j++) {
+                       struct ctf_stream_definition *stream;
+                       struct ctf_file_stream *cfs;
+                       struct ctf_stream_pos *stream_pos;
+                       struct packet_index *index;
+
+                       stream = g_ptr_array_index(stream_class->streams, j);
+                       cfs = container_of(stream, struct ctf_file_stream,
+                                       parent);
+                       stream_pos = &cfs->pos;
+
+                       index = &g_array_index(stream_pos->packet_index,
+                                       struct packet_index,
+                                       stream_pos->packet_index->len - 1);
+                       if (index->timestamp_end > end)
+                               end = index->timestamp_end;
+               }
+       }
+
+       return end;
+
+error:
+       return -1ULL;
+}
+
 /*
  * Update stream current timestamp, keep at clock frequency.
  */
@@ -396,7 +495,7 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
        pos->packet_size = 0;
        pos->content_size = 0;
        pos->content_size_loc = NULL;
-       pos->base = NULL;
+       pos->base_mma = NULL;
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
@@ -431,9 +530,9 @@ void ctf_fini_pos(struct ctf_stream_pos *pos)
 
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
-       if (pos->base) {
+       if (pos->base_mma) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               ret = munmap_align(pos->base_mma);
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
@@ -460,15 +559,15 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
 
-       if (pos->base) {
+       if (pos->base_mma) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               ret = munmap_align(pos->base_mma);
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
                        assert(0);
                }
-               pos->base = NULL;
+               pos->base_mma = NULL;
        }
 
        /*
@@ -499,7 +598,7 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                switch (whence) {
                case SEEK_CUR:
                {
-                       uint32_t events_discarded_diff;
+                       uint64_t events_discarded_diff;
 
                        if (pos->offset == EOF) {
                                return;
@@ -515,6 +614,13 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                                                struct packet_index,
                                                pos->cur_index - 1);
                                events_discarded_diff -= packet_index->events_discarded;
+                               /*
+                                * Deal with 32-bit wrap-around if the
+                                * tracer provided a 32-bit field.
+                                */
+                               if (packet_index->events_discarded_len == 32) {
+                                       events_discarded_diff = (uint32_t) events_discarded_diff;
+                               }
                        }
                        file_stream->parent.events_discarded = events_discarded_diff;
                        file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
@@ -548,14 +654,14 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                                 */
                                if ((&file_stream->parent)->stream_class->trace->collection) {
                                        fflush(stdout);
-                                       fprintf(stderr, "[warning] Tracer discarded %d events at end of stream between [",
+                                       fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events at end of stream between [",
                                                        file_stream->parent.events_discarded);
                                        ctf_print_timestamp(stderr, &file_stream->parent,
                                                        file_stream->parent.prev_timestamp);
                                        fprintf(stderr, "] and [");
                                        ctf_print_timestamp(stderr, &file_stream->parent,
                                                        file_stream->parent.prev_timestamp_end);
-                                       fprintf(stderr, "]. You should consider increasing the buffer size.\n");
+                                       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;
@@ -584,9 +690,9 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                }
        }
        /* map new base. Need mapping length from header. */
-       pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
-                        pos->flags, pos->fd, pos->mmap_offset);
-       if (pos->base == MAP_FAILED) {
+       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);
@@ -888,7 +994,8 @@ end:
        ctf_scanner_free(scanner);
 end_scanner_alloc:
 end_packet_read:
-       fclose(fp);
+       if (fp)
+               fclose(fp);
        free(buf);
 end_stream:
        close(metadata_stream->pos.fd);
@@ -1042,19 +1149,20 @@ int create_stream_packet_index(struct ctf_trace *td,
        for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
                uint64_t stream_id = 0;
 
-               if (pos->base) {
+               if (pos->base_mma) {
                        /* unmap old base */
-                       ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+                       ret = munmap_align(pos->base_mma);
                        if (ret) {
                                fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                        strerror(errno));
                                return ret;
                        }
-                       pos->base = NULL;
+                       pos->base_mma = NULL;
                }
                /* map new base. Need mapping length from header. */
-               pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
+               pos->base_mma = mmap_align(MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
                                 MAP_PRIVATE, pos->fd, pos->mmap_offset);
+               assert(pos->base_mma != MAP_FAILED);
                pos->content_size = MAX_PACKET_HEADER_LEN;      /* Unknown at this point */
                pos->packet_size = MAX_PACKET_HEADER_LEN;       /* Unknown at this point */
                pos->offset = 0;        /* Position of the packet header */
@@ -1200,6 +1308,7 @@ int create_stream_packet_index(struct ctf_trace *td,
 
                                field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
                                packet_index.events_discarded = get_unsigned_int(field);
+                               packet_index.events_discarded_len = get_int_len(field);
                        }
                } else {
                        /* Use file size for packet size */
@@ -1217,7 +1326,7 @@ int create_stream_packet_index(struct ctf_trace *td,
 
                if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
                        fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
-                               packet_index.content_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
+                               packet_index.packet_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
                        return -EINVAL;
                }
 
@@ -1269,14 +1378,29 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
                void (*packet_seek)(struct stream_pos *pos, size_t index,
                        int whence))
 {
-       int ret;
+       int ret, fd;
        struct ctf_file_stream *file_stream;
+       struct stat statbuf;
 
-       ret = openat(td->dirfd, path, flags);
-       if (ret < 0) {
+       fd = openat(td->dirfd, path, flags);
+       if (fd < 0) {
                perror("File stream openat()");
+               ret = fd;
                goto error;
        }
+
+       /* Don't try to mmap subdirectories. Skip them, return success. */
+       ret = fstat(fd, &statbuf);
+       if (ret) {
+               perror("File stream fstat()");
+               goto fstat_error;
+       }
+       if (S_ISDIR(statbuf.st_mode)) {
+               fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
+               ret = 0;
+               goto fd_is_dir_ok;
+       }
+
        file_stream = g_new0(struct ctf_file_stream, 1);
 
        if (packet_seek) {
@@ -1287,7 +1411,7 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
                goto error_def;
        }
 
-       ctf_init_pos(&file_stream->pos, ret, flags);
+       ctf_init_pos(&file_stream->pos, fd, flags);
        ret = create_trace_definitions(td, &file_stream->parent);
        if (ret)
                goto error_def;
@@ -1308,8 +1432,10 @@ error_index:
                definition_unref(&file_stream->parent.trace_packet_header->p);
 error_def:
        ctf_fini_pos(&file_stream->pos);
-       close(file_stream->pos.fd);
        g_free(file_stream);
+fd_is_dir_ok:
+fstat_error:
+       close(fd);
 error:
        return ret;
 }
@@ -1330,14 +1456,14 @@ int ctf_open_trace_read(struct ctf_trace *td,
        /* Open trace directory */
        td->dir = opendir(path);
        if (!td->dir) {
-               fprintf(stderr, "[error] Unable to open trace directory.\n");
+               fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
                ret = -ENOENT;
                goto error;
        }
 
        td->dirfd = open(path, 0);
        if (td->dirfd < 0) {
-               fprintf(stderr, "[error] Unable to open trace directory file descriptor.\n");
+               fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
                perror("Trace directory open");
                ret = -errno;
                goto error_dirfd;
@@ -1351,6 +1477,7 @@ int ctf_open_trace_read(struct ctf_trace *td,
 
        ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
        if (ret) {
+               fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
                goto error_metadata;
        }
 
@@ -1448,7 +1575,7 @@ void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
        pos->content_size = 0;
        pos->content_size_loc = NULL;
        pos->fd = mmap_info->fd;
-       pos->base = 0;
+       pos->base_mma = NULL;
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
@@ -1623,6 +1750,18 @@ void ctf_close_trace(struct trace_descriptor *tdp)
                        struct bt_ctf_event_decl *event;
 
                        event = g_ptr_array_index(td->event_declarations, i);
+                       if (event->context_decl)
+                               g_ptr_array_free(event->context_decl, TRUE);
+                       if (event->fields_decl)
+                               g_ptr_array_free(event->fields_decl, TRUE);
+                       if (event->packet_header_decl)
+                               g_ptr_array_free(event->packet_header_decl, TRUE);
+                       if (event->event_context_decl)
+                               g_ptr_array_free(event->event_context_decl, TRUE);
+                       if (event->event_header_decl)
+                               g_ptr_array_free(event->event_header_decl, TRUE);
+                       if (event->packet_context_decl)
+                               g_ptr_array_free(event->packet_context_decl, TRUE);
                        g_free(event);
                }
                g_ptr_array_free(td->event_declarations, TRUE);
This page took 0.028131 seconds and 4 git commands to generate.