pnv/xive2: Don't overwrite PC registers when writing TCTXT registers
[qemu/rayw.git] / contrib / plugins / execlog.c
bloba5275dcc15c221b0967106629a2176c8a0b5017f
1 /*
2 * Copyright (C) 2021, Alexandre Iooss <erdnaxe@crans.org>
4 * Log instruction execution with memory access.
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>
16 #include <qemu-plugin.h>
18 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
20 /* Store last executed instruction on each vCPU as a GString */
21 GArray *last_exec;
23 /**
24 * Add memory read or write information to current instruction log
26 static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t info,
27 uint64_t vaddr, void *udata)
29 GString *s;
31 /* Find vCPU in array */
32 g_assert(cpu_index < last_exec->len);
33 s = g_array_index(last_exec, GString *, cpu_index);
35 /* Indicate type of memory access */
36 if (qemu_plugin_mem_is_store(info)) {
37 g_string_append(s, ", store");
38 } else {
39 g_string_append(s, ", load");
42 /* If full system emulation log physical address and device name */
43 struct qemu_plugin_hwaddr *hwaddr = qemu_plugin_get_hwaddr(info, vaddr);
44 if (hwaddr) {
45 uint64_t addr = qemu_plugin_hwaddr_phys_addr(hwaddr);
46 const char *name = qemu_plugin_hwaddr_device_name(hwaddr);
47 g_string_append_printf(s, ", 0x%08"PRIx64", %s", addr, name);
48 } else {
49 g_string_append_printf(s, ", 0x%08"PRIx64, vaddr);
53 /**
54 * Log instruction execution
56 static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
58 GString *s;
60 /* Find or create vCPU in array */
61 while (cpu_index >= last_exec->len) {
62 s = g_string_new(NULL);
63 g_array_append_val(last_exec, s);
65 s = g_array_index(last_exec, GString *, cpu_index);
67 /* Print previous instruction in cache */
68 if (s->len) {
69 qemu_plugin_outs(s->str);
70 qemu_plugin_outs("\n");
73 /* Store new instruction in cache */
74 /* vcpu_mem will add memory access information to last_exec */
75 g_string_printf(s, "%u, ", cpu_index);
76 g_string_append(s, (char *)udata);
79 /**
80 * On translation block new translation
82 * QEMU convert code by translation block (TB). By hooking here we can then hook
83 * a callback on each instruction and memory access.
85 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
87 struct qemu_plugin_insn *insn;
88 uint64_t insn_vaddr;
89 uint32_t insn_opcode;
90 char *insn_disas;
92 size_t n = qemu_plugin_tb_n_insns(tb);
93 for (size_t i = 0; i < n; i++) {
95 * `insn` is shared between translations in QEMU, copy needed data here.
96 * `output` is never freed as it might be used multiple times during
97 * the emulation lifetime.
98 * We only consider the first 32 bits of the instruction, this may be
99 * a limitation for CISC architectures.
101 insn = qemu_plugin_tb_get_insn(tb, i);
102 insn_vaddr = qemu_plugin_insn_vaddr(insn);
103 insn_opcode = *((uint32_t *)qemu_plugin_insn_data(insn));
104 insn_disas = qemu_plugin_insn_disas(insn);
105 char *output = g_strdup_printf("0x%"PRIx64", 0x%"PRIx32", \"%s\"",
106 insn_vaddr, insn_opcode, insn_disas);
108 /* Register callback on memory read or write */
109 qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
110 QEMU_PLUGIN_CB_NO_REGS,
111 QEMU_PLUGIN_MEM_RW, NULL);
113 /* Register callback on instruction */
114 qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec,
115 QEMU_PLUGIN_CB_NO_REGS, output);
120 * On plugin exit, print last instruction in cache
122 static void plugin_exit(qemu_plugin_id_t id, void *p)
124 guint i;
125 GString *s;
126 for (i = 0; i < last_exec->len; i++) {
127 s = g_array_index(last_exec, GString *, i);
128 if (s->str) {
129 qemu_plugin_outs(s->str);
130 qemu_plugin_outs("\n");
136 * Install the plugin
138 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
139 const qemu_info_t *info, int argc,
140 char **argv)
143 * Initialize dynamic array to cache vCPU instruction. In user mode
144 * we don't know the size before emulation.
146 last_exec = g_array_new(FALSE, FALSE, sizeof(GString *));
148 /* Register translation block and exit callbacks */
149 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
150 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
152 return 0;