Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/NotifyFramePop/libNotifyFramePopTest.c
41153 views
/*1* Copyright (c) 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"2627#ifdef __cplusplus28extern "C" {29#endif3031#ifndef JNI_ENV_ARG3233#ifdef __cplusplus34#define JNI_ENV_ARG(x, y) y35#define JNI_ENV_PTR(x) x36#else37#define JNI_ENV_ARG(x,y) x, y38#define JNI_ENV_PTR(x) (*x)39#endif4041#endif4243static jvmtiEnv *jvmti = NULL;44static jvmtiCapabilities caps;45static jvmtiEventCallbacks callbacks;46static jboolean framePopReceived = JNI_FALSE;4748static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);4950JNIEXPORT51jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {52return Agent_Initialize(jvm, options, reserved);53}5455JNIEXPORT56jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {57return Agent_Initialize(jvm, options, reserved);58}5960JNIEXPORT61jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {62return JNI_VERSION_9;63}646566static void reportError(const char *msg, int err) {67fprintf(stdout, "%s, error: %d\n", msg, err);68fflush(stdout);69}707172static void JNICALL73FramePop(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,74jmethodID method, jboolean wasPoppedByException)75{76jvmtiError err;77jclass cls = NULL;78char* csig = NULL;79char* name = NULL;80char* sign = NULL;8182framePopReceived = JNI_TRUE;8384err = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &cls);85if (err != JVMTI_ERROR_NONE) {86reportError("FramePop: GetMethodDeclaringClass failed", err);87}88err = (*jvmti_env)->GetClassSignature(jvmti_env, cls, &csig, NULL);89if (err != JVMTI_ERROR_NONE) {90reportError("FramePop: GetClassSignature failed", err);91}92err = (*jvmti_env)->GetMethodName(jvmti_env, method, &name, &sign, NULL);93if (err != JVMTI_ERROR_NONE) {94reportError("FramePop: GetMethodName failed", err);95}96fprintf(stdout, "FramePop event from method: %s %s%s\n", csig, name, sign);97fflush(stdout);9899(*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)csig);100(*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)name);101(*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)sign);102}103104static105jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {106jint res;107jvmtiError err;108109res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_9);110if (res != JNI_OK || jvmti == NULL) {111reportError("GetEnv(JVMTI_VERSION_9) failed", res);112return JNI_ERR;113}114err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);115if (err != JVMTI_ERROR_NONE) {116reportError("GetPotentialCapabilities failed", err);117return JNI_ERR;118}119err = (*jvmti)->AddCapabilities(jvmti, &caps);120if (err != JVMTI_ERROR_NONE) {121reportError("AddCapabilities failed",err);122return JNI_ERR;123}124err = (*jvmti)->GetCapabilities(jvmti, &caps);125if (err != JVMTI_ERROR_NONE) {126reportError("GetCapabilities failed", err);127return JNI_ERR;128}129if (caps.can_generate_frame_pop_events) {130callbacks.FramePop = &FramePop;131err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));132if (err != JVMTI_ERROR_NONE) {133reportError("SetEventCallbacks failed", err);134return JNI_ERR;135}136}137return JNI_OK;138}139140JNIEXPORT jboolean JNICALL141Java_NotifyFramePopTest_canGenerateFramePopEvents(JNIEnv *env, jclass cls) {142return caps.can_generate_frame_pop_events ? JNI_TRUE : JNI_FALSE;143}144145JNIEXPORT void JNICALL146Java_NotifyFramePopTest_setFramePopNotificationMode(JNIEnv *env, jclass cl, jboolean enable) {147jvmtiEventMode mode = enable ? JVMTI_ENABLE : JVMTI_DISABLE;148jvmtiError err = (*jvmti)->SetEventNotificationMode(jvmti, mode, JVMTI_EVENT_FRAME_POP, NULL);149if (err != JVMTI_ERROR_NONE) {150reportError("Failed to set notification mode for FRAME_POP events", err);151}152}153154JNIEXPORT void JNICALL155Java_NotifyFramePopTest_notifyFramePop(JNIEnv *env, jclass cls, jthread thread)156{157jvmtiError err= (*jvmti)->NotifyFramePop(jvmti, thread, 1);158if (err != JVMTI_ERROR_NONE) {159reportError("NotifyFramePop failed", err);160}161}162163JNIEXPORT jboolean JNICALL164Java_NotifyFramePopTest_framePopReceived(JNIEnv *env, jclass cls) {165jboolean result = framePopReceived;166framePopReceived = JNI_FALSE;167return result;168}169170#ifdef __cplusplus171}172#endif173174175