From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: aaron@os.amperecomputing.com, robhenry@microsoft.com,
mahmoudabdalghany@outlook.com, minyihh@uci.edu, cota@braap.org,
Luke.Craig@ll.mit.edu, "Alexandre Iooss" <erdnaxe@crans.org>,
kuhn.chenqun@huawei.com, ma.mandourr@gmail.com,
"Alex Bennée" <alex.bennee@linaro.org>
Subject: [PATCH v1 2/2] plugins: extend execlog to filter matches
Date: Mon, 28 Mar 2022 16:26:14 +0100 [thread overview]
Message-ID: <20220328152614.2452259-3-alex.bennee@linaro.org> (raw)
In-Reply-To: <20220328152614.2452259-1-alex.bennee@linaro.org>
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>
Cc: Robert Henry <robhenry@microsoft.com>
Cc: Aaron Lindsay <aaron@os.amperecomputing.com>
---
docs/devel/tcg-plugins.rst | 9 +++-
contrib/plugins/execlog.c | 96 ++++++++++++++++++++++++++++++++------
2 files changed, 90 insertions(+), 15 deletions(-)
diff --git a/docs/devel/tcg-plugins.rst b/docs/devel/tcg-plugins.rst
index a7cc44aa20..a503d44cee 100644
--- a/docs/devel/tcg-plugins.rst
+++ b/docs/devel/tcg-plugins.rst
@@ -346,7 +346,7 @@ The execlog tool traces executed instructions with memory access. It can be used
for debugging and security analysis purposes.
Please be aware that this will generate a lot of output.
-The plugin takes no argument::
+The plugin needs default argument::
qemu-system-arm $(QEMU_ARGS) \
-plugin ./contrib/plugins/libexeclog.so -d plugin
@@ -364,6 +364,13 @@ which will output an execution trace following this structure::
0, 0xd34, 0xf9c8f000, "bl #0x10c8"
0, 0x10c8, 0xfff96c43, "ldr r3, [r0, #0x44]", load, 0x200000e4, RAM
+the output can be filtered to only track certain instructions or
+addresses using the `ifilter` or `afilter` options. You can stack the
+arguments if required::
+
+ qemu-system-arm $(QEMU_ARGS) \
+ -plugin ./contrib/plugins/libexeclog.so,ifilter=st1w,afilter=0x40001808 -d plugin
+
- contrib/plugins/cache.c
Cache modelling plugin that measures the performance of a given L1 cache
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);
--
2.30.2
next prev parent reply other threads:[~2022-03-28 15:28 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-28 15:26 [PATCH v1 0/2] some tests and plugin tweaks for SVE Alex Bennée
2022-03-28 15:26 ` [PATCH v1 1/2] tests/tcg: add memory-sve test for aarch64 Alex Bennée
2022-03-28 15:26 ` Alex Bennée [this message]
2022-04-03 13:14 ` [PATCH v1 2/2] plugins: extend execlog to filter matches Alexandre IOOSS
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220328152614.2452259-3-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=Luke.Craig@ll.mit.edu \
--cc=aaron@os.amperecomputing.com \
--cc=cota@braap.org \
--cc=erdnaxe@crans.org \
--cc=kuhn.chenqun@huawei.com \
--cc=ma.mandourr@gmail.com \
--cc=mahmoudabdalghany@outlook.com \
--cc=minyihh@uci.edu \
--cc=qemu-devel@nongnu.org \
--cc=robhenry@microsoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).