diff --git a/README.md b/README.md index da911a8..62f6033 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ The program would load, and stop at first instruction. It will then prompt you t Then run `riscv32-none-elf-gdb`, and in the gdb prompt, type: ``` -file ../example/example target remote :1234 ``` diff --git a/src/debug.cpp b/src/debug.cpp index a531b12..47cb85b 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -196,7 +196,24 @@ void GDBStub::handle_packet(const std::string &packet) { case 'q': if (packet.find("qSupported") == 0) { - send_packet("PacketSize=4096"); // Example capability + send_packet("PacketSize=4096;qXfer:exec-file:read+"); + } else if (packet.find("qXfer:exec-file:read:") == 0) { + size_t start = packet.find("::") + 2; + size_t end = packet.find(",", start); + size_t offset = + std::stoul(packet.substr(start, end - start), nullptr, 16); + size_t length = std::stoul(packet.substr(end + 1), nullptr, 16); + + std::string exec_file_path = vm.get_file_path(); + + std::string response; + if (offset < exec_file_path.size()) { + size_t read_length = std::min(length, exec_file_path.size() - offset); + response = "m" + exec_file_path.substr(offset, read_length); + } else { + response = "l"; // No more data + } + send_packet(response); } else { send_packet(""); // Empty response } diff --git a/src/rve.cpp b/src/rve.cpp index 7ba6fe7..677218c 100644 --- a/src/rve.cpp +++ b/src/rve.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -18,7 +19,7 @@ int main(int argc, char *argv[]) { if (std::string(argv[i]) == "--debug") debug = true; else - program_filename = argv[i]; + program_filename = std::filesystem::absolute(argv[i]); } if (program_filename == "") { std::cerr << "Expected filename of program to execute" << std::endl; @@ -35,7 +36,7 @@ int main(int argc, char *argv[]) { memory.resize(MEMORY_SIZE, 0); - VM vm(memory); + VM vm(memory, program_filename); if (!debug) { try { diff --git a/src/vm.cpp b/src/vm.cpp index 9eab0ad..8e34c17 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -12,7 +12,8 @@ inline int32_t sign_extend(int32_t value, int bits) { return (value ^ mask) - mask; } -VM::VM(std::vector memory) : memory_(memory) {} +VM::VM(const std::vector& memory, const std::string& file_path) + : memory_(memory), file_path(file_path) {} std::vector VM::read_memory(size_t start, size_t size) { if (start + size > memory_.size()) { @@ -38,6 +39,8 @@ uint32_t VM::read_register(size_t regnum) { return registers[regnum]; } +const std::string& VM::get_file_path() { return file_path; } + void VM::step() { size_t memory_size = memory_.size(); uint8_t* memory = &memory_[0]; diff --git a/src/vm.hpp b/src/vm.hpp index 37f2885..b81652d 100644 --- a/src/vm.hpp +++ b/src/vm.hpp @@ -11,7 +11,7 @@ const int NUM_REGISTERS = 32; // Standard RISC-V has 32 registers class VM { public: - VM(std::vector memory); + VM(const std::vector &memory, const std::string &file_path); void step(); void eval(); @@ -22,9 +22,12 @@ class VM { uint32_t read_register(size_t regnum); + const std::string &get_file_path(); + private: std::vector memory_; uint32_t registers[NUM_REGISTERS] = {0}; uint32_t pc = 0; + std::string file_path; };