Last active
September 29, 2020 07:55
-
-
Save brianduff/92d58f42ffc957fe014f7c1a8cd4b15d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void JNICALL ClassPrepareCallback(jvmtiEnv *jvmti_env, JNIEnv *jni, jthread thread, jclass klass) { | |
// Avoid re-entrancy, and return early if we already attached. | |
if (in_prepare || attached) { | |
return; | |
} | |
in_prepare = true; | |
jclass clazz = (*jni)->FindClass(jni, classConfig->name); | |
// In case we don't find the class, don't throw an exception | |
(*jni)->ExceptionClear(jni); | |
if (clazz != NULL) { | |
attachMethodBreakpoint(jvmti_env, jni, clazz); | |
(*jni)->DeleteLocalRef(jni, clazz); | |
attached = true; | |
} | |
in_prepare = false; | |
} | |
void attachMethodBreakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni, jclass clazz) { | |
// Look for the method. Here, | |
// methodName is something like "someMethod" | |
// methodSignature is something like "(Lfrodo/Test$RealFile;)Ljava/lang/String;" | |
jmethodID mid = (*jni)->GetMethodID(jni, clazz, methodName, methodSignature); | |
if (mid != NULL) { | |
// Actually set the method breakpoint | |
assert(JVMTI_ERROR_NONE == (*jvmti_env)->SetBreakpoint(jvmti_env, mid, 0)); | |
} else { | |
fprintf(stderr, "mlogagent: Can't find the method\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment