perf symbol: Add command line support for addr2line path
authorIan Rogers <irogers@google.com>
Tue, 28 Mar 2023 23:55:43 +0000 (16:55 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 4 Apr 2023 12:39:56 +0000 (09:39 -0300)
Allow addr2line to be set either on the command line or via the
perfconfig file. This doesn't currently work with llvm-addr2line as
the addr2line code emits two things:
1) the address to decode,
2) a bogus ',' value.
The expectation is the bogus value will generate:
??
??:0
that terminates the addr2line reading. However, the output from
llvm-addr2line is a single line with just the input ',' locking up the
addr2line reading that is expecting a second line.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: German Gomez <german.gomez@arm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Tom Rix <trix@redhat.com>
Cc: llvm@lists.linux.dev
Link: https://lore.kernel.org/r/20230328235543.1082207-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-annotate.txt
tools/perf/Documentation/perf-config.txt
tools/perf/Documentation/perf-report.txt
tools/perf/Documentation/perf-top.txt
tools/perf/builtin-annotate.c
tools/perf/builtin-report.c
tools/perf/builtin-top.c
tools/perf/util/annotate.c
tools/perf/util/srcline.c
tools/perf/util/symbol_conf.h

index 980fe2c292752a795e732898b9cbfa9ddd4b4b23..fe168e8165c8d22dd51933da407c9388c685f252 100644 (file)
@@ -116,6 +116,9 @@ include::itrace.txt[]
 -M::
 --disassembler-style=:: Set disassembler style for objdump.
 
+--addr2line=<path>::
+        Path to addr2line binary.
+
 --objdump=<path>::
         Path to objdump binary.
 
index 697f7f924545762ef2c4b65bf2806981d813bf64..e56ae54805a845ecfaa0b051ff02d48e9cd90f3a 100644 (file)
@@ -250,6 +250,9 @@ annotate.*::
        These are in control of addresses, jump function, source code
        in lines of assembly code from a specific program.
 
+       annotate.addr2line::
+               addr2line binary to use for file names and line numbers.
+
        annotate.objdump::
                objdump binary to use for disassembly and annotations.
 
index cfd502f7e6da5c145f004847cfa130fcf8c669c4..af068b4f1e5a696464ba2040b7b39a4831b32050 100644 (file)
@@ -381,6 +381,9 @@ OPTIONS
        This allows to examine the path the program took to each sample.
        The data collection must have used -b (or -j) and -g.
 
+--addr2line=<path>::
+        Path to addr2line binary.
+
 --objdump=<path>::
         Path to objdump binary.
 
index c60e615b7183c73084d9fa44581fd6a77b94e264..619cc8143ad522539348227c2515c7b289c0e154 100644 (file)
@@ -161,6 +161,12 @@ Default is to monitor all CPUS.
 -M::
 --disassembler-style=:: Set disassembler style for objdump.
 
+--addr2line=<path>::
+        Path to addr2line binary.
+
+--objdump=<path>::
+        Path to objdump binary.
+
 --prefix=PREFIX::
 --prefix-strip=N::
         Remove first N entries from source file path names in executables
index 5b31c4ee8d066aaf0187114ce385dce3c6438e5a..655987afef58c211efa276536d14d6f255f91dec 100644 (file)
@@ -517,7 +517,7 @@ int cmd_annotate(int argc, const char **argv)
        struct itrace_synth_opts itrace_synth_opts = {
                .set = 0,
        };
-       const char *disassembler_style = NULL, *objdump_path = NULL;
+       const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
        struct option options[] = {
        OPT_STRING('i', "input", &input_name, "file",
                    "input file name"),
@@ -570,6 +570,8 @@ int cmd_annotate(int argc, const char **argv)
                    "Strip first N entries of source file path name in programs (with --prefix)"),
        OPT_STRING(0, "objdump", &objdump_path, "path",
                   "objdump binary to use for disassembly and annotations"),
+       OPT_STRING(0, "addr2line", &addr2line_path, "path",
+                  "addr2line binary to use for line numbers"),
        OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
                    "Enable symbol demangling"),
        OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
@@ -629,6 +631,11 @@ int cmd_annotate(int argc, const char **argv)
                if (!annotate.opts.objdump_path)
                        return -ENOMEM;
        }
+       if (addr2line_path) {
+               symbol_conf.addr2line_path = strdup(addr2line_path);
+               if (!symbol_conf.addr2line_path)
+                       return -ENOMEM;
+       }
 
        if (annotate_check_args(&annotate.opts) < 0)
                return -EINVAL;
index 15b0cf649e1aeced662aadd6a94d40b1f3095d91..4011abc03d0de3bb4d91c4cf0059073fdaa0dd97 100644 (file)
@@ -1226,7 +1226,7 @@ int cmd_report(int argc, const char **argv)
        };
        char *sort_order_help = sort_help("sort by key(s):");
        char *field_order_help = sort_help("output field(s): overhead period sample ");
-       const char *disassembler_style = NULL, *objdump_path = NULL;
+       const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
        const struct option options[] = {
        OPT_STRING('i', "input", &input_name, "file",
                    "input file name"),
@@ -1344,6 +1344,8 @@ int cmd_report(int argc, const char **argv)
                    "add last branch records to call history"),
        OPT_STRING(0, "objdump", &objdump_path, "path",
                   "objdump binary to use for disassembly and annotations"),
+       OPT_STRING(0, "addr2line", &addr2line_path, "path",
+                  "addr2line binary to use for line numbers"),
        OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
                    "Disable symbol demangling"),
        OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
@@ -1430,6 +1432,11 @@ int cmd_report(int argc, const char **argv)
                if (!report.annotation_opts.objdump_path)
                        return -ENOMEM;
        }
