From: Mathieu Desnoyers Date: Sun, 23 Aug 2015 03:50:56 +0000 (-0700) Subject: Fix: handle packet_seek errors X-Git-Tag: v1.4.4~2 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=ccce44e83614322e21e6a90f271c7b89824fb968 Fix: handle packet_seek errors This is needed to correctly handle SIGINT in live view mode. Without handling this situation correctly, we can often see the following issue upon SIGINT: [error] Stream 18446744073709551615 is not declared in metadata. Signed-off-by: Mathieu Desnoyers Signed-off-by: Julien Desfossez Signed-off-by: Jérémie Galarneau --- diff --git a/converter/babeltrace-log.c b/converter/babeltrace-log.c index db7f60f1..c478b004 100644 --- a/converter/babeltrace-log.c +++ b/converter/babeltrace-log.c @@ -282,7 +282,8 @@ void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) if (!ctf_move_pos(&dummy, tlen * CHAR_BIT)) packet_filled = 1; if (packet_filled || ctf_pos_packet(&dummy)) { - ctf_pos_pad_packet(pos); + if (ctf_pos_pad_packet(pos)) + goto error; write_packet_header(pos, s_uuid); write_packet_context(pos); if (attempt++ == 1) { diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 0cdce8ef..5d22d9e4 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -482,7 +483,8 @@ int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *str if (unlikely(pos->offset == EOF)) return EOF; - ctf_pos_get_event(pos); + if (ctf_pos_get_event(pos)) + return EOF; /* save the current position as a restore point */ pos->last_offset = pos->offset; @@ -1051,7 +1053,8 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) case SEEK_SET: /* Fall-through */ break; /* OK */ default: - assert(0); + ret = -1; + goto end; } if ((pos->prot & PROT_WRITE) && pos->content_size_loc) @@ -1063,7 +1066,8 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) if (ret) { fprintf(stderr, "[error] Unable to unmap old base: %s.\n", strerror(errno)); - assert(0); + ret = -1; + goto end; } pos->base_mma = NULL; } @@ -1083,7 +1087,8 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) pos->cur_index = 0; break; default: - assert(0); + ret = -1; + goto end; } pos->content_size = -1U; /* Unknown at this point */ pos->packet_size = WRITE_PACKET_LEN; @@ -1099,7 +1104,8 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) case SEEK_CUR: { if (pos->offset == EOF) { - return; + ret = 0; + goto end; } assert(pos->cur_index < pos->packet_index->len); /* The reader will expect us to skip padding */ @@ -1109,17 +1115,20 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) case SEEK_SET: if (index >= pos->packet_index->len) { pos->offset = EOF; - return; + ret = 0; + goto end; } pos->cur_index = index; break; default: - assert(0); + ret = -1; + goto end; } if (pos->cur_index >= pos->packet_index->len) { pos->offset = EOF; - return; + ret = 0; + goto end; } packet_index = &g_array_index(pos->packet_index, @@ -1134,6 +1143,12 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) ctf_update_current_packet_index(&file_stream->parent, prev_index, packet_index); + if (pos->cur_index >= pos->packet_index->len) { + pos->offset = EOF; + ret = 0; + goto end; + } + /* * We need to check if we are in trace read or called * from packet indexing. In this last case, the @@ -1155,7 +1170,8 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) if (packet_index->data_offset == -1) { ret = find_data_offset(pos, file_stream, packet_index); if (ret < 0) { - return; + ret = -1; + goto end; } } pos->content_size = packet_index->content_size; @@ -1171,7 +1187,8 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) goto read_next_packet; } else { pos->offset = EOF; - return; + ret = 0; + goto end; } } /* map new base. Need mapping length from header. */ @@ -1196,6 +1213,9 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p); assert(!ret); } + ret = 0; +end: + bt_packet_seek_set_error(ret); } static @@ -2471,6 +2491,10 @@ int prepare_mmap_stream_definition(struct ctf_trace *td, /* Ask for the first packet to get the stream_id. */ packet_seek(&file_stream->pos.parent, 0, SEEK_SET); + ret = bt_packet_seek_get_error(); + if (ret) { + goto end; + } stream_id = file_stream->parent.stream_id; if (stream_id >= td->streams->len) { fprintf(stderr, "[error] Stream %" PRIu64 " is not declared " diff --git a/formats/lttng-live/lttng-live-comm.c b/formats/lttng-live/lttng-live-comm.c index e0892638..cb871a13 100644 --- a/formats/lttng-live/lttng-live-comm.c +++ b/formats/lttng-live/lttng-live-comm.c @@ -1223,7 +1223,8 @@ void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index, ret = handle_seek_position(index, whence, viewer_stream, pos, file_stream); if (ret != 0) { - return; + ret = -1; + goto end; } retry: @@ -1265,7 +1266,8 @@ retry: if (!lttng_live_should_quit()) { fprintf(stderr, "[error] get_next_index failed\n"); } - return; + ret = -1; + goto end; } printf_verbose("Index received : packet_size : %" PRIu64 ", offset %" PRIu64 ", content_size %" PRIu64 @@ -1294,7 +1296,8 @@ retry: file_stream->parent.stream_id = stream_id; viewer_stream->ctf_stream_id = stream_id; - return; + ret = 0; + goto end; } pos->packet_size = cur_index->packet_size; @@ -1386,15 +1389,18 @@ retry: pos->offset = EOF; if (!lttng_live_should_quit()) { fprintf(stderr, "[error] get_data_packet failed\n"); + ret = -1; + } else { + ret = 0; } - return; + goto end; } viewer_stream->data_pending = 0; read_packet_header(pos, file_stream); - + ret = 0; end: - return; + bt_packet_seek_set_error(ret); } int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx) diff --git a/include/Makefile.am b/include/Makefile.am index f6e8e492..426e2c64 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -5,7 +5,8 @@ babeltraceinclude_HEADERS = \ babeltrace/iterator.h \ babeltrace/trace-handle.h \ babeltrace/list.h \ - babeltrace/clock-types.h + babeltrace/clock-types.h \ + babeltrace/error.h babeltracectfinclude_HEADERS = \ babeltrace/ctf/events.h \ diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 574f97b3..59ce9981 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -39,6 +39,7 @@ #include #include #include +#include #define LAST_OFFSET_POISON ((int64_t) ~0ULL) @@ -223,9 +224,10 @@ int ctf_pos_packet(struct ctf_stream_pos *dummy) } static inline -void ctf_pos_pad_packet(struct ctf_stream_pos *pos) +int ctf_pos_pad_packet(struct ctf_stream_pos *pos) { ctf_packet_seek(&pos->parent, 0, SEEK_CUR); + return bt_packet_seek_get_error(); } /* @@ -233,8 +235,10 @@ void ctf_pos_pad_packet(struct ctf_stream_pos *pos) * 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) +int ctf_pos_get_event(struct ctf_stream_pos *pos) { + int ret = 0; + assert(pos->offset <= pos->content_size); if (pos->offset == pos->content_size) { printf_debug("ctf_packet_seek (before call): %" PRId64 "\n", @@ -242,7 +246,9 @@ void ctf_pos_get_event(struct ctf_stream_pos *pos) pos->packet_seek(&pos->parent, 0, SEEK_CUR); printf_debug("ctf_packet_seek (after call): %" PRId64 "\n", pos->offset); + ret = bt_packet_seek_get_error(); } + return ret; } void ctf_print_timestamp(FILE *fp, struct ctf_stream_definition *stream, diff --git a/include/babeltrace/error.h b/include/babeltrace/error.h new file mode 100644 index 00000000..2f6a6d7c --- /dev/null +++ b/include/babeltrace/error.h @@ -0,0 +1,54 @@ +#ifndef BABELTRACE_ERROR_H +#define BABELTRACE_ERROR_H + +/* + * BabelTrace + * + * Global error accessors. + * + * Copyright 2018 EfficiOS Inc. and Linux Foundation + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * bt_packet_seek_get_error: get the return code of the last packet_seek use. + * + * The packet seek functions defined by the various formats don't return a + * value. An implementation that can fail to seek can set a per-thread + * packet_seek error to be checked by the caller. + * + * The error is cleared after access in order to preserve the compatibility + * with implementation that don't set an error. + */ +int bt_packet_seek_get_error(void); + +/* + * bt_packet_seek_set_error: set the return code of the last packet_seek use. + * + * This function sets the per-thread packet_seek error. A value of 0 indicates + * no error. Implementations of packet_seek are encouraged to use a negative + * value to indicate an error. + */ +void bt_packet_seek_set_error(int error); + +#endif /* BABELTRACE_ERROR_H */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 007abb78..c0749b7d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,7 +9,8 @@ libbabeltrace_la_SOURCES = babeltrace.c \ context.c \ trace-handle.c \ trace-collection.c \ - registry.c + registry.c \ + error.c libbabeltrace_la_LDFLAGS = -version-info $(BABELTRACE_LIBRARY_VERSION) diff --git a/lib/error.c b/lib/error.c new file mode 100644 index 00000000..36cd5c6c --- /dev/null +++ b/lib/error.c @@ -0,0 +1,45 @@ +/* + * BabelTrace + * + * Global error accessors. + * + * Copyright 2018 EfficiOS Inc. and Linux Foundation + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +static __thread int packet_seek_error; + +int bt_packet_seek_get_error(void) +{ + int ret = packet_seek_error; + + packet_seek_error = 0; + return ret; +} + +void bt_packet_seek_set_error(int error) +{ + packet_seek_error = error; +} diff --git a/lib/iterator.c b/lib/iterator.c index d439d7b9..30a45423 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -144,6 +144,10 @@ static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs, continue; stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET); + ret = bt_packet_seek_get_error(); + if (ret < 0) { + return EOF; + } do { ret = stream_read_event(cfs); } while (cfs->parent.real_timestamp < timestamp && ret == 0); @@ -243,6 +247,10 @@ static int find_max_timestamp_ctf_file_stream(struct ctf_file_stream *cfs, */ for (i = stream_pos->packet_index->len - 1; i >= 0; i--) { stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET); + ret = bt_packet_seek_get_error(); + if (ret < 0) { + return EOF; + } count = 0; /* read each event until we reach the end of the stream */ do {