disas: add optional note support to cap_disas

Include support for outputting a note at the top of a chunk of
disassembly to capstone as well.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Message-Id: <20200513175134.19619-10-alex.bennee@linaro.org>
This commit is contained in:
Alex Bennée 2020-05-13 18:51:33 +01:00
parent e5ef4ec28b
commit 16b22e02b5

23
disas.c
View File

@ -260,7 +260,8 @@ static void cap_dump_insn_units(disassemble_info *info, cs_insn *insn,
}
}
static void cap_dump_insn(disassemble_info *info, cs_insn *insn)
static void cap_dump_insn(disassemble_info *info, cs_insn *insn,
const char *note)
{
fprintf_function print = info->fprintf_func;
int i, n, split;
@ -281,7 +282,11 @@ static void cap_dump_insn(disassemble_info *info, cs_insn *insn)
}
/* Print the actual instruction. */
print(info->stream, " %-8s %s\n", insn->mnemonic, insn->op_str);
print(info->stream, " %-8s %s", insn->mnemonic, insn->op_str);
if (note) {
print(info->stream, "\t\t%s", note);
}
print(info->stream, "\n");
/* Dump any remaining part of the insn on subsequent lines. */
for (i = split; i < n; i += split) {
@ -313,7 +318,7 @@ static bool cap_disas_target(disassemble_info *info, uint64_t pc, size_t size)
size -= tsize;
while (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
cap_dump_insn(info, insn);
cap_dump_insn(info, insn, NULL);
}
/* If the target memory is not consumed, go back for more... */
@ -342,7 +347,8 @@ static bool cap_disas_target(disassemble_info *info, uint64_t pc, size_t size)
}
/* Disassemble SIZE bytes at CODE for the host. */
static bool cap_disas_host(disassemble_info *info, void *code, size_t size)
static bool cap_disas_host(disassemble_info *info, void *code, size_t size,
const char *note)
{
csh handle;
const uint8_t *cbuf;
@ -358,7 +364,8 @@ static bool cap_disas_host(disassemble_info *info, void *code, size_t size)
pc = (uintptr_t)code;
while (cs_disasm_iter(handle, &cbuf, &size, &pc, insn)) {
cap_dump_insn(info, insn);
cap_dump_insn(info, insn, note);
note = NULL;
}
if (size != 0) {
(*info->fprintf_func)(info->stream,
@ -402,7 +409,7 @@ static bool cap_disas_monitor(disassemble_info *info, uint64_t pc, int count)
csize += tsize;
if (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
cap_dump_insn(info, insn);
cap_dump_insn(info, insn, NULL);
if (--count <= 0) {
break;
}
@ -416,7 +423,7 @@ static bool cap_disas_monitor(disassemble_info *info, uint64_t pc, int count)
#endif /* !CONFIG_USER_ONLY */
#else
# define cap_disas_target(i, p, s) false
# define cap_disas_host(i, p, s) false
# define cap_disas_host(i, p, s, n) false
# define cap_disas_monitor(i, p, c) false
# define cap_disas_plugin(i, p, c) false
#endif /* CONFIG_CAPSTONE */
@ -664,7 +671,7 @@ void disas(FILE *out, void *code, unsigned long size, const char *note)
print_insn = print_insn_hppa;
#endif
if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) {
if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size, note)) {
return;
}