Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/FieldAccess/fieldacc004/fieldacc004.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 <inttypes.h>
27
#include "jvmti.h"
28
#include "agent_common.h"
29
#include "JVMTITools.h"
30
31
extern "C" {
32
33
34
#define PASSED 0
35
#define STATUS_FAILED 2
36
37
typedef struct {
38
jfieldID fid;
39
char *m_cls;
40
char *m_name;
41
char *m_sig;
42
jlocation loc;
43
char *f_cls;
44
char *f_name;
45
char *f_sig;
46
jboolean is_static;
47
} writable_watch_info;
48
49
typedef struct {
50
jfieldID fid;
51
const char *m_cls;
52
const char *m_name;
53
const char *m_sig;
54
jlocation loc;
55
const char *f_cls;
56
const char *f_name;
57
const char *f_sig;
58
jboolean is_static;
59
} watch_info;
60
61
static jvmtiEnv *jvmti;
62
static jvmtiEventCallbacks callbacks;
63
static jvmtiCapabilities caps;
64
static jint result = PASSED;
65
static jboolean printdump = JNI_FALSE;
66
static int eventsExpected = 0;
67
static int eventsCount = 0;
68
static watch_info watches[] = {
69
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc004a;", "run", "()I", 2,
70
"Lnsk/jvmti/FieldAccess/fieldacc004a;", "interfaceObject",
71
"Ljava/lang/Object;", JNI_TRUE },
72
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc004a;", "run", "()I", 12,
73
"Lnsk/jvmti/FieldAccess/fieldacc004a;", "interfaceArrInt",
74
"[I", JNI_TRUE }
75
};
76
77
void JNICALL FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env,
78
jthread thr, jmethodID method,
79
jlocation location, jclass field_klass, jobject obj, jfieldID field) {
80
jvmtiError err;
81
jclass cls;
82
writable_watch_info watch;
83
char *generic;
84
size_t i;
85
86
eventsCount++;
87
if (printdump == JNI_TRUE) {
88
printf(">>> retrieving access watch info ...\n");
89
}
90
watch.fid = field;
91
watch.loc = location;
92
watch.is_static = (obj == NULL) ? JNI_TRUE : JNI_FALSE;
93
err = jvmti_env->GetMethodDeclaringClass(method, &cls);
94
if (err != JVMTI_ERROR_NONE) {
95
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
96
TranslateError(err), err);
97
result = STATUS_FAILED;
98
return;
99
}
100
err = jvmti_env->GetClassSignature(cls,
101
&watch.m_cls, &generic);
102
if (err != JVMTI_ERROR_NONE) {
103
printf("(GetClassSignature) unexpected error: %s (%d)\n",
104
TranslateError(err), err);
105
result = STATUS_FAILED;
106
return;
107
}
108
err = jvmti_env->GetMethodName(method,
109
&watch.m_name, &watch.m_sig, &generic);
110
if (err != JVMTI_ERROR_NONE) {
111
printf("(GetMethodName) unexpected error: %s (%d)\n",
112
TranslateError(err), err);
113
result = STATUS_FAILED;
114
return;
115
}
116
err = jvmti_env->GetClassSignature(field_klass,
117
&watch.f_cls, &generic);
118
if (err != JVMTI_ERROR_NONE) {
119
printf("(GetClassSignature) unexpected error: %s (%d)\n",
120
TranslateError(err), err);
121
result = STATUS_FAILED;
122
return;
123
}
124
err = jvmti_env->GetFieldName(field_klass, field,
125
&watch.f_name, &watch.f_sig, &generic);
126
if (err != JVMTI_ERROR_NONE) {
127
printf("(GetFieldName) unexpected error: %s (%d)\n",
128
TranslateError(err), err);
129
result = STATUS_FAILED;
130
return;
131
}
132
if (printdump == JNI_TRUE) {
133
printf(">>> class: \"%s\"\n", watch.m_cls);
134
printf(">>> method: \"%s%s\"\n", watch.m_name, watch.m_sig);
135
printf(">>> location: 0x%x%08x\n",
136
(jint)(watch.loc >> 32), (jint)watch.loc);
137
printf(">>> field cls: \"%s\"\n", watch.f_cls);
138
printf(">>> field: \"%s:%s\"\n", watch.f_name, watch.f_sig);
139
printf(">>> object: 0x%p\n", obj);
140
printf(">>> ... done\n");
141
}
142
for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {
143
if (watch.fid == watches[i].fid) {
144
if (watch.m_cls == NULL ||
145
strcmp(watch.m_cls, watches[i].m_cls) != 0) {
146
printf("(watch#%" PRIuPTR ") wrong class: \"%s\", expected: \"%s\"\n",
147
i, watch.m_cls, watches[i].m_cls);
148
result = STATUS_FAILED;
149
}
150
if (watch.m_name == NULL ||
151
strcmp(watch.m_name, watches[i].m_name) != 0) {
152
printf("(watch#%" PRIuPTR ") wrong method name: \"%s\"",
153
i, watch.m_name);
154
printf(", expected: \"%s\"\n", watches[i].m_name);
155
result = STATUS_FAILED;
156
}
157
if (watch.m_sig == NULL ||
158
strcmp(watch.m_sig, watches[i].m_sig) != 0) {
159
printf("(watch#%" PRIuPTR ") wrong method sig: \"%s\"",
160
i, watch.m_sig);
161
printf(", expected: \"%s\"\n", watches[i].m_sig);
162
result = STATUS_FAILED;
163
}
164
if (watch.loc != watches[i].loc) {
165
printf("(watch#%" PRIuPTR ") wrong location: 0x%x%08x",
166
i, (jint)(watch.loc >> 32), (jint)watch.loc);
167
printf(", expected: 0x%x%08x\n",
168
(jint)(watches[i].loc >> 32), (jint)watches[i].loc);
169
result = STATUS_FAILED;
170
}
171
if (watch.f_name == NULL ||
172
strcmp(watch.f_name, watches[i].f_name) != 0) {
173
printf("(watch#%" PRIuPTR ") wrong field name: \"%s\"",
174
i, watch.f_name);
175
printf(", expected: \"%s\"\n", watches[i].f_name);
176
result = STATUS_FAILED;
177
}
178
if (watch.f_sig == NULL ||
179
strcmp(watch.f_sig, watches[i].f_sig) != 0) {
180
printf("(watch#%" PRIuPTR ") wrong field sig: \"%s\"",
181
i, watch.f_sig);
182
printf(", expected: \"%s\"\n", watches[i].f_sig);
183
result = STATUS_FAILED;
184
}
185
if (watch.is_static != watches[i].is_static) {
186
printf("(watch#%" PRIuPTR ") wrong field type: %s", i,
187
(watch.is_static == JNI_TRUE) ? "static" : "instance");
188
printf(", expected: %s\n",
189
(watches[i].is_static == JNI_TRUE) ? "static" : "instance");
190
result = STATUS_FAILED;
191
}
192
return;
193
}
194
}
195
printf("Unexpected field access catched: 0x%p\n", watch.fid);
196
result = STATUS_FAILED;
197
}
198
199
#ifdef STATIC_BUILD
200
JNIEXPORT jint JNICALL Agent_OnLoad_fieldacc004(JavaVM *jvm, char *options, void *reserved) {
201
return Agent_Initialize(jvm, options, reserved);
202
}
203
JNIEXPORT jint JNICALL Agent_OnAttach_fieldacc004(JavaVM *jvm, char *options, void *reserved) {
204
return Agent_Initialize(jvm, options, reserved);
205
}
206
JNIEXPORT jint JNI_OnLoad_fieldacc004(JavaVM *jvm, char *options, void *reserved) {
207
return JNI_VERSION_1_8;
208
}
209
#endif
210
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
211
jvmtiError err;
212
jint res;
213
214
if (options != NULL && strcmp(options, "printdump") == 0) {
215
printdump = JNI_TRUE;
216
}
217
218
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
219
if (res != JNI_OK || jvmti == NULL) {
220
printf("Wrong result of a valid call to GetEnv!\n");
221
return JNI_ERR;
222
}
223
224
err = jvmti->GetPotentialCapabilities(&caps);
225
if (err != JVMTI_ERROR_NONE) {
226
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
227
TranslateError(err), err);
228
return JNI_ERR;
229
}
230
231
err = jvmti->AddCapabilities(&caps);
232
if (err != JVMTI_ERROR_NONE) {
233
printf("(AddCapabilities) unexpected error: %s (%d)\n",
234
TranslateError(err), err);
235
return JNI_ERR;
236
}
237
238
err = jvmti->GetCapabilities(&caps);
239
if (err != JVMTI_ERROR_NONE) {
240
printf("(GetCapabilities) unexpected error: %s (%d)\n",
241
TranslateError(err), err);
242
return JNI_ERR;
243
}
244
245
if (caps.can_generate_field_access_events) {
246
callbacks.FieldAccess = &FieldAccess;
247
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
248
if (err != JVMTI_ERROR_NONE) {
249
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
250
TranslateError(err), err);
251
return JNI_ERR;
252
}
253
254
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
255
JVMTI_EVENT_FIELD_ACCESS, NULL);
256
if (err != JVMTI_ERROR_NONE) {
257
printf("Failed to enable JVMTI_EVENT_FIELD_ACCESS: %s (%d)\n",
258
TranslateError(err), err);
259
return JNI_ERR;
260
}
261
} else {
262
printf("Warning: FieldAccess watch is not implemented\n");
263
}
264
265
return JNI_OK;
266
}
267
268
JNIEXPORT void JNICALL
269
Java_nsk_jvmti_FieldAccess_fieldacc004_getReady(JNIEnv *env, jclass klass) {
270
jvmtiError err;
271
jclass cls;
272
size_t i;
273
274
if (!caps.can_generate_field_access_events) {
275
return;
276
}
277
278
if (printdump == JNI_TRUE) {
279
printf(">>> setting field access watches ...\n");
280
}
281
for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {
282
cls = env->FindClass(watches[i].f_cls);
283
if (cls == NULL) {
284
printf("Cannot find %s class!\n", watches[i].f_cls);
285
result = STATUS_FAILED;
286
return;
287
}
288
if (watches[i].is_static == JNI_TRUE) {
289
watches[i].fid = env->GetStaticFieldID(
290
cls, watches[i].f_name, watches[i].f_sig);
291
} else {
292
watches[i].fid = env->GetFieldID(
293
cls, watches[i].f_name, watches[i].f_sig);
294
}
295
if (watches[i].fid == NULL) {
296
printf("Cannot get field ID for \"%s:%s\"\n",
297
watches[i].f_name, watches[i].f_sig);
298
result = STATUS_FAILED;
299
return;
300
}
301
err = jvmti->SetFieldAccessWatch(cls, watches[i].fid);
302
if (err == JVMTI_ERROR_NONE) {
303
eventsExpected++;
304
} else {
305
printf("(SetFieldAccessWatch#%" PRIuPTR ") unexpected error: %s (%d)\n",
306
i, TranslateError(err), err);
307
result = STATUS_FAILED;
308
}
309
}
310
if (printdump == JNI_TRUE) {
311
printf(">>> ... done\n");
312
}
313
}
314
315
JNIEXPORT jint JNICALL
316
Java_nsk_jvmti_FieldAccess_fieldacc004_check(JNIEnv *env, jclass cls) {
317
if (eventsCount != eventsExpected) {
318
printf("Wrong number of field access events: %d, expected: %d\n",
319
eventsCount, eventsExpected);
320
result = STATUS_FAILED;
321
}
322
return result;
323
}
324
325
}
326
327