28 #include <string.h>
29 #include "jvmti_tools.h"
30 #include "JVMTITools.h"
31 #include "jni_tools.h"
32
33 extern "C" {
34 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS203/hs203t004/MyThread"
35 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS203/hs203t004/MyThread;"
36 #define METHOD_NAME "doTask2"
37
38 static jint redefineNumber;
39 static jvmtiEnv * jvmti;
40
41 JNIEXPORT void JNICALL callbackClassPrepare(jvmtiEnv *jvmti_env,
42 JNIEnv* jni,
43 jthread thread,
44 jclass klass) {
45 char * className;
46 className=NULL;
47
48 if (!NSK_JVMTI_VERIFY (jvmti_env->GetClassSignature(klass, &className, NULL) ) ) {
49 NSK_COMPLAIN0("#error Agent :: while getting classname.\n");
50 nsk_jvmti_agentFailed();
51 } else {
52 if (strcmp(className, CLASS_NAME) == 0) {
53 if (nsk_jvmti_enableNotification(jvmti_env, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL) == NSK_TRUE) {
54 NSK_DISPLAY0(" Agent :: notification enabled for COMPILED_METHOD_LOAD.\n");
55 if ( ! NSK_JVMTI_VERIFY ( jvmti_env->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD) ) ) {
56 NSK_COMPLAIN0("#error Agent :: occured while enabling compiled method events.\n");
57 nsk_jvmti_agentFailed();
58 }
59 }
60 }
61
62 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)className))) {
63 NSK_COMPLAIN1("#error Agent :: failed to Deallocate className = %s.", className);
64 nsk_jvmti_agentFailed();
65 }
66 }
67 }
68
69
70 JNIEXPORT void JNICALL callbackCompiledMethodLoad(jvmtiEnv *jvmti_env,
71 jmethodID method,
72 jint code_size,
73 const void* code_addr,
74 jint map_length,
75 const jvmtiAddrLocationMap* map,
76 const void* compile_info) {
77 jclass threadClass;
78 if (redefineNumber == 0) {
79 if ( ! NSK_JVMTI_VERIFY ( jvmti_env->GetMethodDeclaringClass(method, &threadClass) ) ) {
80 NSK_COMPLAIN0("#error Agent :: while geting the declaring class.\n");
81 nsk_jvmti_agentFailed();
82 } else {
83 char *className;
84 char *methodName;
85
86 className = NULL;
87 methodName = NULL;
88
89 if ( ! NSK_JVMTI_VERIFY (jvmti_env->GetClassSignature(threadClass, &className, NULL) ) ) {
90 NSK_COMPLAIN0("#error Agent :: while getting classname.\n");
91 nsk_jvmti_agentFailed();
92 return;
93 }
94
95 if ( ! NSK_JVMTI_VERIFY (jvmti_env->GetMethodName(method, &methodName, NULL, NULL) ) ) {
96 NSK_COMPLAIN0("#error Agent :: while getting methodname.\n");
97 nsk_jvmti_agentFailed();
98 return;
99 }
100
101 if ((strcmp(className, CLASS_NAME) == 0) && (strcmp(methodName, METHOD_NAME) == 0)) {
102 char fileName[512];
103
104 NSK_DISPLAY2(" Agent :: Got CompiledMethodLoadEvent for class: %s, method: %s.\n", className, methodName);
105 NSK_DISPLAY0(" Agent :: redefining class.\n");
106
107 nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName, sizeof(fileName)/sizeof(char));
108
109 if ( nsk_jvmti_redefineClass(jvmti_env, threadClass, fileName) == NSK_TRUE) {
110 NSK_DISPLAY0(" Agent :: Successfully redefined.\n");
111 redefineNumber++;
112 } else {
113 NSK_COMPLAIN0("#error Agent :: Failed to redefine.\n");
114 nsk_jvmti_agentFailed();
115 }
116 }
117
118 if ( className != NULL ) {
119 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)className))) {
120 NSK_COMPLAIN1("#error Agent :: failed to Deallocate className = %s.", className);
121 nsk_jvmti_agentFailed();
122 }
123 }
124 if ( methodName != NULL ) {
125 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)methodName))) {
126 NSK_COMPLAIN1("#error Agent :: failed to Deallocate methodName = %s.", methodName);
127 nsk_jvmti_agentFailed();
128 }
129 }
130 }
131 }
132 }
133
134 #ifdef STATIC_BUILD
135 JNIEXPORT jint JNICALL Agent_OnLoad_hs203t004(JavaVM *jvm, char *options, void *reserved) {
136 return Agent_Initialize(jvm, options, reserved);
137 }
138 JNIEXPORT jint JNICALL Agent_OnAttach_hs203t004(JavaVM *jvm, char *options, void *reserved) {
139 return Agent_Initialize(jvm, options, reserved);
140 }
141 JNIEXPORT jint JNI_OnLoad_hs203t004(JavaVM *jvm, char *options, void *reserved) {
142 return JNI_VERSION_1_8;
143 }
144 #endif
145 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
146 redefineNumber=0;
147 if ( ! NSK_VERIFY ( JNI_OK == vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
148 NSK_DISPLAY0("#error Agent :: Could not load JVMTI interface.\n");
149 return JNI_ERR;
150 } else {
151 jvmtiCapabilities caps;
152 jvmtiEventCallbacks eventCallbacks;
153 memset(&caps, 0, sizeof(caps));
154 if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
155 NSK_DISPLAY0("#error Agent :: Failed to parse options.\n");
156 return JNI_ERR;
157 }
158 caps.can_redefine_classes = 1;
159 caps.can_suspend = 1;
160 caps.can_pop_frame = 1;
161 caps.can_generate_all_class_hook_events = 1;
162 caps.can_generate_compiled_method_load_events = 1;
163 if (! NSK_JVMTI_VERIFY ( jvmti->AddCapabilities(&caps) ) ) {
164 NSK_DISPLAY0("#error Agent :: occured while adding capabilities.\n");
165 return JNI_ERR;
166 }
167 memset(&eventCallbacks, 0, sizeof(eventCallbacks));
168 eventCallbacks.ClassPrepare =callbackClassPrepare;
169 eventCallbacks.CompiledMethodLoad=callbackCompiledMethodLoad;
170 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
171 NSK_COMPLAIN0("#error Agent :: occured while setting event callback.\n");
172 return JNI_ERR;
173 }
174 if ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_CLASS_PREPARE, NULL) == NSK_TRUE) {
175 NSK_DISPLAY0(" Agent :: Notifications are enabled.\n");
176 } else {
177 NSK_COMPLAIN0("#error Agent :: Error in enableing Notifications.\n");
178 return JNI_ERR;
179 }
180 }
181 return JNI_OK;
182 }
183
184 JNIEXPORT void JNICALL
185 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t004_hs203t004_suspendThread(JNIEnv * jni,
186 jobject clas,
187 jthread thread) {
188 NSK_DISPLAY0(" Agent :: Suspending Thread.\n");
189 if ( NSK_JVMTI_VERIFY( jvmti->SuspendThread(thread) ) ) {
190 NSK_DISPLAY0(" Agent :: Succeded in suspending.\n");
191 } else {
192 NSK_COMPLAIN0("#error Agent :: occured while suspending thread.\n");
193 nsk_jvmti_agentFailed();
194 }
195 }
196
197 JNIEXPORT jboolean JNICALL
198 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t004_hs203t004_popThreadFrame(JNIEnv * jni,
199 jobject clas,
200 jthread thread) {
201 jboolean retvalue;
202 jint state;
203
204 NSK_DISPLAY0(" Agent :: nsk.jvmti.scenarios.hotswap.HS203.hs203t004.popThreadFrame(... ).\n");
205 retvalue = JNI_FALSE;
206 if ( ! NSK_JVMTI_VERIFY (jvmti->GetThreadState(thread, &state) ) ) {
207 NSK_COMPLAIN0("#error Agent :: while getting thread's state.\n");
208 nsk_jvmti_agentFailed();
209 } else {
210 if ( state & JVMTI_THREAD_STATE_SUSPENDED) {
211 if ( ! NSK_JVMTI_VERIFY( jvmti->PopFrame(thread) ) ) {
212 NSK_DISPLAY0("#error Agent :: occured while poping thread's frame.\n");
213 nsk_jvmti_agentFailed();
214 } else {
215 if ( NSK_JVMTI_VERIFY(
216 jvmti->SetEventNotificationMode(JVMTI_DISABLE,
217 JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL) ) ) {
218 NSK_DISPLAY0(" Agent :: Disabled JVMTI_EVENT_COMPILED_METHOD_LOAD.\n");
219 retvalue = JNI_TRUE;
220 } else {
221 NSK_COMPLAIN0("#error Agent :: Failed to disable JVMTI_EVENT_COMPILED_METHOD_LOAD.\n");
222 nsk_jvmti_agentFailed();
223 }
224 }
225 } else {
226 NSK_COMPLAIN0("#error Agent :: Thread was not suspened.\n");
227 nsk_jvmti_agentFailed();
228 }
229 }
230 return retvalue;
231 }
232
233 JNIEXPORT jboolean JNICALL
234 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t004_hs203t004_resumeThread(JNIEnv * jni,
235 jclass clas,
236 jthread thread) {
237 jboolean retvalue;
238
239 retvalue = JNI_FALSE;
240 if ( NSK_JVMTI_VERIFY (jvmti->ResumeThread(thread))) {
241 NSK_DISPLAY0(" Agent :: Thread resumed.\n");
242 retvalue= JNI_TRUE;
243 } else {
244 NSK_COMPLAIN0("#error Agent :: Failed to resume the thread.\n");
245 nsk_jvmti_agentFailed();
246 }
247 return retvalue;
248 }
249
250 }
|
28 #include <string.h>
29 #include "jvmti_tools.h"
30 #include "JVMTITools.h"
31 #include "jni_tools.h"
32
33 extern "C" {
34 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS203/hs203t004/MyThread"
35 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS203/hs203t004/MyThread;"
36 #define METHOD_NAME "doTask2"
37
38 static jint redefineNumber;
39 static jvmtiEnv * jvmti;
40
41 JNIEXPORT void JNICALL callbackClassPrepare(jvmtiEnv *jvmti_env,
42 JNIEnv* jni,
43 jthread thread,
44 jclass klass) {
45 char * className;
46 className=NULL;
47
48 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &className, NULL))) {
49 NSK_COMPLAIN0("#error Agent :: while getting classname.\n");
50 nsk_jvmti_agentFailed();
51 } else {
52 if (strcmp(className, CLASS_NAME) == 0) {
53 if (nsk_jvmti_enableNotification(jvmti_env, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL) == NSK_TRUE) {
54 NSK_DISPLAY0(" Agent :: notification enabled for COMPILED_METHOD_LOAD.\n");
55 if (!NSK_JVMTI_VERIFY(jvmti_env->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) {
56 NSK_COMPLAIN0("#error Agent :: occured while enabling compiled method events.\n");
57 nsk_jvmti_agentFailed();
58 }
59 }
60 }
61
62 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)className))) {
63 NSK_COMPLAIN1("#error Agent :: failed to Deallocate className = %s.", className);
64 nsk_jvmti_agentFailed();
65 }
66 }
67 }
68
69
70 JNIEXPORT void JNICALL callbackCompiledMethodLoad(jvmtiEnv *jvmti_env,
71 jmethodID method,
72 jint code_size,
73 const void* code_addr,
74 jint map_length,
75 const jvmtiAddrLocationMap* map,
76 const void* compile_info) {
77 jclass threadClass;
78 if (redefineNumber == 0) {
79 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &threadClass))) {
80 NSK_COMPLAIN0("#error Agent :: while geting the declaring class.\n");
81 nsk_jvmti_agentFailed();
82 } else {
83 char *className;
84 char *methodName;
85
86 className = NULL;
87 methodName = NULL;
88
89 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(threadClass, &className, NULL))) {
90 NSK_COMPLAIN0("#error Agent :: while getting classname.\n");
91 nsk_jvmti_agentFailed();
92 return;
93 }
94
95 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &methodName, NULL, NULL))) {
96 NSK_COMPLAIN0("#error Agent :: while getting methodname.\n");
97 nsk_jvmti_agentFailed();
98 return;
99 }
100
101 if ((strcmp(className, CLASS_NAME) == 0) && (strcmp(methodName, METHOD_NAME) == 0)) {
102 char fileName[512];
103
104 NSK_DISPLAY2(" Agent :: Got CompiledMethodLoadEvent for class: %s, method: %s.\n", className, methodName);
105 NSK_DISPLAY0(" Agent :: redefining class.\n");
106
107 nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName, sizeof(fileName)/sizeof(char));
108
109 if (nsk_jvmti_redefineClass(jvmti_env, threadClass, fileName) == NSK_TRUE) {
110 NSK_DISPLAY0(" Agent :: Successfully redefined.\n");
111 redefineNumber++;
112 } else {
113 NSK_COMPLAIN0("#error Agent :: Failed to redefine.\n");
114 nsk_jvmti_agentFailed();
115 }
116 }
117
118 if (className != NULL) {
119 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)className))) {
120 NSK_COMPLAIN1("#error Agent :: failed to Deallocate className = %s.", className);
121 nsk_jvmti_agentFailed();
122 }
123 }
124 if (methodName != NULL) {
125 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)methodName))) {
126 NSK_COMPLAIN1("#error Agent :: failed to Deallocate methodName = %s.", methodName);
127 nsk_jvmti_agentFailed();
128 }
129 }
130 }
131 }
132 }
133
134 #ifdef STATIC_BUILD
135 JNIEXPORT jint JNICALL Agent_OnLoad_hs203t004(JavaVM *jvm, char *options, void *reserved) {
136 return Agent_Initialize(jvm, options, reserved);
137 }
138 JNIEXPORT jint JNICALL Agent_OnAttach_hs203t004(JavaVM *jvm, char *options, void *reserved) {
139 return Agent_Initialize(jvm, options, reserved);
140 }
141 JNIEXPORT jint JNI_OnLoad_hs203t004(JavaVM *jvm, char *options, void *reserved) {
142 return JNI_VERSION_1_8;
143 }
144 #endif
145 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
146 redefineNumber=0;
147 if (!NSK_VERIFY(JNI_OK == vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1))) {
148 NSK_DISPLAY0("#error Agent :: Could not load JVMTI interface.\n");
149 return JNI_ERR;
150 } else {
151 jvmtiCapabilities caps;
152 jvmtiEventCallbacks eventCallbacks;
153 memset(&caps, 0, sizeof(caps));
154 if (nsk_jvmti_parseOptions(options) == NSK_FALSE) {
155 NSK_DISPLAY0("#error Agent :: Failed to parse options.\n");
156 return JNI_ERR;
157 }
158 caps.can_redefine_classes = 1;
159 caps.can_suspend = 1;
160 caps.can_pop_frame = 1;
161 caps.can_generate_all_class_hook_events = 1;
162 caps.can_generate_compiled_method_load_events = 1;
163 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
164 NSK_DISPLAY0("#error Agent :: occured while adding capabilities.\n");
165 return JNI_ERR;
166 }
167 memset(&eventCallbacks, 0, sizeof(eventCallbacks));
168 eventCallbacks.ClassPrepare =callbackClassPrepare;
169 eventCallbacks.CompiledMethodLoad=callbackCompiledMethodLoad;
170 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
171 NSK_COMPLAIN0("#error Agent :: occured while setting event callback.\n");
172 return JNI_ERR;
173 }
174 if (nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_CLASS_PREPARE, NULL) == NSK_TRUE) {
175 NSK_DISPLAY0(" Agent :: Notifications are enabled.\n");
176 } else {
177 NSK_COMPLAIN0("#error Agent :: Error in enableing Notifications.\n");
178 return JNI_ERR;
179 }
180 }
181 return JNI_OK;
182 }
183
184 JNIEXPORT void JNICALL
185 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t004_hs203t004_suspendThread(JNIEnv * jni,
186 jobject clas,
187 jthread thread) {
188 NSK_DISPLAY0(" Agent :: Suspending Thread.\n");
189 if (NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread))) {
190 NSK_DISPLAY0(" Agent :: Succeded in suspending.\n");
191 } else {
192 NSK_COMPLAIN0("#error Agent :: occured while suspending thread.\n");
193 nsk_jvmti_agentFailed();
194 }
195 }
196
197 JNIEXPORT jboolean JNICALL
198 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t004_hs203t004_popThreadFrame(JNIEnv * jni,
199 jobject clas,
200 jthread thread) {
201 jboolean retvalue;
202 jint state;
203
204 NSK_DISPLAY0(" Agent :: nsk.jvmti.scenarios.hotswap.HS203.hs203t004.popThreadFrame(...).\n");
205 retvalue = JNI_FALSE;
206 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(thread, &state))) {
207 NSK_COMPLAIN0("#error Agent :: while getting thread's state.\n");
208 nsk_jvmti_agentFailed();
209 } else {
210 if (state & JVMTI_THREAD_STATE_SUSPENDED) {
211 if (!NSK_JVMTI_VERIFY(jvmti->PopFrame(thread))) {
212 NSK_DISPLAY0("#error Agent :: occured while poping thread's frame.\n");
213 nsk_jvmti_agentFailed();
214 } else {
215 if (NSK_JVMTI_VERIFY(
216 jvmti->SetEventNotificationMode(JVMTI_DISABLE,
217 JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL))) {
218 NSK_DISPLAY0(" Agent :: Disabled JVMTI_EVENT_COMPILED_METHOD_LOAD.\n");
219 retvalue = JNI_TRUE;
220 } else {
221 NSK_COMPLAIN0("#error Agent :: Failed to disable JVMTI_EVENT_COMPILED_METHOD_LOAD.\n");
222 nsk_jvmti_agentFailed();
223 }
224 }
225 } else {
226 NSK_COMPLAIN0("#error Agent :: Thread was not suspened.\n");
227 nsk_jvmti_agentFailed();
228 }
229 }
230 return retvalue;
231 }
232
233 JNIEXPORT jboolean JNICALL
234 Java_nsk_jvmti_scenarios_hotswap_HS203_hs203t004_hs203t004_resumeThread(JNIEnv * jni,
235 jclass clas,
236 jthread thread) {
237 jboolean retvalue;
238
239 retvalue = JNI_FALSE;
240 if (NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread))) {
241 NSK_DISPLAY0(" Agent :: Thread resumed.\n");
242 retvalue= JNI_TRUE;
243 } else {
244 NSK_COMPLAIN0("#error Agent :: Failed to resume the thread.\n");
245 nsk_jvmti_agentFailed();
246 }
247 return retvalue;
248 }
249
250 }
|