[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL v2 55/73] plugin: add qemu_plugin_insn_disas helper
From: |
Alex Bennée |
Subject: |
[PULL v2 55/73] plugin: add qemu_plugin_insn_disas helper |
Date: |
Fri, 25 Oct 2019 07:36:55 +0100 |
Give the plugins access to the QEMU dissasembler so they don't have to
re-invent the wheel. We generate a warning when there are spare bytes
in the decode buffer. This is usually due to the front end loading in
more bytes than decoded.
Signed-off-by: Alex Bennée <address@hidden>
Reviewed-by: Richard Henderson <address@hidden>
diff --git a/disas.c b/disas.c
index 3e2bfa572b1..3937da61571 100644
--- a/disas.c
+++ b/disas.c
@@ -418,6 +418,7 @@ static bool cap_disas_monitor(disassemble_info *info,
uint64_t pc, int count)
# define cap_disas_target(i, p, s) false
# define cap_disas_host(i, p, s) false
# define cap_disas_monitor(i, p, c) false
+# define cap_disas_plugin(i, p, c) false
#endif /* CONFIG_CAPSTONE */
/* Disassemble this for me please... (debugging). */
@@ -475,6 +476,115 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong
code,
}
}
+static __thread GString plugin_disas_output;
+
+static int plugin_printf(FILE *stream, const char *fmt, ...)
+{
+ va_list va;
+ GString *s = &plugin_disas_output;
+ int initial_len = s->len;
+
+ va_start(va, fmt);
+ g_string_append_vprintf(s, fmt, va);
+ va_end(va);
+
+ return s->len - initial_len;
+}
+
+static void plugin_print_address(bfd_vma addr, struct disassemble_info *info)
+{
+ /* does nothing */
+}
+
+
+#ifdef CONFIG_CAPSTONE
+/* Disassemble a single instruction directly into plugin output */
+static
+bool cap_disas_plugin(disassemble_info *info, uint64_t pc, size_t size)
+{
+ uint8_t cap_buf[1024];
+ csh handle;
+ cs_insn *insn;
+ size_t csize = 0;
+ int count;
+ GString *s = &plugin_disas_output;
+
+ if (cap_disas_start(info, &handle) != CS_ERR_OK) {
+ return false;
+ }
+ insn = cap_insn;
+
+ size_t tsize = MIN(sizeof(cap_buf) - csize, size);
+ const uint8_t *cbuf = cap_buf;
+ target_read_memory(pc, cap_buf, tsize, info);
+
+ count = cs_disasm(handle, cbuf, size, 0, 1, &insn);
+
+ if (count) {
+ g_string_printf(s, "%s %s", insn->mnemonic, insn->op_str);
+ } else {
+ g_string_printf(s, "cs_disasm failed");
+ }
+
+ cs_close(&handle);
+ return true;
+}
+#endif
+
+/*
+ * We should only be dissembling one instruction at a time here. If
+ * there is left over it usually indicates the front end has read more
+ * bytes than it needed.
+ */
+char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size)
+{
+ CPUClass *cc = CPU_GET_CLASS(cpu);
+ int count;
+ CPUDebug s;
+ GString *ds = g_string_set_size(&plugin_disas_output, 0);
+
+ g_assert(ds == &plugin_disas_output);
+
+ INIT_DISASSEMBLE_INFO(s.info, NULL, plugin_printf);
+
+ s.cpu = cpu;
+ s.info.read_memory_func = target_read_memory;
+ s.info.buffer_vma = addr;
+ s.info.buffer_length = size;
+ s.info.print_address_func = plugin_print_address;
+ s.info.cap_arch = -1;
+ s.info.cap_mode = 0;
+ s.info.cap_insn_unit = 4;
+ s.info.cap_insn_split = 4;
+
+#ifdef TARGET_WORDS_BIGENDIAN
+ s.info.endian = BFD_ENDIAN_BIG;
+#else
+ s.info.endian = BFD_ENDIAN_LITTLE;
+#endif
+
+ if (cc->disas_set_info) {
+ cc->disas_set_info(cpu, &s.info);
+ }
+
+ if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) {
+ return g_strdup(ds->str);
+ }
+
+ if (s.info.print_insn == NULL) {
+ s.info.print_insn = print_insn_od_target;
+ }
+
+ count = s.info.print_insn(addr, &s.info);
+
+ /* The decoder probably read more than it needed it's not critical */
+ if (count < size) {
+ warn_report("%s: %zu bytes left over", __func__, size - count);
+ }
+
+ return g_strdup(ds->str);
+}
+
/* Disassemble this for me please... (debugging). */
void disas(FILE *out, void *code, unsigned long size)
{
diff --git a/include/disas/disas.h b/include/disas/disas.h
index ba47e9197cd..36c33f6f194 100644
--- a/include/disas/disas.h
+++ b/include/disas/disas.h
@@ -14,6 +14,8 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code,
void monitor_disas(Monitor *mon, CPUState *cpu,
target_ulong pc, int nb_insn, int is_physical);
+char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size);
+
/* Look up symbol for debugging purpose. Returns "" if unknown. */
const char *lookup_symbol(target_ulong orig_addr);
#endif
diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index 784f1dfc3da..ddf267fbfe0 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -351,6 +351,15 @@ qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t
id,
qemu_plugin_vcpu_syscall_ret_cb_t cb);
+/**
+ * qemu_plugin_insn_disas() - return disassembly string for instruction
+ * @insn: instruction reference
+ *
+ * Returns an allocated string containing the disassembly
+ */
+
+char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn);
+
/**
* qemu_plugin_vcpu_for_each() - iterate over the existing vCPU
* @id: plugin ID
diff --git a/plugins/api.c b/plugins/api.c
index 33dac8e790d..5adc4d25a1e 100644
--- a/plugins/api.c
+++ b/plugins/api.c
@@ -39,7 +39,8 @@
#include "cpu.h"
#include "sysemu/sysemu.h"
#include "tcg/tcg.h"
-#include "trace/mem-internal.h" /* mem_info macros */
+#include "exec/exec-all.h"
+#include "disas/disas.h"
#include "plugin.h"
#ifndef CONFIG_USER_ONLY
#include "qemu/plugin-memory.h"
@@ -212,6 +213,12 @@ void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn
*insn)
return insn->haddr;
}
+char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
+{
+ CPUState *cpu = current_cpu;
+ return plugin_disas(cpu, insn->vaddr, insn->data->len);
+}
+
/*
* The memory queries allow the plugin to query information about a
* memory access.
diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols
index 40c0d1abd2f..267ec381b4a 100644
--- a/plugins/qemu-plugins.symbols
+++ b/plugins/qemu-plugins.symbols
@@ -25,6 +25,7 @@
qemu_plugin_insn_size;
qemu_plugin_insn_vaddr;
qemu_plugin_insn_haddr;
+ qemu_plugin_insn_disas;
qemu_plugin_mem_size_shift;
qemu_plugin_mem_is_sign_extended;
qemu_plugin_mem_is_big_endian;
--
2.20.1
- [PULL v2 13/73] gitlab-ci.yml: Use libvdeplug-dev to compile-test the VDE network backend, (continued)
- [PULL v2 13/73] gitlab-ci.yml: Use libvdeplug-dev to compile-test the VDE network backend, Alex Bennée, 2019/10/25
- [PULL v2 19/73] cpu: introduce cpu_in_exclusive_context(), Alex Bennée, 2019/10/25
- [PULL v2 56/73] plugin: add qemu_plugin_outs helper, Alex Bennée, 2019/10/25
- [PULL v2 72/73] MAINTAINERS: add me for the TCG plugins code, Alex Bennée, 2019/10/25
- [PULL v2 59/73] tests/plugin: add sample plugins, Alex Bennée, 2019/10/25
- [PULL v2 22/73] plugin: add user-facing API, Alex Bennée, 2019/10/25
- [PULL v2 12/73] travis.yml: cache the clang sanitizer build, Alex Bennée, 2019/10/25
- [PULL v2 21/73] docs/devel: add plugins.rst design document, Alex Bennée, 2019/10/25
- [PULL v2 65/73] tests/plugin: add a hotblocks plugin, Alex Bennée, 2019/10/25
- [PULL v2 24/73] plugin: add implementation of the api, Alex Bennée, 2019/10/25
- [PULL v2 55/73] plugin: add qemu_plugin_insn_disas helper,
Alex Bennée <=
- [PULL v2 40/73] target/arm: fetch code with translator_ld, Alex Bennée, 2019/10/25
- [PULL v2 52/73] configure: add --enable-plugins, Alex Bennée, 2019/10/25
- [PULL v2 28/73] tcg: add tcg_gen_st_ptr, Alex Bennée, 2019/10/25
- [PULL v2 71/73] scripts/checkpatch.pl: don't complain about (foo, /* empty */), Alex Bennée, 2019/10/25
- [PULL v2 36/73] cpu: hook plugin vcpu events, Alex Bennée, 2019/10/25
- [PULL v2 58/73] linux-user: support -plugin option, Alex Bennée, 2019/10/25
- [PULL v2 38/73] cputlb: ensure _cmmu helper functions follow the naming standard, Alex Bennée, 2019/10/25
- [PULL v2 18/73] trace: add mmu_index to mem_info, Alex Bennée, 2019/10/25
- [PULL v2 20/73] translate-all: use cpu_in_exclusive_work_context() in tb_flush, Alex Bennée, 2019/10/25
- [PULL v2 30/73] atomic_template: add inline trace/plugin helpers, Alex Bennée, 2019/10/25