Feat: add event bus and service framework

- introduce EventBus singleton wrapping eventpp::EventDispatcher with
  typed event ids and const void* payloads
- post EventClassFileLoadHook from ClassFileLoadHook callback
- add IService base with typed listen<>() helper that binds a member
  function directly to an event, no virtual onEvent dispatch
- add ServiceManager to register, initialize and shutdown services
- add BytecodeService that subscribes to ClassFileLoadHook
- wire Services::initializeAll() into Agent::init()
This commit is contained in:
xiaozhou233 committed 2026-08-17 15:45:39 +08:00
1 parent 6bf2d31798
commit 6962c4a477
9 files changed
+258 -3

No files matched your search

+7
View File
@@ -3,6 +3,7 @@
#include <JuiceAgent.hpp>
#include <JuiceAgent/Config.hpp>
#include <jni_common.hpp>
#include <services.hpp>
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;
}
}
+15 -2
View File
@@ -1,4 +1,5 @@
#include <jni_common.hpp>
#include <eventbus.hpp>
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);
}
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include <jni_impl.hpp>
#include <jni_common.hpp>
// ClassFileLoadHook
struct EventClassFileLoadHook {
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <eventpp/eventdispatcher.h>
#include <event/event_type.hpp>
enum class EventId {
ClassFileLoadHook,
MethodEntry,
MethodExit,
PreLoad,
Loaded
};
class EventBus {
public:
using Dispatcher = eventpp::EventDispatcher<EventId, void(const void*)>;
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 <typename C>
auto appendListener(EventId id, const C& callback) {
return dispatcher.appendListener(id, callback);
}
template <typename C>
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;
};
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <services/base.hpp>
#include <services/service_manager.hpp>
#include <services/bytecode_service.hpp>
namespace JuiceAgent {
namespace Services {
inline bool initializeAll() {
auto& manager = ServiceManager::getInstance();
manager.registerService<BytecodeService>();
return manager.initializeAll();
}
}
}
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include <vector>
#include <type_traits>
#include <JuiceAgent/Logger.hpp>
#include <eventbus.hpp>
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 <typename ServiceT, typename EventT>
void listen(EventId id, void (ServiceT::*handler)(const EventT&)) {
static_assert(std::is_base_of<IService, ServiceT>::value,
"ServiceT must derive from IService");
auto* self = static_cast<ServiceT*>(this);
handles_.push_back(
EventBus::getInstance().appendListener(
id,
[self, handler](const void* data) {
if (data) {
(self->*handler)(*static_cast<const EventT*>(data));
}
}
)
);
}
private:
std::vector<EventBus::Dispatcher::Handle> handles_;
};
}
}
@@ -0,0 +1,24 @@
#pragma once
#include <services/base.hpp>
#include <event/event_type.hpp>
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);
};
}
}
@@ -0,0 +1,71 @@
#pragma once
#include <services/base.hpp>
#include <algorithm>
#include <memory>
#include <vector>
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 <typename ServiceT, typename... Args>
ServiceT* registerService(Args&&... args) {
auto service = std::make_unique<ServiceT>(std::forward<Args>(args)...);
ServiceT* raw = service.get();
spdlog::debug("[ServiceManager] Registering service: {}", raw->name());
services_.push_back(std::move(service));
return raw;
}
template <typename ServiceT>
ServiceT* getService() const {
const std::type_info& target = typeid(ServiceT);
for (const auto& s : services_) {
if (typeid(*s) == target) {
return static_cast<ServiceT*>(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<std::unique_ptr<IService>> services_;
};
}
}
+32
View File
@@ -0,0 +1,32 @@
#include <services/bytecode_service.hpp>
#include <JuiceAgent/Logger.hpp>
namespace JuiceAgent {
namespace Services {
BytecodeService& BytecodeService::getInstance() {
static BytecodeService instance;
return instance;
}
bool BytecodeService::onInitialize() {
listen<BytecodeService, EventClassFileLoadHook>(
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 : "<null>");
}
}
}