mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Refactor: restructure libloader and drop config parsing
- Move loader logic to core/ and rename plantform/ to platform/ - Remove config.toml parsing from loader; pass runtime_dir to Bootstrap directly - Rewrite JvmAttach: RAII detach, drop dead thread_local, merge acquire helpers - Fix initialize() missing namespace qualification - Make GLOB sources pick up new files with CONFIGURE_DEPENDS
This commit is contained in:
1 parent
f2ce4e6a5a
commit
b34377d447
8 files changed
+139
-175
No files matched your search
@@ -0,0 +1,29 @@
|
||||
#include <Loader.hpp>
|
||||
#include <JvmAttach.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
|
||||
void JuiceAgent::Loader::entrypoint(const char* path) {
|
||||
// Attach to the JVM
|
||||
JuiceAgent::Loader::JvmAttach jvmAttach;
|
||||
|
||||
if(!jvmAttach.attach()) {
|
||||
spdlog::error("Failed to attach to JVM!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get JNIEnv
|
||||
JNIEnv* env = jvmAttach.get_env();
|
||||
if(!env) {
|
||||
spdlog::error("Failed to get JNIEnv!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get JVMTI environment
|
||||
jvmtiEnv* jvmti = jvmAttach.get_jvmti();
|
||||
if(!jvmti) {
|
||||
spdlog::error("Failed to get JVMTI environment!");
|
||||
return;
|
||||
}
|
||||
|
||||
JuiceAgent::Loader::preload(path, env, jvmti);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include <jvm/jni.h>
|
||||
#include <Loader.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <JuiceAgent/JuiceAgent-API-1.4.1+build.1.hpp>
|
||||
|
||||
// Preload section
|
||||
// Check parameters and load the runtime
|
||||
void JuiceAgent::Loader::preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) {
|
||||
// Check parameters
|
||||
if(!runtime_dir) {
|
||||
spdlog::warn("[PreLoad] Runtime directory is not specified!");
|
||||
}
|
||||
spdlog::info("[PreLoad] Runtime directory: {}", runtime_dir ? runtime_dir : "NULL");
|
||||
|
||||
// Extract JuiceAgent API to temp file
|
||||
std::string hash =
|
||||
JuiceAgent::Resource::juiceagent_api_bytes_sha256;
|
||||
|
||||
std::string file_name =
|
||||
hash.substr(0, 8) + "_" +
|
||||
JuiceAgent::Resource::juiceagent_api_bytes_name;
|
||||
|
||||
std::string juiceagent_api_path =
|
||||
JuiceAgent::Utils::File::write_to_tempfile(
|
||||
JuiceAgent::Resource::juiceagent_api_bytes,
|
||||
JuiceAgent::Resource::juiceagent_api_bytes_len,
|
||||
file_name
|
||||
);
|
||||
|
||||
// Debug
|
||||
spdlog::debug("JuiceAgent API path: {}", juiceagent_api_path);
|
||||
spdlog::debug("JuiceAgent API size: {}", JuiceAgent::Resource::juiceagent_api_bytes_len);
|
||||
spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256);
|
||||
|
||||
// Load JuiceAgent API
|
||||
jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
|
||||
if (status != JVMTI_ERROR_NONE) {
|
||||
spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast<int>(status));
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("JuiceAgent API loaded to system classloader!");
|
||||
|
||||
// Init JuiceAgent via JuiceAgent API
|
||||
JuiceAgent::Loader::initialize(runtime_dir, env, jvmti);
|
||||
}
|
||||
|
||||
void JuiceAgent::Loader::initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) {
|
||||
const char* bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap";
|
||||
const char* method_name = "start";
|
||||
|
||||
bool result = JuiceAgent::Utils::call_java_impl(env, bootstrap_class, method_name, runtime_dir);
|
||||
if (!result) {
|
||||
spdlog::error("Failed to initialize JuiceAgent!");
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("JuiceAgent initialized!");
|
||||
}
|
||||
Reference in new issue
Block a user