-
Notifications
You must be signed in to change notification settings - Fork 1
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
mmamayka
wants to merge
2
commits into
main
Choose a base branch
from
hooks-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(debug_tracer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include <cassert> | ||
#include <iostream> | ||
|
||
#include "besm-666/sim/hooks.hpp" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
. Addif
for macos, please)