Fix: Allow 64-bit packet offset
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 28 Sep 2012 00:56:53 +0000 (20:56 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 28 Sep 2012 00:56:53 +0000 (20:56 -0400)
Offset within a packet is calculated in bits. Therefore, if, for an
unforeseen reason, a 32-bit system would happen to build babeltrace
without large file support, the maximum packet size would have been
limited to 256MB (4GB / 8 bits per byte), because we were using size_t.

Use 64-bit type to hold the offset within a packet from now on.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
converter/babeltrace-log.c
include/babeltrace/ctf/types.h
lib/iterator.c

index f7eb5f768d39c88425f3bff276374f8ba47e9074..d24f011e5ed214785840da4435fe447b4a491c1f 100644 (file)
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
+#include <inttypes.h>
 
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/ctf/types.h>
@@ -53,6 +54,7 @@ static const char metadata_fmt[] =
 "/* CTF 1.8 */\n"
 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
+"typealias integer { size = 64; align = 64; signed = false; } := uint64_t;\n"
 "\n"
 "trace {\n"
 "      major = %u;\n"                  /* major (e.g. 0) */
@@ -67,8 +69,8 @@ static const char metadata_fmt[] =
 "\n"
 "stream {\n"
 "      packet.context := struct {\n"
-"              uint32_t content_size;\n"
-"              uint32_t packet_size;\n"
+"              uint64_t content_size;\n"
+"              uint64_t packet_size;\n"
 "      };\n"
 "%s"                                   /* Stream event header (opt.) */
 "};\n"
@@ -136,24 +138,24 @@ void write_packet_context(struct ctf_stream_pos *pos)
 
        /* content_size */
        ctf_dummy_pos(pos, &dummy);
-       ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
-       ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
+       ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
        assert(!ctf_pos_packet(&dummy));
        
-       ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
-       *(uint32_t *) ctf_get_pos_addr(pos) = -1U;      /* Not known yet */
-       pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos);
-       ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+       ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+       *(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL;    /* Not known yet */
+       pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
+       ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
 
        /* packet_size */
        ctf_dummy_pos(pos, &dummy);
-       ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
-       ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
+       ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
        assert(!ctf_pos_packet(&dummy));
        
-       ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
-       *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size;
-       ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+       ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+       *(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
+       ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
 }
 
 static
@@ -209,7 +211,7 @@ retry:
                write_packet_header(pos, s_uuid);
                write_packet_context(pos);
                if (attempt++ == 1) {
-                       fprintf(stderr, "[Error] Line too large for packet size (%zukB) (discarded)\n",
+                       fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
                                pos->packet_size / CHAR_BIT / 1024);
                        return;
                }
index 6a76c6bbf945c3c2e351a191cdf5b015e368f3ad..c1916a4d75b76b7557031e2ca1a96b6febaa1115 100644 (file)
 #include <unistd.h>
 #include <glib.h>
 #include <stdio.h>
+#include <inttypes.h>
 #include <babeltrace/mmap-align.h>
 
-#define LAST_OFFSET_POISON     ((ssize_t) -1L)
+#define LAST_OFFSET_POISON     ((int64_t) ~0ULL)
 
 struct bt_stream_callbacks;
 
 struct packet_index {
        off_t offset;           /* offset of the packet in the file, in bytes */
-       off_t data_offset;      /* offset of data within the packet, in bits */
+       int64_t data_offset;    /* offset of data within the packet, in bits */
        uint64_t packet_size;   /* packet size, in bits */
        uint64_t content_size;  /* content size, in bits */
        uint64_t timestamp_begin;
        uint64_t timestamp_end;
        uint64_t events_discarded;
-       size_t events_discarded_len;    /* length of the field, in bits */
+       uint64_t events_discarded_len;  /* length of the field, in bits */
 };
 
 /*
@@ -60,13 +61,13 @@ struct ctf_stream_pos {
        /* Current position */
        off_t mmap_offset;      /* mmap offset in the file, in bytes */
        off_t mmap_base_offset; /* offset of start of packet in mmap, in bytes */
-       size_t packet_size;     /* current packet size, in bits */
-       size_t content_size;    /* current content size, in bits */
-       uint32_t *content_size_loc; /* pointer to current content size */
+       uint64_t packet_size;   /* current packet size, in bits */
+       uint64_t content_size;  /* current content size, in bits */
+       uint64_t *content_size_loc; /* pointer to current content size */
        struct mmap_align *base_mma;/* mmap base address */
-       ssize_t offset;         /* offset from base, in bits. EOF for end of file. */
-       ssize_t last_offset;    /* offset before the last read_event */
-       size_t cur_index;       /* current index in packet index */
+       int64_t offset;         /* offset from base, in bits. EOF for end of file. */
+       int64_t last_offset;    /* offset before the last read_event */
+       uint64_t cur_index;     /* current index in packet index */
        uint64_t last_events_discarded; /* last known amount of event discarded */
        void (*packet_seek)(struct stream_pos *pos, size_t index,
                        int whence); /* function called to switch packet */
@@ -107,9 +108,9 @@ void ctf_fini_pos(struct ctf_stream_pos *pos);
  * TODO: allow larger files by updating base too.
  */
 static inline
-void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset)
+void ctf_move_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
 {
-       printf_debug("ctf_move_pos test EOF: %zd\n", pos->offset);
+       printf_debug("ctf_move_pos test EOF: %" PRId64 "\n", pos->offset);
        if (unlikely(pos->offset == EOF))
                return;
 
@@ -122,16 +123,16 @@ void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset)
                 */
                if ((pos->prot == PROT_WRITE)
                        && (unlikely(pos->offset + bit_offset >= pos->packet_size))) {
-                       printf_debug("ctf_packet_seek (before call): %zd\n",
+                       printf_debug("ctf_packet_seek (before call): %" PRId64 "\n",
                                     pos->offset);
                        ctf_packet_seek(&pos->parent, 0, SEEK_CUR);
-                       printf_debug("ctf_packet_seek (after call): %zd\n",
+                       printf_debug("ctf_packet_seek (after call): %" PRId64 "\n",
                                     pos->offset);
                        return;
                }
        }
        pos->offset += bit_offset;
-       printf_debug("ctf_move_pos after increment: %zd\n", pos->offset);
+       printf_debug("ctf_move_pos after increment: %" PRId64 "\n", pos->offset);
 }
 
 /*
@@ -140,7 +141,7 @@ void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset)
  * TODO: allow larger files by updating base too.
  */
 static inline
