diff mbox series

[v2,2/2] plugins: add option to dump write argument to syscall plugin

Message ID 20240827021847.218390-3-rowanbhart@gmail.com (mailing list archive)
State New
Headers show
Series plugins: add plugin API to read guest memory | expand

Commit Message

Rowan Hart Aug. 27, 2024, 2:18 a.m. UTC
Signed-off-by: Rowan Hart <rowanbhart@gmail.com>
---
 docs/about/emulation.rst    |  14 ++++-
 tests/tcg/plugins/syscall.c | 117 ++++++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+), 1 deletion(-)

Comments

Pierrick Bouvier Aug. 27, 2024, 4:01 p.m. UTC | #1
On 8/26/24 19:18, Rowan Hart wrote:
> Signed-off-by: Rowan Hart <rowanbhart@gmail.com>
> ---
>   docs/about/emulation.rst    |  14 ++++-
>   tests/tcg/plugins/syscall.c | 117 ++++++++++++++++++++++++++++++++++++
>   2 files changed, 130 insertions(+), 1 deletion(-)
> 
> diff --git a/docs/about/emulation.rst b/docs/about/emulation.rst
> index eea1261baa..e85d494ff0 100644
> --- a/docs/about/emulation.rst
> +++ b/docs/about/emulation.rst
> @@ -388,6 +388,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 +790,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.
> -
> diff --git a/tests/tcg/plugins/syscall.c b/tests/tcg/plugins/syscall.c
> index 72e1a5bf90..7c92f798b5 100644
> --- a/tests/tcg/plugins/syscall.c
> +++ b/tests/tcg/plugins/syscall.c
> @@ -22,8 +22,57 @@ 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 GByteArray *memory_buffer;
> +static bool do_log_writes;
> +static int64_t write_sysno = -1;
>   
>   static SyscallStats *get_or_create_entry(int64_t num)
>   {
> @@ -39,6 +88,44 @@ static SyscallStats *get_or_create_entry(int64_t num)
>       return entry;
>   }
>   
> +/*
> + * Hex-dump a GByteArray to the QEMU plugin output in the format:
> + * 61 63 63 65 6c 09 09 20 20 20 66 70 75 09 09 09  | accel.....fpu...
> + * 20 6d 6f 64 75 6c 65 2d 63 6f 6d 6d 6f 6e 2e 63  | .module-common.c
> + */
> +static void hexdump(const GByteArray *data)
> +{
> +    g_autoptr(GString) out = g_string_new("");
> +
> +    for (guint index = 0; index < data->len; index += 16) {
> +        for (guint col = 0; col < 16; col++) {
> +            if (index + col < data->len) {
> +                g_string_append_printf(out, "%02x ", data->data[index + col]);
> +            } else {
> +                g_string_append(out, "   ");
> +            }
> +        }
> +
> +        g_string_append(out, " | ");
> +
> +        for (guint col = 0; col < 16; col++) {
> +            if (index + col >= data->len) {
> +                break;
> +            }
> +
> +            if (g_ascii_isgraph(data->data[index + col])) {
> +                g_string_append_printf(out, "%c", data->data[index + col]);
> +            } else {
> +                g_string_append(out, ".");
> +            }
> +        }
> +
> +        g_string_append(out, "\n");
> +    }
> +
> +    qemu_plugin_outs(out->str);
> +}
> +
>   static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
>                            int64_t num, uint64_t a1, uint64_t a2,
>                            uint64_t a3, uint64_t a4, uint64_t a5,
> @@ -54,6 +141,14 @@ 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) {
> +        if (qemu_plugin_read_memory_vaddr(a2, memory_buffer, a3)) {
> +            hexdump(memory_buffer);
> +        } else {
> +            fprintf(stderr, "Error reading memory from vaddr %lu\n", a2);
> +        }
> +    }
>   }
>   
>   static void vcpu_syscall_ret(qemu_plugin_id_t id, unsigned int vcpu_idx,
> @@ -127,6 +222,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 +236,24 @@ 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;
> +            }
> +        }
> +
> +        if (write_sysno == -1) {
> +            fprintf(stderr, "write syscall number not found\n");
> +            return -1;
> +        }
> +
> +        memory_buffer = g_byte_array_new();
> +    }
> +
>       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);

Works as expected.

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Tested-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
diff mbox series

Patch

diff --git a/docs/about/emulation.rst b/docs/about/emulation.rst
index eea1261baa..e85d494ff0 100644
--- a/docs/about/emulation.rst
+++ b/docs/about/emulation.rst
@@ -388,6 +388,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 +790,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.
-
diff --git a/tests/tcg/plugins/syscall.c b/tests/tcg/plugins/syscall.c
index 72e1a5bf90..7c92f798b5 100644
--- a/tests/tcg/plugins/syscall.c
+++ b/tests/tcg/plugins/syscall.c
@@ -22,8 +22,57 @@  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 GByteArray *memory_buffer;
+static bool do_log_writes;
+static int64_t write_sysno = -1;
 
 static SyscallStats *get_or_create_entry(int64_t num)
 {
@@ -39,6 +88,44 @@  static SyscallStats *get_or_create_entry(int64_t num)
     return entry;
 }
 
+/*
+ * Hex-dump a GByteArray to the QEMU plugin output in the format:
+ * 61 63 63 65 6c 09 09 20 20 20 66 70 75 09 09 09  | accel.....fpu...
+ * 20 6d 6f 64 75 6c 65 2d 63 6f 6d 6d 6f 6e 2e 63  | .module-common.c
+ */
+static void hexdump(const GByteArray *data)
+{
+    g_autoptr(GString) out = g_string_new("");
+
+    for (guint index = 0; index < data->len; index += 16) {
+        for (guint col = 0; col < 16; col++) {
+            if (index + col < data->len) {
+                g_string_append_printf(out, "%02x ", data->data[index + col]);
+            } else {
+                g_string_append(out, "   ");
+            }
+        }
+
+        g_string_append(out, " | ");
+
+        for (guint col = 0; col < 16; col++) {
+            if (index + col >= data->len) {
+                break;
+            }
+
+            if (g_ascii_isgraph(data->data[index + col])) {
+                g_string_append_printf(out, "%c", data->data[index + col]);
+            } else {
+                g_string_append(out, ".");
+            }
+        }
+
+        g_string_append(out, "\n");
+    }
+
+    qemu_plugin_outs(out->str);
+}
+
 static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
                          int64_t num, uint64_t a1, uint64_t a2,
                          uint64_t a3, uint64_t a4, uint64_t a5,
@@ -54,6 +141,14 @@  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) {
+        if (qemu_plugin_read_memory_vaddr(a2, memory_buffer, a3)) {
+            hexdump(memory_buffer);
+        } else {
+            fprintf(stderr, "Error reading memory from vaddr %lu\n", a2);
+        }
+    }
 }
 
 static void vcpu_syscall_ret(qemu_plugin_id_t id, unsigned int vcpu_idx,
@@ -127,6 +222,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 +236,24 @@  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;
+            }
+        }
+
+        if (write_sysno == -1) {
+            fprintf(stderr, "write syscall number not found\n");
+            return -1;
+        }
+
+        memory_buffer = g_byte_array_new();
+    }
+
     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);