58 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
59
60 NSK_DISPLAY0("Wait for thread to start\n");
61 if (!nsk_jvmti_waitForSync(timeout))
62 return;
63
64 /* perform testing */
65 {
66 NSK_DISPLAY1("Find thread: %s\n", THREAD_NAME);
67 if (!NSK_VERIFY((testedThread =
68 nsk_jvmti_threadByName(THREAD_NAME)) != NULL))
69 return;
70 NSK_DISPLAY1(" ... found thread: %p\n", (void*)testedThread);
71
72 eventsReceived = 0;
73 NSK_DISPLAY1("Enable event: %s\n", "THREAD_END");
74 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL))
75 return;
76
77 NSK_DISPLAY1("Suspend thread: %p\n", (void*)testedThread);
78 if (!NSK_JVMTI_VERIFY(
79 NSK_CPP_STUB2(SuspendThread, jvmti, testedThread))) {
80 nsk_jvmti_setFailStatus();
81 return;
82 }
83
84 NSK_DISPLAY0("Let thread to run and finish\n");
85 if (!nsk_jvmti_resumeSync())
86 return;
87
88 NSK_DISPLAY1("Check that THREAD_END event NOT received for timeout: %ld ms\n", (long)verificationTime);
89 {
90 jlong delta = 1000;
91 jlong time;
92 for (time = 0; time < verificationTime; time += delta) {
93 if (eventsReceived > 0) {
94 NSK_COMPLAIN0("Thread ran and finished after suspension\n");
95 nsk_jvmti_setFailStatus();
96 break;
97 }
98 nsk_jvmti_sleep(delta);
99 }
100 }
101
102 NSK_DISPLAY1("Disable event: %s\n", "THREAD_END");
103 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL))
104 return;
105
106 NSK_DISPLAY1("Resume thread: %p\n", (void*)testedThread);
107 if (!NSK_JVMTI_VERIFY(
108 NSK_CPP_STUB2(ResumeThread, jvmti, testedThread))) {
109 nsk_jvmti_setFailStatus();
110 }
111
112 NSK_DISPLAY0("Wait for thread to finish\n");
113 if (!nsk_jvmti_waitForSync(timeout))
114 return;
115
116 NSK_DISPLAY0("Delete thread reference\n");
117 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedThread));
118 }
119
120 NSK_DISPLAY0("Let debugee to finish\n");
121 if (!nsk_jvmti_resumeSync())
122 return;
123 }
124
125 /* ============================================================================= */
126
127 /** THREAD_END callback. */
128 JNIEXPORT void JNICALL
129 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
130 /* check if event is for tested thread */
131 if (thread != NULL &&
132 NSK_CPP_STUB3(IsSameObject, jni, testedThread, thread)) {
133 NSK_DISPLAY1(" ... received THREAD_END event for tested thread: %p\n", (void*)thread);
134 eventsReceived++;
135 } else {
136 NSK_DISPLAY1(" ... received THREAD_END event for unknown thread: %p\n", (void*)thread);
137 }
138 }
139
140 /* ============================================================================= */
141
142 /** Agent library initialization. */
143 #ifdef STATIC_BUILD
144 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
145 return Agent_Initialize(jvm, options, reserved);
146 }
147 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
148 return Agent_Initialize(jvm, options, reserved);
149 }
150 JNIEXPORT jint JNI_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
151 return JNI_VERSION_1_8;
152 }
153 #endif
154 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
155 jvmtiEnv* jvmti = NULL;
156
157 /* init framework and parse options */
158 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
159 return JNI_ERR;
160
161 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
162
163 /* create JVMTI environment */
164 if (!NSK_VERIFY((jvmti =
165 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
166 return JNI_ERR;
167
168 /* add specific capabilities for suspending thread */
169 {
170 jvmtiCapabilities suspendCaps;
171 memset(&suspendCaps, 0, sizeof(suspendCaps));
172 suspendCaps.can_suspend = 1;
173 if (!NSK_JVMTI_VERIFY(
174 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
175 return JNI_ERR;
176 }
177
178 /* set callbacks for THREAD_END event */
179 {
180 jvmtiEventCallbacks callbacks;
181 memset(&callbacks, 0, sizeof(callbacks));
182 callbacks.ThreadEnd = callbackThreadEnd;
183 if (!NSK_JVMTI_VERIFY(
184 NSK_CPP_STUB3(SetEventCallbacks, jvmti, &callbacks, sizeof(callbacks))))
185 return JNI_ERR;
186 }
187
188 /* register agent proc and arg */
189 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
190 return JNI_ERR;
191
192 return JNI_OK;
193 }
194
195 /* ============================================================================= */
196
197 }
|
58 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
59
60 NSK_DISPLAY0("Wait for thread to start\n");
61 if (!nsk_jvmti_waitForSync(timeout))
62 return;
63
64 /* perform testing */
65 {
66 NSK_DISPLAY1("Find thread: %s\n", THREAD_NAME);
67 if (!NSK_VERIFY((testedThread =
68 nsk_jvmti_threadByName(THREAD_NAME)) != NULL))
69 return;
70 NSK_DISPLAY1(" ... found thread: %p\n", (void*)testedThread);
71
72 eventsReceived = 0;
73 NSK_DISPLAY1("Enable event: %s\n", "THREAD_END");
74 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL))
75 return;
76
77 NSK_DISPLAY1("Suspend thread: %p\n", (void*)testedThread);
78 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(testedThread))) {
79 nsk_jvmti_setFailStatus();
80 return;
81 }
82
83 NSK_DISPLAY0("Let thread to run and finish\n");
84 if (!nsk_jvmti_resumeSync())
85 return;
86
87 NSK_DISPLAY1("Check that THREAD_END event NOT received for timeout: %ld ms\n", (long)verificationTime);
88 {
89 jlong delta = 1000;
90 jlong time;
91 for (time = 0; time < verificationTime; time += delta) {
92 if (eventsReceived > 0) {
93 NSK_COMPLAIN0("Thread ran and finished after suspension\n");
94 nsk_jvmti_setFailStatus();
95 break;
96 }
97 nsk_jvmti_sleep(delta);
98 }
99 }
100
101 NSK_DISPLAY1("Disable event: %s\n", "THREAD_END");
102 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL))
103 return;
104
105 NSK_DISPLAY1("Resume thread: %p\n", (void*)testedThread);
106 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(testedThread))) {
107 nsk_jvmti_setFailStatus();
108 }
109
110 NSK_DISPLAY0("Wait for thread to finish\n");
111 if (!nsk_jvmti_waitForSync(timeout))
112 return;
113
114 NSK_DISPLAY0("Delete thread reference\n");
115 NSK_TRACE(jni->DeleteGlobalRef(testedThread));
116 }
117
118 NSK_DISPLAY0("Let debugee to finish\n");
119 if (!nsk_jvmti_resumeSync())
120 return;
121 }
122
123 /* ============================================================================= */
124
125 /** THREAD_END callback. */
126 JNIEXPORT void JNICALL
127 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
128 /* check if event is for tested thread */
129 if (thread != NULL &&
130 jni->IsSameObject(testedThread, thread)) {
131 NSK_DISPLAY1(" ... received THREAD_END event for tested thread: %p\n", (void*)thread);
132 eventsReceived++;
133 } else {
134 NSK_DISPLAY1(" ... received THREAD_END event for unknown thread: %p\n", (void*)thread);
135 }
136 }
137
138 /* ============================================================================= */
139
140 /** Agent library initialization. */
141 #ifdef STATIC_BUILD
142 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
143 return Agent_Initialize(jvm, options, reserved);
144 }
145 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
146 return Agent_Initialize(jvm, options, reserved);
147 }
148 JNIEXPORT jint JNI_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
149 return JNI_VERSION_1_8;
150 }
151 #endif
152 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
153 jvmtiEnv* jvmti = NULL;
154
155 /* init framework and parse options */
156 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
157 return JNI_ERR;
158
159 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
160
161 /* create JVMTI environment */
162 if (!NSK_VERIFY((jvmti =
163 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
164 return JNI_ERR;
165
166 /* add specific capabilities for suspending thread */
167 {
168 jvmtiCapabilities suspendCaps;
169 memset(&suspendCaps, 0, sizeof(suspendCaps));
170 suspendCaps.can_suspend = 1;
171 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))
172 return JNI_ERR;
173 }
174
175 /* set callbacks for THREAD_END event */
176 {
177 jvmtiEventCallbacks callbacks;
178 memset(&callbacks, 0, sizeof(callbacks));
179 callbacks.ThreadEnd = callbackThreadEnd;
180 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
181 return JNI_ERR;
182 }
183
184 /* register agent proc and arg */
185 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
186 return JNI_ERR;
187
188 return JNI_OK;
189 }
190
191 /* ============================================================================= */
192
193 }
|