Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/CompiledMethodUnload/compmethunload001/compmethunload001.cpp
40955 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include <jvmti.h>26#include "agent_common.h"2728#include "nsk_tools.h"29#include "JVMTITools.h"30#include "jvmti_tools.h"3132extern "C" {3334#define STATUS_FAILED 235#define PASSED 03637static volatile jint result = PASSED;38static volatile int compunload = 0;39static volatile int class_unloaded = 0;40static jvmtiEnv *jvmti = NULL;41static jvmtiEventCallbacks callbacks;42static jvmtiCapabilities caps;4344/** callback functions **/45void JNICALL46VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {47NSK_DISPLAY0("VMInit event received\n\n");4849if (!NSK_JVMTI_VERIFY(jvmti_env->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) {50NSK_COMPLAIN0("TEST FAILED: unable to generate events to represent the current state of the VM\n");51result = STATUS_FAILED;52}53}5455void JNICALL56CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,57const void* code_addr, jint map_length, const jvmtiAddrLocationMap* map,58const void* compile_info) {59char *name;60char *sig;61char *generic;6263NSK_DISPLAY0("CompiledMethodLoad event received for:\n");6465if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &sig, &generic))) {66result = STATUS_FAILED;67NSK_COMPLAIN0("TEST FAILURE: unable to obtain method info\n\n");68return;69}70NSK_DISPLAY4("\tmethod: name=\"%s\" signature=\"%s\"\n\tcompiled code size=%d\n\tnumber of address location map entries=%d\n\n",71name, sig, code_size, map_length);72}7374void JNICALL75CompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,76const void* code_addr) {77char *name;78char *sig;79jvmtiPhase phase;80jvmtiError err;8182compunload++;8384/* jmethodID: Identifies a Java programming language method, initializer, or constructor.85* jmethodIDs returned by JVM TI functions and events may be safely stored. However, if86* the class is unloaded, they become invalid and must not be used. */8788NSK_DISPLAY0("CompiledMethodUnload event received\n");89// Check for the case that the class has been unloaded90err = jvmti_env->GetMethodName(method, &name, &sig, NULL);91if (err == JVMTI_ERROR_NONE) {92NSK_DISPLAY3("for: \tmethod: name=\"%s\" signature=\"%s\"\n\tnative address=0x%p\n",93name, sig, code_addr);94jvmti_env->Deallocate((unsigned char*)name);95jvmti_env->Deallocate((unsigned char*)sig);96} else {97// The class metadata has been completely unloaded so the name is not available.98NSK_DISPLAY0("for: \tmethod: name=<not available>\n");99}100101// Count unloaded events102class_unloaded++;103104if (!NSK_JVMTI_VERIFY(jvmti_env->GetPhase(&phase))) {105result = STATUS_FAILED;106NSK_COMPLAIN0("TEST FAILURE: unable to obtain phase of the VM execution\n");107return;108}109110if (phase != JVMTI_PHASE_LIVE) {111result = STATUS_FAILED;112NSK_COMPLAIN1("TEST FAILED: CompiledMethodUnload event received during non-live phase %s\n",113TranslatePhase(phase));114}115else116NSK_DISPLAY0("CHECK PASSED: CompiledMethodUnload event received during the live phase as expected\n\n");117}118/************************/119120JNIEXPORT jint JNICALL121Java_nsk_jvmti_CompiledMethodUnload_compmethunload001_check(122JNIEnv *env, jobject obj) {123if (!caps.can_generate_compiled_method_load_events)124return PASSED;125126if (compunload == 0)127NSK_DISPLAY0("Warning: no CompiledMethodUnload events\n\tthe test has no results\n");128129return result;130}131132JNIEXPORT jint JNICALL133Java_nsk_jvmti_CompiledMethodUnload_compmethunload001_unloaded(134JNIEnv *env, jobject obj) {135if (!caps.can_generate_compiled_method_load_events)136return 1;137138return class_unloaded;139}140141#ifdef STATIC_BUILD142JNIEXPORT jint JNICALL Agent_OnLoad_compmethunload001(JavaVM *jvm, char *options, void *reserved) {143return Agent_Initialize(jvm, options, reserved);144}145JNIEXPORT jint JNICALL Agent_OnAttach_compmethunload001(JavaVM *jvm, char *options, void *reserved) {146return Agent_Initialize(jvm, options, reserved);147}148JNIEXPORT jint JNI_OnLoad_compmethunload001(JavaVM *jvm, char *options, void *reserved) {149return JNI_VERSION_1_8;150}151#endif152jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {153/* init framework and parse options */154if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))155return JNI_ERR;156157/* create JVMTI environment */158if (!NSK_VERIFY((jvmti =159nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))160return JNI_ERR;161162/* add capability to generate compiled method events */163memset(&caps, 0, sizeof(jvmtiCapabilities));164caps.can_generate_compiled_method_load_events = 1;165if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))166return JNI_ERR;167168if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))169return JNI_ERR;170171if (!caps.can_generate_compiled_method_load_events)172NSK_DISPLAY0("Warning: generation of compiled method events is not implemented\n");173174/* set event callback */175NSK_DISPLAY0("setting event callbacks ...\n");176(void) memset(&callbacks, 0, sizeof(callbacks));177callbacks.VMInit = &VMInit;178callbacks.CompiledMethodLoad = &CompiledMethodLoad;179callbacks.CompiledMethodUnload = &CompiledMethodUnload;180if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))181return JNI_ERR;182183NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");184if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL)))185return JNI_ERR;186if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))187return JNI_ERR;188if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL)))189return JNI_ERR;190NSK_DISPLAY0("enabling the events done\n\n");191192return JNI_OK;193}194195}196197198