Fix: Use the packet's final size when mmap-ing the next packet
[babeltrace.git] / formats / ctf / ctf.c
index 6b292a7e567fff53a00abc1b04f91beae8a7cec3..60d9c9f9459d159b7627c9e76927409eccd55272 100644 (file)
@@ -33,7 +33,7 @@
 #include <babeltrace/ctf/events-internal.h>
 #include <babeltrace/trace-handle-internal.h>
 #include <babeltrace/context-internal.h>
-#include <babeltrace/uuid.h>
+#include <babeltrace/compat/uuid.h>
 #include <babeltrace/endian.h>
 #include <inttypes.h>
 #include <stdio.h>
@@ -51,7 +51,7 @@
 #include "metadata/ctf-parser.h"
 #include "metadata/ctf-ast.h"
 #include "events-private.h"
-#include "memstream.h"
+#include <babeltrace/compat/memstream.h>
 
 #define LOG2_CHAR_BIT  3
 
@@ -670,7 +670,7 @@ int ctf_fini_pos(struct ctf_stream_pos *pos)
 
 /*
  * for SEEK_CUR: go to next packet.
- * for SEEK_POS: go to packet numer (index).
+ * for SEEK_SET: go to packet numer (index).
  */
 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 {
@@ -682,6 +682,14 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
        off_t off;
        struct packet_index *packet_index;
 
+       switch (whence) {
+       case SEEK_CUR:
+       case SEEK_SET:  /* Fall-through */
+               break;  /* OK */
+       default:
+               assert(0);
+       }
+
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
 
@@ -704,7 +712,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                switch (whence) {
                case SEEK_CUR:
                        /* The writer will add padding */
-                       pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
+                       pos->mmap_offset += pos->packet_size / CHAR_BIT;
                        break;
                case SEEK_SET:
                        assert(index == 0);     /* only seek supported for now */
@@ -729,6 +737,9 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                        if (pos->offset == EOF) {
                                return;
                        }
+                       assert(pos->cur_index < pos->packet_cycles_index->len);
+                       assert(pos->cur_index < pos->packet_real_index->len);
+
                        /* For printing discarded event count */
                        packet_index = &g_array_index(pos->packet_cycles_index,
                                        struct packet_index, pos->cur_index);
@@ -766,6 +777,10 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                        break;
                }
                case SEEK_SET:
+                       if (index >= pos->packet_cycles_index->len) {
+                               pos->offset = EOF;
+                               return;
+                       }
                        packet_index = &g_array_index(pos->packet_cycles_index,
                                        struct packet_index, index);
                        pos->last_events_discarded = packet_index->events_discarded;
@@ -1307,6 +1322,31 @@ error:
        return ret;
 }
 
+static
+int stream_assign_class(struct ctf_trace *td,
+               struct ctf_file_stream *file_stream,
+               uint64_t stream_id)
+{
+       struct ctf_stream_declaration *stream;
+       int ret;
+
+       file_stream->parent.stream_id = stream_id;
+       if (stream_id >= td->streams->len) {
+               fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
+               return -EINVAL;
+       }
+       stream = g_ptr_array_index(td->streams, stream_id);
+       if (!stream) {
+               fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
+               return -EINVAL;
+       }
+       file_stream->parent.stream_class = stream;
+       ret = create_stream_definitions(td, &file_stream->parent);
+       if (ret)
+               return ret;
+       return 0;
+}
+
 static
 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
                        struct ctf_trace *td,
@@ -1314,7 +1354,6 @@ int create_stream_one_packet_index(struct ctf_stream_pos *pos,
                        size_t filesize)
 {
        struct packet_index packet_index;
-       struct ctf_stream_declaration *stream;
        uint64_t stream_id = 0;
        uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
        int first_packet = 0;
@@ -1428,18 +1467,7 @@ begin:
                return -EINVAL;
        }
        if (first_packet) {
-               file_stream->parent.stream_id = stream_id;
-               if (stream_id >= td->streams->len) {
-                       fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
-                       return -EINVAL;
-               }
-               stream = g_ptr_array_index(td->streams, stream_id);
-               if (!stream) {
-                       fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
-                       return -EINVAL;
-               }
-               file_stream->parent.stream_class = stream;
-               ret = create_stream_definitions(td, &file_stream->parent);
+               ret = stream_assign_class(td, file_stream, stream_id);
                if (ret)
                        return ret;
        }
@@ -1580,6 +1608,30 @@ int create_stream_packet_index(struct ctf_trace *td,
        if (ret < 0)
                return ret;
 
+       /* Deal with empty files */
+       if (!filestats.st_size) {
+               if (file_stream->parent.trace_packet_header
+                               || file_stream->parent.stream_packet_context) {
+                       /*
+                        * We expect a trace packet header and/or stream packet
+                        * context. Since a trace needs to have at least one
+                        * packet, empty files are therefore not accepted.
+                        */
+                       fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
+                       return -EINVAL;
+               } else {
+                       /*
+                        * Without trace packet header nor stream packet
+                        * context, a one-packet trace can indeed be empty. This
+                        * is only valid if there is only one stream class: 0.
+                        */
+                       ret = stream_assign_class(td, file_stream, 0);
+                       if (ret)
+                               return ret;
+                       return 0;
+               }
+       }
+
        for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
                ret = create_stream_one_packet_index(pos, td, file_stream,
                        filestats.st_size);
This page took 0.025227 seconds and 4 git commands to generate.