[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v1 34/51] disas: use result of ->read_memory_func
From: |
Alex Bennée |
Subject: |
[PATCH v1 34/51] disas: use result of ->read_memory_func |
Date: |
Thu, 29 Sep 2022 12:42:14 +0100 |
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: <20220921160801.1490125-4-alex.bennee@linaro.org>
---
disas.c | 20 ++++++-------
disas/capstone.c | 73 ++++++++++++++++++++++++++++--------------------
2 files changed, 53 insertions(+), 40 deletions(-)
diff --git a/disas.c b/disas.c
index f07b6e760b..94d3b45042 100644
--- a/disas.c
+++ b/disas.c
@@ -83,18 +83,18 @@ static int print_insn_objdump(bfd_vma pc, disassemble_info
*info,
const char *prefix)
{
int i, n = info->buffer_length;
- uint8_t *buf = g_malloc(n);
-
- info->read_memory_func(pc, buf, n, info);
-
- for (i = 0; i < n; ++i) {
- if (i % 32 == 0) {
- info->fprintf_func(info->stream, "\n%s: ", prefix);
+ g_autofree uint8_t *buf = g_malloc(n);
+
+ if (info->read_memory_func(pc, buf, n, info) == 0) {
+ for (i = 0; i < n; ++i) {
+ if (i % 32 == 0) {
+ 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 {
+ info->fprintf_func(info->stream, "unable to read memory");
}
-
- g_free(buf);
return n;
}
diff --git a/disas/capstone.c b/disas/capstone.c
index 20bc8f9669..fe3efb0d3c 100644
--- a/disas/capstone.c
+++ b/disas/capstone.c
@@ -191,37 +191,43 @@ bool cap_disas_target(disassemble_info *info, uint64_t
pc, size_t size)
size_t tsize = MIN(sizeof(cap_buf) - csize, size);
const uint8_t *cbuf = cap_buf;
- info->read_memory_func(pc + csize, cap_buf + csize, tsize, info);
- csize += tsize;
- size -= tsize;
+ if (info->read_memory_func(pc + csize, cap_buf + csize, tsize, info)
== 0) {
+ csize += tsize;
+ size -= tsize;
- while (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
- cap_dump_insn(info, insn);
- }
+ while (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
+ cap_dump_insn(info, insn);
+ }
+
+ /* If the target memory is not consumed, go back for more... */
+ if (size != 0) {
+ /*
+ * ... taking care to move any remaining fractional insn
+ * to the beginning of the buffer.
+ */
+ if (csize != 0) {
+ memmove(cap_buf, cbuf, csize);
+ }
+ continue;
+ }
- /* If the target memory is not consumed, go back for more... */
- if (size != 0) {
/*
- * ... taking care to move any remaining fractional insn
- * to the beginning of the buffer.
+ * Since the target memory is consumed, we should not have
+ * a remaining fractional insn.
*/
if (csize != 0) {
- memmove(cap_buf, cbuf, csize);
+ info->fprintf_func(info->stream,
+ "Disassembler disagrees with translator "
+ "over instruction decoding\n"
+ "Please report this to
qemu-devel@nongnu.org\n");
}
- continue;
- }
+ break;
- /*
- * Since the target memory is consumed, we should not have
- * a remaining fractional insn.
- */
- if (csize != 0) {
+ } else {
info->fprintf_func(info->stream,
- "Disassembler disagrees with translator "
- "over instruction decoding\n"
- "Please report this to qemu-devel@nongnu.org\n");
+ "0x%08" PRIx64 ": unable to read memory\n", pc);
+ break;
}
- break;
}
cs_close(&handle);
@@ -286,16 +292,23 @@ bool cap_disas_monitor(disassemble_info *info, uint64_t
pc, int count)
/* Make certain that we can make progress. */
assert(tsize != 0);
- info->read_memory_func(pc + csize, cap_buf + csize, tsize, info);
- csize += tsize;
-
- if (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
- cap_dump_insn(info, insn);
- if (--count <= 0) {
- break;
+ if (info->read_memory_func(pc + csize, cap_buf + csize,
+ tsize, info) == 0)
+ {
+ csize += tsize;
+
+ if (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) {
+ cap_dump_insn(info, insn);
+ if (--count <= 0) {
+ break;
+ }
}
+ memmove(cap_buf, cbuf, csize);
+ } else {
+ info->fprintf_func(info->stream,
+ "0x%08" PRIx64 ": unable to read memory\n", pc);
+ break;
}
- memmove(cap_buf, cbuf, csize);
}
cs_close(&handle);
--
2.34.1
- [PATCH v1 12/51] configure: do not invoke as/ld directly for pc-bios/optionrom, (continued)
- [PATCH v1 12/51] configure: do not invoke as/ld directly for pc-bios/optionrom, Alex Bennée, 2022/09/29
- [PATCH v1 04/51] tests/docker: run script use realpath instead of readlink, Alex Bennée, 2022/09/29
- [PATCH v1 16/51] build: add recursive distclean rules, Alex Bennée, 2022/09/29
- [PATCH v1 28/51] configure: build ROMs with container-based cross compilers, Alex Bennée, 2022/09/29
- [PATCH v1 09/51] tests/docker: update fedora-win[32|64]-cross with lcitool, Alex Bennée, 2022/09/29
- [PATCH v1 13/51] pc-bios/optionrom: detect CC options just once, Alex Bennée, 2022/09/29
- [PATCH v1 15/51] vof: add distclean target, Alex Bennée, 2022/09/29
- [PATCH v1 19/51] tests: simplify Makefile invocation for tests/tcg, Alex Bennée, 2022/09/29
- [PATCH v1 36/51] plugins: Assert mmu_idx in range before use in qemu_plugin_get_hwaddr, Alex Bennée, 2022/09/29
- [PATCH v1 38/51] docs/devel: move API to end of tcg-plugins.rst, Alex Bennée, 2022/09/29
- [PATCH v1 34/51] disas: use result of ->read_memory_func,
Alex Bennée <=
- [PATCH v1 43/51] gdbstub: move sstep flags probing into AccelClass, Alex Bennée, 2022/09/29
- [PATCH v1 25/51] configure: move tests/tcg/Makefile.prereqs to root build directory, Alex Bennée, 2022/09/29
- [PATCH v1 27/51] configure: cleanup creation of tests/tcg target config, Alex Bennée, 2022/09/29
- [PATCH v1 24/51] tests/tcg: move compiler tests to Makefiles, Alex Bennée, 2022/09/29
- [PATCH v1 29/51] pc-bios/optionrom: Adopt meson style Make output, Alex Bennée, 2022/09/29
- [PATCH v1 17/51] configure: return status code from probe_target_compiler, Alex Bennée, 2022/09/29
- [PATCH v1 21/51] tests/tcg: add distclean rule, Alex Bennée, 2022/09/29
- [PATCH v1 49/51] contrib/gitdm: add WANG Xuerui to individual contributers, Alex Bennée, 2022/09/29
- [PATCH v1 23/51] tests/tcg: clean up calls to run-test, Alex Bennée, 2022/09/29
- [PATCH v1 47/51] contrib/gitdm: add mapping for Loongson Technology, Alex Bennée, 2022/09/29