Skip to content

Commit 363aefe

Browse files
committed
Fix and print register tracing.
1 parent 9d7f9aa commit 363aefe

File tree

3 files changed

+80
-22
lines changed

3 files changed

+80
-22
lines changed

contrib/plugins/bap-tracing/frame_buffer.c

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,62 @@ FrameBuffer *frame_buffer_new(size_t size) {
9595
}
9696

9797
bool frame_buffer_is_full(const FrameBuffer *buf) {
98-
return buf->idx >= buf->max_size;
98+
return buf->idx + 1 >= buf->max_size;
99+
}
100+
101+
void frame_buffer_close_frame(FrameBuffer *buf) {
102+
char *str = frame_buffer_as_str(buf);
103+
qemu_plugin_outs("Close frame: ");
104+
qemu_plugin_outs(str);
105+
qemu_plugin_outs("\n\n");
106+
g_free(str);
107+
buf->idx++;
108+
}
109+
110+
#define FRAME_STR_SIZE 8192
111+
112+
#define APPEND(...) \
113+
snprintf(str + off, max - off, __VA_ARGS__); \
114+
off = strlen(str);
115+
116+
char *frame_buffer_as_str(const FrameBuffer *buf) {
117+
char *str = g_malloc0(FRAME_STR_SIZE);
118+
const Frame *frame = buf->fbuf[buf->idx];
119+
if (!frame) {
120+
snprintf(str, FRAME_STR_SIZE, "<NULL>");
121+
return str;
122+
}
123+
size_t max = FRAME_STR_SIZE - 1;
124+
snprintf(str, max, "{ pre: [ ");
125+
size_t off = strlen(str);
126+
127+
StdFrame *sframe = frame->std_frame;
128+
for (size_t i = 0; i < sframe->operand_pre_list->n_elem; i++) {
129+
OperandInfo *oi = sframe->operand_pre_list->elem[i];
130+
APPEND("r:%s=", oi->operand_info_specific->reg_operand->name);
131+
132+
for (size_t k = 0; k < oi->value.len; ++k) {
133+
APPEND("%02x", oi->value.data[k]);
134+
}
135+
APPEND(", ");
136+
}
137+
APPEND(" ], post: [ ");
138+
for (size_t i = 0; i < sframe->operand_post_list->n_elem; i++) {
139+
OperandInfo *oi = sframe->operand_post_list->elem[i];
140+
APPEND("r:%s=", oi->operand_info_specific->reg_operand->name);
141+
142+
for (size_t k = 0; k < oi->value.len; ++k) {
143+
APPEND("%02x", oi->value.data[k]);
144+
}
145+
APPEND(", ");
146+
}
147+
148+
APPEND("]}");
149+
return str;
150+
}
151+
152+
bool frame_buffer_is_empty(const FrameBuffer *buf) {
153+
return buf->fbuf[buf->idx] == NULL;
99154
}
100155

