mirror of
https://github.com/torvalds/linux.git
synced 2024-11-27 06:31:52 +00:00
perf annotate: Add support to use libcapstone in powerpc
Now perf uses the capstone library to disassemble the instructions in x86. capstone is used (if available) for perf annotate to speed up. Currently it only supports x86 architecture. This patch includes changes to enable this in powerpc. For now, only for data type sort keys, this method is used and only binary code (raw instruction) is read. This is because powerpc approach to understand instructions and reg fields uses raw instruction. The "cs_disasm" is currently not enabled. While attempting to do cs_disasm, observation is that some of the instructions were not identified (ex: extswsli, maddld) and it had to fallback to use objdump. Hence enabling "cs_disasm" is added in comment section as a TODO for powerpc. Reviewed-by: Kajol Jain <kjain@linux.ibm.com> Reviewed-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Tested-by: Kajol Jain <kjain@linux.ibm.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Akanksha J N <akanksha@linux.ibm.com> Cc: Christophe Leroy <christophe.leroy@csgroup.eu> Cc: Disha Goel <disgoel@linux.vnet.ibm.com> Cc: Hari Bathini <hbathini@linux.ibm.com> Cc: Ian Rogers <irogers@google.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Madhavan Srinivasan <maddy@linux.ibm.com> Cc: Segher Boessenkool <segher@kernel.crashing.org> Link: https://lore.kernel.org/lkml/20240718084358.72242-15-atrajeev@linux.vnet.ibm.com [ Use dso__nsinfo(dso) as required to match EXTRA_CFLAGS=-DREFCNT_CHECKING=1 build expectations ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
parent
f1e9347c85
commit
c5d60de181
@ -1614,6 +1614,144 @@ static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
|
||||
}
|
||||
}
|
||||
|
||||
static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
|
||||
struct annotate_args *args)
|
||||
{
|
||||
struct annotation *notes = symbol__annotation(sym);
|
||||
struct map *map = args->ms.map;
|
||||
struct dso *dso = map__dso(map);
|
||||
struct nscookie nsc;
|
||||
u64 start = map__rip_2objdump(map, sym->start);
|
||||
u64 end = map__rip_2objdump(map, sym->end);
|
||||
u64 len = end - start;
|
||||
u64 offset;
|
||||
int i, fd, count;
|
||||
bool is_64bit = false;
|
||||
bool needs_cs_close = false;
|
||||
u8 *buf = NULL;
|
||||
struct find_file_offset_data data = {
|
||||
.ip = start,
|
||||
};
|
||||
csh handle;
|
||||
char disasm_buf[512];
|
||||
struct disasm_line *dl;
|
||||
u32 *line;
|
||||
bool disassembler_style = false;
|
||||
|
||||
if (args->options->objdump_path)
|
||||
return -1;
|
||||
|
||||
nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
|
||||
fd = open(filename, O_RDONLY);
|
||||
nsinfo__mountns_exit(&nsc);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
|
||||
&is_64bit) == 0)
|
||||
goto err;
|
||||
|
||||
if (!args->options->disassembler_style ||
|
||||
!strcmp(args->options->disassembler_style, "att"))
|
||||
disassembler_style = true;
|
||||
|
||||
if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
|
||||
goto err;
|
||||
|
||||
needs_cs_close = true;
|
||||
|
||||
buf = malloc(len);
|
||||
if (buf == NULL)
|
||||
goto err;
|
||||
|
||||
count = pread(fd, buf, len, data.offset);
|
||||
close(fd);
|
||||
fd = -1;
|
||||
|
||||
if ((u64)count != len)
|
||||
goto err;
|
||||
|
||||
line = (u32 *)buf;
|
||||
|
||||
/* add the function address and name */
|
||||
scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
|
||||
start, sym->name);
|
||||
|
||||
args->offset = -1;
|
||||
args->line = disasm_buf;
|
||||
args->line_nr = 0;
|
||||
args->fileloc = NULL;
|
||||
args->ms.sym = sym;
|
||||
|
||||
dl = disasm_line__new(args);
|
||||
if (dl == NULL)
|
||||
goto err;
|
||||
|
||||
annotation_line__add(&dl->al, ¬es->src->source);
|
||||
|
||||
/*
|
||||
* TODO: enable disassm for powerpc
|
||||
* count = cs_disasm(handle, buf, len, start, len, &insn);
|
||||
*
|
||||
* For now, only binary code is saved in disassembled line
|
||||
* to be used in "type" and "typeoff" sort keys. Each raw code
|
||||
* is 32 bit instruction. So use "len/4" to get the number of
|
||||
* entries.
|
||||
*/
|
||||
count = len/4;
|
||||
|
||||
for (i = 0, offset = 0; i < count; i++) {
|
||||
args->offset = offset;
|
||||
sprintf(args->line, "%x", line[i]);
|
||||
|
||||
dl = disasm_line__new(args);
|
||||
if (dl == NULL)
|
||||
goto err;
|
||||
|
||||
annotation_line__add(&dl->al, ¬es->src->source);
|
||||
|
||||
offset += 4;
|
||||
}
|
||||
|
||||
/* It failed in the middle */
|
||||
if (offset != len) {
|
||||
struct list_head *list = ¬es->src->source;
|
||||
|
||||
/* Discard all lines and fallback to objdump */
|
||||
while (!list_empty(list)) {
|
||||
dl = list_first_entry(list, struct disasm_line, al.node);
|
||||
|
||||
list_del_init(&dl->al.node);
|
||||
disasm_line__free(dl);
|
||||
}
|
||||
count = -1;
|
||||
}
|
||||
|
||||
out:
|
||||
if (needs_cs_close)
|
||||
cs_close(&handle);
|
||||
free(buf);
|
||||
return count < 0 ? count : 0;
|
||||
|
||||
err:
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
if (needs_cs_close) {
|
||||
struct disasm_line *tmp;
|
||||
|
||||
/*
|
||||
* It probably failed in the middle of the above loop.
|
||||
* Release any resources it might add.
|
||||
*/
|
||||
list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) {
|
||||
list_del(&dl->al.node);
|
||||
free(dl);
|
||||
}
|
||||
}
|
||||
count = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
|
||||
struct annotate_args *args)
|
||||
{
|
||||
@ -1968,6 +2106,11 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
|
||||
err = symbol__disassemble_raw(symfs_filename, sym, args);
|
||||
if (err == 0)
|
||||
goto out_remove_tmp;
|
||||
#ifdef HAVE_LIBCAPSTONE_SUPPORT
|
||||
err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
|
||||
if (err == 0)
|
||||
goto out_remove_tmp;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user