feat(npc): port to export gdbstub api

This commit is contained in:
xinyangli 2024-07-18 19:16:15 +08:00
parent 8ffab061ed
commit d46cd7b4ae
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
2 changed files with 76 additions and 37 deletions

View file

@ -27,3 +27,26 @@ foreach(DIFFTEST_BINARY_FILE IN LISTS DIFFTEST_BINARY_FILES)
${DIFFTEST_LIB})
unset(FILENAME)
endforeach()
add_library(${TOPMODULE} SHARED config.cpp main.cpp)
target_link_libraries(${TOPMODULE} PRIVATE devices gdbstub)
target_include_directories(${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include)
set_property(TARGET PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_options(${TOPMODULE} PRIVATE -Wl,-E)
verilate(
${TOPMODULE}
TRACE
THREADS
TOP_MODULE
${TOPMODULE}
PREFIX
V${TOPMODULE}
SOURCES
${CHISEL_OUTPUT_TOPMODULE}
${CHISEL_OUTPUT_VERILATOR_CONF}
INCLUDE_DIRS
${CHISEL_OUTPUT_DIR}
VERILATOR_ARGS
"--vpi" # Enable VPI
"-Wno-UNOPTFLAT")

View file

@ -1,7 +1,7 @@
extern "C" {
#include <gdbstub.h>
}
#include "VFlow___024root.h"
// #include "VFlow___024root.h"
#include "components.hpp"
#include <VFlow.h>
#include <config.hpp>
@ -28,7 +28,7 @@ const size_t PMEM_START = 0x80000000;
const size_t PMEM_END = 0x87ffffff;
struct DbgState {
std::vector<Breakpoint> bp;
std::vector<Breakpoint> *bp;
};
extern "C" {
@ -42,7 +42,10 @@ void *pmem_get() {
};
static auto pmem = new MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>(
std::make_unique<Memory<128 * 1024>>(
config.memory_file, config.memory_file_binary, PMEM_START, PMEM_END),
"/nix/store/"
"nv2c00y24qwz1jihfbaip9n1lskbzyb3-am-kernel-riscv32-none-elf-2024-07-"
"10/share/am-kernels/add.bin",
config.memory_file_binary, PMEM_START, PMEM_END),
std::make_unique<Devices::DeviceMap>(devices), config.mtrace_ranges);
return pmem;
}
@ -95,70 +98,83 @@ int npc_write_reg(void *args, int regno, size_t value) { return 1; }
void npc_cont(void *args, gdb_action_t *res) {
DbgState *dbg = (DbgState *)args;
*res = top->eval(dbg->bp);
*res = top->eval(*dbg->bp);
}
void npc_stepi(void *args, gdb_action_t *res) {
DbgState *dbg = (DbgState *)args;
*res = top->eval(dbg->bp);
*res = top->eval(*dbg->bp);
}
bool npc_set_bp(void *args, size_t addr, bp_type_t type) {
DbgState *dbg = (DbgState *)args;
for (const auto &bp : dbg->bp) {
for (const auto &bp : *dbg->bp) {
if (bp.addr == addr && bp.type == type) {
return true;
}
}
dbg->bp.push_back({.addr = addr, .type = type});
dbg->bp->push_back({.addr = addr, .type = type});
return true;
}
bool npc_del_bp(void *args, size_t addr, bp_type_t type) {
DbgState *dbg = (DbgState *)args;
for (auto it = dbg->bp.begin(); it != dbg->bp.end(); it++) {
for (auto it = dbg->bp->begin(); it != dbg->bp->end(); it++) {
if (it->addr == addr && it->type == type) {
std::swap(*it, *dbg->bp.rbegin());
dbg->bp.pop_back();
std::swap(*it, *dbg->bp->rbegin());
dbg->bp->pop_back();
return true;
}
}
return false;
}
static target_ops npc_gdbstub_ops = {.cont = npc_cont,
.stepi = npc_stepi,
.read_reg = npc_read_reg,
.write_reg = npc_write_reg,
.read_mem = npc_read_mem,
.write_mem = npc_write_mem,
.set_bp = npc_set_bp,
.del_bp = npc_del_bp,
.on_interrupt = NULL};
void npc_on_interrupt(void *args) { ; }
static gdbstub_t gdbstub_priv;
static DbgState dbg;
arch_info_t isa_arch_info = {
.target_desc = strdup(TARGET_RV32), .reg_num = 33, .reg_byte = 4};
int gdbstub_loop() {
if (!gdbstub_init(&gdbstub_priv, &npc_gdbstub_ops, (arch_info_t)isa_arch_info,
strdup("127.0.0.1:1234"))) {
return EINVAL;
}
bool success = gdbstub_run(&gdbstub_priv, &dbg);
gdbstub_close(&gdbstub_priv);
return !success;
}
} // extern "C"
int main(int argc, char **argv, char **env) {
config.cli_parse(argc, argv);
void npc_init(void *args) {
DbgState *dbg = (DbgState *)args;
dbg->bp = new std::vector<Breakpoint>;
top = new VlModule;
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
top->setup(config.wavefile, regs);
top->reset_eval(10);
}
static gdbstub_t gdbstub_priv;
arch_info_t isa_arch_info = {
.target_desc = strdup(TARGET_RV32), .reg_num = 32, .reg_byte = 4};
size_t argsize = sizeof(DbgState);
} // extern "C"
int gdbstub_loop() {
DbgState dbg;
target_ops npc_gdbstub_ops = {.cont = npc_cont,
.stepi = npc_stepi,
.read_reg = npc_read_reg,
.write_reg = npc_write_reg,
.read_mem = npc_read_mem,
.write_mem = npc_write_mem,
.set_bp = npc_set_bp,
.del_bp = npc_del_bp,
.on_interrupt = NULL};
if (!gdbstub_init(&gdbstub_priv, &npc_gdbstub_ops, (arch_info_t)isa_arch_info,
strdup("/tmp/gdbstub-npc.sock"))) {
return EINVAL;
}
npc_init(&dbg);
bool success = gdbstub_run(&gdbstub_priv, &dbg);
// gdbstub_close(&gdbstub_priv);
return !success;
}
int main(int argc, char **argv, char **env) {
config.cli_parse(argc, argv);
return gdbstub_loop();
}