1 /* 2 * Copyright (c) 2004, 2018, 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 #include "agent_common.h" 27 #include "jni_tools.h" 28 #include "jvmti_tools.h" 29 #include "JVMTITools.h" 30 31 extern "C" { 32 33 /* ============================================================================= */ 34 35 /* scaffold objects */ 36 static JNIEnv* jni = NULL; 37 static jvmtiEnv *jvmti = NULL; 38 static jlong timeout = 0; 39 static jrawMonitorID syncLock = NULL; 40 41 /* constant names */ 42 #define JVMTI_EVENT_COUNT (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1) 43 #define EXPECTED_CLASS_NAME "nsk.jvmti.scenarios.events.EM06.em06t001a" 44 #define CLASS_LOADER_COUNT_PARAM "classLoaderCount" 45 46 static int classLoaderCount = 0; 47 static int classloadEventCount = 0; 48 static int classprepareEventCount = 0; 49 50 /* ============================================================================= */ 51 52 /* callbacks */ 53 54 void 55 handler(jvmtiEvent event, jvmtiEnv* jvmti, JNIEnv* jni_env, 56 jthread thread, jclass klass) { 57 58 jmethodID methodID; 59 jclass classObject; 60 jstring jclassName; 61 const char *className; 62 63 if (!NSK_JNI_VERIFY(jni_env, (classObject = 64 NSK_CPP_STUB2(GetObjectClass, jni_env, klass)) != NULL)) { 65 nsk_jvmti_setFailStatus(); 66 return; 67 } 68 69 if (!NSK_JNI_VERIFY(jni_env, (methodID = 70 NSK_CPP_STUB4(GetMethodID, jni_env, classObject, 71 "getName", "()Ljava/lang/String;")) != NULL)) { 72 nsk_jvmti_setFailStatus(); 73 return; 74 } 75 76 jclassName = (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) NSK_CPP_STUB3(CallObjectMethod, jni_env, klass, 77 methodID); 78 79 className = NSK_CPP_STUB3(GetStringUTFChars, jni_env, jclassName, 0); 80 81 if ( className != NULL && (strcmp(className, EXPECTED_CLASS_NAME)==0) ) { 82 83 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock))) 84 nsk_jvmti_setFailStatus(); 85 86 switch (event) { 87 case JVMTI_EVENT_CLASS_LOAD: 88 classloadEventCount++; break; 89 case JVMTI_EVENT_CLASS_PREPARE: 90 classprepareEventCount++; break; 91 default: 92 NSK_COMPLAIN1("Unexpected event %s", TranslateEvent(event)); 93 nsk_jvmti_setFailStatus(); 94 } 95 96 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock))) 97 nsk_jvmti_setFailStatus(); 98 99 } 100 101 NSK_CPP_STUB3(ReleaseStringUTFChars, jni_env, jclassName, className); 102 } 103 104 JNIEXPORT void JNICALL 105 cbClassLoad(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) { 106 107 handler(JVMTI_EVENT_CLASS_LOAD, jvmti, jni_env, thread, klass); 108 } 109 110 JNIEXPORT void JNICALL 111 cbClassPrepare(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) { 112 113 handler(JVMTI_EVENT_CLASS_PREPARE, jvmti, jni_env, thread, klass); 114 } 115 116 /* ============================================================================= */ 117 118 static int 119 enableEvent(jvmtiEventMode enable, jvmtiEvent event) { 120 if (!NSK_JVMTI_VERIFY( 121 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, enable, 122 event, NULL))) { 123 nsk_jvmti_setFailStatus(); 124 return NSK_FALSE; 125 } 126 127 return NSK_TRUE; 128 } 129 130 /* ============================================================================= */ 131 132 /** 133 * Testcase: check tested events. 134 * - check if expected events received for each method 135 * 136 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break. 137 */ 138 int checkEvents() { 139 140 int result = NSK_TRUE; 141 142 if (classloadEventCount == classLoaderCount) { 143 NSK_DISPLAY1("Expected number of JVMTI_EVENT_CLASS_LOAD events %d\n", 144 classloadEventCount); 145 } else { 146 NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_LOAD events %d\n\texpected value %d\n", 147 classloadEventCount, 148 classLoaderCount); 149 result = NSK_FALSE; 150 } 151 152 if (classprepareEventCount == classLoaderCount) { 153 NSK_DISPLAY1("Expected number of JVMTI_EVENT_CLASS_PREPARE events %d\n", 154 classloadEventCount); 155 } else { 156 NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_PREPARE events %d\n\texpected value %d\n", 157 classprepareEventCount, 158 classLoaderCount); 159 result = NSK_FALSE; 160 } 161 162 return result; 163 } 164 165 /* ============================================================================= */ 166 167 static int 168 setCallBacks() { 169 jvmtiEventCallbacks eventCallbacks; 170 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 171 172 eventCallbacks.ClassLoad = cbClassLoad; 173 eventCallbacks.ClassPrepare = cbClassPrepare; 174 175 if (!NSK_JVMTI_VERIFY( 176 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 177 &eventCallbacks, 178 sizeof(eventCallbacks)))) 179 return NSK_FALSE; 180 181 return NSK_TRUE; 182 } 183 184 /* ============================================================================= */ 185 186 /** Agent algorithm. */ 187 static void JNICALL 188 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) { 189 190 if (!NSK_JVMTI_VERIFY( 191 NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) { 192 nsk_jvmti_setFailStatus(); 193 return; 194 } 195 196 jni = agentJNI; 197 198 NSK_DISPLAY0("Wait for debuggee to become ready\n"); 199 if (!nsk_jvmti_waitForSync(timeout)) 200 return; 201 202 if (!setCallBacks()) { 203 return; 204 } 205 206 if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD) 207 || !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE)) { 208 NSK_COMPLAIN0("Events could not be enabled"); 209 nsk_jvmti_setFailStatus(); 210 return; 211 } 212 213 NSK_DISPLAY0("Let debuggee to load class\n"); 214 if (!nsk_jvmti_resumeSync()) 215 return; 216 217 if (!nsk_jvmti_waitForSync(timeout)) 218 return; 219 220 if (!checkEvents()) { 221 nsk_jvmti_setFailStatus(); 222 } 223 224 if (!enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_LOAD) 225 || !enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE)) { 226 NSK_COMPLAIN0("Events could not be disabled"); 227 nsk_jvmti_setFailStatus(); 228 } 229 230 NSK_DISPLAY0("Let debuggee to finish\n"); 231 if (!nsk_jvmti_resumeSync()) 232 return; 233 234 if (!NSK_JVMTI_VERIFY( 235 NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock))) 236 nsk_jvmti_setFailStatus(); 237 238 } 239 240 /* ============================================================================= */ 241 242 /** Agent library initialization. */ 243 #ifdef STATIC_BUILD 244 JNIEXPORT jint JNICALL Agent_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) { 245 return Agent_Initialize(jvm, options, reserved); 246 } 247 JNIEXPORT jint JNICALL Agent_OnAttach_em06t001(JavaVM *jvm, char *options, void *reserved) { 248 return Agent_Initialize(jvm, options, reserved); 249 } 250 JNIEXPORT jint JNI_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) { 251 return JNI_VERSION_1_8; 252 } 253 #endif 254 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 255 256 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 257 return JNI_ERR; 258 259 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 260 classLoaderCount = nsk_jvmti_findOptionIntValue(CLASS_LOADER_COUNT_PARAM, 100); 261 262 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 263 return JNI_ERR; 264 265 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 266 return JNI_ERR; 267 268 return JNI_OK; 269 } 270 271 /* ============================================================================= */ 272 273 274 }