From fef3bf22431390dae389d378b7709366b158b83d Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 27 Sep 2012 20:56:53 -0400 Subject: [PATCH] Fix: Allow 64-bit packet offset 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 --- converter/babeltrace-log.c | 30 ++++++++++++++------------- include/babeltrace/ctf/types.h | 37 +++++++++++++++++----------------- lib/iterator.c | 4 ++-- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/converter/babeltrace-log.c b/converter/babeltrace-log.c index f7eb5f76..d24f011e 100644 --- a/converter/babeltrace-log.c +++ b/converter/babeltrace-log.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -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; } diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 6a76c6bb..c1916a4d 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -29,21 +29,22 @@ #include #include #include +#include #include -#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); } } diff --git a/lib/iterator.c b/lib/iterator.c index b57be035..411b377e 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -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); -- 2.34.1