2 * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
15 #include <qemu-plugin.h>
17 QEMU_PLUGIN_EXPORT
int qemu_plugin_version
= QEMU_PLUGIN_VERSION
;
19 static uint64_t insn_count
;
20 static bool do_inline
;
24 static void vcpu_insn_exec_before(unsigned int cpu_index
, void *udata
)
26 static uint64_t last_pc
;
27 uint64_t this_pc
= GPOINTER_TO_UINT(udata
);
28 if (this_pc
== last_pc
) {
29 g_autofree gchar
*out
= g_strdup_printf("detected repeat execution @ 0x%"
30 PRIx64
"\n", this_pc
);
31 qemu_plugin_outs(out
);
37 static void vcpu_tb_trans(qemu_plugin_id_t id
, struct qemu_plugin_tb
*tb
)
39 size_t n
= qemu_plugin_tb_n_insns(tb
);
42 for (i
= 0; i
< n
; i
++) {
43 struct qemu_plugin_insn
*insn
= qemu_plugin_tb_get_insn(tb
, i
);
46 qemu_plugin_register_vcpu_insn_exec_inline(
47 insn
, QEMU_PLUGIN_INLINE_ADD_U64
, &insn_count
, 1);
49 uint64_t vaddr
= qemu_plugin_insn_vaddr(insn
);
50 qemu_plugin_register_vcpu_insn_exec_cb(
51 insn
, vcpu_insn_exec_before
, QEMU_PLUGIN_CB_NO_REGS
,
52 GUINT_TO_POINTER(vaddr
));
56 size_t sz
= qemu_plugin_insn_size(insn
);
57 if (sz
> sizes
->len
) {
58 g_array_set_size(sizes
, sz
);
60 unsigned long *cnt
= &g_array_index(sizes
, unsigned long, sz
);
66 static void plugin_exit(qemu_plugin_id_t id
, void *p
)
68 g_autoptr(GString
) out
= g_string_new(NULL
);
72 for (i
= 0; i
<= sizes
->len
; i
++) {
73 unsigned long *cnt
= &g_array_index(sizes
, unsigned long, i
);
75 g_string_append_printf(out
,
76 "len %d bytes: %ld insns\n", i
, *cnt
);
80 g_string_append_printf(out
, "insns: %" PRIu64
"\n", insn_count
);
82 qemu_plugin_outs(out
->str
);
85 QEMU_PLUGIN_EXPORT
int qemu_plugin_install(qemu_plugin_id_t id
,
86 const qemu_info_t
*info
,
87 int argc
, char **argv
)
89 for (int i
= 0; i
< argc
; i
++) {
91 g_autofree
char **tokens
= g_strsplit(opt
, "=", 2);
92 if (g_strcmp0(tokens
[0], "inline") == 0) {
93 if (!qemu_plugin_bool_parse(tokens
[0], tokens
[1], &do_inline
)) {
94 fprintf(stderr
, "boolean argument parsing failed: %s\n", opt
);
97 } else if (g_strcmp0(tokens
[0], "sizes") == 0) {
98 if (!qemu_plugin_bool_parse(tokens
[0], tokens
[1], &do_size
)) {
99 fprintf(stderr
, "boolean argument parsing failed: %s\n", opt
);
103 fprintf(stderr
, "option parsing failed: %s\n", opt
);
109 sizes
= g_array_new(true, true, sizeof(unsigned long));
112 qemu_plugin_register_vcpu_tb_trans_cb(id
, vcpu_tb_trans
);
113 qemu_plugin_register_atexit_cb(id
, plugin_exit
, NULL
);