< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/general_functions/GF06/gf06t001/gf06t001.cpp

Print this page
rev 52050 : [mq]: refactor


  58 
  59 
  60 /* ============================================================================= */
  61 
  62 /** Fill storage data with given char */
  63 static void fillEnvStorage(StorageStructure* storage) {
  64     NSK_DISPLAY2("Fill storage data with char %c for size: %d bytes\n",
  65                 (char)STORAGE_DATA_CHAR, (int)STORAGE_DATA_SIZE);
  66     memset(storage->data, STORAGE_DATA_CHAR, STORAGE_DATA_SIZE);
  67     NSK_DISPLAY0("  ... ok\n");
  68 }
  69 
  70 /**
  71  * Check JVMTI environment local storage.
  72  * @returns NSK_FALSE if any error occured.
  73  */
  74 static int checkEnvStorage(jvmtiEnv* jvmti, const char where[]) {
  75     void* storage = NULL;
  76 
  77     NSK_DISPLAY0("Calling GetEnvironmentLocalStorage():");
  78     if (!NSK_JVMTI_VERIFY(
  79             NSK_CPP_STUB2(GetEnvironmentLocalStorage, jvmti, &storage))) {
  80         return NSK_FALSE;
  81     }
  82     NSK_DISPLAY1("  ... got storage: 0x%p\n", (void*)storage);
  83 
  84     if (storage != NULL) {
  85         NSK_COMPLAIN2("GetEnvironmentLocalStorage() returned NOT NULL storage in %s:\n"
  86                       "#   storage pointer: 0x%p\n",
  87                         where, (void*)storage);
  88         return NSK_FALSE;
  89     }
  90 
  91     return NSK_TRUE;
  92 }
  93 
  94 /* ============================================================================= */
  95 
  96 /** Agent algorithm. */
  97 static void JNICALL
  98 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  99     NSK_DISPLAY0("Wait for debugee to become ready\n");
 100     if (!nsk_jvmti_waitForSync(timeout))
 101         return;
 102 
 103     NSK_DISPLAY0("CASE #3: Check local storage in agent thread for second JVMTI env.\n");
 104     if (!checkEnvStorage(jvmti, "agent thread")) {
 105         nsk_jvmti_setFailStatus();
 106     }
 107 
 108     NSK_DISPLAY0("Let debugee to finish\n");
 109     if (!nsk_jvmti_resumeSync())
 110         return;
 111 }
 112 
 113 jthread getEnvThread(JNIEnv *env) {
 114     jclass thrClass;
 115     jmethodID cid;
 116     jthread res;
 117 
 118     thrClass = NSK_CPP_STUB2(FindClass, env, "java/lang/Thread");
 119     cid = NSK_CPP_STUB4(GetMethodID, env, thrClass, "<init>", "()V");
 120     res = NSK_CPP_STUB3(NewObject, env, thrClass, cid);
 121     return res;
 122 }
 123 
 124 /* ============================================================================= */
 125 
 126 /**
 127  * Callback for VM_INIT event.
 128  */
 129 JNIEXPORT void JNICALL
 130 callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 131 
 132     NSK_DISPLAY0("CASE #2: Check local storage in VM_INIT callback for second JVMTI env.\n");
 133     if (!checkEnvStorage(jvmti, "VM_INIT callback")) {
 134         nsk_jvmti_setFailStatus();
 135     }
 136 
 137     NSK_DISPLAY0("Set agentProc for second JVMTI env.\n");
 138     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 139         nsk_jvmti_setFailStatus();
 140 }


 176     return JNI_VERSION_1_8;
 177 }
 178 #endif
 179 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 180     jint res;
 181     jvmtiEnv* jvmti_1 = NULL;
 182 
 183     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 184         return JNI_ERR;
 185 
 186     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 187 
 188     NSK_DISPLAY0("Create first JVMTI env.\n");
 189     res = jvm->GetEnv((void **) &jvmti_1, JVMTI_VERSION_1_1);
 190     if (res < 0) {
 191         NSK_COMPLAIN0("Wrong result of a valid call to GetEnv!\n");
 192         return JNI_ERR;
 193     }
 194 
 195     NSK_DISPLAY1("Set local storage in JVM_OnLoad() for first JVMTI env: 0x%p\n", (void*)initialStorage);
 196     if (!NSK_JVMTI_VERIFY(
 197             NSK_CPP_STUB2(SetEnvironmentLocalStorage, jvmti_1, initialStorage))) {
 198         return JNI_ERR;
 199     }
 200     NSK_DISPLAY0("  ... ok\n");
 201 
 202     /* Create second environment */
 203     if (!NSK_VERIFY((jvmti_2 =
 204             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 205         return JNI_ERR;
 206 
 207     /* Set callbacks for second environment */
 208     {
 209         jvmtiEventCallbacks eventCallbacks;
 210 
 211         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 212         eventCallbacks.VMInit = callbackVMInit;
 213         eventCallbacks.VMDeath = callbackVMDeath;
 214         if (!NSK_JVMTI_VERIFY(
 215                 NSK_CPP_STUB3(SetEventCallbacks, jvmti_2,
 216                                     &eventCallbacks, sizeof(eventCallbacks)))) {
 217             return JNI_ERR;
 218         }
 219 
 220     }
 221 
 222 
 223     NSK_DISPLAY1("Prepare storage data at pointer: 0x%p\n", initialStorage);
 224     fillEnvStorage(initialStorage);
 225 
 226     NSK_DISPLAY0("CASE #1: Check local storage in JVM_OnLoad() for second JVMTI env.\n");
 227     if (!checkEnvStorage(jvmti_2, "JVM_OnLoad()")) {
 228         nsk_jvmti_setFailStatus();
 229     }
 230 
 231     NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);
 232     if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {
 233         NSK_DISPLAY0("  ... enabled\n");
 234     }
 235 
 236     return JNI_OK;


  58 
  59 
  60 /* ============================================================================= */
  61 
  62 /** Fill storage data with given char */
  63 static void fillEnvStorage(StorageStructure* storage) {
  64     NSK_DISPLAY2("Fill storage data with char %c for size: %d bytes\n",
  65                 (char)STORAGE_DATA_CHAR, (int)STORAGE_DATA_SIZE);
  66     memset(storage->data, STORAGE_DATA_CHAR, STORAGE_DATA_SIZE);
  67     NSK_DISPLAY0("  ... ok\n");
  68 }
  69 
  70 /**
  71  * Check JVMTI environment local storage.
  72  * @returns NSK_FALSE if any error occured.
  73  */
  74 static int checkEnvStorage(jvmtiEnv* jvmti, const char where[]) {
  75     void* storage = NULL;
  76 
  77     NSK_DISPLAY0("Calling GetEnvironmentLocalStorage():");
  78     if (!NSK_JVMTI_VERIFY(jvmti->GetEnvironmentLocalStorage(&storage))) {

  79         return NSK_FALSE;
  80     }
  81     NSK_DISPLAY1("  ... got storage: 0x%p\n", (void*)storage);
  82 
  83     if (storage != NULL) {
  84         NSK_COMPLAIN2("GetEnvironmentLocalStorage() returned NOT NULL storage in %s:\n"
  85                       "#   storage pointer: 0x%p\n",
  86                         where, (void*)storage);
  87         return NSK_FALSE;
  88     }
  89 
  90     return NSK_TRUE;
  91 }
  92 
  93 /* ============================================================================= */
  94 
  95 /** Agent algorithm. */
  96 static void JNICALL
  97 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  98     NSK_DISPLAY0("Wait for debugee to become ready\n");
  99     if (!nsk_jvmti_waitForSync(timeout))
 100         return;
 101 
 102     NSK_DISPLAY0("CASE #3: Check local storage in agent thread for second JVMTI env.\n");
 103     if (!checkEnvStorage(jvmti, "agent thread")) {
 104         nsk_jvmti_setFailStatus();
 105     }
 106 
 107     NSK_DISPLAY0("Let debugee to finish\n");
 108     if (!nsk_jvmti_resumeSync())
 109         return;
 110 }
 111 
 112 jthread getEnvThread(JNIEnv *env) {
 113     jclass thrClass;
 114     jmethodID cid;
 115     jthread res;
 116 
 117     thrClass = env->FindClass("java/lang/Thread");
 118     cid = env->GetMethodID(thrClass, "<init>", "()V");
 119     res = env->NewObject(thrClass, cid);
 120     return res;
 121 }
 122 
 123 /* ============================================================================= */
 124 
 125 /**
 126  * Callback for VM_INIT event.
 127  */
 128 JNIEXPORT void JNICALL
 129 callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 130 
 131     NSK_DISPLAY0("CASE #2: Check local storage in VM_INIT callback for second JVMTI env.\n");
 132     if (!checkEnvStorage(jvmti, "VM_INIT callback")) {
 133         nsk_jvmti_setFailStatus();
 134     }
 135 
 136     NSK_DISPLAY0("Set agentProc for second JVMTI env.\n");
 137     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 138         nsk_jvmti_setFailStatus();
 139 }


 175     return JNI_VERSION_1_8;
 176 }
 177 #endif
 178 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 179     jint res;
 180     jvmtiEnv* jvmti_1 = NULL;
 181 
 182     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 183         return JNI_ERR;
 184 
 185     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 186 
 187     NSK_DISPLAY0("Create first JVMTI env.\n");
 188     res = jvm->GetEnv((void **) &jvmti_1, JVMTI_VERSION_1_1);
 189     if (res < 0) {
 190         NSK_COMPLAIN0("Wrong result of a valid call to GetEnv!\n");
 191         return JNI_ERR;
 192     }
 193 
 194     NSK_DISPLAY1("Set local storage in JVM_OnLoad() for first JVMTI env: 0x%p\n", (void*)initialStorage);
 195     if (!NSK_JVMTI_VERIFY(jvmti_1->SetEnvironmentLocalStorage(initialStorage))) {

 196         return JNI_ERR;
 197     }
 198     NSK_DISPLAY0("  ... ok\n");
 199 
 200     /* Create second environment */
 201     if (!NSK_VERIFY((jvmti_2 =
 202             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 203         return JNI_ERR;
 204 
 205     /* Set callbacks for second environment */
 206     {
 207         jvmtiEventCallbacks eventCallbacks;
 208 
 209         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 210         eventCallbacks.VMInit = callbackVMInit;
 211         eventCallbacks.VMDeath = callbackVMDeath;
 212         if (!NSK_JVMTI_VERIFY(
 213                 jvmti_2->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {

 214             return JNI_ERR;
 215         }
 216 
 217     }
 218 
 219 
 220     NSK_DISPLAY1("Prepare storage data at pointer: 0x%p\n", initialStorage);
 221     fillEnvStorage(initialStorage);
 222 
 223     NSK_DISPLAY0("CASE #1: Check local storage in JVM_OnLoad() for second JVMTI env.\n");
 224     if (!checkEnvStorage(jvmti_2, "JVM_OnLoad()")) {
 225         nsk_jvmti_setFailStatus();
 226     }
 227 
 228     NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);
 229     if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {
 230         NSK_DISPLAY0("  ... enabled\n");
 231     }
 232 
 233     return JNI_OK;
< prev index next >