Skip to content

Commit 3b0b16b

Browse files
Andi Kleenacmel
authored andcommitted
perf tools: Support --prefix/--prefix-strip
The objdump utility has useful --prefix / --prefix-strip options to allow changing source code file names hardcoded into executables' debug info. Add options to 'perf report', 'perf top' and 'perf annotate', which are then passed to objdump. $ mkdir foo $ echo 'main() { for (;;); }' > foo/foo.c $ gcc -g foo/foo.c foo/foo.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int] 1 | main() { for (;;); } | ^~~~ $ perf record ./a.out ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.230 MB perf.data (5721 samples) ] $ mv foo bar $ perf annotate <does not show source code> $ perf annotate --prefix=/home/ak/lsrc/git/bar --prefix-strip=5 <does show source code> Signed-off-by: Andi Kleen <[email protected]> Tested-by: Jiri Olsa <[email protected]> LPU-Reference: [email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent aa9d1f8 commit 3b0b16b

File tree

8 files changed

+61
-2
lines changed

8 files changed

+61
-2
lines changed

tools/perf/Documentation/perf-annotate.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ OPTIONS
112112
--objdump=<path>::
113113
Path to objdump binary.
114114

115+
--prefix=PREFIX::
116+
--prefix-strip=N::
117+
Remove first N entries from source file path names in executables
118+
and add PREFIX. This allows to display source code compiled on systems
119+
with different file system layout.
120+
115121
--skip-missing::
116122
Skip symbols that cannot be annotated.
117123

tools/perf/Documentation/perf-report.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ OPTIONS
367367
--objdump=<path>::
368368
Path to objdump binary.
369369

370+
--prefix=PREFIX::
371+
--prefix-strip=N::
372+
Remove first N entries from source file path names in executables
373+
and add PREFIX. This allows to display source code compiled on systems
374+
with different file system layout.
375+
370376
--group::
371377
Show event group information together. It forces group output also
372378
if there are no groups defined in data file.

tools/perf/Documentation/perf-top.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ Default is to monitor all CPUS.
158158
-M::
159159
--disassembler-style=:: Set disassembler style for objdump.
160160

161+
--prefix=PREFIX::
162+
--prefix-strip=N::
163+
Remove first N entries from source file path names in executables
164+
and add PREFIX. This allows to display source code compiled on systems
165+
with different file system layout.
166+
161167
--source::
162168
Interleave source code with assembly code. Enabled by default,
163169
disable with --no-source.

tools/perf/builtin-annotate.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ int cmd_annotate(int argc, const char **argv)
535535
"Display raw encoding of assembly instructions (default)"),
536536
OPT_STRING('M', "disassembler-style", &annotate.opts.disassembler_style, "disassembler style",
537537
"Specify disassembler style (e.g. -M intel for intel syntax)"),
538+
OPT_STRING(0, "prefix", &annotate.opts.prefix, "prefix",
539+
"Add prefix to source file path names in programs (with --prefix-strip)"),
540+
OPT_STRING(0, "prefix-strip", &annotate.opts.prefix_strip, "N",
541+
"Strip first N entries of source file path name in programs (with --prefix)"),
538542
OPT_STRING(0, "objdump", &annotate.opts.objdump_path, "path",
539543
"objdump binary to use for disassembly and annotations"),
540544
OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
@@ -574,6 +578,9 @@ int cmd_annotate(int argc, const char **argv)
574578
annotate.sym_hist_filter = argv[0];
575579
}
576580

581+
if (annotate_check_args(&annotate.opts) < 0)
582+
return -EINVAL;
583+
577584
if (symbol_conf.show_nr_samples && annotate.use_gtk) {
578585
pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
579586
return ret;

tools/perf/builtin-report.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,10 @@ int cmd_report(int argc, const char **argv)
12081208
"Display raw encoding of assembly instructions (default)"),
12091209
OPT_STRING('M', "disassembler-style", &report.annotation_opts.disassembler_style, "disassembler style",
12101210
"Specify disassembler style (e.g. -M intel for intel syntax)"),
1211+
OPT_STRING(0, "prefix", &report.annotation_opts.prefix, "prefix",
1212+
"Add prefix to source file path names in programs (with --prefix-strip)"),
1213+
OPT_STRING(0, "prefix-strip", &report.annotation_opts.prefix_strip, "N",
1214+
"Strip first N entries of source file path name in programs (with --prefix)"),
12111215
OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
12121216
"Show a column with the sum of periods"),
12131217
OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
@@ -1287,6 +1291,9 @@ int cmd_report(int argc, const char **argv)
12871291
report.symbol_filter_str = argv[0];
12881292
}
12891293

