Skip to contentSkip to conversation

plugins: add tb convenience functions

PATCH Superseded Luke Craig 3 participants 6 messages Jan 27, 2025 – Jan 28, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Luke Craig

author
#0 lore raw

cover plugins: add tb convenience functions

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.

Luke Craig (2):
plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr
plugin: extend API with qemu_plugin_tb_size

 include/qemu/qemu-plugin.h | 21 +++++++++++++++++++++
 plugins/api.c              | 18 ++++++++++++++++++
 2 files changed, 39 insertions(+)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr

---
 include/qemu/qemu-plugin.h | 11 +++++++++++
 plugins/api.c              | 13 +++++++++++++
 2 files changed, 24 insertions(+)
include/qemu/qemu-plugin.h +11 -0
index 3a850aa216..a1c478c54f 100644
@@ -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()
plugins/api.c +13 -0
index 4110cfaa23..7ff5e1c1bd 100644
@@ -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, idx);
+        if (insn != NULL && insn->vaddr == vaddr){
+            return insn;
+        }
+    }
+    return NULL;
+}
+
 /*
  * Instruction information
  *
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] plugin: extend API with qemu_plugin_tb_get_insn_by_vaddr

Quoted 38 lines · Luke Craig
---
 include/qemu/qemu-plugin.h | 11 +++++++++++
 plugins/api.c              | 13 +++++++++++++
 2 files changed, 24 insertions(+)
include/qemu/qemu-plugin.h +11 -0
index 3a850aa216..a1c478c54f 100644
@@ -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()
plugins/api.c +3 -0
index 4110cfaa23..7ff5e1c1bd 100644
@@ -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)
+{

plugins/api.c qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx) · line 273

Quoted 10 lines
plugins/api.c +10 -0
+    struct qemu_plugin_insn *insn;
+    for (size_t i = 0; i < tb->n; i++){
+        insn = g_ptr_array_index(tb->insns, idx);
+        if (insn != NULL && insn->vaddr == vaddr){
+            return insn;
+        }
+    }
+    return NULL;
+}
+

I don't have any fundamental objection to this. I would prefer it if the new helper was used by either one of the test or contrib plugins to ensure we don't bitrot it.

Quoted 3 lines
plugins/api.c +0 -0
 /*
  * Instruction information
  *
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
author
#3 lore raw

PATCH 2/2 plugin: extend API with qemu_plugin_tb_size

In reply to Luke Craig
---
 include/qemu/qemu-plugin.h | 10 ++++++++++
 plugins/api.c              |  5 +++++
 2 files changed, 15 insertions(+)
include/qemu/qemu-plugin.h +10 -0
index a1c478c54f..1fa656da82 100644
@@ -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
plugins/api.c +5 -0
index 7ff5e1c1bd..177f0ac9b6 100644
@@ -241,6 +241,11 @@ 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){
+    const DisasContextBase *db = tcg_ctx->plugin_db;
+    return db->size;
+}
+
 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
 {
     const DisasContextBase *db = tcg_ctx->plugin_db;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] plugin: extend API with qemu_plugin_tb_size

Quoted 31 lines · Luke Craig
---
 include/qemu/qemu-plugin.h | 10 ++++++++++
 plugins/api.c              |  5 +++++
 2 files changed, 15 insertions(+)
include/qemu/qemu-plugin.h +10 -0
index a1c478c54f..1fa656da82 100644
@@ -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
plugins/api.c +0 -0
index 7ff5e1c1bd..177f0ac9b6 100644

plugins/api.c size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb) · line 248

Quoted 8 lines
plugins/api.c +5 -0
@@ -241,6 +241,11 @@ 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){
+    const DisasContextBase *db = tcg_ctx->plugin_db;
+    return db->size;
+}
+

FAILED: libqemu-aarch64-linux-user.a.p/plugins_api.c.o
cc -m64 -Ilibqemu-aarch64-linux-user.a.p -I. -I../.. -Itarget/arm -I../../target/arm -I../../common-user/host/x86_64 -I../../linux-user/include/host/x86_64 -I../../linux-user/include -Ilinux-user -I../../linux-user -Ilinux-user/aarch64 -I../../linux-user/aarch64 -Iqapi -Itrace -Iui -Iui/shader -I/usr/include/capstone -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -fdiagnostics-color=auto -Wall -Winvalid-pch -Werror -std=gnu11 -O2 -g -fstack-protector-strong -Wempty-body -Wendif-labels -Wexpansion-to-defined -Wformat-security -Wformat-y2k -Wignored-qualifiers -Wimplicit-fallthrough=2 -Winit-self -Wmissing-format-attribute -Wmissing-prototypes -Wnested-externs -Wold-style-declaration -Wold-style-definition -Wredundant-decls -Wshadow=local -Wstrict-prototypes -Wtype-limits -Wundef -Wvla -Wwrite-strings -Wno-missing-include-dirs -Wno-psabi -Wno-shift-negative-value -isystem /home/alex/lsrc/qemu.git/linux-headers -isystem linux-headers -iquote . -iquote /home/alex/lsrc/qemu.git -iquote /home/alex/lsrc/qemu.git/include -iquote /home/alex/lsrc/qemu.git/host/include/x86_64 -iquote /home/alex/lsrc/qemu.git/host/include/generic -iquote /home/alex/lsrc/qemu.git/tcg/i386 -pthread -mcx16 -msse2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -ftrivial-auto-var-init=zero -fzero-call-used-regs=used-gpr -fPIE -isystem../../linux-headers -isystemlinux-headers -DCOMPILING_PER_TARGET '-DCONFIG_TARGET="aarch64-linux-user-config-target.h"' -MD -MQ libqemu-aarch64-linux-user.a.p/plugins_api.c.o -MF libqemu-aarch64-linux-user.a.p/plugins_api.c.o.d -o libqemu-aarch64-linux-user.a.p/plugins_api.c.o -c ../../plugins/api.c ../../plugins/api.c: In function ‘qemu_plugin_tb_size’:
../../plugins/api.c:246:14: error: ‘DisasContextBase’ has no member named ‘size’
246 | return db->size;
| ^~

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?

Quoted 3 lines
plugins/api.c +0 -0
 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
 {
     const DisasContextBase *db = tcg_ctx->plugin_db;
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw

Re: [PATCH 2/2] plugin: extend API with qemu_plugin_tb_size

In reply to Luke Craig

Hi Luke,

Quoted 32 lines · Luke Craig
---
  include/qemu/qemu-plugin.h | 10 ++++++++++
  plugins/api.c              |  5 +++++
  2 files changed, 15 insertions(+)
include/qemu/qemu-plugin.h +10 -0
index a1c478c54f..1fa656da82 100644
@@ -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
plugins/api.c +0 -0
index 7ff5e1c1bd..177f0ac9b6 100644
@@ -241,6 +241,11 @@ size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
      return tb->n;

plugins/api.c size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb) · line 251

Quoted 10 lines
plugins/api.c +5 -0
  }
 
+size_t qemu_plugin_tb_size(const struct qemu_plugin_tb *tb){
+    const DisasContextBase *db = tcg_ctx->plugin_db;
+    return db->size;
+}
+
  uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
  {
      const DisasContextBase *db = tcg_ctx->plugin_db;

by tb size, do you mean the size, in bytes, of all (original) instructions of the tb?

If yes, you can get it by fetching first and last instruction, and compute the difference between last->vaddr + last->len - first->vaddr.

Regards,
Pierrick