Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GenerateEvents/genevents001/genevents001.cpp
40951 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 <string.h>24#include "jvmti.h"25#include "agent_common.h"26#include "jni_tools.h"27#include "jvmti_tools.h"2829extern "C" {3031/* ============================================================================= */3233/* scaffold objects */34static jlong timeout = 0;3536/* constants */37#define EVENTS_COUNT 33839/* tested events */40static jvmtiEvent eventsList[EVENTS_COUNT] = {41JVMTI_EVENT_COMPILED_METHOD_LOAD,42JVMTI_EVENT_COMPILED_METHOD_UNLOAD,43JVMTI_EVENT_DYNAMIC_CODE_GENERATED44};4546/* event counts */47static int eventsCountList[EVENTS_COUNT];4849/* ============================================================================= */5051/** Check if all expected events received. */52static int checkEvents() {53int success = NSK_TRUE;5455NSK_DISPLAY0("Events received:\n");56NSK_DISPLAY1(" COMPILED_METHOD_LOAD: %d events\n", eventsCountList[0]);57NSK_DISPLAY1(" COMPILED_METHOD_UNLOAD: %d events\n", eventsCountList[1]);58NSK_DISPLAY1(" DYNAMIC_CODE_GENERATED: %d events\n", eventsCountList[2]);5960if (eventsCountList[0] <= 0) {61NSK_DISPLAY0("# WARNING: GenerateEvents() produced no COMPILED_METHOD_LOAD events\n");62NSK_DISPLAY0("# (but methods may not be compiled)\n");63}6465if (eventsCountList[2] <= 0) {66NSK_DISPLAY0("# WARNING: GenerateEvents() produced no DYNAMIC_CODE_GENERATED events\n");67NSK_DISPLAY0("# (but dynamic code may not be generated)\n");68}6970if (eventsCountList[1] > 0) {71NSK_DISPLAY1("# WARNING: COMPILED_METHOD_UNLOAD events were received: %d events\n",72eventsCountList[1]);73}7475return success;76}7778/* ============================================================================= */7980/** Agent algorithm. */81static void JNICALL82agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {83NSK_DISPLAY0("Wait for tested method forced to compile\n");84if (!nsk_jvmti_waitForSync(timeout))85return;8687NSK_DISPLAY0(">>> Testcase #1: Check if GenerateEvents() sends missed events\n");88{89int i;9091for (i = 0; i < EVENTS_COUNT; i++) {92eventsCountList[i] = 0;93}9495NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);96if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL)) {97nsk_jvmti_setFailStatus();98}99100NSK_DISPLAY0("Call GenerateEvents() to send missed events\n");101if (!NSK_JVMTI_VERIFY(jvmti->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) {102nsk_jvmti_setFailStatus();103}104105if (!NSK_JVMTI_VERIFY(jvmti->GenerateEvents(JVMTI_EVENT_DYNAMIC_CODE_GENERATED))) {106nsk_jvmti_setFailStatus();107}108109NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);110if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL)) {111nsk_jvmti_setFailStatus();112}113114NSK_DISPLAY0("Check received events\n");115if (!checkEvents()) {116nsk_jvmti_setFailStatus();117}118}119120NSK_DISPLAY0("Let debugee to finish\n");121if (!nsk_jvmti_resumeSync())122return;123}124125/* ============================================================================= */126127/**128* Callback for COMPILED_METHOD_LOAD event.129*/130JNIEXPORT void JNICALL131callbackCompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,132jint code_size, const void* code_addr,133jint map_length, const jvmtiAddrLocationMap* map,134const void* compile_info) {135NSK_DISPLAY3(" <COMPILED_METHOD_LOAD>: method: 0x%p, code: 0x%p, size: %d\n",136(void*)method, (void*)code_addr, (int)code_size);137eventsCountList[0]++;138}139140/**141* Callback for COMPILED_METHOD_UNLOAD event.142*/143JNIEXPORT void JNICALL144callbackCompiledMethodUnload(jvmtiEnv* jvmti, jmethodID method,145const void* code_addr) {146NSK_DISPLAY1(" <COMPILED_METHOD_UNLOAD>: method: 0x%p\n",147(void*)method);148eventsCountList[1]++;149}150151/**152* Callback for DYNAMIC_CODE_GENERATED event.153*/154JNIEXPORT void JNICALL155callbackDynamicCodeGenerated(jvmtiEnv* jvmti, const char* name, const void* address, jint length) {156NSK_DISPLAY3(" <DYNAMIC_CODE_GENERATED>: name: %s, address: 0x%p, length: %d\n",157nsk_null_string(name), (void*)address, (int)length);158eventsCountList[2]++;159}160161/* ============================================================================= */162163/** Agent library initialization. */164#ifdef STATIC_BUILD165JNIEXPORT jint JNICALL Agent_OnLoad_genevents001(JavaVM *jvm, char *options, void *reserved) {166return Agent_Initialize(jvm, options, reserved);167}168JNIEXPORT jint JNICALL Agent_OnAttach_genevents001(JavaVM *jvm, char *options, void *reserved) {169return Agent_Initialize(jvm, options, reserved);170}171JNIEXPORT jint JNI_OnLoad_genevents001(JavaVM *jvm, char *options, void *reserved) {172return JNI_VERSION_1_8;173}174#endif175jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {176jvmtiEnv* jvmti = NULL;177178if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))179return JNI_ERR;180181timeout = nsk_jvmti_getWaitTime() * 60 * 1000;182183if (!NSK_VERIFY((jvmti =184nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))185return JNI_ERR;186187if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))188return JNI_ERR;189190/* add required capabilities */191{192jvmtiCapabilities caps;193memset(&caps, 0, sizeof(caps));194caps.can_generate_compiled_method_load_events = 1;195if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))196return JNI_ERR;197}198199/* set event callbacks */200{201jvmtiEventCallbacks eventCallbacks;202memset(&eventCallbacks, 0, sizeof(eventCallbacks));203eventCallbacks.CompiledMethodLoad = callbackCompiledMethodLoad;204eventCallbacks.CompiledMethodUnload = callbackCompiledMethodUnload;205eventCallbacks.DynamicCodeGenerated = callbackDynamicCodeGenerated;206if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))207return JNI_ERR;208}209210return JNI_OK;211}212213/* ============================================================================= */214215}216217218