1 /*
2 * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
3 *
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7 #include <inttypes.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <glib.h>
14
15 #include <qemu-plugin.h>
16
17 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
18
19 static uint64_t insn_count;
20 static bool do_inline;
21
vcpu_insn_exec_before(unsigned int cpu_index,void * udata)22 static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata)
23 {
24 insn_count++;
25 }
26
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)27 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
28 {
29 size_t n = qemu_plugin_tb_n_insns(tb);
30 size_t i;
31
32 for (i = 0; i < n; i++) {
33 struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
34
35 if (do_inline) {
36 qemu_plugin_register_vcpu_insn_exec_inline(
37 insn, QEMU_PLUGIN_INLINE_ADD_U64, &insn_count, 1);
38 } else {
39 qemu_plugin_register_vcpu_insn_exec_cb(
40 insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS, NULL);
41 }
42 }
43 }
44
plugin_exit(qemu_plugin_id_t id,void * p)45 static void plugin_exit(qemu_plugin_id_t id, void *p)
46 {
47 g_autofree gchar *out = g_strdup_printf("insns: %" PRIu64 "\n", insn_count);
48 qemu_plugin_outs(out);
49 }
50
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)51 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
52 const qemu_info_t *info,
53 int argc, char **argv)
54 {
55 if (argc && !strcmp(argv[0], "inline")) {
56 do_inline = true;
57 }
58
59 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
60 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
61 return 0;
62 }
63