Skip to content

Commit 4c1d8f0

Browse files
committed
perf disasm: Introduce symbol__disassemble_objdump()
With the first disassemble method in perf, the parsing of objdump output, just like we have for llvm and capstone. This paves the way to allow the user to specify what disassemblers are preferred and to also to at some point allow building without the objdump method. Acked-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Athira Rajeev <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Steinar H. Gunderson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent ddbfb6f commit 4c1d8f0

File tree

1 file changed

+88
-80
lines changed

1 file changed

+88
-80
lines changed

tools/perf/util/disasm.c

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,17 +2045,14 @@ static char *expand_tabs(char *line, char **storage, size_t *storage_len)
20452045
return new_line;
20462046
}
20472047

2048-
int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
2048+
static int symbol__disassemble_objdump(const char *filename, struct symbol *sym,
2049+
struct annotate_args *args)
20492050
{
20502051
struct annotation_options *opts = &annotate_opts;
20512052
struct map *map = args->ms.map;
20522053
struct dso *dso = map__dso(map);
20532054
char *command;
20542055
FILE *file;
2055-
char symfs_filename[PATH_MAX];
2056-
struct kcore_extract kce;
2057-
bool delete_extract = false;
2058-
bool decomp = false;
20592056
int lineno = 0;
20602057
char *fileloc = NULL;
20612058
int nline;
@@ -2070,77 +2067,7 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
20702067
NULL,
20712068
};
20722069
struct child_process objdump_process;
2073-
int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
2074-
2075-
if (err)
2076-
return err;
2077-
2078-
pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
2079-
symfs_filename, sym->name, map__unmap_ip(map, sym->start),
2080-
map__unmap_ip(map, sym->end));
2081-
2082-
pr_debug("annotating [%p] %30s : [%p] %30s\n",
2083-
dso, dso__long_name(dso), sym, sym->name);
2084-
2085-
if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
2086-
return symbol__disassemble_bpf(sym, args);
2087-
} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
2088-
return symbol__disassemble_bpf_image(sym, args);
2089-
} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
2090-
return -1;
2091-
} else if (dso__is_kcore(dso)) {
2092-
kce.kcore_filename = symfs_filename;
2093-
kce.addr = map__rip_2objdump(map, sym->start);
2094-
kce.offs = sym->start;
2095-
kce.len = sym->end - sym->start;
2096-
if (!kcore_extract__create(&kce)) {
2097-
delete_extract = true;
2098-
strlcpy(symfs_filename, kce.extract_filename,
2099-
sizeof(symfs_filename));
2100-
}
2101-
} else if (dso__needs_decompress(dso)) {
2102-
char tmp[KMOD_DECOMP_LEN];
2103-
2104-
if (dso__decompress_kmodule_path(dso, symfs_filename,
2105-
tmp, sizeof(tmp)) < 0)
2106-
return -1;
2107-
2108-
decomp = true;
2109-
strcpy(symfs_filename, tmp);
2110-
}
2111-
2112-
/*
2113-
* For powerpc data type profiling, use the dso__data_read_offset
2114-
* to read raw instruction directly and interpret the binary code
2115-
* to understand instructions and register fields. For sort keys as
2116-
* type and typeoff, disassemble to mnemonic notation is
2117-
* not required in case of powerpc.
2118-
*/
2119-
if (arch__is(args->arch, "powerpc")) {
2120-
extern const char *sort_order;
2121-
2122-
if (sort_order && !strstr(sort_order, "sym")) {
2123-
err = symbol__disassemble_raw(symfs_filename, sym, args);
2124-
if (err == 0)
2125-
goto out_remove_tmp;
2126-
#ifdef HAVE_LIBCAPSTONE_SUPPORT
2127-
err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
2128-
if (err == 0)
2129-
goto out_remove_tmp;
2130-
#endif
2131-
}
2132-
}
2133-
2134-
#ifdef HAVE_LIBLLVM_SUPPORT
2135-
err = symbol__disassemble_llvm(symfs_filename, sym, args);
2136-
if (err == 0)
2137-
goto out_remove_tmp;
2138-
#endif
2139-
#ifdef HAVE_LIBCAPSTONE_SUPPORT
2140-
err = symbol__disassemble_capstone(symfs_filename, sym, args);
2141-
if (err == 0)
2142-
goto out_remove_tmp;
2143-
#endif
2070+
int err;
21442071

