xref: /qemu/tests/tcg/plugins/insn.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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 qemu_plugin_u64 insn_count;
20 
21 static bool do_inline;
22 static bool do_size;
23 static bool do_trace;
24 static GArray *sizes;
25 
26 typedef struct {
27     uint64_t hits;
28     uint64_t last_hit;
29     uint64_t total_delta;
30 } MatchCount;
31 
32 typedef struct {
33     char *match_string;
34     struct qemu_plugin_scoreboard *counts; /* MatchCount */
35 } Match;
36 
37 static GArray *matches;
38 
39 typedef struct {
40     Match *match;
41     uint64_t vaddr;
42     uint64_t hits;
43     char *disas;
44 } Instruction;
45 
46 /* A hash table to hold matched instructions */
47 static GHashTable *match_insn_records;
48 static GMutex match_hash_lock;
49 
50 
51 static Instruction * get_insn_record(const char *disas, uint64_t vaddr, Match *m)
52 {
53     g_autofree char *str_hash = g_strdup_printf("%"PRIx64" %s", vaddr, disas);
54     Instruction *record;
55 
56     g_mutex_lock(&match_hash_lock);
57 
58     if (!match_insn_records) {
59         match_insn_records = g_hash_table_new(g_str_hash, g_str_equal);
60     }
61 
62     record = g_hash_table_lookup(match_insn_records, str_hash);
63 
64     if (!record) {
65         g_autoptr(GString) ts = g_string_new(str_hash);
66 
67         record = g_new0(Instruction, 1);
68         record->disas = g_strdup(disas);
69         record->vaddr = vaddr;
70         record->match = m;
71 
72         g_hash_table_insert(match_insn_records, str_hash, record);
73 
74         g_string_prepend(ts, "Created record for: ");
75         g_string_append(ts, "\n");
76         qemu_plugin_outs(ts->str);
77     }
78 
79     g_mutex_unlock(&match_hash_lock);
80 
81     return record;
82 }
83 
84 /*
85  * Initialise a new vcpu with reading the register list
86  */
87 static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index)
88 {
89     g_autoptr(GArray) reg_list = qemu_plugin_get_registers();
90     g_autoptr(GByteArray) reg_value = g_byte_array_new();
91 
92     if (reg_list) {
93         for (int i = 0; i < reg_list->len; i++) {
94             qemu_plugin_reg_descriptor *rd = &g_array_index(
95                 reg_list, qemu_plugin_reg_descriptor, i);
96             int count = qemu_plugin_read_register(rd->handle, reg_value);
97             g_assert(count > 0);
98         }
99     }
100 }
101 
102 
103 static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata)
104 {
105     qemu_plugin_u64_add(insn_count, cpu_index, 1);
106 }
107 
108 static void vcpu_insn_matched_exec_before(unsigned int cpu_index, void *udata)
109 {
110     Instruction *insn = (Instruction *) udata;
111     Match *insn_match = insn->match;
112     MatchCount *match = qemu_plugin_scoreboard_find(insn_match->counts,
113                                                     cpu_index);
114 
115     insn->hits++;
116 
117     uint64_t icount = qemu_plugin_u64_get(insn_count, cpu_index);
118     uint64_t delta = icount - match->last_hit;
119 
120     match->hits++;
121     match->total_delta += delta;
122     match->last_hit = icount;
123 
124     if (do_trace) {
125         g_autoptr(GString) ts = g_string_new("");
126         g_string_append_printf(ts, "0x%" PRIx64 ", '%s', %"PRId64 " hits",
127                                insn->vaddr, insn->disas, insn->hits);
128         g_string_append_printf(ts,
129                                " , cpu %u,"
130                                " %"PRId64" match hits,"
131                                " Δ+%"PRId64 " since last match,"
132                                " %"PRId64 " avg insns/match\n",
133                                cpu_index,
134                                match->hits, delta,
135                                match->total_delta / match->hits);
136 
137         qemu_plugin_outs(ts->str);
138     }
139 }
140 
141 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
142 {
143     size_t n = qemu_plugin_tb_n_insns(tb);
144     size_t i;
145 
146     for (i = 0; i < n; i++) {
147         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
148 
149         if (do_inline) {
150             qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
151                 insn, QEMU_PLUGIN_INLINE_ADD_U64, insn_count, 1);
152         } else {
153             qemu_plugin_register_vcpu_insn_exec_cb(
154                 insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS, NULL);
155         }
156 
157         if (do_size) {
158             size_t sz = qemu_plugin_insn_size(insn);
159             if (sz > sizes->len) {
160                 g_array_set_size(sizes, sz);
161             }
162             unsigned long *cnt = &g_array_index(sizes, unsigned long, sz);
163             (*cnt)++;
164         }
165 
166         /*
167          * If we are tracking certain instructions we will need more
168          * information about the instruction which we also need to
169          * save if there is a hit.
170          *
171          * We only want one record for each occurrence of the matched
172          * instruction.
173          */
174         if (matches->len) {
175             char *insn_disas = qemu_plugin_insn_disas(insn);
176             for (int j = 0; j < matches->len; j++) {
177                 Match *m = &g_array_index(matches, Match, j);
178                 if (g_str_has_prefix(insn_disas, m->match_string)) {
179                     Instruction *rec = get_insn_record(insn_disas,
180                                                        qemu_plugin_insn_vaddr(insn),
181                                                        m);
182 
183                     qemu_plugin_register_vcpu_insn_exec_cb(
184                         insn, vcpu_insn_matched_exec_before,
185                         QEMU_PLUGIN_CB_NO_REGS, rec);
186                 }
187             }
188             g_free(insn_disas);
189         }
190     }
191 }
192 
193 static void plugin_exit(qemu_plugin_id_t id, void *p)
194 {
195     g_autoptr(GString) out = g_string_new(NULL);
196     int i;
197 
198     if (do_size) {
199         for (i = 0; i <= sizes->len; i++) {
200             unsigned long *cnt = &g_array_index(sizes, unsigned long, i);
201             if (*cnt) {
202                 g_string_append_printf(out,
203                                        "len %d bytes: %ld insns\n", i, *cnt);
204             }
205         }
206     } else {
207         for (i = 0; i < qemu_plugin_num_vcpus(); i++) {
208             g_string_append_printf(out, "cpu %d insns: %" PRIu64 "\n",
209                                    i, qemu_plugin_u64_get(insn_count, i));
210         }
211         g_string_append_printf(out, "total insns: %" PRIu64 "\n",
212                                qemu_plugin_u64_sum(insn_count));
213     }
214     qemu_plugin_outs(out->str);
215     qemu_plugin_scoreboard_free(insn_count.score);
216 
217     g_mutex_lock(&match_hash_lock);
218 
219     for (i = 0; i < matches->len; ++i) {
220         Match *m = &g_array_index(matches, Match, i);
221         GHashTableIter iter;
222         Instruction *record;
223         qemu_plugin_u64 hit_e = qemu_plugin_scoreboard_u64_in_struct(m->counts, MatchCount, hits);
224         uint64_t hits = qemu_plugin_u64_sum(hit_e);
225 
226         g_string_printf(out, "Match: %s, hits %"PRId64"\n", m->match_string, hits);
227         qemu_plugin_outs(out->str);
228 
229         g_hash_table_iter_init(&iter, match_insn_records);
230         while (g_hash_table_iter_next(&iter, NULL, (void **)&record)) {
231             if (record->match == m) {
232                 g_string_printf(out,
233                                 "  %"PRIx64": %s (hits %"PRId64")\n",
234                                 record->vaddr,
235                                 record->disas,
236                                 record->hits);
237                 qemu_plugin_outs(out->str);
238             }
239         }
240 
241         g_free(m->match_string);
242         qemu_plugin_scoreboard_free(m->counts);
243     }
244 
245     g_mutex_unlock(&match_hash_lock);
246 
247     g_array_free(matches, TRUE);
248     g_array_free(sizes, TRUE);
249 }
250 
251 
252 /* Add a match to the array of matches */
253 static void parse_match(char *match)
254 {
255     Match new_match = {
256         .match_string = g_strdup(match),
257         .counts = qemu_plugin_scoreboard_new(sizeof(MatchCount)) };
258     g_array_append_val(matches, new_match);
259 }
260 
261 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
262                                            const qemu_info_t *info,
263                                            int argc, char **argv)
264 {
265     matches = g_array_new(false, true, sizeof(Match));
266     /* null terminated so 0 is not a special case */
267     sizes = g_array_new(true, true, sizeof(unsigned long));
268 
269     for (int i = 0; i < argc; i++) {
270         char *opt = argv[i];
271         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
272         if (g_strcmp0(tokens[0], "inline") == 0) {
273             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
274                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
275                 return -1;
276             }
277         } else if (g_strcmp0(tokens[0], "sizes") == 0) {
278             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_size)) {
279                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
280                 return -1;
281             }
282         } else if (g_strcmp0(tokens[0], "match") == 0) {
283             parse_match(tokens[1]);
284         } else if (g_strcmp0(tokens[0], "trace") == 0) {
285             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_trace)) {
286                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
287                 return -1;
288             }
289         } else {
290             fprintf(stderr, "option parsing failed: %s\n", opt);
291             return -1;
292         }
293     }
294 
295     insn_count = qemu_plugin_scoreboard_u64(
296         qemu_plugin_scoreboard_new(sizeof(uint64_t)));
297 
298     /* Register init, translation block and exit callbacks */
299     qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
300     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
301     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
302     return 0;
303 }
304