Skip to contentSkip to conversation

Fix: handle packet_seek errors

PATCH New Mathieu Desnoyers 1 participant 2 messages Aug 18, 2015 – Aug 19, 2015 1 message not in the archive Library code

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 2/2 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.

---
 converter/babeltrace-log.c             |  3 ++-
 converter/babeltrace.c                 |  2 +-
 formats/bt-dummy/bt-dummy.c            |  2 +-
 formats/ctf-metadata/ctf-metadata.c    |  4 +--
 formats/ctf-text/ctf-text.c            |  4 +--
 formats/ctf/ctf.c                      | 47 +++++++++++++++++++---------------
 formats/ctf/ir/stream.c                |  5 +++-
 formats/lttng-live/lttng-live-comm.c   | 18 +++++++------
 formats/lttng-live/lttng-live-plugin.c |  2 +-
 include/babeltrace/context.h           |  2 +-
 include/babeltrace/ctf/types.h         | 15 ++++++-----
 include/babeltrace/format.h            |  4 +--
 lib/context.c                          |  2 +-
 13 files changed, 62 insertions(+), 48 deletions(-)
converter/babeltrace-log.c +2 -1
index 5a8b26a..5677824 100644
@@ -283,7 +283,8 @@ void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
 		if (!ctf_move_pos(&dummy, tlen * CHAR_BIT))
 			goto error;
 		if (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) {
converter/babeltrace.c +1 -1
index 0ceca0c..cb54eaf 100644
@@ -496,7 +496,7 @@ static int traverse_trace_dir(const char *fpath, const struct stat *sb,
  */
 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
 		const char *format_str,
-		void (*packet_seek)(struct bt_stream_pos *pos,
+		int (*packet_seek)(struct bt_stream_pos *pos,
 			size_t offset, int whence))
 {
 	int ret = 0, trace_ids = 0;
formats/bt-dummy/bt-dummy.c +1 -1
index 6192e88..17e2e33 100644
@@ -46,7 +46,7 @@ int bt_dummy_write_event(struct bt_stream_pos *ppos, struct ctf_stream_definitio
 
 static
 struct bt_trace_descriptor *bt_dummy_open_trace(const char *path, int flags,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence), FILE *metadata_fp)
 {
 	struct ctf_text_stream_pos *pos;
formats/ctf-metadata/ctf-metadata.c +2 -2
index a5a74c3..cb10b9a 100644
@@ -43,7 +43,7 @@
 
 static
 struct bt_trace_descriptor *ctf_metadata_open_trace(const char *path, int flags,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence), FILE *metadata_fp);
 static
 int ctf_metadata_close_trace(struct bt_trace_descriptor *descriptor);
@@ -75,7 +75,7 @@ int ctf_metadata_trace_pre_handler(struct bt_stream_pos *ppos,
 
 static
 struct bt_trace_descriptor *ctf_metadata_open_trace(const char *path, int flags,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence), FILE *metadata_fp)
 {
 	struct ctf_text_stream_pos *pos;
formats/ctf-text/ctf-text.c +2 -2
index 2ba0823..1e2344b 100644
@@ -88,7 +88,7 @@ enum bt_loglevel {
 
 static
 struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence), FILE *metadata_fp);
 static
 int ctf_text_close_trace(struct bt_trace_descriptor *descriptor);
@@ -537,7 +537,7 @@ error:
 
 static
 struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence), FILE *metadata_fp)
 {
 	struct ctf_text_stream_pos *pos;
formats/ctf/ctf.c +26 -21

176 lines not on this page · raw message

formats/ctf/ir/stream.c +4 -1
index 6a6a3a7..22a3cd6 100644
@@ -733,7 +733,10 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 	}
 
 	/* mmap the next packet */
-	ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
+	ret = ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
+	if (ret) {
+		goto end;
+	}
 
 	ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
 	if (ret) {
formats/lttng-live/lttng-live-comm.c +10 -8
index 8fa90cf..378d19e 100644
@@ -68,7 +68,7 @@
 	((type) (a) > (type) (b) ? (type) (a) : (type) (b))
 #endif
 
-static void ctf_live_packet_seek(struct bt_stream_pos *stream_pos,
+static int ctf_live_packet_seek(struct bt_stream_pos *stream_pos,
 		size_t index, int whence);
 static int add_traces(struct lttng_live_ctx *ctx);
 static int del_traces(gpointer key, gpointer value, gpointer user_data);
@@ -1154,7 +1154,7 @@ end:
 }
 
 static
-void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
+int ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
 		int whence)
 {
 	struct ctf_stream_pos *pos;
@@ -1173,7 +1173,7 @@ 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;
+		return -1;
 	}
 
 retry:
@@ -1215,7 +1215,7 @@ retry:
 			if (!lttng_live_should_quit()) {
 				fprintf(stderr, "[error] get_next_index failed\n");
 			}
-			return;
+			return -1;
 		}
 		printf_verbose("Index received : packet_size : %" PRIu64
 				", offset %" PRIu64 ", content_size %" PRIu64
@@ -1232,7 +1232,7 @@ retry:
 	 * stream id. We need to skip this packet for now.
 	 */
 	if (stream_id == -1ULL) {
-		return;
+		return 0;
 	}
 	/*
 	 * On the first time we receive an index, the stream_id needs to
@@ -1252,7 +1252,7 @@ retry:
 		file_stream->parent.stream_id = stream_id;
 		viewer_stream->ctf_stream_id = stream_id;
 
-		return;
+		return 0;
 	}
 
 	pos->packet_size = cur_index->packet_size;
@@ -1307,15 +1307,17 @@ retry:
 		pos->offset = EOF;
 		if (!lttng_live_should_quit()) {
 			fprintf(stderr, "[error] get_data_packet failed\n");
+			return -1;
+		} else {
+			return 0;
 		}
-		return;
 	}
 	viewer_stream->data_pending = 0;
 
 	read_packet_header(pos, file_stream);
 
 end:
-	return;
+	return 0;
 }
 
 int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
formats/lttng-live/lttng-live-plugin.c +1 -1
index 643129c..3fbcf0a 100644
@@ -271,7 +271,7 @@ end_free:
 
 static
 struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence), FILE *metadata_fp)
 {
 	struct ctf_text_stream_pos *pos;
include/babeltrace/context.h +1 -1
index b28df09..3ac15d5 100644
@@ -82,7 +82,7 @@ struct bt_context *bt_context_create(void);
  */
 int bt_context_add_trace(struct bt_context *ctx, const char *path,
 		const char *format,
-		void (*packet_seek)(struct bt_stream_pos *pos,
+		int (*packet_seek)(struct bt_stream_pos *pos,
 			size_t index, int whence),
 		struct bt_mmap_stream_list *stream_list,
 		FILE *metadata);
include/babeltrace/ctf/types.h +9 -6
index c336632..abcde4c 100644
@@ -83,7 +83,7 @@ struct ctf_stream_pos {
 	int64_t data_offset;	/* offset of data in current packet */
 	uint64_t cur_index;	/* current index in packet index */
 	uint64_t last_events_discarded;	/* last known amount of event discarded */
-	void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+	int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence); /* function called to switch packet */
 
 	int dummy;		/* dummy position, for length calculation */
@@ -126,7 +126,7 @@ int ctf_sequence_read(struct bt_stream_pos *pos, struct bt_definition *definitio
 BT_HIDDEN
 int ctf_sequence_write(struct bt_stream_pos *pos, struct bt_definition *definition);
 
-void ctf_packet_seek(struct bt_stream_pos *pos, size_t index, int whence);
+int ctf_packet_seek(struct bt_stream_pos *pos, size_t index, int whence);
 
 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
 		int fd, int open_flags);
@@ -220,9 +220,9 @@ 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 ctf_packet_seek(&pos->parent, 0, SEEK_CUR);
 }
 
 /*
@@ -230,16 +230,19 @@ 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",
 			     pos->offset);
-		pos->packet_seek(&pos->parent, 0, SEEK_CUR);
+		ret = pos->packet_seek(&pos->parent, 0, SEEK_CUR);
 		printf_debug("ctf_packet_seek (after call): %" PRId64 "\n",
 			     pos->offset);
 	}
+	return ret;
 }
 
 void ctf_print_timestamp(FILE *fp, struct ctf_stream_definition *stream,
include/babeltrace/format.h +2 -2
index dea8e0e..dd52ce4 100644
@@ -60,12 +60,12 @@ struct bt_format {
 	bt_intern_str name;
 
 	struct bt_trace_descriptor *(*open_trace)(const char *path, int flags,
-			void (*packet_seek)(struct bt_stream_pos *pos,
+			int (*packet_seek)(struct bt_stream_pos *pos,
 				size_t index, int whence),
 			FILE *metadata_fp);
 	struct bt_trace_descriptor *(*open_mmap_trace)(
 			struct bt_mmap_stream_list *mmap_list,
-			void (*packet_seek)(struct bt_stream_pos *pos,
+			int (*packet_seek)(struct bt_stream_pos *pos,
 				size_t index, int whence),
 			FILE *metadata_fp);
 	int (*close_trace)(struct bt_trace_descriptor *descriptor);
lib/context.c +1 -1
index 45aab34..e54bc32 100644
@@ -71,7 +71,7 @@ struct bt_context *bt_context_create(void)
 
 int bt_context_add_trace(struct bt_context *ctx, const char *path,
 		const char *format_name,
-		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+		int (*packet_seek)(struct bt_stream_pos *pos, size_t index,
 			int whence),
 		struct bt_mmap_stream_list *stream_list,
 		FILE *metadata)
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 1/2] Fix: return error on ask_new_stream when should quit

In reply to a message not in the archive

this patch might not be needed... the second patch of this series might be sufficient.

Quoted 38 lines · ----- On Aug 18, 2015, at 4:36 PM, Mathieu Desnoyers…

Otherwise, the caller will try to continue adding partially set streams to the trace.

---
formats/lttng-live/lttng-live-comm.c | 8 +++++---

1 file changed, 5 insertions(+), 3 deletions(-)
formats/lttng-live/lttng-live-comm.c +5 -4
index 47ea589..8fa90cf 100644
@@ -560,10 +560,11 @@ restart:
 		id = g_array_index(ctx->session_ids, uint64_t, i);
 		ret = lttng_live_get_new_streams(ctx, id);
 		printf_verbose("Asking for new streams returns %d\n", ret);
+		if (lttng_live_should_quit()) {
+			ret = -1;
+			goto end;
+		}
 		if (ret < 0) {
-			if (lttng_live_should_quit()) {
-				goto end;
-			}
 			if (ret == -LTTNG_VIEWER_NEW_STREAMS_HUP) {
 				printf_verbose("Session %" PRIu64 " closed\n",
 						id);
@@ -1699,6 +1700,7 @@ int lttng_live_read(struct lttng_live_ctx *ctx)
 			}
 			ret = ask_new_streams(ctx);
 			if (ret < 0) {
+				ret = 0;
 				goto end_free;
 			}
 			if (!ctx->session->stream_count) {
--

2.1.4

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com