13d7caf14SAlexandre Iooss /*
23d7caf14SAlexandre Iooss * Copyright (C) 2021, Alexandre Iooss <erdnaxe@crans.org>
33d7caf14SAlexandre Iooss *
4af6e4e0aSAlex Bennée * Log instruction execution with memory access and register changes
53d7caf14SAlexandre Iooss *
63d7caf14SAlexandre Iooss * License: GNU GPL, version 2 or later.
73d7caf14SAlexandre Iooss * See the COPYING file in the top-level directory.
83d7caf14SAlexandre Iooss */
93d7caf14SAlexandre Iooss #include <glib.h>
103d7caf14SAlexandre Iooss #include <inttypes.h>
113d7caf14SAlexandre Iooss #include <stdio.h>
123d7caf14SAlexandre Iooss #include <stdlib.h>
133d7caf14SAlexandre Iooss #include <string.h>
143d7caf14SAlexandre Iooss #include <unistd.h>
153d7caf14SAlexandre Iooss
163d7caf14SAlexandre Iooss #include <qemu-plugin.h>
173d7caf14SAlexandre Iooss
18af6e4e0aSAlex Bennée typedef struct {
19af6e4e0aSAlex Bennée struct qemu_plugin_register *handle;
20af6e4e0aSAlex Bennée GByteArray *last;
21af6e4e0aSAlex Bennée GByteArray *new;
22af6e4e0aSAlex Bennée const char *name;
23af6e4e0aSAlex Bennée } Register;
24af6e4e0aSAlex Bennée
25af6e4e0aSAlex Bennée typedef struct CPU {
26af6e4e0aSAlex Bennée /* Store last executed instruction on each vCPU as a GString */
27af6e4e0aSAlex Bennée GString *last_exec;
28af6e4e0aSAlex Bennée /* Ptr array of Register */
29af6e4e0aSAlex Bennée GPtrArray *registers;
30af6e4e0aSAlex Bennée } CPU;
31af6e4e0aSAlex Bennée
323d7caf14SAlexandre Iooss QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
333d7caf14SAlexandre Iooss
34af6e4e0aSAlex Bennée static GArray *cpus;
351063693eSAkihiko Odaki static GRWLock expand_array_lock;
363d7caf14SAlexandre Iooss
37b7855bf6SAlex Bennée static GPtrArray *imatches;
38b7855bf6SAlex Bennée static GArray *amatches;
39af6e4e0aSAlex Bennée static GPtrArray *rmatches;
40af6e4e0aSAlex Bennée static bool disas_assist;
41af6e4e0aSAlex Bennée static GMutex add_reg_name_lock;
42af6e4e0aSAlex Bennée static GPtrArray *all_reg_names;
43b7855bf6SAlex Bennée
get_cpu(int vcpu_index)44af6e4e0aSAlex Bennée static CPU *get_cpu(int vcpu_index)
4514fd492bSAlex Bennée {
46af6e4e0aSAlex Bennée CPU *c;
47af6e4e0aSAlex Bennée g_rw_lock_reader_lock(&expand_array_lock);
48af6e4e0aSAlex Bennée c = &g_array_index(cpus, CPU, vcpu_index);
49af6e4e0aSAlex Bennée g_rw_lock_reader_unlock(&expand_array_lock);
50af6e4e0aSAlex Bennée
51af6e4e0aSAlex Bennée return c;
5214fd492bSAlex Bennée }
5314fd492bSAlex Bennée
543d7caf14SAlexandre Iooss /**
553d7caf14SAlexandre Iooss * Add memory read or write information to current instruction log
563d7caf14SAlexandre Iooss */
vcpu_mem(unsigned int cpu_index,qemu_plugin_meminfo_t info,uint64_t vaddr,void * udata)573d7caf14SAlexandre Iooss static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t info,
583d7caf14SAlexandre Iooss uint64_t vaddr, void *udata)
593d7caf14SAlexandre Iooss {
60af6e4e0aSAlex Bennée CPU *c = get_cpu(cpu_index);
61af6e4e0aSAlex Bennée GString *s = c->last_exec;
623d7caf14SAlexandre Iooss
633d7caf14SAlexandre Iooss /* Find vCPU in array */
643d7caf14SAlexandre Iooss
653d7caf14SAlexandre Iooss /* Indicate type of memory access */
663d7caf14SAlexandre Iooss if (qemu_plugin_mem_is_store(info)) {
673d7caf14SAlexandre Iooss g_string_append(s, ", store");
683d7caf14SAlexandre Iooss } else {
693d7caf14SAlexandre Iooss g_string_append(s, ", load");
703d7caf14SAlexandre Iooss }
713d7caf14SAlexandre Iooss
723d7caf14SAlexandre Iooss /* If full system emulation log physical address and device name */
733d7caf14SAlexandre Iooss struct qemu_plugin_hwaddr *hwaddr = qemu_plugin_get_hwaddr(info, vaddr);
743d7caf14SAlexandre Iooss if (hwaddr) {
753d7caf14SAlexandre Iooss uint64_t addr = qemu_plugin_hwaddr_phys_addr(hwaddr);
763d7caf14SAlexandre Iooss const char *name = qemu_plugin_hwaddr_device_name(hwaddr);
773d7caf14SAlexandre Iooss g_string_append_printf(s, ", 0x%08"PRIx64", %s", addr, name);
783d7caf14SAlexandre Iooss } else {
793d7caf14SAlexandre Iooss g_string_append_printf(s, ", 0x%08"PRIx64, vaddr);
803d7caf14SAlexandre Iooss }
813d7caf14SAlexandre Iooss }
823d7caf14SAlexandre Iooss
833d7caf14SAlexandre Iooss /**
84af6e4e0aSAlex Bennée * Log instruction execution, outputting the last one.
85af6e4e0aSAlex Bennée *
86af6e4e0aSAlex Bennée * vcpu_insn_exec() is a copy and paste of vcpu_insn_exec_with_regs()
87af6e4e0aSAlex Bennée * without the checking of register values when we've attempted to
88af6e4e0aSAlex Bennée * optimise with disas_assist.
893d7caf14SAlexandre Iooss */
insn_check_regs(CPU * cpu)90af6e4e0aSAlex Bennée static void insn_check_regs(CPU *cpu)
913d7caf14SAlexandre Iooss {
92af6e4e0aSAlex Bennée for (int n = 0; n < cpu->registers->len; n++) {
93af6e4e0aSAlex Bennée Register *reg = cpu->registers->pdata[n];
94af6e4e0aSAlex Bennée int sz;
953d7caf14SAlexandre Iooss
96af6e4e0aSAlex Bennée g_byte_array_set_size(reg->new, 0);
97af6e4e0aSAlex Bennée sz = qemu_plugin_read_register(reg->handle, reg->new);
98af6e4e0aSAlex Bennée g_assert(sz == reg->last->len);
99af6e4e0aSAlex Bennée
100af6e4e0aSAlex Bennée if (memcmp(reg->last->data, reg->new->data, sz)) {
101af6e4e0aSAlex Bennée GByteArray *temp = reg->last;
102af6e4e0aSAlex Bennée g_string_append_printf(cpu->last_exec, ", %s -> 0x", reg->name);
103af6e4e0aSAlex Bennée /* TODO: handle BE properly */
1048d073d48SFrédéric Pétrot for (int i = sz - 1; i >= 0; i--) {
105af6e4e0aSAlex Bennée g_string_append_printf(cpu->last_exec, "%02x",
106af6e4e0aSAlex Bennée reg->new->data[i]);
1073d7caf14SAlexandre Iooss }
108af6e4e0aSAlex Bennée reg->last = reg->new;
109af6e4e0aSAlex Bennée reg->new = temp;
110af6e4e0aSAlex Bennée }
111af6e4e0aSAlex Bennée }
112af6e4e0aSAlex Bennée }
113af6e4e0aSAlex Bennée
114af6e4e0aSAlex Bennée /* Log last instruction while checking registers */
vcpu_insn_exec_with_regs(unsigned int cpu_index,void * udata)115af6e4e0aSAlex Bennée static void vcpu_insn_exec_with_regs(unsigned int cpu_index, void *udata)
116af6e4e0aSAlex Bennée {
117af6e4e0aSAlex Bennée CPU *cpu = get_cpu(cpu_index);
1183d7caf14SAlexandre Iooss
1193d7caf14SAlexandre Iooss /* Print previous instruction in cache */
120af6e4e0aSAlex Bennée if (cpu->last_exec->len) {
121af6e4e0aSAlex Bennée if (cpu->registers) {
122af6e4e0aSAlex Bennée insn_check_regs(cpu);
123af6e4e0aSAlex Bennée }
124af6e4e0aSAlex Bennée
125af6e4e0aSAlex Bennée qemu_plugin_outs(cpu->last_exec->str);
126b4031061SMahmoud Mandour qemu_plugin_outs("\n");
1273d7caf14SAlexandre Iooss }
1283d7caf14SAlexandre Iooss
1293d7caf14SAlexandre Iooss /* Store new instruction in cache */
1303d7caf14SAlexandre Iooss /* vcpu_mem will add memory access information to last_exec */
131af6e4e0aSAlex Bennée g_string_printf(cpu->last_exec, "%u, ", cpu_index);
132af6e4e0aSAlex Bennée g_string_append(cpu->last_exec, (char *)udata);
133af6e4e0aSAlex Bennée }
134af6e4e0aSAlex Bennée
135af6e4e0aSAlex Bennée /* Log last instruction while checking registers, ignore next */
vcpu_insn_exec_only_regs(unsigned int cpu_index,void * udata)136af6e4e0aSAlex Bennée static void vcpu_insn_exec_only_regs(unsigned int cpu_index, void *udata)
137af6e4e0aSAlex Bennée {
138af6e4e0aSAlex Bennée CPU *cpu = get_cpu(cpu_index);
139af6e4e0aSAlex Bennée
140af6e4e0aSAlex Bennée /* Print previous instruction in cache */
141af6e4e0aSAlex Bennée if (cpu->last_exec->len) {
142af6e4e0aSAlex Bennée if (cpu->registers) {
143af6e4e0aSAlex Bennée insn_check_regs(cpu);
144af6e4e0aSAlex Bennée }
145af6e4e0aSAlex Bennée
146af6e4e0aSAlex Bennée qemu_plugin_outs(cpu->last_exec->str);
147af6e4e0aSAlex Bennée qemu_plugin_outs("\n");
148af6e4e0aSAlex Bennée }
149af6e4e0aSAlex Bennée
150af6e4e0aSAlex Bennée /* reset */
151af6e4e0aSAlex Bennée cpu->last_exec->len = 0;
152af6e4e0aSAlex Bennée }
153af6e4e0aSAlex Bennée
154af6e4e0aSAlex Bennée /* Log last instruction without checking regs, setup next */
vcpu_insn_exec(unsigned int cpu_index,void * udata)155af6e4e0aSAlex Bennée static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
156af6e4e0aSAlex Bennée {
157af6e4e0aSAlex Bennée CPU *cpu = get_cpu(cpu_index);
158af6e4e0aSAlex Bennée
159af6e4e0aSAlex Bennée /* Print previous instruction in cache */
160af6e4e0aSAlex Bennée if (cpu->last_exec->len) {
161af6e4e0aSAlex Bennée qemu_plugin_outs(cpu->last_exec->str);
162af6e4e0aSAlex Bennée qemu_plugin_outs("\n");
163af6e4e0aSAlex Bennée }
164af6e4e0aSAlex Bennée
165af6e4e0aSAlex Bennée /* Store new instruction in cache */
166af6e4e0aSAlex Bennée /* vcpu_mem will add memory access information to last_exec */
167af6e4e0aSAlex Bennée g_string_printf(cpu->last_exec, "%u, ", cpu_index);
168af6e4e0aSAlex Bennée g_string_append(cpu->last_exec, (char *)udata);
1693d7caf14SAlexandre Iooss }
1703d7caf14SAlexandre Iooss
1713d7caf14SAlexandre Iooss /**
1723d7caf14SAlexandre Iooss * On translation block new translation
1733d7caf14SAlexandre Iooss *
1743d7caf14SAlexandre Iooss * QEMU convert code by translation block (TB). By hooking here we can then hook
1753d7caf14SAlexandre Iooss * a callback on each instruction and memory access.
1763d7caf14SAlexandre Iooss */
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)1773d7caf14SAlexandre Iooss static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
1783d7caf14SAlexandre Iooss {
1793d7caf14SAlexandre Iooss struct qemu_plugin_insn *insn;
1808c1f1020SAlex Bennée bool skip = (imatches || amatches);
181af6e4e0aSAlex Bennée bool check_regs_this = rmatches;
182af6e4e0aSAlex Bennée bool check_regs_next = false;
1833d7caf14SAlexandre Iooss
184*47f06fb4SPierrick Bouvier size_t n_insns = qemu_plugin_tb_n_insns(tb);
185*47f06fb4SPierrick Bouvier for (size_t i = 0; i < n_insns; i++) {
186b7855bf6SAlex Bennée char *insn_disas;
187b7855bf6SAlex Bennée uint64_t insn_vaddr;
188b7855bf6SAlex Bennée
1893d7caf14SAlexandre Iooss /*
1903d7caf14SAlexandre Iooss * `insn` is shared between translations in QEMU, copy needed data here.
1913d7caf14SAlexandre Iooss * `output` is never freed as it might be used multiple times during
1923d7caf14SAlexandre Iooss * the emulation lifetime.
1933d7caf14SAlexandre Iooss * We only consider the first 32 bits of the instruction, this may be
1943d7caf14SAlexandre Iooss * a limitation for CISC architectures.
1953d7caf14SAlexandre Iooss */
1963d7caf14SAlexandre Iooss insn = qemu_plugin_tb_get_insn(tb, i);
1973d7caf14SAlexandre Iooss insn_disas = qemu_plugin_insn_disas(insn);
198b7855bf6SAlex Bennée insn_vaddr = qemu_plugin_insn_vaddr(insn);
199b7855bf6SAlex Bennée
200b7855bf6SAlex Bennée /*
201b7855bf6SAlex Bennée * If we are filtering we better check out if we have any
202b7855bf6SAlex Bennée * hits. The skip "latches" so we can track memory accesses
203af6e4e0aSAlex Bennée * after the instruction we care about. Also enable register
204af6e4e0aSAlex Bennée * checking on the next instruction.
205b7855bf6SAlex Bennée */
206b7855bf6SAlex Bennée if (skip && imatches) {
207b7855bf6SAlex Bennée int j;
208b7855bf6SAlex Bennée for (j = 0; j < imatches->len && skip; j++) {
209b7855bf6SAlex Bennée char *m = g_ptr_array_index(imatches, j);
210b7855bf6SAlex Bennée if (g_str_has_prefix(insn_disas, m)) {
211b7855bf6SAlex Bennée skip = false;
212af6e4e0aSAlex Bennée check_regs_next = rmatches;
213b7855bf6SAlex Bennée }
214b7855bf6SAlex Bennée }
215b7855bf6SAlex Bennée }
216b7855bf6SAlex Bennée
217b7855bf6SAlex Bennée if (skip && amatches) {
218b7855bf6SAlex Bennée int j;
219b7855bf6SAlex Bennée for (j = 0; j < amatches->len && skip; j++) {
220b7855bf6SAlex Bennée uint64_t v = g_array_index(amatches, uint64_t, j);
221b7855bf6SAlex Bennée if (v == insn_vaddr) {
222b7855bf6SAlex Bennée skip = false;
223b7855bf6SAlex Bennée }
224b7855bf6SAlex Bennée }
225b7855bf6SAlex Bennée }
226b7855bf6SAlex Bennée
227af6e4e0aSAlex Bennée /*
228af6e4e0aSAlex Bennée * Check the disassembly to see if a register we care about
229af6e4e0aSAlex Bennée * will be affected by this instruction. This relies on the
230af6e4e0aSAlex Bennée * dissembler doing something sensible for the registers we
231af6e4e0aSAlex Bennée * care about.
232af6e4e0aSAlex Bennée */
233af6e4e0aSAlex Bennée if (disas_assist && rmatches) {
234af6e4e0aSAlex Bennée check_regs_next = false;
235af6e4e0aSAlex Bennée gchar *args = g_strstr_len(insn_disas, -1, " ");
236af6e4e0aSAlex Bennée for (int n = 0; n < all_reg_names->len; n++) {
237af6e4e0aSAlex Bennée gchar *reg = g_ptr_array_index(all_reg_names, n);
238af6e4e0aSAlex Bennée if (g_strrstr(args, reg)) {
239af6e4e0aSAlex Bennée check_regs_next = true;
240af6e4e0aSAlex Bennée skip = false;
241af6e4e0aSAlex Bennée }
242af6e4e0aSAlex Bennée }
243af6e4e0aSAlex Bennée }
244af6e4e0aSAlex Bennée
245af6e4e0aSAlex Bennée /*
246af6e4e0aSAlex Bennée * We now have 3 choices:
247af6e4e0aSAlex Bennée *
248af6e4e0aSAlex Bennée * - Log insn
249af6e4e0aSAlex Bennée * - Log insn while checking registers
250af6e4e0aSAlex Bennée * - Don't log this insn but check if last insn changed registers
251af6e4e0aSAlex Bennée */
252af6e4e0aSAlex Bennée
253b7855bf6SAlex Bennée if (skip) {
254af6e4e0aSAlex Bennée if (check_regs_this) {
255af6e4e0aSAlex Bennée qemu_plugin_register_vcpu_insn_exec_cb(insn,
256af6e4e0aSAlex Bennée vcpu_insn_exec_only_regs,
257af6e4e0aSAlex Bennée QEMU_PLUGIN_CB_R_REGS,
258af6e4e0aSAlex Bennée NULL);
259af6e4e0aSAlex Bennée }
260b7855bf6SAlex Bennée } else {
2614abc8923SRichard Henderson uint32_t insn_opcode = 0;
2624abc8923SRichard Henderson qemu_plugin_insn_data(insn, &insn_opcode, sizeof(insn_opcode));
2634abc8923SRichard Henderson
2643d7caf14SAlexandre Iooss char *output = g_strdup_printf("0x%"PRIx64", 0x%"PRIx32", \"%s\"",
2653d7caf14SAlexandre Iooss insn_vaddr, insn_opcode, insn_disas);
2663d7caf14SAlexandre Iooss
2673d7caf14SAlexandre Iooss /* Register callback on memory read or write */
2683d7caf14SAlexandre Iooss qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
2693d7caf14SAlexandre Iooss QEMU_PLUGIN_CB_NO_REGS,
2703d7caf14SAlexandre Iooss QEMU_PLUGIN_MEM_RW, NULL);
2713d7caf14SAlexandre Iooss
2723d7caf14SAlexandre Iooss /* Register callback on instruction */
273af6e4e0aSAlex Bennée if (check_regs_this) {
274af6e4e0aSAlex Bennée qemu_plugin_register_vcpu_insn_exec_cb(
275af6e4e0aSAlex Bennée insn, vcpu_insn_exec_with_regs,
276af6e4e0aSAlex Bennée QEMU_PLUGIN_CB_R_REGS,
277af6e4e0aSAlex Bennée output);
278af6e4e0aSAlex Bennée } else {
279af6e4e0aSAlex Bennée qemu_plugin_register_vcpu_insn_exec_cb(
280af6e4e0aSAlex Bennée insn, vcpu_insn_exec,
281af6e4e0aSAlex Bennée QEMU_PLUGIN_CB_NO_REGS,
282af6e4e0aSAlex Bennée output);
283af6e4e0aSAlex Bennée }
2848c1f1020SAlex Bennée
2858c1f1020SAlex Bennée /* reset skip */
2868c1f1020SAlex Bennée skip = (imatches || amatches);
2873d7caf14SAlexandre Iooss }
288b7855bf6SAlex Bennée
289af6e4e0aSAlex Bennée /* set regs for next */
290af6e4e0aSAlex Bennée if (disas_assist && rmatches) {
291af6e4e0aSAlex Bennée check_regs_this = check_regs_next;
292b7855bf6SAlex Bennée }
293af6e4e0aSAlex Bennée
294af6e4e0aSAlex Bennée g_free(insn_disas);
295af6e4e0aSAlex Bennée }
296af6e4e0aSAlex Bennée }
297af6e4e0aSAlex Bennée
init_vcpu_register(qemu_plugin_reg_descriptor * desc)298af6e4e0aSAlex Bennée static Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc)
299af6e4e0aSAlex Bennée {
300af6e4e0aSAlex Bennée Register *reg = g_new0(Register, 1);
301af6e4e0aSAlex Bennée g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
302af6e4e0aSAlex Bennée int r;
303af6e4e0aSAlex Bennée
304af6e4e0aSAlex Bennée reg->handle = desc->handle;
305af6e4e0aSAlex Bennée reg->name = g_intern_string(lower);
306af6e4e0aSAlex Bennée reg->last = g_byte_array_new();
307af6e4e0aSAlex Bennée reg->new = g_byte_array_new();
308af6e4e0aSAlex Bennée
309af6e4e0aSAlex Bennée /* read the initial value */
310af6e4e0aSAlex Bennée r = qemu_plugin_read_register(reg->handle, reg->last);
311af6e4e0aSAlex Bennée g_assert(r > 0);
312af6e4e0aSAlex Bennée return reg;
313af6e4e0aSAlex Bennée }
314af6e4e0aSAlex Bennée
315d5866a7aSYao Xingtao /*
316d5866a7aSYao Xingtao * g_pattern_match_string has been deprecated in Glib since 2.70 and
317d5866a7aSYao Xingtao * will complain about it if you try to use it. Fortunately the
318d5866a7aSYao Xingtao * signature of both functions is the same making it easy to work
319d5866a7aSYao Xingtao * around.
320d5866a7aSYao Xingtao */
321d5866a7aSYao Xingtao static inline
g_pattern_spec_match_string_qemu(GPatternSpec * pspec,const gchar * string)322d5866a7aSYao Xingtao gboolean g_pattern_spec_match_string_qemu(GPatternSpec *pspec,
323d5866a7aSYao Xingtao const gchar *string)
324d5866a7aSYao Xingtao {
325d5866a7aSYao Xingtao #if GLIB_CHECK_VERSION(2, 70, 0)
326d5866a7aSYao Xingtao return g_pattern_spec_match_string(pspec, string);
327d5866a7aSYao Xingtao #else
328d5866a7aSYao Xingtao return g_pattern_match_string(pspec, string);
329d5866a7aSYao Xingtao #endif
330d5866a7aSYao Xingtao };
331d5866a7aSYao Xingtao #define g_pattern_spec_match_string(p, s) g_pattern_spec_match_string_qemu(p, s)
332d5866a7aSYao Xingtao
registers_init(int vcpu_index)333af6e4e0aSAlex Bennée static GPtrArray *registers_init(int vcpu_index)
334af6e4e0aSAlex Bennée {
335af6e4e0aSAlex Bennée g_autoptr(GPtrArray) registers = g_ptr_array_new();
336af6e4e0aSAlex Bennée g_autoptr(GArray) reg_list = qemu_plugin_get_registers();
337af6e4e0aSAlex Bennée
338af6e4e0aSAlex Bennée if (rmatches && reg_list->len) {
339af6e4e0aSAlex Bennée /*
340af6e4e0aSAlex Bennée * Go through each register in the complete list and
341af6e4e0aSAlex Bennée * see if we want to track it.
342af6e4e0aSAlex Bennée */
343af6e4e0aSAlex Bennée for (int r = 0; r < reg_list->len; r++) {
344af6e4e0aSAlex Bennée qemu_plugin_reg_descriptor *rd = &g_array_index(
345af6e4e0aSAlex Bennée reg_list, qemu_plugin_reg_descriptor, r);
346af6e4e0aSAlex Bennée for (int p = 0; p < rmatches->len; p++) {
347af6e4e0aSAlex Bennée g_autoptr(GPatternSpec) pat = g_pattern_spec_new(rmatches->pdata[p]);
348af6e4e0aSAlex Bennée g_autofree gchar *rd_lower = g_utf8_strdown(rd->name, -1);
349d5866a7aSYao Xingtao if (g_pattern_spec_match_string(pat, rd->name) ||
350d5866a7aSYao Xingtao g_pattern_spec_match_string(pat, rd_lower)) {
351af6e4e0aSAlex Bennée Register *reg = init_vcpu_register(rd);
352af6e4e0aSAlex Bennée g_ptr_array_add(registers, reg);
353af6e4e0aSAlex Bennée
354af6e4e0aSAlex Bennée /* we need a list of regnames at TB translation time */
355af6e4e0aSAlex Bennée if (disas_assist) {
356af6e4e0aSAlex Bennée g_mutex_lock(&add_reg_name_lock);
357af6e4e0aSAlex Bennée if (!g_ptr_array_find(all_reg_names, reg->name, NULL)) {
358d5866a7aSYao Xingtao g_ptr_array_add(all_reg_names, (gpointer)reg->name);
359af6e4e0aSAlex Bennée }
360af6e4e0aSAlex Bennée g_mutex_unlock(&add_reg_name_lock);
361af6e4e0aSAlex Bennée }
362af6e4e0aSAlex Bennée }
363af6e4e0aSAlex Bennée }
364af6e4e0aSAlex Bennée }
365af6e4e0aSAlex Bennée }
366af6e4e0aSAlex Bennée
367af6e4e0aSAlex Bennée return registers->len ? g_steal_pointer(®isters) : NULL;
368af6e4e0aSAlex Bennée }
369af6e4e0aSAlex Bennée
370af6e4e0aSAlex Bennée /*
371af6e4e0aSAlex Bennée * Initialise a new vcpu/thread with:
372af6e4e0aSAlex Bennée * - last_exec tracking data
373af6e4e0aSAlex Bennée * - list of tracked registers
374af6e4e0aSAlex Bennée * - initial value of registers
375af6e4e0aSAlex Bennée *
376af6e4e0aSAlex Bennée * As we could have multiple threads trying to do this we need to
377af6e4e0aSAlex Bennée * serialise the expansion under a lock.
378af6e4e0aSAlex Bennée */
vcpu_init(qemu_plugin_id_t id,unsigned int vcpu_index)379af6e4e0aSAlex Bennée static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index)
380af6e4e0aSAlex Bennée {
381af6e4e0aSAlex Bennée CPU *c;
382af6e4e0aSAlex Bennée
383af6e4e0aSAlex Bennée g_rw_lock_writer_lock(&expand_array_lock);
384af6e4e0aSAlex Bennée if (vcpu_index >= cpus->len) {
385af6e4e0aSAlex Bennée g_array_set_size(cpus, vcpu_index + 1);
386af6e4e0aSAlex Bennée }
387af6e4e0aSAlex Bennée g_rw_lock_writer_unlock(&expand_array_lock);
388af6e4e0aSAlex Bennée
389af6e4e0aSAlex Bennée c = get_cpu(vcpu_index);
390af6e4e0aSAlex Bennée c->last_exec = g_string_new(NULL);
391af6e4e0aSAlex Bennée c->registers = registers_init(vcpu_index);
3923d7caf14SAlexandre Iooss }
3933d7caf14SAlexandre Iooss
3943d7caf14SAlexandre Iooss /**
3953d7caf14SAlexandre Iooss * On plugin exit, print last instruction in cache
3963d7caf14SAlexandre Iooss */
plugin_exit(qemu_plugin_id_t id,void * p)3973d7caf14SAlexandre Iooss static void plugin_exit(qemu_plugin_id_t id, void *p)
3983d7caf14SAlexandre Iooss {
3993d7caf14SAlexandre Iooss guint i;
400af6e4e0aSAlex Bennée g_rw_lock_reader_lock(&expand_array_lock);
401af6e4e0aSAlex Bennée for (i = 0; i < cpus->len; i++) {
402af6e4e0aSAlex Bennée CPU *c = get_cpu(i);
403af6e4e0aSAlex Bennée if (c->last_exec && c->last_exec->str) {
404af6e4e0aSAlex Bennée qemu_plugin_outs(c->last_exec->str);
4053d7caf14SAlexandre Iooss qemu_plugin_outs("\n");
4063d7caf14SAlexandre Iooss }
4073d7caf14SAlexandre Iooss }
408af6e4e0aSAlex Bennée g_rw_lock_reader_unlock(&expand_array_lock);
4093d7caf14SAlexandre Iooss }
4103d7caf14SAlexandre Iooss
411b7855bf6SAlex Bennée /* Add a match to the array of matches */
parse_insn_match(char * match)412b7855bf6SAlex Bennée static void parse_insn_match(char *match)
413b7855bf6SAlex Bennée {
414b7855bf6SAlex Bennée if (!imatches) {
415b7855bf6SAlex Bennée imatches = g_ptr_array_new();
416b7855bf6SAlex Bennée }
4179e096a76SAlex Bennée g_ptr_array_add(imatches, g_strdup(match));
418b7855bf6SAlex Bennée }
419b7855bf6SAlex Bennée
parse_vaddr_match(char * match)420b7855bf6SAlex Bennée static void parse_vaddr_match(char *match)
421b7855bf6SAlex Bennée {
422b7855bf6SAlex Bennée uint64_t v = g_ascii_strtoull(match, NULL, 16);
423b7855bf6SAlex Bennée
424b7855bf6SAlex Bennée if (!amatches) {
425b7855bf6SAlex Bennée amatches = g_array_new(false, true, sizeof(uint64_t));
426b7855bf6SAlex Bennée }
427b7855bf6SAlex Bennée g_array_append_val(amatches, v);
428b7855bf6SAlex Bennée }
429b7855bf6SAlex Bennée
430af6e4e0aSAlex Bennée /*
431af6e4e0aSAlex Bennée * We have to wait until vCPUs are started before we can check the
432af6e4e0aSAlex Bennée * patterns find anything.
433af6e4e0aSAlex Bennée */
add_regpat(char * regpat)434af6e4e0aSAlex Bennée static void add_regpat(char *regpat)
435af6e4e0aSAlex Bennée {
436af6e4e0aSAlex Bennée if (!rmatches) {
437af6e4e0aSAlex Bennée rmatches = g_ptr_array_new();
438af6e4e0aSAlex Bennée }
439af6e4e0aSAlex Bennée g_ptr_array_add(rmatches, g_strdup(regpat));
440af6e4e0aSAlex Bennée }
441af6e4e0aSAlex Bennée
4423d7caf14SAlexandre Iooss /**
4433d7caf14SAlexandre Iooss * Install the plugin
4443d7caf14SAlexandre Iooss */
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)4453d7caf14SAlexandre Iooss QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
4463d7caf14SAlexandre Iooss const qemu_info_t *info, int argc,
4473d7caf14SAlexandre Iooss char **argv)
4483d7caf14SAlexandre Iooss {
4493d7caf14SAlexandre Iooss /*
4503d7caf14SAlexandre Iooss * Initialize dynamic array to cache vCPU instruction. In user mode
4513d7caf14SAlexandre Iooss * we don't know the size before emulation.
4523d7caf14SAlexandre Iooss */
453af6e4e0aSAlex Bennée cpus = g_array_sized_new(true, true, sizeof(CPU),
454af6e4e0aSAlex Bennée info->system_emulation ? info->system.max_vcpus : 1);
4553d7caf14SAlexandre Iooss
456b7855bf6SAlex Bennée for (int i = 0; i < argc; i++) {
457b7855bf6SAlex Bennée char *opt = argv[i];
45840258741SAlex Bennée g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
459b7855bf6SAlex Bennée if (g_strcmp0(tokens[0], "ifilter") == 0) {
460b7855bf6SAlex Bennée parse_insn_match(tokens[1]);
461b7855bf6SAlex Bennée } else if (g_strcmp0(tokens[0], "afilter") == 0) {
462b7855bf6SAlex Bennée parse_vaddr_match(tokens[1]);
463af6e4e0aSAlex Bennée } else if (g_strcmp0(tokens[0], "reg") == 0) {
464af6e4e0aSAlex Bennée add_regpat(tokens[1]);
465af6e4e0aSAlex Bennée } else if (g_strcmp0(tokens[0], "rdisas") == 0) {
466af6e4e0aSAlex Bennée if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &disas_assist)) {
467af6e4e0aSAlex Bennée fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
468af6e4e0aSAlex Bennée return -1;
469af6e4e0aSAlex Bennée }
470af6e4e0aSAlex Bennée all_reg_names = g_ptr_array_new();
471b7855bf6SAlex Bennée } else {
472b7855bf6SAlex Bennée fprintf(stderr, "option parsing failed: %s\n", opt);
473b7855bf6SAlex Bennée return -1;
474b7855bf6SAlex Bennée }
475b7855bf6SAlex Bennée }
476b7855bf6SAlex Bennée
477af6e4e0aSAlex Bennée /* Register init, translation block and exit callbacks */
478af6e4e0aSAlex Bennée qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
4793d7caf14SAlexandre Iooss qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
4803d7caf14SAlexandre Iooss qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
4813d7caf14SAlexandre Iooss
4823d7caf14SAlexandre Iooss return 0;
4833d7caf14SAlexandre Iooss }
484