@@ -98,11 +98,11 @@ struct qemu_plugin_dyn_cb {
/* Internal context for instrumenting an instruction */
struct qemu_plugin_insn {
- GByteArray *data;
uint64_t vaddr;
void *haddr;
GArray *insn_cbs;
GArray *mem_cbs;
+ uint8_t len;
bool calls_helpers;
/* if set, the instruction calls helpers that might access guest memory */
@@ -117,18 +117,6 @@ struct qemu_plugin_scoreboard {
QLIST_ENTRY(qemu_plugin_scoreboard) entry;
};
-/*
- * qemu_plugin_insn allocate and cleanup functions. We don't expect to
- * cleanup many of these structures. They are reused for each fresh
- * translation.
- */
-
-static inline void qemu_plugin_insn_cleanup_fn(gpointer data)
-{
- struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data;
- g_byte_array_free(insn->data, true);
-}
-
/* Internal context for this TranslationBlock */
struct qemu_plugin_tb {
GPtrArray *insns;
@@ -346,11 +346,9 @@ void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
ptb->n = n;
if (n <= ptb->insns->len) {
insn = g_ptr_array_index(ptb->insns, n - 1);
- g_byte_array_set_size(insn->data, 0);
} else {
assert(n - 1 == ptb->insns->len);
insn = g_new0(struct qemu_plugin_insn, 1);
- insn->data = g_byte_array_sized_new(4);
g_ptr_array_add(ptb->insns, insn);
}
@@ -389,6 +387,11 @@ void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
void plugin_gen_insn_end(void)
{
+ const DisasContextBase *db = tcg_ctx->plugin_db;
+ struct qemu_plugin_insn *pinsn = tcg_ctx->plugin_insn;
+
+ pinsn->len = db->fake_insn ? db->record_len : db->pc_next - pinsn->vaddr;
+
tcg_gen_plugin_cb(PLUGIN_GEN_AFTER_INSN);
}
@@ -409,27 +409,6 @@ bool translator_st(const DisasContextBase *db, void *dest,
return false;
}
-static void plugin_insn_append(vaddr pc, const void *from, size_t size)
-{
-#ifdef CONFIG_PLUGIN
- struct qemu_plugin_insn *insn = tcg_ctx->plugin_insn;
- size_t off;
-
- if (insn == NULL) {
- return;
- }
- off = pc - insn->vaddr;
- if (off < insn->data->len) {
- g_byte_array_set_size(insn->data, off);
- } else if (off > insn->data->len) {
- /* we have an unexpected gap */
- g_assert_not_reached();
- }
-
- insn->data = g_byte_array_append(insn->data, from, size);
-#endif
-}
-
uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, vaddr pc)
{
uint8_t raw;
@@ -438,7 +417,6 @@ uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, vaddr pc)
raw = cpu_ldub_code(env, pc);
record_save(db, pc, &raw, sizeof(raw));
}
- plugin_insn_append(pc, &raw, sizeof(raw));
return raw;
}
@@ -453,7 +431,6 @@ uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, vaddr pc)
raw = tswap16(tgt);
record_save(db, pc, &raw, sizeof(raw));
}
- plugin_insn_append(pc, &raw, sizeof(raw));
return tgt;
}
@@ -468,7 +445,6 @@ uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, vaddr pc)
raw = tswap32(tgt);
record_save(db, pc, &raw, sizeof(raw));
}
- plugin_insn_append(pc, &raw, sizeof(raw));
return tgt;
}
@@ -483,7 +459,6 @@ uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, vaddr pc)
raw = tswap64(tgt);
record_save(db, pc, &raw, sizeof(raw));
}
- plugin_insn_append(pc, &raw, sizeof(raw));
return tgt;
}
@@ -492,5 +467,4 @@ void translator_fake_ldb(DisasContextBase *db, vaddr pc, uint8_t insn8)
assert(pc >= db->pc_first);
db->fake_insn = true;
record_save(db, pc, &insn8, sizeof(insn8));
- plugin_insn_append(pc, &insn8, sizeof(insn8));
}
@@ -42,6 +42,7 @@
#include "tcg/tcg.h"
#include "exec/exec-all.h"
#include "exec/gdbstub.h"
+#include "exec/translator.h"
#include "disas/disas.h"
#include "plugin.h"
#ifndef CONFIG_USER_ONLY
@@ -219,14 +220,15 @@ qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn,
void *dest, size_t len)
{
- len = MIN(len, insn->data->len);
- memcpy(dest, insn->data->data, len);
- return len;
+ const DisasContextBase *db = tcg_ctx->plugin_db;
+
+ len = MIN(len, insn->len);
+ return translator_st(db, dest, insn->vaddr, len) ? len : 0;
}
size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
{
- return insn->data->len;
+ return insn->len;
}
uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
@@ -242,7 +244,7 @@ void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
{
CPUState *cpu = current_cpu;
- return plugin_disas(cpu, insn->vaddr, insn->data->len);
+ return plugin_disas(cpu, insn->vaddr, insn->len);
}
const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
@@ -765,8 +765,7 @@ static void alloc_tcg_plugin_context(TCGContext *s)
{
#ifdef CONFIG_PLUGIN
s->plugin_tb = g_new0(struct qemu_plugin_tb, 1);
- s->plugin_tb->insns =
- g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn);
+ s->plugin_tb->insns = g_ptr_array_new();
#endif
}