Created
September 29, 2020 07:14
-
-
Save brianduff/2fd9d116815d02f4158fd40c8ec9d1d4 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
JNIEXPORT jint JNICALL | |
Agent_OnLoad(JavaVM *vm, char *options, void *reserved) { | |
char *option = strtok(options, ","); | |
// ... | |
// Load the config file using options from `option`. Also parse out an option for | |
// a log file to write to. | |
// .. | |
fprintf(stderr, "mlogagent: Loaded agent\n"); | |
// Get a JVMTI env object, which is used to make function calls into JVMTI | |
jvmtiEnv *env; | |
assert(JVMTI_ERROR_NONE == (*vm)->GetEnv(vm, (void **)&env, JVMTI_VERSION)); | |
// Let JVMTI know we are going to be registering breakpoints and accessing local | |
// variables. If these capabilities are not supported by the JVM, then the AddCapabilities | |
// function will return an error code, and our agent will terminate the VM. | |
jvmtiCapabilities capabilities = { 0 }; | |
capabilities.can_generate_breakpoint_events = 1; | |
capabilities.can_access_local_variables = 1; | |
assert(JVMTI_ERROR_NONE == (*env)->AddCapabilities(env, &capabilities)); | |
// Register callbacks for ClassPrepare and Breakpoint events. We'll dig into these callback | |
// functions soon. | |
jvmtiEventCallbacks callbacks = { 0 }; | |
callbacks.ClassPrepare = &ClassPrepareCallback; | |
callbacks.Breakpoint = &BreakpointCallback; | |
assert(JVMTI_ERROR_NONE == (*env)->SetEventCallbacks(env, &callbacks, sizeof(callbacks))); | |
// Tell JVMTI to enable breakpoint and class prepare events so our callbacks will be invoked. | |
assert(JVMTI_ERROR_NONE == (*env)->SetEventNotificationMode(env, | |
JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)); | |
assert(JVMTI_ERROR_NONE == (*env)->SetEventNotificationMode(env, | |
JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL)); | |
return JNI_OK; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment