Skip to content

Commit

Permalink
disas: add optional note support to cap_disas
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
stsquad committed May 15, 2020
1 parent e5ef4ec commit 16b22e0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions disas.c
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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... */
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 16b22e0

Please sign in to comment.