src/java.instrument/share/native/libinstrument/JPLISAgent.c
changeset 47777 d85284ccd1bd
parent 47651 148b73047771
child 54187 f554d3e88da3
--- a/src/java.instrument/share/native/libinstrument/JPLISAgent.c	Fri Nov 03 10:48:26 2017 -0700
+++ b/src/java.instrument/share/native/libinstrument/JPLISAgent.c	Fri Nov 03 17:09:25 2017 -0700
@@ -404,8 +404,8 @@
 
 
     /*
-     *  Then turn off the VMInit handler and turn on the ClassFileLoadHook.
-     *  This way it is on before anyone registers a transformer.
+     *  Register a handler for ClassFileLoadHook (without enabling this event).
+     *  Turn off the VMInit handler.
      */
     if ( result ) {
         result = setLivePhaseEventHandlers(agent);
@@ -649,17 +649,6 @@
         jplis_assert(jvmtierror == JVMTI_ERROR_NONE);
     }
 
-    if ( jvmtierror == JVMTI_ERROR_NONE ) {
-        /* turn on ClassFileLoadHook */
-        jvmtierror = (*jvmtienv)->SetEventNotificationMode(
-                                                    jvmtienv,
-                                                    JVMTI_ENABLE,
-                                                    JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
-                                                    NULL /* all threads */);
-        check_phase_ret_false(jvmtierror);
-        jplis_assert(jvmtierror == JVMTI_ERROR_NONE);
-    }
-
     return (jvmtierror == JVMTI_ERROR_NONE);
 }
 
@@ -1097,6 +1086,21 @@
 }
 
 void
+setHasTransformers(JNIEnv * jnienv, JPLISAgent * agent, jboolean has) {
+    jvmtiEnv *          jvmtienv = jvmti(agent);
+    jvmtiError          jvmtierror;
+
+    jplis_assert(jvmtienv != NULL);
+    jvmtierror = (*jvmtienv)->SetEventNotificationMode(
+                                            jvmtienv,
+                                            has? JVMTI_ENABLE : JVMTI_DISABLE,
+                                            JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
+                                            NULL /* all threads */);
+    check_phase_ret(jvmtierror);
+    jplis_assert(jvmtierror == JVMTI_ERROR_NONE);
+}
+
+void
 setHasRetransformableTransformers(JNIEnv * jnienv, JPLISAgent * agent, jboolean has) {
     jvmtiEnv *          retransformerEnv     = retransformableEnvironment(agent);
     jvmtiError          jvmtierror;
@@ -1107,6 +1111,7 @@
                                                     has? JVMTI_ENABLE : JVMTI_DISABLE,
                                                     JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
                                                     NULL /* all threads */);
+    check_phase_ret(jvmtierror);
     jplis_assert(jvmtierror == JVMTI_ERROR_NONE);
 }
 
@@ -1185,6 +1190,10 @@
         deallocate(retransformerEnv, (void*)classArray);
     }
 
+    /* Return back if we executed the JVMTI API in a wrong phase
+     */
+    check_phase_ret(errorCode);
+
     if (errorCode != JVMTI_ERROR_NONE) {
         createAndThrowThrowableFromJVMTIErrorCode(jnienv, errorCode);
     }