From 2ab1c134ed798aeea74841440ba1104dba978aef Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sun, 16 Aug 2026 22:38:08 +0800 Subject: [PATCH] Feat: load config in libagent to prepare for modules - Make runtime_dir optional in JuiceAgent.init (tolerate null jstring) - Load config.toml into Agent and expose getConfig/setConfig - Warn instead of failing when runtime directory or config is missing --- src/libagent/JuiceAgent.cpp | 13 +++++++++++++ src/libagent/entry/jni.cpp | 19 +++++++++++++------ src/libagent/includes/JuiceAgent.hpp | 5 ++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/libagent/JuiceAgent.cpp b/src/libagent/JuiceAgent.cpp index 8405b9a..baf4c8b 100644 --- a/src/libagent/JuiceAgent.cpp +++ b/src/libagent/JuiceAgent.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include namespace JuiceAgent { @@ -29,6 +30,18 @@ namespace JuiceAgent { spdlog::error("[JuiceAgent] Could not get JVMTI!"); return false; } + if(runtime_dir.empty()) { + spdlog::warn("[JuiceAgent] No runtime directory specified!"); + spdlog::warn("[JuiceAgent] No config provided, Modules will not be loaded!"); + } else { + JuiceAgent::Config::Config cfg(runtime_dir); + setConfig(cfg); + if(!getConfig().is_valid()) { + spdlog::warn("[JuiceAgent] Config is not valid!"); + spdlog::warn("[JuiceAgent] No config provided, Modules will not be loaded!"); + } + } + // Set Environment setJavaVM(jvm); diff --git a/src/libagent/entry/jni.cpp b/src/libagent/entry/jni.cpp index e2ed918..d9cdfe9 100644 --- a/src/libagent/entry/jni.cpp +++ b/src/libagent/entry/jni.cpp @@ -3,18 +3,25 @@ // This function is called by Java to initialize the agent. JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init (JNIEnv *env, jclass , jstring j_runtime_dir) { + Logger::init("libagent.log"); spdlog::info("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); + std::string runtime_dir; + + if (j_runtime_dir != nullptr) { + const char* c_runtime_dir = env->GetStringUTFChars(j_runtime_dir, nullptr); + + if (c_runtime_dir != nullptr) { + runtime_dir = c_runtime_dir; + env->ReleaseStringUTFChars(j_runtime_dir, c_runtime_dir); + } + } if (runtime_dir.empty()) { - spdlog::error("Failed to get runtime directory from argument, runtime_dir is empty"); - return JNI_FALSE; + spdlog::warn("Runtime directory is empty"); } spdlog::info("JuiceAgent got runtime directory: {}", runtime_dir); @@ -41,4 +48,4 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init agent.init(); return JNI_TRUE; -} +} \ No newline at end of file diff --git a/src/libagent/includes/JuiceAgent.hpp b/src/libagent/includes/JuiceAgent.hpp index 6df0fd4..57f1400 100644 --- a/src/libagent/includes/JuiceAgent.hpp +++ b/src/libagent/includes/JuiceAgent.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace JuiceAgent { class Agent{ @@ -9,6 +10,7 @@ namespace JuiceAgent { jvmtiEnv* jvmti; // Unsafe: JNIEnv JNIEnv* env; + JuiceAgent::Config::Config config; bool loaded = false; public: @@ -27,12 +29,13 @@ namespace JuiceAgent { jvmtiEnv* getJVMTI() const { return jvmti; } JNIEnv* getJNIEnv() const { return env; } bool isLoaded() const { return loaded; } + JuiceAgent::Config::Config& getConfig() { return config; } // setters void setJavaVM(JavaVM* jvm) { this->jvm = jvm; } void setJVMTI(jvmtiEnv* jvmti) { this->jvmti = jvmti; } void setJNIEnv(JNIEnv* env) { this->env = env; } void setLoaded(bool loaded) { this->loaded = loaded; } - + void setConfig(JuiceAgent::Config::Config& config) { this->config = config; } }; } \ No newline at end of file