test/hotspot/jtreg/vmTestbase/nsk/jvmti/ThreadStart/threadstart002/threadstart002.cpp
changeset 52215 0b0ba3a2fec9
parent 51774 79dc492c00ab
child 52221 27ba7cc31f9f
equal deleted inserted replaced
52214:b3c7c5a62521 52215:0b0ba3a2fec9
   117     jobject temp;
   117     jobject temp;
   118 
   118 
   119     /* Notify VMInit callback as well as ThreadStart callback (if any)
   119     /* Notify VMInit callback as well as ThreadStart callback (if any)
   120      * that agent thread has been started
   120      * that agent thread has been started
   121      */
   121      */
   122     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, agent_start_lock))) {
   122     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(agent_start_lock))) {
   123         result = STATUS_FAILED;
   123         result = STATUS_FAILED;
   124         NSK_COMPLAIN0("[agent] failed to acquire agent_start_lock\n");
   124         NSK_COMPLAIN0("[agent] failed to acquire agent_start_lock\n");
   125     }
   125     }
   126 
   126 
   127     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorNotifyAll, jvmti, agent_start_lock))) {
   127     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorNotifyAll(agent_start_lock))) {
   128         result = STATUS_FAILED;
   128         result = STATUS_FAILED;
   129         NSK_COMPLAIN0("[agent] failed to notify about agent_start_lock\n");
   129         NSK_COMPLAIN0("[agent] failed to notify about agent_start_lock\n");
   130     }
   130     }
   131 
   131 
   132     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, agent_start_lock))) {
   132     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(agent_start_lock))) {
   133         result = STATUS_FAILED;
   133         result = STATUS_FAILED;
   134         NSK_COMPLAIN0("[agent] failed to release agent_start_lock\n");
   134         NSK_COMPLAIN0("[agent] failed to release agent_start_lock\n");
   135     }
   135     }
   136 
   136 
   137     NSK_DISPLAY0(">>> [agent] agent created\n");
   137     NSK_DISPLAY0(">>> [agent] agent created\n");
   138 
   138 
   139     debug_agent_started = JNI_TRUE;
   139     debug_agent_started = JNI_TRUE;
   140 
   140 
   141     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, thr_start_lock))) {
   141     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(thr_start_lock))) {
   142         result = STATUS_FAILED;
   142         result = STATUS_FAILED;
   143         NSK_COMPLAIN0("[agent] failed to enter thr_start_lock\n");
   143         NSK_COMPLAIN0("[agent] failed to enter thr_start_lock\n");
   144     }
   144     }
   145 
   145 
   146     while (terminate_debug_agent != JNI_TRUE) {
   146     while (terminate_debug_agent != JNI_TRUE) {
   147 
   147 
   148         if (next_thread == NULL ) {
   148         if (next_thread == NULL ) {
   149             /* wait till new thread will be created and started */
   149             /* wait till new thread will be created and started */
   150             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(RawMonitorWait, jvmti, thr_start_lock, (jlong)0))) {
   150             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorWait(thr_start_lock, (jlong)0))) {
   151                 result = STATUS_FAILED;
   151                 result = STATUS_FAILED;
   152                 NSK_COMPLAIN0("[agent] Failed while waiting thr_start_lock\n");
   152                 NSK_COMPLAIN0("[agent] Failed while waiting thr_start_lock\n");
   153             }
   153             }
   154         }
   154         }
   155 
   155 
   156         if (next_thread != NULL) {
   156         if (next_thread != NULL) {
   157             /* hmm, why NewGlobalRef is called one more time???
   157             /* hmm, why NewGlobalRef is called one more time???
   158              * next_thread = NSK_CPP_STUB2(NewGlobalRef, env, next_thread);
   158              * next_thread = env->NewGlobalRef(next_thread);
   159              */
   159              */
   160             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(SuspendThread, jvmti, next_thread))) {
   160             if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(next_thread))) {
   161                 result = STATUS_FAILED;
   161                 result = STATUS_FAILED;
   162                 NSK_COMPLAIN1("[agent] Failed to suspend thread#%d\n", eventsCount);
   162                 NSK_COMPLAIN1("[agent] Failed to suspend thread#%d\n", eventsCount);
   163             }
   163             }
   164 
   164 
   165             NSK_DISPLAY2(">>> [agent] thread#%d %s suspended ...\n", eventsCount, inf.name);
   165             NSK_DISPLAY2(">>> [agent] thread#%d %s suspended ...\n", eventsCount, inf.name);
   166 
   166 
   167             /* these dummy calls provoke VM to hang */
   167             /* these dummy calls provoke VM to hang */
   168             temp = NSK_CPP_STUB2(NewGlobalRef, env, next_thread);
   168             temp = env->NewGlobalRef(next_thread);
   169             NSK_CPP_STUB2(DeleteGlobalRef, env, temp);
   169             env->DeleteGlobalRef(temp);
   170 
   170 
   171             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(ResumeThread, jvmti, next_thread))) {
   171             if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(next_thread))) {
   172                 result = STATUS_FAILED;
   172                 result = STATUS_FAILED;
   173                 NSK_COMPLAIN1("[agent] Failed to resume thread#%d\n", eventsCount);
   173                 NSK_COMPLAIN1("[agent] Failed to resume thread#%d\n", eventsCount);
   174             }
   174             }
   175 
   175 
   176             NSK_DISPLAY2(">>> [agent] thread#%d %s resumed ...\n", eventsCount, inf.name);
   176             NSK_DISPLAY2(">>> [agent] thread#%d %s resumed ...\n", eventsCount, inf.name);
   177 
   177 
   178             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadState, jvmti, next_thread, &thrStat))) {
   178             if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(next_thread, &thrStat))) {
   179                 result = STATUS_FAILED;
   179                 result = STATUS_FAILED;
   180                 NSK_COMPLAIN1("[agent] Failed to get thread state for thread#%d\n", eventsCount);
   180                 NSK_COMPLAIN1("[agent] Failed to get thread state for thread#%d\n", eventsCount);
   181             }
   181             }
   182 
   182 
   183             NSK_DISPLAY3(">>> [agent] %s threadState=%s (%x)\n",
   183             NSK_DISPLAY3(">>> [agent] %s threadState=%s (%x)\n",
   184                     inf.name, TranslateState(thrStat), thrStat);
   184                     inf.name, TranslateState(thrStat), thrStat);
   185 
   185 
   186             if (thrStat & JVMTI_THREAD_STATE_SUSPENDED) {
   186             if (thrStat & JVMTI_THREAD_STATE_SUSPENDED) {
   187                 NSK_COMPLAIN1("[agent] \"%s\" was not resumed\n", inf.name);
   187                 NSK_COMPLAIN1("[agent] \"%s\" was not resumed\n", inf.name);
   188                 NSK_CPP_STUB2(FatalError, env, "[agent] could not recover");
   188                 env->FatalError("[agent] could not recover");
   189             }
   189             }
   190 
   190 
   191             NSK_CPP_STUB2(DeleteGlobalRef, env, next_thread);
   191             env->DeleteGlobalRef(next_thread);
   192             next_thread = NULL;
   192             next_thread = NULL;
   193 
   193 
   194             /* Notify ThreadStart callback that thread has been resumed */
   194             /* Notify ThreadStart callback that thread has been resumed */
   195             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, thr_resume_lock))) {
   195             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(thr_resume_lock))) {
   196                 NSK_COMPLAIN0("[agent] Failed to acquire thr_resume_lock\n");
   196                 NSK_COMPLAIN0("[agent] Failed to acquire thr_resume_lock\n");
   197                 result = STATUS_FAILED;
   197                 result = STATUS_FAILED;
   198             }
   198             }
   199 
   199 
   200             debug_agent_timed_out = JNI_FALSE;
   200             debug_agent_timed_out = JNI_FALSE;
   201 
   201 
   202             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorNotify, jvmti, thr_resume_lock))) {
   202             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorNotify(thr_resume_lock))) {
   203                 NSK_COMPLAIN0("[agent] Failed to notifing about thr_resume_lock\n");
   203                 NSK_COMPLAIN0("[agent] Failed to notifing about thr_resume_lock\n");
   204                 result = STATUS_FAILED;
   204                 result = STATUS_FAILED;
   205             }
   205             }
   206 
   206 
   207             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, thr_resume_lock))) {
   207             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(thr_resume_lock))) {
   208                 NSK_COMPLAIN0("[agent] Failed to release thr_resume_lock\n");
   208                 NSK_COMPLAIN0("[agent] Failed to release thr_resume_lock\n");
   209                 result = STATUS_FAILED;
   209                 result = STATUS_FAILED;
   210             }
   210             }
   211         }
   211         }
   212     }
   212     }
   213 
   213 
   214     /*
   214     /*
   215      * We don't call RawMonitorExit(thr_start_lock) in the loop so we don't
   215      * We don't call RawMonitorExit(thr_start_lock) in the loop so we don't
   216      * lose any notify calls.
   216      * lose any notify calls.
   217      */
   217      */
   218     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, thr_start_lock))) {
   218     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(thr_start_lock))) {
   219         NSK_COMPLAIN0("[agent] Failed to release thr_start_lock\n");
   219         NSK_COMPLAIN0("[agent] Failed to release thr_start_lock\n");
   220         result = STATUS_FAILED;
   220         result = STATUS_FAILED;
   221     }
   221     }
   222 
   222 
   223     NSK_DISPLAY0(">>> [agent] done.\n");
   223     NSK_DISPLAY0(">>> [agent] done.\n");
   228     jvmtiPhase phase;
   228     jvmtiPhase phase;
   229 
   229 
   230     NSK_DISPLAY0(">>> [ThreadStart hook] start\n");
   230     NSK_DISPLAY0(">>> [ThreadStart hook] start\n");
   231 
   231 
   232     /* skip if thread is 'agent thread' */
   232     /* skip if thread is 'agent thread' */
   233     if (NSK_CPP_STUB3(IsSameObject, env, agent_thread, thread) == JNI_TRUE) {
   233     if (env->IsSameObject(agent_thread, thread) == JNI_TRUE) {
   234         NSK_DISPLAY0(">>> [ThreadStart hook] skip agent thread\n");
   234         NSK_DISPLAY0(">>> [ThreadStart hook] skip agent thread\n");
   235         NSK_DISPLAY0(">>> [ThreadStart hook] end\n");
   235         NSK_DISPLAY0(">>> [ThreadStart hook] end\n");
   236         return;
   236         return;
   237     }
   237     }
   238 
   238 
   239     /* wait till agent thread is started
   239     /* wait till agent thread is started
   240      * (otherwise can fail while waiting on thr_resume_thread due to timeout)
   240      * (otherwise can fail while waiting on thr_resume_thread due to timeout)
   241      */
   241      */
   242     if (debug_agent_started != JNI_TRUE) {
   242     if (debug_agent_started != JNI_TRUE) {
   243         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti_env, agent_start_lock))) {
   243         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(agent_start_lock))) {
   244             NSK_COMPLAIN0("[ThreadStart hook] Failed to acquire agent_start_lock\n");
   244             NSK_COMPLAIN0("[ThreadStart hook] Failed to acquire agent_start_lock\n");
   245             result = STATUS_FAILED;
   245             result = STATUS_FAILED;
   246         }
   246         }
   247 
   247 
   248         while (debug_agent_started != JNI_TRUE) {
   248         while (debug_agent_started != JNI_TRUE) {
   249             NSK_DISPLAY1(">>> [ThreadStart hook] waiting %dms for agent thread to start\n", WAIT_TIME);
   249             NSK_DISPLAY1(">>> [ThreadStart hook] waiting %dms for agent thread to start\n", WAIT_TIME);
   250 
   250 
   251             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(RawMonitorWait,
   251             if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorWait(agent_start_lock, (jlong)WAIT_TIME))) {
   252                     jvmti_env, agent_start_lock, (jlong)WAIT_TIME))) {
       
   253                 NSK_COMPLAIN0("[ThreadStart hook] Failed to wait for agent_start_lock\n");
   252                 NSK_COMPLAIN0("[ThreadStart hook] Failed to wait for agent_start_lock\n");
   254                 result = STATUS_FAILED;
   253                 result = STATUS_FAILED;
   255             }
   254             }
   256         }
   255         }
   257 
   256 
   258         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti_env, agent_start_lock))) {
   257         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(agent_start_lock))) {
   259             NSK_COMPLAIN0("[ThreadStart hook] Failed to release agent_start_lock\n");
   258             NSK_COMPLAIN0("[ThreadStart hook] Failed to release agent_start_lock\n");
   260             result = STATUS_FAILED;
   259             result = STATUS_FAILED;
   261         }
   260         }
   262     }
   261     }
   263 
   262 
   264 
   263 
   265     /* get JVMTI phase */
   264     /* get JVMTI phase */
   266     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase, jvmti_env, &phase))) {
   265     if (!NSK_JVMTI_VERIFY(jvmti_env->GetPhase(&phase))) {
   267         NSK_COMPLAIN0("[ThreadStart hook] Failed to get JVMTI phase\n");
   266         NSK_COMPLAIN0("[ThreadStart hook] Failed to get JVMTI phase\n");
   268         result = STATUS_FAILED;
   267         result = STATUS_FAILED;
   269     }
   268     }
   270 
   269 
   271     /* Acquire event lock,
   270     /* Acquire event lock,
   272      * so only one StartThread callback could be proceeded at the time
   271      * so only one StartThread callback could be proceeded at the time
   273      */
   272      */
   274     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti_env, thr_event_lock))) {
   273     if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(thr_event_lock))) {
   275         NSK_COMPLAIN0("[ThreadStart hook] Failed to acquire thr_event_lock\n");
   274         NSK_COMPLAIN0("[ThreadStart hook] Failed to acquire thr_event_lock\n");
   276         result = STATUS_FAILED;
   275         result = STATUS_FAILED;
   277     }
   276     }
   278 
   277 
   279     {
   278     {
   280         /* Get thread name */
   279         /* Get thread name */
   281         inf.name = (char*) "UNKNOWN";
   280         inf.name = (char*) "UNKNOWN";
   282         if (phase == JVMTI_PHASE_LIVE) {
   281         if (phase == JVMTI_PHASE_LIVE) {
   283             /* GetThreadInfo may only be called during the live phase */
   282             /* GetThreadInfo may only be called during the live phase */
   284             if (!NSK_JVMTI_VERIFY(
   283             if (!NSK_JVMTI_VERIFY(jvmti_env->GetThreadInfo(thread, &inf))) {
   285                     NSK_CPP_STUB3(GetThreadInfo, jvmti_env, thread, &inf))) {
       
   286                 NSK_COMPLAIN1("[ThreadStart hook] Failed to get thread infor for thread#%d\n", eventsCount);
   284                 NSK_COMPLAIN1("[ThreadStart hook] Failed to get thread infor for thread#%d\n", eventsCount);
   287                 result = STATUS_FAILED;
   285                 result = STATUS_FAILED;
   288             }
   286             }
   289         }
   287         }
   290 
   288 
   291         NSK_DISPLAY2(">>> [ThreadStart hook] thread#%d: %s\n", eventsCount, inf.name);
   289         NSK_DISPLAY2(">>> [ThreadStart hook] thread#%d: %s\n", eventsCount, inf.name);
   292 
   290 
   293         /* Acquire thr_start_lock */
   291         /* Acquire thr_start_lock */
   294         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti_env, thr_start_lock))) {
   292         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(thr_start_lock))) {
   295             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to acquire thr_start_lock\n", eventsCount);
   293             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to acquire thr_start_lock\n", eventsCount);
   296             result = STATUS_FAILED;
   294             result = STATUS_FAILED;
   297         }
   295         }
   298 
   296 
   299             /* Acquire thr_resume_lock before we release thr_start_lock to prevent
   297             /* Acquire thr_resume_lock before we release thr_start_lock to prevent
   300              * debug agent from notifying us before we are ready.
   298              * debug agent from notifying us before we are ready.
   301          */
   299          */
   302         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti_env, thr_resume_lock))) {
   300         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(thr_resume_lock))) {
   303             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to acquire thr_resume_lock\n", eventsCount);
   301             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to acquire thr_resume_lock\n", eventsCount);
   304             result = STATUS_FAILED;
   302             result = STATUS_FAILED;
   305         }
   303         }
   306 
   304 
   307         /* Store thread */
   305         /* Store thread */
   308         next_thread = NSK_CPP_STUB2(NewGlobalRef, env, thread);
   306         next_thread = env->NewGlobalRef(thread);
   309         debug_agent_timed_out = JNI_TRUE;
   307         debug_agent_timed_out = JNI_TRUE;
   310 
   308 
   311         /* Notify agent thread about new started thread and let agent thread to work with it */
   309         /* Notify agent thread about new started thread and let agent thread to work with it */
   312         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorNotify, jvmti_env, thr_start_lock))) {
   310         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorNotify(thr_start_lock))) {
   313             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to notify about thr_start_lock\n", eventsCount);
   311             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to notify about thr_start_lock\n", eventsCount);
   314             result = STATUS_FAILED;
   312             result = STATUS_FAILED;
   315         }
   313         }
   316 
   314 
   317         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti_env, thr_start_lock))) {
   315         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(thr_start_lock))) {
   318             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to release thr_start_lock\n", eventsCount);
   316             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to release thr_start_lock\n", eventsCount);
   319             result = STATUS_FAILED;
   317             result = STATUS_FAILED;
   320         }
   318         }
   321 
   319 
   322         /* Wait till this started thread will be resumed by agent thread */
   320         /* Wait till this started thread will be resumed by agent thread */
   323         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(RawMonitorWait,
   321         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorWait(thr_resume_lock, (jlong)WAIT_TIME ))) {
   324                 jvmti_env, thr_resume_lock, (jlong)WAIT_TIME ))) {
       
   325             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed while waiting for thr_resume_lock\n", eventsCount);
   322             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed while waiting for thr_resume_lock\n", eventsCount);
   326             result = STATUS_FAILED;
   323             result = STATUS_FAILED;
   327         }
   324         }
   328 
   325 
   329         if (debug_agent_timed_out == JNI_TRUE) {
   326         if (debug_agent_timed_out == JNI_TRUE) {
   330             NSK_COMPLAIN1("[ThreadStart hook] \"%s\": debug agent timed out\n", inf.name);
   327             NSK_COMPLAIN1("[ThreadStart hook] \"%s\": debug agent timed out\n", inf.name);
   331             NSK_CPP_STUB2(FatalError, env, "[ThreadStart hook] could not recover");
   328             env->FatalError("[ThreadStart hook] could not recover");
   332         }
   329         }
   333 
   330 
   334         /* Release thr_resume_lock lock */
   331         /* Release thr_resume_lock lock */
   335         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti_env, thr_resume_lock))) {
   332         if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(thr_resume_lock))) {
   336             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to release thr_resume_lock\n", eventsCount);
   333             NSK_COMPLAIN1("[ThreadStart hook] thread#%d failed to release thr_resume_lock\n", eventsCount);
   337             result = STATUS_FAILED;
   334             result = STATUS_FAILED;
   338         }
   335         }
   339 
   336 
   340         /* check that thread is not in SUSPENDED state */
   337         /* check that thread is not in SUSPENDED state */
   341         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadState, jvmti_env, thread, &thrStat))) {
   338         if (!NSK_JVMTI_VERIFY(jvmti_env->GetThreadState(thread, &thrStat))) {
   342             NSK_COMPLAIN1("[ThreadStart hook] Failed to get thread state for thread#%d\n", eventsCount);
   339             NSK_COMPLAIN1("[ThreadStart hook] Failed to get thread state for thread#%d\n", eventsCount);
   343             result = STATUS_FAILED;
   340             result = STATUS_FAILED;
   344         }
   341         }
   345 
   342 
   346         NSK_DISPLAY2(">>> [ThreadStart hook] threadState=%s (%x)\n",
   343         NSK_DISPLAY2(">>> [ThreadStart hook] threadState=%s (%x)\n",
   347                 TranslateState(thrStat), thrStat);
   344                 TranslateState(thrStat), thrStat);
   348 
   345 
   349         if (thrStat & JVMTI_THREAD_STATE_SUSPENDED) {
   346         if (thrStat & JVMTI_THREAD_STATE_SUSPENDED) {
   350             NSK_COMPLAIN1("[ThreadStart hook] \"%s\" was self-suspended\n", inf.name);
   347             NSK_COMPLAIN1("[ThreadStart hook] \"%s\" was self-suspended\n", inf.name);
   351             NSK_CPP_STUB2(FatalError, env, "[ThreadStart hook] could not recover");
   348             env->FatalError("[ThreadStart hook] could not recover");
   352         }
   349         }
   353 
   350 
   354         eventsCount++;
   351         eventsCount++;
   355     }
   352     }
   356 
   353 
   357     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti_env, thr_event_lock))) {
   354     if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(thr_event_lock))) {
   358         NSK_COMPLAIN0("[ThreadStart hook] Failed to release thr_event_lock\n");
   355         NSK_COMPLAIN0("[ThreadStart hook] Failed to release thr_event_lock\n");
   359         result = STATUS_FAILED;
   356         result = STATUS_FAILED;
   360     }
   357     }
   361 
   358 
   362     NSK_DISPLAY0(">>> [ThreadStart hook] end\n");
   359     NSK_DISPLAY0(">>> [ThreadStart hook] end\n");
   366     jclass cls = NULL;
   363     jclass cls = NULL;
   367     jmethodID mid = NULL;
   364     jmethodID mid = NULL;
   368 
   365 
   369     NSK_DISPLAY0(">>> VMInit event: start\n");
   366     NSK_DISPLAY0(">>> VMInit event: start\n");
   370 
   367 
   371     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
   368     if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL))) {
   372             jvmti_env, JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL))) {
       
   373         NSK_COMPLAIN0("TEST FAILED: failed to enable JVMTI_EVENT_THREAD_START\n");
   369         NSK_COMPLAIN0("TEST FAILED: failed to enable JVMTI_EVENT_THREAD_START\n");
   374         return;
   370         return;
   375     }
   371     }
   376 
   372 
   377     /* Start agent thread */
   373     /* Start agent thread */
   378     if (!NSK_VERIFY((cls =
   374     if (!NSK_VERIFY((cls = env->FindClass("java/lang/Thread")) != NULL)) {
   379             NSK_CPP_STUB2(FindClass, env, "java/lang/Thread")) != NULL)) {
       
   380         result = STATUS_FAILED;
   375         result = STATUS_FAILED;
   381             NSK_COMPLAIN0("TEST FAILED: Cannot start agent thread: FindClass() failed\n");
   376             NSK_COMPLAIN0("TEST FAILED: Cannot start agent thread: FindClass() failed\n");
   382         return;
   377         return;
   383     }
   378     }
   384 
   379 
   385 
   380 
   386     if (!NSK_VERIFY((mid =
   381     if (!NSK_VERIFY((mid = env->GetMethodID(cls, "<init>", "()V")) != NULL)) {
   387             NSK_CPP_STUB4(GetMethodID, env, cls, "<init>", "()V")) != NULL)) {
       
   388         result = STATUS_FAILED;
   382         result = STATUS_FAILED;
   389             NSK_COMPLAIN0("TEST FAILED: Cannot start agent thread: GetMethodID() failed\n");
   383             NSK_COMPLAIN0("TEST FAILED: Cannot start agent thread: GetMethodID() failed\n");
   390         return;
   384         return;
   391     }
   385     }
   392 
   386 
   393 
   387 
   394     if (!NSK_VERIFY((agent_thread =
   388     if (!NSK_VERIFY((agent_thread = env->NewObject(cls, mid)) != NULL)) {
   395             NSK_CPP_STUB3(NewObject, env, cls, mid)) != NULL)) {
       
   396         result = STATUS_FAILED;
   389         result = STATUS_FAILED;
   397             NSK_COMPLAIN0("Cannot start agent thread: NewObject() failed\n");
   390             NSK_COMPLAIN0("Cannot start agent thread: NewObject() failed\n");
   398         return;
   391         return;
   399     }
   392     }
   400 
   393 
   401     agent_thread = (jthread) NSK_CPP_STUB2(NewGlobalRef, env, agent_thread);
   394     agent_thread = (jthread) env->NewGlobalRef(agent_thread);
   402     if (agent_thread == NULL) {
   395     if (agent_thread == NULL) {
   403         result = STATUS_FAILED;
   396         result = STATUS_FAILED;
   404         NSK_COMPLAIN0("Cannot create global reference for agent_thread\n");
   397         NSK_COMPLAIN0("Cannot create global reference for agent_thread\n");
   405         return;
   398         return;
   406     }
   399     }
   408     /*
   401     /*
   409      * Grab agent_start_lock before launching debug_agent to prevent
   402      * Grab agent_start_lock before launching debug_agent to prevent
   410      * debug_agent from notifying us before we are ready.
   403      * debug_agent from notifying us before we are ready.
   411      */
   404      */
   412 
   405 
   413     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti_env, agent_start_lock))) {
   406     if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(agent_start_lock))) {
   414         result = STATUS_FAILED;
   407         result = STATUS_FAILED;
   415         NSK_COMPLAIN0("TEST FAILED: failed to enter agent_start_lock\n");
   408         NSK_COMPLAIN0("TEST FAILED: failed to enter agent_start_lock\n");
   416     }
   409     }
   417 
   410 
   418     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(RunAgentThread,
   411     if (!NSK_JVMTI_VERIFY(jvmti_env->RunAgentThread(agent_thread, debug_agent, NULL, JVMTI_THREAD_NORM_PRIORITY))) {
   419             jvmti_env, agent_thread, debug_agent, NULL, JVMTI_THREAD_NORM_PRIORITY))) {
       
   420         result = STATUS_FAILED;
   412         result = STATUS_FAILED;
   421         NSK_COMPLAIN0("TEST FAILED: failed to create agent thread\n");
   413         NSK_COMPLAIN0("TEST FAILED: failed to create agent thread\n");
   422     }
   414     }
   423 
   415 
   424     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(RawMonitorWait, jvmti_env, agent_start_lock, (jlong)0))) {
   416     if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorWait(agent_start_lock, (jlong)0))) {
   425         result = STATUS_FAILED;
   417         result = STATUS_FAILED;
   426         NSK_COMPLAIN0("TEST FAILED: failed to wait agent_start_lock\n");
   418         NSK_COMPLAIN0("TEST FAILED: failed to wait agent_start_lock\n");
   427     }
   419     }
   428 
   420 
   429     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti_env, agent_start_lock))) {
   421     if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(agent_start_lock))) {
   430         result = STATUS_FAILED;
   422         result = STATUS_FAILED;
   431         NSK_COMPLAIN0("TEST FAILED: failed to exit agent_start_lock\n");
   423         NSK_COMPLAIN0("TEST FAILED: failed to exit agent_start_lock\n");
   432     }
   424     }
   433 
   425 
   434     NSK_DISPLAY0(">>> VMInit event: end\n");
   426     NSK_DISPLAY0(">>> VMInit event: end\n");
   462             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) {
   454             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) {
   463         NSK_COMPLAIN0("TEST FAILED: failed to create JVMTIEnv\n");
   455         NSK_COMPLAIN0("TEST FAILED: failed to create JVMTIEnv\n");
   464         return JNI_ERR;
   456         return JNI_ERR;
   465     }
   457     }
   466 
   458 
   467     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPotentialCapabilities, jvmti, &caps))) {
   459     if (!NSK_JVMTI_VERIFY(jvmti->GetPotentialCapabilities(&caps))) {
   468         NSK_COMPLAIN0("TEST FAILED: failed to get potential capabilities\n");
   460         NSK_COMPLAIN0("TEST FAILED: failed to get potential capabilities\n");
   469         return JNI_ERR;
   461         return JNI_ERR;
   470     }
   462     }
   471 
   463 
   472     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
   464     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
   473         NSK_COMPLAIN0("TEST FAILED: failed to add capabilities during agent load\n");
   465         NSK_COMPLAIN0("TEST FAILED: failed to add capabilities during agent load\n");
   474         return JNI_ERR;
   466         return JNI_ERR;
   475     }
   467     }
   476 
   468 
   477     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities, jvmti, &caps))) {
   469     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps))) {
   478         NSK_COMPLAIN0("TEST FAILED: failed to get capabilities\n");
   470         NSK_COMPLAIN0("TEST FAILED: failed to get capabilities\n");
   479         return JNI_ERR;
   471         return JNI_ERR;
   480     }
   472     }
   481 
   473 
   482     if (!caps.can_suspend) {
   474     if (!caps.can_suspend) {
   483         NSK_DISPLAY0("WARNING: suspend/resume is not implemented\n");
   475         NSK_DISPLAY0("WARNING: suspend/resume is not implemented\n");
   484     }
   476     }
   485 
   477 
   486     /* create raw monitors */
   478     /* create raw monitors */
   487     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_agent_start_lock", &agent_start_lock))) {
   479     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_agent_start_lock", &agent_start_lock))) {
   488         NSK_COMPLAIN0("TEST FAILED: failed to create agent_start_lock\n");
   480         NSK_COMPLAIN0("TEST FAILED: failed to create agent_start_lock\n");
   489         return JNI_ERR;
   481         return JNI_ERR;
   490     }
   482     }
   491 
   483 
   492     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_thr_event_lock", &thr_event_lock))) {
   484     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_thr_event_lock", &thr_event_lock))) {
   493         NSK_COMPLAIN0("TEST FAILED: failed to create thr_event_lock\n");
   485         NSK_COMPLAIN0("TEST FAILED: failed to create thr_event_lock\n");
   494         return JNI_ERR;
   486         return JNI_ERR;
   495     }
   487     }
   496 
   488 
   497     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_thr_start_lock", &thr_start_lock))) {
   489     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_thr_start_lock", &thr_start_lock))) {
   498         NSK_COMPLAIN0("TEST FAILED: failed to create thr_start_lock\n");
   490         NSK_COMPLAIN0("TEST FAILED: failed to create thr_start_lock\n");
   499         return JNI_ERR;
   491         return JNI_ERR;
   500     }
   492     }
   501 
   493 
   502     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_thr_resume_lock", &thr_resume_lock))) {
   494     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_thr_resume_lock", &thr_resume_lock))) {
   503         NSK_COMPLAIN0("TEST FAILED: failed to create thr_resume_lock\n");
   495         NSK_COMPLAIN0("TEST FAILED: failed to create thr_resume_lock\n");
   504         return JNI_ERR;
   496         return JNI_ERR;
   505     }
   497     }
   506 
   498 
   507     callbacks.VMInit = &VMInit;
   499     callbacks.VMInit = &VMInit;
   508     callbacks.VMDeath = &VMDeath;
   500     callbacks.VMDeath = &VMDeath;
   509     callbacks.ThreadStart = &ThreadStart;
   501     callbacks.ThreadStart = &ThreadStart;
   510 
   502 
   511     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &callbacks, sizeof(callbacks)))) {
   503     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)))) {
   512         NSK_COMPLAIN0("TEST FAILED: failed to set event callbacks\n");
   504         NSK_COMPLAIN0("TEST FAILED: failed to set event callbacks\n");
   513         return JNI_ERR;
   505         return JNI_ERR;
   514     }
   506     }
   515 
   507 
   516     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
   508     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL))) {
   517             jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL))) {
       
   518         NSK_COMPLAIN0("TEST FAILED: failed to enable JVMTI_EVENT_VM_INIT\n");
   509         NSK_COMPLAIN0("TEST FAILED: failed to enable JVMTI_EVENT_VM_INIT\n");
   519         return JNI_ERR;
   510         return JNI_ERR;
   520     }
   511     }
   521 
   512 
   522     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
   513     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) {
   523             jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) {
       
   524         NSK_COMPLAIN0("TEST FAILED: failed to enable JVMTI_EVENT_VM_DEATH\n");
   514         NSK_COMPLAIN0("TEST FAILED: failed to enable JVMTI_EVENT_VM_DEATH\n");
   525         return JNI_ERR;
   515         return JNI_ERR;
   526     }
   516     }
   527 
   517 
   528     return JNI_OK;
   518     return JNI_OK;