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 
  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         jvmti_env->Deallocate((unsigned char*)name);
  85         jvmti_env->Deallocate((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 (!NSK_VERIFY(CompiledMethodLoadEventsCount == 0))
 101         nsk_jvmti_setFailStatus();
 102 
 103     NSK_DISPLAY1("CompiledMethodUnload events received: %d\n",
 104         CompiledMethodUnloadEventsCount);
 105     if (!NSK_VERIFY(CompiledMethodUnloadEventsCount == 0))
 106         nsk_jvmti_setFailStatus();
 107 
 108     if (!nsk_jvmti_resumeSync())
 109         return;
 110 }
 111 
 112 /* ========================================================================== */
 113 
 114 /** Agent library initialization. */
 115 #ifdef STATIC_BUILD
 116 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t006a(JavaVM *jvm, char *options, void *reserved) {
 117     return Agent_Initialize(jvm, options, reserved);
 118 }
 119 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t006a(JavaVM *jvm, char *options, void *reserved) {
 120     return Agent_Initialize(jvm, options, reserved);
 121 }
 122 JNIEXPORT jint JNI_OnLoad_ma10t006a(JavaVM *jvm, char *options, void *reserved) {
 123     return JNI_VERSION_1_8;
 124 }
 125 #endif
 126 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 127     jvmtiEnv* jvmti = NULL;
 128     jvmtiCapabilities caps;
 129     jvmtiEventCallbacks callbacks;
 130 
 131     NSK_DISPLAY0("Agent_OnLoad\n");
 132 
 133     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 134         return JNI_ERR;
 135 
 136     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 137 
 138     if (!NSK_VERIFY((jvmti =
 139             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 140         return JNI_ERR;
 141 
 142     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 143         return JNI_ERR;
 144 
 145     memset(&caps, 0, sizeof(caps));
 146     caps.can_generate_compiled_method_load_events = 1;
 147     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 148         return JNI_ERR;
 149     }
 150 
 151     memset(&callbacks, 0, sizeof(callbacks));
 152     callbacks.CompiledMethodLoad = &CompiledMethodLoad;
 153     callbacks.CompiledMethodUnload = &CompiledMethodUnload;
 154     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 155         return JNI_ERR;
 156 
 157     return JNI_OK;
 158 }
 159 
 160 /* ========================================================================== */
 161 
 162 }