typedef void (JNICALL *jvmtiEventClassFileLoadHook) //类字节码变换的回调接口
(jvmtiEnv *jvmti_env, //注册的jvmti
JNIEnv* jni_env, //JNI环境
jclass class_being_redefined, //
jobject loader,
const char* name,
jobject protection_domain,
jint class_data_len,
const unsigned char* class_data,
jint* new_class_data_len,
unsigned char** new_class_data);
2、注册回调事件
jint result = jvm->GetEnv((void**) &jvmti, JVMTI_VERSION_1_1);
jvmtiEventCallbacks callbacks;
callbacks.ClassFileLoadHook = &eventHandlerClassFileLoadHook;
(*jvmtienv)->SetEventCallbacks( jvmtienv,
&callbacks,
sizeof(callbacks));
3、启用类字节码加载的hook事件,如果不启用,注册的回调函数将不会被调用的
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
4、启用字节码变换的标记,如果不启用,可以调用回调函数,但是里面进行的字节码变换将不起效
jvmtiCapabilities capabilities;
memset(&capabilities, 0, sizeof(capabilities));
capabilities.can_retransform_classes = 1;
#include
#include
#include
#include
extern "C"
JNICALL void myClassTransform//类字节码变换的回调接口
(jvmtiEnv *jvmti_env, //注册的jvmti
JNIEnv* jni_env, //JNI环境
jclass class_being_redefined, //
jobject loader,
const char* name,
jobject protection_domain,
jint class_data_len,
const unsigned char* class_data,
jint* new_class_data_len,
unsigned char** new_class_data) {
printf("Loading class %s\n", name);
}
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void* reserved) {
jvmtiEnv *jvmti = NULL;
jint error = jvm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_1);
//加载jvmti环境
if(error != JNI_OK) {
printf("Get env fail, errorCode=%d\n", error);
return JNI_ERR;
}
//使用字节码变换能力
jvmtiCapabilities capabilities;
memset(&capabilities, 0, sizeof(capabilities));
capabilities.can_retransform_classes = 1;
capabilities.can_retransform_any_class = 1;
jvmtiError e2 = jvmti->AddCapabilities(&capabilities);
if(e2 != JVMTI_ERROR_NONE) {
printf("AddCapabilities error\n");
return JNI_ERR;
}
//设置回调
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.ClassFileLoadHook = &myClassTransform; //设置类文件变换的文件句柄
e2 = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
if(e2 != JVMTI_ERROR_NONE) {
printf("setcallback error\n");
return JNI_ERR;
}
e2 = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
printf("Register ok\n");
return JNI_OK;
}
6、运行,注意要用g++进行编译,如果用gcc进行编译的,jvmti的方法调用,应该形如(*jvmti)->xxx(jvmti,...)
g++ -fPIC -shared -g -o test.dylib -I$JAVA_HOME/include -I$JAVA_HOME/include/darwin/ test.cpp
javac Test.java
java -agentpath:test.dylib Test