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 <stdlib.h> 26 #include "jvmti.h" 27 #include "agent_common.h" 28 #include "jni_tools.h" 29 #include "jvmti_tools.h" 30 #include "JVMTITools.h" 31 #include "nsk_list.h" 32 33 extern "C" { 34 35 /* ============================================================================= */ 36 37 /* scaffold objects */ 38 static jvmtiEnv *jvmti = NULL; 39 static jlong timeout = 0; 40 static jrawMonitorID syncLock = NULL; 41 42 static int methodLoadCount = 0; 43 static int methodUnloadCount = 0; 44 45 #define NAME_LENGTH 50 46 const void *plist = NULL; 47 48 typedef struct nsk_jvmti_CompiledMethodIDStruct { 49 jmethodID method; 50 const void* code_addr; 51 char name[NAME_LENGTH]; 52 } nsk_jvmti_CompiledMethod; 53 54 55 /* ============================================================================= */ 56 57 /* callbacks */ 58 void JNICALL 59 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size, 60 const void* code_addr, jint map_length, 61 const jvmtiAddrLocationMap* map, const void* compile_info) { 62 char *name; 63 char *sign; 64 char *genc; 65 66 if (!NSK_JVMTI_VERIFY( 67 NSK_CPP_STUB5( 68 GetMethodName, jvmti_env, method, &name, &sign, &genc))) { 69 nsk_jvmti_setFailStatus(); 70 return; 71 } 72 73 if (!strncmp(name,"javaMethod", 8)) { 74 nsk_jvmti_CompiledMethod *rec = 75 (nsk_jvmti_CompiledMethod *)malloc(sizeof(nsk_jvmti_CompiledMethod)); 76 77 rec->method = method; 78 rec->code_addr = code_addr; 79 strncpy(rec->name, name, NAME_LENGTH); 80 rec->name[NAME_LENGTH - 1] = '\0'; 81 82 if (!NSK_VERIFY(nsk_list_add(plist, rec))) { 83 nsk_jvmti_setFailStatus(); 84 free((void *)rec); 85 } else { 86 NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_LOAD received for\n"); 87 NSK_DISPLAY1("\t\tmethod: %s\n", rec->name); 88 89 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock))) 90 nsk_jvmti_setFailStatus(); 91 92 methodLoadCount++; 93 94 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock))) 95 nsk_jvmti_setFailStatus(); 96 97 } 98 } 99 100 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 101 jvmti_env, (unsigned char*)name))) { 102 nsk_jvmti_setFailStatus(); 103 } 104 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 105 jvmti_env, (unsigned char*)sign))) { 106 nsk_jvmti_setFailStatus(); 107 } 108 if (genc != NULL) 109 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 110 jvmti_env, (unsigned char*)genc))) { 111 nsk_jvmti_setFailStatus(); 112 } 113 } 114 115 void JNICALL 116 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method, 117 const void* code_addr) { 118 119 nsk_jvmti_CompiledMethod *rec; 120 121 int count = nsk_list_getCount(plist); 122 int i; 123 124 for (i = 0; i < count; i ++) { 125 rec = (nsk_jvmti_CompiledMethod *)nsk_list_get(plist, i); 126 if ((rec->code_addr == code_addr) && (rec->method == method)) { 127 NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_UNLOAD received for\n"); 128 NSK_DISPLAY1("\t\tmethod: %s\n", rec->name); 129 130 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock))) 131 nsk_jvmti_setFailStatus(); 132 133 methodUnloadCount++; 134 135 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock))) 136 nsk_jvmti_setFailStatus(); 137 138 free(rec); 139 nsk_list_remove(plist, i); 140 return; 141 } 142 143 } 144 145 } 146 147 /* ============================================================================= */ 148 149 static int 150 enableEvent(jvmtiEventMode enable, jvmtiEvent event) { 151 if (!NSK_JVMTI_VERIFY( 152 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, enable, 153 event, NULL))) { 154 nsk_jvmti_setFailStatus(); 155 return NSK_FALSE; 156 } 157 158 return NSK_TRUE; 159 } 160 161 int checkEvents() { 162 163 int result = methodUnloadCount <= methodLoadCount; 164 165 if (result) { 166 NSK_DISPLAY0("Received correct number of events:\n"); 167 NSK_DISPLAY1("\t\tCOMPILED_METHOD_LOAD events number = %d\n", 168 methodLoadCount); 169 NSK_DISPLAY1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n", 170 methodUnloadCount); 171 } else { 172 NSK_COMPLAIN0("Received incorrect number of events:\n"); 173 NSK_COMPLAIN1("\t\tCOMPILED_METHOD_LOAD events number = %d\n", 174 methodLoadCount); 175 NSK_COMPLAIN1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n", 176 methodUnloadCount); 177 } 178 179 return result; 180 } 181 182 /* ============================================================================= */ 183 184 static int 185 setCallBacks() { 186 187 jvmtiEventCallbacks eventCallbacks; 188 189 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 190 191 eventCallbacks.CompiledMethodLoad = cbCompiledMethodLoad; 192 eventCallbacks.CompiledMethodUnload = cbCompiledMethodUnload; 193 194 return NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, 195 &eventCallbacks, 196 sizeof(eventCallbacks))); 197 } 198 199 /* ============================================================================= */ 200 201 /** Agent algorithm. */ 202 static void JNICALL 203 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) { 204 205 int i; 206 207 int attempts = nsk_jvmti_findOptionIntValue("attempts", 1); 208 209 for (i = 0; i < attempts; i++) { 210 211 if (!nsk_jvmti_waitForSync(timeout)) 212 return; 213 214 if (!checkEvents()) 215 nsk_jvmti_setFailStatus(); 216 217 NSK_DISPLAY0("Let debuggee to continue\n"); 218 if (!nsk_jvmti_resumeSync()) 219 return; 220 } 221 222 { 223 int count = nsk_list_getCount(plist); 224 225 while (count > 0) { 226 free((void *)nsk_list_get(plist, 0)); 227 nsk_list_remove(plist, 0); 228 count = nsk_list_getCount(plist); 229 } 230 231 } 232 233 if (!NSK_JVMTI_VERIFY( 234 NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock))) 235 nsk_jvmti_setFailStatus(); 236 237 } 238 239 /* ============================================================================= */ 240 241 /** Agent library initialization. */ 242 #ifdef STATIC_BUILD 243 JNIEXPORT jint JNICALL Agent_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) { 244 return Agent_Initialize(jvm, options, reserved); 245 } 246 JNIEXPORT jint JNICALL Agent_OnAttach_em07t002(JavaVM *jvm, char *options, void *reserved) { 247 return Agent_Initialize(jvm, options, reserved); 248 } 249 JNIEXPORT jint JNI_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) { 250 return JNI_VERSION_1_8; 251 } 252 #endif 253 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 254 255 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 256 return JNI_ERR; 257 258 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 259 260 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 261 return JNI_ERR; 262 263 if (!NSK_JVMTI_VERIFY( 264 NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) { 265 nsk_jvmti_setFailStatus(); 266 return JNI_ERR; 267 } 268 269 if (!NSK_VERIFY((plist = (const void *)nsk_list_create()) != NULL)) 270 return JNI_ERR; 271 272 { 273 jvmtiCapabilities caps; 274 memset(&caps, 0, sizeof(caps)); 275 276 caps.can_generate_compiled_method_load_events = 1; 277 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) 278 return JNI_ERR; 279 } 280 281 if (!setCallBacks()) { 282 return JNI_ERR; 283 } 284 285 if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD) || 286 !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) { 287 return JNI_ERR; 288 } 289 290 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 291 return JNI_ERR; 292 293 return JNI_OK; 294 } 295 296 /* ============================================================================= */ 297 298 299 }