Skip to content

Commit b820d38

Browse files
committed
Init registers for VCPU
1 parent 7054ef8 commit b820d38

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

contrib/plugins/bap-tracing/tracing.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
static TraceState state;
99

10-
static void log_insn_exec(unsigned int vcpu_index, void *udata) {
10+
static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
1111
g_rw_lock_reader_lock(&state.vcpus_array_lock);
1212
// VCPU *c = &g_array_index(state.vcpus, VCPU, vcpu_index);
1313

@@ -22,9 +22,43 @@ static void log_insn_exec(unsigned int vcpu_index, void *udata) {
2222
return;
2323
}
2424

25+
static Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc)
26+
{
27+
Register *reg = g_new0(Register, 1);
28+
g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
29+
int r;
30+
31+
reg->handle = desc->handle;
32+
reg->name = g_intern_string(lower);
33+
reg->content = g_byte_array_new();
34+
35+
/* read the initial value */
36+
r = qemu_plugin_read_register(reg->handle, reg->content);
37+
g_assert(r > 0);
38+
return reg;
39+
}
40+
41+
static GPtrArray *registers_init(int vcpu_index) {
42+
g_autoptr(GPtrArray) registers = g_ptr_array_new();
43+
g_autoptr(GArray) reg_list = qemu_plugin_get_registers();
44+
45+
if (!reg_list->len) {
46+
return NULL;
47+
}
48+
for (int r = 0; r < reg_list->len; r++) {
49+
qemu_plugin_reg_descriptor *rd =
50+
&g_array_index(reg_list, qemu_plugin_reg_descriptor, r);
51+
Register *reg = init_vcpu_register(rd);
52+
g_ptr_array_add(registers, reg);
53+
}
54+
55+
return registers->len ? g_steal_pointer(&registers) : NULL;
56+
}
57+
2558
static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) {
2659
g_rw_lock_writer_lock(&state.vcpus_array_lock);
2760
VCPU *vcpu = calloc(sizeof(VCPU), 1);
61+
vcpu->registers = registers_init(vcpu_index);
2862
g_array_insert_vals(state.vcpus, vcpu_index, &vcpu, 1);
2963
g_rw_lock_writer_unlock(&state.vcpus_array_lock);
3064
}
@@ -39,7 +73,7 @@ static void cb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) {
3973
size_t n_insns = qemu_plugin_tb_n_insns(tb);
4074
for (size_t i = 0; i < n_insns; i++) {
4175
insn = qemu_plugin_tb_get_insn(tb, i);
42-
qemu_plugin_register_vcpu_insn_exec_cb(insn, log_insn_exec,
76+
qemu_plugin_register_vcpu_insn_exec_cb(insn, log_insn_reg_access,
4377
QEMU_PLUGIN_CB_R_REGS, NULL);
4478
}
4579
}

contrib/plugins/bap-tracing/tracing.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ typedef struct {
1616
} FrameBuffer;
1717

1818
typedef struct {
19-
// Current instruction related things.
19+
struct qemu_plugin_register *handle; ///< Passed to qemu API.
20+
GByteArray *content;
21+
const char *name;
22+
} Register;
23+
24+
typedef struct {
25+
GPtrArray /*<Register>*/ *registers;
2026
} VCPU;
2127

2228
typedef struct {
2329
GRWLock vcpus_array_lock;
24-
GArray *vcpus;
30+
GArray /*<VCPU>*/ *vcpus;
2531

2632
GRWLock frame_buffer_lock;
2733
FrameBuffer *frame_buffer;

0 commit comments

Comments
 (0)