diff --git a/includes/JuiceAgent/Config.hpp b/includes/JuiceAgent/Config.hpp index d0d8cce..081cdf4 100644 --- a/includes/JuiceAgent/Config.hpp +++ b/includes/JuiceAgent/Config.hpp @@ -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;"; diff --git a/src/libagent/entry/jni.cpp b/src/libagent/entry/jni.cpp index f5b52eb..052e52e 100644 --- a/src/libagent/entry/jni.cpp +++ b/src/libagent/entry/jni.cpp @@ -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; } diff --git a/src/libagent/includes/cn_xiaozhou233_juiceagent_api_JuiceAgent.h b/src/libagent/includes/cn_xiaozhou233_juiceagent_api_JuiceAgent.h index c99f2ca..5f019b1 100644 --- a/src/libagent/includes/cn_xiaozhou233_juiceagent_api_JuiceAgent.h +++ b/src/libagent/includes/cn_xiaozhou233_juiceagent_api_JuiceAgent.h @@ -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 diff --git a/src/libagent/includes/libagent.hpp b/src/libagent/includes/libagent.hpp index 92375d4..0cd91ff 100644 --- a/src/libagent/includes/libagent.hpp +++ b/src/libagent/includes/libagent.hpp @@ -4,6 +4,7 @@ #include #include #include +#include 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; } }; } \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index 76cbbec..b958860 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -3,6 +3,7 @@ #include #include #include +#include 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; } } \ No newline at end of file