Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/SetBreakpoint/libTestManyBreakpoints.cpp
66646 views
1
/*
2
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include <string.h>
25
#include <stdio.h>
26
27
#include "jvmti.h"
28
29
#define TARGET_CLASS_NAME "LTarget;"
30
31
static jvmtiEnv *jvmti = NULL;
32
33
static void
34
check_jvmti_status(JNIEnv* jni, jvmtiError err, const char* msg) {
35
if (err != JVMTI_ERROR_NONE) {
36
printf("check_jvmti_status: %s, JVMTI function returned error: %d\n", msg, err);
37
jni->FatalError(msg);
38
}
39
}
40
41
void JNICALL classprepare(jvmtiEnv* jvmti_env, JNIEnv* jni_env, jthread thread, jclass klass) {
42
char* buf;
43
jvmtiError err;
44
45
err = jvmti->GetClassSignature(klass, &buf, NULL);
46
check_jvmti_status(jni_env, err, "classprepare: GetClassSignature error");
47
48
if (strncmp(buf, TARGET_CLASS_NAME, strlen(TARGET_CLASS_NAME)) == 0) {
49
jint nMethods;
50
jmethodID* methods;
51
int i;
52
53
err = jvmti->GetClassMethods(klass, &nMethods, &methods);
54
check_jvmti_status(jni_env, err, "classprepare: GetClassMethods error");
55
printf("Setting breakpoints in %s\n", buf);
56
fflush(stdout);
57
for (i = 0; i < nMethods; i++) {
58
err = jvmti->SetBreakpoint(methods[i], 0);
59
check_jvmti_status(jni_env, err, "classprepare: SetBreakpoint error");
60
}
61
}
62
}
63
64
65
void JNICALL breakpoint(jvmtiEnv* jvmti_env, JNIEnv* jni_env, jthread thread, jmethodID method, jlocation location) {
66
// Do nothing
67
}
68
69
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
70
jvmtiCapabilities capa;
71
jvmtiEventCallbacks cbs;
72
jint err;
73
74
err = vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);
75
if (err != JNI_OK) {
76
printf("Agent_OnLoad: GetEnv error\n");
77
return JNI_ERR;
78
}
79
80
memset(&capa, 0, sizeof(capa));
81
capa.can_generate_breakpoint_events = 1;
82
capa.can_generate_single_step_events = 1;
83
err = jvmti->AddCapabilities(&capa);
84
if (err != JNI_OK) {
85
printf("Agent_OnLoad: AddCapabilities error\n");
86
return JNI_ERR;
87
}
88
89
memset(&cbs, 0, sizeof(cbs));
90
cbs.ClassPrepare = classprepare;
91
cbs.Breakpoint = breakpoint;
92
err = jvmti->SetEventCallbacks(&cbs, sizeof(cbs));
93
if (err != JNI_OK) {
94
printf("Agent_OnLoad: SetEventCallbacks error\n");
95
return JNI_ERR;
96
}
97
98
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
99
if (err != JNI_OK) {
100
printf("Agent_OnLoad: SetEventNotificationMode CLASS_PREPARE error\n");
101
return JNI_ERR;
102
}
103
104
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL);
105
if (err != JNI_OK) {
106
printf("Agent_OnLoad: SetEventNotificationMode BREAKPOINT error\n");
107
return JNI_ERR;
108
}
109
110
return JNI_OK;
111
}
112
113