< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/SingleStep/singlestep001/singlestep001.cpp

Print this page
rev 52185 : [mq]: refactor


  46 static const char *METHOD_SIGS[] = {
  47     "()V",
  48     "([Ljava/lang/String;Ljava/io/PrintStream;)I"
  49 };
  50 
  51 static volatile long stepEv[] = {0, 0};
  52 
  53 static const char *CLASS_SIG =
  54     "Lnsk/jvmti/SingleStep/singlestep001;";
  55 
  56 static volatile jint result = PASSED;
  57 static jvmtiEnv *jvmti = NULL;
  58 static jvmtiEventCallbacks callbacks;
  59 
  60 static int vm_started = 0;
  61 static jrawMonitorID agent_lock;
  62 
  63 static void setBP(jvmtiEnv *jvmti_env, JNIEnv *env, jclass klass) {
  64     jmethodID mid;
  65 
  66     if (!NSK_JNI_VERIFY(env, (mid = NSK_CPP_STUB4(GetMethodID,
  67             env, klass, METHODS[0], METHOD_SIGS[0])) != NULL))
  68         NSK_CPP_STUB2(FatalError, env,
  69             "failed to get ID for the java method\n");
  70 
  71     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetBreakpoint,
  72             jvmti_env, mid, 0)))
  73         NSK_CPP_STUB2(FatalError, env,
  74             "failed to set breakpoint\n");
  75 }
  76 
  77 /** callback functions **/
  78 void JNICALL
  79 ClassLoad(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, jclass klass) {
  80     char *sig, *generic;
  81 
  82     jvmti->RawMonitorEnter(agent_lock);
  83 
  84     if (vm_started) {
  85         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature,
  86                                             jvmti_env, klass, &sig, &generic)))
  87             NSK_CPP_STUB2(FatalError, env,
  88                           "failed to obtain a class signature\n");
  89 
  90         if (sig != NULL && (strcmp(sig, CLASS_SIG) == 0)) {
  91             NSK_DISPLAY1(
  92                 "ClassLoad event received for the class \"%s\"\n"
  93                 "\tsetting breakpoint ...\n",
  94                 sig);
  95             setBP(jvmti_env, env, klass);
  96         }
  97     }
  98 
  99     jvmti->RawMonitorExit(agent_lock);
 100 }
 101 
 102 void JNICALL
 103 Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr, jmethodID method,
 104         jlocation loc) {
 105     jclass klass;
 106     char *sig, *generic;
 107 
 108     NSK_DISPLAY0("Breakpoint event received\n");
 109     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetMethodDeclaringClass,
 110             jvmti_env, method, &klass)))
 111         NSK_COMPLAIN0("TEST FAILURE: unable to get method declaring class\n\n");
 112 
 113     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature,
 114             jvmti_env, klass, &sig, &generic)))
 115         NSK_CPP_STUB2(FatalError, env,
 116             "Breakpoint: failed to obtain a class signature\n");
 117 
 118     if (sig != NULL && (strcmp(sig, CLASS_SIG) == 0)) {
 119         NSK_DISPLAY1("method declaring class \"%s\"\n\tenabling SingleStep events ...\n",
 120             sig);
 121         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 122                 jvmti_env, JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thr))) {
 123             result = STATUS_FAILED;
 124             NSK_COMPLAIN0("TEST FAILURE: cannot enable SingleStep events\n\n");
 125         }
 126     } else {
 127         result = STATUS_FAILED;
 128         NSK_COMPLAIN1("TEST FAILURE: unexpected breakpoint event in method of class \"%s\"\n\n",
 129             sig);
 130     }
 131 }
 132 
 133 void JNICALL
 134 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 135         jmethodID method, jlocation location) {
 136     jclass klass;
 137     char *sig, *generic, *methNam, *methSig;
 138 
 139     if (result == STATUS_FAILED) {
 140         return;
 141     }
 142 
 143     NSK_DISPLAY0(">>>> SingleStep event received\n");
 144 
 145     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
 146             jvmti_env, method, &methNam, &methSig, NULL))) {
 147         result = STATUS_FAILED;
 148         NSK_COMPLAIN0("TEST FAILED: unable to get method name during SingleStep callback\n\n");
 149         return;
 150     }
 151     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetMethodDeclaringClass,
 152             jvmti_env, method, &klass))) {
 153         result = STATUS_FAILED;
 154         NSK_COMPLAIN0("TEST FAILED: unable to get method declaring class during SingleStep callback\n\n");
 155         return;
 156     }
 157     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature,
 158             jvmti_env, klass, &sig, &generic))) {
 159         result = STATUS_FAILED;
 160         NSK_COMPLAIN0("TEST FAILED: unable to obtain a class signature during SingleStep callback\n\n");
 161         return;
 162     }
 163 
 164     if (sig != NULL) {
 165         NSK_DISPLAY3(
 166             "\tmethod name: \"%s\"\n"
 167             "\tsignature: \"%s\"\n"
 168             "\tmethod declaring class: \"%s\"\n",
 169             methNam, methSig, sig);
 170 
 171         if (stepEv[1] == 1) {
 172             result = STATUS_FAILED;
 173             NSK_COMPLAIN0("TEST FAILED: SingleStep event received after disabling the event generation\n\n");
 174         }
 175         else if ((strcmp(methNam,METHODS[0]) == 0) &&
 176                 (strcmp(methSig,METHOD_SIGS[0]) == 0) &&
 177                 (strcmp(sig,CLASS_SIG) == 0)) {
 178             stepEv[0]++;
 179             NSK_DISPLAY1("CHECK PASSED: SingleStep event received for the method \"%s\" as expected\n",
 180                 methNam);
 181         }
 182         else if ((strcmp(methNam,METHODS[1]) == 0) &&
 183                 (strcmp(methSig,METHOD_SIGS[1]) == 0) &&
 184                 (strcmp(sig,CLASS_SIG) == 0)) {
 185             stepEv[1]++;
 186             NSK_DISPLAY1(
 187                 "CHECK PASSED: SingleStep event received for the method \"%s\" as expected\n"
 188                 "\tdisabling the event generation\n",
 189                 methNam);
 190             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 191                     jvmti_env, JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thread))) {
 192                 result = STATUS_FAILED;
 193                 NSK_COMPLAIN0("TEST FAILED: cannot disable SingleStep events\n\n");
 194             }
 195         }
 196     }
 197 
 198     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
 199             jvmti_env, (unsigned char*) methNam))) {
 200         result = STATUS_FAILED;
 201         NSK_COMPLAIN0("TEST FAILED: unable to deallocate memory pointed to method name\n\n");
 202     }
 203     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
 204             jvmti_env, (unsigned char*) methSig))) {
 205         result = STATUS_FAILED;
 206         NSK_COMPLAIN0("TEST FAILED: unable to deallocate memory pointed to method signature\n\n");
 207     }
 208 
 209     NSK_DISPLAY0("<<<<\n\n");
 210 }
 211 
 212 void JNICALL
 213 VMStart(jvmtiEnv *jvmti_env, JNIEnv* jni_env) {
 214     jvmti->RawMonitorEnter(agent_lock);
 215 
 216     vm_started = 1;
 217 
 218     jvmti->RawMonitorExit(agent_lock);
 219 }
 220 /************************/
 221 
 222 JNIEXPORT jint JNICALL
 223 Java_nsk_jvmti_SingleStep_singlestep001_check(
 224         JNIEnv *env, jobject obj) {


 244 JNIEXPORT jint JNI_OnLoad_singlestep001(JavaVM *jvm, char *options, void *reserved) {
 245     return JNI_VERSION_1_8;
 246 }
 247 #endif
 248 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 249     jvmtiCapabilities caps;
 250 
 251     /* init framework and parse options */
 252     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 253         return JNI_ERR;
 254 
 255     /* create JVMTI environment */
 256     if (!NSK_VERIFY((jvmti =
 257             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 258         return JNI_ERR;
 259 
 260     /* add capability to generate compiled method events */
 261     memset(&caps, 0, sizeof(jvmtiCapabilities));
 262     caps.can_generate_breakpoint_events = 1;
 263     caps.can_generate_single_step_events = 1;
 264     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities,
 265             jvmti, &caps)))
 266         return JNI_ERR;
 267 
 268     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities,
 269             jvmti, &caps)))
 270         return JNI_ERR;
 271 
 272     if (!caps.can_generate_single_step_events)
 273         NSK_DISPLAY0("Warning: generation of single step events is not implemented\n");
 274 
 275     /* set event callback */
 276     NSK_DISPLAY0("setting event callbacks ...\n");
 277     (void) memset(&callbacks, 0, sizeof(callbacks));
 278     callbacks.ClassLoad = &ClassLoad;
 279     callbacks.Breakpoint = &Breakpoint;
 280     callbacks.SingleStep = &SingleStep;
 281     callbacks.VMStart = &VMStart;
 282     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks,
 283             jvmti, &callbacks, sizeof(callbacks))))
 284         return JNI_ERR;
 285 
 286     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
 287     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 288             jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_START, NULL)))
 289         return JNI_ERR;
 290     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 291             jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL)))
 292         return JNI_ERR;
 293     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 294             jvmti, JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
 295         return JNI_ERR;
 296     NSK_DISPLAY0("enabling the events done\n\n");
 297 
 298     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor,
 299             jvmti, "agent lock", &agent_lock)))
 300         return JNI_ERR;
 301 
 302     return JNI_OK;
 303 }
 304 
 305 }


  46 static const char *METHOD_SIGS[] = {
  47     "()V",
  48     "([Ljava/lang/String;Ljava/io/PrintStream;)I"
  49 };
  50 
  51 static volatile long stepEv[] = {0, 0};
  52 
  53 static const char *CLASS_SIG =
  54     "Lnsk/jvmti/SingleStep/singlestep001;";
  55 
  56 static volatile jint result = PASSED;
  57 static jvmtiEnv *jvmti = NULL;
  58 static jvmtiEventCallbacks callbacks;
  59 
  60 static int vm_started = 0;
  61 static jrawMonitorID agent_lock;
  62 
  63 static void setBP(jvmtiEnv *jvmti_env, JNIEnv *env, jclass klass) {
  64     jmethodID mid;
  65 
  66     if (!NSK_JNI_VERIFY(env, (mid = env->GetMethodID(klass, METHODS[0], METHOD_SIGS[0])) != NULL))
  67         env->FatalError("failed to get ID for the java method\n");
  68 
  69     if (!NSK_JVMTI_VERIFY(jvmti_env->SetBreakpoint(mid, 0)))
  70         env->FatalError("failed to set breakpoint\n");




  71 }
  72 
  73 /** callback functions **/
  74 void JNICALL
  75 ClassLoad(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, jclass klass) {
  76     char *sig, *generic;
  77 
  78     jvmti->RawMonitorEnter(agent_lock);
  79 
  80     if (vm_started) {
  81         if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic)))
  82             env->FatalError("failed to obtain a class signature\n");


  83 
  84         if (sig != NULL && (strcmp(sig, CLASS_SIG) == 0)) {
  85             NSK_DISPLAY1(
  86                 "ClassLoad event received for the class \"%s\"\n"
  87                 "\tsetting breakpoint ...\n",
  88                 sig);
  89             setBP(jvmti_env, env, klass);
  90         }
  91     }
  92 
  93     jvmti->RawMonitorExit(agent_lock);
  94 }
  95 
  96 void JNICALL
  97 Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr, jmethodID method,
  98         jlocation loc) {
  99     jclass klass;
 100     char *sig, *generic;
 101 
 102     NSK_DISPLAY0("Breakpoint event received\n");
 103     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &klass)))

 104         NSK_COMPLAIN0("TEST FAILURE: unable to get method declaring class\n\n");
 105 
 106     if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic)))
 107         env->FatalError("Breakpoint: failed to obtain a class signature\n");


 108 
 109     if (sig != NULL && (strcmp(sig, CLASS_SIG) == 0)) {
 110         NSK_DISPLAY1("method declaring class \"%s\"\n\tenabling SingleStep events ...\n",
 111             sig);
 112         if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thr))) {

 113             result = STATUS_FAILED;
 114             NSK_COMPLAIN0("TEST FAILURE: cannot enable SingleStep events\n\n");
 115         }
 116     } else {
 117         result = STATUS_FAILED;
 118         NSK_COMPLAIN1("TEST FAILURE: unexpected breakpoint event in method of class \"%s\"\n\n",
 119             sig);
 120     }
 121 }
 122 
 123 void JNICALL
 124 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 125         jmethodID method, jlocation location) {
 126     jclass klass;
 127     char *sig, *generic, *methNam, *methSig;
 128 
 129     if (result == STATUS_FAILED) {
 130         return;
 131     }
 132 
 133     NSK_DISPLAY0(">>>> SingleStep event received\n");
 134 
 135     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &methNam, &methSig, NULL))) {

 136         result = STATUS_FAILED;
 137         NSK_COMPLAIN0("TEST FAILED: unable to get method name during SingleStep callback\n\n");
 138         return;
 139     }
 140     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &klass))) {

 141         result = STATUS_FAILED;
 142         NSK_COMPLAIN0("TEST FAILED: unable to get method declaring class during SingleStep callback\n\n");
 143         return;
 144     }
 145     if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic))) {

 146         result = STATUS_FAILED;
 147         NSK_COMPLAIN0("TEST FAILED: unable to obtain a class signature during SingleStep callback\n\n");
 148         return;
 149     }
 150 
 151     if (sig != NULL) {
 152         NSK_DISPLAY3(
 153             "\tmethod name: \"%s\"\n"
 154             "\tsignature: \"%s\"\n"
 155             "\tmethod declaring class: \"%s\"\n",
 156             methNam, methSig, sig);
 157 
 158         if (stepEv[1] == 1) {
 159             result = STATUS_FAILED;
 160             NSK_COMPLAIN0("TEST FAILED: SingleStep event received after disabling the event generation\n\n");
 161         }
 162         else if ((strcmp(methNam,METHODS[0]) == 0) &&
 163                 (strcmp(methSig,METHOD_SIGS[0]) == 0) &&
 164                 (strcmp(sig,CLASS_SIG) == 0)) {
 165             stepEv[0]++;
 166             NSK_DISPLAY1("CHECK PASSED: SingleStep event received for the method \"%s\" as expected\n",
 167                 methNam);
 168         }
 169         else if ((strcmp(methNam,METHODS[1]) == 0) &&
 170                 (strcmp(methSig,METHOD_SIGS[1]) == 0) &&
 171                 (strcmp(sig,CLASS_SIG) == 0)) {
 172             stepEv[1]++;
 173             NSK_DISPLAY1(
 174                 "CHECK PASSED: SingleStep event received for the method \"%s\" as expected\n"
 175                 "\tdisabling the event generation\n",
 176                 methNam);
 177             if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thread))) {

 178                 result = STATUS_FAILED;
 179                 NSK_COMPLAIN0("TEST FAILED: cannot disable SingleStep events\n\n");
 180             }
 181         }
 182     }
 183 
 184     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) methNam))) {

 185         result = STATUS_FAILED;
 186         NSK_COMPLAIN0("TEST FAILED: unable to deallocate memory pointed to method name\n\n");
 187     }
 188     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) methSig))) {

 189         result = STATUS_FAILED;
 190         NSK_COMPLAIN0("TEST FAILED: unable to deallocate memory pointed to method signature\n\n");
 191     }
 192 
 193     NSK_DISPLAY0("<<<<\n\n");
 194 }
 195 
 196 void JNICALL
 197 VMStart(jvmtiEnv *jvmti_env, JNIEnv* jni_env) {
 198     jvmti->RawMonitorEnter(agent_lock);
 199 
 200     vm_started = 1;
 201 
 202     jvmti->RawMonitorExit(agent_lock);
 203 }
 204 /************************/
 205 
 206 JNIEXPORT jint JNICALL
 207 Java_nsk_jvmti_SingleStep_singlestep001_check(
 208         JNIEnv *env, jobject obj) {


 228 JNIEXPORT jint JNI_OnLoad_singlestep001(JavaVM *jvm, char *options, void *reserved) {
 229     return JNI_VERSION_1_8;
 230 }
 231 #endif
 232 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 233     jvmtiCapabilities caps;
 234 
 235     /* init framework and parse options */
 236     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 237         return JNI_ERR;
 238 
 239     /* create JVMTI environment */
 240     if (!NSK_VERIFY((jvmti =
 241             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 242         return JNI_ERR;
 243 
 244     /* add capability to generate compiled method events */
 245     memset(&caps, 0, sizeof(jvmtiCapabilities));
 246     caps.can_generate_breakpoint_events = 1;
 247     caps.can_generate_single_step_events = 1;
 248     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))

 249         return JNI_ERR;
 250 
 251     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))

 252         return JNI_ERR;
 253 
 254     if (!caps.can_generate_single_step_events)
 255         NSK_DISPLAY0("Warning: generation of single step events is not implemented\n");
 256 
 257     /* set event callback */
 258     NSK_DISPLAY0("setting event callbacks ...\n");
 259     (void) memset(&callbacks, 0, sizeof(callbacks));
 260     callbacks.ClassLoad = &ClassLoad;
 261     callbacks.Breakpoint = &Breakpoint;
 262     callbacks.SingleStep = &SingleStep;
 263     callbacks.VMStart = &VMStart;
 264     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))

 265         return JNI_ERR;
 266 
 267     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
 268     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_START, NULL)))

 269         return JNI_ERR;
 270     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL)))

 271         return JNI_ERR;
 272     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))

 273         return JNI_ERR;
 274     NSK_DISPLAY0("enabling the events done\n\n");
 275 
 276     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("agent lock", &agent_lock)))

 277         return JNI_ERR;
 278 
 279     return JNI_OK;
 280 }
 281 
 282 }
< prev index next >