diff --git a/src/libagent/JuiceAgent.cpp b/src/libagent/JuiceAgent.cpp index baf4c8b..7444eb0 100644 --- a/src/libagent/JuiceAgent.cpp +++ b/src/libagent/JuiceAgent.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace JuiceAgent { // Singleton @@ -93,6 +94,12 @@ namespace JuiceAgent { spdlog::error("[JuiceAgent] Could not enable JVMTI event notifications: {}", result); } + // Initialize services (and their plugins) + if(!JuiceAgent::Services::initializeAll()) { + spdlog::error("[JuiceAgent] Failed to initialize services"); + return false; + } + return true; } } \ No newline at end of file diff --git a/src/libagent/callbacks/ClassFileLoadHook.cpp b/src/libagent/callbacks/ClassFileLoadHook.cpp index baa7ed9..f69c03c 100644 --- a/src/libagent/callbacks/ClassFileLoadHook.cpp +++ b/src/libagent/callbacks/ClassFileLoadHook.cpp @@ -1,4 +1,5 @@ #include +#include void JNICALL ClassFileLoadHook( jvmtiEnv* jvmti_env, @@ -15,7 +16,19 @@ void JNICALL ClassFileLoadHook( spdlog::warn("[ClassFileLoadHook] name is null"); return; } + + EventClassFileLoadHook event{ + jvmti_env, + jni_env, + class_being_redefined, + loader, + name, + protection_domain, + class_data_len, + classbytes, + new_class_data_len, + new_classbytes + }; - // TODO: Impl & Delete this - spdlog::trace("[ClassFileLoadHook] name: {}", name); + EventBus::getInstance().post(EventId::ClassFileLoadHook, &event); } \ No newline at end of file diff --git a/src/libagent/includes/event/event_type.hpp b/src/libagent/includes/event/event_type.hpp index f7a20d6..4b1b0cf 100644 --- a/src/libagent/includes/event/event_type.hpp +++ b/src/libagent/includes/event/event_type.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include // ClassFileLoadHook struct EventClassFileLoadHook { diff --git a/src/libagent/includes/eventbus.hpp b/src/libagent/includes/eventbus.hpp new file mode 100644 index 0000000..e6d74a7 --- /dev/null +++ b/src/libagent/includes/eventbus.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +enum class EventId { + ClassFileLoadHook, + MethodEntry, + MethodExit, + PreLoad, + Loaded +}; + +class EventBus { +public: + using Dispatcher = eventpp::EventDispatcher; + + static EventBus& getInstance() { + static EventBus instance; + return instance; + } + + EventBus(const EventBus&) = delete; + EventBus& operator=(const EventBus&) = delete; + + void post(EventId id, const void* event) { + dispatcher.dispatch(id, event); + } + + template + auto appendListener(EventId id, const C& callback) { + return dispatcher.appendListener(id, callback); + } + + template + auto prependListener(EventId id, const C& callback) { + return dispatcher.prependListener(id, callback); + } + + bool removeListener(EventId id, Dispatcher::Handle handle) { + return dispatcher.removeListener(id, handle); + } + +private: + EventBus() = default; + Dispatcher dispatcher; +}; \ No newline at end of file diff --git a/src/libagent/includes/services.hpp b/src/libagent/includes/services.hpp new file mode 100644 index 0000000..1771fe7 --- /dev/null +++ b/src/libagent/includes/services.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +namespace JuiceAgent { +namespace Services { + +inline bool initializeAll() { + auto& manager = ServiceManager::getInstance(); + manager.registerService(); + return manager.initializeAll(); +} + +} +} diff --git a/src/libagent/includes/services/base.hpp b/src/libagent/includes/services/base.hpp new file mode 100644 index 0000000..e5fa5e2 --- /dev/null +++ b/src/libagent/includes/services/base.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include + +namespace JuiceAgent { +namespace Services { + +class IService { +public: + virtual ~IService() = default; + + virtual const char* name() const = 0; + + virtual bool onInitialize() { return true; } + + virtual void onShutdown() {} + +protected: + template + void listen(EventId id, void (ServiceT::*handler)(const EventT&)) { + static_assert(std::is_base_of::value, + "ServiceT must derive from IService"); + auto* self = static_cast(this); + handles_.push_back( + EventBus::getInstance().appendListener( + id, + [self, handler](const void* data) { + if (data) { + (self->*handler)(*static_cast(data)); + } + } + ) + ); + } + +private: + std::vector handles_; +}; + +} +} diff --git a/src/libagent/includes/services/bytecode_service.hpp b/src/libagent/includes/services/bytecode_service.hpp new file mode 100644 index 0000000..d96b6bc --- /dev/null +++ b/src/libagent/includes/services/bytecode_service.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace JuiceAgent { +namespace Services { + +class BytecodeService : public IService { +public: + static BytecodeService& getInstance(); + + const char* name() const override { return "BytecodeService"; } + + bool onInitialize() override; + + void onShutdown() override; + +private: + void onClassFileLoad(const EventClassFileLoadHook& event); +}; + +} +} diff --git a/src/libagent/includes/services/service_manager.hpp b/src/libagent/includes/services/service_manager.hpp new file mode 100644 index 0000000..6d50389 --- /dev/null +++ b/src/libagent/includes/services/service_manager.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include + +#include +#include +#include + +namespace JuiceAgent { +namespace Services { + +class ServiceManager { +public: + static ServiceManager& getInstance() { + static ServiceManager instance; + return instance; + } + + ServiceManager(const ServiceManager&) = delete; + ServiceManager& operator=(const ServiceManager&) = delete; + + template + ServiceT* registerService(Args&&... args) { + auto service = std::make_unique(std::forward(args)...); + ServiceT* raw = service.get(); + spdlog::debug("[ServiceManager] Registering service: {}", raw->name()); + services_.push_back(std::move(service)); + return raw; + } + + template + ServiceT* getService() const { + const std::type_info& target = typeid(ServiceT); + for (const auto& s : services_) { + if (typeid(*s) == target) { + return static_cast(s.get()); + } + } + return nullptr; + } + + bool initializeAll() { + for (auto& s : services_) { + spdlog::info("[ServiceManager] Initializing service: {}", s->name()); + if (!s->onInitialize()) { + spdlog::error("[ServiceManager] Service '{}' failed to initialize", s->name()); + return false; + } + } + return true; + } + + void shutdownAll() { + for (auto it = services_.rbegin(); it != services_.rend(); ++it) { + spdlog::info("[ServiceManager] Shutting down service: {}", (*it)->name()); + (*it)->onShutdown(); + } + services_.clear(); + } + +private: + ServiceManager() = default; + ~ServiceManager() { + shutdownAll(); + } + + std::vector> services_; +}; + +} +} diff --git a/src/libagent/services/BytecodeService.cpp b/src/libagent/services/BytecodeService.cpp new file mode 100644 index 0000000..429ad08 --- /dev/null +++ b/src/libagent/services/BytecodeService.cpp @@ -0,0 +1,32 @@ +#include +#include + +namespace JuiceAgent { +namespace Services { + +BytecodeService& BytecodeService::getInstance() { + static BytecodeService instance; + return instance; +} + +bool BytecodeService::onInitialize() { + listen( + EventId::ClassFileLoadHook, + &BytecodeService::onClassFileLoad + ); + + spdlog::info("[BytecodeService] Initialized"); + return true; +} + +void BytecodeService::onShutdown() { + spdlog::info("[BytecodeService] Shutdown"); +} + +void BytecodeService::onClassFileLoad(const EventClassFileLoadHook& event) { + spdlog::trace("[BytecodeService] ClassFileLoadHook: {}", + event.name ? event.name : ""); +} + +} +}