Fix: handle packet_seek errors
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 23 Aug 2015 03:50:56 +0000 (20:50 -0700)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 31 Jan 2018 19:46:32 +0000 (14:46 -0500)
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 <mathieu.desnoyers@efficios.com>
Signed-off-by: Julien Desfossez <jdesfossez@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
converter/babeltrace-log.c
formats/ctf/ctf.c
formats/lttng-live/lttng-live-comm.c
include/Makefile.am
include/babeltrace/ctf/types.h
include/babeltrace/error.h [new file with mode: 0644]
lib/Makefile.am
lib/error.c [new file with mode: 0644]
lib/iterator.c

index db7f60f1d2b60b2eeccf114ab24e2965a5641caa..c478b004d71cf1ecf844c339db46db6fa912c3eb 100644 (file)
@@ -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) {
index 0cdce8ef48ea365d323eeb23007448a2eeaad352..5d22d9e44de2574391fb157d3739db7a2c3b8b03 100644 (file)
@@ -36,6 +36,7 @@
 #include <babeltrace/context-internal.h>
 #include <babeltrace/compat/uuid.h>
 #include <babeltrace/endian.h>
+#include <babeltrace/error.h>
 #include <babeltrace/trace-debug-info.h>
 #include <babeltrace/ctf/ctf-index.h>
 #include <inttypes.h>
@@ -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 "
index e08926380efab45e306a01ec59607012f51fbc90..cb871a1318113e4909ba3db8ad3ceae132ed7e05 100644 (file)
@@ -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)
index f6e8e4929f238f341ee48db7062fa586136b0d56..426e2c6456fe62787a60276571f98662b4b2d93e 100644 (file)
@@ -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 \
index 574f97b3657aa9b83ba4610f8afdde7efcda50a2..59ce99811f73993457ffdd9aacff7d2d73e1b691 100644 (file)
@@ -39,6 +39,7 @@
 #include <stdio.h>
 #include <inttypes.h>
 #include <babeltrace/mmap-align.h>
+#include <babeltrace/error.h>
 
 #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 (file)
index 0000000..2f6a6d7
--- /dev/null
@@ -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 <jeremie.galarneau@efficios.com>
+ *
+ * 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 */
index 007abb780934079321bee53ae664a9c78714ee80..c0749b7dfccde657acc3e91f59abc0b900451ca9 100644 (file)
@@ -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 (file)
index 0000000..36cd5c6
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * BabelTrace
+ *
+ * Global error accessors.
+ *
+ * Copyright 2018 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/error.h>
+
+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;
+}
index d439d7b926c52f6cdee9a7716d2bc75d3d3d194f..30a45423dfc1c2130b342aa7d274bd8dcd9b39d0 100644 (file)
@@ -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 {
This page took 0.03198 seconds and 4 git commands to generate.