aboutsummaryrefslogtreecommitdiffstats
path: root/tests/plugin
diff options
context:
space:
mode:
Diffstat (limited to 'tests/plugin')
-rw-r--r--tests/plugin/bb.c144
-rw-r--r--tests/plugin/empty.c32
-rw-r--r--tests/plugin/insn.c115
-rw-r--r--tests/plugin/mem.c123
-rw-r--r--tests/plugin/meson.build7
-rw-r--r--tests/plugin/syscall.c144
6 files changed, 565 insertions, 0 deletions
diff --git a/tests/plugin/bb.c b/tests/plugin/bb.c
new file mode 100644
index 000000000..7d470a101
--- /dev/null
+++ b/tests/plugin/bb.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+typedef struct {
+ GMutex lock;
+ int index;
+ uint64_t bb_count;
+ uint64_t insn_count;
+} CPUCount;
+
+/* Used by the inline & linux-user counts */
+static bool do_inline;
+static CPUCount inline_count;
+
+/* Dump running CPU total on idle? */
+static bool idle_report;
+static GPtrArray *counts;
+static int max_cpus;
+
+static void gen_one_cpu_report(CPUCount *count, GString *report)
+{
+ if (count->bb_count) {
+ g_string_append_printf(report, "CPU%d: "
+ "bb's: %" PRIu64", insns: %" PRIu64 "\n",
+ count->index,
+ count->bb_count, count->insn_count);
+ }
+}
+
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+ g_autoptr(GString) report = g_string_new("");
+
+ if (do_inline || !max_cpus) {
+ g_string_printf(report, "bb's: %" PRIu64", insns: %" PRIu64 "\n",
+ inline_count.bb_count, inline_count.insn_count);
+ } else {
+ g_ptr_array_foreach(counts, (GFunc) gen_one_cpu_report, report);
+ }
+ qemu_plugin_outs(report->str);
+}
+
+static void vcpu_idle(qemu_plugin_id_t id, unsigned int cpu_index)
+{
+ CPUCount *count = g_ptr_array_index(counts, cpu_index);
+ g_autoptr(GString) report = g_string_new("");
+ gen_one_cpu_report(count, report);
+
+ if (report->len > 0) {
+ g_string_prepend(report, "Idling ");
+ qemu_plugin_outs(report->str);
+ }
+}
+
+static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
+{
+ CPUCount *count = max_cpus ?
+ g_ptr_array_index(counts, cpu_index) : &inline_count;
+
+ uintptr_t n_insns = (uintptr_t)udata;
+ g_mutex_lock(&count->lock);
+ count->insn_count += n_insns;
+ count->bb_count++;
+ g_mutex_unlock(&count->lock);
+}
+
+static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
+{
+ size_t n_insns = qemu_plugin_tb_n_insns(tb);
+
+ if (do_inline) {
+ qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64,
+ &inline_count.bb_count, 1);
+ qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64,
+ &inline_count.insn_count,
+ n_insns);
+ } else {
+ qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
+ QEMU_PLUGIN_CB_NO_REGS,
+ (void *)n_insns);
+ }
+}
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+ const qemu_info_t *info,
+ int argc, char **argv)
+{
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ char *opt = argv[i];
+ g_autofree char **tokens = g_strsplit(opt, "=", 2);
+ if (g_strcmp0(tokens[0], "inline") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else if (g_strcmp0(tokens[0], "idle") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &idle_report)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else {
+ fprintf(stderr, "option parsing failed: %s\n", opt);
+ return -1;
+ }
+ }
+
+ if (info->system_emulation && !do_inline) {
+ max_cpus = info->system.max_vcpus;
+ counts = g_ptr_array_new();
+ for (i = 0; i < max_cpus; i++) {
+ CPUCount *count = g_new0(CPUCount, 1);
+ g_mutex_init(&count->lock);
+ count->index = i;
+ g_ptr_array_add(counts, count);
+ }
+ } else if (!do_inline) {
+ g_mutex_init(&inline_count.lock);
+ }
+
+ if (idle_report) {
+ qemu_plugin_register_vcpu_idle_cb(id, vcpu_idle);
+ }
+
+ qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+ qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
+ return 0;
+}
diff --git a/tests/plugin/empty.c b/tests/plugin/empty.c
new file mode 100644
index 000000000..8fa6bacd9
--- /dev/null
+++ b/tests/plugin/empty.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+/*
+ * Empty TB translation callback.
+ * This allows us to measure the overhead of injecting and then
+ * removing empty instrumentation.
+ */
+static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
+{ }
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+ const qemu_info_t *info,
+ int argc, char **argv)
+{
+ qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+ return 0;
+}
diff --git a/tests/plugin/insn.c b/tests/plugin/insn.c
new file mode 100644
index 000000000..d229fdc00
--- /dev/null
+++ b/tests/plugin/insn.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+static uint64_t insn_count;
+static bool do_inline;
+static bool do_size;
+static GArray *sizes;
+
+static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata)
+{
+ static uint64_t last_pc;
+ uint64_t this_pc = GPOINTER_TO_UINT(udata);
+ if (this_pc == last_pc) {
+ g_autofree gchar *out = g_strdup_printf("detected repeat execution @ 0x%"
+ PRIx64 "\n", this_pc);
+ qemu_plugin_outs(out);
+ }
+ last_pc = this_pc;
+ insn_count++;
+}
+
+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;
+
+ for (i = 0; i < n; i++) {
+ struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
+
+ if (do_inline) {
+ qemu_plugin_register_vcpu_insn_exec_inline(
+ insn, QEMU_PLUGIN_INLINE_ADD_U64, &insn_count, 1);
+ } else {
+ uint64_t vaddr = qemu_plugin_insn_vaddr(insn);
+ qemu_plugin_register_vcpu_insn_exec_cb(
+ insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS,
+ GUINT_TO_POINTER(vaddr));
+ }
+
+ if (do_size) {
+ size_t sz = qemu_plugin_insn_size(insn);
+ if (sz > sizes->len) {
+ g_array_set_size(sizes, sz);
+ }
+ unsigned long *cnt = &g_array_index(sizes, unsigned long, sz);
+ (*cnt)++;
+ }
+ }
+}
+
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+ g_autoptr(GString) out = g_string_new(NULL);
+
+ if (do_size) {
+ int i;
+ for (i = 0; i <= sizes->len; i++) {
+ unsigned long *cnt = &g_array_index(sizes, unsigned long, i);
+ if (*cnt) {
+ g_string_append_printf(out,
+ "len %d bytes: %ld insns\n", i, *cnt);
+ }
+ }
+ } else {
+ g_string_append_printf(out, "insns: %" PRIu64 "\n", insn_count);
+ }
+ qemu_plugin_outs(out->str);
+}
+
+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_autofree char **tokens = g_strsplit(opt, "=", 2);
+ if (g_strcmp0(tokens[0], "inline") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else if (g_strcmp0(tokens[0], "sizes") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_size)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else {
+ fprintf(stderr, "option parsing failed: %s\n", opt);
+ return -1;
+ }
+ }
+
+ if (do_size) {
+ sizes = g_array_new(true, true, sizeof(unsigned long));
+ }
+
+ qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+ qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
+ return 0;
+}
diff --git a/tests/plugin/mem.c b/tests/plugin/mem.c
new file mode 100644
index 000000000..4570f7d81
--- /dev/null
+++ b/tests/plugin/mem.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+static uint64_t inline_mem_count;
+static uint64_t cb_mem_count;
+static uint64_t io_count;
+static bool do_inline, do_callback;
+static bool do_haddr;
+static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
+
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+ g_autoptr(GString) out = g_string_new("");
+
+ if (do_inline) {
+ g_string_printf(out, "inline mem accesses: %" PRIu64 "\n", inline_mem_count);
+ }
+ if (do_callback) {
+ g_string_append_printf(out, "callback mem accesses: %" PRIu64 "\n", cb_mem_count);
+ }
+ if (do_haddr) {
+ g_string_append_printf(out, "io accesses: %" PRIu64 "\n", io_count);
+ }
+ qemu_plugin_outs(out->str);
+}
+
+static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
+ uint64_t vaddr, void *udata)
+{
+ if (do_haddr) {
+ struct qemu_plugin_hwaddr *hwaddr;
+ hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr);
+ if (qemu_plugin_hwaddr_is_io(hwaddr)) {
+ io_count++;
+ } else {
+ cb_mem_count++;
+ }
+ } else {
+ cb_mem_count++;
+ }
+}
+
+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;
+
+ for (i = 0; i < n; i++) {
+ struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
+
+ if (do_inline) {
+ qemu_plugin_register_vcpu_mem_inline(insn, rw,
+ QEMU_PLUGIN_INLINE_ADD_U64,
+ &inline_mem_count, 1);
+ }
+ if (do_callback) {
+ qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
+ QEMU_PLUGIN_CB_NO_REGS,
+ rw, NULL);
+ }
+ }
+}
+
+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_autofree char **tokens = g_strsplit(opt, "=", 2);
+
+ if (g_strcmp0(tokens[0], "haddr") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_haddr)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else if (g_strcmp0(tokens[0], "track") == 0) {
+ if (g_strcmp0(tokens[1], "r") == 0) {
+ rw = QEMU_PLUGIN_MEM_R;
+ } else if (g_strcmp0(tokens[1], "w") == 0) {
+ rw = QEMU_PLUGIN_MEM_W;
+ } else if (g_strcmp0(tokens[1], "rw") == 0) {
+ rw = QEMU_PLUGIN_MEM_RW;
+ } else {
+ fprintf(stderr, "invaild value for argument track: %s\n", opt);
+ return -1;
+ }
+ } else if (g_strcmp0(tokens[0], "inline") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else if (g_strcmp0(tokens[0], "callback") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_callback)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ return -1;
+ }
+ } else {
+ fprintf(stderr, "option parsing failed: %s\n", opt);
+ return -1;
+ }
+ }
+
+ qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+ qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
+ return 0;
+}
diff --git a/tests/plugin/meson.build b/tests/plugin/meson.build
new file mode 100644
index 000000000..2bbfc4b19
--- /dev/null
+++ b/tests/plugin/meson.build
@@ -0,0 +1,7 @@
+t = []
+foreach i : ['bb', 'empty', 'insn', 'mem', 'syscall']
+ t += shared_module(i, files(i + '.c'),
+ include_directories: '../../include/qemu',
+ dependencies: glib)
+endforeach
+alias_target('test-plugins', t)
diff --git a/tests/plugin/syscall.c b/tests/plugin/syscall.c
new file mode 100644
index 000000000..96040c578
--- /dev/null
+++ b/tests/plugin/syscall.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2020, Matthias Weckbecker <matthias@weckbecker.name>
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+typedef struct {
+ int64_t num;
+ int64_t calls;
+ int64_t errors;
+} SyscallStats;
+
+static GMutex lock;
+static GHashTable *statistics;
+
+static SyscallStats *get_or_create_entry(int64_t num)
+{
+ SyscallStats *entry =
+ (SyscallStats *) g_hash_table_lookup(statistics, GINT_TO_POINTER(num));
+
+ if (!entry) {
+ entry = g_new0(SyscallStats, 1);
+ entry->num = num;
+ g_hash_table_insert(statistics, GINT_TO_POINTER(num), (gpointer) entry);
+ }
+
+ return entry;
+}
+
+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,
+ uint64_t a6, uint64_t a7, uint64_t a8)
+{
+ if (statistics) {
+ SyscallStats *entry;
+ g_mutex_lock(&lock);
+ entry = get_or_create_entry(num);
+ entry->calls++;
+ g_mutex_unlock(&lock);
+ } else {
+ g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
+ qemu_plugin_outs(out);
+ }
+}
+
+static void vcpu_syscall_ret(qemu_plugin_id_t id, unsigned int vcpu_idx,
+ int64_t num, int64_t ret)
+{
+ if (statistics) {
+ SyscallStats *entry;
+
+ g_mutex_lock(&lock);
+ /* Should always return an existent entry. */
+ entry = get_or_create_entry(num);
+ if (ret < 0) {
+ entry->errors++;
+ }
+ g_mutex_unlock(&lock);
+ } else {
+ g_autofree gchar *out = g_strdup_printf(
+ "syscall #%" PRIi64 " returned -> %" PRIi64 "\n", num, ret);
+ qemu_plugin_outs(out);
+ }
+}
+
+static void print_entry(gpointer val, gpointer user_data)
+{
+ SyscallStats *entry = (SyscallStats *) val;
+ int64_t syscall_num = entry->num;
+ g_autofree gchar *out = g_strdup_printf(
+ "%-13" PRIi64 "%-6" PRIi64 " %" PRIi64 "\n",
+ syscall_num, entry->calls, entry->errors);
+ qemu_plugin_outs(out);
+}
+
+static gint comp_func(gconstpointer ea, gconstpointer eb)
+{
+ SyscallStats *ent_a = (SyscallStats *) ea;
+ SyscallStats *ent_b = (SyscallStats *) eb;
+
+ return ent_a->calls > ent_b->calls ? -1 : 1;
+}
+
+/* ************************************************************************* */
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+ if (!statistics) {
+ return;
+ }
+
+ g_mutex_lock(&lock);
+ GList *entries = g_hash_table_get_values(statistics);
+ entries = g_list_sort(entries, comp_func);
+ qemu_plugin_outs("syscall no. calls errors\n");
+
+ g_list_foreach(entries, print_entry, NULL);
+
+ g_list_free(entries);
+ g_hash_table_destroy(statistics);
+ g_mutex_unlock(&lock);
+}
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+ const qemu_info_t *info,
+ int argc, char **argv)
+{
+ bool do_print = false;
+
+ for (int i = 0; i < argc; i++) {
+ char *opt = argv[i];
+ g_autofree char **tokens = g_strsplit(opt, "=", 2);
+
+ if (g_strcmp0(tokens[0], "print") == 0) {
+ if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_print)) {
+ fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
+ }
+ } else {
+ fprintf(stderr, "unsupported argument: %s\n", argv[i]);
+ return -1;
+ }
+ }
+
+ if (!do_print) {
+ statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
+ }
+
+ 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);
+ return 0;
+}