1294+
if (annotate_check_args(&report.annotation_opts) < 0)
1295+
return -EINVAL;
1296+
12901297
if (report.mmaps_mode)
12911298
report.tasks_mode = true;
12921299

tools/perf/builtin-top.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,10 @@ int cmd_top(int argc, const char **argv)
15121512
"objdump binary to use for disassembly and annotations"),
15131513
OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
15141514
"Specify disassembler style (e.g. -M intel for intel syntax)"),
1515+
OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
1516+
"Add prefix to source file path names in programs (with --prefix-strip)"),
1517+
OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N",
1518+
"Strip first N entries of source file path name in programs (with --prefix)"),
15151519
OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
15161520
OPT_CALLBACK(0, "percent-limit", &top, "percent",
15171521
"Don't show entries under that percent", parse_percent_limit),
@@ -1582,6 +1586,9 @@ int cmd_top(int argc, const char **argv)
15821586
if (argc)
15831587
usage_with_options(top_usage, options);
15841588

1589+
if (annotate_check_args(&top.annotation_opts) < 0)
1590+
goto out_delete_evlist;
1591+
15851592
if (!top.evlist->core.nr_entries &&
15861593
perf_evlist__add_default(top.evlist) < 0) {
15871594
pr_err("Not enough memory for event selector list\n");

tools/perf/util/annotate.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,14 +1966,20 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
19661966
err = asprintf(&command,
19671967
"%s %s%s --start-address=0x%016" PRIx64
19681968
" --stop-address=0x%016" PRIx64
1969-
" -l -d %s %s -C \"$1\"",
1969+
" -l -d %s %s %s %c%s%c %s%s -C \"$1\"",
19701970
opts->objdump_path ?: "objdump",
19711971
opts->disassembler_style ? "-M " : "",
19721972
opts->disassembler_style ?: "",
19731973
map__rip_2objdump(map, sym->start),
19741974
map__rip_2objdump(map, sym->end),
19751975
opts->show_asm_raw ? "" : "--no-show-raw-insn",
1976-
opts->annotate_src ? "-S" : "");
1976+
opts->annotate_src ? "-S" : "",
1977+
opts->prefix ? "--prefix " : "",
1978+
opts->prefix ? '"' : ' ',
1979+
opts->prefix ?: "",
1980+
opts->prefix ? '"' : ' ',
1981+
opts->prefix_strip ? "--prefix-strip=" : "",
1982+
opts->prefix_strip ?: "");
19771983

19781984
if (err < 0) {
19791985
pr_err("Failure allocating memory for the command to run\n");
@@ -3204,3 +3210,12 @@ int annotate_parse_percent_type(const struct option *opt, const char *_str,
32043210
free(str1);
32053211
return err;
32063212
}
3213+
3214+
int annotate_check_args(struct annotation_options *args)
3215+
{
3216+
if (args->prefix_strip && !args->prefix) {
3217+
pr_err("--prefix-strip requires --prefix\n");
3218+
return -1;
3219+
}
3220+
return 0;
3221+
}

tools/perf/util/annotate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ struct annotation_options {
9494
int context;
9595
const char *objdump_path;
9696
const char *disassembler_style;
97+
const char *prefix;
98+
const char *prefix_strip;
9799
unsigned int percent_type;
98100
};
99101

@@ -415,4 +417,7 @@ void annotation_config__init(void);
415417

416418
int annotate_parse_percent_type(const struct option *opt, const char *_str,
417419
int unset);
420+
421+
int annotate_check_args(struct annotation_options *args);
422+
418423
#endif /* __PERF_ANNOTATE_H */

0 commit comments

Comments
 (0)