diff --git a/nyx/Makefile.objs b/nyx/Makefile.objs index 5400116161..93db8e07bb 100644 --- a/nyx/Makefile.objs +++ b/nyx/Makefile.objs @@ -7,6 +7,7 @@ synchronization.o \ page_cache.o \ kvm_nested.o \ debug.o \ +trace_dump.o \ auxiliary_buffer.o \ mmh3.o \ nested_hypercalls.o \ diff --git a/nyx/auxiliary_buffer.c b/nyx/auxiliary_buffer.c index 8680cf7685..c6dddacf73 100644 --- a/nyx/auxiliary_buffer.c +++ b/nyx/auxiliary_buffer.c @@ -25,6 +25,7 @@ along with QEMU-PT. If not, see . #include #include "nyx/state/state.h" #include "nyx/debug.h" +#include "nyx/trace_dump.h" /* experimental feature (currently broken) * enabled via trace mode @@ -104,6 +105,7 @@ void check_auxiliary_config_buffer(auxilary_buffer_t* auxilary_buffer, auxilary_ GET_GLOBAL_STATE()->pt_trace_mode_force = true; #endif redqueen_set_trace_mode(); + pt_trace_dump_enable(true); } } else { @@ -113,6 +115,7 @@ void check_auxiliary_config_buffer(auxilary_buffer_t* auxilary_buffer, auxilary_ GET_GLOBAL_STATE()->pt_trace_mode_force = false; #endif redqueen_unset_trace_mode(); + pt_trace_dump_enable(false); } } diff --git a/nyx/interface.c b/nyx/interface.c index c1e6a8d137..d1c751d097 100644 --- a/nyx/interface.c +++ b/nyx/interface.c @@ -51,6 +51,7 @@ along with QEMU-PT. If not, see . #include "nyx/state/state.h" #include "nyx/sharedir.h" #include "nyx/helpers.h" +#include "nyx/trace_dump.h" #include @@ -278,7 +279,7 @@ static bool verify_workdir_state(nyx_interface_state *s, Error **errp){ if(s->dump_pt_trace){ assert(asprintf(&tmp, "%s/pt_trace_dump_%d", workdir, id) != -1); - pt_trace_dump_enable(tmp); + pt_trace_dump_init(tmp); free(tmp); } diff --git a/nyx/pt.c b/nyx/pt.c index c6597e3f63..e1826ae852 100644 --- a/nyx/pt.c +++ b/nyx/pt.c @@ -44,6 +44,7 @@ along with QEMU-PT. If not, see . #include "nyx/state/state.h" #include #include "nyx/helpers.h" +#include "nyx/trace_dump.h" #define PT_BUFFER_MMAP_ADDR 0x3ffff0000000 @@ -53,55 +54,6 @@ uint32_t last = 0; uint32_t alt_bitmap_size = 0; uint8_t* alt_bitmap = NULL; -int pt_trace_dump_fd = 0; -char *pt_trace_dump_filename; -bool should_dump_pt_trace= false; /* dump PT trace as returned from HW */ - -void pt_trace_dump_enable(char* filename) -{ - int test_fd; - - printf("Enable pt trace dump at %s", filename); - pt_trace_dump_filename = filename; - should_dump_pt_trace = true; - - test_fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); - if (test_fd < 0) - fprintf(stderr, "Error accessing pt_dump output path: %s", strerror(errno)); - assert(test_fd >= 0); -} - -static void pt_truncate_pt_dump_file(void) { - int fd; - - if (!should_dump_pt_trace) - return; - - fd = open(pt_trace_dump_filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); - if (fd < 0) { - fprintf(stderr, "Error truncating pt_trace_dump: %s\n", strerror(errno)); - assert(0); - } - close(fd); -} - -static void pt_write_pt_dump_file(uint8_t *data, size_t bytes) -{ - int fd; - - if (!should_dump_pt_trace) - return; - - fd = open(pt_trace_dump_filename, O_APPEND|O_WRONLY, 0644); - //fd = open(pt_trace_dump_filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); - if (fd < 0) { - fprintf(stderr, "Error writing pt_trace_dump: %s\n", strerror(errno)); - assert(0); - } - assert(bytes == write(fd, data, bytes)); - close(fd); -} - static void pt_set(CPUState *cpu, run_on_cpu_data arg){ asm volatile("" ::: "memory"); } diff --git a/nyx/pt.h b/nyx/pt.h index 174b463229..ca49602272 100644 --- a/nyx/pt.h +++ b/nyx/pt.h @@ -44,6 +44,5 @@ void pt_post_kvm_run(CPUState *cpu); void pt_handle_overflow(CPUState *cpu); void pt_dump(CPUState *cpu, int bytes); -void pt_trace_dump_enable(char* filename); #endif diff --git a/nyx/trace_dump.c b/nyx/trace_dump.c new file mode 100644 index 0000000000..f325377441 --- /dev/null +++ b/nyx/trace_dump.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +#include "state/state.h" +#include "trace_dump.h" + +/* dump PT trace as returned from HW */ + +char *pt_trace_dump_filename; +bool pt_dump_initialized = false; +bool pt_dump_enabled = false; + +void pt_trace_dump_enable(bool enable){ + if (pt_dump_initialized) + pt_dump_enabled = enable; +} + +void pt_trace_dump_init(char* filename) +{ + int test_fd; + + //fprintf(stderr, "Enable pt trace dump at %s", filename); + pt_dump_initialized = true; + + test_fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); + if (test_fd < 0) + fprintf(stderr, "Error accessing pt_dump output path %s: %s", pt_trace_dump_filename, strerror(errno)); + assert(test_fd >= 0); + + pt_trace_dump_filename = strdup(filename); + assert(pt_trace_dump_filename); +} + +void pt_truncate_pt_dump_file(void) { + int fd; + + if (!pt_dump_enabled) + return; + + fd = open(pt_trace_dump_filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); + if (fd < 0) { + fprintf(stderr, "Error truncating %s: %s\n", pt_trace_dump_filename, strerror(errno)); + assert(0); + } + close(fd); +} + +void pt_write_pt_dump_file(uint8_t *data, size_t bytes) +{ + int fd; + + if (!pt_dump_enabled) + return; + + fd = open(pt_trace_dump_filename, O_APPEND|O_WRONLY, 0644); + //fd = open(pt_trace_dump_filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); + if (fd < 0) { + fprintf(stderr, "Error writing pt_trace_dump to %s: %s\n", pt_trace_dump_filename, strerror(errno)); + assert(0); + } + assert(bytes == write(fd, data, bytes)); + close(fd); +} + diff --git a/nyx/trace_dump.h b/nyx/trace_dump.h new file mode 100644 index 0000000000..fb3235f34b --- /dev/null +++ b/nyx/trace_dump.h @@ -0,0 +1,6 @@ +#pragma once + +void pt_trace_dump_init(char* filename); +void pt_trace_dump_enable(bool enable); +void pt_write_pt_dump_file(uint8_t *data, size_t bytes); +void pt_truncate_pt_dump_file(void);