Skip to contentSkip to conversation

plugins: add plugin API to read guest memory

PATCH Superseded Rowan Hart 3 participants 7 messages Aug 22, 2024 – Aug 26, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Rowan Hart

author
#0 lore raw

PATCH 1/1 plugins: add plugin API to read guest memory

---
 docs/about/emulation.rst     |  16 ++++-
 include/qemu/qemu-plugin.h   |  24 +++++++-
 plugins/api.c                |  21 +++++++
 plugins/qemu-plugins.symbols |   1 +
 tests/tcg/plugins/mem.c      |  37 +++++++++++-
 tests/tcg/plugins/syscall.c  | 113 +++++++++++++++++++++++++++++++++++
 6 files changed, 208 insertions(+), 4 deletions(-)
docs/about/emulation.rst +15 -1
index eea1261baa..9c68e37887 100644
@@ -354,6 +354,8 @@ Behaviour can be tweaked with the following arguments:
     - Use callbacks on each memory instrumentation.
   * - hwaddr=true|false
     - Count IO accesses (only for system emulation)
+  * - read=true|false
+    - Read the memory content of each access and display it
 
 System Calls
 ............
@@ -388,6 +390,19 @@ run::
   160          1      0
   135          1      0
 
+Behaviour can be tweaked with the following arguments:
+
+.. list-table:: Syscall plugin arguments
+  :widths: 20 80
+  :header-rows: 1
+
+  * - Option
+    - Description
+  * - print=true|false
+    - Print the number of times each syscall is called
+  * - log_writes=true|false
+    - Log the buffer of each write syscall in hexdump format
+
 Test inline operations
 ......................
 
@@ -777,4 +792,3 @@ Other emulation features
 When running system emulation you can also enable deterministic
 execution which allows for repeatable record/replay debugging. See
 :ref:`Record/Replay<replay>` for more details.
-
include/qemu/qemu-plugin.h +23 -1
index c71c705b69..d4ec73574b 100644
@@ -57,11 +57,19 @@ typedef uint64_t qemu_plugin_id_t;
  * - Remove qemu_plugin_register_vcpu_{tb, insn, mem}_exec_inline.
  *   Those functions are replaced by *_per_vcpu variants, which guarantee
  *   thread-safety for operations.
+ *
+ * version 3:
+ * - modified arguments and return value of qemu_plugin_insn_data to copy
+ *   the data into a user-provided buffer instead of returning a pointer
+ *   to the data.
+ *
+ * version 4:
+ * - added qemu_plugin_read_memory_vaddr
  */
 
 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
 
