43
44 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS203/hs203t003/MyThread"
45 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS203/hs203t003/MyThread;"
46 #define SEARCH_NAME "nsk/jvmti/scenarios/hotswap/HS203/hs203t003/MyThread"
47 #define FIELDNAME "threadState"
48 #define TYPE "I"
49
50 static jint redefineNumber;
51 static jvmtiEnv * jvmti;
52 static int redefineCnt=0;
53
54 JNIEXPORT void JNICALL callbackClassPrepare(jvmtiEnv *jvmti_env,
55 JNIEnv* jni,
56 jthread thread,
57 jclass klass) {
58 char * className;
59 char * generic;
60 redefineNumber=0;
61 className=NULL;
62 generic=NULL;
63 if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB4(GetClassSignature,
64 jvmti_env, klass, &className, &generic) ) ) {
65 nsk_printf("#error Agent :: while getting classname Signature.\n");
66 nsk_jvmti_agentFailed();
67 } else {
68 if (strcmp(className,CLASS_NAME) == 0) {
69 jfieldID field;
70 /* get the field id and set watch on that .*/
71 if (! NSK_JNI_VERIFY(jni, (field = NSK_CPP_STUB4(GetFieldID,
72 jni, klass, FIELDNAME, TYPE)) != NULL) ) {
73 nsk_printf(" Agent :: (*JNI)->GetFieldID(jni, ... ) returns `null`.\n");
74 nsk_jvmti_agentFailed();
75 } else if ( ! NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetFieldAccessWatch,
76 jvmti_env, klass, field) ) ) {
77 nsk_printf("#error Agent :: occured while jvmti->SetFieldAccessWatch(... ) .\n");
78 nsk_jvmti_agentFailed();
79 }
80 }
81 }
82 }
83
84 JNIEXPORT void JNICALL callbackFieldAccess(jvmtiEnv *jvmti_env,
85 JNIEnv* jni,
86 jthread thread,
87 jmethodID method,
88 jlocation location,
89 jclass field_klass,
90 jobject object,
91 jfieldID field) {
92 jclass clas;
93 char fileName[512];
94 if (redefineCnt < 10) {
95 redefineCnt++;
96 return;
97 }
98 redefineNumber=0;
99 if (! NSK_JNI_VERIFY(jni, (clas = NSK_CPP_STUB2(FindClass, jni, SEARCH_NAME)) != NULL) ) {
100 nsk_printf(" Agent :: (*JNI)->FindClass(jni, %s) returns `null`.\n",SEARCH_NAME);
101 nsk_jvmti_agentFailed();
102 } else {
103 nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName,
104 sizeof(fileName)/sizeof(char));
105 if ( nsk_jvmti_redefineClass(jvmti_env, clas, fileName ) != NSK_TRUE) {
106 nsk_printf(" Agent :: Failed to redefine.\n");
107 nsk_jvmti_agentFailed();
108 } else {
109 nsk_printf(" Agent :: Redefined.\n");
110 nsk_printf(" Agent :: Suspendeding thread.\n");
111 /* pop the current working frame. */
112 if ( ! NSK_JVMTI_VERIFY( NSK_CPP_STUB2(SuspendThread, jvmti_env, thread) ) ) {
113 nsk_printf("#error Agent :: occured suspending Thread.\n");
114 nsk_jvmti_agentFailed();
115 } else {
116 nsk_printf(" Agent :: Succeded in suspending.\n");
117 }
118 }
119 }
120 }
121
122 #ifdef STATIC_BUILD
123 JNIEXPORT jint JNICALL Agent_OnLoad_hs203t003(JavaVM *jvm, char *options, void *reserved) {
124 return Agent_Initialize(jvm, options, reserved);
125 }
126 JNIEXPORT jint JNICALL Agent_OnAttach_hs203t003(JavaVM *jvm, char *options, void *reserved) {
127 return Agent_Initialize(jvm, options, reserved);
128 }
129 JNIEXPORT jint JNI_OnLoad_hs203t003(JavaVM *jvm, char *options, void *reserved) {
130 return JNI_VERSION_1_8;
131 }
132 #endif
133 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
134 if ( ! NSK_VERIFY ( JNI_OK == NSK_CPP_STUB3(GetEnv, vm,
135 (void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
136 nsk_printf(" Agent :: Could not load JVMTI interface.\n");
137 return JNI_ERR;
138 } else {
139 jvmtiCapabilities caps;
140 jvmtiEventCallbacks eventCallbacks;
141 if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
142 nsk_printf("#error Agent :: Failed to parse options.\n");
143 return JNI_ERR;
144 }
145 memset(&caps, 0, sizeof(caps));
146 caps.can_redefine_classes = 1;
147 caps.can_suspend=1;
148 caps.can_pop_frame=1;
149 caps.can_generate_all_class_hook_events=1;
150 caps.can_generate_field_access_events=1;
151 if (! NSK_JVMTI_VERIFY ( NSK_CPP_STUB2(AddCapabilities, jvmti, &caps) )) {
152 nsk_printf("#error Agent :: while adding capabilities.\n");
153 return JNI_ERR;
154 }
155 memset(&eventCallbacks, 0, sizeof(eventCallbacks));
156 eventCallbacks.ClassPrepare =callbackClassPrepare;
157 eventCallbacks.FieldAccess= callbackFieldAccess;
158 if (!NSK_JVMTI_VERIFY(
159 NSK_CPP_STUB3(SetEventCallbacks, jvmti,
160 &eventCallbacks, sizeof(eventCallbacks)))) {
161 nsk_printf("#error Agent :: while setting event callbacks.\n");
162 return JNI_ERR;
163 }
164 if ( ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_CLASS_PREPARE, NULL)
165 == NSK_TRUE ) &&
166 ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_FIELD_ACCESS, NULL)
167 == NSK_TRUE ) ) {
168 nsk_printf(" Agent :: Notifications are enabled.\n");
169 } else {
170 nsk_printf("#error Agent :: Eanableing Notifications.\n");
171 return JNI_ERR;
172 }
173 }
174 return JNI_OK;
175 }
176
177 JNIEXPORT jboolean JNICALL
178 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t003_hs203t003_isSuspended(JNIEnv * jni,
179 jclass clas,
180 jthread thread) {
181 jboolean retvalue;
182 jint state;
183 retvalue = JNI_FALSE;
184 if ( ! NSK_JVMTI_VERIFY( NSK_CPP_STUB3(GetThreadState, jvmti, thread, &state) ) ) {
185 nsk_printf(" Agent :: Error while getting thread state.\n");
186 nsk_jvmti_agentFailed();
187 } else {
188 if ( state & JVMTI_THREAD_STATE_SUSPENDED) {
189 retvalue = JNI_TRUE;
190 }
191 }
192 return retvalue;
193 }
194
195 JNIEXPORT jboolean JNICALL
196 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t003_hs203t003_popThreadFrame(JNIEnv * jni,
197 jclass clas,
198 jthread thread) {
199 jboolean retvalue;
200 jint state;
201 retvalue = JNI_FALSE;
202 if ( ! NSK_JVMTI_VERIFY( NSK_CPP_STUB3(GetThreadState, jvmti, thread, &state) ) ) {
203 nsk_printf(" Agent :: Error while getting thread state.\n");
204 nsk_jvmti_agentFailed();
205 } else {
206 if ( state & JVMTI_THREAD_STATE_SUSPENDED) {
207 if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB2( PopFrame, jvmti, thread) ) ) {
208 nsk_printf("#error Agent :: while poping thread's frame.\n");
209 nsk_jvmti_agentFailed();
210 } else {
211 nsk_printf(" Agent :: poped thread frame.\n");
212 if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB4 (SetEventNotificationMode, jvmti,
213 JVMTI_DISABLE, JVMTI_EVENT_FIELD_ACCESS, NULL) ) ) {
214 nsk_printf("#error Agent :: failed to disable notification JVMTI_EVENT_FIELD ACCESS.\n");
215 nsk_jvmti_agentFailed();
216 } else {
217 nsk_printf(" Agent :: Disabled notification JVMTI_EVENT_FIELD ACCESS. \n");
218 retvalue = JNI_TRUE;
219 }
220 }
221 } else {
222 nsk_printf("#error Agent :: Thread was not suspened.");
223 nsk_jvmti_agentFailed();
224 }
225 }
226 return retvalue;
227 }
228
229 JNIEXPORT jboolean JNICALL
230 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t003_hs203t003_resumeThread(JNIEnv * jni,
231 jclass clas,
232 jthread thread) {
233 jboolean retvalue;
234 retvalue = JNI_FALSE;
235 if ( !NSK_JVMTI_VERIFY( NSK_CPP_STUB2 ( ResumeThread, jvmti, thread)) ) {
236 nsk_printf("#error Agent :: while resuming thread.\n");
237 nsk_jvmti_agentFailed();
238 } else {
239 nsk_printf(" Agent :: Thread resumed.\n");
240 retvalue= JNI_TRUE;
241 }
242 return retvalue;
243 }
244
245 }
|
43
44 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS203/hs203t003/MyThread"
45 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS203/hs203t003/MyThread;"
46 #define SEARCH_NAME "nsk/jvmti/scenarios/hotswap/HS203/hs203t003/MyThread"
47 #define FIELDNAME "threadState"
48 #define TYPE "I"
49
50 static jint redefineNumber;
51 static jvmtiEnv * jvmti;
52 static int redefineCnt=0;
53
54 JNIEXPORT void JNICALL callbackClassPrepare(jvmtiEnv *jvmti_env,
55 JNIEnv* jni,
56 jthread thread,
57 jclass klass) {
58 char * className;
59 char * generic;
60 redefineNumber=0;
61 className=NULL;
62 generic=NULL;
63 if ( ! NSK_JVMTI_VERIFY (jvmti_env->GetClassSignature(klass, &className, &generic) ) ) {
64 nsk_printf("#error Agent :: while getting classname Signature.\n");
65 nsk_jvmti_agentFailed();
66 } else {
67 if (strcmp(className,CLASS_NAME) == 0) {
68 jfieldID field;
69 /* get the field id and set watch on that .*/
70 if (! NSK_JNI_VERIFY(jni, (field = jni->GetFieldID(klass, FIELDNAME, TYPE)) != NULL) ) {
71 nsk_printf(" Agent :: (*JNI)->GetFieldID(jni, ... ) returns `null`.\n");
72 nsk_jvmti_agentFailed();
73 } else if ( ! NSK_JVMTI_VERIFY(jvmti_env->SetFieldAccessWatch(klass, field) ) ) {
74 nsk_printf("#error Agent :: occured while jvmti->SetFieldAccessWatch(... ) .\n");
75 nsk_jvmti_agentFailed();
76 }
77 }
78 }
79 }
80
81 JNIEXPORT void JNICALL callbackFieldAccess(jvmtiEnv *jvmti_env,
82 JNIEnv* jni,
83 jthread thread,
84 jmethodID method,
85 jlocation location,
86 jclass field_klass,
87 jobject object,
88 jfieldID field) {
89 jclass clas;
90 char fileName[512];
91 if (redefineCnt < 10) {
92 redefineCnt++;
93 return;
94 }
95 redefineNumber=0;
96 if (! NSK_JNI_VERIFY(jni, (clas = jni->FindClass(SEARCH_NAME)) != NULL) ) {
97 nsk_printf(" Agent :: (*JNI)->FindClass(jni, %s) returns `null`.\n",SEARCH_NAME);
98 nsk_jvmti_agentFailed();
99 } else {
100 nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName,
101 sizeof(fileName)/sizeof(char));
102 if ( nsk_jvmti_redefineClass(jvmti_env, clas, fileName ) != NSK_TRUE) {
103 nsk_printf(" Agent :: Failed to redefine.\n");
104 nsk_jvmti_agentFailed();
105 } else {
106 nsk_printf(" Agent :: Redefined.\n");
107 nsk_printf(" Agent :: Suspendeding thread.\n");
108 /* pop the current working frame. */
109 if ( ! NSK_JVMTI_VERIFY(jvmti_env->SuspendThread(thread) ) ) {
110 nsk_printf("#error Agent :: occured suspending Thread.\n");
111 nsk_jvmti_agentFailed();
112 } else {
113 nsk_printf(" Agent :: Succeded in suspending.\n");
114 }
115 }
116 }
117 }
118
119 #ifdef STATIC_BUILD
120 JNIEXPORT jint JNICALL Agent_OnLoad_hs203t003(JavaVM *jvm, char *options, void *reserved) {
121 return Agent_Initialize(jvm, options, reserved);
122 }
123 JNIEXPORT jint JNICALL Agent_OnAttach_hs203t003(JavaVM *jvm, char *options, void *reserved) {
124 return Agent_Initialize(jvm, options, reserved);
125 }
126 JNIEXPORT jint JNI_OnLoad_hs203t003(JavaVM *jvm, char *options, void *reserved) {
127 return JNI_VERSION_1_8;
128 }
129 #endif
130 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
131 if ( ! NSK_VERIFY ( JNI_OK == vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
132 nsk_printf(" Agent :: Could not load JVMTI interface.\n");
133 return JNI_ERR;
134 } else {
135 jvmtiCapabilities caps;
136 jvmtiEventCallbacks eventCallbacks;
137 if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
138 nsk_printf("#error Agent :: Failed to parse options.\n");
139 return JNI_ERR;
140 }
141 memset(&caps, 0, sizeof(caps));
142 caps.can_redefine_classes = 1;
143 caps.can_suspend=1;
144 caps.can_pop_frame=1;
145 caps.can_generate_all_class_hook_events=1;
146 caps.can_generate_field_access_events=1;
147 if (! NSK_JVMTI_VERIFY (jvmti->AddCapabilities(&caps) )) {
148 nsk_printf("#error Agent :: while adding capabilities.\n");
149 return JNI_ERR;
150 }
151 memset(&eventCallbacks, 0, sizeof(eventCallbacks));
152 eventCallbacks.ClassPrepare =callbackClassPrepare;
153 eventCallbacks.FieldAccess= callbackFieldAccess;
154 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
155 nsk_printf("#error Agent :: while setting event callbacks.\n");
156 return JNI_ERR;
157 }
158 if ( ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_CLASS_PREPARE, NULL)
159 == NSK_TRUE ) &&
160 ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_FIELD_ACCESS, NULL)
161 == NSK_TRUE ) ) {
162 nsk_printf(" Agent :: Notifications are enabled.\n");
163 } else {
164 nsk_printf("#error Agent :: Eanableing Notifications.\n");
165 return JNI_ERR;
166 }
167 }
168 return JNI_OK;
169 }
170
171 JNIEXPORT jboolean JNICALL
172 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t003_hs203t003_isSuspended(JNIEnv * jni,
173 jclass clas,
174 jthread thread) {
175 jboolean retvalue;
176 jint state;
177 retvalue = JNI_FALSE;
178 if ( ! NSK_JVMTI_VERIFY(jvmti->GetThreadState(thread, &state) ) ) {
179 nsk_printf(" Agent :: Error while getting thread state.\n");
180 nsk_jvmti_agentFailed();
181 } else {
182 if ( state & JVMTI_THREAD_STATE_SUSPENDED) {
183 retvalue = JNI_TRUE;
184 }
185 }
186 return retvalue;
187 }
188
189 JNIEXPORT jboolean JNICALL
190 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t003_hs203t003_popThreadFrame(JNIEnv * jni,
191 jclass clas,
192 jthread thread) {
193 jboolean retvalue;
194 jint state;
195 retvalue = JNI_FALSE;
196 if ( ! NSK_JVMTI_VERIFY(jvmti->GetThreadState(thread, &state) ) ) {
197 nsk_printf(" Agent :: Error while getting thread state.\n");
198 nsk_jvmti_agentFailed();
199 } else {
200 if ( state & JVMTI_THREAD_STATE_SUSPENDED) {
201 if ( ! NSK_JVMTI_VERIFY (jvmti->PopFrame(thread) ) ) {
202 nsk_printf("#error Agent :: while poping thread's frame.\n");
203 nsk_jvmti_agentFailed();
204 } else {
205 nsk_printf(" Agent :: poped thread frame.\n");
206 if ( ! NSK_JVMTI_VERIFY (
207 jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_FIELD_ACCESS, NULL) ) ) {
208 nsk_printf("#error Agent :: failed to disable notification JVMTI_EVENT_FIELD ACCESS.\n");
209 nsk_jvmti_agentFailed();
210 } else {
211 nsk_printf(" Agent :: Disabled notification JVMTI_EVENT_FIELD ACCESS. \n");
212 retvalue = JNI_TRUE;
213 }
214 }
215 } else {
216 nsk_printf("#error Agent :: Thread was not suspened.");
217 nsk_jvmti_agentFailed();
218 }
219 }
220 return retvalue;
221 }
222
223 JNIEXPORT jboolean JNICALL
224 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t003_hs203t003_resumeThread(JNIEnv * jni,
225 jclass clas,
226 jthread thread) {
227 jboolean retvalue;
228 retvalue = JNI_FALSE;
229 if ( !NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)) ) {
230 nsk_printf("#error Agent :: while resuming thread.\n");
231 nsk_jvmti_agentFailed();
232 } else {
233 nsk_printf(" Agent :: Thread resumed.\n");
234 retvalue= JNI_TRUE;
235 }
236 return retvalue;
237 }
238
239 }
|