linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel
@ 2023-06-13  3:48 Ian Rogers
  2023-06-13  3:48 ` [PATCH v2 1/2] perf srcline: Make addr2line configuration failure more verbose Ian Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ian Rogers @ 2023-06-13  3:48 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Ian Rogers, Adrian Hunter, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, linux-perf-users, linux-kernel, llvm, Changbin Du

addr2line makes it hard to detect when inline output has finished. To
detect it srcline issues a bogus ',' symbol and expects to see a
filename:line of "??:0". For binutils addr2line ',' gets turned into
address 0 and if there is a symbol there then the sentinel isn't
detected, this problem has been occurring with kernel binaries.

The issue was reported by and debugged by Changbin Du <changbin.du@huawei.com>.

v2. Incorporate feedback from Changbin.

Ian Rogers (2):
  perf srcline: Make addr2line configuration failure more verbose
  perf srcline: Make sentinel reading for binutils addr2line more robust

 tools/perf/util/srcline.c | 93 ++++++++++++++++++++++++++++++---------
 1 file changed, 73 insertions(+), 20 deletions(-)

-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] perf srcline: Make addr2line configuration failure more verbose
  2023-06-13  3:48 [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Ian Rogers
@ 2023-06-13  3:48 ` Ian Rogers
  2023-06-13  3:48 ` [PATCH v2 2/2] perf srcline: Make sentinel reading for binutils addr2line more robust Ian Rogers
  2023-06-13  4:02 ` [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Changbin Du
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2023-06-13  3:48 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Ian Rogers, Adrian Hunter, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, linux-perf-users, linux-kernel, llvm, Changbin Du

To aid debugging why it fails. Also, combine the loops for reading a
line for the llvm/binutils cases.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/srcline.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index aec596a0b0bb..d477332586b2 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -443,7 +443,7 @@ enum a2l_style {
 	LLVM,
 };
 
-static enum a2l_style addr2line_configure(struct child_process *a2l)
+static enum a2l_style addr2line_configure(struct child_process *a2l, const char *dso_name)
 {
 	static bool cached;
 	static enum a2l_style style;
@@ -452,6 +452,7 @@ static enum a2l_style addr2line_configure(struct child_process *a2l)
 		char buf[128];
 		struct io io;
 		int ch;
+		int lines;
 
 		if (write(a2l->in, ",\n", 2) != 2)
 			return BROKEN;
@@ -461,19 +462,29 @@ static enum a2l_style addr2line_configure(struct child_process *a2l)
 		if (ch == ',') {
 			style = LLVM;
 			cached = true;
+			lines = 1;
 		} else if (ch == '?') {
 			style = GNU_BINUTILS;
 			cached = true;
+			lines = 2;
 		} else {
-			style = BROKEN;
+			if (!symbol_conf.disable_add2line_warn) {
+				char *output = NULL;
+				size_t output_len;
+
+				io__getline(&io, &output, &output_len);
+				pr_warning("%s %s: addr2line configuration failed\n",
+					   __func__, dso_name);
+				pr_warning("\t%c%s", ch, output);
+			}
+			return BROKEN;
 		}
-		do {
+		while (lines) {
 			ch = io__get_char(&io);
-		} while (ch > 0 && ch != '\n');
-		if (style == GNU_BINUTILS) {
-			do {
-				ch = io__get_char(&io);
-			} while (ch > 0 && ch != '\n');
+			if (ch <= 0)
+				break;
+			if (ch == '\n')
+				lines--;
 		}
 		/* Ignore SIGPIPE in the event addr2line exits. */
 		signal(SIGPIPE, SIG_IGN);
@@ -593,12 +604,9 @@ static int addr2line(const char *dso_name, u64 addr,
 			pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name);
 		goto out;
 	}
-	a2l_style = addr2line_configure(a2l);
-	if (a2l_style == BROKEN) {
-		if (!symbol_conf.disable_add2line_warn)
-			pr_warning("%s: addr2line configuration failed\n", __func__);
+	a2l_style = addr2line_configure(a2l, dso_name);
+	if (a2l_style == BROKEN)
 		goto out;
-	}
 
 	/*
 	 * Send our request and then *deliberately* send something that can't be interpreted as
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] perf srcline: Make sentinel reading for binutils addr2line more robust
  2023-06-13  3:48 [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Ian Rogers
  2023-06-13  3:48 ` [PATCH v2 1/2] perf srcline: Make addr2line configuration failure more verbose Ian Rogers
@ 2023-06-13  3:48 ` Ian Rogers
  2023-06-13  4:02 ` [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Changbin Du
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2023-06-13  3:48 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Ian Rogers, Adrian Hunter, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, linux-perf-users, linux-kernel, llvm, Changbin Du

The addr2line process is sent an address then multiple function,
filename:line "records" are read. To detect the end of output a ',' is
sent and for llvm-addr2line a ',' is then read back showing the end of
addrline's output. For binutils addr2line the ',' translates to
address 0 and we expect the bogus filename marker "??:0" (see
filename_split) to be sent from addr2line. For some kernels address 0
may have a mapping and so a seemingly valid inline output is given and
breaking the sentinel discovery:

```
$ addr2line -e vmlinux -f -i
,
__per_cpu_start
./arch/x86/kernel/cpu/common.c:1850
```

To avoid this problem enable the address dumping for addr2line (the -a
option). If an address of 0x0000000000000000 is read then this is the
sentinel value working around the problem above. The filename_split
still needs to check for "??:0" as bogus non-zero addresses also need
handling.

Reported-by: Changbin Du <changbin.du@huawei.com>
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/srcline.c | 61 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 8 deletions(-)

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index d477332586b2..b27b4b3c391b 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -408,7 +408,7 @@ static struct child_process *addr2line_subprocess_init(const char *addr2line_pat
 	const char *argv[] = {
 		addr2line_path ?: "addr2line",
 		"-e", binary_path,
-		"-i", "-f", NULL
+		"-a", "-i", "-f", NULL
 	};
 	struct child_process *a2l = zalloc(sizeof(*a2l));
 	int start_command_status = 0;
@@ -463,10 +463,10 @@ static enum a2l_style addr2line_configure(struct child_process *a2l, const char
 			style = LLVM;
 			cached = true;
 			lines = 1;
-		} else if (ch == '?') {
+		} else if (ch == '0') {
 			style = GNU_BINUTILS;
 			cached = true;
-			lines = 2;
+			lines = 3;
 		} else {
 			if (!symbol_conf.disable_add2line_warn) {
 				char *output = NULL;
@@ -518,20 +518,64 @@ static int read_addr2line_record(struct io *io,
 	if (line_nr != NULL)
 		*line_nr = 0;
 
+	/*
+	 * Read the first line. Without an error this will be either an address
+	 * like 0x1234 or for llvm-addr2line the sentinal ',' character.
+	 */
 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
 		goto error;
 
-	if (style == LLVM && line_len == 2 && line[0] == ',') {
-		zfree(&line);
-		return 0;
+	if (style == LLVM) {
+		if (line_len == 2 && line[0] == ',') {
+			zfree(&line);
+			return 0;
+		}
+	} else {
+		int zero_count = 0, non_zero_count = 0;
+
+		/* The address should always start 0x. */
+		if (line_len < 2 || line[0] != '0' || line[1] != 'x')
+			goto error;
+
+		for (size_t i = 2; i < line_len; i++) {
+			if (line[i] == '0')
+				zero_count++;
+			else if (line[i] != '\n')
+				non_zero_count++;
+		}
+		if (!non_zero_count) {
+			int ch;
+
+			if (!zero_count) {
+				/* Line was erroneous just '0x'. */
+				goto error;
+			}
+			/*
+			 * Line was 0x0..0, the sentinel for binutils. Remove
+			 * the function and filename lines.
+			 */
+			zfree(&line);
+			do {
+				ch = io__get_char(io);
+			} while (ch > 0 && ch != '\n');
+			do {
+				ch = io__get_char(io);
+			} while (ch > 0 && ch != '\n');
+			return 0;
+		}
 	}
 
+	/* Read the second function name line. */
+	if (io__getline(io, &line, &line_len) < 0 || !line_len)
+		goto error;
+
 	if (function != NULL)
 		*function = strdup(strim(line));
 
 	zfree(&line);
 	line_len = 0;
 
+	/* Read the third filename and line number line. */
 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
 		goto error;
 
@@ -635,8 +679,9 @@ static int addr2line(const char *dso_name, u64 addr,
 		goto out;
 	case 0:
 		/*
-		 * The first record was invalid, so return failure, but first read another
-		 * record, since we asked a junk question and have to clear the answer out.
+		 * The first record was invalid, so return failure, but first
+		 * read another record, since we sent a sentinel ',' for the
+		 * sake of detected the last inlined function.
 		 */
 		switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) {
 		case -1:
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel
  2023-06-13  3:48 [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Ian Rogers
  2023-06-13  3:48 ` [PATCH v2 1/2] perf srcline: Make addr2line configuration failure more verbose Ian Rogers
  2023-06-13  3:48 ` [PATCH v2 2/2] perf srcline: Make sentinel reading for binutils addr2line more robust Ian Rogers
@ 2023-06-13  4:02 ` Changbin Du
  2 siblings, 0 replies; 4+ messages in thread
From: Changbin Du @ 2023-06-13  4:02 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Adrian Hunter, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	linux-perf-users, linux-kernel, llvm, Changbin Du, hw.huiwang

The problem is fixed, thanks!

Tested-by: Changbin Du <changbin.du@huawei.com>

On Mon, Jun 12, 2023 at 08:48:15PM -0700, Ian Rogers wrote:
> addr2line makes it hard to detect when inline output has finished. To
> detect it srcline issues a bogus ',' symbol and expects to see a
> filename:line of "??:0". For binutils addr2line ',' gets turned into
> address 0 and if there is a symbol there then the sentinel isn't
> detected, this problem has been occurring with kernel binaries.
> 
> The issue was reported by and debugged by Changbin Du <changbin.du@huawei.com>.
> 
> v2. Incorporate feedback from Changbin.
> 
> Ian Rogers (2):
>   perf srcline: Make addr2line configuration failure more verbose
>   perf srcline: Make sentinel reading for binutils addr2line more robust
> 
>  tools/perf/util/srcline.c | 93 ++++++++++++++++++++++++++++++---------
>  1 file changed, 73 insertions(+), 20 deletions(-)
> 
> -- 
> 2.41.0.162.gfafddb0af9-goog
> 

-- 
Cheers,
Changbin Du

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-13  4:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-13  3:48 [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Ian Rogers
2023-06-13  3:48 ` [PATCH v2 1/2] perf srcline: Make addr2line configuration failure more verbose Ian Rogers
2023-06-13  3:48 ` [PATCH v2 2/2] perf srcline: Make sentinel reading for binutils addr2line more robust Ian Rogers
2023-06-13  4:02 ` [PATCH v2 0/2] Fix srcline addr2line issue with the ',' sentinel Changbin Du

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).