From 607ed58ffa2c5b8e8dbae6cfe490bbda7f845a77 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Tue, 16 Jul 2024 14:33:45 +0800 Subject: [PATCH] feat: provide cli options for library api prefix --- include/config.hpp | 3 +++ src/cli.cpp | 23 +++++++++++++++++++++++ src/difftest.cpp | 34 +++++++++++++++++----------------- src/gdbstub.cpp | 2 +- src/loader.cpp | 7 +------ src/main.cpp | 11 ++++++++--- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/include/config.hpp b/include/config.hpp index f08593f..bb90278 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -7,7 +7,10 @@ struct Config { std::filesystem::path memory_file; std::vector refs; + std::vector refs_prefix; std::filesystem::path dut; + std::string dut_prefix = ""; + int cli_parse(int argc, char **argv); }; diff --git a/src/cli.cpp b/src/cli.cpp index e17214b..a92ecbb 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -1,6 +1,8 @@ #include "config.hpp" #include +#include #include +#include int Config::cli_parse(int argc, char **argv) { CLI::App app; @@ -12,13 +14,34 @@ int Config::cli_parse(int argc, char **argv) { ->required() ->check(CLI::ExistingFile); + app.add_option("--ref-prefix", refs_prefix, + "Optional prefix for each reference library"); + app.add_option("--dut", dut, "Design under test") ->required() ->check(CLI::ExistingFile); + app.add_option("--dut-prefix", dut_prefix, + "Optional prefix for design under test"); + app.set_config("-c,--config") ->transform(CLI::FileOnDefaultPath("./difftest.toml")); + // Default value for refs_prefix + app.callback([&]() { + if (refs_prefix.size() == 0) { + refs_prefix.insert(refs_prefix.end(), refs.size(), ""); + } + }); + + // Check if refs_prefix matches refs. + app.callback([&]() { + if (refs_prefix.size() != refs.size()) { + throw CLI::ParseError( + "Same number of --ref and --ref-prefix must be provided.", EINVAL); + } + }); + CLI11_PARSE(app, argc, argv); return 0; diff --git a/src/difftest.cpp b/src/difftest.cpp index 49493e9..3d5d333 100644 --- a/src/difftest.cpp +++ b/src/difftest.cpp @@ -35,7 +35,6 @@ void Difftest::setup(const std::filesystem::path &memory_file) { // for(auto target : *this) { for (auto it = this->begin(); it != this->end(); ++it) { auto &target = *it; - printf("init addr: %p\n", target.ops.init); target.ops.init(target.args.data()); target.ops.write_mem(target.args.data(), 0x80000000UL, membuf.size(), membuf.data()); @@ -51,21 +50,23 @@ bool Difftest::check_all() { } bool Difftest::exec(size_t n, gdb_action_t *ret) { - bool breakflag = false; - Target *pbreak = &(*(this->begin())); - for (auto it = this->begin(); it != this->end(); ++it) { - auto &target = *it; - target.ops.stepi(target.args.data(), &target.last_res); - if (target.is_on_breakpoint()) { - breakflag = true; - pbreak = ⌖ + while (n--) { + bool breakflag = false; + Target *pbreak = &(*(this->begin())); + for (auto it = this->begin(); it != this->end(); ++it) { + auto &target = *it; + target.ops.stepi(target.args.data(), &target.last_res); + if (target.is_on_breakpoint()) { + breakflag = true; + pbreak = ⌖ + } } - } - if (breakflag) { - ret->reason = pbreak->last_res.reason; - ret->data = pbreak->last_res.data; - return false; + if (breakflag) { + ret->reason = pbreak->last_res.reason; + ret->data = pbreak->last_res.data; + return false; + } } return true; } @@ -86,7 +87,6 @@ gdb_action_t Difftest::cont() { } int Difftest::read_reg(int regno, size_t *value) { - std::cout << "read_reg(" << regno << ", " << value << ")" << std::endl; return current_target->ops.read_reg(current_target->args.data(), regno, value); } @@ -110,7 +110,7 @@ bool Difftest::set_bp(size_t addr, bp_type_t type) { bool ret = true; for (auto it = this->begin(); it != this->end(); ++it) { auto &target = *it; - ret = ret && target.ops.set_bp(target.args.data(), addr, type); + ret = target.ops.set_bp(target.args.data(), addr, type) && ret; } return ret; } @@ -119,7 +119,7 @@ bool Difftest::del_bp(size_t addr, bp_type_t type) { bool ret = true; for (auto it = this->begin(); it != this->end(); ++it) { auto &target = *it; - ret = ret && target.ops.del_bp(target.args.data(), addr, type); + ret = target.ops.del_bp(target.args.data(), addr, type) && ret; } return ret; } diff --git a/src/gdbstub.cpp b/src/gdbstub.cpp index a9c9b8d..583c4e3 100644 --- a/src/gdbstub.cpp +++ b/src/gdbstub.cpp @@ -57,8 +57,8 @@ int gdbstub_loop(Difftest *diff) { char socket_addr[] = "127.0.0.1:1234"; gdbstub_init(&gdbstub_priv, &gdbstub_ops, diff->get_arch(), socket_addr); + std::cout << "Waiting for gdb connection at " << socket_addr << std::endl; bool success = gdbstub_run(&gdbstub_priv, diff); - std::cout << "Waiting for gdb connection at " << socket_addr; gdbstub_close(&gdbstub_priv); return !success; } \ No newline at end of file diff --git a/src/loader.cpp b/src/loader.cpp index b4a3bfc..cab95ab 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -2,13 +2,11 @@ #include #include #include -#include #include Target::Target(const std::string &name, const std::string &func_prefix, const std::filesystem::path &path) { - std::cout << path.c_str() << std::endl; meta = {.name = name, .libpath = path, .dlhandle = dlopen(path.c_str(), RTLD_LAZY)}; @@ -60,10 +58,7 @@ load_error: throw std::runtime_error(err); } -Target::~Target() { - std::cout << "Destruct target " << meta.name << std::endl; - dlclose(meta.dlhandle); -} +Target::~Target() { dlclose(meta.dlhandle); } bool Target::is_on_breakpoint() const { return is_on_breakpoint(last_res); } diff --git a/src/main.cpp b/src/main.cpp index e432690..eed1644 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,9 +13,14 @@ int main(int argc, char **argv) { return ret; std::vector refs; - Target *dut = new Target{"dut", "nemu_", config.dut}; - for (const auto &ref_libpath : config.refs) { - refs.emplace_back(ref_libpath.string(), "nemu_", ref_libpath); + Target *dut = new Target{"dut", config.dut_prefix, config.dut}; + auto ref_libpath = config.refs.begin(); + auto ref_prefix = config.refs_prefix.begin(); + while (ref_libpath != config.refs.end() && + ref_prefix != config.refs_prefix.end()) { + refs.emplace_back(ref_libpath->string(), *ref_prefix, *ref_libpath); + ref_libpath++; + ref_prefix++; } Difftest difftest{std::move(*dut), std::move(refs)};