xref: /qemu/tests/plugin/insn.c (revision b83a80e8)
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 static bool do_size;
22 static GArray *sizes;
23 
24 static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata)
25 {
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);
32     }
33     last_pc = this_pc;
34     insn_count++;
35 }
36 
37 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
38 {
39     size_t n = qemu_plugin_tb_n_insns(tb);
40     size_t i;
41 
42     for (i = 0; i < n; i++) {
43         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
44 
45         if (do_inline) {
46             qemu_plugin_register_vcpu_insn_exec_inline(
47                 insn, QEMU_PLUGIN_INLINE_ADD_U64, &insn_count, 1);
48         } else {
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));
53         }
54 
55         if (do_size) {
56             size_t sz = qemu_plugin_insn_size(insn);
57             if (sz > sizes->len) {
58                 g_array_set_size(sizes, sz);
59             }
60             unsigned long *cnt = &g_array_index(sizes, unsigned long, sz);
61             (*cnt)++;
62         }
63     }
64 }
65 
66 static void plugin_exit(qemu_plugin_id_t id, void *p)
67 {
68     g_autoptr(GString) out = g_string_new(NULL);
69 
70     if (do_size) {
71         int i;
72         for (i = 0; i <= sizes->len; i++) {
73             unsigned long *cnt = &g_array_index(sizes, unsigned long, i);
74             if (*cnt) {
75                 g_string_append_printf(out,
76                                        "len %d bytes: %ld insns\n", i, *cnt);
77             }
78         }
79     } else {
80         g_string_append_printf(out, "insns: %" PRIu64 "\n", insn_count);
81     }
82     qemu_plugin_outs(out->str);
83 }
84 
85 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
86                                            const qemu_info_t *info,
87                                            int argc, char **argv)
88 {
89     for (int i = 0; i < argc; i++) {
90         char *opt = argv[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);
95                 return -1;
96             }
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);
100                 return -1;
101             }
102         } else {
103             fprintf(stderr, "option parsing failed: %s\n", opt);
104             return -1;
105         }
106     }
107 
108     if (do_size) {
109         sizes = g_array_new(true, true, sizeof(unsigned long));
110     }
111 
112     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
113     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
114     return 0;
115 }
116