mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
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:
1 parent
3b180e7199
commit
51bcfbd549
47 files changed
+5583
-1462
No files matched your search
@@ -1,64 +0,0 @@
|
||||
#include <services.hpp>
|
||||
#include <jni_impl.hpp>
|
||||
|
||||
namespace JuiceAgent::services::Bytecode {
|
||||
void capture_bytecodes(const EventClassFileLoadHook& e) {
|
||||
std::lock_guard<std::mutex> lock(classDataMutex);
|
||||
|
||||
if (!classToCapture.contains(e.name) || classFileDataMap.contains(e.name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ClassFileData data;
|
||||
data.classname = e.name;
|
||||
data.bytecode.assign(e.classbytes, e.classbytes + e.class_data_len);
|
||||
|
||||
if (e.jni_env) {
|
||||
if (e.class_being_redefined) data.clazz = static_cast<jclass>(e.jni_env->NewGlobalRef(e.class_being_redefined));
|
||||
if (e.loader) data.classloader = e.jni_env->NewGlobalRef(e.loader);
|
||||
if (e.protection_domain) data.protection_domain = e.jni_env->NewGlobalRef(e.protection_domain);
|
||||
}
|
||||
|
||||
classFileDataMap[e.name] = std::move(data);
|
||||
classToCapture.erase(e.name);
|
||||
|
||||
spdlog::info("Captured class: {} (length: {})", e.name, e.class_data_len);
|
||||
}
|
||||
|
||||
void patch_bytecodes(const EventClassFileLoadHook& e) {
|
||||
std::lock_guard<std::mutex> lock(pendingRetransformMutex);
|
||||
|
||||
auto it = pendingRetransform.find(e.name);
|
||||
if (it == pendingRetransform.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& bytes = it->second;
|
||||
const jint new_len = static_cast<jint>(bytes.size());
|
||||
|
||||
unsigned char* new_buf = nullptr;
|
||||
if (e.jvmti_env->Allocate(bytes.size(), &new_buf) != JVMTI_ERROR_NONE) {
|
||||
spdlog::error("Failed to allocate buffer for retransform: {}", e.name);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(new_buf, bytes.data(), bytes.size());
|
||||
|
||||
*e.new_class_data_len = new_len;
|
||||
*e.new_classbytes = new_buf;
|
||||
|
||||
pendingRetransform.erase(it);
|
||||
|
||||
spdlog::info("Retransformed: {} (new length: {})", e.name, new_len);
|
||||
}
|
||||
|
||||
void init() {
|
||||
agent().get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
|
||||
agent().get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
|
||||
}
|
||||
|
||||
void start() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
#include <services.hpp>
|
||||
|
||||
namespace JuiceAgent::services::JarLoader {
|
||||
static JarLoaderConfig config;
|
||||
|
||||
static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader";
|
||||
static const char* MODULE_METHOD = "loadJar";
|
||||
|
||||
void init() {
|
||||
auto& cfg = agent().get_config();
|
||||
|
||||
config.enabled = cfg.get<bool>(
|
||||
"JuiceAgent.Modules.JarLoader.Enabled",
|
||||
false
|
||||
);
|
||||
|
||||
config.injection_dir = cfg.get<std::string>(
|
||||
"JuiceAgent.Modules.JarLoader.InjectionDir",
|
||||
(cfg.runtime_dir() / "injection").string(),
|
||||
true
|
||||
);
|
||||
|
||||
config.jar_path = cfg.get<std::string>(
|
||||
"JuiceAgent.Modules.JarLoader.JarPath",
|
||||
"./Entry.jar",
|
||||
true
|
||||
);
|
||||
|
||||
config.entry_class = cfg.get<std::string>(
|
||||
"JuiceAgent.Modules.JarLoader.EntryClass",
|
||||
"Example.Main",
|
||||
false
|
||||
);
|
||||
|
||||
config.entry_method = cfg.get<std::string>(
|
||||
"JuiceAgent.Modules.JarLoader.EntryMethod",
|
||||
"start",
|
||||
false
|
||||
);
|
||||
|
||||
spdlog::info("JarLoaderService initialized");
|
||||
}
|
||||
|
||||
void start() {
|
||||
if (!config.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
JuiceAgent::Utils::Serializer ser;
|
||||
|
||||
ser.add_kv("InjectionDir", config.injection_dir);
|
||||
ser.add_kv("JarPath", config.jar_path);
|
||||
ser.add_kv("EntryClass", config.entry_class);
|
||||
ser.add_kv("EntryMethod", config.entry_method);
|
||||
|
||||
auto data = ser.serialize();
|
||||
|
||||
JuiceAgent::Utils::call_java_impl(
|
||||
agent().get_env(),
|
||||
MODULE_CLASS,
|
||||
MODULE_METHOD,
|
||||
data.c_str()
|
||||
);
|
||||
|
||||
spdlog::info("JarLoader start");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#include <services.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
|
||||
namespace JuiceAgent::services::Manager {
|
||||
void register_events() {
|
||||
// PreLoad
|
||||
agent().get_eventbus().subscribe<EventPreLoad>(init);
|
||||
|
||||
// Loaded
|
||||
agent().get_eventbus().subscribe<EventLoaded>(start);
|
||||
|
||||
// TODO: Stopped
|
||||
|
||||
spdlog::info("Services Manager registered events");
|
||||
}
|
||||
|
||||
void init(const EventPreLoad& event) {
|
||||
spdlog::info("Initializing services");
|
||||
// JarLoader
|
||||
JuiceAgent::services::JarLoader::init();
|
||||
|
||||
// Bytecode
|
||||
JuiceAgent::services::Bytecode::init();
|
||||
}
|
||||
|
||||
void start(const EventLoaded& event) {
|
||||
spdlog::info("Starting services");
|
||||
// JarLoader
|
||||
JuiceAgent::services::JarLoader::start();
|
||||
|
||||
// Bytecode
|
||||
JuiceAgent::services::Bytecode::start();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user