Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorStackDepthInfo/libGetOwnedMonitorStackDepthInfoTest.c
41153 views
1
/*
2
* Copyright (c) 2018, 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 <stdio.h>
25
#include <string.h>
26
#include "jvmti.h"
27
#include "jni.h"
28
29
#ifdef __cplusplus
30
extern "C" {
31
#endif
32
33
#ifndef JNI_ENV_ARG
34
35
#ifdef __cplusplus
36
#define JNI_ENV_ARG(x, y) y
37
#define JNI_ENV_PTR(x) x
38
#else
39
#define JNI_ENV_ARG(x,y) x, y
40
#define JNI_ENV_PTR(x) (*x)
41
#endif
42
43
#endif
44
45
#define PASSED 0
46
#define FAILED 2
47
48
#define TEST_CLASS "GetOwnedMonitorStackDepthInfoTest"
49
#define LOCK1_CLASS "GetOwnedMonitorStackDepthInfoTest$Lock1"
50
#define LOCK2_CLASS "GetOwnedMonitorStackDepthInfoTest$Lock2"
51
52
#define TEST_OBJECT_LOCK_DEPTH 2
53
#define LOCK1_DEPTH 3
54
#define LOCK2_DEPTH 1
55
#define EXP_MONITOR_COUNT 3
56
57
static jvmtiEnv *jvmti;
58
59
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
60
61
static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) {
62
char *errMsg;
63
jvmtiError result;
64
65
result = (*jvmti)->GetErrorName(jvmti, errCode, &errMsg);
66
if (result == JVMTI_ERROR_NONE) {
67
fprintf(stderr, "%s: %s (%d)\n", message, errMsg, errCode);
68
(*jvmti)->Deallocate(jvmti, (unsigned char *)errMsg);
69
} else {
70
fprintf(stderr, "%s (%d)\n", message, errCode);
71
}
72
}
73
74
static jboolean CheckLockObject(JNIEnv *env, jobject monitor, jclass testClass) {
75
return (*env)->IsInstanceOf(env, monitor, testClass);
76
}
77
78
JNIEXPORT jint JNICALL
79
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
80
return Agent_Initialize(jvm, options, reserved);
81
}
82
83
JNIEXPORT jint JNICALL
84
Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
85
return Agent_Initialize(jvm, options, reserved);
86
}
87
88
JNIEXPORT jint JNICALL
89
JNI_OnLoad(JavaVM *jvm, void *reserved) {
90
jint res;
91
JNIEnv *env;
92
93
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &env),
94
JNI_VERSION_9);
95
if (res != JNI_OK || env == NULL) {
96
fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res);
97
return JNI_ERR;
98
}
99
100
return JNI_VERSION_9;
101
}
102
103
static
104
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
105
jint res;
106
jvmtiError err;
107
jvmtiCapabilities caps;
108
109
printf("Agent_OnLoad started\n");
110
111
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
112
JVMTI_VERSION_9);
113
if (res != JNI_OK || jvmti == NULL) {
114
fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");
115
return JNI_ERR;
116
}
117
118
err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);
119
if (err != JVMTI_ERROR_NONE) {
120
ShowErrorMessage(jvmti, err,
121
"Agent_OnLoad: error in JVMTI GetPotentialCapabilities");
122
return JNI_ERR;
123
}
124
125
err = (*jvmti)->AddCapabilities(jvmti, &caps);
126
if (err != JVMTI_ERROR_NONE) {
127
ShowErrorMessage(jvmti, err,
128
"Agent_OnLoad: error in JVMTI AddCapabilities");
129
return JNI_ERR;
130
}
131
132
err = (*jvmti)->GetCapabilities(jvmti, &caps);
133
if (err != JVMTI_ERROR_NONE) {
134
ShowErrorMessage(jvmti, err,
135
"Agent_OnLoad: error in JVMTI GetCapabilities");
136
return JNI_ERR;
137
}
138
139
if (!caps.can_get_owned_monitor_stack_depth_info) {
140
fprintf(stderr, "Warning: GetOwnedMonitorStackDepthInfo is not implemented\n");
141
return JNI_ERR;
142
}
143
144
printf("Agent_OnLoad finished\n");
145
return JNI_OK;
146
}
147
148
JNIEXPORT jint JNICALL
149
Java_GetOwnedMonitorStackDepthInfoTest_verifyOwnedMonitors(JNIEnv *env, jclass cls) {
150
jthread thread;
151
jvmtiError err;
152
jvmtiThreadInfo threadInfo;
153
jint monitorCount;
154
jvmtiMonitorStackDepthInfo* stackDepthInfo;
155
jclass testClass;
156
jclass lock1Class;
157
jclass lock2Class;
158
159
jint status = PASSED;
160
161
jint idx = 0;
162
163
testClass = (*env)->FindClass(env, TEST_CLASS);
164
if (testClass == NULL) {
165
fprintf(stderr, "Error: Could not load class %s!\n", TEST_CLASS);
166
return FAILED;
167
}
168
169
lock1Class = (*env)->FindClass(env, LOCK1_CLASS);
170
if (lock1Class == NULL) {
171
fprintf(stderr, "Error: Could not load class %s!\n", LOCK1_CLASS);
172
return FAILED;
173
}
174
175
lock2Class = (*env)->FindClass(env, LOCK2_CLASS);
176
if (lock2Class == NULL) {
177
fprintf(stderr, "Error: Could not load class %s!\n", LOCK2_CLASS);
178
return FAILED;
179
}
180
181
err = (*jvmti) -> GetCurrentThread(jvmti, &thread);
182
if (err != JVMTI_ERROR_NONE) {
183
ShowErrorMessage(jvmti, err,
184
"VerifyOwnedMonitors: error in JVMTI GetCurrentThread");
185
return FAILED;
186
}
187
err = (*jvmti)->GetThreadInfo(jvmti, thread, &threadInfo);
188
if (err != JVMTI_ERROR_NONE) {
189
ShowErrorMessage(jvmti, err,
190
"VerifyOwnedMonitors: error in JVMTI GetThreadInfo");
191
return FAILED;
192
}
193
194
err = (*jvmti)->GetOwnedMonitorStackDepthInfo(jvmti, thread, &monitorCount, &stackDepthInfo);
195
if (err != JVMTI_ERROR_NONE) {
196
ShowErrorMessage(jvmti, err,
197
"VerifyOwnedMonitors: error in JVMTI GetOwnedMonitorStackDepthInfo");
198
return FAILED;
199
}
200
201
printf("VerifyOwnedMonitors: %s owns %d monitor(s)\n", threadInfo.name, monitorCount);
202
203
if (monitorCount != EXP_MONITOR_COUNT) {
204
fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid monitorCount, expected: %d, found: %d.\n",
205
EXP_MONITOR_COUNT, monitorCount);
206
status = FAILED;
207
}
208
for (idx = 0; idx < monitorCount; idx++) {
209
if (CheckLockObject(env, stackDepthInfo[idx].monitor, testClass) == JNI_TRUE) {
210
if (stackDepthInfo[idx].stack_depth != TEST_OBJECT_LOCK_DEPTH) {
211
fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid stack_depth for %s monitor, expected: %d, found: %d.\n",
212
TEST_CLASS, TEST_OBJECT_LOCK_DEPTH, stackDepthInfo[idx].stack_depth);
213
status = FAILED;
214
}
215
} else if (CheckLockObject(env, stackDepthInfo[idx].monitor, lock1Class) == JNI_TRUE) {
216
if (stackDepthInfo[idx].stack_depth != LOCK1_DEPTH) {
217
fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid stack_depth for %s monitor, expected: %d, found: %d.\n",
218
LOCK1_CLASS, LOCK1_DEPTH, stackDepthInfo[idx].stack_depth);
219
status = FAILED;
220
}
221
} else if (CheckLockObject(env, stackDepthInfo[idx].monitor, lock2Class) == JNI_TRUE) {
222
if (stackDepthInfo[idx].stack_depth != LOCK2_DEPTH) {
223
fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid stack_depth for %s monitor, expected: %d, found: %d.\n",
224
LOCK2_CLASS, LOCK2_DEPTH, stackDepthInfo[idx].stack_depth);
225
status = FAILED;
226
}
227
} else {
228
fprintf(stderr, "VerifyOwnedMonitors: "
229
"FAIL: monitor should be instance of %s, %s, or %s\n", TEST_CLASS, LOCK1_CLASS, LOCK2_CLASS);
230
status = FAILED;
231
232
}
233
}
234
235
(*jvmti)->Deallocate(jvmti, (unsigned char *) stackDepthInfo);
236
(*jvmti)->Deallocate(jvmti, (unsigned char *) threadInfo.name);
237
return status;
238
}
239
240
#ifdef __cplusplus
241
}
242
#endif
243
244