101156
void frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file) {
@@ -143,15 +198,15 @@ bool frame_buffer_new_frame_std(FrameBuffer *buf, unsigned int thread_id,
143198
ol_out->n_elem = 0;
144199
stdframe->operand_post_list = ol_out;
145200

146-
buf->fbuf[buf->idx++] = frame;
201+
buf->fbuf[buf->idx] = frame;
147202
return true;
148203
}
149204

150205
bool frame_buffer_append_reg_info(FrameBuffer *buf, const char *name,
151-
const GByteArray *content,
206+
const GByteArray *content, size_t reg_size,
152207
OperandAccess acc) {
153-
OperandInfo *oi =
154-
frame_init_reg_operand_info(name, content->data, content->len, acc);
208+
OperandInfo *oi = frame_init_reg_operand_info(
209+
name, content->data + content->len - reg_size, reg_size, acc);
155210
g_assert(oi);
156211
Frame *frame = buf->fbuf[buf->idx];
157212
if (!frame || !frame->std_frame) {

contrib/plugins/bap-tracing/frame_buffer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ FrameBuffer *frame_buffer_new(size_t size);
3131

3232
void frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file);
3333
bool frame_buffer_is_full(const FrameBuffer *buf);
34+
bool frame_buffer_is_empty(const FrameBuffer *buf);
35+
void frame_buffer_close_frame(FrameBuffer *buf);
36+
char *frame_buffer_as_str(const FrameBuffer *buf);
3437

3538
bool frame_buffer_new_frame_std(FrameBuffer *buf,
3639
unsigned int thread_id, uint64_t vaddr,
@@ -40,7 +43,7 @@ bool frame_buffer_new_frame_std(FrameBuffer *buf,
4043
* \brief Appends the given operand info to the open frame.
4144
*/
4245
bool frame_buffer_append_reg_info(FrameBuffer *buf, const char *name,
43-
const GByteArray *content,
46+
const GByteArray *content, size_t reg_size,
4447
OperandAccess acc);
4548

4649
OperandInfo *frame_init_reg_operand_info(const char *name, const uint8_t *value,

contrib/plugins/bap-tracing/tracing.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static void log_insn_mem_access(unsigned int vcpu_index,
1515

1616
static void add_post_reg_state(VCPU *vcpu, unsigned int vcpu_index,
1717
GArray *current_regs, FrameBuffer *fbuf) {
18+
1819
GByteArray *rdata = g_byte_array_new();
1920
for (size_t i = 0; i < current_regs->len; ++i) {
2021
Register *prev_reg = vcpu->registers->pdata[i];
@@ -28,7 +29,8 @@ static void add_post_reg_state(VCPU *vcpu, unsigned int vcpu_index,
2829
continue;
2930
}
3031

31-
if (!frame_buffer_append_reg_info(fbuf, reg->name, rdata, OperandWritten)) {
32+
if (!frame_buffer_append_reg_info(fbuf, reg->name, rdata, s,
33+
OperandWritten)) {
3234
qemu_plugin_outs("Failed to append opinfo.\n");
3335
g_assert(false);
3436
}
@@ -41,15 +43,15 @@ static void add_pre_reg_state(VCPU *vcpu, unsigned int vcpu_index,
4143
for (size_t i = 0; i < current_regs->len; ++i) {
4244
qemu_plugin_reg_descriptor *reg =
4345
&g_array_index(current_regs, qemu_plugin_reg_descriptor, i);
44-
qemu_plugin_read_register(reg->handle, rdata);
45-
frame_buffer_append_reg_info(fbuf, reg->name, rdata, OperandRead);
46+
size_t s = qemu_plugin_read_register(reg->handle, rdata);
47+
frame_buffer_append_reg_info(fbuf, reg->name, rdata, s, OperandRead);
4648
}
4749
}
4850

49-
static void add_new_insn_frame(VCPU *vcpu, unsigned int vcpu_index,
51+
static bool add_new_insn_frame(VCPU *vcpu, unsigned int vcpu_index,
5052
FrameBuffer *fbuf, Instruction *insn) {
51-
frame_buffer_new_frame_std(fbuf, vcpu_index, insn->vaddr, insn->bytes,
52-
insn->size);
53+
return frame_buffer_new_frame_std(fbuf, vcpu_index, insn->vaddr, insn->bytes,
54+
insn->size);
5355
}
5456

5557
static GPtrArray *registers_init(void) {
@@ -77,18 +79,13 @@ static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
7779
FrameBuffer *fbuf = g_ptr_array_index(state.frame_buffer, vcpu_index);
7880
VCPU *vcpu = g_ptr_array_index(state.vcpus, vcpu_index);
7981
g_assert(vcpu);
80-
if (!vcpu->registers) {
81-
vcpu->registers = registers_init();
82-
if (!vcpu->registers) {
83-
// Registers are still not available. So return until the VCPU is
84-
// sufficiently initialized.
85-
goto unlock_return;
86-
}
87-
}
8882
GArray *current_regs = qemu_plugin_get_registers();
8983
g_assert(current_regs->len == vcpu->registers->len);
9084

91-
add_post_reg_state(vcpu, vcpu_index, current_regs, fbuf);
85+
if (!frame_buffer_is_empty(fbuf)) {
86+
add_post_reg_state(vcpu, vcpu_index, current_regs, fbuf);
87+
frame_buffer_close_frame(fbuf);
88+
}
9289

9390
if (frame_buffer_is_full(fbuf)) {
9491
g_rw_lock_writer_lock(&state.file_lock);
@@ -101,7 +98,6 @@ static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
10198
add_new_insn_frame(vcpu, vcpu_index, fbuf, insn);
10299
add_pre_reg_state(vcpu, vcpu_index, current_regs, fbuf);
103100

104-
unlock_return:
105101
g_rw_lock_reader_unlock(&state.frame_buffer_lock);
106102
g_rw_lock_reader_unlock(&state.vcpus_array_lock);
107103
}
@@ -125,6 +121,10 @@ static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) {
125121
g_rw_lock_writer_lock(&state.frame_buffer_lock);
126122

127123
VCPU *vcpu = g_malloc0(sizeof(VCPU));
124+
vcpu->registers = registers_init();
125+
if (!vcpu->registers) {
126+
g_assert(false);
127+
}
128128
g_ptr_array_insert(state.vcpus, vcpu_index, vcpu);
129129

130130
FrameBuffer *vcpu_frame_buffer = frame_buffer_new(FRAME_BUFFER_SIZE_DEFAULT);

0 commit comments

Comments
 (0)