diff --git a/src/libagent/callback/ClassFileLoadHook.cpp b/src/libagent/callback/ClassFileLoadHook.cpp new file mode 100644 index 0000000..02a1841 --- /dev/null +++ b/src/libagent/callback/ClassFileLoadHook.cpp @@ -0,0 +1,16 @@ +#include +#include + +void JNICALL ClassFileLoadHook( + jvmtiEnv* jvmti_env, + JNIEnv* jni_env, + jclass class_being_redefined, + jobject loader, + const char* name, + jobject protection_domain, + jint class_data_len, + const unsigned char* classbytes, + jint* new_class_data_len, + unsigned char** new_classbytes) { + std::cout << "ClassFileLoadHook: " << name << std::endl; + } \ No newline at end of file diff --git a/src/libagent/includes/jni_impl.hpp b/src/libagent/includes/jni_impl.hpp index c182a6d..440bbec 100644 --- a/src/libagent/includes/jni_impl.hpp +++ b/src/libagent/includes/jni_impl.hpp @@ -8,3 +8,17 @@ // Generated by JuiceAgent-API #include + +// Event callbacks +// See https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#ClassFileLoadHook +void JNICALL ClassFileLoadHook( + jvmtiEnv* jvmti_env, + JNIEnv* jni_env, + jclass class_being_redefined, + jobject loader, + const char* name, + jobject protection_domain, + jint class_data_len, + const unsigned char* classbytes, + jint* new_class_data_len, + unsigned char** new_classbytes); \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index bab114c..e363f83 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -1,4 +1,5 @@ #include +#include namespace JuiceAgent { // ===== Singleton ===== @@ -45,6 +46,20 @@ namespace JuiceAgent { 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; + } + return true; } } \ No newline at end of file