[RFC PATCH -tip 04/16] x86: Show kernel symbol in disassembler

From: Masami Hiramatsu
Date: Sun Apr 01 2012 - 12:03:10 EST


Show kernel symbol if the immediate value of disassembling
instruction is the destination of jump or call.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu@xxxxxxxxx>
---
arch/x86/lib/disasm.c | 25 ++++++++++++++++++++++++-
1 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/arch/x86/lib/disasm.c b/arch/x86/lib/disasm.c
index 473ae52..4abe844 100644
--- a/arch/x86/lib/disasm.c
+++ b/arch/x86/lib/disasm.c
@@ -7,6 +7,8 @@
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/errno.h>
+#include <linux/kallsyms.h>
+
#include <asm/disasm.h>

#define X86_LEA_OPCODE 0x8d
@@ -28,6 +30,27 @@ static int psnprintf(char **buf, size_t *len, const char *fmt, ...)
return ret;
}

+/* Print address with symbol */
+static int psnprint_symbol(char **buf, size_t *len, unsigned long addr)
+{
+ unsigned long offs;
+ char func[KSYM_NAME_LEN];
+ char *modname;
+ int ret;
+
+ ret = psnprintf(buf, len, "%lx", addr);
+ if (!kallsyms_lookup(addr, NULL, &offs, &modname, func))
+ return ret;
+
+ psnprintf(buf, len, " <%s", func);
+ if (offs)
+ psnprintf(buf, len, "+0x%lx", offs);
+ if (modname)
+ psnprintf(buf, len, " [%s]", modname);
+
+ return psnprintf(buf, len, ">");
+}
+
/* Operand classifiers */
static bool operand_is_register(const char *p)
{
@@ -385,7 +408,7 @@ static int disasm_immediate(char **buf, size_t *len, const char *opnd,
if (opnd[0] == 'J' || opnd[0] == 'A') {
if (opnd[0] == 'J') /* Relative from IP */
imm += (long)insn->kaddr + insn->length;
- return psnprintf(buf, len, "%lx", (unsigned long)imm);
+ return psnprint_symbol(buf, len, (unsigned long)imm);
}

size = insn->opnd_bytes;

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/