-#define QEMU_PLUGIN_VERSION 3
+#define QEMU_PLUGIN_VERSION 4
 
 /**
  * struct qemu_info_t - system information for plugins
@@ -852,6 +860,20 @@ typedef struct {
 QEMU_PLUGIN_API
 GArray *qemu_plugin_get_registers(void);
 
+/**
+ * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
+ *
+ * @addr: A virtual address to read from
+ * @len: The number of bytes to read, starting from @addr
+ *
+ * Returns a GByteArray with the read memory. Ownership of the GByteArray is
+ * transferred to the caller, which is responsible for deallocating it after
+ * use. On failure returns NULL.
+ */
+QEMU_PLUGIN_API
+GByteArray *qemu_plugin_read_memory_vaddr(uint64_t addr,
+                                          size_t len);
+
 /**
  * qemu_plugin_read_register() - read register for current vCPU
  *
plugins/api.c +21 -0
index 2ff13d09de..f210ca166a 100644
@@ -527,6 +527,27 @@ GArray *qemu_plugin_get_registers(void)
     return create_register_handles(regs);
 }
 
+GByteArray *qemu_plugin_read_memory_vaddr(vaddr addr, size_t len)
+{
+    g_assert(current_cpu);
+
+    if (len == 0) {
+        return NULL;
+    }
+
+    GByteArray *buf = g_byte_array_sized_new(len);
+    g_byte_array_set_size(buf, len);
+
+    int result = cpu_memory_rw_debug(current_cpu, addr, buf->data, buf->len, 0);
+
+    if (result < 0) {
+        g_byte_array_unref(buf);
+        return NULL;
+    }
+
+    return buf;
+}
+
 int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
 {
     g_assert(current_cpu);
plugins/qemu-plugins.symbols +1 -0
index ca773d8d9f..3ad479a924 100644
@@ -20,6 +20,7 @@
   qemu_plugin_num_vcpus;
   qemu_plugin_outs;
   qemu_plugin_path_to_binary;
+  qemu_plugin_read_memory_vaddr;
   qemu_plugin_read_register;
   qemu_plugin_register_atexit_cb;
   qemu_plugin_register_flush_cb;
tests/tcg/plugins/mem.c +35 -2
index b650dddcce..c65d48680b 100644
@@ -24,7 +24,7 @@ typedef struct {
 static struct qemu_plugin_scoreboard *counts;
 static qemu_plugin_u64 mem_count;
 static qemu_plugin_u64 io_count;
-static bool do_inline, do_callback;
+static bool do_inline, do_callback, do_read;
 static bool do_haddr;
 static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
 
@@ -58,6 +58,30 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
     } else {
         qemu_plugin_u64_add(mem_count, cpu_index, 1);
     }
+
+    if (do_read) {
+        size_t size = qemu_plugin_mem_size_shift(meminfo);
+        GByteArray *data = qemu_plugin_read_memory_vaddr(vaddr, size);
+
+        if (data) {
+            g_autoptr(GString) out = g_string_new("");
+
+            if (qemu_plugin_mem_is_store(meminfo)) {
+                g_string_append(out, "store: ");
+            } else {
+                g_string_append(out, "load: ");
+            }
+
+            g_string_append_printf(out, "vaddr: 0x%" PRIx64 " data: 0x",
+                                   vaddr);
+            for (size_t i = 0; i < data->len; i++) {
+                g_string_append_printf(out, "%02x", data->data[i]);
+            }
+            g_string_append(out, "\n");
+            qemu_plugin_outs(out->str);
+            g_byte_array_free(data, TRUE);
+        }
+    }
 }
 
 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
@@ -86,7 +110,6 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info,
                                            int argc, char **argv)
 {
-
     for (int i = 0; i < argc; i++) {
         char *opt = argv[i];
         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
@@ -117,6 +140,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
                 return -1;
             }
+        } else if (g_strcmp0(tokens[0], "read") == 0) {
+            if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_read)) {
+                fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+                return -1;
+            }
         } else {
             fprintf(stderr, "option parsing failed: %s\n", opt);
             return -1;
@@ -129,6 +157,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
         return -1;
     }
 
+    if (do_read && !do_callback) {
+        fprintf(stderr, "can't enable memory reading without callback\n");
+        return -1;
+    }
+
     counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
     mem_count = qemu_plugin_scoreboard_u64_in_struct(
         counts, CPUCount, mem_count);
tests/tcg/plugins/syscall.c +113 -0

139 lines not on this page · raw message

Signature · 1 line
2.46.0
#1 lore raw

Hi Rowan, thanks for your contribution.

To give some context on the answer, we are currently working to add a similar "read_memory" API, but associated to memory callbacks for plugins (https://lore.kernel.org/qemu-devel/[email protected]/T/#t).

A key aspect of what you propose here, is that the memory may have changed during the write time, and when you read it, while what we propose guarantees to track every change correctly.

It's not a bad thing, and both API are definitely complementary. When talking to Alex, he was keen to add a global read_memory API (like you propose), after we merge the first one.

@Alex: any thought on this?

Regarding your patch, it would be much easier if you could split that in different commits. Adding API first, then modify each plugin in a different commit. This way, it would be easier to review. I'll make my comments in this patch, but for v2, please split those individual commits, and a cover letter, describing your changes (https://github.com/stefanha/git-publish is a great tool if you want to easily push series).

Quoted 53 lines · Rowan Hart
---
  docs/about/emulation.rst     |  16 ++++-
  include/qemu/qemu-plugin.h   |  24 +++++++-
  plugins/api.c                |  21 +++++++
  plugins/qemu-plugins.symbols |   1 +
  tests/tcg/plugins/mem.c      |  37 +++++++++++-
  tests/tcg/plugins/syscall.c  | 113 +++++++++++++++++++++++++++++++++++
  6 files changed, 208 insertions(+), 4 deletions(-)
docs/about/emulation.rst +15 -1
index eea1261baa..9c68e37887 100644
@@ -354,6 +354,8 @@ Behaviour can be tweaked with the following arguments:
      - Use callbacks on each memory instrumentation.
    * - hwaddr=true|false
      - Count IO accesses (only for system emulation)
+  * - read=true|false
+    - Read the memory content of each access and display it
 
  System Calls
............
@@ -388,6 +390,19 @@ run::
    160          1      0
    135          1      0
 
+Behaviour can be tweaked with the following arguments:
+
+.. list-table:: Syscall plugin arguments
+  :widths: 20 80
+  :header-rows: 1
+
+  * - Option
+    - Description
+  * - print=true|false
+    - Print the number of times each syscall is called
+  * - log_writes=true|false
+    - Log the buffer of each write syscall in hexdump format
+
  Test inline operations
......................
@@ -777,4 +792,3 @@ Other emulation features
  When running system emulation you can also enable deterministic
  execution which allows for repeatable record/replay debugging. See
  :ref:`Record/Replay<replay>` for more details.
-
include/qemu/qemu-plugin.h +0 -0
index c71c705b69..d4ec73574b 100644

include/qemu/qemu-plugin.h typedef uint64_t qemu_plugin_id_t; · line 65

Quoted 9 lines
include/qemu/qemu-plugin.h +6 -0
@@ -57,11 +57,19 @@ typedef uint64_t qemu_plugin_id_t;
   * - Remove qemu_plugin_register_vcpu_{tb, insn, mem}_exec_inline.
   *   Those functions are replaced by *_per_vcpu variants, which guarantee
   *   thread-safety for operations.
+ *
+ * version 3:
+ * - modified arguments and return value of qemu_plugin_insn_data to copy
+ *   the data into a user-provided buffer instead of returning a pointer
+ *   to the data.
+ *

Is it a change left on your side, or a bad diff?

Quoted 19 lines
include/qemu/qemu-plugin.h +7 -1
+ * version 4:
+ * - added qemu_plugin_read_memory_vaddr
   */
 
  extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
 
-#define QEMU_PLUGIN_VERSION 3
+#define QEMU_PLUGIN_VERSION 4
 
  /**
   * struct qemu_info_t - system information for plugins
@@ -852,6 +860,20 @@ typedef struct {
  QEMU_PLUGIN_API
  GArray *qemu_plugin_get_registers(void);
 
+/**
+ * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
+ *
+ * @addr: A virtual address to read from

include/qemu/qemu-plugin.h typedef struct { · line 876

Quoted 10 lines
include/qemu/qemu-plugin.h +10 -0
+ * @len: The number of bytes to read, starting from @addr
+ *
+ * Returns a GByteArray with the read memory. Ownership of the GByteArray is
+ * transferred to the caller, which is responsible for deallocating it after
+ * use. On failure returns NULL.
+ */
+QEMU_PLUGIN_API
+GByteArray *qemu_plugin_read_memory_vaddr(uint64_t addr,
+                                          size_t len);
+

IMHO, it would be better to pass the buffer as a parameter, instead of allocating a new one everytime.

bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *buf, size_t len).

