1 /*
   2  * Copyright (c) 2003, 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 <stdio.h>
  25 #include <string.h>
  26 #include <jvmti.h>
  27 #include "agent_common.h"
  28 
  29 #include "nsk_tools.h"
  30 #include "JVMTITools.h"
  31 #include "jvmti_tools.h"
  32 
  33 extern "C" {
  34 
  35 #define STATUS_FAILED 2
  36 #define PASSED 0
  37 
  38 static volatile jint result = PASSED;
  39 static volatile int compunload = 0;
  40 static volatile int class_unloaded = 0;
  41 static jvmtiEnv *jvmti = NULL;
  42 static jvmtiEventCallbacks callbacks;
  43 static jvmtiCapabilities caps;
  44 
  45 /** callback functions **/
  46 void JNICALL
  47 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
  48     NSK_DISPLAY0("VMInit event received\n\n");
  49 
  50     if (!NSK_JVMTI_VERIFY(jvmti_env->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) {
  51         NSK_COMPLAIN0("TEST FAILED: unable to generate events to represent the current state of the VM\n");
  52         result = STATUS_FAILED;
  53     }
  54 }
  55 
  56 void JNICALL
  57 CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
  58         const void* code_addr,  jint map_length, const jvmtiAddrLocationMap* map,
  59         const void* compile_info) {
  60     char *name;
  61     char *sig;
  62     char *generic;
  63 
  64     NSK_DISPLAY0("CompiledMethodLoad event received for:\n");
  65 
  66     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &sig, &generic))) {
  67         result = STATUS_FAILED;
  68         NSK_COMPLAIN0("TEST FAILURE: unable to obtain method info\n\n");
  69         return;
  70     }
  71     NSK_DISPLAY4("\tmethod: name=\"%s\" signature=\"%s\"\n\tcompiled code size=%d\n\tnumber of address location map entries=%d\n\n",
  72         name, sig, code_size, map_length);
  73 }
  74 
  75 void JNICALL
  76 CompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
  77         const void* code_addr) {
  78     char *name;
  79     char *sig;
  80     jvmtiPhase phase;
  81     jvmtiError err;
  82 
  83     compunload++;
  84 
  85     /* jmethodID: Identifies a Java programming language method, initializer, or constructor.
  86      * jmethodIDs returned by JVM TI functions and events may be safely stored. However, if
  87      * the class is unloaded, they become invalid and must not be used. */
  88 
  89     NSK_DISPLAY0("CompiledMethodUnload event received\n");
  90     // Check for the case that the class has been unloaded
  91     err = jvmti_env->GetMethodName(method, &name, &sig, NULL);
  92     if (err == JVMTI_ERROR_NONE) {
  93         NSK_DISPLAY3("for: \tmethod: name=\"%s\" signature=\"%s\"\n\tnative address=0x%p\n",
  94           name, sig, code_addr);
  95         jvmti_env->Deallocate((unsigned char*)name);
  96         jvmti_env->Deallocate((unsigned char*)sig);
  97     } else {
  98         // The class metadata has been completely unloaded so the name is not available.
  99         NSK_DISPLAY0("for: \tmethod: name=<not available>\n");
 100     }
 101 
 102     // Count unloaded events
 103     class_unloaded++;
 104 
 105     if (!NSK_JVMTI_VERIFY(jvmti_env->GetPhase(&phase))) {
 106         result = STATUS_FAILED;
 107         NSK_COMPLAIN0("TEST FAILURE: unable to obtain phase of the VM execution\n");
 108         return;
 109     }
 110 
 111     if (phase != JVMTI_PHASE_LIVE) {
 112         result = STATUS_FAILED;
 113         NSK_COMPLAIN1("TEST FAILED: CompiledMethodUnload event received during non-live phase %s\n",
 114             TranslatePhase(phase));
 115     }
 116     else
 117         NSK_DISPLAY0("CHECK PASSED: CompiledMethodUnload event received during the live phase as expected\n\n");
 118 }
 119 /************************/
 120 
 121 JNIEXPORT jint JNICALL
 122 Java_nsk_jvmti_CompiledMethodUnload_compmethunload001_check(
 123         JNIEnv *env, jobject obj) {
 124     if (!caps.can_generate_compiled_method_load_events)
 125         return PASSED;
 126 
 127     if (compunload == 0)
 128         NSK_DISPLAY0("Warning: no CompiledMethodUnload events\n\tthe test has no results\n");
 129 
 130     return result;
 131 }
 132 
 133 JNIEXPORT jint JNICALL
 134 Java_nsk_jvmti_CompiledMethodUnload_compmethunload001_unloaded(
 135         JNIEnv *env, jobject obj) {
 136     if (!caps.can_generate_compiled_method_load_events)
 137         return 1;
 138 
 139     return class_unloaded;
 140 }
 141 
 142 #ifdef STATIC_BUILD
 143 JNIEXPORT jint JNICALL Agent_OnLoad_compmethunload001(JavaVM *jvm, char *options, void *reserved) {
 144     return Agent_Initialize(jvm, options, reserved);
 145 }
 146 JNIEXPORT jint JNICALL Agent_OnAttach_compmethunload001(JavaVM *jvm, char *options, void *reserved) {
 147     return Agent_Initialize(jvm, options, reserved);
 148 }
 149 JNIEXPORT jint JNI_OnLoad_compmethunload001(JavaVM *jvm, char *options, void *reserved) {
 150     return JNI_VERSION_1_8;
 151 }
 152 #endif
 153 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 154     /* init framework and parse options */
 155     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 156         return JNI_ERR;
 157 
 158     /* create JVMTI environment */
 159     if (!NSK_VERIFY((jvmti =
 160             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 161         return JNI_ERR;
 162 
 163     /* add capability to generate compiled method events */
 164     memset(&caps, 0, sizeof(jvmtiCapabilities));
 165     caps.can_generate_compiled_method_load_events = 1;
 166     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 167         return JNI_ERR;
 168 
 169     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
 170         return JNI_ERR;
 171 
 172     if (!caps.can_generate_compiled_method_load_events)
 173         NSK_DISPLAY0("Warning: generation of compiled method events is not implemented\n");
 174 
 175     /* set event callback */
 176     NSK_DISPLAY0("setting event callbacks ...\n");
 177     (void) memset(&callbacks, 0, sizeof(callbacks));
 178     callbacks.VMInit = &VMInit;
 179     callbacks.CompiledMethodLoad = &CompiledMethodLoad;
 180     callbacks.CompiledMethodUnload = &CompiledMethodUnload;
 181     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
 182         return JNI_ERR;
 183 
 184     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
 185     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL)))
 186         return JNI_ERR;
 187     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))
 188         return JNI_ERR;
 189     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL)))
 190         return JNI_ERR;
 191     NSK_DISPLAY0("enabling the events done\n\n");
 192 
 193     return JNI_OK;
 194 }
 195 
 196 }