Skip to content

Commit 6d61f9c

Browse files
committed
Store values always in little endian as the trace defines it.
1 parent cc17940 commit 6d61f9c

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

contrib/plugins/bap-tracing/trace_meta.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,25 @@ void write_meta(WLOCKED FILE *file, char **plugin_argv, size_t plugin_argc) {
191191
g_free(host);
192192
g_free(arg_bin_path);
193193
}
194+
195+
/// Copies src to dst. dst will always be in little endian byte order.
196+
void memcpy_le(uint8_t *dst, uint8_t *src, size_t len, bool big_endian) {
197+
if (!big_endian) {
198+
memcpy(dst, src, len);
199+
return;
200+
}
201+
for (size_t k = 0; k < len; ++k) {
202+
dst[k] = src[len - 1 - k];
203+
}
204+
}
205+
206+
void swap_to_le(uint8_t *buf, size_t len, bool big_endian) {
207+
if (!big_endian || len == 1) {
208+
return;
209+
}
210+
for (size_t k = 0; k < len / 2; ++k) {
211+
uint8_t tmp = buf[k];
212+
buf[k] = buf[len - 1 - k];
213+
buf[len - 1 - k] = tmp;
214+
}
215+
}

contrib/plugins/bap-tracing/trace_meta.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define BAP_TRACE_META_H
66

77
#include <err.h>
8+
#include <stdbool.h>
9+
#include <stdint.h>
810
#include <stdio.h>
911

1012
/**
@@ -34,5 +36,7 @@
3436
void write_meta(WLOCKED FILE *file, char **plugin_argv, size_t plugin_argc);
3537
char *get_argv_val(char **argv, int argc, const char *key);
3638
void file_exists_exit(const char *file);
39+
void memcpy_le(uint8_t *dst, uint8_t *src, size_t len, bool big_endian);
40+
void swap_to_le(uint8_t *buf, size_t len, bool big_endian);
3741

3842
#endif

contrib/plugins/bap-tracing/tracing.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "frame_buffer.h"
1010
#include "qemu-plugin.h"
1111
#include "trace_consts.h"
12+
#include "trace_meta.h"
1213
#include "tracing.h"
1314

1415
static TraceState state = {0};
@@ -52,6 +53,7 @@ static void add_post_reg_state(VCPU *vcpu, unsigned int vcpu_index,
5253
&g_array_index(current_regs, qemu_plugin_reg_descriptor, i);
5354
int s = qemu_plugin_read_register(reg->handle, rdata);
5455
assert(s == prev_reg->content->len);
56+
swap_to_le(rdata->data, s, state.is_big_endian);
5557
if (!memcmp(rdata->data, prev_reg->content->data, s)) {
5658
// No change
5759
// Flush byte array
@@ -79,7 +81,7 @@ static void add_pre_reg_state(VCPU *vcpu, unsigned int vcpu_index,
7981
Register *prev_reg = g_ptr_array_index(vcpu->registers, i);
8082
g_assert(!strcmp(prev_reg->name, reg->name) &&
8183
prev_reg->handle == reg->handle);
82-
memcpy(prev_reg->content->data, rdata->data, prev_reg->content->len);
84+
memcpy_le(prev_reg->content->data, rdata->data, prev_reg->content->len, state.is_big_endian);
8385
frame_buffer_append_reg_info(fbuf, reg->name, rdata, s, OperandRead);
8486
// Flush byte array
8587
g_byte_array_set_size(rdata, 0);
@@ -338,6 +340,14 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
338340
qemu_plugin_outs("Pass it with 'out=<output_file>'.\n\n");
339341
exit(1);
340342
}
343+
char *endianess = get_argv_val(argv, argc, "endianess");
344+
if (!endianess || (strcmp(endianess, "b") && strcmp(endianess, "l"))) {
345+
qemu_plugin_outs("'endianess' argument is missing or is not 'b' or 'l'.\n");
346+
qemu_plugin_outs("This is required until QEMU plugins get a richer API.\n");
347+
qemu_plugin_outs("Pass it with 'endianess=[b/l]'.\n\n");
348+
exit(1);
349+
}
350+
state.is_big_endian = endianess[0] == 'b';
341351

342352
state.target_name = g_strdup(info->target_name);
343353
state.frame_buffer = g_ptr_array_new();

contrib/plugins/bap-tracing/tracing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ typedef struct {
171171
GPtrArray /*<const char *>*/ *vcpu_modes; ///< Indexed by vcpu id.
172172

173173
const char *target_name;
174+
bool is_big_endian;
174175
} TraceState;
175176

176177
VCPU *vcpu_new(void);

0 commit comments

Comments
 (0)