Skip to content

Commit 87407fe

Browse files
committed
Add pre register states and some clean up
1 parent dbbeb5c commit 87407fe

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

contrib/plugins/bap-tracing/tracing.c

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ static void log_insn_mem_access(unsigned int vcpu_index,
1111
qemu_plugin_meminfo_t info, uint64_t vaddr,
1212
void *userdata) {}
1313

14-
static void add_post_state_regs(VCPU *vcpu, unsigned int vcpu_index, GArray *current_regs) {
14+
static void add_post_reg_state(VCPU *vcpu, unsigned int vcpu_index,
15+
GArray *current_regs, FrameBuffer *fbuf) {
1516
GByteArray *rtmp = g_byte_array_new();
1617
for (size_t i = 0; i < current_regs->len; ++i) {
1718
Register *prev_reg = vcpu->registers->pdata[i];
@@ -26,36 +27,75 @@ static void add_post_state_regs(VCPU *vcpu, unsigned int vcpu_index, GArray *cur
2627
}
2728

2829
OperandInfo *rinfo = init_reg_operand_info(prev_reg->name, rtmp->data,
29-
rtmp->len, OperandRead);
30+
rtmp->len, OperandWritten);
3031
g_assert(rinfo);
32+
frame_buffer_append_op_info(fbuf, rinfo);
33+
}
34+
}
3135

32-
g_rw_lock_writer_lock(&state.frame_buffer_lock);
33-
FrameBuffer *fb = g_ptr_array_index(state.frame_buffer, vcpu_index);
34-
frame_buffer_append_op_info(fb, rinfo);
35-
g_rw_lock_writer_unlock(&state.frame_buffer_lock);
36+
static void add_pre_reg_state(VCPU *vcpu, unsigned int vcpu_index,
37+
GArray *current_regs, FrameBuffer *fbuf) {
38+
GByteArray *rtmp = g_byte_array_new();
39+
for (size_t i = 0; i < current_regs->len; ++i) {
40+
qemu_plugin_reg_descriptor *reg =
41+
&g_array_index(current_regs, qemu_plugin_reg_descriptor, i);
42+
qemu_plugin_read_register(reg->handle, rtmp);
43+
OperandInfo *rinfo = init_reg_operand_info(reg->name, rtmp->data,
44+
rtmp->len, OperandRead);
45+
g_assert(rinfo);
46+
frame_buffer_append_op_info(fbuf, rinfo);
3647
}
3748
}
3849

50+
static void add_new_insn_frame(VCPU *vcpu, unsigned int vcpu_index,
51+
FrameBuffer *fbuf, Instruction *insn) {
52+
Frame *frame = frame_buffer_new_frame(fbuf);
53+
frame__init(frame);
54+
55+
StdFrame *sframe = g_new(StdFrame, 1);
56+
std_frame__init(sframe);
57+
frame->std_frame = sframe;
58+
59+
sframe->thread_id = vcpu_index;
60+
sframe->address = insn->vaddr;
61+
sframe->rawbytes.len = insn->size;
62+
sframe->rawbytes.data = g_malloc(insn->size);
63+
memcpy(sframe->rawbytes.data, insn->bytes, insn->size);
64+
65+
OperandValueList *ol_in = g_new(OperandValueList, 1);
66+
operand_value_list__init(ol_in);
67+
ol_in->n_elem = 0;
68+
sframe->operand_pre_list = ol_in;
69+
70+
OperandValueList *ol_out = g_new(OperandValueList, 1);
71+
operand_value_list__init(ol_out);
72+
ol_out->n_elem = 0;
73+
sframe->operand_post_list = ol_out;
74+
}
75+
3976
static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
4077
g_rw_lock_reader_lock(&state.vcpus_array_lock);
78+
g_rw_lock_writer_lock(&state.frame_buffer_lock);
79+
g_rw_lock_writer_lock(&state.file_lock);
4180

81+
FrameBuffer *fbuf = g_ptr_array_index(state.frame_buffer, vcpu_index);
4282
VCPU *vcpu = &g_array_index(state.vcpus, VCPU, vcpu_index);
4383
GArray *current_regs = qemu_plugin_get_registers();
4484
g_assert(current_regs->len == vcpu->registers->len);
4585

46-
add_post_state_regs(vcpu, vcpu_index, current_regs);
86+
add_post_reg_state(vcpu, vcpu_index, current_regs, fbuf);
4787

48-
g_rw_lock_writer_lock(&state.frame_buffer_lock);
49-
g_rw_lock_writer_lock(&state.file_lock);
50-
FrameBuffer *vcpu_buf = g_ptr_array_index(state.frame_buffer, vcpu_index);
51-
if (frame_buffer_is_full(vcpu_buf)) {
52-
frame_buffer_flush_to_file(vcpu_buf, state.file);
88+
if (frame_buffer_is_full(fbuf)) {
89+
frame_buffer_flush_to_file(fbuf, state.file);
5390
}
54-
g_rw_lock_writer_unlock(&state.file_lock);
55-
g_rw_lock_writer_unlock(&state.frame_buffer_lock);
5691

5792
// Open new one.
5893
Instruction *insn = udata;
94+
add_new_insn_frame(vcpu, vcpu_index, fbuf, insn);
95+
add_pre_reg_state(vcpu, vcpu_index, current_regs, fbuf);
96+
97+
g_rw_lock_writer_unlock(&state.file_lock);
98+
g_rw_lock_writer_unlock(&state.frame_buffer_lock);
5999
g_rw_lock_reader_unlock(&state.vcpus_array_lock);
60100

61101
return;

contrib/plugins/bap-tracing/tracing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ bool frame_buffer_push(FrameBuffer *buf, Frame *frame);
7171
void frame_buffer_flush_to_file(FrameBuffer *buf, FILE *file);
7272
bool frame_buffer_is_full(const FrameBuffer *buf);
7373

74-
void frame_buffer_new_frame(FrameBuffer *buf);
74+
Frame *frame_buffer_new_frame(FrameBuffer *buf);
7575
void frame_buffer_append_op_info(FrameBuffer *buf, OperandInfo *oi);
7676

7777
/**

0 commit comments

Comments
 (0)