xref: /openbmc/qemu/contrib/plugins/execlog.c (revision c079d3a31e45093286c65f8ca5350beb3a4404a9)
1 /*
2  * Copyright (C) 2021, Alexandre Iooss <erdnaxe@crans.org>
3  *
4  * Log instruction execution with memory access and register changes
5  *
6  * License: GNU GPL, version 2 or later.
7  *   See the COPYING file in the top-level directory.
8  */
9 #include <glib.h>
10 #include <inttypes.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 
16 #include <qemu-plugin.h>
17 
18 typedef struct {
19     struct qemu_plugin_register *handle;
20     GByteArray *last;
21     GByteArray *new;
22     const char *name;
23 } Register;
24 
25 typedef struct CPU {
26     /* Store last executed instruction on each vCPU as a GString */
27     GString *last_exec;
28     /* Ptr array of Register */
29     GPtrArray *registers;
30 } CPU;
31 
32 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
33 
34 static GArray *cpus;
35 static GRWLock expand_array_lock;
36 
37 static GPtrArray *imatches;
38 static GArray *amatches;
39 static GPtrArray *rmatches;
40 static bool disas_assist;
41 static GMutex add_reg_name_lock;
42 static GPtrArray *all_reg_names;
43 
get_cpu(int vcpu_index)44 static CPU *get_cpu(int vcpu_index)
45 {
46     CPU *c;
47     g_rw_lock_reader_lock(&expand_array_lock);
48     c = &g_array_index(cpus, CPU, vcpu_index);
49     g_rw_lock_reader_unlock(&expand_array_lock);
50 
51     return c;
52 }
53 
54 /**
55  * Add memory read or write information to current instruction log
56  */
vcpu_mem(unsigned int cpu_index,qemu_plugin_meminfo_t info,uint64_t vaddr,void * udata)57 static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t info,
58                      uint64_t vaddr, void *udata)
59 {
60     CPU *c = get_cpu(cpu_index);
61     GString *s = c->last_exec;
62 
63     /* Find vCPU in array */
64 
65     /* Indicate type of memory access */
66     if (qemu_plugin_mem_is_store(info)) {
67         g_string_append(s, ", store");
68     } else {
69         g_string_append(s, ", load");
70     }
71 
72     /* If full system emulation log physical address and device name */
73     struct qemu_plugin_hwaddr *hwaddr = qemu_plugin_get_hwaddr(info, vaddr);
74     if (hwaddr) {
75         uint64_t addr = qemu_plugin_hwaddr_phys_addr(hwaddr);
76         const char *name = qemu_plugin_hwaddr_device_name(hwaddr);
77         g_string_append_printf(s, ", 0x%08"PRIx64", %s", addr, name);
78     } else {
79         g_string_append_printf(s, ", 0x%08"PRIx64, vaddr);
80     }
81 }
82 
83 /**
84  * Log instruction execution, outputting the last one.
85  *
86  * vcpu_insn_exec() is a copy and paste of vcpu_insn_exec_with_regs()
87  * without the checking of register values when we've attempted to
88  * optimise with disas_assist.
89  */
insn_check_regs(CPU * cpu)90 static void insn_check_regs(CPU *cpu)
91 {
92     for (int n = 0; n < cpu->registers->len; n++) {
93         Register *reg = cpu->registers->pdata[n];
94         int sz;
95 
96         g_byte_array_set_size(reg->new, 0);
97         sz = qemu_plugin_read_register(reg->handle, reg->new);
98         g_assert(sz == reg->last->len);
99 
100         if (memcmp(reg->last->data, reg->new->data, sz)) {
101             GByteArray *temp = reg->last;
102             g_string_append_printf(cpu->last_exec, ", %s -> 0x", reg->name);
103             /* TODO: handle BE properly */
104             for (int i = sz - 1; i >= 0; i--) {
105                 g_string_append_printf(cpu->last_exec, "%02x",
106                                        reg->new->data[i]);
107             }
108             reg->last = reg->new;
109             reg->new = temp;
110         }
111     }
112 }
113 
114 /* Log last instruction while checking registers */
vcpu_insn_exec_with_regs(unsigned int cpu_index,void * udata)115 static void vcpu_insn_exec_with_regs(unsigned int cpu_index, void *udata)
116 {
117     CPU *cpu = get_cpu(cpu_index);
118 
119     /* Print previous instruction in cache */
120     if (cpu->last_exec->len) {
121         if (cpu->registers) {
122             insn_check_regs(cpu);
123         }
124 
125         qemu_plugin_outs(cpu->last_exec->str);
126         qemu_plugin_outs("\n");
127     }
128 
129     /* Store new instruction in cache */
130     /* vcpu_mem will add memory access information to last_exec */
131     g_string_printf(cpu->last_exec, "%u, ", cpu_index);
132     g_string_append(cpu->last_exec, (char *)udata);
133 }
134 
135 /* Log last instruction while checking registers, ignore next */
vcpu_insn_exec_only_regs(unsigned int cpu_index,void * udata)136 static void vcpu_insn_exec_only_regs(unsigned int cpu_index, void *udata)
137 {
138     CPU *cpu = get_cpu(cpu_index);
139 
140     /* Print previous instruction in cache */
141     if (cpu->last_exec->len) {
142         if (cpu->registers) {
143             insn_check_regs(cpu);
144         }
145 
146         qemu_plugin_outs(cpu->last_exec->str);
147         qemu_plugin_outs("\n");
148     }
149 
150     /* reset */
151     cpu->last_exec->len = 0;
152 }
153 
154 /* Log last instruction without checking regs, setup next */
vcpu_insn_exec(unsigned int cpu_index,void * udata)155 static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
156 {
157     CPU *cpu = get_cpu(cpu_index);
158 
159     /* Print previous instruction in cache */
160     if (cpu->last_exec->len) {
161         qemu_plugin_outs(cpu->last_exec->str);
162         qemu_plugin_outs("\n");
163     }
164 
165     /* Store new instruction in cache */
166     /* vcpu_mem will add memory access information to last_exec */
167     g_string_printf(cpu->last_exec, "%u, ", cpu_index);
168     g_string_append(cpu->last_exec, (char *)udata);
169 }
170 
171 /**
172  * On translation block new translation
173  *
174  * QEMU convert code by translation block (TB). By hooking here we can then hook
175  * a callback on each instruction and memory access.
176  */
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)177 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
178 {
179     struct qemu_plugin_insn *insn;
180     bool skip = (imatches || amatches);
181     bool check_regs_this = rmatches;
182     bool check_regs_next = false;
183 
184     size_t n_insns = qemu_plugin_tb_n_insns(tb);
185     for (size_t i = 0; i < n_insns; i++) {
186         char *insn_disas;
187         uint64_t insn_vaddr;
188 
189         /*
190          * `insn` is shared between translations in QEMU, copy needed data here.
191          * `output` is never freed as it might be used multiple times during
192          * the emulation lifetime.
193          * We only consider the first 32 bits of the instruction, this may be
194          * a limitation for CISC architectures.
195          */
196         insn = qemu_plugin_tb_get_insn(tb, i);
197         insn_disas = qemu_plugin_insn_disas(insn);
198         insn_vaddr = qemu_plugin_insn_vaddr(insn);
199 
200         /*
201          * If we are filtering we better check out if we have any
202          * hits. The skip "latches" so we can track memory accesses
203          * after the instruction we care about. Also enable register
204          * checking on the next instruction.
205          */
206         if (skip && imatches) {
207             int j;
208             for (j = 0; j < imatches->len && skip; j++) {
209                 char *m = g_ptr_array_index(imatches, j);
210                 if (g_str_has_prefix(insn_disas, m)) {
211                     skip = false;
212                     check_regs_next = rmatches;
213                 }
214             }
215         }
216 
217         if (skip && amatches) {
218             int j;
219             for (j = 0; j < amatches->len && skip; j++) {
220                 uint64_t v = g_array_index(amatches, uint64_t, j);
221                 if (v == insn_vaddr) {
222                     skip = false;
223                 }
224             }
225         }
226 
227         /*
228          * Check the disassembly to see if a register we care about
229          * will be affected by this instruction. This relies on the
230          * dissembler doing something sensible for the registers we
231          * care about.
232          */
233         if (disas_assist && rmatches) {
234             check_regs_next = false;
235             g_auto(GStrv) args = g_strsplit_set(insn_disas, " \t", 2);
236             if (args && args[1]) {
237                 for (int n = 0; n < all_reg_names->len; n++) {
238                     const gchar *reg = g_ptr_array_index(all_reg_names, n);
239                     if (g_strrstr(args[1], reg)) {
240                         check_regs_next = true;
241                         skip = false;
242                         break;
243                     }
244                 }
245             }
246         }
247 
248         /*
249          * We now have 3 choices:
250          *
251          * - Log insn
252          * - Log insn while checking registers
253          * - Don't log this insn but check if last insn changed registers
254          */
255 
256         if (skip) {
257             if (check_regs_this) {
258                 qemu_plugin_register_vcpu_insn_exec_cb(insn,
259                                                        vcpu_insn_exec_only_regs,
260                                                        QEMU_PLUGIN_CB_R_REGS,
261                                                        NULL);
262             }
263         } else {
264             uint32_t insn_opcode = 0;
265             qemu_plugin_insn_data(insn, &insn_opcode, sizeof(insn_opcode));
266 
267             char *output = g_strdup_printf("0x%"PRIx64", 0x%"PRIx32", \"%s\"",
268                                            insn_vaddr, insn_opcode, insn_disas);
269 
270             /* Register callback on memory read or write */
271             qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
272                                              QEMU_PLUGIN_CB_NO_REGS,
273                                              QEMU_PLUGIN_MEM_RW, NULL);
274 
275             /* Register callback on instruction */
276             if (check_regs_this) {
277                 qemu_plugin_register_vcpu_insn_exec_cb(
278                     insn, vcpu_insn_exec_with_regs,
279                     QEMU_PLUGIN_CB_R_REGS,
280                     output);
281             } else {
282                 qemu_plugin_register_vcpu_insn_exec_cb(
283                     insn, vcpu_insn_exec,
284                     QEMU_PLUGIN_CB_NO_REGS,
285                     output);
286             }
287 
288             /* reset skip */
289             skip = (imatches || amatches);
290         }
291 
292         /* set regs for next */
293         if (disas_assist && rmatches) {
294             check_regs_this = check_regs_next;
295         }
296 
297         g_free(insn_disas);
298     }
299 }
300 
init_vcpu_register(qemu_plugin_reg_descriptor * desc)301 static Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc)
302 {
303     Register *reg = g_new0(Register, 1);
304     g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
305     int r;
306 
307     reg->handle = desc->handle;
308     reg->name = g_intern_string(lower);
309     reg->last = g_byte_array_new();
310     reg->new = g_byte_array_new();
311 
312     /* read the initial value */
313     r = qemu_plugin_read_register(reg->handle, reg->last);
314     g_assert(r > 0);
315     return reg;
316 }
317 
318 /*
319  * g_pattern_match_string has been deprecated in Glib since 2.70 and
320  * will complain about it if you try to use it. Fortunately the
321  * signature of both functions is the same making it easy to work
322  * around.
323  */
324 static inline
g_pattern_spec_match_string_qemu(GPatternSpec * pspec,const gchar * string)325 gboolean g_pattern_spec_match_string_qemu(GPatternSpec *pspec,
326                                           const gchar *string)
327 {
328 #if GLIB_CHECK_VERSION(2, 70, 0)
329     return g_pattern_spec_match_string(pspec, string);
330 #else
331     return g_pattern_match_string(pspec, string);
332 #endif
333 };
334 #define g_pattern_spec_match_string(p, s) g_pattern_spec_match_string_qemu(p, s)
335 
registers_init(int vcpu_index)336 static GPtrArray *registers_init(int vcpu_index)
337 {
338     g_autoptr(GPtrArray) registers = g_ptr_array_new();
339     g_autoptr(GArray) reg_list = qemu_plugin_get_registers();
340 
341     if (rmatches && reg_list->len) {
342         /*
343          * Go through each register in the complete list and
344          * see if we want to track it.
345          */
346         for (int r = 0; r < reg_list->len; r++) {
347             qemu_plugin_reg_descriptor *rd = &g_array_index(
348                 reg_list, qemu_plugin_reg_descriptor, r);
349             for (int p = 0; p < rmatches->len; p++) {
350                 g_autoptr(GPatternSpec) pat = g_pattern_spec_new(rmatches->pdata[p]);
351                 g_autofree gchar *rd_lower = g_utf8_strdown(rd->name, -1);
352                 if (g_pattern_spec_match_string(pat, rd->name) ||
353                     g_pattern_spec_match_string(pat, rd_lower)) {
354                     Register *reg = init_vcpu_register(rd);
355                     g_ptr_array_add(registers, reg);
356 
357                     /* we need a list of regnames at TB translation time */
358                     if (disas_assist) {
359                         g_mutex_lock(&add_reg_name_lock);
360                         if (!g_ptr_array_find(all_reg_names, reg->name, NULL)) {
361                             g_ptr_array_add(all_reg_names, (gpointer)reg->name);
362                         }
363                         g_mutex_unlock(&add_reg_name_lock);
364                     }
365                 }
366             }
367         }
368     }
369 
370     return registers->len ? g_steal_pointer(&registers) : NULL;
371 }
372 
373 /*
374  * Initialise a new vcpu/thread with:
375  *   - last_exec tracking data
376  *   - list of tracked registers
377  *   - initial value of registers
378  *
379  * As we could have multiple threads trying to do this we need to
380  * serialise the expansion under a lock.
381  */
vcpu_init(qemu_plugin_id_t id,unsigned int vcpu_index)382 static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index)
383 {
384     CPU *c;
385 
386     g_rw_lock_writer_lock(&expand_array_lock);
387     if (vcpu_index >= cpus->len) {
388         g_array_set_size(cpus, vcpu_index + 1);
389     }
390     g_rw_lock_writer_unlock(&expand_array_lock);
391 
392     c = get_cpu(vcpu_index);
393     c->last_exec = g_string_new(NULL);
394     c->registers = registers_init(vcpu_index);
395 }
396 
397 /**
398  * On plugin exit, print last instruction in cache
399  */
plugin_exit(qemu_plugin_id_t id,void * p)400 static void plugin_exit(qemu_plugin_id_t id, void *p)
401 {
402     guint i;
403     g_rw_lock_reader_lock(&expand_array_lock);
404     for (i = 0; i < cpus->len; i++) {
405         CPU *c = get_cpu(i);
406         if (c->last_exec && c->last_exec->str) {
407             qemu_plugin_outs(c->last_exec->str);
408             qemu_plugin_outs("\n");
409         }
410     }
411     g_rw_lock_reader_unlock(&expand_array_lock);
412 }
413 
414 /* Add a match to the array of matches */
parse_insn_match(char * match)415 static void parse_insn_match(char *match)
416 {
417     if (!imatches) {
418         imatches = g_ptr_array_new();
419     }
420     g_ptr_array_add(imatches, g_strdup(match));
421 }
422 
parse_vaddr_match(char * match)423 static void parse_vaddr_match(char *match)
424 {
425     uint64_t v = g_ascii_strtoull(match, NULL, 16);
426 
427     if (!amatches) {
428         amatches = g_array_new(false, true, sizeof(uint64_t));
429     }
430     g_array_append_val(amatches, v);
431 }
432 
433 /*
434  * We have to wait until vCPUs are started before we can check the
435  * patterns find anything.
436  */
add_regpat(char * regpat)437 static void add_regpat(char *regpat)
438 {
439     if (!rmatches) {
440         rmatches = g_ptr_array_new();
441     }
442     g_ptr_array_add(rmatches, g_strdup(regpat));
443 }
444 
445 /**
446  * Install the plugin
447  */
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)448 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
449                                            const qemu_info_t *info, int argc,
450                                            char **argv)
451 {
452     /*
453      * Initialize dynamic array to cache vCPU instruction. In user mode
454      * we don't know the size before emulation.
455      */
456     cpus = g_array_sized_new(true, true, sizeof(CPU),
457                              info->system_emulation ? info->system.max_vcpus : 1);
458 
459     for (int i = 0; i < argc; i++) {
460         char *opt = argv[i];
461         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
462         if (g_strcmp0(tokens[0], "ifilter") == 0) {
463             parse_insn_match(tokens[1]);
464         } else if (g_strcmp0(tokens[0], "afilter") == 0) {
465             parse_vaddr_match(tokens[1]);
466         } else if (g_strcmp0(tokens[0], "reg") == 0) {
467             add_regpat(tokens[1]);
468         } else if (g_strcmp0(tokens[0], "rdisas") == 0) {
469             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &disas_assist)) {
470                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
471                 return -1;
472             }
473             all_reg_names = g_ptr_array_new();
474         } else {
475             fprintf(stderr, "option parsing failed: %s\n", opt);
476             return -1;
477         }
478     }
479 
480     /* Register init, translation block and exit callbacks */
481     qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
482     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
483     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
484 
485     return 0;
486 }
487