Quoted 78 lines
include/qemu/qemu-plugin.h +0 -0
  /**
   * qemu_plugin_read_register() - read register for current vCPU
   *
plugins/api.c +21 -0
index 2ff13d09de..f210ca166a 100644
@@ -527,6 +527,27 @@ GArray *qemu_plugin_get_registers(void)
      return create_register_handles(regs);
  }
 
+GByteArray *qemu_plugin_read_memory_vaddr(vaddr addr, size_t len)
+{
+    g_assert(current_cpu);
+
+    if (len == 0) {
+        return NULL;
+    }
+
+    GByteArray *buf = g_byte_array_sized_new(len);
+    g_byte_array_set_size(buf, len);
+
+    int result = cpu_memory_rw_debug(current_cpu, addr, buf->data, buf->len, 0);
+
+    if (result < 0) {
+        g_byte_array_unref(buf);
+        return NULL;
+    }
+
+    return buf;
+}
+
  int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
  {
      g_assert(current_cpu);
plugins/qemu-plugins.symbols +1 -0
index ca773d8d9f..3ad479a924 100644
@@ -20,6 +20,7 @@
    qemu_plugin_num_vcpus;
    qemu_plugin_outs;
    qemu_plugin_path_to_binary;
+  qemu_plugin_read_memory_vaddr;
    qemu_plugin_read_register;
    qemu_plugin_register_atexit_cb;
    qemu_plugin_register_flush_cb;
tests/tcg/plugins/mem.c +15 -1
index b650dddcce..c65d48680b 100644
@@ -24,7 +24,7 @@ typedef struct {
  static struct qemu_plugin_scoreboard *counts;
  static qemu_plugin_u64 mem_count;
  static qemu_plugin_u64 io_count;
-static bool do_inline, do_callback;
+static bool do_inline, do_callback, do_read;
  static bool do_haddr;
  static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
 
@@ -58,6 +58,30 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
      } else {
          qemu_plugin_u64_add(mem_count, cpu_index, 1);
      }
+
+    if (do_read) {
+        size_t size = qemu_plugin_mem_size_shift(meminfo);
+        GByteArray *data = qemu_plugin_read_memory_vaddr(vaddr, size);
+
+        if (data) {
+            g_autoptr(GString) out = g_string_new("");
+
+            if (qemu_plugin_mem_is_store(meminfo)) {
+                g_string_append(out, "store: ");
+            } else {
+                g_string_append(out, "load: ");
+            }
+

tests/tcg/plugins/mem.c static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, · line 84

Quoted 10 lines
tests/tcg/plugins/mem.c +10 -0
+            g_string_append_printf(out, "vaddr: 0x%" PRIx64 " data: 0x",
+                                   vaddr);
+            for (size_t i = 0; i < data->len; i++) {
+                g_string_append_printf(out, "%02x", data->data[i]);
+            }
+            g_string_append(out, "\n");
+            qemu_plugin_outs(out->str);
+            g_byte_array_free(data, TRUE);
+        }
+    }

See other series, merging an API for getting values read on a memory access. It's a better fit to implement this. So I think it's better to drop this plugin modification.

Quoted 129 lines
tests/tcg/plugins/mem.c +10 -1
  }
 
  static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
@@ -86,7 +110,6 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                             const qemu_info_t *info,
                                             int argc, char **argv)
  {
-
      for (int i = 0; i < argc; i++) {
          char *opt = argv[i];
          g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
@@ -117,6 +140,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                  fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
                  return -1;
              }
+        } else if (g_strcmp0(tokens[0], "read") == 0) {
+            if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_read)) {
+                fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+                return -1;
+            }
          } else {
              fprintf(stderr, "option parsing failed: %s\n", opt);
              return -1;
@@ -129,6 +157,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
          return -1;
      }
 
+    if (do_read && !do_callback) {
+        fprintf(stderr, "can't enable memory reading without callback\n");
+        return -1;
+    }
+
      counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
      mem_count = qemu_plugin_scoreboard_u64_in_struct(
          counts, CPUCount, mem_count);
tests/tcg/plugins/syscall.c +77 -0
index 72e1a5bf90..67c7c404c4 100644
@@ -22,8 +22,56 @@ typedef struct {
      int64_t errors;
  } SyscallStats;
 
+struct SyscallInfo {
+    const char *name;
+    int64_t write_sysno;
+};
+
+const struct SyscallInfo arch_syscall_info[] = {
+    { "aarch64", 64 },
+    { "aarch64_be", 64 },
+    { "alpha", 4 },
+    { "arm", 4 },
+    { "armeb", 4 },
+    { "avr", -1 },
+    { "cris", -1 },
+    { "hexagon", 64 },
+    { "hppa", -1 },
+    { "i386", 4 },
+    { "loongarch64", -1 },
+    { "m68k", 4 },
+    { "microblaze", 4 },
+    { "microblazeel", 4 },
+    { "mips", 1 },
+    { "mips64", 1 },
+    { "mips64el", 1 },
+    { "mipsel", 1 },
+    { "mipsn32", 1 },
+    { "mipsn32el", 1 },
+    { "or1k", -1 },
+    { "ppc", 4 },
+    { "ppc64", 4 },
+    { "ppc64le", 4 },
+    { "riscv32", 64 },
+    { "riscv64", 64 },
+    { "rx", -1 },
+    { "s390x", -1 },
+    { "sh4", -1 },
+    { "sh4eb", -1 },
+    { "sparc", 4 },
+    { "sparc32plus", 4 },
+    { "sparc64", 4 },
+    { "tricore", -1 },
+    { "x86_64", 1 },
+    { "xtensa", 13 },
+    { "xtensaeb", 13 },
+    { NULL, -1 },
+};
+
  static GMutex lock;
  static GHashTable *statistics;
+static bool do_log_writes;
+static int64_t write_sysno = -1;
 
  static SyscallStats *get_or_create_entry(int64_t num)
  {
@@ -54,6 +102,51 @@ static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
          g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
          qemu_plugin_outs(out);
      }
+
+    if (do_log_writes && num == write_sysno) {
+        GByteArray *data = qemu_plugin_read_memory_vaddr(a2, a3);
+
+        g_autoptr(GString) out = g_string_new("");
+
+        size_t rows = (data->len % 16 == 0)
+            ? (data->len / 16)
+            : ((data->len / 16) + 1);
+        for (size_t row = 0; row < rows; row++) {
+            size_t len = (rows != data->len / 16 && row == rows - 1)
+                ? (data->len % 16)
+                : 16;
+            for (size_t i = 0; i < len; i++) {
+                g_string_append_printf(out, "%02x ",
+                    data->data[(row * 16) + i]);
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            for (size_t i = len; i < 16; i++) {
+                g_string_append(out, "   ");
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            g_string_append(out, " | ");
+            for (size_t i = 0; i < len; i++) {
+                g_string_append_printf(out,

tests/tcg/plugins/syscall.c static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index, · line 143

Quoted 10 lines
tests/tcg/plugins/syscall.c +10 -0
+                    (data->data[(row * 16) + i] >= 0x21
+                        && data->data[(row * 16) + i] <= 0x7e)
+                    ? "%c"
+                    : ".", data->data[(row * 16) + i]);
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            g_string_append(out, "\n");
+        }

Could you add a comment to show what is expected format? From the code, with all these loops and ternary expressions, it's not easy to follow.

Quoted 30 lines
tests/tcg/plugins/syscall.c +16 -0
+
+
+        qemu_plugin_outs(out->str);
+
+        g_byte_array_free(data, TRUE);
+    }
  }
 
  static void vcpu_syscall_ret(qemu_plugin_id_t id, unsigned int vcpu_idx,
@@ -127,6 +220,10 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
              if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_print)) {
                  fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
              }
+        } else if (g_strcmp0(tokens[0], "log_writes") == 0) {
+            if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_log_writes)) {
+                fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+            }
          } else {
              fprintf(stderr, "unsupported argument: %s\n", argv[i]);
              return -1;
@@ -137,6 +234,22 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
          statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
      }
 
+    if (do_log_writes) {
+        for (const struct SyscallInfo *syscall_info = arch_syscall_info;
+            syscall_info->name != NULL; syscall_info++) {
+
+            if (g_strcmp0(syscall_info->name, info->target_name) == 0) {
+                write_sysno = syscall_info->write_sysno;

tests/tcg/plugins/syscall.c QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, · line 252

Quoted 10 lines
tests/tcg/plugins/syscall.c +10 -0
+                break;
+            }
+        }
+
+        if (write_sysno == -1) {
+            fprintf(stderr, "write syscall number not found\n");
+            return -1;
+        }
+    }
+

It's good! I appreciate to see this feature.

tests/tcg/plugins/syscall.c QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, · line 255

Quoted 3 lines
tests/tcg/plugins/syscall.c +0 -0
      qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
      qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
      qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);

Thanks,
Pierrick

#2 lore raw
Quoted 9 lines · Pierrick Bouvier
Quoted 6 lines

+ *
+ * version 3:
+ * - modified arguments and return value of qemu_plugin_insn_data to copy
+ * the data into a user-provided buffer instead of returning a pointer
+ * to the data.
+ *

Is it a change left on your side, or a bad diff?

Just saw it's a comment we forgot to add when bumping version. Thanks for changing it.

#3 lore raw
In reply to Pierrick Bouvier
Quoted 11 lines · Pierrick Bouvier

Hi Rowan, thanks for your contribution.

To give some context on the answer, we are currently working to add a similar "read_memory" API, but associated to memory callbacks for plugins
(https://lore.kernel.org/qemu-devel/[email protected]/T/#t).

A key aspect of what you propose here, is that the memory may have changed during the write time, and when you read it, while what we propose guarantees to track every change correctly.

Quoted 5 lines

It's not a bad thing, and both API are definitely complementary. When talking to Alex, he was keen to add a global read_memory API (like you propose), after we merge the first one.

@Alex: any thought on this?

I'd like to get the memory callback API merged first - mostly because that is the API that will absolutely reflect what was loaded or stored to a given memory location. For precise instrumentation that is the one to use.

However I agree the ability to read the state of memory outside of loads and stores is useful especially for something like this. It's not unreasonable to assume the memory state of arguments going into a syscall isn't being messed with and it is easier to track pointers and strings with a more general purpose API.

Quoted 83 lines

Regarding your patch, it would be much easier if you could split that in different commits. Adding API first, then modify each plugin in a different commit. This way, it would be easier to review. I'll make my comments in this patch, but for v2, please split those individual commits, and a cover letter, describing your changes
(https://github.com/stefanha/git-publish is a great tool if you want to easily push series).

Quoted 49 lines · Rowan Hart
---
  docs/about/emulation.rst     |  16 ++++-
  include/qemu/qemu-plugin.h   |  24 +++++++-
  plugins/api.c                |  21 +++++++
  plugins/qemu-plugins.symbols |   1 +
  tests/tcg/plugins/mem.c      |  37 +++++++++++-
  tests/tcg/plugins/syscall.c  | 113 +++++++++++++++++++++++++++++++++++
  6 files changed, 208 insertions(+), 4 deletions(-)
docs/about/emulation.rst +14 -1
index eea1261baa..9c68e37887 100644
@@ -354,6 +354,8 @@ Behaviour can be tweaked with the following arguments:
      - Use callbacks on each memory instrumentation.
    * - hwaddr=true|false
      - Count IO accesses (only for system emulation)
+  * - read=true|false
+    - Read the memory content of each access and display it
    System Calls
............
@@ -388,6 +390,19 @@ run::
    160          1      0
    135          1      0
  +Behaviour can be tweaked with the following arguments:
+
+.. list-table:: Syscall plugin arguments
+  :widths: 20 80
+  :header-rows: 1
+
+  * - Option
+    - Description
+  * - print=true|false
+    - Print the number of times each syscall is called
+  * - log_writes=true|false
+    - Log the buffer of each write syscall in hexdump format
+
  Test inline operations
......................
  @@ -777,4 +792,3 @@ Other emulation features
  When running system emulation you can also enable deterministic
  execution which allows for repeatable record/replay debugging. See
  :ref:`Record/Replay<replay>` for more details.
-
include/qemu/qemu-plugin.h +0 -0
index c71c705b69..d4ec73574b 100644

include/qemu/qemu-plugin.h typedef uint64_t qemu_plugin_id_t; · line 65

Quoted 9 lines
include/qemu/qemu-plugin.h +6 -0
@@ -57,11 +57,19 @@ typedef uint64_t qemu_plugin_id_t;
   * - Remove qemu_plugin_register_vcpu_{tb, insn, mem}_exec_inline.
   *   Those functions are replaced by *_per_vcpu variants, which guarantee
   *   thread-safety for operations.
+ *
+ * version 3:
+ * - modified arguments and return value of qemu_plugin_insn_data to copy
+ *   the data into a user-provided buffer instead of returning a pointer
+ *   to the data.
+ *

Is it a change left on your side, or a bad diff?

Quoted 12 lines
include/qemu/qemu-plugin.h +3 -0
+ * version 4:
+ * - added qemu_plugin_read_memory_vaddr
   */
    extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
  -#define QEMU_PLUGIN_VERSION 3
