-
Notifications
You must be signed in to change notification settings - Fork 0
Implement plugin subsystem #16
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
base: master
Are you sure you want to change the base?
Changes from all commits
c5532f9
3bb586c
69a4a9b
2d805f0
6a86e30
feac4cf
cca53f8
bcdbb30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,5 +33,7 @@ | |
|
|
||
| # Directories | ||
| build/ | ||
|
|
||
| # IDEs and LSP specific | ||
| .vscode/ | ||
| __pycache__/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #ifndef HSIM_MACHINE_EVENT_INCLUDED | ||
| #define HSIM_MACHINE_EVENT_INCLUDED | ||
|
|
||
| #include <list> | ||
| #include <memory> | ||
| #include <utility> | ||
|
|
||
| #include "support.hh" | ||
|
|
||
| namespace hsim { | ||
|
|
||
| struct MemRead {}; | ||
| struct MemWrite {}; | ||
| struct PreInsn {}; | ||
| struct PostInsn {}; | ||
|
|
||
| class IEventConsumer { | ||
| public: | ||
| virtual ~IEventConsumer() = default; | ||
| virtual void handle(MemRead event, Addr addr) = 0; | ||
| virtual void handle(MemWrite event, Addr addr, Word value) = 0; | ||
| virtual void handle(PreInsn event, Word insn) = 0; | ||
| virtual void handle(PostInsn event, Word insn) = 0; | ||
| }; | ||
|
|
||
| using IEventConsumerHandle = std::unique_ptr<IEventConsumer>; | ||
| class EventManager final { | ||
| public: | ||
| template <typename Event, typename... Args> void notify(Args &&...args) { | ||
| for (auto &consumer : m_consumers) { | ||
| consumer->handle(Event{}, std::forward<Args>(args)...); | ||
| } | ||
| } | ||
| void attach(IEventConsumerHandle consumer) { | ||
| m_consumers.push_back(std::move(consumer)); | ||
| } | ||
| void detach(IEventConsumerHandle consumer) { m_consumers.remove(consumer); } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused a bit with thus function. How could you remove plugin by address, if you've lost it after EventManager ev;
auto consumer = std::make_unique<IntConsumer>(42);
ev.attach(std::move(consumer));
// consumer is in moved-from state hereafter
ev.detach(???); // what to pass here?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
is detach necessary at all?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this method |
||
|
|
||
| private: | ||
| std::list<IEventConsumerHandle> m_consumers; | ||
| }; | ||
|
|
||
| } // namespace hsim | ||
|
|
||
| #endif // HSIM_MACHINE_EVENT_INCLUDED | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #ifndef HSIM_PLUGIN_INCLUDED | ||
| #define HSIM_PLUGIN_INCLUDED | ||
|
|
||
| #include <filesystem> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include <dlfcn.h> | ||
|
|
||
| #include "machine_event.hh" | ||
| #include "so_loader.hh" | ||
| #include "support.hh" | ||
|
|
||
| namespace hsim { | ||
|
|
||
| class IPlugin { | ||
| public: | ||
| virtual ~IPlugin() = default; | ||
| virtual void handle(MemRead event, Addr addr) = 0; | ||
| virtual void handle(MemWrite event, Addr addr, Word value) = 0; | ||
| virtual void handle(PreInsn event, Word insn) = 0; | ||
| virtual void handle(PostInsn event, Word insn) = 0; | ||
| }; | ||
|
|
||
| using LoadPluginFunc = IPlugin *(*)(const std::string &options); | ||
| using UnloadPluginFunc = void (*)(IPlugin *plugin); | ||
|
|
||
| constexpr std::string kLoadPluginFuncName = "loadPlugin"; | ||
| constexpr std::string kUnloadPluginFuncName = "unloadPlugin"; | ||
|
|
||
| // NOTE plugins should use this defines when declaring a function | ||
| #define HSIM_LOAD_PLUGIN_FUNC extern "C" hsim::IPlugin *loadPlugin | ||
| #define HSIM_UNLOAD_PLUGIN_FUNC extern "C" void unloadPlugin | ||
|
|
||
| class PluginConsumer : public IEventConsumer { | ||
| public: | ||
| PluginConsumer(const std::filesystem::path &path, | ||
| const std::string &options) | ||
| : m_sharedLib{path, kLazy}, m_plugin{nullptr, nullptr} { | ||
| auto loadFunc = m_sharedLib.get<LoadPluginFunc>(kLoadPluginFuncName); | ||
| auto unloadFunc = | ||
| m_sharedLib.get<UnloadPluginFunc>(kUnloadPluginFuncName); | ||
|
|
||
| m_plugin = std::unique_ptr<IPlugin, UnloadPluginFunc>(loadFunc(options), | ||
| unloadFunc); | ||
| } | ||
|
|
||
| void handle(MemRead event, Addr addr) override { | ||
| m_plugin->handle(event, addr); | ||
| } | ||
| void handle(MemWrite event, Addr addr, Word value) override { | ||
| m_plugin->handle(event, addr, value); | ||
| } | ||
| void handle(PreInsn event, Word insn) override { | ||
| m_plugin->handle(event, insn); | ||
| } | ||
| void handle(PostInsn event, Word insn) override { | ||
| m_plugin->handle(event, insn); | ||
| } | ||
|
|
||
| private: | ||
| SharedLib m_sharedLib; | ||
| std::unique_ptr<IPlugin, UnloadPluginFunc> m_plugin; | ||
| }; | ||
|
|
||
| } // namespace hsim | ||
|
|
||
| #endif // HSIM_PLUGIN_INCLUDED |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #ifndef HSIM_SO_LOADER_INCLUDED | ||
| #define HSIM_SO_LOADER_INCLUDED | ||
|
|
||
| #include <cstdint> | ||
| #include <filesystem> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <type_traits> | ||
|
|
||
| #include <dlfcn.h> | ||
|
|
||
| namespace hsim { | ||
|
|
||
| enum SharedLibMode : std::uint16_t { | ||
| kLazy = RTLD_LAZY, | ||
| kNow = RTLD_NOW, | ||
| kGlobal = RTLD_GLOBAL, | ||
| kLocal = RTLD_LOCAL, | ||
| }; | ||
|
|
||
| template <typename T> | ||
| concept PointerT = std::is_pointer_v<T>; | ||
|
|
||
| class SharedLib { | ||
| private: | ||
| struct DlCloser { | ||
| void operator()(void *handle) { dlclose(handle); } | ||
| }; | ||
|
|
||
| public: | ||
| SharedLib(const std::filesystem::path &libPath, SharedLibMode mode) | ||
| : m_handle{dlopen(libPath.c_str(), mode)} { | ||
| if (m_handle == nullptr) { | ||
| throw std::runtime_error{dlerror()}; | ||
| } | ||
| } | ||
| template <PointerT T> T get(const std::string &symbol) { | ||
| void *loadedSymbol = dlsym(m_handle.get(), symbol.c_str()); | ||
| if (loadedSymbol == nullptr) { | ||
| throw std::runtime_error{dlerror()}; | ||
| } | ||
|
|
||
| return reinterpret_cast<T>(loadedSymbol); | ||
| } | ||
|
|
||
| private: | ||
| std::unique_ptr<void, DlCloser> m_handle; | ||
| static_assert(sizeof(m_handle) == sizeof(void *)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, it is not required by standard |
||
| }; | ||
|
|
||
| } // namespace hsim | ||
|
|
||
| #endif // HSIM_SO_LOADER_INCLUDED | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| add_library(simple_plugin SHARED | ||
| simple_plugin.cc | ||
| ) | ||
|
|
||
| target_include_directories(simple_plugin | ||
| PRIVATE | ||
| ${CMAKE_SOURCE_DIR}/include | ||
| ) | ||
|
|
||
| set(PLUGIN_OUTPUT_DIR "${CMAKE_BINARY_DIR}/plugins") | ||
| file(MAKE_DIRECTORY ${PLUGIN_OUTPUT_DIR}) | ||
|
|
||
| set_target_properties(simple_plugin PROPERTIES | ||
| LIBRARY_OUTPUT_DIRECTORY ${PLUGIN_OUTPUT_DIR} | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "machine_event.hh" | ||
| #include "plugin.hh" | ||
| #include "support.hh" | ||
|
|
||
| namespace hsim { | ||
|
|
||
| class SimplePlugin : public hsim::IPlugin { | ||
| public: | ||
| SimplePlugin(std::string name) : m_name{std::move(name)} { | ||
| std::cout << "SimplePlugin: " << m_name << std::endl; | ||
| } | ||
|
|
||
| ~SimplePlugin() override { | ||
| std::cout << "~SimplePlugin: " << m_name << std::endl; | ||
| } | ||
|
|
||
| void handle([[maybe_unused]] MemRead event, Addr addr) override { | ||
| std::cout << m_name << " MemRead: " << std::hex << addr << std::endl; | ||
| } | ||
|
|
||
| void handle([[maybe_unused]] MemWrite event, Addr addr, | ||
| Word value) override { | ||
| std::cout << m_name << " MemWrite: " << std::hex << addr << " " << value | ||
| << std::endl; | ||
| } | ||
|
|
||
| void handle([[maybe_unused]] PreInsn event, Word insn) override { | ||
| std::cout << m_name << " PreInsn: " << std::hex << insn << std::endl; | ||
| } | ||
|
|
||
| void handle([[maybe_unused]] PostInsn event, Word insn) override { | ||
| std::cout << m_name << " PostInsn: " << std::hex << insn << std::endl; | ||
| } | ||
|
|
||
| private: | ||
| std::string m_name; | ||
| }; | ||
|
|
||
| HSIM_LOAD_PLUGIN_FUNC(const std::string &options) { | ||
| return new SimplePlugin{options}; | ||
| } | ||
|
|
||
| HSIM_UNLOAD_PLUGIN_FUNC(SimplePlugin *plugin) { delete plugin; } | ||
|
|
||
| } // namespace hsim |
Uh oh!
There was an error while loading. Please reload this page.