21452072
err = asprintf(&command,
21462073
"%s %s%s --start-address=0x%016" PRIx64
@@ -2163,13 +2090,13 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
21632090

21642091
if (err < 0) {
21652092
pr_err("Failure allocating memory for the command to run\n");
2166-
goto out_remove_tmp;
2093+
return err;
21672094
}
21682095

21692096
pr_debug("Executing: %s\n", command);
21702097

21712098
objdump_argv[2] = command;
2172-
objdump_argv[4] = symfs_filename;
2099+
objdump_argv[4] = filename;
21732100

21742101
/* Create a pipe to read from for stdout */
21752102
memset(&objdump_process, 0, sizeof(objdump_process));
@@ -2207,8 +2134,8 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
22072134
break;
22082135

22092136
/* Skip lines containing "filename:" */
2210-
match = strstr(line, symfs_filename);
2211-
if (match && match[strlen(symfs_filename)] == ':')
2137+
match = strstr(line, filename);
2138+
if (match && match[strlen(filename)] == ':')
22122139
continue;
22132140

22142141
expanded_line = strim(line);
@@ -2253,6 +2180,87 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
22532180

22542181
out_free_command:
22552182
free(command);
2183+
return err;
2184+
}
2185+
2186+
int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
2187+
{
2188+
struct map *map = args->ms.map;
2189+
struct dso *dso = map__dso(map);
2190+
char symfs_filename[PATH_MAX];
2191+
bool delete_extract = false;
2192+
struct kcore_extract kce;
2193+
bool decomp = false;
2194+
int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
2195+
2196+
if (err)
2197+
return err;
2198+
2199+
pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
2200+
symfs_filename, sym->name, map__unmap_ip(map, sym->start),
2201+
map__unmap_ip(map, sym->end));
2202+
2203+
pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name);
2204+
2205+
if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
2206+
return symbol__disassemble_bpf(sym, args);
2207+
} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
2208+
return symbol__disassemble_bpf_image(sym, args);
2209+
} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
2210+
return -1;
2211+
} else if (dso__is_kcore(dso)) {
2212+
kce.addr = map__rip_2objdump(map, sym->start);
2213+
kce.kcore_filename = symfs_filename;
2214+
kce.len = sym->end - sym->start;
2215+
kce.offs = sym->start;
2216+
2217+
if (!kcore_extract__create(&kce)) {
2218+
delete_extract = true;
2219+
strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename));
2220+
}
2221+
} else if (dso__needs_decompress(dso)) {
2222+
char tmp[KMOD_DECOMP_LEN];
2223+
2224+
if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0)
2225+
return -1;
2226+
2227+
decomp = true;
2228+
strcpy(symfs_filename, tmp);
2229+
}
2230+
2231+
/*
2232+
* For powerpc data type profiling, use the dso__data_read_offset to
2233+
* read raw instruction directly and interpret the binary code to
2234+
* understand instructions and register fields. For sort keys as type
2235+
* and typeoff, disassemble to mnemonic notation is not required in
2236+
* case of powerpc.
2237+
*/
2238+
if (arch__is(args->arch, "powerpc")) {
2239+
extern const char *sort_order;
2240+
2241+
if (sort_order && !strstr(sort_order, "sym")) {
2242+
err = symbol__disassemble_raw(symfs_filename, sym, args);
2243+
if (err == 0)
2244+
goto out_remove_tmp;
2245+
#ifdef HAVE_LIBCAPSTONE_SUPPORT
2246+
err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
2247+
if (err == 0)
2248+
goto out_remove_tmp;
2249+
#endif
2250+
}
2251+
}
2252+
2253+
#ifdef HAVE_LIBLLVM_SUPPORT
2254+
err = symbol__disassemble_llvm(symfs_filename, sym, args);
2255+
if (err == 0)
2256+
goto out_remove_tmp;
2257+
#endif
2258+
#ifdef HAVE_LIBCAPSTONE_SUPPORT
2259+
err = symbol__disassemble_capstone(symfs_filename, sym, args);
2260+
if (err == 0)
2261+
goto out_remove_tmp;
2262+
#endif
2263+
err = symbol__disassemble_objdump(symfs_filename, sym, args);
22562264

22572265
out_remove_tmp:
22582266
if (decomp)

0 commit comments

Comments
 (0)