Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ExceptionCatch/excatch001/excatch001.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 *c_cls;
39
char *c_name;
40
char *c_sig;
41
jlocation c_loc;
42
} writable_exceptionInfo;
43
44
typedef struct {
45
const char *name;
46
const char *c_cls;
47
const char *c_name;
48
const char *c_sig;
49
jlocation c_loc;
50
} exceptionInfo;
51
52
static jvmtiEnv *jvmti = NULL;
53
static jvmtiCapabilities caps;
54
static jvmtiEventCallbacks callbacks;
55
static jint result = PASSED;
56
static jboolean printdump = JNI_FALSE;
57
static exceptionInfo exs[] = {
58
{ "Lnsk/jvmti/ExceptionCatch/excatch001c;",
59
"Lnsk/jvmti/ExceptionCatch/excatch001a;", "run", "()V", 14 },
60
{ "Ljava/lang/ArithmeticException;",
61
"Lnsk/jvmti/ExceptionCatch/excatch001a;", "run", "()V", 24 },
62
{ "Ljava/lang/ArrayIndexOutOfBoundsException;",
63
"Lnsk/jvmti/ExceptionCatch/excatch001a;", "run", "()V", 34 }
64
};
65
static int eventsCount = 0;
66
static int eventsExpected = 0;
67
68
void JNICALL
69
ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr,
70
jmethodID method, jlocation location, jobject exception) {
71
jvmtiError err;
72
jclass cls;
73
writable_exceptionInfo ex;
74
char *generic;
75
size_t i;
76
77
if (printdump == JNI_TRUE) {
78
printf(">>> retrieving ExceptionCatch info ...\n");
79
}
80
cls = env->GetObjectClass(exception);
81
err = jvmti_env->GetClassSignature(cls, &ex.name, &generic);
82
if (err != JVMTI_ERROR_NONE) {
83
printf("(GetClassSignature#e) unexpected error: %s (%d)\n",
84
TranslateError(err), err);
85
result = STATUS_FAILED;
86
return;
87
}
88
err = jvmti_env->GetMethodDeclaringClass(method, &cls);
89
if (err != JVMTI_ERROR_NONE) {
90
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
91
TranslateError(err), err);
92
result = STATUS_FAILED;
93
return;
94
}
95
err = jvmti_env->GetClassSignature(cls, &ex.c_cls, &generic);
96
if (err != JVMTI_ERROR_NONE) {
97
printf("(GetClassSignature#c) unexpected error: %s (%d)\n",
98
TranslateError(err), err);
99
result = STATUS_FAILED;
100
return;
101
}
102
err = jvmti_env->GetMethodName(method,
103
&ex.c_name, &ex.c_sig, &generic);
104
if (err != JVMTI_ERROR_NONE) {
105
printf("(GetMethodName) unexpected error: %s (%d)\n",
106
TranslateError(err), err);
107
result = STATUS_FAILED;
108
return;
109
}
110
ex.c_loc = location;
111
if (printdump == JNI_TRUE) {
112
printf(">>> %s\n", ex.name);
113
printf(">>> catch at %s.%s%s:0x%x%08x\n",
114
ex.c_cls, ex.c_name, ex.c_sig,
115
(jint)(ex.c_loc >> 32), (jint)ex.c_loc);
116
printf(">>> ... done\n");
117
}
118
for (i = 0; i < sizeof(exs)/sizeof(exceptionInfo); i++) {
119
if (ex.name != NULL && strcmp(ex.name, exs[i].name) == 0
120
&& ex.c_cls != NULL && strcmp(ex.c_cls, exs[i].c_cls) == 0
121
&& ex.c_name != NULL && strcmp(ex.c_name, exs[i].c_name) == 0
122
&& ex.c_sig != NULL && strcmp(ex.c_sig, exs[i].c_sig) == 0
123
&& ex.c_loc == exs[i].c_loc) {
124
eventsCount++;
125
break;
126
}
127
}
128
if (i == sizeof(exs)/sizeof(exceptionInfo)) {
129
printf("Unexpected exception catch event:\n");
130
printf(" %s\n", ex.name);
131
printf(" catch at %s.%s%s:0x%x%08x\n",
132
ex.c_cls, ex.c_name, ex.c_sig,
133
(jint)(ex.c_loc >> 32), (jint)ex.c_loc);
134
result = STATUS_FAILED;
135
}
136
}
137
138
#ifdef STATIC_BUILD
139
JNIEXPORT jint JNICALL Agent_OnLoad_excatch001(JavaVM *jvm, char *options, void *reserved) {
140
return Agent_Initialize(jvm, options, reserved);
141
}
142
JNIEXPORT jint JNICALL Agent_OnAttach_excatch001(JavaVM *jvm, char *options, void *reserved) {
143
return Agent_Initialize(jvm, options, reserved);
144
}
145
JNIEXPORT jint JNI_OnLoad_excatch001(JavaVM *jvm, char *options, void *reserved) {
146
return JNI_VERSION_1_8;
147
}
148
#endif
149
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
150
jvmtiError err;
151
jint res;
152
153
if (options != NULL && strcmp(options, "printdump") == 0) {
154
printdump = JNI_TRUE;
155
}
156
157
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
158
if (res != JNI_OK || jvmti == NULL) {
159
printf("Wrong result of a valid call to GetEnv!\n");
160
return JNI_ERR;
161
}
162
163
err = jvmti->GetPotentialCapabilities(&caps);
164
if (err != JVMTI_ERROR_NONE) {
165
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
166
TranslateError(err), err);
167
return JNI_ERR;
168
}
169
170
err = jvmti->AddCapabilities(&caps);
171
if (err != JVMTI_ERROR_NONE) {
172
printf("(AddCapabilities) unexpected error: %s (%d)\n",
173
TranslateError(err), err);
174
return JNI_ERR;
175
}
176
177
err = jvmti->GetCapabilities(&caps);
178
if (err != JVMTI_ERROR_NONE) {
179
printf("(GetCapabilities) unexpected error: %s (%d)\n",
180
TranslateError(err), err);
181
return JNI_ERR;
182
}
183
184
if (caps.can_generate_exception_events) {
185
callbacks.ExceptionCatch = &ExceptionCatch;
186
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
187
if (err != JVMTI_ERROR_NONE) {
188
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
189
TranslateError(err), err);
190
return JNI_ERR;
191
}
192
} else {
193
printf("Warning: Exception event is not implemented\n");
194
}
195
196
return JNI_OK;
197
}
198
199
JNIEXPORT jint JNICALL
200
Java_nsk_jvmti_ExceptionCatch_excatch001_check(JNIEnv *env, jclass cls) {
201
jvmtiError err;
202
jclass clz;
203
jmethodID mid;
204
205
if (jvmti == NULL) {
206
printf("JVMTI client was not properly loaded!\n");
207
return STATUS_FAILED;
208
}
209
210
if (!caps.can_generate_exception_events) {
211
return result;
212
}
213
214
clz = env->FindClass("nsk/jvmti/ExceptionCatch/excatch001c");
215
if (clz == NULL) {
216
printf("Cannot find excatch001c class!\n");
217
return STATUS_FAILED;
218
}
219
clz = env->FindClass("nsk/jvmti/ExceptionCatch/excatch001b");
220
if (clz == NULL) {
221
printf("Cannot find excatch001b class!\n");
222
return STATUS_FAILED;
223
}
224
clz = env->FindClass("nsk/jvmti/ExceptionCatch/excatch001a");
225
if (clz == NULL) {
226
printf("Cannot find excatch001a class!\n");
227
return STATUS_FAILED;
228
}
229
mid = env->GetStaticMethodID(clz, "run", "()V");
230
if (mid == NULL) {
231
printf("Cannot find method run!\n");
232
return STATUS_FAILED;
233
}
234
235
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
236
JVMTI_EVENT_EXCEPTION_CATCH, NULL);
237
if (err == JVMTI_ERROR_NONE) {
238
eventsExpected = sizeof(exs)/sizeof(exceptionInfo);
239
} else {
240
printf("Failed to enable JVMTI_EVENT_EXCEPTION_CATCH: %s (%d)\n",
241
TranslateError(err), err);
242
result = STATUS_FAILED;
243
}
244
245
env->CallStaticVoidMethod(clz, mid);
246
247
err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
248
JVMTI_EVENT_EXCEPTION_CATCH, NULL);
249
if (err != JVMTI_ERROR_NONE) {
250
printf("Failed to disable JVMTI_EVENT_EXCEPTION_CATCH: %s (%d)\n",
251
TranslateError(err), err);
252
result = STATUS_FAILED;
253
}
254
255
if (eventsCount != eventsExpected) {
256
printf("Wrong number of exception catch events: %d, expected: %d\n",
257
eventsCount, eventsExpected);
258
result = STATUS_FAILED;
259
}
260
return result;
261
}
262
263
}
264
265