Skip to content

Instantly share code, notes, and snippets.

@brianduff
Created September 29, 2020 08:03
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/d4b8bb4dca29828a95954258b8cabad9 to your computer and use it in GitHub Desktop.
Save brianduff/d4b8bb4dca29828a95954258b8cabad9 to your computer and use it in GitHub Desktop.
// Called when a breakpoint is hit.
void JNICALL BreakpointCallback(jvmtiEnv *jvmti_env, JNIEnv *jni, jthread thread, jmethodID method, jlocation location) {
// Get hold of the parameter. In this example, we just get the first parameter.
// Which parameter is first depends on whether this is a static or instance method.
// For instance methods, the first parameter is a synthetic parameter representing
// the current instance of the class. So here, we get the first "real" parameter.
int parameterPos = 1;
jobject the_parameter;
assert(JVMTI_ERROR_NONE == (*jvmti_env)->GetLocalObject(jvmti_env, thread, 0, parameterPos, &the_parameter));
// TODO: something that displays the parameter... See the full code for details.
// Show a simplified stack trace.
jvmtiFrameInfo frames[8];
jint count;
jvmtiError err;
err = (*jvmti_env)->GetStackTrace(jvmti_env, thread, 0, 8, frames, &count);
if (err == JVMTI_ERROR_NONE && count >= 1) {
char *methodName;
// fout is the output log file
fprintf(fout, " trace: ");
for (int i = 0; i < count; i++) {
err = (*jvmti_env)->GetMethodName(jvmti_env, frames[i].method, &methodName, NULL, NULL);
if (err == JVMTI_ERROR_NONE) {
fprintf(fout, "<- %s", methodName);
}
}
fprintf(fout, "\n");
}
fflush(fout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment