mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
- libagent: rewrite EventBus as a type-safe dispatcher keyed by event type; listeners are invoked with the concrete event type instead of 'const void*', and the bus is mutex-protected - libagent: services subscribe/unsubscribe by event type (IService::listen no longer takes an EventId) - libagent: remove the unused MethodEntry/MethodExit events and EventMethodEntry/EventMethodExit structs - build: drop the in-tree eventpp headers (23 files) and its license
121 lines
4.1 KiB
C++
121 lines
4.1 KiB
C++
#include <string>
|
|
#include <JuiceAgent/Logger.hpp>
|
|
#include <JuiceAgent.hpp>
|
|
#include <JuiceAgent/Config.hpp>
|
|
#include <jni_common.hpp>
|
|
#include <services.hpp>
|
|
#include <eventbus.hpp>
|
|
#include <event/event_type.hpp>
|
|
|
|
namespace JuiceAgent {
|
|
// Singleton
|
|
Agent& Agent::getInstance() {
|
|
static Agent instance;
|
|
return instance;
|
|
}
|
|
|
|
bool Agent::preload(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti, std::string& runtime_dir) {
|
|
// Check if the agent is already loaded
|
|
if(Agent::getInstance().isLoaded()) {
|
|
spdlog::warn("[JuiceAgent] Agent already loaded! Loading again will cause problems!");
|
|
}
|
|
|
|
// Environment check
|
|
if(!jvm || jvm==nullptr) {
|
|
spdlog::error("[JuiceAgent] No JVM found!");
|
|
return false;
|
|
}
|
|
if(!env || env==nullptr) {
|
|
spdlog::error("[JuiceAgent] Could not get JNIEnv!");
|
|
return false;
|
|
}
|
|
if(!jvmti || jvmti==nullptr) {
|
|
spdlog::error("[JuiceAgent] Could not get JVMTI!");
|
|
return false;
|
|
}
|
|
if(runtime_dir.empty()) {
|
|
spdlog::warn("[JuiceAgent] No runtime directory specified!");
|
|
spdlog::warn("[JuiceAgent] No config provided, Modules will not be loaded!");
|
|
} else {
|
|
JuiceAgent::Config::Config cfg(runtime_dir);
|
|
setConfig(cfg);
|
|
if(!getConfig().isValid()) {
|
|
spdlog::warn("[JuiceAgent] Config is not valid!");
|
|
spdlog::warn("[JuiceAgent] No config provided, Modules will not be loaded!");
|
|
}
|
|
}
|
|
|
|
// Set Environment
|
|
setJavaVM(jvm);
|
|
setJNIEnv(env);
|
|
setJVMTI(jvmti);
|
|
// Debug
|
|
spdlog::debug("[JuiceAgent] JavaVM: {:p}, JNIEnv: {:p}, JVMTI: {:p}",
|
|
(void*)jvm,
|
|
(void*)env,
|
|
(void*)jvmti);
|
|
|
|
setLoaded(true);
|
|
|
|
// Post preload event (after environment is set so listeners
|
|
// can access the agent state via Agent::getInstance())
|
|
EventPreLoad preloadEvent{ jvm, env, jvmti };
|
|
EventBus::getInstance().post(preloadEvent);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Agent::init() {
|
|
if(!isLoaded()) {
|
|
spdlog::error("[JuiceAgent] Agent not loaded! Please call preload() first!");
|
|
return false;
|
|
}
|
|
if(initialized_) {
|
|
spdlog::warn("[JuiceAgent] Agent already initialized! Skipping...");
|
|
return true;
|
|
}
|
|
initialized_ = true;
|
|
|
|
// Add Abilities
|
|
jvmtiCapabilities caps{};
|
|
caps.can_get_bytecodes = 1;
|
|
caps.can_redefine_classes = 1;
|
|
caps.can_redefine_any_class = 1;
|
|
caps.can_retransform_classes = 1;
|
|
caps.can_retransform_any_class = 1;
|
|
caps.can_generate_all_class_hook_events = 1;
|
|
jint result = getJVMTI()->AddCapabilities(&caps);
|
|
if(result != JVMTI_ERROR_NONE) {
|
|
spdlog::error("[JuiceAgent] Could not add JVMTI capabilities: {}", result);
|
|
return false;
|
|
}
|
|
spdlog::info("[JuiceAgent] Added JVMTI capabilities");
|
|
|
|
// Register callbacks
|
|
jvmtiEventCallbacks callbacks{};
|
|
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
|
|
result = getJVMTI()->SetEventCallbacks(&callbacks, sizeof(callbacks));
|
|
if(result != JVMTI_ERROR_NONE) {
|
|
spdlog::error("[JuiceAgent] Could not set JVMTI event callbacks: {}", result);
|
|
}
|
|
|
|
// Enable events
|
|
result = getJVMTI()->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, nullptr);
|
|
if(result != JVMTI_ERROR_NONE) {
|
|
spdlog::error("[JuiceAgent] Could not enable JVMTI event notifications: {}", result);
|
|
}
|
|
|
|
// Initialize services
|
|
if(!JuiceAgent::Services::initializeAll()) {
|
|
spdlog::error("[JuiceAgent] Failed to initialize services");
|
|
return false;
|
|
}
|
|
|
|
// Post loaded event
|
|
EventLoaded loadedEvent{ getJavaVM(), getJNIEnv(), getJVMTI() };
|
|
EventBus::getInstance().post(loadedEvent);
|
|
|
|
|
|
return true;
|
|
}
|
|
} |