diff options
Diffstat (limited to 'tools/perf/util/capstone.c')
| -rw-r--r-- | tools/perf/util/capstone.c | 66 |
1 files changed, 11 insertions, 55 deletions
diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c index 01e47d5c8e3e..c23df911e91c 100644 --- a/tools/perf/util/capstone.c +++ b/tools/perf/util/capstone.c @@ -215,55 +215,6 @@ static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg) } #endif -#ifdef HAVE_LIBCAPSTONE_SUPPORT -static u8 * -read_symbol(const char *filename, struct map *map, struct symbol *sym, - u64 *len, bool *is_64bit) -{ - 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); - int fd, count; - u8 *buf = NULL; - struct find_file_offset_data data = { - .ip = start, - }; - - *is_64bit = false; - - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd = open(filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) - return NULL; - - if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, - is_64bit) == 0) - goto err; - - *len = end - start; - 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; - - return buf; - -err: - if (fd >= 0) - close(fd); - free(buf); - return NULL; -} -#endif - int symbol__disassemble_capstone(const char *filename __maybe_unused, struct symbol *sym __maybe_unused, struct annotate_args *args __maybe_unused) @@ -271,13 +222,17 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, #ifdef HAVE_LIBCAPSTONE_SUPPORT struct annotation *notes = symbol__annotation(sym); struct map *map = args->ms.map; + struct dso *dso = map__dso(map); u64 start = map__rip_2objdump(map, sym->start); - u64 len; u64 offset; int i, count, free_count; bool is_64bit = false; bool needs_cs_close = false; - u8 *buf = NULL; + /* Malloc-ed buffer containing instructions read from disk. */ + u8 *code_buf = NULL; + /* Pointer to code to be disassembled. */ + const u8 *buf; + u64 buf_len; csh handle; cs_insn *insn = NULL; char disasm_buf[512]; @@ -287,7 +242,8 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, if (args->options->objdump_path) return -1; - buf = read_symbol(filename, map, sym, &len, &is_64bit); + buf = dso__read_symbol(dso, filename, map, sym, + &code_buf, &buf_len, &is_64bit); if (buf == NULL) return -1; @@ -316,7 +272,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, needs_cs_close = true; - free_count = count = cs_disasm(handle, buf, len, start, len, &insn); + free_count = count = cs_disasm(handle, buf, buf_len, start, buf_len, &insn); for (i = 0, offset = 0; i < count; i++) { int printed; @@ -340,7 +296,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, } /* It failed in the middle: probably due to unknown instructions */ - if (offset != len) { + if (offset != buf_len) { struct list_head *list = ¬es->src->source; /* Discard all lines and fallback to objdump */ @@ -359,7 +315,7 @@ out: if (free_count > 0) cs_free(insn, free_count); } - free(buf); + free(code_buf); return count < 0 ? count : 0; err: |
