Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugins support & debug-tracer plugin #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ if(DEFINED BESM666__SIMULATOR_BUILD)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Python3 REQUIRED)
find_package(Git REQUIRED)

Expand Down Expand Up @@ -86,6 +88,7 @@ if(DEFINED BESM666__SIMULATOR_BUILD)
add_subdirectory(src)
add_subdirectory(standalone)
add_subdirectory(third_party)
add_subdirectory(plugins)

enable_testing()
if (DEFINED BESM666_TEST_WITH_VALGRIND)
Expand Down
5 changes: 5 additions & 0 deletions include/besm-666/sim/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
#include <filesystem>
#include <functional>
#include <stdexcept>
#include <string>
#include <vector>

namespace besm::sim {

struct ConfigData {
// Input files
std::filesystem::path executablePath;
std::vector<std::string> plugins;
};

class InvalidConfiguration : public std::runtime_error {
Expand All @@ -22,6 +25,7 @@ class InvalidConfiguration : public std::runtime_error {
class Config {
public:
std::filesystem::path executablePath() const;
std::vector<std::string> const &plugins() const;

private:
friend class ConfigBuilder;
Expand All @@ -35,6 +39,7 @@ class ConfigBuilder {
ConfigBuilder() = default;

void setExecutablePath(std::filesystem::path executablePath);
void addPlugin(std::filesystem::path path);

Config build();

Expand Down
8 changes: 7 additions & 1 deletion include/besm-666/sim/hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ namespace besm::sim {

class HookManager {
public:
enum Event { INSTRUCTION_FETCH, INSTRUCTION_DECODE, INSTRUCTION_EXECUTE };
enum Event {
SIMULATION_STARTED,
SIMULATION_FINISHED,
INSTRUCTION_FETCH,
INSTRUCTION_DECODE,
INSTRUCTION_EXECUTE
};

using SPtr = std::shared_ptr<HookManager>;
using Callback = void (*)(sim::Hart const &hart, void const *extraArg);
Expand Down
6 changes: 6 additions & 0 deletions include/besm-666/sim/machine.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#pragma once

#include <vector>

#include "besm-666/sim/config.hpp"
#include "besm-666/sim/hart.hpp"
#include "besm-666/sim/hooks.hpp"
#include "besm-666/sim/plugin.hpp"
#include "besm-666/util/non-copyable.hpp"

namespace besm::sim {
Expand All @@ -19,7 +22,10 @@ class Machine : INonCopyable {
sim::HookManager::SPtr getHookManager() { return hookManager_; }

private:
void loadPlugins(sim::Config const &config);

HookManager::SPtr hookManager_;
std::vector<Plugin> plugins_;
mem::PhysMem::SPtr pMem_;
sim::Hart::SPtr hart_;
};
Expand Down
31 changes: 31 additions & 0 deletions include/besm-666/sim/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <ostream>
#include <string>

#include "besm-666/sim/hooks.hpp"
#include "besm-666/util/dynamic-library.hpp"
#include "besm-666/util/non-copyable.hpp"

namespace besm::sim {

class Plugin : public INonCopyable {
public:
Plugin(std::string const &path);
Plugin(Plugin &&other);

Plugin const &operator=(Plugin &&other);

~Plugin() = default;

void init(sim::HookManager::SPtr hookManager,
std::string const &commandLine, std::ostream &defaulLogStream);

private:
using InitFunction = void (*)(sim::HookManager::SPtr, std::string const &,
std::ostream &);

util::DynamicLibrary pluginLibrary_;
};

} // namespace besm::sim
45 changes: 45 additions & 0 deletions include/besm-666/util/dynamic-library.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <filesystem>
#include <stdexcept>
#include <string>

#include "besm-666/util/non-copyable.hpp"

namespace besm::util {

class DynamicLibraryError : public std::runtime_error {
public:
DynamicLibraryError(std::string const &message) : runtime_error(message) {}
DynamicLibraryError(char const *message) : runtime_error(message) {}
};

class DynamicLibrarySymbolNotFound : public std::runtime_error {
public:
DynamicLibrarySymbolNotFound(std::string const &message)
: runtime_error(message) {}
DynamicLibrarySymbolNotFound(char const *message)
: runtime_error(message) {}
};

class DynamicLibrary : public INonCopyable {
public:
explicit DynamicLibrary(std::filesystem::path const &path);
DynamicLibrary(DynamicLibrary &&other);
~DynamicLibrary();

DynamicLibrary const &operator=(DynamicLibrary &&other);

void *getSymbol(std::string const &symbol);

template <typename FunctionType>
FunctionType getFunction(std::string const &symbol);

private:
void *handle_;
};

template <typename FunctionType>
inline FunctionType DynamicLibrary::getFunction(std::string const &symbol) {
return reinterpret_cast<FunctionType>(this->getSymbol(symbol));
}

} // namespace besm::util
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(debug_tracer)
15 changes: 15 additions & 0 deletions plugins/debug_tracer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project(BESM-666-DebugTracer
LANGUAGES CXX
VERSION 1.0.0
DESCRIPTION "Plugin used to debug the simulator"
)

add_library(besm666_debug_tracer SHARED)
target_sources(besm666_debug_tracer PRIVATE
./debug-tracer.cpp
)
target_link_libraries(besm666_debug_tracer PRIVATE
besm666_include
capstone::capstone
CLI11::CLI11
)
96 changes: 96 additions & 0 deletions plugins/debug_tracer/debug-tracer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <iostream>
#include "CLI/CLI.hpp"
#include "besm-666/exec/gprf.hpp"
#include "besm-666/instruction.hpp"
#include "besm-666/riscv-types.hpp"
#include "capstone/capstone.h"

#include "besm-666/sim/hooks.hpp"
#include "besm-666/sim/hart.hpp"

namespace {

std::ofstream logFile;
std::ostream* pLogStream = nullptr;


csh CapstoneHandler;

void InstructionFetchHandler(besm::sim::Hart const& hart, void const* pBytecode) {
besm::RV64UWord bytecode = *reinterpret_cast<besm::RV64UWord const*>(pBytecode);
besm::RV64UDWord pc = hart.getState().read(besm::exec::GPRF::PC);

*pLogStream << "[DebugTracer] Fetched bytecode " << std::hex << bytecode <<
std::dec << " at pc = " << pc << ", disassembly:\n\t";

cs_insn* instruction;
size_t count = cs_disasm(CapstoneHandler, reinterpret_cast<uint8_t const*>(pBytecode),
4, pc, 0, &instruction);

if(count > 0) {
*pLogStream << instruction->mnemonic << " " << instruction->op_str << std::endl;
} else {
*pLogStream << "[unable to disasm]" << std::endl;
}

cs_free(instruction, count);
}

void InstructionDecodeHandler(besm::sim::Hart const& hart, void const* pInstr) {
// Too early to implement it
}

void InstructionExecHandler(besm::sim::Hart const& hart, void const*) {
*pLogStream << "[DebugTracer] Instruction executed. Force dumping machine state..."
<< std::endl;
besm::exec::GPRFStateDumper(*pLogStream).dump(hart.getState());
}

}

extern "C" {

void init(besm::sim::HookManager::SPtr hookManager, std::string const& commandLine,
std::ostream& defaultLogStream) {
pLogStream = &defaultLogStream;

*pLogStream << "[DebugTracer] DebugTracer plugin enabled, good luck in debugging!"
<< std::endl;

std::string logFilePath;

CLI::App app;
app.add_option("-l,--log-file", logFilePath,
"Dump logs to file (dumped to clog by default)");

app.parse(commandLine, true);

if(!logFilePath.empty()) {
logFile.open(logFilePath);
if(!logFile.is_open()) {
*pLogStream << "[DebugTracer] Failed to open log file \'" <<
logFilePath << "\', using clog stream instead" << std::endl;
} else {
*pLogStream << "[DebugTracer] Log file was set up" << std::endl;
std::ref(*pLogStream) = logFile;
}
}

cs_err csErr = cs_open(CS_ARCH_RISCV, CS_MODE_RISCV64, &CapstoneHandler);
if(csErr != CS_ERR_OK) {
*pLogStream << "[DebugTracer] Unable to initialize capstone engine, "
"disassembly will be unavailable" << std::endl;
*pLogStream << "[DebugTracer] The error occured in Capstone Engine is \'" <<
cs_strerror(csErr) << "\'" << std::endl;
}

hookManager->registerHook(besm::sim::HookManager::INSTRUCTION_FETCH,
InstructionFetchHandler);
hookManager->registerHook(besm::sim::HookManager::INSTRUCTION_DECODE,
InstructionDecodeHandler);
hookManager->registerHook(besm::sim::HookManager::INSTRUCTION_EXECUTE,
InstructionExecHandler);
}

}

3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ PUBLIC
besm666_decoder
besm666_sim
)
target_link_options(besm666_shared PRIVATE
"-Wl,-export-dynamic"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ld for macos is using -export_dynamic. Add if for macos, please)

)

add_subdirectory(memory)
add_subdirectory(util)
Expand Down
1 change: 1 addition & 0 deletions src/sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ target_sources(besm666_sim PRIVATE
./hart.cpp
./machine.cpp
./hooks.cpp
./plugin.cpp
)
target_link_libraries(besm666_sim PRIVATE
besm666_include
Expand Down
8 changes: 8 additions & 0 deletions src/sim/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ std::filesystem::path Config::executablePath() const {
return data_.executablePath;
}

std::vector<std::string> const &Config::plugins() const {
return data_.plugins;
}

void ConfigBuilder::setExecutablePath(std::filesystem::path path) {
data_.executablePath = path;
}

void ConfigBuilder::addPlugin(std::filesystem::path path) {
data_.plugins.push_back(path);
}

Config ConfigBuilder::build() {
this->validateState();
return Config(std::move(data_));
Expand Down
3 changes: 3 additions & 0 deletions src/sim/hart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ bool Hart::finished() const {
}

void Hart::run() {
hookManager_->triggerHooks(HookManager::SIMULATION_STARTED, *this, nullptr);
while (!this->finished()) {
this->runCycle();
}
hookManager_->triggerHooks(HookManager::SIMULATION_FINISHED, *this,
nullptr);
}

} // namespace besm::sim
1 change: 1 addition & 0 deletions src/sim/hooks.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cassert>
#include <iostream>

#include "besm-666/sim/hooks.hpp"

Expand Down
13 changes: 13 additions & 0 deletions src/sim/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@ Machine::Machine(sim::Config const &config) {

hookManager_ = sim::HookManager::Create();
hart_ = sim::Hart::Create(pMem_, hookManager_);

this->loadPlugins(config);
}

void Machine::run() { hart_->run(); }

exec::GPRF const &Machine::getState() const { return hart_->getState(); }

void Machine::loadPlugins(sim::Config const &config) {
for (auto const &pluginCmd : config.plugins()) {
std::filesystem::path pluginPath =
pluginCmd.substr(0, pluginCmd.find(" "));

Plugin plugin(pluginPath);
plugin.init(hookManager_, pluginCmd, std::clog);
plugins_.push_back(std::move(plugin));
}
}

} // namespace besm::sim
21 changes: 21 additions & 0 deletions src/sim/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "besm-666/sim/plugin.hpp"

namespace besm::sim {

Plugin::Plugin(std::string const &path) : pluginLibrary_(path) {}
Plugin::Plugin(Plugin &&other)
: pluginLibrary_(std::move(other.pluginLibrary_)) {}

Plugin const &Plugin::operator=(Plugin &&other) {
std::swap(pluginLibrary_, other.pluginLibrary_);
return *this;
}

void Plugin::init(sim::HookManager::SPtr hookManager,
std::string const &commandLine,
std::ostream &defaultLogStream) {
pluginLibrary_.getFunction<InitFunction>("init")(hookManager, commandLine,
defaultLogStream);
}

} // namespace besm::sim
1 change: 1 addition & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
add_library(besm666_util STATIC)
target_sources(besm666_util PRIVATE
./elf-parser.cpp
./dynamic-library.cpp
)
target_link_libraries(besm666_util
PUBLIC
Expand Down
Loading