mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Feat: libagent can now read the config
This commit is contained in:
1 parent
6a7e5a1d38
commit
9f366645c9
5 files changed
+103
-74
No files matched your search
@@ -49,7 +49,7 @@ public:
|
||||
};
|
||||
|
||||
namespace Utils {
|
||||
LoaderConfig get_loader_config(const Config& config) {
|
||||
inline LoaderConfig get_loader_config(const Config& config) {
|
||||
auto cfg = config.get_config().at("JuiceAgent").at("Loader");
|
||||
|
||||
LoaderConfig loader_config = LoaderConfig();
|
||||
@@ -60,13 +60,13 @@ namespace Utils {
|
||||
return loader_config;
|
||||
}
|
||||
|
||||
void print_loader_config(const LoaderConfig& config) {
|
||||
inline void print_loader_config(const LoaderConfig& config) {
|
||||
PLOGI << "JuiceAgentAPIJarPath: " << config.JuiceAgentAPIJarPath;
|
||||
PLOGI << "JuiceAgentNativeLibraryPath: " << config.JuiceAgentNativeLibraryPath;
|
||||
PLOGI << "RuntimeDir: " << config.RuntimeDir;
|
||||
}
|
||||
|
||||
std::string serialize_loader_config(const LoaderConfig& config) {
|
||||
inline std::string serialize_loader_config(const LoaderConfig& config) {
|
||||
std::string result = "";
|
||||
|
||||
result += "Version=2;";
|
||||
|
||||
@@ -3,11 +3,22 @@
|
||||
|
||||
// This function is called by Java to initialize the agent.
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init
|
||||
(JNIEnv *env, jclass loader_class) {
|
||||
(JNIEnv *env, jclass , jstring j_runtime_dir) {
|
||||
InitLogger();
|
||||
|
||||
PLOGI << "Initializing JuiceAgent...";
|
||||
|
||||
// Get runtime directory
|
||||
const char* c_runtime_dir = env->GetStringUTFChars(j_runtime_dir, nullptr);
|
||||
std::string runtime_dir(c_runtime_dir);
|
||||
env->ReleaseStringUTFChars(j_runtime_dir, c_runtime_dir);
|
||||
|
||||
if (runtime_dir.empty()) {
|
||||
PLOGE << "Failed to get runtime directory from argument, runtime_dir is empty";
|
||||
return JNI_FALSE;
|
||||
}
|
||||
PLOGI << "JuiceAgent got runtime directory: " << runtime_dir;
|
||||
|
||||
JavaVM* jvm = nullptr;
|
||||
jvmtiEnv* jvmti = nullptr;
|
||||
|
||||
@@ -27,7 +38,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init
|
||||
|
||||
// Invoke EntryPoint
|
||||
JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||
agent.init(jvm, env, jvmti);
|
||||
agent.init(jvm, env, jvmti, runtime_dir);
|
||||
|
||||
return JNI_TRUE;
|
||||
}
|
||||
@@ -10,10 +10,10 @@ extern "C" {
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceagent_api_JuiceAgent
|
||||
* Method: init
|
||||
* Signature: ()Z
|
||||
* Signature: (Ljava/lang/String;)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init
|
||||
(JNIEnv *, jclass);
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceagent_api_JuiceAgent
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <event/eventbus.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
|
||||
namespace JuiceAgent {
|
||||
class Agent {
|
||||
@@ -11,6 +12,7 @@ namespace JuiceAgent {
|
||||
JavaVM* jvm;
|
||||
jvmtiEnv* jvmti;
|
||||
EventBus eventbus;
|
||||
JuiceAgent::Config::Config config;
|
||||
|
||||
private:
|
||||
Agent() = default;
|
||||
@@ -24,15 +26,17 @@ namespace JuiceAgent {
|
||||
Agent& operator=(const Agent&) = delete;
|
||||
|
||||
// EntryPoint
|
||||
bool init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti);
|
||||
bool init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti, std::string& runtime_dir);
|
||||
|
||||
// getters
|
||||
JavaVM* get_jvm() const { return jvm; }
|
||||
jvmtiEnv* get_jvmti() const { return jvmti; }
|
||||
EventBus& get_eventbus() { return eventbus; }
|
||||
JuiceAgent::Config::Config& get_config() { return config; }
|
||||
|
||||
// setters
|
||||
void set_jvm(JavaVM* jvm) { this->jvm = jvm; }
|
||||
void set_jvmti(jvmtiEnv* jvmti) { this->jvmti = jvmti; }
|
||||
void set_config(JuiceAgent::Config::Config& config) { this->config = config; }
|
||||
};
|
||||
}
|
||||
+80
-66
@@ -3,6 +3,7 @@
|
||||
#include <bytecodes_opt.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
#include <event/eventbus.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
|
||||
namespace JuiceAgent {
|
||||
// ===== Singleton =====
|
||||
@@ -11,74 +12,87 @@ namespace JuiceAgent {
|
||||
return instance;
|
||||
}
|
||||
|
||||
bool Agent::init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti) {
|
||||
PLOGI << "JuiceAgent Native EntryPoint Invoked!";
|
||||
bool Agent::init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti, std::string& runtime_dir) {
|
||||
try {
|
||||
PLOGI << "JuiceAgent Native EntryPoint Invoked!";
|
||||
|
||||
// ======== neccessary environment check ========
|
||||
if (jvm == nullptr) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: jvm is null";
|
||||
// ======== neccessary environment check ========
|
||||
if (jvm == nullptr) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: jvm is null";
|
||||
return false;
|
||||
}
|
||||
if (env == nullptr) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: env is null";
|
||||
return false;
|
||||
}
|
||||
if (jvmti == nullptr) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: jvmti is null";
|
||||
return false;
|
||||
}
|
||||
JuiceAgent::Config::Config cfg (runtime_dir);
|
||||
if (!cfg.is_valid()) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: config is invalid";
|
||||
return false;
|
||||
}
|
||||
|
||||
set_jvm(jvm);
|
||||
set_jvmti(jvmti);
|
||||
set_config(cfg);
|
||||
PLOGD << "env: " << env << ", jvmti: " << jvmti;
|
||||
PLOGD << "config: " << &cfg.get_config();
|
||||
|
||||
|
||||
// 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) {
|
||||
PLOGE << "Failed to add capabilities: " << result;
|
||||
return false;
|
||||
}
|
||||
|
||||
PLOGI << "Abilities added successfully";
|
||||
|
||||
// ======== Register callbacks ========
|
||||
jvmtiEventCallbacks callbacks{};
|
||||
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
|
||||
result = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
|
||||
if (result != JNI_OK) {
|
||||
PLOGE << "Failed to set event callbacks: " << result;
|
||||
}
|
||||
|
||||
// Enable events
|
||||
result = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
|
||||
if (result != JNI_OK) {
|
||||
PLOGE << "Failed to enable event notification: " << result;
|
||||
}
|
||||
|
||||
// register bytecodes_opt
|
||||
JuiceAgent::Bytecodes::register_bytecodes();
|
||||
|
||||
|
||||
// Loaded Event
|
||||
eventbus.post(EventLoaded{
|
||||
.jvm = jvm,
|
||||
.env = env,
|
||||
.jvmti = jvmti
|
||||
});
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
PLOGE << "Exception: " << e.what();
|
||||
return false;
|
||||
}
|
||||
if (env == nullptr) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: env is null";
|
||||
return false;
|
||||
}
|
||||
if (jvmti == nullptr) {
|
||||
PLOGE << "Failed to initialize JuiceAgent: jvmti is null";
|
||||
return false;
|
||||
}
|
||||
|
||||
set_jvm(jvm);
|
||||
set_jvmti(jvmti);
|
||||
PLOGD << "env: " << env << ", jvmti: " << jvmti;
|
||||
|
||||
// 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) {
|
||||
PLOGE << "Failed to add capabilities: " << result;
|
||||
return false;
|
||||
}
|
||||
|
||||
PLOGI << "Abilities added successfully";
|
||||
|
||||
// ======== Register callbacks ========
|
||||
jvmtiEventCallbacks callbacks{};
|
||||
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
|
||||
result = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
|
||||
if (result != JNI_OK) {
|
||||
PLOGE << "Failed to set event callbacks: " << result;
|
||||
}
|
||||
|
||||
// Enable events
|
||||
result = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
|
||||
if (result != JNI_OK) {
|
||||
PLOGE << "Failed to enable event notification: " << result;
|
||||
}
|
||||
|
||||
// register bytecodes_opt
|
||||
JuiceAgent::Bytecodes::register_bytecodes();
|
||||
|
||||
|
||||
// Loaded Event
|
||||
eventbus.post(EventLoaded{
|
||||
.jvm = jvm,
|
||||
.env = env,
|
||||
.jvmti = jvmti
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user