1 /*
   2  * Copyright (c) 2003, 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 <stdio.h>
  25 #include <string.h>
  26 #include <jvmti.h>
  27 #include "agent_common.h"
  28 
  29 #include "nsk_tools.h"
  30 #include "JVMTITools.h"
  31 #include "jvmti_tools.h"
  32 #include "jni_tools.h"
  33 
  34 extern "C" {
  35 
  36 #define STATUS_FAILED 2
  37 #define PASSED 0
  38 
  39 #define METH_NUM 2
  40 
  41 static const char *METHODS[] = {
  42     "bpMethod",
  43     "runThis"
  44 };
  45 
  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 volatile int callbacksEnabled = NSK_FALSE;
  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 (callbacksEnabled) {
  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     jvmti->RawMonitorEnter(agent_lock);
 103 
 104     if (!callbacksEnabled) {
 105         jvmti->RawMonitorExit(agent_lock);
 106         return;
 107     }
 108 
 109     NSK_DISPLAY0("Breakpoint event received\n");
 110     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &klass)))
 111         NSK_COMPLAIN0("TEST FAILURE: unable to get method declaring class\n\n");
 112 
 113     if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic)))
 114         env->FatalError("Breakpoint: failed to obtain a class signature\n");
 115 
 116     if (sig != NULL && (strcmp(sig, CLASS_SIG) == 0)) {
 117         NSK_DISPLAY1("method declaring class \"%s\"\n\tenabling SingleStep events ...\n",
 118             sig);
 119         if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thr))) {
 120             result = STATUS_FAILED;
 121             NSK_COMPLAIN0("TEST FAILURE: cannot enable SingleStep events\n\n");
 122         }
 123     } else {
 124         result = STATUS_FAILED;
 125         NSK_COMPLAIN1("TEST FAILURE: unexpected breakpoint event in method of class \"%s\"\n\n",
 126             sig);
 127     }
 128     jvmti->RawMonitorExit(agent_lock);
 129 }
 130 
 131 void JNICALL
 132 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 133         jmethodID method, jlocation location) {
 134     jclass klass;
 135     char *sig, *generic, *methNam, *methSig;
 136 
 137     if (result == STATUS_FAILED) {
 138         return;
 139     }
 140 
 141     NSK_DISPLAY0(">>>> SingleStep event received\n");
 142 
 143     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &methNam, &methSig, NULL))) {
 144         result = STATUS_FAILED;
 145         NSK_COMPLAIN0("TEST FAILED: unable to get method name during SingleStep callback\n\n");
 146         return;
 147     }
 148     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &klass))) {
 149         result = STATUS_FAILED;
 150         NSK_COMPLAIN0("TEST FAILED: unable to get method declaring class during SingleStep callback\n\n");
 151         return;
 152     }
 153     if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic))) {
 154         result = STATUS_FAILED;
 155         NSK_COMPLAIN0("TEST FAILED: unable to obtain a class signature during SingleStep callback\n\n");
 156         return;
 157     }
 158 
 159     if (sig != NULL) {
 160         NSK_DISPLAY3(
 161             "\tmethod name: \"%s\"\n"
 162             "\tsignature: \"%s\"\n"
 163             "\tmethod declaring class: \"%s\"\n",
 164             methNam, methSig, sig);
 165 
 166         if (stepEv[1] == 1) {
 167             result = STATUS_FAILED;
 168             NSK_COMPLAIN0("TEST FAILED: SingleStep event received after disabling the event generation\n\n");
 169         }
 170         else if ((strcmp(methNam,METHODS[0]) == 0) &&
 171                 (strcmp(methSig,METHOD_SIGS[0]) == 0) &&
 172                 (strcmp(sig,CLASS_SIG) == 0)) {
 173             stepEv[0]++;
 174             NSK_DISPLAY1("CHECK PASSED: SingleStep event received for the method \"%s\" as expected\n",
 175                 methNam);
 176         }
 177         else if ((strcmp(methNam,METHODS[1]) == 0) &&
 178                 (strcmp(methSig,METHOD_SIGS[1]) == 0) &&
 179                 (strcmp(sig,CLASS_SIG) == 0)) {
 180             stepEv[1]++;
 181             NSK_DISPLAY1(
 182                 "CHECK PASSED: SingleStep event received for the method \"%s\" as expected\n"
 183                 "\tdisabling the event generation\n",
 184                 methNam);
 185             if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thread))) {
 186                 result = STATUS_FAILED;
 187                 NSK_COMPLAIN0("TEST FAILED: cannot disable SingleStep events\n\n");
 188             }
 189         }
 190     }
 191 
 192     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) methNam))) {
 193         result = STATUS_FAILED;
 194         NSK_COMPLAIN0("TEST FAILED: unable to deallocate memory pointed to method name\n\n");
 195     }
 196     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) methSig))) {
 197         result = STATUS_FAILED;
 198         NSK_COMPLAIN0("TEST FAILED: unable to deallocate memory pointed to method signature\n\n");
 199     }
 200 
 201     NSK_DISPLAY0("<<<<\n\n");
 202 }
 203 
 204 void JNICALL
 205 VMStart(jvmtiEnv *jvmti_env, JNIEnv* jni_env) {
 206     jvmti->RawMonitorEnter(agent_lock);
 207 
 208     callbacksEnabled = NSK_TRUE;
 209 
 210     jvmti->RawMonitorExit(agent_lock);
 211 }
 212 
 213 void JNICALL
 214 VMDeath(jvmtiEnv *jvmti_env, JNIEnv* jni_env) {
 215     jvmti->RawMonitorEnter(agent_lock);
 216 
 217     callbacksEnabled = NSK_FALSE;
 218 
 219     jvmti->RawMonitorExit(agent_lock);
 220 }
 221 /************************/
 222 
 223 JNIEXPORT jint JNICALL
 224 Java_nsk_jvmti_SingleStep_singlestep001_check(
 225         JNIEnv *env, jobject obj) {
 226     int i;
 227 
 228     for (i=0; i<METH_NUM; i++)
 229         if (stepEv[i] == 0) {
 230             result = STATUS_FAILED;
 231             NSK_COMPLAIN1("TEST FAILED: no SingleStep events for the method \"%s\"\n\n",
 232                 METHODS[i]);
 233         }
 234 
 235     return result;
 236 }
 237 
 238 #ifdef STATIC_BUILD
 239 JNIEXPORT jint JNICALL Agent_OnLoad_singlestep001(JavaVM *jvm, char *options, void *reserved) {
 240     return Agent_Initialize(jvm, options, reserved);
 241 }
 242 JNIEXPORT jint JNICALL Agent_OnAttach_singlestep001(JavaVM *jvm, char *options, void *reserved) {
 243     return Agent_Initialize(jvm, options, reserved);
 244 }
 245 JNIEXPORT jint JNI_OnLoad_singlestep001(JavaVM *jvm, char *options, void *reserved) {
 246     return JNI_VERSION_1_8;
 247 }
 248 #endif
 249 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 250     jvmtiCapabilities caps;
 251 
 252     /* init framework and parse options */
 253     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 254         return JNI_ERR;
 255 
 256     /* create JVMTI environment */
 257     if (!NSK_VERIFY((jvmti =
 258             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 259         return JNI_ERR;
 260 
 261     /* add capability to generate compiled method events */
 262     memset(&caps, 0, sizeof(jvmtiCapabilities));
 263     caps.can_generate_breakpoint_events = 1;
 264     caps.can_generate_single_step_events = 1;
 265     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 266         return JNI_ERR;
 267 
 268     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
 269         return JNI_ERR;
 270 
 271     if (!caps.can_generate_single_step_events)
 272         NSK_DISPLAY0("Warning: generation of single step events is not implemented\n");
 273 
 274     /* set event callback */
 275     NSK_DISPLAY0("setting event callbacks ...\n");
 276     (void) memset(&callbacks, 0, sizeof(callbacks));
 277     callbacks.ClassLoad = &ClassLoad;
 278     callbacks.Breakpoint = &Breakpoint;
 279     callbacks.SingleStep = &SingleStep;
 280     callbacks.VMStart = &VMStart;
 281     callbacks.VMDeath = &VMDeath;
 282     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
 283         return JNI_ERR;
 284 
 285     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
 286     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_START, NULL)))
 287         return JNI_ERR;
 288     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))
 289         return JNI_ERR;
 290     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL)))
 291         return JNI_ERR;
 292     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
 293         return JNI_ERR;
 294     NSK_DISPLAY0("enabling the events done\n\n");
 295 
 296     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("agent lock", &agent_lock)))
 297         return JNI_ERR;
 298 
 299     return JNI_OK;
 300 }
 301 
 302 }