mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
85 lines
2.5 KiB
C++
85 lines
2.5 KiB
C++
#include <libagent.hpp>
|
|
#include <jni_impl.hpp>
|
|
#include <bytecodes_opt.hpp>
|
|
#include <event/event_type.hpp>
|
|
#include <event/eventbus.hpp>
|
|
|
|
namespace JuiceAgent {
|
|
// ===== Singleton =====
|
|
Agent& Agent::instance() {
|
|
static Agent instance;
|
|
return instance;
|
|
}
|
|
|
|
bool Agent::init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti) {
|
|
PLOGI << "JuiceAgent Native EntryPoint Invoked!";
|
|
|
|
// PreLoad Event
|
|
eventbus.post(EventPreLoad{
|
|
.jvm = jvm,
|
|
.env = env,
|
|
.jvmti = jvmti
|
|
});
|
|
|
|
// ======== 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;
|
|
}
|
|
|
|
set_jvm(jvm);
|
|
set_jvmti(jvmti);
|
|
|
|
PLOGD << "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;
|
|
}
|
|
} |