Skip to content

Instantly share code, notes, and snippets.

@brianduff
Last active September 29, 2020 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianduff/92d58f42ffc957fe014f7c1a8cd4b15d to your computer and use it in GitHub Desktop.
Save brianduff/92d58f42ffc957fe014f7c1a8cd4b15d to your computer and use it in GitHub Desktop.
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