Refactor: rebuild libagent with simplified lifecycle

- Replace eventbus/services with Agent preload/init lifecycle
- Move JNI header to shared includes; add jni_common.hpp umbrella header
- Reduce ClassFileLoadHook to a stub for later implementation
- Add eventpp dependency
- Document custom API JAR workflow (WIP)
This commit is contained in:
xiaozhou233 committed 2026-08-16 21:14:44 +08:00
1 parent 3b180e7199
commit 51bcfbd549
47 files changed
+5583 -1462

No files matched your search

+73 -84
View File
@@ -1,96 +1,85 @@
#include <string>
#include <JuiceAgent/Logger.hpp>
#include <JuiceAgent.hpp>
#include <jni_impl.hpp>
#include <event/event_type.hpp>
#include <event/eventbus.hpp>
#include <JuiceAgent/Config.hpp>
#include <services.hpp>
#include <jni_common.hpp>
namespace JuiceAgent {
// ===== Singleton =====
Agent& Agent::instance() {
// Singleton
Agent& Agent::getInstance() {
static Agent instance;
return instance;
}
bool Agent::init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti, std::string& runtime_dir) {
try {
spdlog::info("JuiceAgent Native EntryPoint Invoked!");
// ======== neccessary environment check ========
if (jvm == nullptr) {
spdlog::error("Failed to initialize JuiceAgent: jvm is null");
return false;
}
if (env == nullptr) {
spdlog::error("Failed to initialize JuiceAgent: env is null");
return false;
}
if (jvmti == nullptr) {
spdlog::error("Failed to initialize JuiceAgent: jvmti is null");
return false;
}
JuiceAgent::Config::Config cfg (runtime_dir);
if (!cfg.is_valid()) {
spdlog::error("Failed to initialize JuiceAgent: config is invalid");
return false;
}
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!");
}
set_jvm(jvm);
set_jvmti(jvmti);
set_env(env);
set_config(cfg);
spdlog::debug("env: {}, jvmti: {}", fmt::ptr(env), fmt::ptr(jvmti));
// Register events for services
JuiceAgent::services::Manager::register_events();
// PreLoad Event
eventbus.post(EventPreLoad{
.jvm = jvm,
.env = env,
.jvmti = jvmti
});
// ======== Application ability ========
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 = jvmti->AddCapabilities(&caps);
if (result != JNI_OK) {
spdlog::error("Failed to add capabilities: {}", result);
return false;
}
spdlog::info("Abilities added successfully");
// ======== Register callbacks ========
jvmtiEventCallbacks callbacks{};
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
result = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
if (result != JNI_OK) {
spdlog::error("Failed to set event callbacks: {}", result);
}
// Enable events
result = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
if (result != JNI_OK) {
spdlog::error("Failed to enable event notification: {}", result);
}
// Loaded Event
eventbus.post(EventLoaded{
.jvm = jvm,
.env = env,
.jvmti = jvmti
});
return true;
} catch (const std::exception& e) {
spdlog::error("Exception: {}", e.what());
// 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;
}
// 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);
return true;
}
bool Agent::init() {
if(!isLoaded()) {
spdlog::error("[JuiceAgent] Agent not loaded! Please call preload() first!");
return false;
}
// 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);
}
return true;
}
}