+       if (addr2line_path) {
+               symbol_conf.addr2line_path = strdup(addr2line_path);
+               if (!symbol_conf.addr2line_path)
+                       return -ENOMEM;
+       }
 
        if (annotate_check_args(&report.annotation_opts) < 0) {
                ret = -EINVAL;
index 57a273cd03deef5a5ce9cf61706c971bfd687349..82c6c065830dc562da73dbb51d8f830a8d50186f 100644 (file)
@@ -1439,7 +1439,7 @@ int cmd_top(int argc, const char **argv)
        };
        struct record_opts *opts = &top.record_opts;
        struct target *target = &opts->target;
-       const char *disassembler_style = NULL, *objdump_path = NULL;
+       const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
        const struct option options[] = {
        OPT_CALLBACK('e', "event", &top.evlist, "event",
                     "event selector. use 'perf list' to list available events",
@@ -1527,6 +1527,8 @@ int cmd_top(int argc, const char **argv)
        OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"),
        OPT_STRING(0, "objdump", &objdump_path, "path",
                    "objdump binary to use for disassembly and annotations"),
+       OPT_STRING(0, "addr2line", &addr2line_path, "path",
+                  "addr2line binary to use for line numbers"),
        OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
                   "Specify disassembler style (e.g. -M intel for intel syntax)"),
        OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
@@ -1629,7 +1631,11 @@ int cmd_top(int argc, const char **argv)
                if (!top.annotation_opts.objdump_path)
                        return -ENOMEM;
        }
-
+       if (addr2line_path) {
+               symbol_conf.addr2line_path = strdup(addr2line_path);
+               if (!symbol_conf.addr2line_path)
+                       return -ENOMEM;
+       }
 
        status = symbol__validate_sym_arguments();
        if (status)
index 317c987d3001d27fa20e1fe903e15b2bc25151bb..f47b5dde66bcd74dcda00527434b8f841811debe 100644 (file)
@@ -3217,6 +3217,12 @@ static int annotation__config(const char *var, const char *value, void *data)
                        pr_err("Not enough memory for annotate.objdump\n");
                        return -1;
                }
+       } else if (!strcmp(var, "annotate.addr2line")) {
+               symbol_conf.addr2line_path = strdup(value);
+               if (!symbol_conf.addr2line_path) {
+                       pr_err("Not enough memory for annotate.addr2line\n");
+                       return -1;
+               }
        } else if (!strcmp(var, "annotate.demangle")) {
                symbol_conf.demangle = perf_config_bool("demangle", value);
        } else if (!strcmp(var, "annotate.demangle_kernel")) {
index 33321867416b3570d1cbb018764b36e05ad5defa..f0a96a834e4b6d5fb438074c77586699e6c6009e 100644 (file)
@@ -414,9 +414,14 @@ static void addr2line_subprocess_cleanup(struct a2l_subprocess *a2l)
        free(a2l);
 }
 
-static struct a2l_subprocess *addr2line_subprocess_init(const char *path)
-{
-       const char *argv[] = { "addr2line", "-e", path, "-i", "-f", NULL };
+static struct a2l_subprocess *addr2line_subprocess_init(const char *addr2line_path,
+                                                       const char *binary_path)
+{
+       const char *argv[] = {
+               addr2line_path ?: "addr2line",
+               "-e", binary_path,
+               "-i", "-f", NULL
+       };
        struct a2l_subprocess *a2l = zalloc(sizeof(*a2l));
        int start_command_status = 0;
 
@@ -436,21 +441,22 @@ static struct a2l_subprocess *addr2line_subprocess_init(const char *path)
        a2l->addr2line.argv = NULL; /* it's not used after start_command; avoid dangling pointers */
 
        if (start_command_status != 0) {
-               pr_warning("could not start addr2line for %s: start_command return code %d\n",
-                          path,
-                          start_command_status);
+               pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n",
+                       addr2line_path, binary_path, start_command_status);
                goto out;
        }
 
        a2l->to_child = fdopen(a2l->addr2line.in, "w");
        if (a2l->to_child == NULL) {
-               pr_warning("could not open write-stream to addr2line of %s\n", path);
+               pr_warning("could not open write-stream to addr2line (%s) of %s\n",
+                       addr2line_path, binary_path);
                goto out;
        }
 
        a2l->from_child = fdopen(a2l->addr2line.out, "r");
        if (a2l->from_child == NULL) {
-               pr_warning("could not open read-stream from addr2line of %s\n", path);
+               pr_warning("could not open read-stream from addr2line (%s) of %s\n",
+                       addr2line_path, binary_path);
                goto out;
        }
 
@@ -490,7 +496,6 @@ static int read_addr2line_record(struct a2l_subprocess *a2l,
 
        if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len)
                goto error;
-
        if (function != NULL)
                *function = strdup(strim(line));
 
@@ -553,7 +558,8 @@ static int addr2line(const char *dso_name, u64 addr,
                if (!filename__has_section(dso_name, ".debug_line"))
                        goto out;
 
-               dso->a2l = addr2line_subprocess_init(dso_name);
+               dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path,
+                                                    dso_name);
                a2l = dso->a2l;
        }
 
index bc3d046fbb632a4afe6f97ad50b5744e7331c1d8..5accd8e69ad258d2438cf29195bc6358f9aa03c7 100644 (file)
@@ -61,6 +61,7 @@ struct symbol_conf {
                        *sym_list_str,
                        *col_width_list_str,
                        *bt_stop_list_str;
+       char            *addr2line_path;
        unsigned long   time_quantum;
        struct strlist  *dso_list,
                        *comm_list,