Skip to content

Commit 8dc26b6

Browse files
captain5050acmel
authored andcommitted
perf srcline: Make sentinel reading for binutils addr2line more robust
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 <[email protected]> Signed-off-by: Ian Rogers <[email protected]> Tested-by: Changbin Du <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Tom Rix <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c7a0023 commit 8dc26b6

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

tools/perf/util/srcline.c

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ static struct child_process *addr2line_subprocess_init(const char *addr2line_pat
408408
const char *argv[] = {
409409
addr2line_path ?: "addr2line",
410410
"-e", binary_path,
411-
"-i", "-f", NULL
411+
"-a", "-i", "-f", NULL
412412
};
413413
struct child_process *a2l = zalloc(sizeof(*a2l));
414414
int start_command_status = 0;
@@ -463,10 +463,10 @@ static enum a2l_style addr2line_configure(struct child_process *a2l, const char
463463
style = LLVM;
464464
cached = true;
465465
lines = 1;
466-
} else if (ch == '?') {
466+
} else if (ch == '0') {
467467
style = GNU_BINUTILS;
468468
cached = true;
469-
lines = 2;
469+
lines = 3;
470470
} else {
471471
if (!symbol_conf.disable_add2line_warn) {
472472
char *output = NULL;
@@ -518,20 +518,64 @@ static int read_addr2line_record(struct io *io,
518518
if (line_nr != NULL)
519519
*line_nr = 0;
520520

521+
/*
522+
* Read the first line. Without an error this will be either an address
523+
* like 0x1234 or for llvm-addr2line the sentinal ',' character.
524+
*/
521525
if (io__getline(io, &line, &line_len) < 0 || !line_len)
522526
goto error;
523527

524-
if (style == LLVM && line_len == 2 && line[0] == ',') {
525-
zfree(&line);
526-
return 0;
528+
if (style == LLVM) {
529+
if (line_len == 2 && line[0] == ',') {
530+
zfree(&line);
531+
return 0;
532+
}
533+
} else {
534+
int zero_count = 0, non_zero_count = 0;
535+
536+
/* The address should always start 0x. */
537+
if (line_len < 2 || line[0] != '0' || line[1] != 'x')
538+
goto error;
539+
540+
for (size_t i = 2; i < line_len; i++) {
541+
if (line[i] == '0')
542+
zero_count++;
543+
else if (line[i] != '\n')
544+
non_zero_count++;
545+
}
546+
if (!non_zero_count) {
547+
int ch;
548+
549+
if (!zero_count) {
550+
/* Line was erroneous just '0x'. */
551+
goto error;
552+
}
553+
/*
554+
* Line was 0x0..0, the sentinel for binutils. Remove
555+
* the function and filename lines.
556+
*/
557+
zfree(&line);
558+
do {
559+
ch = io__get_char(io);
560+
} while (ch > 0 && ch != '\n');
561+
do {
562+
ch = io__get_char(io);
563+
} while (ch > 0 && ch != '\n');
564+
return 0;
565+
}
527566
}
528567

568+
/* Read the second function name line. */
569+
if (io__getline(io, &line, &line_len) < 0 || !line_len)
570+
goto error;
571+
529572
if (function != NULL)
530573
*function = strdup(strim(line));
531574

532575
zfree(&line);
533576
line_len = 0;
534577

578+
/* Read the third filename and line number line. */
535579
if (io__getline(io, &line, &line_len) < 0 || !line_len)
536580
goto error;
537581

@@ -635,8 +679,9 @@ static int addr2line(const char *dso_name, u64 addr,
635679
goto out;
636680
case 0:
637681
/*
638-
* The first record was invalid, so return failure, but first read another
639-
* record, since we asked a junk question and have to clear the answer out.
682+
* The first record was invalid, so return failure, but first
683+
* read another record, since we sent a sentinel ',' for the
684+
* sake of detected the last inlined function.
640685
*/
641686
switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) {
642687
case -1:

0 commit comments

Comments
 (0)