Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClearBreakpoint/clrbrk001/clrbrk001.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 <stdio.h>24#include <string.h>25#include "jvmti.h"26#include "agent_common.h"27#include "JVMTITools.h"2829extern "C" {303132#define PASSED 033#define STATUS_FAILED 23435static jvmtiEnv *jvmti = NULL;36static jvmtiCapabilities caps;37static jvmtiEventCallbacks callbacks;38static jint result = PASSED;39static jboolean printdump = JNI_FALSE;40static jmethodID mid;41static int breakpointsExpected = 0;42static int breakpointsCount = 0;4344void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,45jthread thr, jmethodID method, jlocation location) {46breakpointsCount++;47}4849#ifdef STATIC_BUILD50JNIEXPORT jint JNICALL Agent_OnLoad_clrbrk001(JavaVM *jvm, char *options, void *reserved) {51return Agent_Initialize(jvm, options, reserved);52}53JNIEXPORT jint JNICALL Agent_OnAttach_clrbrk001(JavaVM *jvm, char *options, void *reserved) {54return Agent_Initialize(jvm, options, reserved);55}56JNIEXPORT jint JNI_OnLoad_clrbrk001(JavaVM *jvm, char *options, void *reserved) {57return JNI_VERSION_1_8;58}59#endif60jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {61jvmtiError err;62jint res;6364if (options != NULL && strcmp(options, "printdump") == 0) {65printdump = JNI_TRUE;66}6768res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);69if (res != JNI_OK || jvmti == NULL) {70printf("Wrong result of a valid call to GetEnv!\n");71return JNI_ERR;72}7374err = jvmti->GetPotentialCapabilities(&caps);75if (err != JVMTI_ERROR_NONE) {76printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",77TranslateError(err), err);78return JNI_ERR;79}8081err = jvmti->AddCapabilities(&caps);82if (err != JVMTI_ERROR_NONE) {83printf("(AddCapabilities) unexpected error: %s (%d)\n",84TranslateError(err), err);85return JNI_ERR;86}8788err = jvmti->GetCapabilities(&caps);89if (err != JVMTI_ERROR_NONE) {90printf("(GetCapabilities) unexpected error: %s (%d)\n",91TranslateError(err), err);92return JNI_ERR;93}9495if (caps.can_generate_breakpoint_events) {96callbacks.Breakpoint = &Breakpoint;97err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));98if (err != JVMTI_ERROR_NONE) {99printf("(SetEventCallbacks) unexpected error: %s (%d)\n",100TranslateError(err), err);101return JNI_ERR;102}103err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,104JVMTI_EVENT_BREAKPOINT, NULL);105if (err != JVMTI_ERROR_NONE) {106printf("Failed to enable BREAKPOINT event: %s (%d)\n",107TranslateError(err), err);108return JNI_ERR;109}110} else {111printf("Warning: Breakpoint is not implemented\n");112}113114return JNI_OK;115}116117JNIEXPORT void JNICALL118Java_nsk_jvmti_ClearBreakpoint_clrbrk001_getReady(JNIEnv *env, jclass cls, jint i) {119mid = env->GetStaticMethodID(cls, "trial", "(I)I");120if (mid == NULL) {121printf("Cannot find method \"trial(I)I\"\n");122result = STATUS_FAILED;123} else {124breakpointsExpected = i;125}126}127128JNIEXPORT void JNICALL Java_nsk_jvmti_ClearBreakpoint_clrbrk001_setBP(JNIEnv *env, jclass cls) {129jvmtiError err;130131if (jvmti == NULL) {132printf("JVMTI client was not properly loaded!\n");133return;134}135136if (!caps.can_generate_breakpoint_events) {137return;138}139140if (mid == NULL) {141return;142}143144err = jvmti->SetBreakpoint(mid, 0);145if (err != JVMTI_ERROR_NONE) {146printf("Failed to SetBreakpoint: %s (%d)\n",147TranslateError(err), err);148result = STATUS_FAILED;149}150}151152JNIEXPORT void JNICALL Java_nsk_jvmti_ClearBreakpoint_clrbrk001_clearBP(JNIEnv *env, jclass cls) {153jvmtiError err;154155if (jvmti == NULL) {156printf("JVMTI client was not properly loaded!\n");157return;158}159160if (!caps.can_generate_breakpoint_events) {161return;162}163164if (mid == NULL) {165return;166}167168err = jvmti->ClearBreakpoint(mid, 0);169if (err != JVMTI_ERROR_NONE) {170printf("Failed to ClearBreakpoint: %s (%d)\n",171TranslateError(err), err);172result = STATUS_FAILED;173}174}175176JNIEXPORT jint JNICALL177Java_nsk_jvmti_ClearBreakpoint_clrbrk001_check(JNIEnv *env, jclass cls) {178if (jvmti == NULL) {179printf("JVMTI client was not properly loaded!\n");180return STATUS_FAILED;181}182183if (!caps.can_generate_breakpoint_events) {184return result;185}186187if (printdump == JNI_TRUE) {188printf("Total number of Breakpoint events: %d\n", breakpointsCount);189}190191if (breakpointsCount != breakpointsExpected) {192printf("Wrong number of Breakpoint events: %d, expected: %d\n",193breakpointsCount, breakpointsExpected);194result = STATUS_FAILED;195}196197return result;198}199200}201202203