test/hotspot/jtreg/serviceability/jvmti/SuspendWithCurrentThread/libSuspendWithCurrentThread.cpp
changeset 58530 865c889ce351
equal deleted inserted replaced
58529:36cdb1cab7b0 58530:865c889ce351
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 #include <string.h>
       
    25 #include "jvmti.h"
       
    26 
       
    27 extern "C" {
       
    28 
       
    29 static jvmtiEnv* jvmti = NULL;
       
    30 static jthread* threads = NULL;
       
    31 static jsize threads_count = 0;
       
    32 static jrawMonitorID agent_monitor = NULL;
       
    33 
       
    34 #define LOG(...) \
       
    35   do { \
       
    36     printf(__VA_ARGS__); \
       
    37     printf("\n"); \
       
    38     fflush(stdout); \
       
    39   } while (0)
       
    40 
       
    41 static void
       
    42 check_jvmti_status(JNIEnv* jni, jvmtiError err, const char* msg) {
       
    43   if (err != JVMTI_ERROR_NONE) {
       
    44     LOG("check_jvmti_status: JVMTI function returned error: %d", err);
       
    45     jni->FatalError(msg);
       
    46   }
       
    47 }
       
    48 
       
    49 static void
       
    50 agent_lock(JNIEnv* jni) {
       
    51   jvmtiError err = jvmti->RawMonitorEnter(agent_monitor);
       
    52   check_jvmti_status(jni, err, "monitor_enter: error in JVMTI RawMonitorEnter");
       
    53 }
       
    54 
       
    55 static void
       
    56 agent_unlock(JNIEnv* jni) {
       
    57   jvmtiError err = jvmti->RawMonitorExit(agent_monitor);
       
    58   check_jvmti_status(jni, err, "monitor_exit: error in JVMTI RawMonitorExit");
       
    59 }
       
    60 
       
    61 JNIEXPORT void JNICALL
       
    62 Java_SuspendWithCurrentThread_registerTestedThreads(JNIEnv *jni, jclass cls, jobjectArray threadsArr) {
       
    63   LOG("\nregisterTestedThreads: started");
       
    64   threads_count = jni->GetArrayLength(threadsArr);
       
    65 
       
    66   jvmtiError err = jvmti->Allocate((threads_count * sizeof(jthread)),
       
    67                                    (unsigned char**)&threads);
       
    68   check_jvmti_status(jni, err, "registerTestedThreads: error in JVMTI Allocate threads array");
       
    69 
       
    70   for (int i = 0; i < threads_count; i++) {
       
    71     jobject elem = jni->GetObjectArrayElement(threadsArr, i);
       
    72     threads[i] = (jthread)jni->NewGlobalRef(elem);
       
    73   }
       
    74   LOG("registerTestedThreads: finished\n");
       
    75 }
       
    76 
       
    77 /* This function is executed on the suspender thread, not the Main thread */
       
    78 JNIEXPORT void JNICALL
       
    79 Java_ThreadToSuspend_init(JNIEnv *jni, jclass cls) {
       
    80   jvmtiError err = jvmti->CreateRawMonitor("Agent monitor", &agent_monitor);
       
    81   check_jvmti_status(jni, err, "Java_ThreadToSuspend_init: error in JVMTI CreateRawMonitor");
       
    82 
       
    83   // Main thread has to wait for the suspender thread to complete tested threads suspension
       
    84   agent_lock(jni);
       
    85 }
       
    86 
       
    87 /* This function is executed on the suspender thread which is not Main thread */
       
    88 JNIEXPORT void JNICALL
       
    89 Java_ThreadToSuspend_suspendTestedThreads(JNIEnv *jni, jclass cls) {
       
    90   jvmtiError* results = NULL;
       
    91   jvmtiError err;
       
    92 
       
    93   LOG("\nsuspendTestedThreads: started");
       
    94   err = jvmti->Allocate((threads_count * sizeof(jvmtiError)),
       
    95                         (unsigned char**)&results);
       
    96   check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI Allocate results array");
       
    97 
       
    98   LOG("suspendTestedThreads: before JVMTI SuspendThreadList");
       
    99   err = jvmti->SuspendThreadList(threads_count, threads, results);
       
   100   check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI SuspendThreadList");
       
   101 
       
   102   LOG("suspendTestedThreads: check and print SuspendThreadList results:");
       
   103   for (int i = 0; i < threads_count; i++) {
       
   104     LOG("  thread #%d: (%d)", i, (int)results[i]);
       
   105     check_jvmti_status(jni, results[i], "suspendTestedThreads: error in SuspendThreadList results[i]");
       
   106   }
       
   107   LOG("suspendTestedThreads: finished\n");
       
   108 
       
   109   // Allow the Main thread to inspect the result of tested threads suspension
       
   110   agent_unlock(jni);
       
   111 
       
   112   err = jvmti->Deallocate((unsigned char*)results);
       
   113   check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI Deallocate results");
       
   114 }
       
   115 
       
   116 JNIEXPORT jboolean JNICALL
       
   117 Java_SuspendWithCurrentThread_checkTestedThreadsSuspended(JNIEnv *jni, jclass cls) {
       
   118   LOG("checkTestedThreadsSuspended: started");
       
   119 
       
   120   // Block until the suspender thread competes the tested threads suspension
       
   121   agent_lock(jni);
       
   122   agent_unlock(jni);
       
   123 
       
   124   for (int i = 0; i < threads_count; i++) {
       
   125     jint state = 0;
       
   126     jvmtiError err = jvmti->GetThreadState(threads[i], &state);
       
   127     check_jvmti_status(jni, err, "checkTestedThreadsSuspended: error in GetThreadState");
       
   128 
       
   129     if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
       
   130       LOG("thread #%d has not been suspended yet: "
       
   131              "#   state: (%#x)", i, (int)state);
       
   132       jni->FatalError("checkTestedThreadsSuspended: error: expected all tested threads suspended");
       
   133     }
       
   134   }
       
   135   LOG("checkTestedThreadsSuspended: finished\n");
       
   136   return JNI_TRUE;
       
   137 }
       
   138 
       
   139 JNIEXPORT void JNICALL
       
   140 Java_SuspendWithCurrentThread_resumeTestedThreads(JNIEnv *jni, jclass cls) {
       
   141   jvmtiError* results = NULL;
       
   142   jvmtiError err;
       
   143 
       
   144   LOG("\nresumeTestedThreads: started");
       
   145   err = jvmti->Allocate((threads_count * sizeof(jvmtiError)),
       
   146                         (unsigned char**)&results);
       
   147   check_jvmti_status(jni, err, "resumeTestedThreads: error in JVMTI Allocate results array");
       
   148 
       
   149   LOG("resumeTestedThreads: before JVMTI ResumeThreadList");
       
   150   err = jvmti->ResumeThreadList(threads_count, threads, results);
       
   151   check_jvmti_status(jni, err, "resumeTestedThreads: error in ResumeThreadList");
       
   152 
       
   153   LOG("resumeTestedThreads: check and print ResumeThreadList results:");
       
   154   for (int i = 0; i < threads_count; i++) {
       
   155     LOG("  thread #%d: (%d)", i, (int)results[i]);
       
   156     check_jvmti_status(jni, results[i], "resumeTestedThreads: error in ResumeThreadList results[i]");
       
   157   }
       
   158 
       
   159   err = jvmti->Deallocate((unsigned char*)results);
       
   160   check_jvmti_status(jni, err, "resumeTestedThreads: error in JVMTI Deallocate results");
       
   161 
       
   162   LOG("resumeTestedThreads: finished\n");
       
   163 }
       
   164 
       
   165 JNIEXPORT void JNICALL
       
   166 Java_SuspendWithCurrentThread_releaseTestedThreadsInfo(JNIEnv *jni, jclass cls) {
       
   167   jvmtiError err;
       
   168 
       
   169   LOG("\nreleaseTestedThreadsInfo: started");
       
   170   err = jvmti->DestroyRawMonitor(agent_monitor);
       
   171   check_jvmti_status(jni, err, "releaseTestedThreadsInfo: error in JVMTI DestroyRawMonitor");
       
   172 
       
   173   for (int i = 0; i < threads_count; i++) {
       
   174     if (threads[i] != NULL) {
       
   175       jni->DeleteGlobalRef(threads[i]);
       
   176     }
       
   177   }
       
   178   err = jvmti->Deallocate((unsigned char*)threads);
       
   179   check_jvmti_status(jni, err, "releaseTestedThreadsInfo: error in JVMTI Deallocate threads");
       
   180 
       
   181   LOG("releaseTestedThreadsInfo: finished\n");
       
   182 }
       
   183 
       
   184 
       
   185 /** Agent library initialization. */
       
   186 
       
   187 JNIEXPORT jint JNICALL
       
   188 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
       
   189   LOG("\nAgent_OnLoad started");
       
   190 
       
   191   // create JVMTI environment
       
   192   if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
       
   193     return JNI_ERR;
       
   194   }
       
   195 
       
   196   // add specific capabilities for suspending thread
       
   197   jvmtiCapabilities suspendCaps;
       
   198   memset(&suspendCaps, 0, sizeof(suspendCaps));
       
   199   suspendCaps.can_suspend = 1;
       
   200 
       
   201   jvmtiError err = jvmti->AddCapabilities(&suspendCaps);
       
   202   if (err != JVMTI_ERROR_NONE) {
       
   203     return JNI_ERR;
       
   204   }
       
   205   LOG("Agent_OnLoad finished\n");
       
   206   return JNI_OK;
       
   207 }
       
   208 
       
   209 }