From 407216b17cb8b81fc5fe13af977b995834a8c013 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Thu, 8 Aug 2024 17:06:59 +0800 Subject: [PATCH] feat: find image file relative to config option image_path --- difftest.toml | 3 ++- include/config.hpp | 1 + src/cli.cpp | 13 ++++++++++--- src/difftest.cpp | 1 + src/loader.cpp | 4 +++- src/main.cpp | 9 ++++++++- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/difftest.toml b/difftest.toml index f45fd50..9d9bad8 100644 --- a/difftest.toml +++ b/difftest.toml @@ -9,5 +9,6 @@ dut = "/home/xin/repo/spike-diff/build/lib/libspike-diff.so" dut-prefix = "spike_" listen = "/tmp/gdbstub-diffu.sock" # listen = "127.0.0.1:1234" -memory = "/nix/store/37986mdgsqm5m8w74k0f5llzqhxgsbnv-am-kernel-riscv32-none-elf-2024-07-10/share/am-kernels/string.bin" +# memory = "/nix/store/37986mdgsqm5m8w74k0f5llzqhxgsbnv-am-kernel-riscv32-none-elf-2024-07-10/share/am-kernels/string.bin" +memory = "./add.bin" # g = true diff --git a/include/config.hpp b/include/config.hpp index 7574852..bf27c5e 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -5,6 +5,7 @@ #include struct Config { + std::filesystem::path images_path = "./"; std::filesystem::path memory_file; std::vector refs; std::vector refs_prefix; diff --git a/src/cli.cpp b/src/cli.cpp index add616d..e6913fa 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -6,9 +6,16 @@ int Config::cli_parse(int argc, char **argv) { CLI::App app; - app.add_option("-m,--memory", memory_file, "Content of memory") - ->required() - ->check(CLI::ExistingFile); + app.add_option( + "--images-path", images_path, + "Directory containing image files. Search image files in this path.") + ->envname("DIFFU_IMAGES_PATH") + ->check(CLI::ExistingPath); + + app.add_option("-m,--memory", memory_file, + "Image file used to fill up the memory. Relative path to " + "--images-path") + ->required(); app.add_option("--ref", refs, "Reference dynamic library") ->required() diff --git a/src/difftest.cpp b/src/difftest.cpp index acba918..e5648d1 100644 --- a/src/difftest.cpp +++ b/src/difftest.cpp @@ -20,6 +20,7 @@ Difftest::Difftest(Target &&dut, std::vector &&refs) { void Difftest::setup(const std::filesystem::path &memory_file) { std::ifstream is = std::ifstream(memory_file, std::ios::binary); + spdlog::debug("Reading image file: {}", memory_file.c_str()); // Seek to the end to determine the file size is.seekg(0, std::ios::end); std::streampos memsize = is.tellg(); diff --git a/src/loader.cpp b/src/loader.cpp index b7f8413..2a0ab38 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -11,7 +11,9 @@ Target::Target(const std::string &name, const std::string &func_prefix, .libpath = path, .dlhandle = dlopen(path.c_str(), RTLD_NOW)}; - spdlog::info("Library handle: {}", meta.dlhandle); + spdlog::info("Found dlopen API handle for {} at {}", meta.name, + meta.dlhandle); + if (!meta.dlhandle) { throw std::runtime_error(dlerror()); } diff --git a/src/main.cpp b/src/main.cpp index 29d209f..c2b3bf3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,9 @@ #include "api.hpp" #include "config.hpp" #include "difftest.hpp" +#include #include +#include int gdbstub_loop(Difftest *, std::string); @@ -26,7 +28,12 @@ int main(int argc, char **argv) { Difftest difftest{std::move(*dut), std::move(refs)}; - difftest.setup(config.memory_file); + std::filesystem::path image_file = config.images_path / config.memory_file; + if (!std::filesystem::exists(image_file)) { + spdlog::error("Cannot find {} in {}.", config.memory_file.c_str(), + config.images_path.c_str()); + } + difftest.setup(image_file); if (config.use_debugger) { gdbstub_loop(&difftest, config.gdbstub_addr);