qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] plugins: add tb convenience functions
@ 2025-01-31 21:07 Luke Craig
  2025-01-31 21:07 ` [PATCH v3 1/3] plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr Luke Craig
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Luke Craig @ 2025-01-31 21:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Alex Bennée, Alexandre Iooss, Pierrick Bouvier,
	Mahmoud Mandour, Luke Craig

This PR extends the plugin API with two functions which allow convenient access
around tbs.

The first, qemu_plugin_tb_size, provides a mechanism for determining the total
size of a translation block.

The second, qemu_plugin_tb_get_insn_by_vaddr, allows users to get a reference
to an instruction by its virtual address rather than just its index.

In response to feedback from Pierrick I have updated the implementation of
qemu_plugin_tb_size.

Additionally, I have added these functions to the insn.c test plugin in
response to Alex's feedback.

Lastly, I'll provide a reply to Alex's feeback (repeated below):

> But the general comment is this is an example of tying the plugin API
> too deeply with the internals of the translator. Why does a plugin need
> to know what is an implementation detail?

Finding the line between implementation detail and relevant to plugins is
challenging, but I submitted this change because I found myself implementing
these functions in plugins. If you'd like for me to enumerate examples where
knowing the tb_size is relevant to analysis I'd be happy to submit some.

The change relevant from v2 and v3 is adding a sign off on the commits (thank you Perrick).

Luke Craig (3):
  plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr
  plugin: extend API with qemu_plugin_tb_size
  plugins: extend insn test for new convenience functions

 include/qemu/qemu-plugin.h | 21 +++++++++++++++++++++
 plugins/api.c              | 20 ++++++++++++++++++++
 tests/tcg/plugins/insn.c   | 10 ++++++++++
 3 files changed, 51 insertions(+)

-- 
2.34.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/3] plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr
  2025-01-31 21:07 [PATCH v3 0/3] plugins: add tb convenience functions Luke Craig
@ 2025-01-31 21:07 ` Luke Craig
  2025-01-31 21:07 ` [PATCH v3 2/3] plugin: extend API with qemu_plugin_tb_size Luke Craig
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Luke Craig @ 2025-01-31 21:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Alex Bennée, Alexandre Iooss, Pierrick Bouvier,
	Mahmoud Mandour, Luke Craig

Signed-off-by: Luke Craig <lacraig3@gmail.com>
---
 include/qemu/qemu-plugin.h | 11 +++++++++++
 plugins/api.c              | 13 +++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index 3a850aa216..a1c478c54f 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -500,6 +500,17 @@ QEMU_PLUGIN_API
 struct qemu_plugin_insn *
 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx);
 
+/**
+ * qemu_plugin_tb_get_insn_by_vaddr() - lookup handle for instruction by vaddr
+ * @tb: opaque handle to TB passed to callback
+ * @vaddr: virtual address of instruction
+ *
+ * Returns: opaque handle to instruction
+ */
+QEMU_PLUGIN_API
+struct qemu_plugin_insn *
+qemu_plugin_tb_get_insn_by_vaddr(const struct qemu_plugin_tb *tb, uint64_t vaddr);
+
 /**
  * qemu_plugin_insn_data() - copy instruction data
  * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
diff --git a/plugins/api.c b/plugins/api.c
index 4110cfaa23..a6bd912c56 100644
--- a/plugins/api.c
+++ b/plugins/api.c
@@ -258,6 +258,19 @@ qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
     return insn;
 }
 
+struct qemu_plugin_insn *
+qemu_plugin_tb_get_insn_by_vaddr(const struct qemu_plugin_tb *tb, uint64_t vaddr)
+{
+    struct qemu_plugin_insn *insn;
+    for (size_t i = 0; i < tb->n; i++){
+        insn = g_ptr_array_index(tb->insns, i);
+        if (insn != NULL && insn->vaddr == vaddr){
+            return insn;
+        }
+    }
+    return NULL;
+}
+
 /*
  * Instruction information
  *
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/3] plugin: extend API with qemu_plugin_tb_size
  2025-01-31 21:07 [PATCH v3 0/3] plugins: add tb convenience functions Luke Craig
  2025-01-31 21:07 ` [PATCH v3 1/3] plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr Luke Craig
