disas: use result of ->read_memory_func

This gets especially confusing if you start plugging in host addresses
from a trace and you wonder why the output keeps changing. Report when
read_memory_func fails instead of blindly disassembling the buffer
contents.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20220929114231.583801-35-alex.bennee@linaro.org>
This commit is contained in:
Alex Bennée 2022-09-29 12:42:14 +01:00
parent 4332099f0e
commit 90bbf9d9db
2 changed files with 51 additions and 38 deletions

10
disas.c
View File

@ -83,18 +83,18 @@ static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
const char *prefix) const char *prefix)
{ {
int i, n = info->buffer_length; int i, n = info->buffer_length;
uint8_t *buf = g_malloc(n); g_autofree uint8_t *buf = g_malloc(n);
info->read_memory_func(pc, buf, n, info);
if (info->read_memory_func(pc, buf, n, info) == 0) {
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
if (i % 32 == 0) { if (i % 32 == 0) {
info->fprintf_func(info->stream, "\n%s: ", prefix); info->fprintf_func(info->stream, "\n%s: ", prefix);
} }
info->fprintf_func(info->stream, "%02x", buf[i]); info->fprintf_func(info->stream, "%02x", buf[i]);
} }
} else {
g_free(buf); info->fprintf_func(info->stream, "unable to read memory");
}
return n; return n;
} }

View File

@ -191,7 +191,7 @@ bool cap_disas_target(disassemble_info *info, uint64_t pc, size_t size)
size_t tsize = MIN(sizeof(cap_buf) - csize, size); size_t tsize = MIN(sizeof(cap_buf) - csize, size);
const uint8_t *cbuf = cap_buf; const uint8_t *cbuf = cap_buf;
info->read_memory_func(pc + csize, cap_buf + csize, tsize, info); if (info->read_memory_func(pc + csize, cap_buf + csize, tsize, info) == 0) {
csize += tsize; csize += tsize;
size -= tsize; size -= tsize;
@ -222,6 +222,12 @@ bool cap_disas_target(disassemble_info *info, uint64_t pc, size_t size)
"Please report this to qemu-devel@nongnu.org\n"); "Please report this to qemu-devel@nongnu.org\n");
} }
break; break;
} else {
info->fprintf_func(info->stream,
"0x%08" PRIx64 ": unable to read memory\n", pc);
break;
}
} }
cs_close(&handle); cs_close(&handle);
@ -286,7 +292,9 @@ bool cap_disas_monitor(disassemble_info *info, uint64_t pc, int count)
/* Make certain that we can make progress. */ /* Make certain that we can make progress. */
assert(tsize != 0); assert(tsize != 0);
info->read_memory_func(pc + csize, cap_buf + csize, tsize, info); if (info->read_memory_func(pc + csize, cap_buf + csize,
tsize, info) == 0)
{
csize += tsize; csize += tsize;
if (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) { if (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
@ -296,6 +304,11 @@ bool cap_disas_monitor(disassemble_info *info, uint64_t pc, int count)
} }
} }
memmove(cap_buf, cbuf, csize); memmove(cap_buf, cbuf, csize);
} else {
info->fprintf_func(info->stream,
"0x%08" PRIx64 ": unable to read memory\n", pc);
break;
}
} }
cs_close(&handle); cs_close(&handle);