From: Mathieu Desnoyers Date: Thu, 11 Aug 2011 20:38:04 +0000 (-0400) Subject: Fix non-matching packet context wrt last packet event read X-Git-Tag: v0.5~2 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=5f643ed7d0d2d21d3797938e34735ef8f1bd9186 Fix non-matching packet context wrt last packet event read The last event in a packet is seen with the timestamp of the beginning of the next packet due to an incorrect handling of ctf_move_pos() : the "ctf_move_pos_slow" that switches between packets needs to be called at the next event, rather than at the end of the last event of the packet. When the position was getting to the end of the payload of the last event, it was immediately changing packet, which changed all packet context information, including the current timestamp. This is fixed by explicitely calling the ctf_move_pos_slow() on packet boundaries at the beginning of event read rather than at each ctf_move_pos call. We also fix handling of empty trace packets here: when an empty packet is detected, we need to move on to the next packet immediately within ctf_move_pos_slow() rather than let the caller think there is some event data in the empty packet. Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 23a2179b..f078735c 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -125,8 +125,11 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) uint64_t id = 0; int ret; + ctf_pos_get_event(pos); + if (pos->offset == EOF) return EOF; + assert(pos->offset < pos->content_size); /* Read event header */ if (stream->stream_event_header) { @@ -390,8 +393,11 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) assert(off >= 0); pos->offset = 0; } else { + read_next_packet: switch (whence) { case SEEK_CUR: + if (pos->offset == EOF) + return; /* The reader will expect us to skip padding */ assert(pos->offset + offset == pos->content_size); ++pos->cur_index; @@ -415,8 +421,13 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) file_stream->parent.timestamp = index->timestamp_begin; pos->content_size = index->content_size; pos->packet_size = index->packet_size; - if (index->data_offset <= index->content_size) { + if (index->data_offset < index->content_size) { pos->offset = 0; /* will read headers */ + } else if (index->data_offset == index->content_size) { + /* empty packet */ + pos->offset = index->data_offset; + offset = 0; + goto read_next_packet; } else { pos->offset = EOF; return; diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index c5912714..8d57ec74 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -101,10 +101,14 @@ void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset) return; if (pos->fd >= 0) { - if (((pos->prot == PROT_READ) - && (pos->offset + bit_offset >= pos->content_size)) - || ((pos->prot == PROT_WRITE) - && (pos->offset + bit_offset >= pos->packet_size))) { + /* + * PROT_READ ctf_move_pos_slow is called from within + * ctf_pos_get_event so end of packet does not change + * the packet context on for the last event of the + * packet. + */ + if ((pos->prot == PROT_WRITE) + && (pos->offset + bit_offset >= pos->packet_size)) { printf_debug("ctf_move_pos_slow (before call): %zd\n", pos->offset); ctf_move_pos_slow(pos, bit_offset, SEEK_CUR); @@ -172,4 +176,21 @@ int ctf_pos_access_ok(struct ctf_stream_pos *pos, size_t bit_len) return 1; } +/* + * Update the stream position for to the current event. This moves to + * the next packet if we are located at the end of the current packet. + */ +static inline +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_move_pos_slow (before call): %zd\n", + pos->offset); + ctf_move_pos_slow(pos, 0, SEEK_CUR); + printf_debug("ctf_move_pos_slow (after call): %zd\n", + pos->offset); + } +} + #endif /* _BABELTRACE_CTF_TYPES_H */