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 <stdlib.h> 25 #include <string.h> 26 #include "jni_tools.h" 27 #include "agent_common.h" 28 #include "jvmti_tools.h" 29 30 #define PASSED 0 31 #define STATUS_FAILED 2 32 33 extern "C" { 34 35 /* ========================================================================== */ 36 37 /* scaffold objects */ 38 static jlong timeout = 0; 39 40 /* event counts */ 41 static int CompiledMethodLoadEventsCount = 0; 42 static int CompiledMethodUnloadEventsCount = 0; 43 44 /* ========================================================================== */ 45 46 /** callback functions **/ 47 48 static void JNICALL 49 CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, 50 jint code_size, const void* code_addr, jint map_length, 51 const jvmtiAddrLocationMap* map, const void* compile_info) { 52 char *name = NULL; 53 char *signature = NULL; 54 55 CompiledMethodLoadEventsCount++; 56 57 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, 58 jvmti_env, method, &name, &signature, NULL))) { 59 nsk_jvmti_setFailStatus(); 60 return; 61 } 62 NSK_DISPLAY3("CompiledMethodLoad event: %s%s (0x%p)\n", 63 name, signature, code_addr); 64 if (name != NULL) 65 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name); 66 if (signature != NULL) 67 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature); 68 } 69 70 static void JNICALL 71 CompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method, 72 const void* code_addr) { 73 char *name = NULL; 74 char *sig = NULL; 75 jvmtiError err; 76 CompiledMethodUnloadEventsCount++; 77 78 NSK_DISPLAY0("CompiledMethodUnload event received\n"); 79 // Check for the case that the class has been unloaded 80 err = jvmti_env->GetMethodName(method, &name, &sig, NULL); 81 if (err == JVMTI_ERROR_NONE) { 82 NSK_DISPLAY3("for: \tmethod: name=\"%s\" signature=\"%s\"\n\tnative address=0x%p\n", 83 name, sig, code_addr); 84 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name); 85 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)sig); 86 } 87 } 88 89 /* ========================================================================== */ 90 91 /** Agent algorithm. */ 92 static void JNICALL 93 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 94 95 if (!nsk_jvmti_waitForSync(timeout)) 96 return; 97 98 NSK_DISPLAY1("CompiledMethodLoad events received: %d\n", 99 CompiledMethodLoadEventsCount); 100 if (CompiledMethodLoadEventsCount == 0) { 101 NSK_DISPLAY0("# WARNING: no CompiledMethodLoad events\n"); 102 NSK_DISPLAY0("# (VM might not compile any methods at all)\n"); 103 } 104 105 NSK_DISPLAY1("CompiledMethodUnload events received: %d\n", 106 CompiledMethodUnloadEventsCount); 107 if (CompiledMethodUnloadEventsCount == 0) { 108 NSK_DISPLAY0("# WARNING: no CompiledMethodUnload events\n"); 109 NSK_DISPLAY0("# (VM might not compile any methods at all)\n"); 110 } 111 112 if (!nsk_jvmti_resumeSync()) 113 return; 114 } 115 116 /* ========================================================================== */ 117 118 /** Agent library initialization. */ 119 #ifdef STATIC_BUILD 120 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t006(JavaVM *jvm, char *options, void *reserved) { 121 return Agent_Initialize(jvm, options, reserved); 122 } 123 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t006(JavaVM *jvm, char *options, void *reserved) { 124 return Agent_Initialize(jvm, options, reserved); 125 } 126 JNIEXPORT jint JNI_OnLoad_ma10t006(JavaVM *jvm, char *options, void *reserved) { 127 return JNI_VERSION_1_8; 128 } 129 #endif 130 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 131 jvmtiEnv* jvmti = NULL; 132 jvmtiCapabilities caps; 133 jvmtiEventCallbacks callbacks; 134 135 NSK_DISPLAY0("Agent_OnLoad\n"); 136 137 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 138 return JNI_ERR; 139 140 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 141 142 if (!NSK_VERIFY((jvmti = 143 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 144 return JNI_ERR; 145 146 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 147 return JNI_ERR; 148 149 memset(&caps, 0, sizeof(caps)); 150 caps.can_generate_compiled_method_load_events = 1; 151 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { 152 return JNI_ERR; 153 } 154 155 memset(&callbacks, 0, sizeof(callbacks)); 156 callbacks.CompiledMethodLoad = &CompiledMethodLoad; 157 callbacks.CompiledMethodUnload = &CompiledMethodUnload; 158 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 159 return JNI_ERR; 160 161 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 162 jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL))) 163 return JNI_ERR; 164 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 165 jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL))) 166 return JNI_ERR; 167 168 return JNI_OK; 169 } 170 171 /* ========================================================================== */ 172 173 }