代理库中未调用JVMTI事件

时间:2019-04-16 18:33:43

标签: java jvmti

我正在尝试创建一个jvmti代理,该代理通知是否在Java应用程序中触发了以下事件 1)发生对象创建 2)发生异常

当我使用代理库创建和运行应用程序时,仅调用Agent_OnLoad,Agent_OnUnload,callbackVMDeath。其他事件不会触发。

以下是我的应用程序文件和代理文件的链接: https://drive.google.com/file/d/1_O8VU4UZE5WcIGhcsTCNE8lifZXChjZs/view?usp=sharing https://drive.google.com/file/d/1Fs5nSgE-v7qAQkGBnqcidtynhEFNgNDb/view?usp=sharing

亲爱的朋友,请在这里帮助我。作为JVMTI的新手,我不确定为什么不调用这些事件。

1 个答案:

答案 0 :(得分:0)

我认为答案可能为时已晚... 您添加了capabilities吗?您的代码。

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
        printf("Load called\n");

        static jvmtiEnv *jvmti = NULL;
        static jvmtiCapabilities capa;
        static  jvmtiEventCallbacks callbacks;

        static GlobalAgentData data;
        (void)memset((void*)&data, 0, sizeof(data));

        /*get environment */
                (*jvm)->GetEnv(jvm, (void**)&jvmti, JVMTI_VERSION_1_0);

        jvmtiError error;
        (void)memset(&capa, 0, sizeof(jvmtiCapabilities));
        capa.can_signal_thread = 1;
        capa.can_get_owned_monitor_info = 1;
        capa.can_generate_method_entry_events = 1;
        capa.can_generate_exception_events = 1;
        capa.can_generate_vm_object_alloc_events = 1;
        capa.can_tag_objects = 1;


        (jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, (jthread)NULL);
        error = (*jvmti)->SetEventNotificationMode
      (jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, (jthread)NULL);
        error = (*jvmti)->SetEventNotificationMode
      (jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, (jthread)NULL);
      error = (*jvmti)->SetEventNotificationMode
      (jvmti, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, (jthread)NULL);

        /* We return JNI_OK to signify success */


        callbacks.VMInit = &callbackVMInit; /* JVMTI_EVENT_VM_INIT */
        callbacks.VMDeath = &callbackVMDeath; /* JVMTI_EVENT_VM_DEATH */
        //callbacks.Exception = &callbackException;/* JVMTI_EVENT_EXCEPTION */
        callbacks.VMObjectAlloc = &callbackVMObjectAlloc;/* JVMTI_EVENT_VM_OBJECT_ALLOC */

        error = (*jvmti)->SetEventCallbacks(jvmti, &callbacks,(jint)sizeof(callbacks));


        return JNI_OK;
}

尽管打开了异常事件和vm_object_alloc事件的功能。但是您不会呼叫(*jvmti)->AddCapabilities(&capa)。您应该调用此方法以启用功能。