Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GenerateEvents/libGenerateEvents1.cpp
41155 views
/*1* Copyright (c) 2019, 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"2526extern "C" {2728#define AGENT_NAME "agent1"2930static JavaVM *java_vm = NULL;31static jthread exp_thread = NULL;32static jvmtiEnv *jvmti1 = NULL;33static jint agent1_event_count = 0;34static bool fail_status = false;3536static void37check_jvmti_status(JNIEnv* env, jvmtiError err, const char* msg) {38if (err != JVMTI_ERROR_NONE) {39printf("check_jvmti_status: JVMTI function returned error: %d\n", err);40fail_status = true;41env->FatalError(msg);42}43}4445static void JNICALL46CompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,47jint code_size, const void* code_addr,48jint map_length, const jvmtiAddrLocationMap* map,49const void* compile_info) {50JNIEnv* env = NULL;51jthread thread = NULL;52char* name = NULL;53char* sign = NULL;54jvmtiError err;5556// Posted on JavaThread's, so it is legal to obtain JNIEnv*57if (java_vm->GetEnv((void **) (&env), JNI_VERSION_9) != JNI_OK) {58printf("CompiledMethodLoad: failed to obtain JNIEnv*\n");59fail_status = true;60return;61}6263jvmti->GetCurrentThread(&thread);64if (!env->IsSameObject(thread, exp_thread)) {65return; // skip events from unexpected threads66}67agent1_event_count++;6869err = jvmti->GetMethodName(method, &name, &sign, NULL);70check_jvmti_status(env, err, "CompiledMethodLoad: Error in JVMTI GetMethodName");7172printf("%s: CompiledMethodLoad: %s%s\n", AGENT_NAME, name, sign);73fflush(0);74}7576JNIEXPORT jint JNICALL77Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {78jvmtiEventCallbacks callbacks;79jvmtiCapabilities caps;80jvmtiError err;8182java_vm = jvm;83if (jvm->GetEnv((void **) (&jvmti1), JVMTI_VERSION) != JNI_OK) {84printf("Agent_OnLoad: Error in GetEnv in obtaining jvmtiEnv*\n");85fail_status = true;86return JNI_ERR;87}8889memset(&callbacks, 0, sizeof(callbacks));90callbacks.CompiledMethodLoad = &CompiledMethodLoad;9192err = jvmti1->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));93if (err != JVMTI_ERROR_NONE) {94printf("Agent_OnLoad: Error in JVMTI SetEventCallbacks: %d\n", err);95fail_status = true;96return JNI_ERR;97}9899memset(&caps, 0, sizeof(caps));100caps.can_generate_compiled_method_load_events = 1;101102err = jvmti1->AddCapabilities(&caps);103if (err != JVMTI_ERROR_NONE) {104printf("Agent_OnLoad: Error in JVMTI AddCapabilities: %d\n", err);105fail_status = true;106return JNI_ERR;107}108return JNI_OK;109}110111JNIEXPORT void JNICALL112Java_MyPackage_GenerateEventsTest_agent1GenerateEvents(JNIEnv *env, jclass cls) {113jthread thread = NULL;114jvmtiError err;115116err = jvmti1->GetCurrentThread(&thread);117check_jvmti_status(env, err, "generateEvents1: Error in JVMTI GetCurrentThread");118119exp_thread = (jthread)env->NewGlobalRef(thread);120121err = jvmti1->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);122check_jvmti_status(env, err, "generateEvents1: Error in JVMTI SetEventNotificationMode: JVMTI_ENABLE");123124err = jvmti1->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD);125check_jvmti_status(env, err, "generateEvents1: Error in JVMTI GenerateEvents");126127err = jvmti1->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);128check_jvmti_status(env, err, "generateEvents1: Error in JVMTI SetEventNotificationMode: JVMTI_DISABLE");129}130131JNIEXPORT jboolean JNICALL132Java_MyPackage_GenerateEventsTest_agent1FailStatus(JNIEnv *env, jclass cls) {133return fail_status;134}135136} // extern "C"137138139