Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClearFieldModificationWatch/clrfmodw001/clrfmodw001.cpp
40955 views
1
/*
2
* Copyright (c) 2003, 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 "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
const char *klass;
38
const char *name;
39
const char *sig;
40
int stat;
41
jfieldID fid;
42
} field;
43
44
static jvmtiEnv *jvmti;
45
static jvmtiEventCallbacks callbacks;
46
static jvmtiCapabilities caps;
47
static jint result = PASSED;
48
static jfieldID thrown_fid = NULL;
49
static field fields[] = {
50
{ "nsk/jvmti/ClearFieldModificationWatch/clrfmodw001", "fld0", "I", 0, NULL },
51
{ "nsk/jvmti/ClearFieldModificationWatch/clrfmodw001", "fld1", "I", 1, NULL },
52
{ "nsk/jvmti/ClearFieldModificationWatch/clrfmodw001", "fld2",
53
"Lnsk/jvmti/ClearFieldModificationWatch/clrfmodw001a;", 0, NULL },
54
{ "nsk/jvmti/ClearFieldModificationWatch/clrfmodw001a", "fld3", "[I", 0, NULL },
55
{ "nsk/jvmti/ClearFieldModificationWatch/clrfmodw001b", "fld4", "F", 0, NULL },
56
};
57
58
void switchWatch(JNIEnv *env, jint ind, jboolean on) {
59
jvmtiError err;
60
jclass cls;
61
field fld = fields[ind];
62
const char *msg;
63
64
cls = env->FindClass(fld.klass);
65
if (fld.fid == NULL) {
66
if (fld.stat) {
67
fields[ind].fid = env->GetStaticFieldID(cls, fld.name, fld.sig);
68
} else {
69
fields[ind].fid = env->GetFieldID(cls, fld.name, fld.sig);
70
}
71
}
72
73
if (on == JNI_TRUE) {
74
msg = "Set";
75
err = jvmti->SetFieldModificationWatch(cls, fields[ind].fid);
76
} else {
77
msg = "Clear";
78
err = jvmti->ClearFieldModificationWatch(cls, fields[ind].fid);
79
}
80
if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY &&
81
!caps.can_generate_field_modification_events) {
82
/* Ok, it's expected */
83
} else if (err != JVMTI_ERROR_NONE) {
84
printf("(%sFieldModificationWatch#%d) unexpected error: %s (%d)\n",
85
msg, ind, TranslateError(err), err);
86
result = STATUS_FAILED;
87
}
88
}
89
90
void JNICALL FieldModification(jvmtiEnv *jvmti_env, JNIEnv *env,
91
jthread thd, jmethodID mid, jlocation loc, jclass field_klass, jobject obj,
92
jfieldID field, char sig, jvalue new_value) {
93
thrown_fid = field;
94
}
95
96
#ifdef STATIC_BUILD
97
JNIEXPORT jint JNICALL Agent_OnLoad_clrfmodw001(JavaVM *jvm, char *options, void *reserved) {
98
return Agent_Initialize(jvm, options, reserved);
99
}
100
JNIEXPORT jint JNICALL Agent_OnAttach_clrfmodw001(JavaVM *jvm, char *options, void *reserved) {
101
return Agent_Initialize(jvm, options, reserved);
102
}
103
JNIEXPORT jint JNI_OnLoad_clrfmodw001(JavaVM *jvm, char *options, void *reserved) {
104
return JNI_VERSION_1_8;
105
}
106
#endif
107
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
108
jint res;
109
jvmtiError err;
110
111
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
112
if (res != JNI_OK || jvmti == NULL) {
113
printf("Wrong result of a valid call to GetEnv !\n");
114
return JNI_ERR;
115
}
116
117
err = jvmti->GetPotentialCapabilities(&caps);
118
if (err != JVMTI_ERROR_NONE) {
119
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
120
TranslateError(err), err);
121
return JNI_ERR;
122
}
123
124
err = jvmti->AddCapabilities(&caps);
125
if (err != JVMTI_ERROR_NONE) {
126
printf("(AddCapabilities) unexpected error: %s (%d)\n",
127
TranslateError(err), err);
128
return JNI_ERR;
129
}
130
131
err = jvmti->GetCapabilities(&caps);
132
if (err != JVMTI_ERROR_NONE) {
133
printf("(GetCapabilities) unexpected error: %s (%d)\n",
134
TranslateError(err), err);
135
return JNI_ERR;
136
}
137
138
if (caps.can_generate_field_modification_events) {
139
callbacks.FieldModification = &FieldModification;
140
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
141
if (err != JVMTI_ERROR_NONE) {
142
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
143
TranslateError(err), err);
144
return JNI_ERR;
145
}
146
147
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
148
JVMTI_EVENT_FIELD_MODIFICATION, NULL);
149
if (err != JVMTI_ERROR_NONE) {
150
printf("Failed to enable JVMTI_EVENT_FIELD_MODIFICATION: %s (%d)\n",
151
TranslateError(err), err);
152
return JNI_ERR;
153
}
154
} else {
155
printf("Warning: FieldModification watch is not implemented\n");
156
}
157
158
return JNI_OK;
159
}
160
161
JNIEXPORT void JNICALL
162
Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw001_setWatch(JNIEnv *env, jclass cls, jint fld_ind) {
163
switchWatch(env, fld_ind, JNI_TRUE);
164
}
165
166
JNIEXPORT void JNICALL
167
Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw001_clearWatch(JNIEnv *env, jclass cls, jint fld_ind) {
168
switchWatch(env, fld_ind, JNI_FALSE);
169
}
170
171
JNIEXPORT void JNICALL Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw001_touchfld0(JNIEnv *env, jobject obj) {
172
env->SetIntField(obj, fields[0].fid, 2000);
173
}
174
175
JNIEXPORT void JNICALL
176
Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw001_check(JNIEnv *env, jclass cls, jint fld_ind, jboolean flag) {
177
if (caps.can_generate_field_modification_events) {
178
if (flag == JNI_FALSE && thrown_fid != NULL) {
179
result = STATUS_FAILED;
180
printf("(Field %d) ", fld_ind);
181
printf("FieldModification event without modification watch set\n");
182
} else if (flag == JNI_TRUE && thrown_fid != fields[fld_ind].fid) {
183
result = STATUS_FAILED;
184
printf("(Field %d) thrown field ID expected: 0x%p, got: 0x%p\n",
185
fld_ind, fields[fld_ind].fid, thrown_fid);
186
}
187
thrown_fid = NULL;
188
}
189
}
190
191
JNIEXPORT jint JNICALL Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw001_getRes(JNIEnv *env, jclass cls) {
192
return result;
193
}
194
195
}
196
197