Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/SetBreakpoint/libTestManyBreakpoints.cpp
66646 views
/*1* Copyright (c) 2022, 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 <stdio.h>2526#include "jvmti.h"2728#define TARGET_CLASS_NAME "LTarget;"2930static jvmtiEnv *jvmti = NULL;3132static void33check_jvmti_status(JNIEnv* jni, jvmtiError err, const char* msg) {34if (err != JVMTI_ERROR_NONE) {35printf("check_jvmti_status: %s, JVMTI function returned error: %d\n", msg, err);36jni->FatalError(msg);37}38}3940void JNICALL classprepare(jvmtiEnv* jvmti_env, JNIEnv* jni_env, jthread thread, jclass klass) {41char* buf;42jvmtiError err;4344err = jvmti->GetClassSignature(klass, &buf, NULL);45check_jvmti_status(jni_env, err, "classprepare: GetClassSignature error");4647if (strncmp(buf, TARGET_CLASS_NAME, strlen(TARGET_CLASS_NAME)) == 0) {48jint nMethods;49jmethodID* methods;50int i;5152err = jvmti->GetClassMethods(klass, &nMethods, &methods);53check_jvmti_status(jni_env, err, "classprepare: GetClassMethods error");54printf("Setting breakpoints in %s\n", buf);55fflush(stdout);56for (i = 0; i < nMethods; i++) {57err = jvmti->SetBreakpoint(methods[i], 0);58check_jvmti_status(jni_env, err, "classprepare: SetBreakpoint error");59}60}61}626364void JNICALL breakpoint(jvmtiEnv* jvmti_env, JNIEnv* jni_env, jthread thread, jmethodID method, jlocation location) {65// Do nothing66}6768JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {69jvmtiCapabilities capa;70jvmtiEventCallbacks cbs;71jint err;7273err = vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);74if (err != JNI_OK) {75printf("Agent_OnLoad: GetEnv error\n");76return JNI_ERR;77}7879memset(&capa, 0, sizeof(capa));80capa.can_generate_breakpoint_events = 1;81capa.can_generate_single_step_events = 1;82err = jvmti->AddCapabilities(&capa);83if (err != JNI_OK) {84printf("Agent_OnLoad: AddCapabilities error\n");85return JNI_ERR;86}8788memset(&cbs, 0, sizeof(cbs));89cbs.ClassPrepare = classprepare;90cbs.Breakpoint = breakpoint;91err = jvmti->SetEventCallbacks(&cbs, sizeof(cbs));92if (err != JNI_OK) {93printf("Agent_OnLoad: SetEventCallbacks error\n");94return JNI_ERR;95}9697err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);98if (err != JNI_OK) {99printf("Agent_OnLoad: SetEventNotificationMode CLASS_PREPARE error\n");100return JNI_ERR;101}102103err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL);104if (err != JNI_OK) {105printf("Agent_OnLoad: SetEventNotificationMode BREAKPOINT error\n");106return JNI_ERR;107}108109return JNI_OK;110}111112113