Initialize debugger with the VM instance

This commit is contained in:
Konstantin Nazarov 2024-12-09 00:49:57 +00:00
parent 79177d17e5
commit c7ff2c784c
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 71 additions and 68 deletions

View file

@ -1,6 +1,7 @@
#include "debug.hpp"
GDBStub::GDBStub(int port) : server_fd(-1), client_fd(-1), running(false) {
GDBStub::GDBStub(VM &vm, int port)
: server_fd(-1), client_fd(-1), running(false), vm(vm) {
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
throw std::runtime_error("Failed to create socket.");
@ -11,7 +12,8 @@ GDBStub::GDBStub(int port) : server_fd(-1), client_fd(-1), running(false) {
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==
-1) {
throw std::runtime_error("Failed to bind socket.");
}
@ -19,7 +21,8 @@ GDBStub::GDBStub(int port) : server_fd(-1), client_fd(-1), running(false) {
throw std::runtime_error("Failed to listen on socket.");
}
std::cout << "Waiting for GDB connection on port " << port << "..." << std::endl;
std::cout << "Waiting for GDB connection on port " << port << "..."
<< std::endl;
client_fd = accept(server_fd, nullptr, nullptr);
if (client_fd == -1) {
throw std::runtime_error("Failed to accept connection.");
@ -49,9 +52,7 @@ void GDBStub::run() {
}
}
void GDBStub::stop() {
running = false;
}
void GDBStub::stop() { running = false; }
void GDBStub::send_ack() {
if (send(client_fd, "+", 1, 0) == -1) {
@ -78,7 +79,8 @@ void GDBStub::send_packet(const std::string &packet) {
}
std::ostringstream response;
response << '$' << packet << '#' << std::hex << std::setfill('0') << std::setw(2) << (checksum & 0xFF);
response << '$' << packet << '#' << std::hex << std::setfill('0')
<< std::setw(2) << (checksum & 0xFF);
const std::string response_str = response.str();
if (send(client_fd, response_str.c_str(), response_str.size(), 0) == -1) {
@ -182,7 +184,8 @@ void GDBStub::handle_packet(const std::string &packet) {
}
}
bool GDBStub::parse_memory_request(const std::string &packet, size_t &addr, size_t &length) {
bool GDBStub::parse_memory_request(const std::string &packet, size_t &addr,
size_t &length) {
size_t comma_pos = packet.find(',');
if (comma_pos == std::string::npos) {
return false;

View file

@ -17,7 +17,7 @@
class GDBStub {
public:
GDBStub(int port);
GDBStub(VM &vm, int port);
~GDBStub();
void run();
@ -47,4 +47,6 @@ class GDBStub {
std::string read_registers();
std::string read_memory(size_t addr, size_t length);
VM &vm;
};

View file

@ -1,16 +1,14 @@
#include "vm.hpp"
#include "debug.hpp"
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include "debug.hpp"
#include "vm.hpp"
int main(int argc, char* argv[]) {
const size_t MEMORY_SIZE = 128*1024;
int main(int argc, char *argv[]) {
const size_t MEMORY_SIZE = 128 * 1024;
bool debug = false;
std::string program_filename = "";
@ -39,7 +37,7 @@ int main(int argc, char* argv[]) {
if (!debug) {
try {
vm.eval();
} catch (const std::exception& e) {
} catch (const std::exception &e) {
std::cerr << "Emulator error: " << e.what() << std::endl;
return 1;
}
@ -50,7 +48,7 @@ int main(int argc, char* argv[]) {
} else {
// to debug, do: "set debug remote 1" in gdb
// and then "target remote :1234"
GDBStub stub(1234);
GDBStub stub(vm, 1234);
stub.run();
}