Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/CompiledMethodLoad/compmethload001/compmethload001.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 jvmtiEnv *jvmti = NULL;39static jvmtiEventCallbacks callbacks;40static jvmtiCapabilities caps;4142/** callback functions **/43void JNICALL44VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {45NSK_DISPLAY0("VMInit event received\n\n");4647if (!NSK_JVMTI_VERIFY(jvmti_env->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) {48NSK_COMPLAIN0("TEST FAILED: unable to generate events to represent the current state of the VM\n");49result = STATUS_FAILED;50}51}5253void JNICALL54CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,55const void* code_addr, jint map_length, const jvmtiAddrLocationMap* map,56const void* compile_info) {57char *name;58char *sig;59char *generic;60jvmtiPhase phase;6162NSK_DISPLAY0("CompiledMethodLoad event received for:\n");6364if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &sig, &generic))) {65result = STATUS_FAILED;66NSK_COMPLAIN0("TEST FAILURE: unable to obtain method info\n");67return;68}69NSK_DISPLAY4("\tmethod: name=\"%s\" signature=\"%s\"\n\tcompiled code size=%d\n\tnumber of address location map entries=%d\n",70name, sig, code_size, map_length);7172if (!NSK_JVMTI_VERIFY(jvmti_env->GetPhase(&phase))) {73result = STATUS_FAILED;74NSK_COMPLAIN0("TEST FAILURE: unable to obtain phase of the VM execution\n");75return;76}7778if (phase != JVMTI_PHASE_LIVE) {79result = STATUS_FAILED;80NSK_COMPLAIN1("TEST FAILED: CompiledMethodLoad event received during non-live phase %s\n",81TranslatePhase(phase));82}83else84NSK_DISPLAY0("CHECK PASSED: CompiledMethodLoad event received during the live phase as expected\n\n");85}86/************************/8788JNIEXPORT jint JNICALL89Java_nsk_jvmti_CompiledMethodLoad_compmethload001_check(90JNIEnv *env, jobject obj) {91if (!caps.can_generate_compiled_method_load_events)92return PASSED;9394return result;95}9697#ifdef STATIC_BUILD98JNIEXPORT jint JNICALL Agent_OnLoad_compmethload001(JavaVM *jvm, char *options, void *reserved) {99return Agent_Initialize(jvm, options, reserved);100}101JNIEXPORT jint JNICALL Agent_OnAttach_compmethload001(JavaVM *jvm, char *options, void *reserved) {102return Agent_Initialize(jvm, options, reserved);103}104JNIEXPORT jint JNI_OnLoad_compmethload001(JavaVM *jvm, char *options, void *reserved) {105return JNI_VERSION_1_8;106}107#endif108jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {109/* init framework and parse options */110if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))111return JNI_ERR;112113/* create JVMTI environment */114if (!NSK_VERIFY((jvmti =115nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))116return JNI_ERR;117118/* add capability to generate compiled method events */119memset(&caps, 0, sizeof(jvmtiCapabilities));120caps.can_generate_compiled_method_load_events = 1;121if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))122return JNI_ERR;123124if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))125return JNI_ERR;126127if (!caps.can_generate_compiled_method_load_events)128NSK_DISPLAY0("Warning: generation of compiled method events is not implemented\n");129130/* set event callback */131NSK_DISPLAY0("setting event callbacks ...\n");132(void) memset(&callbacks, 0, sizeof(callbacks));133callbacks.VMInit = &VMInit;134callbacks.CompiledMethodLoad = &CompiledMethodLoad;135if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))136return JNI_ERR;137138NSK_DISPLAY0("setting event callbacks done\nenabling VMInit, CompiledMethodLoad event ...\n");139if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL)))140return JNI_ERR;141if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))142return JNI_ERR;143NSK_DISPLAY0("enabling the events done\n\n");144145return JNI_OK;146}147148}149150151