Skip to content

Commit 6c245ae

Browse files
committed
Add back some of the meta frame code
1 parent 9d79ae1 commit 6c245ae

File tree

5 files changed

+180
-45
lines changed

5 files changed

+180
-45
lines changed

contrib/plugins/bap-tracing/frame_buffer.c

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@
22
// SPDX-License-Identifier: GPL-2.0-only
33

44
#include "frame_buffer.h"
5-
6-
#define WRITE(x) \
7-
do { \
8-
if (fwrite(&(x), sizeof(x), 1, file) != 1) \
9-
qemu_plugin_outs("fwrite failed"); \
10-
} while (0)
11-
12-
#define WRITE_BUF(x, n) \
13-
do { \
14-
if (fwrite((x), 1, (n), file) != n) \
15-
qemu_plugin_outs("fwrite failed"); \
16-
} while (0)
5+
#include "trace_meta.h"
176

187
static Frame *frame_new_std(uint64_t addr, int vcpu_id) {
198
Frame *frame = g_new(Frame, 1);
@@ -39,22 +28,22 @@ static Frame *frame_new_std(uint64_t addr, int vcpu_id) {
3928
}
4029

4130
static inline void free_operand(OperandInfo *oi) {
42-
OperandInfoSpecific *ois = oi->operand_info_specific;
43-
44-
//Free reg-operand
45-
RegOperand *ro = ois->reg_operand;
46-
if (ro && ro->name)
47-
g_free(ro->name);
48-
g_free(ro);
49-
50-
//Free mem-operand
51-
MemOperand *mo = ois->mem_operand;
52-
g_free(mo);
53-
g_free(oi->value.data);
54-
g_free(oi->taint_info);
55-
g_free(ois);
56-
g_free(oi->operand_usage);
57-
g_free(oi);
31+
OperandInfoSpecific *ois = oi->operand_info_specific;
32+
33+
// Free reg-operand
34+
RegOperand *ro = ois->reg_operand;
35+
if (ro && ro->name)
36+
g_free(ro->name);
37+
g_free(ro);
38+
39+
// Free mem-operand
40+
MemOperand *mo = ois->mem_operand;
41+
g_free(mo);
42+
g_free(oi->value.data);
43+
g_free(oi->taint_info);
44+
g_free(ois);
45+
g_free(oi->operand_usage);
46+
g_free(oi);
5847
}
5948

6049
static void frame_free(Frame *frame) {
@@ -79,17 +68,12 @@ static void frame_free(Frame *frame) {
7968
g_free(frame);
8069
}
8170

82-
static bool frame_add_operand(Frame *frame, OperandInfo *oi) {
83-
if (!frame->std_frame) {
84-
qemu_plugin_outs(
85-
"Append operand info to non-std frames is not implemented.");
86-
return false;
87-
}
71+
static bool std_frame_add_operand(StdFrame *std_frame, OperandInfo *oi) {
8872
OperandValueList *ol;
8973
if (oi->operand_usage->written) {
90-
ol = frame->std_frame->operand_post_list;
74+
ol = std_frame->operand_post_list;
9175
} else {
92-
ol = frame->std_frame->operand_pre_list;
76+
ol = std_frame->operand_pre_list;
9377
}
9478

9579
oi->taint_info = g_new(TaintInfo, 1);
@@ -170,12 +154,12 @@ bool frame_buffer_append_reg_info(FrameBuffer *buf, const char *name,
170154
frame_init_reg_operand_info(name, content->data, content->len, acc);
171155
g_assert(oi);
172156
Frame *frame = buf->fbuf[buf->idx];
173-
if (!frame) {
157+
if (!frame || !frame->std_frame) {
174158
qemu_plugin_outs(
175159
"Attempt to append operand info to a uninitialzied frame.");
176160
return false;
177161
}
178-
return frame_add_operand(frame, oi);
162+
return std_frame_add_operand(frame->std_frame, oi);
179163
}
180164

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

contrib/plugins/bap-tracing/frame_buffer.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
#include <qemu-plugin.h>
99
#include <stdio.h>
1010

11+
#include "trace_meta.h"
1112
#include "frame.piqi.pb-c-patched.h"
1213

13-
/**
14-
* \brief Empty macros indicate the argument, variable etc.
15-
* must be locked for writing.
16-
*/
17-
#define WLOCKED
18-
1914
typedef enum {
2015
OperandRead = 1,
2116
OperandWritten = 2,
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// SPDX-FileCopyrightText: 2025 Rot127 <[email protected]>
2+
// SPDX-License-Identifier: GPL-2.0-only
3+
4+
#include <glib.h>
5+
#include <qemu-plugin.h>
6+
#include <stdio.h>
7+
#include <sys/stat.h>
8+
9+
#include "frame.piqi.pb-c-patched.h"
10+
#include "trace_meta.h"
11+
12+
#define MD5LEN 16
13+
14+
static void compute_target_md5(const char *binary_path) {
15+
const GChecksumType md5 = G_CHECKSUM_MD5;
16+
guchar target_md5[MD5LEN];
17+
18+
GChecksum *cs = g_checksum_new(md5);
19+
FILE *target = fopen(binary_path, "r");
20+
guchar buf[BUFSIZ];
21+
gsize expected_length = MD5LEN;
22+
23+
if (!cs)
24+
qemu_plugin_outs("failed to create a checksum");
25+
if (!target)
26+
qemu_plugin_outs("failed to open target binary");
27+
if (g_checksum_type_get_length(md5) != expected_length)
28+
abort();
29+
30+
while (!feof(target)) {
31+
size_t len = fread(buf, 1, BUFSIZ, target);
32+
if (ferror(target))
33+
qemu_plugin_outs("failed to read target binary");
34+
g_checksum_update(cs, buf, len);
35+
}
36+
37+
g_checksum_get_digest(cs, target_md5, &expected_length);
38+
fclose(target);
39+
}
40+
41+
static void meta_write_header(FILE *file) {
42+
// uint64_t toc_off = 0L;
43+
// WRITE(magic_number);
44+
// WRITE(out_trace_version);
45+
// WRITE(frame_arch);
46+
// WRITE(frame_mach);
47+
// WRITE(toc_num_frames);
48+
// WRITE(toc_off);
49+
}
50+
51+
static void init_tracer(Tracer *tracer, char **argv, char **envp) {
52+
// tracer__init(tracer);
53+
// tracer->name = tracer_name;
54+
// tracer->n_args = list_length(argv);
55+
// tracer->args = argv;
56+
// tracer->n_envp = list_length(envp);
57+
// tracer->envp = envp;
58+
// tracer->version = tracer_version;
59+
}
60+
61+
static void init_target(Target *target, char **argv, char **envp) {
62+
// compute_target_md5();
63+
64+
// target__init(target);
65+
// target->path = target_path;
66+
// target->n_args = list_length(argv);
67+
// target->args = argv;
68+
// target->n_envp = list_length(envp);
69+
// target->envp = envp;
70+
// target->md5sum.len = MD5LEN;
71+
// target->md5sum.data = target_md5;
72+
}
73+
74+
#ifdef G_OS_UNIX
75+
static bool unix_fill_fstats(Fstats *fstats, const char *path) {
76+
struct stat stats;
77+
if (stat(path, &stats) < 0) {
78+
qemu_plugin_outs("failed to obtain file stats");
79+
return false;
80+
}
81+
82+
fstats->size = stats.st_size;
83+
fstats->atime = stats.st_atime;
84+
fstats->mtime = stats.st_mtime;
85+
fstats->ctime = stats.st_ctime;
86+
return true;
87+
}
88+
#endif
89+
90+
static bool init_fstats(Fstats *fstats, const char *binary_path) {
91+
fstats__init(fstats);
92+
#ifdef G_OS_UNIX
93+
return unix_fill_fstats(fstats, binary_path);
94+
#endif
95+
return true;
96+
}
97+
98+
static void write_meta(WLOCKED FILE *file, char **tracer_argv,
99+
char **tracer_envp, char **target_argv,
100+
char **target_envp) {
101+
MetaFrame meta;
102+
Tracer tracer;
103+
Target target;
104+
Fstats fstats;
105+
106+
meta_frame__init(&meta);
107+
init_tracer(&tracer, tracer_argv, tracer_envp);
108+
init_target(&target, target_argv, target_envp);
109+
init_fstats(&fstats, "target-path");
110+
111+
meta.tracer = &tracer;
112+
meta.target = &target;
113+
meta.fstats = &fstats;
114+
meta.time = time(NULL);
115+
char *user = g_strdup(g_get_real_name());
116+
meta.user = user;
117+
118+
char *host = g_strdup(g_get_host_name());
119+
meta.host = host;
120+
121+
size_t msg_size = meta_frame__get_packed_size(&meta);
122+
uint8_t *packed_buffer = g_alloca(msg_size);
123+
uint64_t packed_size = meta_frame__pack(&meta, packed_buffer);
124+
WRITE(packed_size);
125+
WRITE_BUF(&meta, packed_size);
126+
127+
free(user);
128+
free(host);
129+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-FileCopyrightText: 2025 Rot127 <[email protected]>
2+
// SPDX-License-Identifier: GPL-2.0-only
3+
4+
#ifndef BAP_TRACE_META_H
5+
#define BAP_TRACE_META_H
6+
7+
/**
8+
* \brief Empty macros indicate the argument, variable etc.
9+
* must be locked for writing.
10+
*/
11+
#define WLOCKED
12+
13+
#define WRITE(x) \
14+
do { \
15+
if (fwrite(&(x), sizeof(x), 1, file) != 1) \
16+
qemu_plugin_outs("fwrite failed"); \
17+
} while (0)
18+
19+
#define WRITE_BUF(x, n) \
20+
do { \
21+
if (fwrite((x), 1, (n), file) != n) \
22+
qemu_plugin_outs("fwrite failed"); \
23+
} while (0)
24+
25+
#endif

contrib/plugins/bap-tracing/tracing.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
156156
const char *target_path = "/tmp/test.trace";
157157
state.frame_buffer = g_ptr_array_new();
158158
state.vcpus = g_array_new(false, true, sizeof(VCPU));
159-
state.file = fopen(target_path, "r");
159+
state.file = fopen(target_path, "wb");
160160
if (!(state.frame_buffer || state.vcpus || state.file)) {
161161
return 1;
162162
}
163+
// write_header();
164+
// write_meta(argv, envp, target_argv, target_envp);
163165

164166
qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
165167
qemu_plugin_register_vcpu_tb_trans_cb(id, cb_trans);

0 commit comments

Comments
 (0)