-void ctf_align_pos(struct ctf_stream_pos *pos, size_t bit_offset)
+void ctf_align_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
 {
        ctf_move_pos(pos, offset_align(pos->offset, bit_offset));
 }
@@ -181,7 +182,7 @@ void ctf_pos_pad_packet(struct ctf_stream_pos *pos)
 }
 
 static inline
-int ctf_pos_access_ok(struct ctf_stream_pos *pos, size_t bit_len)
+int ctf_pos_access_ok(struct ctf_stream_pos *pos, uint64_t bit_len)
 {
        if (unlikely(pos->offset == EOF))
                return 0;
@@ -199,10 +200,10 @@ void ctf_pos_get_event(struct ctf_stream_pos *pos)
 {
        assert(pos->offset <= pos->content_size);
        if (pos->offset == pos->content_size) {
-               printf_debug("ctf_packet_seek (before call): %zd\n",
+               printf_debug("ctf_packet_seek (before call): %" PRId64 "\n",
                             pos->offset);
                pos->packet_seek(&pos->parent, 0, SEEK_CUR);
-               printf_debug("ctf_packet_seek (after call): %zd\n",
+               printf_debug("ctf_packet_seek (after call): %" PRId64 "\n",
                             pos->offset);
        }
 }
index b57be035057923a5bee693c3e3889a4ef1f8d26b..411b377ecf8cda2e6a1ec2ada151e0ed3d944eae 100644 (file)
@@ -381,8 +381,8 @@ int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
                        stream->prev_cycles_timestamp = 0;
                        stream->prev_cycles_timestamp_end = 0;
 
-                       printf_debug("restored to cur_index = %zd and "
-                               "offset = %zd, timestamp = %" PRIu64 "\n",
+                       printf_debug("restored to cur_index = %" PRId64 " and "
+                               "offset = %" PRId64 ", timestamp = %" PRIu64 "\n",
                                stream_pos->cur_index,
                                stream_pos->offset, stream->real_timestamp);
 
This page took 0.041299 seconds and 4 git commands to generate.