< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/multienv/MA04/ma04t001/ma04t001.cpp

Print this page
rev 52050 : [mq]: refactor


  36 /* ========================================================================== */
  37 
  38 /* scaffold objects */
  39 static jlong timeout = 0;
  40 
  41 /* test objects */
  42 static jobject testedObject = NULL;
  43 
  44 /* ========================================================================== */
  45 
  46 static int prepare(JNIEnv* jni) {
  47     const char* CLASS_NAME = "nsk/jvmti/scenarios/multienv/MA04/ma04t001";
  48     const char* FIELD_NAME = "testedObject";
  49     const char* FIELD_SIGNATURE = "Ljava/lang/Object;";
  50     jclass cls = NULL;
  51     jfieldID fid = NULL;
  52 
  53     NSK_DISPLAY0("Obtain tested object from a static field of debugee class\n");
  54 
  55     NSK_DISPLAY1("Find class: %s\n", CLASS_NAME);
  56     if (!NSK_JNI_VERIFY(jni, (cls =
  57             NSK_CPP_STUB2(FindClass, jni, CLASS_NAME)) != NULL))
  58         return NSK_FALSE;
  59 
  60     NSK_DISPLAY2("Find field: %s:%s\n", FIELD_NAME, FIELD_SIGNATURE);
  61     if (!NSK_JNI_VERIFY(jni, (fid =
  62             NSK_CPP_STUB4(GetStaticFieldID, jni, cls,
  63                 FIELD_NAME, FIELD_SIGNATURE)) != NULL))
  64         return NSK_FALSE;
  65 
  66     if (!NSK_JNI_VERIFY(jni, (testedObject =
  67             NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fid)) != NULL))
  68         return NSK_FALSE;
  69 
  70     if (!NSK_JNI_VERIFY(jni, (testedObject =
  71             NSK_CPP_STUB2(NewGlobalRef, jni, testedObject)) != NULL))
  72         return NSK_FALSE;
  73 
  74     return NSK_TRUE;
  75 }
  76 
  77 /* ========================================================================== */
  78 
  79 /** Agent algorithm. */
  80 static void JNICALL
  81 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  82     jlong tag = -1;
  83     char buffer[32];
  84 
  85     if (!nsk_jvmti_waitForSync(timeout))
  86         return;
  87 
  88     if (!prepare(jni)) {
  89         nsk_jvmti_setFailStatus();
  90         return;
  91     }
  92 
  93     NSK_DISPLAY0("Testcase #1: check that testedObject is not tagged \n");
  94     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
  95         nsk_jvmti_setFailStatus();
  96         return;
  97     }
  98     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
  99     if (tag != 0) {
 100         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 101             jlong_to_string(tag, buffer));
 102         nsk_jvmti_setFailStatus();
 103     }
 104     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject,
 105             SAMPLE_TAG))) {
 106         nsk_jvmti_setFailStatus();
 107         return;
 108     }
 109     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 110         return;
 111     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 112         return;
 113 
 114     NSK_DISPLAY0("Testcase #2: check that testedObject is tagged correctly\n");
 115     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 116         nsk_jvmti_setFailStatus();
 117         return;
 118     }
 119     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 120     if (tag != SAMPLE_TAG) {
 121         if (tag == 0) {
 122             NSK_COMPLAIN0("testedObject not tagged\n");
 123         } else {
 124             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 125                 jlong_to_string(SAMPLE_TAG, buffer));
 126             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 127         }
 128         nsk_jvmti_setFailStatus();
 129     }
 130     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 131         return;
 132     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 133         return;
 134 
 135     NSK_DISPLAY0("Testcase #3: check that testedObject is tagged correctly\n");
 136     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 137         nsk_jvmti_setFailStatus();
 138         return;
 139     }
 140     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 141     if (tag != SAMPLE_TAG) {
 142         if (tag == 0) {
 143             NSK_COMPLAIN0("testedObject not tagged\n");
 144         } else {
 145             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 146                 jlong_to_string(SAMPLE_TAG, buffer));
 147             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 148         }
 149         nsk_jvmti_setFailStatus();
 150     }
 151     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject, (jlong)0))) {
 152         nsk_jvmti_setFailStatus();
 153         return;
 154     }
 155     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 156         return;
 157     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 158         return;
 159 
 160     NSK_DISPLAY0("Testcase #4: check that testedObject is not tagged \n");
 161     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 162         nsk_jvmti_setFailStatus();
 163         return;
 164     }
 165     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 166     if (tag != 0) {
 167         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 168             jlong_to_string(tag, buffer));
 169         nsk_jvmti_setFailStatus();
 170     }
 171     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 172         return;
 173     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 174         return;
 175 
 176     NSK_DISPLAY0("Testcase #5: check that testedObject is not tagged\n");
 177     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 178         nsk_jvmti_setFailStatus();
 179         return;
 180     }
 181     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 182     if (tag != 0) {
 183         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 184             jlong_to_string(tag, buffer));
 185         nsk_jvmti_setFailStatus();
 186     }
 187     NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedObject));
 188 
 189     if (!nsk_jvmti_resumeSync())
 190         return;
 191 }
 192 
 193 /* ========================================================================== */
 194 
 195 /** Agent library initialization. */
 196 #ifdef STATIC_BUILD
 197 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 198     return Agent_Initialize(jvm, options, reserved);
 199 }
 200 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 201     return Agent_Initialize(jvm, options, reserved);
 202 }
 203 JNIEXPORT jint JNI_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 204     return JNI_VERSION_1_8;
 205 }
 206 #endif
 207 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 208     jvmtiEnv* jvmti = NULL;
 209     jvmtiEventCallbacks callbacks;
 210     jvmtiCapabilities caps;
 211 
 212     NSK_DISPLAY0("Agent_OnLoad\n");
 213 
 214     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 215         return JNI_ERR;
 216 
 217     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 218 
 219     if (!NSK_VERIFY((jvmti =
 220             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 221         return JNI_ERR;
 222 
 223     memset(&caps, 0, sizeof(caps));
 224     caps.can_tag_objects = 1;
 225     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 226         return JNI_ERR;
 227     }
 228 
 229     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 230         return JNI_ERR;
 231 
 232     memset(&callbacks, 0, sizeof(callbacks));
 233     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 234         return JNI_ERR;
 235 
 236     return JNI_OK;
 237 }
 238 
 239 /* ========================================================================== */
 240 
 241 }


  36 /* ========================================================================== */
  37 
  38 /* scaffold objects */
  39 static jlong timeout = 0;
  40 
  41 /* test objects */
  42 static jobject testedObject = NULL;
  43 
  44 /* ========================================================================== */
  45 
  46 static int prepare(JNIEnv* jni) {
  47     const char* CLASS_NAME = "nsk/jvmti/scenarios/multienv/MA04/ma04t001";
  48     const char* FIELD_NAME = "testedObject";
  49     const char* FIELD_SIGNATURE = "Ljava/lang/Object;";
  50     jclass cls = NULL;
  51     jfieldID fid = NULL;
  52 
  53     NSK_DISPLAY0("Obtain tested object from a static field of debugee class\n");
  54 
  55     NSK_DISPLAY1("Find class: %s\n", CLASS_NAME);
  56     if (!NSK_JNI_VERIFY(jni, (cls = jni->FindClass(CLASS_NAME)) != NULL))

  57         return NSK_FALSE;
  58 
  59     NSK_DISPLAY2("Find field: %s:%s\n", FIELD_NAME, FIELD_SIGNATURE);
  60     if (!NSK_JNI_VERIFY(jni, (fid =
  61             jni->GetStaticFieldID(cls, FIELD_NAME, FIELD_SIGNATURE)) != NULL))

  62         return NSK_FALSE;
  63 
  64     if (!NSK_JNI_VERIFY(jni, (testedObject = jni->GetStaticObjectField(cls, fid)) != NULL))

  65         return NSK_FALSE;
  66 
  67     if (!NSK_JNI_VERIFY(jni, (testedObject = jni->NewGlobalRef(testedObject)) != NULL))

  68         return NSK_FALSE;
  69 
  70     return NSK_TRUE;
  71 }
  72 
  73 /* ========================================================================== */
  74 
  75 /** Agent algorithm. */
  76 static void JNICALL
  77 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  78     jlong tag = -1;
  79     char buffer[32];
  80 
  81     if (!nsk_jvmti_waitForSync(timeout))
  82         return;
  83 
  84     if (!prepare(jni)) {
  85         nsk_jvmti_setFailStatus();
  86         return;
  87     }
  88 
  89     NSK_DISPLAY0("Testcase #1: check that testedObject is not tagged \n");
  90     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
  91         nsk_jvmti_setFailStatus();
  92         return;
  93     }
  94     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
  95     if (tag != 0) {
  96         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
  97             jlong_to_string(tag, buffer));
  98         nsk_jvmti_setFailStatus();
  99     }
 100     if (!NSK_JVMTI_VERIFY(jvmti->SetTag(testedObject, SAMPLE_TAG))) {

 101         nsk_jvmti_setFailStatus();
 102         return;
 103     }
 104     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 105         return;
 106     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 107         return;
 108 
 109     NSK_DISPLAY0("Testcase #2: check that testedObject is tagged correctly\n");
 110     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 111         nsk_jvmti_setFailStatus();
 112         return;
 113     }
 114     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 115     if (tag != SAMPLE_TAG) {
 116         if (tag == 0) {
 117             NSK_COMPLAIN0("testedObject not tagged\n");
 118         } else {
 119             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 120                 jlong_to_string(SAMPLE_TAG, buffer));
 121             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 122         }
 123         nsk_jvmti_setFailStatus();
 124     }
 125     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 126         return;
 127     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 128         return;
 129 
 130     NSK_DISPLAY0("Testcase #3: check that testedObject is tagged correctly\n");
 131     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 132         nsk_jvmti_setFailStatus();
 133         return;
 134     }
 135     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 136     if (tag != SAMPLE_TAG) {
 137         if (tag == 0) {
 138             NSK_COMPLAIN0("testedObject not tagged\n");
 139         } else {
 140             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 141                 jlong_to_string(SAMPLE_TAG, buffer));
 142             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 143         }
 144         nsk_jvmti_setFailStatus();
 145     }
 146     if (!NSK_JVMTI_VERIFY(jvmti->SetTag(testedObject, (jlong)0))) {
 147         nsk_jvmti_setFailStatus();
 148         return;
 149     }
 150     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 151         return;
 152     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 153         return;
 154 
 155     NSK_DISPLAY0("Testcase #4: check that testedObject is not tagged \n");
 156     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 157         nsk_jvmti_setFailStatus();
 158         return;
 159     }
 160     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 161     if (tag != 0) {
 162         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 163             jlong_to_string(tag, buffer));
 164         nsk_jvmti_setFailStatus();
 165     }
 166     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 167         return;
 168     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 169         return;
 170 
 171     NSK_DISPLAY0("Testcase #5: check that testedObject is not tagged\n");
 172     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 173         nsk_jvmti_setFailStatus();
 174         return;
 175     }
 176     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 177     if (tag != 0) {
 178         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 179             jlong_to_string(tag, buffer));
 180         nsk_jvmti_setFailStatus();
 181     }
 182     NSK_TRACE(jni->DeleteGlobalRef(testedObject));
 183 
 184     if (!nsk_jvmti_resumeSync())
 185         return;
 186 }
 187 
 188 /* ========================================================================== */
 189 
 190 /** Agent library initialization. */
 191 #ifdef STATIC_BUILD
 192 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 193     return Agent_Initialize(jvm, options, reserved);
 194 }
 195 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 196     return Agent_Initialize(jvm, options, reserved);
 197 }
 198 JNIEXPORT jint JNI_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 199     return JNI_VERSION_1_8;
 200 }
 201 #endif
 202 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 203     jvmtiEnv* jvmti = NULL;
 204     jvmtiEventCallbacks callbacks;
 205     jvmtiCapabilities caps;
 206 
 207     NSK_DISPLAY0("Agent_OnLoad\n");
 208 
 209     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 210         return JNI_ERR;
 211 
 212     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 213 
 214     if (!NSK_VERIFY((jvmti =
 215             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 216         return JNI_ERR;
 217 
 218     memset(&caps, 0, sizeof(caps));
 219     caps.can_tag_objects = 1;
 220     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 221         return JNI_ERR;
 222     }
 223 
 224     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 225         return JNI_ERR;
 226 
 227     memset(&callbacks, 0, sizeof(callbacks));
 228     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 229         return JNI_ERR;
 230 
 231     return JNI_OK;
 232 }
 233 
 234 /* ========================================================================== */
 235 
 236 }
< prev index next >