Skip to content

Commit 9916241

Browse files
committed
Add skeleton implementation of tracing plugin.
1 parent ff3419c commit 9916241

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

contrib/plugins/execlog_bap.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: 2025 Rot127 <[email protected]>
2+
// SPDX-License-Identifier: GPL-2.0-only
3+
4+
#include <qemu-plugin.h>
5+
6+
QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
7+
8+
typedef struct {
9+
// Current instruction related things.
10+
} VCPU;
11+
12+
typedef struct {
13+
GRWLock vcpus_array_lock;
14+
GArray *vcpus;
15+
16+
GRWLock frame_buffer_lock;
17+
GPtrArray *frame_buffer;
18+
} TraceState;
19+
20+
static TraceState state;
21+
22+
static VCPU *get_vcpu(TraceState *state, int vcpu_index) {
23+
VCPU *c;
24+
g_rw_lock_reader_lock(&state->vcpus_array_lock);
25+
c = &g_array_index(state->vcpus, VCPU, vcpu_index);
26+
g_rw_lock_reader_unlock(&state->vcpus_array_lock);
27+
28+
return c;
29+
}
30+
31+
static void log_insn_frame(unsigned int cpu_index, void *udata) {
32+
// VCPU *vcpu = get_vcpu(state, cpu_index);
33+
34+
// Add change to previous frame
35+
// Finish previous frame
36+
// Check if buffer should be dumped to file.
37+
// Open new one.
38+
return;
39+
}
40+
41+
static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) {
42+
// Add new vcpu
43+
}
44+
45+
static void plugin_exit(qemu_plugin_id_t id, void *udata) {
46+
// Dump rest of frames to file.
47+
}
48+
49+
static void cb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) {
50+
// Add a callback for each instruction in every translated block.
51+
struct qemu_plugin_insn *insn;
52+
size_t n_insns = qemu_plugin_tb_n_insns(tb);
53+
for (size_t i = 0; i < n_insns; i++) {
54+
insn = qemu_plugin_tb_get_insn(tb, i);
55+
qemu_plugin_register_vcpu_insn_exec_cb(insn, log_insn_frame,
56+
QEMU_PLUGIN_CB_R_REGS, &state);
57+
}
58+
}
59+
60+
QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
61+
const qemu_info_t *info, int argc,
62+
char **argv) {
63+
qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
64+
qemu_plugin_register_vcpu_tb_trans_cb(id, cb_trans);
65+
qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
66+
67+
// Get reg names
68+
// qemu_plugin_get_registers
69+
//
70+
// Logging
71+
// qemu_plugin_outs
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)