Skip to content

Commit 44a49b6

Browse files
committed
Pad TOC entries when closing the trace.
1 parent e91696e commit 44a49b6

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

contrib/plugins/bap-tracing/frame_buffer.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,25 @@ bool frame_buffer_is_empty(const FrameBuffer *buf) {
164164
}
165165

166166
/// @brief Dumps the file buffer as TOC entry into the file.
167-
uint64_t frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file) {
167+
uint64_t frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file, bool add_padding) {
168168
uint64_t n = 0;
169-
for (size_t i = 0; i <= buf->idx && i < frames_per_toc_entry; ++i) {
170-
Frame *frame = buf->fbuf[i];
171-
size_t msg_size = frame__get_packed_size(frame);
172-
uint8_t *packed_buffer = g_alloca(msg_size);
173-
uint64_t packed_size = frame__pack(frame, packed_buffer);
174-
WRITE(packed_size);
175-
WRITE_BUF(packed_buffer, packed_size);
176-
frame_free(frame);
177-
n++;
169+
for (size_t i = 0; i < frames_per_toc_entry; ++i) {
170+
if (i <= buf->idx) {
171+
Frame *frame = buf->fbuf[i];
172+
size_t msg_size = frame__get_packed_size(frame);
173+
uint8_t *packed_buffer = g_alloca(msg_size);
174+
uint64_t packed_size = frame__pack(frame, packed_buffer);
175+
WRITE(packed_size);
176+
WRITE_BUF(packed_buffer, packed_size);
177+
frame_free(frame);
178+
n++;
179+
} else if (add_padding) {
180+
uint64_t pad = 0;
181+
WRITE(pad);
182+
n++;
183+
} else {
184+
break;
185+
}
178186
}
179187
memset(buf->fbuf, 0, sizeof(buf->fbuf));
180188
buf->idx = 0;

contrib/plugins/bap-tracing/frame_buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef struct {
2828
*/
2929
FrameBuffer *frame_buffer_new(void);
3030

31-
uint64_t frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file);
31+
uint64_t frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file, bool add_padding);
3232
bool frame_buffer_is_full(const FrameBuffer *buf);
3333
bool frame_buffer_is_empty(const FrameBuffer *buf);
3434
void frame_buffer_close_frame(FrameBuffer *buf);

contrib/plugins/bap-tracing/tracing.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ static GPtrArray *registers_init(void) {
100100
return registers->len ? g_steal_pointer(&registers) : NULL;
101101
}
102102

103-
static void write_toc_entry(FrameBuffer *fbuf) {
103+
static void write_toc_entry(FrameBuffer *fbuf, bool add_padding) {
104104
g_rw_lock_writer_lock(&state.file_lock);
105105
g_rw_lock_writer_lock(&state.toc_entries_offsets_lock);
106106
g_rw_lock_writer_lock(&state.total_num_frames_lock);
107107

108-
state.total_num_frames += frame_buffer_flush_to_file(fbuf, state.file);
108+
state.total_num_frames += frame_buffer_flush_to_file(fbuf, state.file, add_padding);
109109
uint64_t next_toc_entry = ftell(state.file);
110110
g_array_append_val(state.toc_entries_offsets, next_toc_entry);
111111

@@ -130,7 +130,7 @@ static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
130130
}
131131

132132
if (frame_buffer_is_full(fbuf)) {
133-
write_toc_entry(fbuf);
133+
write_toc_entry(fbuf, false);
134134
}
135135

136136
// Open new one.
@@ -202,7 +202,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *udata) {
202202
// Dump the rest of the frames.
203203
for (size_t i = 0; i < state.vcpus->len; ++i) {
204204
FrameBuffer *fbuf = g_ptr_array_index(state.frame_buffer, i);
205-
write_toc_entry(fbuf);
205+
write_toc_entry(fbuf, true);
206206
}
207207
g_rw_lock_writer_unlock(&state.frame_buffer_lock);
208208

0 commit comments

Comments
 (0)