@ 2025-01-31 21:07 ` Luke Craig
  2025-01-31 21:07 ` [PATCH v3 3/3] plugins: extend insn test for new convenience functions Luke Craig
  2025-02-01  7:24 ` [PATCH v3 0/3] plugins: add tb " Philippe Mathieu-Daudé
  3 siblings, 0 replies; 5+ messages in thread
From: Luke Craig @ 2025-01-31 21:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Alex Bennée, Alexandre Iooss, Pierrick Bouvier,
	Mahmoud Mandour, Luke Craig

Signed-off-by: Luke Craig <lacraig3@gmail.com>
---
 include/qemu/qemu-plugin.h | 10 ++++++++++
 plugins/api.c              |  7 +++++++
 2 files changed, 17 insertions(+)

diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index a1c478c54f..1fa656da82 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -476,6 +476,16 @@ void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
 QEMU_PLUGIN_API
 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb);
 
+/**
+ * qemu_plugin_tb_size() - query helper for size of TB
+ * @tb: opaque handle to TB passed to callback
+ * 
+ * Returns: size of block in bytes
+ */
+
+QEMU_PLUGIN_API
+size_t qemu_plugin_tb_size(const struct qemu_plugin_tb *tb);
+
 /**
  * qemu_plugin_tb_vaddr() - query helper for vaddr of TB start
  * @tb: opaque handle to TB passed to callback
diff --git a/plugins/api.c b/plugins/api.c
index a6bd912c56..ae74668c2e 100644
--- a/plugins/api.c
+++ b/plugins/api.c
@@ -241,6 +241,13 @@ size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
     return tb->n;
 }
 
+size_t qemu_plugin_tb_size(const struct qemu_plugin_tb *tb)
+{
+    struct qemu_plugin_insn *last;
+    last = g_ptr_array_index(tb->insns, tb->n - 1);
+    return qemu_plugin_insn_vaddr(last) + qemu_plugin_insn_size(last) - qemu_plugin_tb_vaddr(tb);
+}
+
 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
 {
     const DisasContextBase *db = tcg_ctx->plugin_db;
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 3/3] plugins: extend insn test for new convenience functions
  2025-01-31 21:07 [PATCH v3 0/3] plugins: add tb convenience functions Luke Craig
  2025-01-31 21:07 ` [PATCH v3 1/3] plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr Luke Craig
  2025-01-31 21:07 ` [PATCH v3 2/3] plugin: extend API with qemu_plugin_tb_size Luke Craig
@ 2025-01-31 21:07 ` Luke Craig
  2025-02-01  7:24 ` [PATCH v3 0/3] plugins: add tb " Philippe Mathieu-Daudé
  3 siblings, 0 replies; 5+ messages in thread
From: Luke Craig @ 2025-01-31 21:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Alex Bennée, Alexandre Iooss, Pierrick Bouvier,
	Mahmoud Mandour, Luke Craig, Luke Craig

From: Luke Craig <luke.craig@mit.edu>

Signed-off-by: Luke Craig <lacraig3@gmail.com>
---
 tests/tcg/plugins/insn.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tests/tcg/plugins/insn.c b/tests/tcg/plugins/insn.c
index 0c723cb9ed..5974e9d6e6 100644
--- a/tests/tcg/plugins/insn.c
+++ b/tests/tcg/plugins/insn.c
@@ -142,6 +142,8 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
 {
     size_t n = qemu_plugin_tb_n_insns(tb);
     size_t i;
+    size_t tb_size = 0;
+    struct qemu_plugin_insn *last;
 
     for (i = 0; i < n; i++) {
         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
@@ -156,6 +158,7 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
 
         if (do_size) {
             size_t sz = qemu_plugin_insn_size(insn);
+            tb_size += sz;
             if (sz > sizes->len) {
                 g_array_set_size(sizes, sz);
             }
@@ -188,6 +191,13 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
             g_free(insn_disas);
         }
     }
+
+    last = qemu_plugin_tb_get_insn(tb, n - 1);
+    g_assert(qemu_plugin_tb_get_insn_by_vaddr(tb, qemu_plugin_insn_vaddr(last)) == last);
+
+    if (do_size){
+        g_assert(tb_size == qemu_plugin_tb_size(tb));
+    }
 }
 
 static void plugin_exit(qemu_plugin_id_t id, void *p)
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/3] plugins: add tb convenience functions
  2025-01-31 21:07 [PATCH v3 0/3] plugins: add tb convenience functions Luke Craig
                   ` (2 preceding siblings ...)
  2025-01-31 21:07 ` [PATCH v3 3/3] plugins: extend insn test for new convenience functions Luke Craig
@ 2025-02-01  7:24 ` Philippe Mathieu-Daudé
  3 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-02-01  7:24 UTC (permalink / raw)
  To: Luke Craig, qemu-devel
  Cc: Alex Bennée, Alexandre Iooss, Pierrick Bouvier,
	Mahmoud Mandour

On 31/1/25 22:07, Luke Craig wrote:
> This PR extends the plugin API with two functions which allow convenient access
> around tbs.


> Luke Craig (3):
>    plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr
>    plugin: extend API with qemu_plugin_tb_size
>    plugins: extend insn test for new convenience functions

Per v2, series:
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-02-01  7:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-31 21:07 [PATCH v3 0/3] plugins: add tb convenience functions Luke Craig
2025-01-31 21:07 ` [PATCH v3 1/3] plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr Luke Craig
2025-01-31 21:07 ` [PATCH v3 2/3] plugin: extend API with qemu_plugin_tb_size Luke Craig
2025-01-31 21:07 ` [PATCH v3 3/3] plugins: extend insn test for new convenience functions Luke Craig
2025-02-01  7:24 ` [PATCH v3 0/3] plugins: add tb " Philippe Mathieu-Daudé

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).