+#define QEMU_PLUGIN_VERSION 4
    /**
   * struct qemu_info_t - system information for plugins
@@ -852,6 +860,20 @@ typedef struct {
  QEMU_PLUGIN_API
  GArray *qemu_plugin_get_registers(void);
  +/**
Quoted 8 lines

include/qemu/qemu-plugin.h typedef struct { · line 871

Quoted 8 lines
include/qemu/qemu-plugin.h +8 -0
+ * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
+ *
+ * @addr: A virtual address to read from
+ * @len: The number of bytes to read, starting from @addr
+ *
+ * Returns a GByteArray with the read memory. Ownership of the GByteArray is
+ * transferred to the caller, which is responsible for deallocating it after
+ * use. On failure returns NULL.

We should definitely point out the pitfalls w.r.t callbacks here.

Quoted 11 lines

include/qemu/qemu-plugin.h typedef struct { · line 876

Quoted 5 lines
include/qemu/qemu-plugin.h +5 -0
+ */
+QEMU_PLUGIN_API
+GByteArray *qemu_plugin_read_memory_vaddr(uint64_t addr,
+                                          size_t len);
+

IMHO, it would be better to pass the buffer as a parameter, instead of allocating a new one everytime.

bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *buf, size_t len).

The registers case certainly does it this way so it makes sense to be consistent. It also allows the plugins to re-use buffers if it wants and makes managing lifetime a bit easier.

