Skip to content

Commit 7054ef8

Browse files
committed
Imlpement plugin setup, vcpu init and locks for insn logging.
1 parent e3a5dc8 commit 7054ef8

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

contrib/plugins/bap-tracing/tracing.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,26 @@
77

88
static TraceState state;
99

10-
#pragma GCC diagnostic push
11-
#pragma GCC diagnostic ignored "-Wunused-function"
12-
static VCPU *get_vcpu(TraceState *state, int vcpu_index) {
13-
VCPU *c;
14-
g_rw_lock_reader_lock(&state->vcpus_array_lock);
15-
c = &g_array_index(state->vcpus, VCPU, vcpu_index);
16-
g_rw_lock_reader_unlock(&state->vcpus_array_lock);
17-
18-
return c;
19-
}
20-
#pragma GCC diagnostic pop
21-
22-
static void log_insn_frame(unsigned int cpu_index, void *udata) {
23-
// VCPU *vcpu = get_vcpu(state, cpu_index);
10+
static void log_insn_exec(unsigned int vcpu_index, void *udata) {
11+
g_rw_lock_reader_lock(&state.vcpus_array_lock);
12+
// VCPU *c = &g_array_index(state.vcpus, VCPU, vcpu_index);
2413

14+
g_rw_lock_writer_lock(&state.frame_buffer_lock);
2515
// Add change to previous frame
2616
// Finish previous frame
2717
// Check if buffer should be dumped to file.
2818
// Open new one.
19+
g_rw_lock_writer_unlock(&state.frame_buffer_lock);
20+
g_rw_lock_reader_unlock(&state.vcpus_array_lock);
21+
2922
return;
3023
}
3124

3225
static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) {
33-
// Add new vcpu
26+
g_rw_lock_writer_lock(&state.vcpus_array_lock);
27+
VCPU *vcpu = calloc(sizeof(VCPU), 1);
28+
g_array_insert_vals(state.vcpus, vcpu_index, &vcpu, 1);
29+
g_rw_lock_writer_unlock(&state.vcpus_array_lock);
3430
}
3531

3632
static void plugin_exit(qemu_plugin_id_t id, void *udata) {
@@ -43,23 +39,25 @@ static void cb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) {
4339
size_t n_insns = qemu_plugin_tb_n_insns(tb);
4440
for (size_t i = 0; i < n_insns; i++) {
4541
insn = qemu_plugin_tb_get_insn(tb, i);
46-
qemu_plugin_register_vcpu_insn_exec_cb(insn, log_insn_frame,
47-
QEMU_PLUGIN_CB_R_REGS, &state);
42+
qemu_plugin_register_vcpu_insn_exec_cb(insn, log_insn_exec,
43+
QEMU_PLUGIN_CB_R_REGS, NULL);
4844
}
4945
}
5046

5147
QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
5248
const qemu_info_t *info, int argc,
5349
char **argv) {
50+
const char *target_path = "/tmp/test.trace";
51+
state.frame_buffer = frame_buffer_init(FRAME_BUFFER_SIZE);
52+
state.vcpus = g_array_new(false, true, sizeof(VCPU));
53+
state.file = fopen(target_path, "r");
54+
if (!(state.frame_buffer || state.vcpus || state.file)) {
55+
return 1;
56+
}
57+
5458
qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
5559
qemu_plugin_register_vcpu_tb_trans_cb(id, cb_trans);
5660
qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
5761

58-
// Get reg names
59-
// qemu_plugin_get_registers
60-
//
61-
// Logging
62-
// qemu_plugin_outs
63-
6462
return 0;
6563
}

0 commit comments

Comments
 (0)