Skip to contentSkip to conversation

perf data: if a bad header size, retry in pipe mode

PATCH In review Ian Rogers 2 participants 5 messages Apr 9, 2020 – Apr 30, 2020 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf data: if a bad header size, retry in pipe mode

Currently pipe mode files fail like:

$ perf record -o - sleep 1 > /tmp/perf.pipe.data
$ perf report -i /tmp/perf.pipe.data
incompatible file format (rerun with -v to learn more)

This change makes it so that if a perf.data file's header size is wrong it is re-checked in pipe mode, where if wrong it fails as it currently does.

---
 tools/perf/util/header.c | 35 ++++++++++++++++++++++++++---------
 tools/perf/util/header.h |  5 -----
 2 files changed, 26 insertions(+), 14 deletions(-)
tools/perf/util/header.c +26 -9
index acbd046bf95c..1df00a4a71b6 100644
@@ -3329,8 +3329,15 @@ static int check_magic_endian(u64 magic, uint64_t hdr_sz,
 	return 0;
 }
 
-int perf_file_header__read(struct perf_file_header *header,
-			   struct perf_header *ph, int fd)
+enum header_read_status {
+	HEADER_READ_OK,
+	HEADER_READ_BAD_SIZE,
+	HEADER_READ_BAD,
+};
+
+static enum header_read_status
+perf_file_header__read(struct perf_file_header *header,
+		       struct perf_header *ph, int fd)
 {
 	ssize_t ret;
 
@@ -3338,12 +3345,12 @@ int perf_file_header__read(struct perf_file_header *header,
 
 	ret = readn(fd, header, sizeof(*header));
 	if (ret <= 0)
-		return -1;
+		return HEADER_READ_BAD;
 
 	if (check_magic_endian(header->magic,
 			       header->attr_size, false, ph) < 0) {
 		pr_debug("magic/endian check failed\n");
-		return -1;
+		return HEADER_READ_BAD;
 	}
 
 	if (ph->needs_swap) {
@@ -3356,7 +3363,7 @@ int perf_file_header__read(struct perf_file_header *header,
 		if (header->size == offsetof(typeof(*header), adds_features))
 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
 		else
-			return -1;
+			return HEADER_READ_BAD_SIZE;
 	} else if (ph->needs_swap) {
 		/*
 		 * feature bitmap is declared as an array of unsigned longs --
@@ -3398,7 +3405,7 @@ int perf_file_header__read(struct perf_file_header *header,
 	ph->data_offset  = header->data.offset;
 	ph->data_size	 = header->data.size;
 	ph->feat_offset  = header->data.offset + header->data.size;
-	return 0;
+	return HEADER_READ_OK;
 }
 
 static int perf_file_section__process(struct perf_file_section *section,
@@ -3580,12 +3587,22 @@ int perf_session__read_header(struct perf_session *session)
 
 	session->evlist->env = &header->env;
 	session->machines.host.env = &header->env;
+
+	if (!perf_data__is_pipe(data)) {
+		enum header_read_status status =
+			perf_file_header__read(&f_header, header, fd);
+		if (status == HEADER_READ_BAD)
+			return -EINVAL;
+		if (status == HEADER_READ_BAD_SIZE) {
+			pr_debug("Data had a bad header size, retrying in pipe mode\n");
+			lseek(fd, 0, SEEK_SET);
+			data->is_pipe = true;
+		}
+	}
+
 	if (perf_data__is_pipe(data))
 		return perf_header__read_pipe(session);
 
-	if (perf_file_header__read(&f_header, header, fd) < 0)
-		return -EINVAL;
-
 	/*
 	 * Sanity check that perf.data was written cleanly; data size is
 	 * initialized to 0 and updated only if the on_exit function is run.
tools/perf/util/header.h +0 -5
index 840f95cee349..3d7f39358265 100644
@@ -73,11 +73,6 @@ struct perf_pipe_file_header {
 	u64				size;
 };
 
-struct perf_header;
-
-int perf_file_header__read(struct perf_file_header *header,
-			   struct perf_header *ph, int fd);
-
 struct perf_header {
 	enum perf_header_version	version;
 	bool				needs_swap;
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
#1 lore raw
Quoted 9 lines · Ian Rogers

Currently pipe mode files fail like:

$ perf record -o - sleep 1 > /tmp/perf.pipe.data
$ perf report -i /tmp/perf.pipe.data
incompatible file format (rerun with -v to learn more)

This change makes it so that if a perf.data file's header size is wrong it is re-checked in pipe mode, where if wrong it fails as it currently does.

hi,
how about doing it the other way round like below, read header and find out if it's pipe..

seems it's less changes

jirka

---

tools/perf/util/header.c +12 -4
index acbd046bf95c..20c34cec9a46 100644
@@ -3469,7 +3469,7 @@ static int perf_header__read_pipe(struct perf_session *session)
 		return -EINVAL;
 	}
 
-	return 0;
+	return f_header.size == sizeof(f_header) ? 0 : -1;
 }
 
 static int read_attr(int fd, struct perf_header *ph,
@@ -3571,7 +3571,7 @@ int perf_session__read_header(struct perf_session *session)
 	struct perf_file_header	f_header;
 	struct perf_file_attr	f_attr;
 	u64			f_id;
-	int nr_attrs, nr_ids, i, j;
+	int nr_attrs, nr_ids, i, j, err;
 	int fd = perf_data__fd(data);
 
 	session->evlist = evlist__new();
@@ -3580,8 +3580,16 @@ int perf_session__read_header(struct perf_session *session)
 
 	session->evlist->env = &header->env;
 	session->machines.host.env = &header->env;
-	if (perf_data__is_pipe(data))
-		return perf_header__read_pipe(session);
+
+	/*
+	 * We could still read 'pipe' data from regular file,
+	 * check for the pipe header first.
+	 */
+	err = perf_header__read_pipe(session);
+	if (!err || (err && perf_data__is_pipe(data))) {
+		data->is_pipe = true;
+		return err;
+	}
 
 	if (perf_file_header__read(&f_header, header, fd) < 0)
 		return -EINVAL;
author reviewer
#2 lore raw
Quoted 12 lines · Jiri Olsa
Quoted 9 lines · Ian Rogers

Currently pipe mode files fail like:

$ perf record -o - sleep 1 > /tmp/perf.pipe.data
$ perf report -i /tmp/perf.pipe.data
incompatible file format (rerun with -v to learn more)

This change makes it so that if a perf.data file's header size is wrong it is re-checked in pipe mode, where if wrong it fails as it currently does.

Quoted 7 lines

hi,
how about doing it the other way round like below, read header and find out if it's pipe..

seems it's less changes

jirka

Looks good to me. I think removing the function declaration from util/header.h is still worthwhile, but needn't be in a patch with this change.

Thanks!
Ian

Quoted 43 lines

---

tools/perf/util/header.c +12 -4
index acbd046bf95c..20c34cec9a46 100644
@@ -3469,7 +3469,7 @@ static int perf_header__read_pipe(struct perf_session *session)
                return -EINVAL;
        }
 
-       return 0;
+       return f_header.size == sizeof(f_header) ? 0 : -1;
 }
 
 static int read_attr(int fd, struct perf_header *ph,
@@ -3571,7 +3571,7 @@ int perf_session__read_header(struct perf_session *session)
        struct perf_file_header f_header;
        struct perf_file_attr   f_attr;
        u64                     f_id;
-       int nr_attrs, nr_ids, i, j;
+       int nr_attrs, nr_ids, i, j, err;
        int fd = perf_data__fd(data);
 
        session->evlist = evlist__new();
@@ -3580,8 +3580,16 @@ int perf_session__read_header(struct perf_session *session)
 
        session->evlist->env = &header->env;
        session->machines.host.env = &header->env;
-       if (perf_data__is_pipe(data))
-               return perf_header__read_pipe(session);
+
+       /*
+        * We could still read 'pipe' data from regular file,
+        * check for the pipe header first.
+        */
+       err = perf_header__read_pipe(session);
+       if (!err || (err && perf_data__is_pipe(data))) {
+               data->is_pipe = true;
+               return err;
+       }
 
        if (perf_file_header__read(&f_header, header, fd) < 0)
                return -EINVAL;
author reviewer
#3 lore raw
Quoted 18 lines · Ian Rogers
Quoted 16 lines · Jiri Olsa
Quoted 9 lines · Ian Rogers

Currently pipe mode files fail like:

$ perf record -o - sleep 1 > /tmp/perf.pipe.data
$ perf report -i /tmp/perf.pipe.data
incompatible file format (rerun with -v to learn more)

This change makes it so that if a perf.data file's header size is wrong it is re-checked in pipe mode, where if wrong it fails as it currently does.

hi,
how about doing it the other way round like below, read header and find out if it's pipe..

Quoted 7 lines
Quoted 3 lines

seems it's less changes

jirka

Looks good to me. I think removing the function declaration from util/header.h is still worthwhile, but needn't be in a patch with this change.

Hi Jiri,

Do you need to send this patch?

Thanks,
Ian

Quoted 46 lines

Thanks!
Ian

Quoted 43 lines

---

tools/perf/util/header.c +12 -4
index acbd046bf95c..20c34cec9a46 100644
@@ -3469,7 +3469,7 @@ static int perf_header__read_pipe(struct perf_session *session)
                return -EINVAL;
        }
 
-       return 0;
+       return f_header.size == sizeof(f_header) ? 0 : -1;
 }
 
 static int read_attr(int fd, struct perf_header *ph,
@@ -3571,7 +3571,7 @@ int perf_session__read_header(struct perf_session *session)
        struct perf_file_header f_header;
        struct perf_file_attr   f_attr;
        u64                     f_id;
-       int nr_attrs, nr_ids, i, j;
+       int nr_attrs, nr_ids, i, j, err;
        int fd = perf_data__fd(data);
 
        session->evlist = evlist__new();
@@ -3580,8 +3580,16 @@ int perf_session__read_header(struct perf_session *session)
 
        session->evlist->env = &header->env;
        session->machines.host.env = &header->env;
-       if (perf_data__is_pipe(data))
-               return perf_header__read_pipe(session);
+
+       /*
+        * We could still read 'pipe' data from regular file,
+        * check for the pipe header first.
+        */
+       err = perf_header__read_pipe(session);
+       if (!err || (err && perf_data__is_pipe(data))) {
+               data->is_pipe = true;
+               return err;
+       }
 
        if (perf_file_header__read(&f_header, header, fd) < 0)
                return -EINVAL;
#4 lore raw
Quoted 23 lines · Ian Rogers
Quoted 22 lines · Ian Rogers
Quoted 19 lines · Jiri Olsa
Quoted 9 lines · Ian Rogers

Currently pipe mode files fail like:

$ perf record -o - sleep 1 > /tmp/perf.pipe.data
$ perf report -i /tmp/perf.pipe.data
incompatible file format (rerun with -v to learn more)

This change makes it so that if a perf.data file's header size is wrong it is re-checked in pipe mode, where if wrong it fails as it currently does.

hi,
how about doing it the other way round like below, read header and find out if it's pipe..

seems it's less changes

jirka

Quoted 7 lines
Quoted 3 lines

Looks good to me. I think removing the function declaration from util/header.h is still worthwhile, but needn't be in a patch with this change.

Hi Jiri,

Do you need to send this patch?

ah right, will post, thanks for reminder

jirka

Quoted 51 lines

Thanks,
Ian

Quoted 46 lines

Thanks!
Ian

Quoted 43 lines

---

tools/perf/util/header.c +12 -4
index acbd046bf95c..20c34cec9a46 100644
@@ -3469,7 +3469,7 @@ static int perf_header__read_pipe(struct perf_session *session)
                return -EINVAL;
        }
 
-       return 0;
+       return f_header.size == sizeof(f_header) ? 0 : -1;
 }
 
 static int read_attr(int fd, struct perf_header *ph,
@@ -3571,7 +3571,7 @@ int perf_session__read_header(struct perf_session *session)
        struct perf_file_header f_header;
        struct perf_file_attr   f_attr;
        u64                     f_id;
-       int nr_attrs, nr_ids, i, j;
+       int nr_attrs, nr_ids, i, j, err;
        int fd = perf_data__fd(data);
 
        session->evlist = evlist__new();
@@ -3580,8 +3580,16 @@ int perf_session__read_header(struct perf_session *session)
 
        session->evlist->env = &header->env;
        session->machines.host.env = &header->env;
-       if (perf_data__is_pipe(data))
-               return perf_header__read_pipe(session);
+
+       /*
+        * We could still read 'pipe' data from regular file,
+        * check for the pipe header first.
+        */
+       err = perf_header__read_pipe(session);
+       if (!err || (err && perf_data__is_pipe(data))) {
+               data->is_pipe = true;
+               return err;
+       }
 
        if (perf_file_header__read(&f_header, header, fd) < 0)
                return -EINVAL;