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(jvmti_env->GetMethodName(method, &name, &signature, NULL))) { 58 nsk_jvmti_setFailStatus(); 59 return; 60 } 61 NSK_DISPLAY3("CompiledMethodLoad event: %s%s (0x%p)\n", 62 name, signature, code_addr); 63 if (name != NULL) 64 jvmti_env->Deallocate((unsigned char*)name); 65 if (signature != NULL) 66 jvmti_env->Deallocate((unsigned char*)signature); 67 } 68 69 static void JNICALL 70 CompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method, 71 const void* code_addr) { 72 char *name = NULL; 73 char *sig = NULL; 74 jvmtiError err; 75 CompiledMethodUnloadEventsCount++; 76 77 NSK_DISPLAY0("CompiledMethodUnload event received\n"); 78 // Check for the case that the class has been unloaded 79 err = jvmti_env->GetMethodName(method, &name, &sig, NULL); 80 if (err == JVMTI_ERROR_NONE) { 81 NSK_DISPLAY3("for: \tmethod: name=\"%s\" signature=\"%s\"\n\tnative address=0x%p\n", 82 name, sig, code_addr); 83 jvmti_env->Deallocate((unsigned char*)name); 84 jvmti_env->Deallocate((unsigned char*)sig); 85 } 86 } 87 88 /* ========================================================================== */ 89 90 /** Agent algorithm. */ 91 static void JNICALL 92 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 93 94 if (!nsk_jvmti_waitForSync(timeout)) 95 return; 96 97 NSK_DISPLAY1("CompiledMethodLoad events received: %d\n", 98 CompiledMethodLoadEventsCount); 99 if (CompiledMethodLoadEventsCount == 0) { 100 NSK_DISPLAY0("# WARNING: no CompiledMethodLoad events\n"); 101 NSK_DISPLAY0("# (VM might not compile any methods at all)\n"); 102 } 103 104 NSK_DISPLAY1("CompiledMethodUnload events received: %d\n", 105 CompiledMethodUnloadEventsCount); 106 if (CompiledMethodUnloadEventsCount == 0) { 107 NSK_DISPLAY0("# WARNING: no CompiledMethodUnload events\n"); 108 NSK_DISPLAY0("# (VM might not compile any methods at all)\n"); 109 } 110 111 if (!nsk_jvmti_resumeSync()) 112 return; 113 } 114 115 /* ========================================================================== */ 116 117 /** Agent library initialization. */ 118 #ifdef STATIC_BUILD 119 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t006(JavaVM *jvm, char *options, void *reserved) { 120 return Agent_Initialize(jvm, options, reserved); 121 } 122 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t006(JavaVM *jvm, char *options, void *reserved) { 123 return Agent_Initialize(jvm, options, reserved); 124 } 125 JNIEXPORT jint JNI_OnLoad_ma10t006(JavaVM *jvm, char *options, void *reserved) { 126 return JNI_VERSION_1_8; 127 } 128 #endif 129 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 130 jvmtiEnv* jvmti = NULL; 131 jvmtiCapabilities caps; 132 jvmtiEventCallbacks callbacks; 133 134 NSK_DISPLAY0("Agent_OnLoad\n"); 135 136 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 137 return JNI_ERR; 138 139 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 140 141 if (!NSK_VERIFY((jvmti = 142 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 143 return JNI_ERR; 144 145 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 146 return JNI_ERR; 147 148 memset(&caps, 0, sizeof(caps)); 149 caps.can_generate_compiled_method_load_events = 1; 150 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 151 return JNI_ERR; 152 } 153 154 memset(&callbacks, 0, sizeof(callbacks)); 155 callbacks.CompiledMethodLoad = &CompiledMethodLoad; 156 callbacks.CompiledMethodUnload = &CompiledMethodUnload; 157 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 158 return JNI_ERR; 159 160 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL))) 161 return JNI_ERR; 162 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL))) 163 return JNI_ERR; 164 165 return JNI_OK; 166 } 167 168 /* ========================================================================== */ 169 170 }