Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GenerateEvents/libGenerateEvents2.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 "agent2"2930static JavaVM *java_vm = NULL;31static jthread exp_thread = NULL;32static jvmtiEnv *jvmti2 = NULL;33static jint agent2_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) {58fail_status = true;59return;60}6162err = jvmti->GetCurrentThread(&thread);63check_jvmti_status(env, err, "CompiledMethodLoad: Error in JVMTI GetCurrentThread");64if (!env->IsSameObject(thread, exp_thread)) {65return; // skip events from unexpected threads66}67agent2_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 **) (&jvmti2), JVMTI_VERSION_9) != JNI_OK) {84return JNI_ERR;85}8687memset(&callbacks, 0, sizeof(callbacks));88callbacks.CompiledMethodLoad = &CompiledMethodLoad;8990err = jvmti2->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));91if (err != JVMTI_ERROR_NONE) {92printf("Agent_OnLoad: Error in JVMTI SetEventCallbacks: %d\n", err);93fail_status = true;94return JNI_ERR;95}9697memset(&caps, 0, sizeof(caps));98caps.can_generate_compiled_method_load_events = 1;99100err = jvmti2->AddCapabilities(&caps);101if (err != JVMTI_ERROR_NONE) {102printf("Agent_OnLoad: Error in JVMTI AddCapabilities: %d\n", err);103fail_status = true;104return JNI_ERR;105}106return JNI_OK;107}108109JNIEXPORT void JNICALL110Java_MyPackage_GenerateEventsTest_agent2SetThread(JNIEnv *env, jclass cls, jthread thread) {111jvmtiError err;112113exp_thread = (jthread)env->NewGlobalRef(thread);114115err = jvmti2->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);116check_jvmti_status(env, err, "setThread2: Error in JVMTI SetEventNotificationMode: JVMTI_ENABLE");117}118119JNIEXPORT jboolean JNICALL120Java_MyPackage_GenerateEventsTest_agent2FailStatus(JNIEnv *env, jclass cls) {121jvmtiError err;122123err = jvmti2->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);124check_jvmti_status(env, err, "check2: Error in JVMTI SetEventNotificationMode: JVMTI_DISABLE");125126printf("\n");127if (agent2_event_count == 0) {128printf("check2: Zero events in agent2 as expected\n");129} else {130fail_status = true;131printf("check2: Unexpected non-zero event count in agent2: %d\n", agent2_event_count);132}133printf("\n");134fflush(0);135136return fail_status;137}138139} // extern "C"140141142