Skip to content

Commit e1f7bdc

Browse files
committed
Add an instruction object
1 parent 4b3a908 commit e1f7bdc

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

contrib/plugins/bap-tracing/tracing.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ static void log_insn_mem_access(unsigned int vcpu_index,
1212
void *userdata) {}
1313

1414
static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
15+
Instruction *insn = udata;
1516
g_rw_lock_reader_lock(&state.vcpus_array_lock);
16-
// VCPU *c = &g_array_index(state.vcpus, VCPU, vcpu_index);
17-
1817
g_rw_lock_writer_lock(&state.frame_buffer_lock);
18+
19+
VCPU *vcpu = &g_array_index(state.vcpus, VCPU, vcpu_index);
20+
GArray *current_regs = qemu_plugin_get_registers();
21+
g_assert(current_regs->len == vcpu->registers->len);
22+
1923
// Add change to previous frame
2024
// Finish previous frame
2125
// Check if buffer should be dumped to file.
@@ -26,20 +30,19 @@ static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
2630
return;
2731
}
2832

29-
static Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc)
30-
{
31-
Register *reg = g_new0(Register, 1);
32-
g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
33-
int r;
33+
Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc) {
34+
Register *reg = g_new0(Register, 1);
35+
g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
36+
int r;
3437

35-
reg->handle = desc->handle;
36-
reg->name = g_intern_string(lower);
37-
reg->content = g_byte_array_new();
38+
reg->handle = desc->handle;
39+
reg->name = g_intern_string(lower);
40+
reg->content = g_byte_array_new();
3841

39-
/* read the initial value */
40-
r = qemu_plugin_read_register(reg->handle, reg->content);
41-
g_assert(r > 0);
42-
return reg;
42+
/* read the initial value */
43+
r = qemu_plugin_read_register(reg->handle, reg->content);
44+
g_assert(r > 0);
45+
return reg;
4346
}
4447

4548
static GPtrArray *registers_init(int vcpu_index) {
@@ -61,7 +64,7 @@ static GPtrArray *registers_init(int vcpu_index) {
6164

6265
static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) {
6366
g_rw_lock_writer_lock(&state.vcpus_array_lock);
64-
VCPU *vcpu = calloc(sizeof(VCPU), 1);
67+
VCPU *vcpu = g_malloc0(sizeof(VCPU));
6568
vcpu->registers = registers_init(vcpu_index);
6669
g_array_insert_vals(state.vcpus, vcpu_index, &vcpu, 1);
6770
g_rw_lock_writer_unlock(&state.vcpus_array_lock);
@@ -71,15 +74,24 @@ static void plugin_exit(qemu_plugin_id_t id, void *udata) {
7174
// Dump rest of frames to file.
7275
}
7376

77+
Instruction *init_insn(struct qemu_plugin_insn *tb_insn) {
78+
Instruction *insn = g_malloc0(sizeof(Instruction));
79+
qemu_plugin_insn_data(tb_insn, &insn->bytes, sizeof(insn->bytes));
80+
insn->size = qemu_plugin_insn_size(tb_insn);
81+
insn->vaddr = qemu_plugin_insn_vaddr(tb_insn);
82+
return insn;
83+
}
84+
7485
static void cb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) {
7586
// Add a callback for each instruction in every translated block.
76-
struct qemu_plugin_insn *insn;
87+
struct qemu_plugin_insn *tb_insn;
7788
size_t n_insns = qemu_plugin_tb_n_insns(tb);
7889
for (size_t i = 0; i < n_insns; i++) {
79-
insn = qemu_plugin_tb_get_insn(tb, i);
80-
qemu_plugin_register_vcpu_insn_exec_cb(insn, log_insn_reg_access,
81-
QEMU_PLUGIN_CB_R_REGS, NULL);
82-
qemu_plugin_register_vcpu_mem_cb(insn, log_insn_mem_access,
90+
tb_insn = qemu_plugin_tb_get_insn(tb, i);
91+
Instruction *insn_data = init_insn(tb_insn);
92+
qemu_plugin_register_vcpu_insn_exec_cb(tb_insn, log_insn_reg_access,
93+
QEMU_PLUGIN_CB_R_REGS, insn_data);
94+
qemu_plugin_register_vcpu_mem_cb(tb_insn, log_insn_mem_access,
8395
QEMU_PLUGIN_CB_R_REGS, QEMU_PLUGIN_MEM_R,
8496
NULL);
8597
}

contrib/plugins/bap-tracing/tracing.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
1010

1111
#define FRAME_BUFFER_SIZE_DEFAULT 1024
1212

13+
/**
14+
* \brief VLIW architecture have instructions longer than 4 or 8bytes.
15+
*/
16+
#define MAX_INSTRUCTION_SIZE 64
17+
1318
typedef struct {
1419
Frame **fbuf;
1520
size_t len;
1621
} FrameBuffer;
1722

1823
typedef struct {
19-
struct qemu_plugin_register *handle; ///< Passed to qemu API.
20-
GByteArray *content;
21-
const char *name;
24+
uint8_t bytes[MAX_INSTRUCTION_SIZE]; ///< Instruction bytes.
25+
size_t size; ///< Len of instruction in bytes.
26+
uint64_t vaddr;
27+
} Instruction;
28+
29+
typedef struct {
30+
struct qemu_plugin_register *handle; ///< Passed to qemu API.
31+
GByteArray *content;
32+
const char *name;
2233
} Register;
2334

2435
typedef struct {
@@ -63,4 +74,7 @@ Frame *frame_new_std(uint64_t addr, int vcpu_id);
6374

6475
void frame_add_operand(Frame *frame, OperandInfo *oi, bool is_out);
6576

77+
Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc);
78+
Instruction *init_insn(struct qemu_plugin_insn *insn);
79+
6680
#endif

0 commit comments

Comments
 (0)