如果想用JVMTI来监控java程序中的某块区域的执行情况,就需要用到watched field中的函数
Set Field Access Watch
jvmtiError
SetFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field)
用这个函数可以设置开始的监测点
当程序执行到设置了监测点的区域时候就自动调用生成的
void JNICALL
FieldAccess(jvmtiEnv *jvmti_env,
JNIEnv* jni_env,
jthread thread,
jmethodID method,
jlocation location,
jclass field_klass,
jobject object,
jfieldID field)
函数,直到监测点被清除,使用函数
jvmtiError
ClearFieldAccessWatch(jvmtiEnv* env,jclass klass,jfieldID field)
不过需要向虚拟机注册功能:
jvmtiCapabilities capa = {0};
capa.can_generate_field_access_events=1;
err = g_jvmti->SetEventNotificationMode(JVMTI_ENABLE,JVMTI_EVENT_FIELD_ACCESS,NULL);
jvmtiEventCallbacks callbacks = {0};
callbacks.FieldAccess = &actionFieldAccess;
其中 actionFieldAccess是一个函数指针,我们可以在函数中执行我们想要的检测
void JNICALL actionFieldAccess(jvmtiEnv *jvmti_env,JNIEnv* jni_env, jthread thread,jmethodID method,
jlocation location,jclass field_klass,jobject object,jfieldID field)
{
// do the check
}