Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/Exception/exception001/exception001.cpp
40951 views
1
/*
2
* Copyright (c) 2003, 2020, 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 "agent_common.h"
28
#include "JVMTITools.h"
29
30
extern "C" {
31
32
33
#define PASSED 0
34
#define STATUS_FAILED 2
35
36
typedef struct {
37
char *name;
38
char *t_cls;
39
char *t_name;
40
char *t_sig;
41
jlocation t_loc;
42
char *c_cls;
43
char *c_name;
44
char *c_sig;
45
jlocation c_loc;
46
} writable_exceptionInfo;
47
48
typedef struct {
49
const char *name;
50
const char *t_cls;
51
const char *t_name;
52
const char *t_sig;
53
jlocation t_loc;
54
const char *c_cls;
55
const char *c_name;
56
const char *c_sig;
57
jlocation c_loc;
58
} exceptionInfo;
59
60
static jvmtiEnv *jvmti = NULL;
61
static jvmtiCapabilities caps;
62
static jvmtiEventCallbacks callbacks;
63
static jint result = PASSED;
64
static jboolean printdump = JNI_FALSE;
65
static exceptionInfo exs[] = {
66
{ "Lnsk/jvmti/Exception/exception001c;",
67
"Lnsk/jvmti/Exception/exception001b;", "meth1", "()V", 7,
68
"Lnsk/jvmti/Exception/exception001a;", "run", "()V", 14 },
69
{ "Ljava/lang/ArithmeticException;",
70
"Lnsk/jvmti/Exception/exception001b;", "meth2", "(I)I", 3,
71
"Lnsk/jvmti/Exception/exception001a;", "run", "()V", 24 },
72
{ "Ljava/lang/ArrayIndexOutOfBoundsException;",
73
"Lnsk/jvmti/Exception/exception001b;", "meth3", "(I)I", 10,
74
"Lnsk/jvmti/Exception/exception001a;", "run", "()V", 34 }
75
};
76
static int eventsCount = 0;
77
static int eventsExpected = 0;
78
79
void JNICALL
80
Exception(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr,
81
jmethodID method, jlocation location, jobject exception,
82
jmethodID catch_method, jlocation catch_location) {
83
jvmtiError err;
84
writable_exceptionInfo ex;
85
jclass cls;
86
char *generic;
87
size_t i;
88
89
if (printdump == JNI_TRUE) {
90
printf(">>> retrieving Exception info ...\n");
91
}
92
cls = env->GetObjectClass(exception);
93
err = jvmti_env->GetClassSignature(cls, &ex.name, &generic);
94
if (err != JVMTI_ERROR_NONE) {
95
printf("(GetClassSignature) unexpected error: %s (%d)\n",
96
TranslateError(err), err);
97
result = STATUS_FAILED;
98
return;
99
}
100
err = jvmti_env->GetMethodDeclaringClass(method, &cls);
101
if (err != JVMTI_ERROR_NONE) {
102
printf("(GetMethodDeclaringClass#t) unexpected error: %s (%d)\n",
103
TranslateError(err), err);
104
result = STATUS_FAILED;
105
return;
106
}
107
err = jvmti_env->GetClassSignature(cls, &ex.t_cls, &generic);
108
if (err != JVMTI_ERROR_NONE) {
109
printf("(GetClassSignature#t) unexpected error: %s (%d)\n",
110
TranslateError(err), err);
111
result = STATUS_FAILED;
112
return;
113
}
114
err = jvmti_env->GetMethodName(method,
115
&ex.t_name, &ex.t_sig, &generic);
116
if (err != JVMTI_ERROR_NONE) {
117
printf("(GetMethodName#t) unexpected error: %s (%d)\n",
118
TranslateError(err), err);
119
result = STATUS_FAILED;
120
return;
121
}
122
ex.t_loc = location;
123
err = jvmti_env->GetMethodDeclaringClass(catch_method, &cls);
124
if (err != JVMTI_ERROR_NONE) {
125
printf("(GetMethodDeclaringClass#c) unexpected error: %s (%d)\n",
126
TranslateError(err), err);
127
result = STATUS_FAILED;
128
return;
129
}
130
err = jvmti_env->GetClassSignature(cls, &ex.c_cls, &generic);
131
if (err != JVMTI_ERROR_NONE) {
132
printf("(GetClassSignature#c) unexpected error: %s (%d)\n",
133
TranslateError(err), err);
134
result = STATUS_FAILED;
135
return;
136
}
137
err = jvmti_env->GetMethodName(catch_method,
138
&ex.c_name, &ex.c_sig, &generic);
139
if (err != JVMTI_ERROR_NONE) {
140
printf("(GetMethodName#c) unexpected error: %s (%d)\n",
141
TranslateError(err), err);
142
result = STATUS_FAILED;
143
return;
144
}
145
ex.c_loc = catch_location;
146
if (printdump == JNI_TRUE) {
147
printf(">>> %s\n", ex.name);
148
printf(">>> thrown at %s.%s%s:0x%x%08x\n",
149
ex.t_cls, ex.t_name, ex.t_sig,
150
(jint)(ex.t_loc >> 32), (jint)ex.t_loc);
151
printf(">>> catch at %s.%s%s:0x%x%08x\n",
152
ex.c_cls, ex.c_name, ex.c_sig,
153
(jint)(ex.c_loc >> 32), (jint)ex.c_loc);
154
printf(">>> ... done\n");
155
}
156
for (i = 0; i < sizeof(exs)/sizeof(exceptionInfo); i++) {
157
if (ex.name != NULL && strcmp(ex.name, exs[i].name) == 0
158
&& ex.t_cls != NULL && strcmp(ex.t_cls, exs[i].t_cls) == 0
159
&& ex.t_name != NULL && strcmp(ex.t_name, exs[i].t_name) == 0
160
&& ex.t_sig != NULL && strcmp(ex.t_sig, exs[i].t_sig) == 0
161
&& ex.c_cls != NULL && strcmp(ex.c_cls, exs[i].c_cls) == 0
162
&& ex.c_name != NULL && strcmp(ex.c_name, exs[i].c_name) == 0
163
&& ex.c_sig != NULL && strcmp(ex.c_sig, exs[i].c_sig) == 0
164
&& ex.t_loc == exs[i].t_loc && ex.c_loc == exs[i].c_loc) {
165
eventsCount++;
166
break;
167
}
168
}
169
if (i == sizeof(exs)/sizeof(exceptionInfo)) {
170
printf("Unexpected exception event:\n");
171
printf(" %s\n", ex.name);
172
printf(" thrown at %s.%s%s:0x%x%08x\n",
173
ex.t_cls, ex.t_name, ex.t_sig,
174
(jint)(ex.t_loc >> 32), (jint)ex.t_loc);
175
printf(" catch at %s.%s%s:0x%x%08x\n",
176
ex.c_cls, ex.c_name, ex.c_sig,
177
(jint)(ex.c_loc >> 32), (jint)ex.c_loc);
178
result = STATUS_FAILED;
179
}
180
}
181
182
#ifdef STATIC_BUILD
183
JNIEXPORT jint JNICALL Agent_OnLoad_exception001(JavaVM *jvm, char *options, void *reserved) {
184
return Agent_Initialize(jvm, options, reserved);
185
}
186
JNIEXPORT jint JNICALL Agent_OnAttach_exception001(JavaVM *jvm, char *options, void *reserved) {
187
return Agent_Initialize(jvm, options, reserved);
188
}
189
JNIEXPORT jint JNI_OnLoad_exception001(JavaVM *jvm, char *options, void *reserved) {
190
return JNI_VERSION_1_8;
191
}
192
#endif
193
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
194
jvmtiError err;
195
jint res;
196
197
if (options != NULL && strcmp(options, "printdump") == 0) {
198
printdump = JNI_TRUE;
199
}
200
201
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
202
if (res != JNI_OK || jvmti == NULL) {
203
printf("Wrong result of a valid call to GetEnv!\n");
204
return JNI_ERR;
205
}
206
207
err = jvmti->GetPotentialCapabilities(&caps);
208
if (err != JVMTI_ERROR_NONE) {
209
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
210
TranslateError(err), err);
211
return JNI_ERR;
212
}
213
214
err = jvmti->AddCapabilities(&caps);
215
if (err != JVMTI_ERROR_NONE) {
216
printf("(AddCapabilities) unexpected error: %s (%d)\n",
217
TranslateError(err), err);
218
return JNI_ERR;
219
}
220
221
err = jvmti->GetCapabilities(&caps);
222
if (err != JVMTI_ERROR_NONE) {
223
printf("(GetCapabilities) unexpected error: %s (%d)\n",
224
TranslateError(err), err);
225
return JNI_ERR;
226
}
227
228
if (caps.can_generate_exception_events) {
229
callbacks.Exception = &Exception;
230
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
231
if (err != JVMTI_ERROR_NONE) {
232
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
233
TranslateError(err), err);
234
return JNI_ERR;
235
}
236
} else {
237
printf("Warning: Exception event is not implemented\n");
238
}
239
240
return JNI_OK;
241
}
242
243
JNIEXPORT jint JNICALL
244
Java_nsk_jvmti_Exception_exception001_check(JNIEnv *env, jclass cls) {
245
jvmtiError err;
246
jthread thread;
247
jclass clz;
248
jmethodID mid;
249
250
if (jvmti == NULL) {
251
printf("JVMTI client was not properly loaded!\n");
252
return STATUS_FAILED;
253
}
254
255
if (!caps.can_generate_exception_events) {
256
return result;
257
}
258
259
clz = env->FindClass("nsk/jvmti/Exception/exception001c");
260
if (clz == NULL) {
261
printf("Cannot find exception001c class!\n");
262
return STATUS_FAILED;
263
}
264
clz = env->FindClass("nsk/jvmti/Exception/exception001b");
265
if (clz == NULL) {
266
printf("Cannot find exception001b class!\n");
267
return STATUS_FAILED;
268
}
269
clz = env->FindClass("nsk/jvmti/Exception/exception001a");
270
if (clz == NULL) {
271
printf("Cannot find exception001a class!\n");
272
return STATUS_FAILED;
273
}
274
mid = env->GetStaticMethodID(clz, "run", "()V");
275
if (mid == NULL) {
276
printf("Cannot find method run!\n");
277
return STATUS_FAILED;
278
}
279
280
err = jvmti->GetCurrentThread(&thread);
281
if (err != JVMTI_ERROR_NONE) {
282
printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
283
result = STATUS_FAILED;
284
return STATUS_FAILED;
285
}
286
287
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
288
JVMTI_EVENT_EXCEPTION, thread);
289
if (err == JVMTI_ERROR_NONE) {
290
eventsExpected = sizeof(exs)/sizeof(exceptionInfo);
291
} else {
292
printf("Failed to enable JVMTI_EVENT_EXCEPTION: %s (%d)\n",
293
TranslateError(err), err);
294
result = STATUS_FAILED;
295
}
296
297
env->CallStaticVoidMethod(clz, mid);
298
299
err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
300
JVMTI_EVENT_EXCEPTION, thread);
301
if (err != JVMTI_ERROR_NONE) {
302
printf("Failed to disable JVMTI_EVENT_EXCEPTION: %s (%d)\n",
303
TranslateError(err), err);
304
result = STATUS_FAILED;
305
}
306
307
if (eventsCount != eventsExpected) {
308
printf("Wrong number of exception events: %d, expected: %d\n",
309
eventsCount, eventsExpected);
310
result = STATUS_FAILED;
311
}
312
return result;
313
}
314
315
}
316
317