summaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorAlex Bennée2022-09-29 13:42:15 +0200
committerAlex Bennée2022-10-06 12:53:40 +0200
commitb7855bf65f5bed868b06d6492a6d8a174f6cb71b (patch)
tree7e65b426592f7e7201a0bed2a26f1cb9961b81ed /contrib
parentdisas: use result of ->read_memory_func (diff)
downloadqemu-b7855bf65f5bed868b06d6492a6d8a174f6cb71b.tar.gz
qemu-b7855bf65f5bed868b06d6492a6d8a174f6cb71b.tar.xz
qemu-b7855bf65f5bed868b06d6492a6d8a174f6cb71b.zip
plugins: extend execlog to filter matches
Sometimes the whole execlog is just two much so add the ability to filter by instruction opcode or address. [AJB: this shows for example qemu-system-aarch64 -display none -serial mon:stdio \ -M virt -cpu max \ -semihosting-config enable=on \ -kernel ./tests/tcg/aarch64-softmmu/memory-sve \ -plugin ./contrib/plugins/libexeclog.so,ifilter=st1w,afilter=0x40001808 -d plugin -D plugin.out the st1w SVE instruction is not instrumenting its stores.] Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Alexandre Iooss <erdnaxe@crans.org> Cc: Robert Henry <robhenry@microsoft.com> Cc: Aaron Lindsay <aaron@os.amperecomputing.com> Message-Id: <20220929114231.583801-36-alex.bennee@linaro.org>
Diffstat (limited to 'contrib')
-rw-r--r--contrib/plugins/execlog.c96
1 files changed, 82 insertions, 14 deletions
diff --git a/contrib/plugins/execlog.c b/contrib/plugins/execlog.c
index a5275dcc15..e659ac9cbb 100644
--- a/contrib/plugins/execlog.c
+++ b/contrib/plugins/execlog.c
@@ -20,6 +20,9 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
/* Store last executed instruction on each vCPU as a GString */
GArray *last_exec;
+static GPtrArray *imatches;
+static GArray *amatches;
+
/**
* Add memory read or write information to current instruction log
*/
@@ -85,12 +88,13 @@ static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
{
struct qemu_plugin_insn *insn;
- uint64_t insn_vaddr;
- uint32_t insn_opcode;
- char *insn_disas;
+ bool skip = (imatches || amatches) ? true : false;
size_t n = qemu_plugin_tb_n_insns(tb);
for (size_t i = 0; i < n; i++) {
+ char *insn_disas;
+ uint64_t insn_vaddr;
+
/*
* `insn` is shared between translations in QEMU, copy needed data here.
* `output` is never freed as it might be used multiple times during
@@ -99,20 +103,52 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
* a limitation for CISC architectures.
*/
insn = qemu_plugin_tb_get_insn(tb, i);
- insn_vaddr = qemu_plugin_insn_vaddr(insn);
- insn_opcode = *((uint32_t *)qemu_plugin_insn_data(insn));
insn_disas = qemu_plugin_insn_disas(insn);
- char *output = g_strdup_printf("0x%"PRIx64", 0x%"PRIx32", \"%s\"",
- insn_vaddr, insn_opcode, insn_disas);
+ insn_vaddr = qemu_plugin_insn_vaddr(insn);
+
+ /*
+ * If we are filtering we better check out if we have any
+ * hits. The skip "latches" so we can track memory accesses
+ * after the instruction we care about.
+ */
+ if (skip && imatches) {
+ int j;
+ for (j = 0; j < imatches->len && skip; j++) {
+ char *m = g_ptr_array_index(imatches, j);
+ if (g_str_has_prefix(insn_disas, m)) {
+ skip = false;
+ }
+ }
+ }
+
+ if (skip && amatches) {
+ int j;
+ for (j = 0; j < amatches->len && skip; j++) {
+ uint64_t v = g_array_index(amatches, uint64_t, j);
+ if (v == insn_vaddr) {
+ skip = false;
+ }
+ }
+ }
- /* Register callback on memory read or write */
- qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
- QEMU_PLUGIN_CB_NO_REGS,
- QEMU_PLUGIN_MEM_RW, NULL);
+ if (skip) {
+ g_free(insn_disas);
+ } else {
+ uint32_t insn_opcode;
+ insn_opcode = *((uint32_t *)qemu_plugin_insn_data(insn));
+ char *output = g_strdup_printf("0x%"PRIx64", 0x%"PRIx32", \"%s\"",
+ insn_vaddr, insn_opcode, insn_disas);
+
+ /* Register callback on memory read or write */
+ qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
+ QEMU_PLUGIN_CB_NO_REGS,
+ QEMU_PLUGIN_MEM_RW, NULL);
+
+ /* Register callback on instruction */
+ qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec,
+ QEMU_PLUGIN_CB_NO_REGS, output);
+ }
- /* Register callback on instruction */
- qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec,
- QEMU_PLUGIN_CB_NO_REGS, output);
}
}
@@ -132,6 +168,25 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
}
}
+/* Add a match to the array of matches */
+static void parse_insn_match(char *match)
+{
+ if (!imatches) {
+ imatches = g_ptr_array_new();
+ }
+ g_ptr_array_add(imatches, match);
+}
+
+static void parse_vaddr_match(char *match)
+{
+ uint64_t v = g_ascii_strtoull(match, NULL, 16);
+
+ if (!amatches) {
+ amatches = g_array_new(false, true, sizeof(uint64_t));
+ }
+ g_array_append_val(amatches, v);
+}
+
/**
* Install the plugin
*/
@@ -145,6 +200,19 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
*/
last_exec = g_array_new(FALSE, FALSE, sizeof(GString *));
+ for (int i = 0; i < argc; i++) {
+ char *opt = argv[i];
+ g_autofree char **tokens = g_strsplit(opt, "=", 2);
+ if (g_strcmp0(tokens[0], "ifilter") == 0) {
+ parse_insn_match(tokens[1]);
+ } else if (g_strcmp0(tokens[0], "afilter") == 0) {
+ parse_vaddr_match(tokens[1]);
+ } else {
+ fprintf(stderr, "option parsing failed: %s\n", opt);
+ return -1;
+ }
+ }
+
/* Register translation block and exit callbacks */
qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);