<snip>

Quoted 16 lines
Quoted 16 lines
include/qemu/qemu-plugin.h +9 -0
          } else {
              fprintf(stderr, "unsupported argument: %s\n", argv[i]);
              return -1;
@@ -137,6 +234,22 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
          statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
      }
  +    if (do_log_writes) {
+        for (const struct SyscallInfo *syscall_info = arch_syscall_info;
+            syscall_info->name != NULL; syscall_info++) {
+
+            if (g_strcmp0(syscall_info->name, info->target_name) == 0) {
+                write_sysno = syscall_info->write_sysno;
+                break;
+            }
+        }
+
Quoted 12 lines

include/qemu/qemu-plugin.h QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, · line 251

Quoted 6 lines
include/qemu/qemu-plugin.h +6 -0
+        if (write_sysno == -1) {
+            fprintf(stderr, "write syscall number not found\n");
+            return -1;
+        }
+    }
+

It's good! I appreciate to see this feature.

Quoted 3 lines
include/qemu/qemu-plugin.h +0 -0
      qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
      qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
      qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);

There was someone on IRC looking to trace system calls in system mode (by tracking the syscall instruction and reading the registers at the time). I wonder if we could make this plugin do the right thing in both modes?

Quoted 3 lines

Thanks,
Pierrick

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
author
#4 lore raw

Alex,

Thanks for the additional information.

Quoted 16 lines
Quoted 10 lines

A key aspect of what you propose here, is that the memory may have changed during the write time, and when you read it, while what we propose guarantees to track every change correctly.

It's not a bad thing, and both API are definitely complementary. When talking to Alex, he was keen to add a global read_memory API (like you propose), after we merge the first one.

@Alex: any thought on this?

I'd like to get the memory callback API merged first - mostly because that is the API that will absolutely reflect what was loaded or stored to a given memory location. For precise instrumentation that is the one to use.

Quoted 6 lines

However I agree the ability to read the state of memory outside of loads and stores is useful especially for something like this. It's not unreasonable to assume the memory state of arguments going into a syscall isn't being messed with and it is easier to track pointers and strings with a more general purpose API.

I agree, I considered the absolute load/store question and poked around the code a bit, but I didn't find what looked like a solid way to either:

A) Ensure that all writes are flushed before the read happens (which sounds like a hefty performance penalty anyway) or
B) Check whether there are outstanding writes and return an error

It sounds like essentially use cases where that level of per-insn write granularity matters should utilize your upcoming API instead of this one, and I will add a call-out to the doc of this one to alert users of the potential pitfall.

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
      qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
      qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
      qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);

There was someone on IRC looking to trace system calls in system mode (by tracking the syscall instruction and reading the registers at the time). I wonder if we could make this plugin do the right thing in both modes?

Cool! I think this should be doable.

-Rowan

author
#5 lore raw
In reply to Pierrick Bouvier

Alex & Pierrick,

Thank you for the feedback! This is my first contribution to QEMU, so I'm glad it at least passes the initial smell test :)

