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