From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161682AbdAJBDJ (ORCPT ); Mon, 9 Jan 2017 20:03:09 -0500 Received: from mga06.intel.com ([134.134.136.31]:42159 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S942637AbdAJBCd (ORCPT ); Mon, 9 Jan 2017 20:02:33 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,341,1477983600"; d="scan'208";a="51946655" From: Andi Kleen To: acme@kernel.org Cc: jolsa@kernel.org, mingo@kernel.org, linux-kernel@vger.kernel.org, Andi Kleen Subject: [PATCH 3/5] perf, tools: Add disassembler for x86 using the XED library Date: Mon, 9 Jan 2017 17:02:23 -0800 Message-Id: <20170110010225.24870-4-andi@firstfloor.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170110010225.24870-1-andi@firstfloor.org> References: <20170110010225.24870-1-andi@firstfloor.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andi Kleen Add a generic disassembler function for x86 using the XED library, and a fallback function for architectures that don't implement one. Other architectures can implement their own disassembler functions. The previous version of this patch used udis86, but was rejected because udis86 was unmaintained and a runtime dependency. Using the recently released xed avoids both of these problems: - XED is well maintained, uptodate, and used by many Intel tools - XED is linked statically so there is no runtime dependency. The XED library can be downloaded from http://github.com/intelxed/xed v2: Clean up includes. Signed-off-by: Andi Kleen --- tools/perf/arch/x86/util/Build | 1 + tools/perf/arch/x86/util/dis.c | 86 ++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/Build | 1 + tools/perf/util/dis.c | 15 ++++++++ tools/perf/util/dis.h | 20 ++++++++++ 5 files changed, 123 insertions(+) create mode 100644 tools/perf/arch/x86/util/dis.c create mode 100644 tools/perf/util/dis.c create mode 100644 tools/perf/util/dis.h diff --git a/tools/perf/arch/x86/util/Build b/tools/perf/arch/x86/util/Build index f95e6f46ef0d..93490009ea6a 100644 --- a/tools/perf/arch/x86/util/Build +++ b/tools/perf/arch/x86/util/Build @@ -14,3 +14,4 @@ libperf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o libperf-$(CONFIG_AUXTRACE) += auxtrace.o libperf-$(CONFIG_AUXTRACE) += intel-pt.o libperf-$(CONFIG_AUXTRACE) += intel-bts.o +libperf-$(CONFIG_XED) += dis.o diff --git a/tools/perf/arch/x86/util/dis.c b/tools/perf/arch/x86/util/dis.c new file mode 100644 index 000000000000..39703512fe17 --- /dev/null +++ b/tools/perf/arch/x86/util/dis.c @@ -0,0 +1,86 @@ +/* Disassembler using the XED library */ +#include "perf.h" +#include "util/session.h" +#include "util/symbol.h" +#include "util/thread.h" +#include "util/dis.h" + +#include +#include +#include + +static int dis_resolve(xed_uint64_t addr, char *buf, xed_uint32_t buflen, + xed_uint64_t *off, void *data) +{ + struct perf_dis *x = data; + struct addr_location al; + + memset(&al, 0, sizeof(struct addr_location)); + + thread__find_addr_map(x->thread, x->cpumode, MAP__FUNCTION, addr, &al); + if (!al.map) + thread__find_addr_map(x->thread, x->cpumode, MAP__VARIABLE, + addr, &al); + al.cpu = x->cpu; + al.sym = NULL; + + if (al.map) + al.sym = map__find_symbol(al.map, al.addr); + + if (!al.sym) + return 0; + + if (al.addr < al.sym->end) + *off = al.addr - al.sym->start; + else + *off = al.addr - al.map->start - al.sym->start; + snprintf(buf, buflen, "%s", al.sym->name); + return 1; +} + +/* x must be set up earlier */ +char *disas_inst(struct perf_dis *x, uint64_t ip, u8 *inbuf, int inlen, + int *lenp) +{ + xed_decoded_inst_t inst; + xed_print_info_t info; + xed_error_enum_t err; + static bool init; + + if (!init) { + xed_tables_init(); + init = true; + } + + if (lenp) + *lenp = 0; + + xed_init_print_info(&info); + info.syntax = XED_SYNTAX_ATT; + info.disassembly_callback = dis_resolve; + info.context = x; + + xed_decoded_inst_zero(&inst); + if (x->is64bit) + xed_decoded_inst_set_mode(&inst, XED_MACHINE_MODE_LONG_64, + XED_ADDRESS_WIDTH_64b); + else + xed_decoded_inst_set_mode(&inst, XED_MACHINE_MODE_LEGACY_32, + XED_ADDRESS_WIDTH_32b); + + err = xed_decode(&inst, (uint8_t *)inbuf, inlen); + if (err != XED_ERROR_NONE) { + snprintf(x->out, sizeof(x->out), "err: %s for %d bytes", + xed_error_enum_t2str(err), inlen); + return x->out; + } + if (lenp) + *lenp = xed_decoded_inst_get_length(&inst); + info.p = &inst; + info.buf = x->out; + info.blen = sizeof(x->out); + info.runtime_address = ip; + if (!xed_format_generic(&info)) + strcpy(x->out, "err: cannot format"); + return x->out; +} diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 3840e3a87057..393000501579 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -88,6 +88,7 @@ libperf-y += mem-events.o libperf-y += vsprintf.o libperf-y += drv_configs.o libperf-y += time-utils.o +libperf-y += dis.o libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o diff --git a/tools/perf/util/dis.c b/tools/perf/util/dis.c new file mode 100644 index 000000000000..89c9b84051b8 --- /dev/null +++ b/tools/perf/util/dis.c @@ -0,0 +1,15 @@ +#include "perf.h" +#include "dis.h" +#include "util.h" + +/* Fallback for architectures with no disassembler */ + +__weak char *disas_inst(struct perf_dis *x, uint64_t ip __maybe_unused, + u8 *inbuf __maybe_unused, int inlen __maybe_unused, + int *lenp) +{ + if (lenp) + *lenp = 0; + strcpy(x->out, "?"); + return x->out; +} diff --git a/tools/perf/util/dis.h b/tools/perf/util/dis.h new file mode 100644 index 000000000000..ffda324cbc1a --- /dev/null +++ b/tools/perf/util/dis.h @@ -0,0 +1,20 @@ +#ifndef DIS_H +#define DIS_H 1 + +#define MAXINSN 15 + +struct perf_dis { + /* Initialized by callers: */ + struct thread *thread; + u8 cpumode; + int cpu; + bool is64bit; + /* Temporary */ + char out[256]; +}; + +char *disas_inst(struct perf_dis *x, uint64_t ip, u8 *inbuf, int inlen, + int *lenp); + + +#endif -- 2.9.3