Quoted 3 lines

I'll make my comments in this patch, but for v2, please split those individual commits, and a cover letter, describing your changes (https://github.com/
stefanha/git-publish is a great tool if you want to easily push series).

Will do! Mailing list dev is new to me but I will do a practice run to try and not mess it up.

Quoted 8 lines

include/qemu/qemu-plugin.h typedef struct { · line 876

Quoted 4 lines
include/qemu/qemu-plugin.h +4 -0
+QEMU_PLUGIN_API
+GByteArray *qemu_plugin_read_memory_vaddr(uint64_t addr,
+                                          size_t len);
+

IMHO, it would be better to pass the buffer as a parameter, instead of allocating a new one everytime.

bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *buf, size_t len).

Sure, this makes perfect sense. I considered both and picked wrong so not hard to change.

Quoted 80 lines
Quoted 80 lines

/**
* qemu_plugin_read_register() - read register for current vCPU *

plugins/api.c +20 -0
index 2ff13d09de..f210ca166a 100644
@@ -527,6 +527,27 @@ GArray *qemu_plugin_get_registers(void)
      return create_register_handles(regs);
  }
  +GByteArray *qemu_plugin_read_memory_vaddr(vaddr addr, size_t len)
+{
+    g_assert(current_cpu);
+
+    if (len == 0) {
+        return NULL;
+    }
+
+    GByteArray *buf = g_byte_array_sized_new(len);
+    g_byte_array_set_size(buf, len);
+
+    int result = cpu_memory_rw_debug(current_cpu, addr, buf->data, buf->len, 0);
+
+    if (result < 0) {
+        g_byte_array_unref(buf);
+        return NULL;
+    }
+
+    return buf;
+}
+
  int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
  {
      g_assert(current_cpu);
plugins/qemu-plugins.symbols +1 -0
index ca773d8d9f..3ad479a924 100644
@@ -20,6 +20,7 @@
    qemu_plugin_num_vcpus;
    qemu_plugin_outs;
    qemu_plugin_path_to_binary;
+  qemu_plugin_read_memory_vaddr;
    qemu_plugin_read_register;
    qemu_plugin_register_atexit_cb;
    qemu_plugin_register_flush_cb;
tests/tcg/plugins/mem.c +1 -1
index b650dddcce..c65d48680b 100644
@@ -24,7 +24,7 @@ typedef struct {
  static struct qemu_plugin_scoreboard *counts;
  static qemu_plugin_u64 mem_count;
  static qemu_plugin_u64 io_count;
-static bool do_inline, do_callback;
+static bool do_inline, do_callback, do_read;
  static bool do_haddr;
  static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
  @@ -58,6 +58,30 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
      } else {
          qemu_plugin_u64_add(mem_count, cpu_index, 1);
      }
tests/tcg/plugins/mem.c +18 -0
+
+    if (do_read) {
+        size_t size = qemu_plugin_mem_size_shift(meminfo);
+        GByteArray *data = qemu_plugin_read_memory_vaddr(vaddr, size);
+
+        if (data) {
+            g_autoptr(GString) out = g_string_new("");
+
+            if (qemu_plugin_mem_is_store(meminfo)) {
+                g_string_append(out, "store: ");
+            } else {
+                g_string_append(out, "load: ");
+            }
+
+            g_string_append_printf(out, "vaddr: 0x%" PRIx64 " data: 0x",
+                                   vaddr);
+            for (size_t i = 0; i < data->len; i++) {
+                g_string_append_printf(out, "%02x", data->data[i]);
Quoted 8 lines

tests/tcg/plugins/mem.c static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, · line 84

Quoted 6 lines
tests/tcg/plugins/mem.c +6 -0
+            }
+            g_string_append(out, "\n");
+            qemu_plugin_outs(out->str);
+            g_byte_array_free(data, TRUE);
+        }
+    }

See other series, merging an API for getting values read on a memory access. It's a better fit to implement this. So I think it's better to drop this plugin modification.

Ok, will drop this one and keep the modification to the syscalls plugin only as an example of how to use the API.

Quoted 129 lines
Quoted 129 lines
  }
    static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
tests/tcg/plugins/mem.c +9 -1
@@ -86,7 +110,6 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                             const qemu_info_t *info,
                                             int argc, char **argv)
  {
-
      for (int i = 0; i < argc; i++) {
          char *opt = argv[i];
          g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
@@ -117,6 +140,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                  fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
                  return -1;
              }
+        } else if (g_strcmp0(tokens[0], "read") == 0) {
+            if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_read)) {
+                fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+                return -1;
+            }
          } else {
              fprintf(stderr, "option parsing failed: %s\n", opt);
              return -1;
@@ -129,6 +157,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
          return -1;
      }
  +    if (do_read && !do_callback) {
+        fprintf(stderr, "can't enable memory reading without callback\n");
+        return -1;
+    }
+
      counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
      mem_count = qemu_plugin_scoreboard_u64_in_struct(
          counts, CPUCount, mem_count);
tests/tcg/plugins/syscall.c +80 -0
index 72e1a5bf90..67c7c404c4 100644
@@ -22,8 +22,56 @@ typedef struct {
      int64_t errors;
  } SyscallStats;
  +struct SyscallInfo {
+    const char *name;
+    int64_t write_sysno;
+};
+
+const struct SyscallInfo arch_syscall_info[] = {
+    { "aarch64", 64 },
+    { "aarch64_be", 64 },
+    { "alpha", 4 },
+    { "arm", 4 },
+    { "armeb", 4 },
+    { "avr", -1 },
+    { "cris", -1 },
+    { "hexagon", 64 },
+    { "hppa", -1 },
+    { "i386", 4 },
+    { "loongarch64", -1 },
+    { "m68k", 4 },
+    { "microblaze", 4 },
+    { "microblazeel", 4 },
+    { "mips", 1 },
+    { "mips64", 1 },
+    { "mips64el", 1 },
+    { "mipsel", 1 },
+    { "mipsn32", 1 },
+    { "mipsn32el", 1 },
+    { "or1k", -1 },
+    { "ppc", 4 },
+    { "ppc64", 4 },
+    { "ppc64le", 4 },
+    { "riscv32", 64 },
+    { "riscv64", 64 },
+    { "rx", -1 },
+    { "s390x", -1 },
+    { "sh4", -1 },
+    { "sh4eb", -1 },
+    { "sparc", 4 },
+    { "sparc32plus", 4 },
+    { "sparc64", 4 },
+    { "tricore", -1 },
+    { "x86_64", 1 },
+    { "xtensa", 13 },
+    { "xtensaeb", 13 },
+    { NULL, -1 },
+};
+
  static GMutex lock;
  static GHashTable *statistics;
+static bool do_log_writes;
+static int64_t write_sysno = -1;
    static SyscallStats *get_or_create_entry(int64_t num)
  {
@@ -54,6 +102,51 @@ static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
          g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
          qemu_plugin_outs(out);
      }
+
+    if (do_log_writes && num == write_sysno) {
+        GByteArray *data = qemu_plugin_read_memory_vaddr(a2, a3);
+
+        g_autoptr(GString) out = g_string_new("");
+
+        size_t rows = (data->len % 16 == 0)
+            ? (data->len / 16)
+            : ((data->len / 16) + 1);
+        for (size_t row = 0; row < rows; row++) {
+            size_t len = (rows != data->len / 16 && row == rows - 1)
+                ? (data->len % 16)
+                : 16;
+            for (size_t i = 0; i < len; i++) {
+                g_string_append_printf(out, "%02x ",
+                    data->data[(row * 16) + i]);
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            for (size_t i = len; i < 16; i++) {
+                g_string_append(out, "   ");
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            g_string_append(out, " | ");
+            for (size_t i = 0; i < len; i++) {
+                g_string_append_printf(out,
+                    (data->data[(row * 16) + i] >= 0x21
+                        && data->data[(row * 16) + i] <= 0x7e)
+                    ? "%c"
+                    : ".", data->data[(row * 16) + i]);
Quoted 8 lines

tests/tcg/plugins/syscall.c static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index, · line 143

Quoted 6 lines
tests/tcg/plugins/syscall.c +6 -0
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            g_string_append(out, "\n");
+        }

Could you add a comment to show what is expected format? From the code, with all these loops and ternary expressions, it's not easy to follow.

Indeed. I will just generally simplify this

Thanks!

-Rowan

#6 lore raw
Quoted 5 lines · Rowan Hart

Alex & Pierrick,

Thank you for the feedback! This is my first contribution to QEMU, so I'm glad it at least passes the initial smell test :)

Sure, no worries, you did well!

Quoted 7 lines
Quoted 3 lines

I'll make my comments in this patch, but for v2, please split those individual commits, and a cover letter, describing your changes (https://github.com/
stefanha/git-publish is a great tool if you want to easily push series).

Will do! Mailing list dev is new to me but I will do a practice run to try and not mess it up.

If you already configured git send-email, git-publish just does all the rest for you (collecting reviewers, prepare patches, and send using git send-email). In more, it tracks you different versions, so it's pretty easy to iterate on a series.

Quoted 103 lines
Quoted 10 lines

include/qemu/qemu-plugin.h typedef struct { · line 876

Quoted 6 lines
include/qemu/qemu-plugin.h +4 -0
+QEMU_PLUGIN_API
+GByteArray *qemu_plugin_read_memory_vaddr(uint64_t addr,
+                                          size_t len);
+

IMHO, it would be better to pass the buffer as a parameter, instead of allocating a new one everytime.

bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *buf, size_t len).

Sure, this makes perfect sense. I considered both and picked wrong so not hard to change.

Quoted 80 lines
Quoted 80 lines
   /**
    * qemu_plugin_read_register() - read register for current vCPU
    *
plugins/api.c +20 -0
index 2ff13d09de..f210ca166a 100644
@@ -527,6 +527,27 @@ GArray *qemu_plugin_get_registers(void)
       return create_register_handles(regs);
   }
   +GByteArray *qemu_plugin_read_memory_vaddr(vaddr addr, size_t len)
+{
+    g_assert(current_cpu);
+
+    if (len == 0) {
+        return NULL;
+    }
+
+    GByteArray *buf = g_byte_array_sized_new(len);
+    g_byte_array_set_size(buf, len);
+
+    int result = cpu_memory_rw_debug(current_cpu, addr, buf->data, buf->len, 0);
+
+    if (result < 0) {
+        g_byte_array_unref(buf);
+        return NULL;
+    }
+
+    return buf;
+}
+
   int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
   {
       g_assert(current_cpu);
plugins/qemu-plugins.symbols +1 -0
index ca773d8d9f..3ad479a924 100644
@@ -20,6 +20,7 @@
     qemu_plugin_num_vcpus;
     qemu_plugin_outs;
     qemu_plugin_path_to_binary;
+  qemu_plugin_read_memory_vaddr;
     qemu_plugin_read_register;
     qemu_plugin_register_atexit_cb;
     qemu_plugin_register_flush_cb;
tests/tcg/plugins/mem.c +1 -1
index b650dddcce..c65d48680b 100644
@@ -24,7 +24,7 @@ typedef struct {
   static struct qemu_plugin_scoreboard *counts;
   static qemu_plugin_u64 mem_count;
   static qemu_plugin_u64 io_count;
-static bool do_inline, do_callback;
+static bool do_inline, do_callback, do_read;
   static bool do_haddr;
   static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
   @@ -58,6 +58,30 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
       } else {
           qemu_plugin_u64_add(mem_count, cpu_index, 1);
       }
tests/tcg/plugins/mem.c +18 -0
+
+    if (do_read) {
+        size_t size = qemu_plugin_mem_size_shift(meminfo);
+        GByteArray *data = qemu_plugin_read_memory_vaddr(vaddr, size);
+
+        if (data) {
+            g_autoptr(GString) out = g_string_new("");
+
+            if (qemu_plugin_mem_is_store(meminfo)) {
+                g_string_append(out, "store: ");
+            } else {
+                g_string_append(out, "load: ");
+            }
+
+            g_string_append_printf(out, "vaddr: 0x%" PRIx64 " data: 0x",
+                                   vaddr);
+            for (size_t i = 0; i < data->len; i++) {
+                g_string_append_printf(out, "%02x", data->data[i]);
Quoted 8 lines

tests/tcg/plugins/mem.c static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, · line 84

Quoted 6 lines
tests/tcg/plugins/mem.c +6 -0
+            }
+            g_string_append(out, "\n");
+            qemu_plugin_outs(out->str);
+            g_byte_array_free(data, TRUE);
+        }
+    }

See other series, merging an API for getting values read on a memory access. It's a better fit to implement this. So I think it's better to drop this plugin modification.

Quoted 3 lines

Ok, will drop this one and keep the modification to the syscalls plugin only as an example of how to use the API.

Good, thanks!

Quoted 138 lines
Quoted 129 lines
Quoted 129 lines
   }
     static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
tests/tcg/plugins/mem.c +9 -1
@@ -86,7 +110,6 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                              const qemu_info_t *info,
                                              int argc, char **argv)
   {
-
       for (int i = 0; i < argc; i++) {
           char *opt = argv[i];
           g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
@@ -117,6 +140,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                   fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
                   return -1;
               }
+        } else if (g_strcmp0(tokens[0], "read") == 0) {
+            if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_read)) {
+                fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+                return -1;
+            }
           } else {
               fprintf(stderr, "option parsing failed: %s\n", opt);
               return -1;
@@ -129,6 +157,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
           return -1;
       }
   +    if (do_read && !do_callback) {
+        fprintf(stderr, "can't enable memory reading without callback\n");
+        return -1;
+    }
+
       counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
       mem_count = qemu_plugin_scoreboard_u64_in_struct(
           counts, CPUCount, mem_count);
tests/tcg/plugins/syscall.c +80 -0
index 72e1a5bf90..67c7c404c4 100644
@@ -22,8 +22,56 @@ typedef struct {
       int64_t errors;
   } SyscallStats;
   +struct SyscallInfo {
+    const char *name;
+    int64_t write_sysno;
+};
+
+const struct SyscallInfo arch_syscall_info[] = {
+    { "aarch64", 64 },
+    { "aarch64_be", 64 },
+    { "alpha", 4 },
+    { "arm", 4 },
+    { "armeb", 4 },
+    { "avr", -1 },
+    { "cris", -1 },
+    { "hexagon", 64 },
+    { "hppa", -1 },
+    { "i386", 4 },
+    { "loongarch64", -1 },
+    { "m68k", 4 },
+    { "microblaze", 4 },
+    { "microblazeel", 4 },
+    { "mips", 1 },
+    { "mips64", 1 },
+    { "mips64el", 1 },
+    { "mipsel", 1 },
+    { "mipsn32", 1 },
+    { "mipsn32el", 1 },
+    { "or1k", -1 },
+    { "ppc", 4 },
+    { "ppc64", 4 },
+    { "ppc64le", 4 },
+    { "riscv32", 64 },
+    { "riscv64", 64 },
+    { "rx", -1 },
+    { "s390x", -1 },
+    { "sh4", -1 },
+    { "sh4eb", -1 },
+    { "sparc", 4 },
+    { "sparc32plus", 4 },
+    { "sparc64", 4 },
+    { "tricore", -1 },
+    { "x86_64", 1 },
+    { "xtensa", 13 },
+    { "xtensaeb", 13 },
+    { NULL, -1 },
+};
+
   static GMutex lock;
   static GHashTable *statistics;
+static bool do_log_writes;
+static int64_t write_sysno = -1;
     static SyscallStats *get_or_create_entry(int64_t num)
   {
@@ -54,6 +102,51 @@ static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
           g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
           qemu_plugin_outs(out);
       }
+
+    if (do_log_writes && num == write_sysno) {
+        GByteArray *data = qemu_plugin_read_memory_vaddr(a2, a3);
+
+        g_autoptr(GString) out = g_string_new("");
+
+        size_t rows = (data->len % 16 == 0)
+            ? (data->len / 16)
+            : ((data->len / 16) + 1);
+        for (size_t row = 0; row < rows; row++) {
+            size_t len = (rows != data->len / 16 && row == rows - 1)
+                ? (data->len % 16)
+                : 16;
+            for (size_t i = 0; i < len; i++) {
+                g_string_append_printf(out, "%02x ",
+                    data->data[(row * 16) + i]);
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            for (size_t i = len; i < 16; i++) {
+                g_string_append(out, "   ");
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            g_string_append(out, " | ");
+            for (size_t i = 0; i < len; i++) {
+                g_string_append_printf(out,
+                    (data->data[(row * 16) + i] >= 0x21
+                        && data->data[(row * 16) + i] <= 0x7e)
+                    ? "%c"
+                    : ".", data->data[(row * 16) + i]);
Quoted 8 lines

tests/tcg/plugins/syscall.c static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index, · line 143

Quoted 6 lines
tests/tcg/plugins/syscall.c +6 -0
+                if (i != 0 && i % 4 == 0) {
+                    g_string_append(out, " ");
+                }
+            }
+            g_string_append(out, "\n");
+        }

Could you add a comment to show what is expected format? From the code, with all these loops and ternary expressions, it's not easy to follow.

Quoted 4 lines

Indeed. I will just generally simplify this

Thanks!

You're welcome.

Quoted 1 line

-Rowan

Pierrick