1 /*
2 * Copyright (c) 2004, 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 "jni_tools.h"
27 #include "agent_common.h"
28 #include "jvmti_tools.h"
29
30 extern "C" {
31
32 /* The test adds all capabilities suitable for debugging at OnLoad phase:
33 *
34 * can_get_bytecodes
35 * can_get_synthetic_attribute
36 * can_pop_frame
37 * can_redefine_classes
38 * can_signal_thread
39 * can_get_source_file_name
40 * can_get_line_numbers
41 * can_get_source_debug_extension
42 * can_access_local_variables
43 * can_suspend
44 * can_generate_field_modification_events
45 * can_generate_field_access_events
46 * can_generate_single_step_events
47 * can_generate_exception_events
48 * can_generate_frame_pop_events
49 * can_generate_breakpoint_events
50 * can_generate_method_entry_events
51 * can_generate_method_exit_events
52 *
53 * and sets calbacks and enables events correspondent to capabilities above.
54 * Then checks that GetCapabilities returns correct list of possessed
55 * capabilities in Live phase, and checks that correspondent possessed
56 * functions works and requested events are generated.
57 */
58
59 /* ========================================================================== */
60
61 /* scaffold objects */
62 static jlong timeout = 0;
63
64 /* test objects */
65 static jthread thread = NULL;
66 static jclass klass = NULL;
67 static jmethodID method = NULL;
68 static jfieldID field = NULL;
69 static jint klass_byte_count = 0;
70 static unsigned char *klass_bytes = NULL;
71
72 /* event counts */
73 static int FieldAccessEventsCount = 0;
74 static int FieldModificationEventsCount = 0;
75 static int SingleStepEventsCount = 0;
76 static int ExceptionEventsCount = 0;
77 static int ExceptionCatchEventsCount = 0;
78 static int BreakpointEventsCount = 0;
79 static int FramePopEventsCount = 0;
80 static int MethodEntryEventsCount = 0;
81 static int MethodExitEventsCount = 0;
82
83 /* ========================================================================== */
84
85 /** callback functions **/
86
87 /* to get class file data for RedefineClasses */
88 const char* CLASS_NAME = "nsk/jvmti/scenarios/capability/CM03/cm03t001Thread";
89 static void JNICALL
ClassFileLoadHook(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jclass class_beeing_redefined,jobject loader,const char * name,jobject protection_domain,jint class_data_len,const unsigned char * class_data,jint * new_class_data_len,unsigned char ** new_class_data)90 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
91 jclass class_beeing_redefined, jobject loader,
92 const char* name, jobject protection_domain,
93 jint class_data_len, const unsigned char* class_data,
94 jint *new_class_data_len, unsigned char** new_class_data) {
95
96 if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) {
97 NSK_DISPLAY1("ClassFileLoadHook: %s\n", name);
98 if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(class_data_len, &klass_bytes)))
99 nsk_jvmti_setFailStatus();
100 else {
101 memcpy(klass_bytes, class_data, class_data_len);
102 klass_byte_count = class_data_len;
103 }
104 NSK_JVMTI_VERIFY(
105 jvmti_env->SetEventNotificationMode(
106 JVMTI_DISABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL));
107 }
108 }
109
110 static void JNICALL
FieldAccess(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location,jclass field_klass,jobject object,jfieldID field)111 FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
112 jthread thread, jmethodID method, jlocation location,
113 jclass field_klass, jobject object, jfieldID field) {
114 char *name = NULL;
115 char *signature = NULL;
116
117 FieldAccessEventsCount++;
118
119 if (!NSK_JVMTI_VERIFY(jvmti_env->ClearFieldAccessWatch(klass, field)))
120 return;
121
122 if (!NSK_JVMTI_VERIFY(jvmti_env->GetFieldName(field_klass, field, &name, &signature, NULL))) {
123 nsk_jvmti_setFailStatus();
124 return;
125 }
126 NSK_DISPLAY2("FieldAccess event: %s:%s\n", name, signature);
127 if (name != NULL)
128 jvmti_env->Deallocate((unsigned char*)name);
129 if (signature != NULL)
130 jvmti_env->Deallocate((unsigned char*)signature);
131 }
132
133 static void JNICALL
FieldModification(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location,jclass field_klass,jobject object,jfieldID field,char sig,jvalue new_value)134 FieldModification(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
135 jthread thread, jmethodID method, jlocation location,
136 jclass field_klass, jobject object,
137 jfieldID field, char sig, jvalue new_value) {
138 char *name = NULL;
139 char *signature = NULL;
140
141 FieldModificationEventsCount++;
142
143 if (!NSK_JVMTI_VERIFY(jvmti_env->GetFieldName(field_klass, field, &name, &signature, NULL))) {
144 nsk_jvmti_setFailStatus();
145 return;
146 }
147 NSK_DISPLAY2("FieldModification event: %s:%s\n", name, signature);
148 if (name != NULL)
149 jvmti_env->Deallocate((unsigned char*)name);
150 if (signature != NULL)
151 jvmti_env->Deallocate((unsigned char*)signature);
152 }
153
154 static void JNICALL
SingleStep(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location)155 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
156 jmethodID method, jlocation location) {
157 char *name = NULL;
158 char *signature = NULL;
159
160 SingleStepEventsCount++;
161
162 NSK_JVMTI_VERIFY(
163 jvmti_env->SetEventNotificationMode(
164 JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, NULL));
165
166 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
167 nsk_jvmti_setFailStatus();
168 return;
169 }
170 NSK_DISPLAY2("SingleStep event: %s%s\n", name, signature);
171 if (name != NULL)
172 jvmti_env->Deallocate((unsigned char*)name);
173 if (signature != NULL)
174 jvmti_env->Deallocate((unsigned char*)signature);
175 }
176
177 static void JNICALL
Exception(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location,jobject exception,jmethodID catch_method,jlocation catch_location)178 Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
179 jmethodID method, jlocation location, jobject exception,
180 jmethodID catch_method, jlocation catch_location) {
181 jclass klass = NULL;
182 char *signature = NULL;
183
184 ExceptionEventsCount++;
185
186 if (!NSK_JNI_VERIFY(jni_env, (klass = jni_env->GetObjectClass(exception)) != NULL)) {
187 nsk_jvmti_setFailStatus();
188 return;
189 }
190 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &signature, NULL))) {
191 nsk_jvmti_setFailStatus();
192 return;
193 }
194 NSK_DISPLAY1("Exception event: %s\n", signature);
195 if (signature != NULL)
196 jvmti_env->Deallocate((unsigned char*)signature);
197 }
198
199 void JNICALL
ExceptionCatch(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location,jobject exception)200 ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
201 jmethodID method, jlocation location, jobject exception) {
202 jclass klass = NULL;
203 char *signature = NULL;
204
205 ExceptionCatchEventsCount++;
206
207 if (!NSK_JNI_VERIFY(jni_env, (klass = jni_env->GetObjectClass(exception)) != NULL)) {
208 nsk_jvmti_setFailStatus();
209 return;
210 }
211 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &signature, NULL))) {
212 nsk_jvmti_setFailStatus();
213 return;
214 }
215 NSK_DISPLAY1("ExceptionCatch event: %s\n", signature);
216 if (signature != NULL)
217 jvmti_env->Deallocate((unsigned char*)signature);
218 }
219
220 static void JNICALL
Breakpoint(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location)221 Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
222 jthread thread, jmethodID method, jlocation location) {
223 char *name = NULL;
224 char *signature = NULL;
225
226 BreakpointEventsCount++;
227 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
228 nsk_jvmti_setFailStatus();
229 return;
230 }
231 NSK_DISPLAY2("Breakpoint event: %s%s\n", name, signature);
232 if (name != NULL)
233 jvmti_env->Deallocate((unsigned char*)name);
234 if (signature != NULL)
235 jvmti_env->Deallocate((unsigned char*)signature);
236
237 jvmti_env->NotifyFramePop(thread, 0);
238 }
239
240 static void JNICALL
FramePop(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jboolean wasPopedByException)241 FramePop(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
242 jthread thread, jmethodID method,
243 jboolean wasPopedByException) {
244 char *name = NULL;
245 char *signature = NULL;
246
247 FramePopEventsCount++;
248 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
249 nsk_jvmti_setFailStatus();
250 return;
251 }
252 NSK_DISPLAY2("FramePop event: %s%s\n", name, signature);
253 if (name != NULL)
254 jvmti_env->Deallocate((unsigned char*)name);
255 if (signature != NULL)
256 jvmti_env->Deallocate((unsigned char*)signature);
257 }
258
259 static void JNICALL
MethodEntry(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method)260 MethodEntry(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
261 jthread thread, jmethodID method) {
262 char *name = NULL;
263 char *signature = NULL;
264
265 MethodEntryEventsCount++;
266 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
267 nsk_jvmti_setFailStatus();
268 return;
269 }
270 NSK_DISPLAY2("MethodEntry event: %s%s\n", name, signature);
271 if (name != NULL)
272 jvmti_env->Deallocate((unsigned char*)name);
273 if (signature != NULL)
274 jvmti_env->Deallocate((unsigned char*)signature);
275 }
276
277 static void JNICALL
MethodExit(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jboolean was_poped_by_exception,jvalue return_value)278 MethodExit(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
279 jthread thread, jmethodID method,
280 jboolean was_poped_by_exception, jvalue return_value) {
281 char *name = NULL;
282 char *signature = NULL;
283
284 MethodExitEventsCount++;
285 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
286 nsk_jvmti_setFailStatus();
287 return;
288 }
289 NSK_DISPLAY2("MethodExit event: %s%s\n", name, signature);
290 if (name != NULL)
291 jvmti_env->Deallocate((unsigned char*)name);
292 if (signature != NULL)
293 jvmti_env->Deallocate((unsigned char*)signature);
294 }
295
296 /* ========================================================================== */
297
prepare(jvmtiEnv * jvmti,JNIEnv * jni)298 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
299 const char* THREAD_NAME = "Debuggee Thread";
300 jvmtiThreadInfo info;
301 jthread *threads = NULL;
302 jint threads_count = 0;
303 int i;
304
305 NSK_DISPLAY0("Prepare: find tested thread\n");
306
307 /* get all live threads */
308 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))
309 return NSK_FALSE;
310
311 if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
312 return NSK_FALSE;
313
314 /* find tested thread */
315 for (i = 0; i < threads_count; i++) {
316 if (!NSK_VERIFY(threads[i] != NULL))
317 return NSK_FALSE;
318
319 /* get thread information */
320 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
321 return NSK_FALSE;
322
323 NSK_DISPLAY3(" thread #%d (%s): %p\n", i, info.name, threads[i]);
324
325 /* find by name */
326 if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
327 thread = threads[i];
328 }
329 }
330
331 if (!NSK_JNI_VERIFY(jni, (thread = jni->NewGlobalRef(thread)) != NULL))
332 return NSK_FALSE;
333
334 /* deallocate threads list */
335 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
336 return NSK_FALSE;
337
338 /* get tested thread class */
339 if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
340 return NSK_FALSE;
341
342 /* klass is used by other threads - convert to global handle */
343 if (!NSK_JNI_VERIFY(jni, (klass = (jclass)jni->NewGlobalRef(klass)) != NULL))
344 return NSK_FALSE;
345
346 /* get tested thread method 'delay' */
347 if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "delay", "()V")) != NULL))
348 return NSK_FALSE;
349
350 /* get tested thread field 'waitingFlag' */
351 if (!NSK_JNI_VERIFY(jni, (field = jni->GetFieldID(klass, "waitingFlag", "Z")) != NULL))
352 return NSK_FALSE;
353
354 return NSK_TRUE;
355 }
356
prepareEvents(jvmtiEnv * jvmti,JNIEnv * jni)357 static int prepareEvents(jvmtiEnv* jvmti, JNIEnv* jni) {
358 NSK_DISPLAY0("Prepare events ...\n");
359
360 /* get tested thread method 'letItGo' */
361 if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "letItGo", "()V")) != NULL))
362 return NSK_FALSE;
363
364 /* get tested thread field 'waitingFlag' */
365 if (!NSK_JNI_VERIFY(jni, (field = jni->GetFieldID(klass, "waitingFlag", "Z")) != NULL))
366 return NSK_FALSE;
367
368 if (!NSK_JVMTI_VERIFY(jvmti->SetFieldAccessWatch(klass, field)))
369 return NSK_FALSE;
370
371 if (!NSK_JVMTI_VERIFY(jvmti->SetFieldModificationWatch(klass, field)))
372 return NSK_FALSE;
373
374 if (!NSK_JVMTI_VERIFY(jvmti->SetBreakpoint(method, 0)))
375 return NSK_FALSE;
376
377 /* enable events */
378 if (!NSK_JVMTI_VERIFY(
379 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
380 return JNI_ERR;
381 if (!NSK_JVMTI_VERIFY(
382 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
383 return JNI_ERR;
384 if (!NSK_JVMTI_VERIFY(
385 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION_CATCH, NULL)))
386 return JNI_ERR;
387 if (!NSK_JVMTI_VERIFY(
388 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, thread)))
389 return NSK_FALSE;
390 if (!NSK_JVMTI_VERIFY(
391 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread)))
392 return NSK_FALSE;
393
394 return NSK_TRUE;
395 }
396
397 /* ========================================================================== */
398
399 /* Check GetCapabilities function
400 */
checkGetCapabilities(jvmtiEnv * jvmti)401 static int checkGetCapabilities(jvmtiEnv* jvmti) {
402 jvmtiCapabilities caps;
403
404 memset(&caps, 0, sizeof(caps));
405 if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
406 return NSK_FALSE;
407 if (!NSK_VERIFY(caps.can_get_bytecodes))
408 return NSK_FALSE;
409 if (!NSK_VERIFY(caps.can_get_synthetic_attribute))
410 return NSK_FALSE;
411 if (!NSK_VERIFY(caps.can_pop_frame))
412 return NSK_FALSE;
413 if (!NSK_VERIFY(caps.can_redefine_classes))
414 return NSK_FALSE;
415 if (!NSK_VERIFY(caps.can_signal_thread))
416 return NSK_FALSE;
417 if (!NSK_VERIFY(caps.can_get_source_file_name))
418 return NSK_FALSE;
419 if (!NSK_VERIFY(caps.can_get_line_numbers))
420 return NSK_FALSE;
421 if (!NSK_VERIFY(caps.can_get_source_debug_extension))
422 return NSK_FALSE;
423 if (!NSK_VERIFY(caps.can_access_local_variables))
424 return NSK_FALSE;
425 if (!NSK_VERIFY(caps.can_suspend))
426 return NSK_FALSE;
427 if (!NSK_VERIFY(caps.can_generate_field_modification_events))
428 return NSK_FALSE;
429 if (!NSK_VERIFY(caps.can_generate_field_access_events))
430 return NSK_FALSE;
431 if (!NSK_VERIFY(caps.can_generate_single_step_events))
432 return NSK_FALSE;
433 if (!NSK_VERIFY(caps.can_generate_exception_events))
434 return NSK_FALSE;
435 if (!NSK_VERIFY(caps.can_generate_frame_pop_events))
436 return NSK_FALSE;
437 if (!NSK_VERIFY(caps.can_generate_breakpoint_events))
438 return NSK_FALSE;
439 if (!NSK_VERIFY(caps.can_generate_method_entry_events))
440 return NSK_FALSE;
441 if (!NSK_VERIFY(caps.can_generate_method_exit_events))
442 return NSK_FALSE;
443
444 return NSK_TRUE;
445 }
446
447 /* ========================================================================== */
448
449 /* Check "can_get_bytecodes" function
450 */
checkGetBytecodes(jvmtiEnv * jvmti)451 static int checkGetBytecodes(jvmtiEnv* jvmti) {
452 jint count;
453 unsigned char *bytecodes;
454
455 NSK_DISPLAY0("Checking positive: GetBytecodes\n");
456 if (!NSK_JVMTI_VERIFY(jvmti->GetBytecodes(method, &count, &bytecodes)))
457 return NSK_FALSE;
458 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate(bytecodes)))
459 return NSK_FALSE;
460
461 return NSK_TRUE;
462 }
463
464 /* Check "can_get_synthetic_attribute" functions
465 */
checkIsSyntheticFunctions(jvmtiEnv * jvmti)466 static int checkIsSyntheticFunctions(jvmtiEnv* jvmti) {
467 jboolean is_synthetic;
468
469 NSK_DISPLAY0("Checking positive: IsFieldSynthetic\n");
470 if (!NSK_JVMTI_VERIFY(jvmti->IsFieldSynthetic(klass, field, &is_synthetic)))
471 return NSK_FALSE;
472
473 NSK_DISPLAY0("Checking positive: IsMethodSynthetic\n");
474 if (!NSK_JVMTI_VERIFY(jvmti->IsMethodSynthetic(method, &is_synthetic)))
475 return NSK_FALSE;
476
477 return NSK_TRUE;
478 }
479
480 /* Check "can_redefine_classes" functions
481 */
checkRedefineClasses(jvmtiEnv * jvmti)482 static int checkRedefineClasses(jvmtiEnv* jvmti) {
483 jvmtiClassDefinition class_def;
484 jboolean is_obsolete;
485
486 if (!NSK_VERIFY(klass_byte_count != 0 && klass_bytes != NULL))
487 return NSK_FALSE;
488
489 NSK_DISPLAY0("Checking positive: RedefineClasses\n");
490 class_def.klass = klass;
491 class_def.class_byte_count = klass_byte_count;
492 class_def.class_bytes = klass_bytes;
493 if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &class_def)))
494 return NSK_FALSE;
495
496 NSK_DISPLAY0("Checking positive: IsMethodObsolete\n");
497 if (!NSK_JVMTI_VERIFY(jvmti->IsMethodObsolete(method, &is_obsolete)))
498 return NSK_FALSE;
499
500 return NSK_TRUE;
501 }
502
503 /* Check "can_get_source_file_name" function
504 */
checkGetSourceFileName(jvmtiEnv * jvmti)505 static int checkGetSourceFileName(jvmtiEnv* jvmti) {
506 char *name;
507
508 NSK_DISPLAY0("Checking positive: GetSourceFileName\n");
509 if (!NSK_JVMTI_VERIFY(jvmti->GetSourceFileName(klass, &name)))
510 return NSK_FALSE;
511
512 return NSK_TRUE;
513 }
514
515 /* Check "can_get_line_numbers" function
516 */
checkGetLineNumberTable(jvmtiEnv * jvmti)517 static int checkGetLineNumberTable(jvmtiEnv* jvmti) {
518 jint count;
519 jvmtiLineNumberEntry *line_number_table = NULL;
520
521 NSK_DISPLAY0("Checking positive: GetLineNumberTable\n");
522 if (!NSK_JVMTI_VERIFY(jvmti->GetLineNumberTable(method, &count, &line_number_table)))
523 return NSK_FALSE;
524
525 return NSK_TRUE;
526 }
527
528 /* Check "can_get_source_debug_extension" function
529 */
checkGetSourceDebugExtension(jvmtiEnv * jvmti)530 static int checkGetSourceDebugExtension(jvmtiEnv* jvmti) {
531 char *name;
532
533 NSK_DISPLAY0("Checking positive: GetSourceDebugExtension\n");
534 if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_ABSENT_INFORMATION,
535 jvmti->GetSourceDebugExtension(klass, &name)))
536 return NSK_FALSE;
537
538 return NSK_TRUE;
539 }
540
541 /* Check "can_access_local_variables" functions
542 */
checkLocalVariableFunctions(jvmtiEnv * jvmti)543 static int checkLocalVariableFunctions(jvmtiEnv* jvmti) {
544 jint count;
545 jvmtiLocalVariableEntry *local_variable_table = NULL;
546 jobject object_value;
547 jint int_value;
548 jlong long_value;
549 jfloat float_value;
550 jdouble double_value;
551 int i;
552
553 NSK_DISPLAY0("Checking positive: GetLocalVariableTable\n");
554 if (!NSK_JVMTI_VERIFY(jvmti->GetLocalVariableTable(method, &count, &local_variable_table)))
555 return NSK_FALSE;
556
557 /* DEBUG -- while 4913796 bug not fixed thread should be suspended
558 */
559 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
560 return NSK_FALSE;
561
562 for (i = 0; i < count; i++) {
563 if (strcmp(local_variable_table[i].name, "o") == 0) {
564 NSK_DISPLAY0("Checking positive: GetLocalObject\n");
565 if (!NSK_JVMTI_VERIFY(
566 jvmti->GetLocalObject(thread, 1, local_variable_table[i].slot, &object_value)))
567 return NSK_FALSE;
568
569 NSK_DISPLAY0("Checking positive: SetLocalObject\n");
570 if (!NSK_JVMTI_VERIFY(
571 jvmti->SetLocalObject(thread, 1, local_variable_table[i].slot, object_value)))
572 return NSK_FALSE;
573 } else if (strcmp(local_variable_table[i].name, "i") == 0) {
574 NSK_DISPLAY0("Checking positive: GetLocalInt\n");
575 if (!NSK_JVMTI_VERIFY(
576 jvmti->GetLocalInt(thread, 1, local_variable_table[i].slot, &int_value)))
577 return NSK_FALSE;
578
579 NSK_DISPLAY0("Checking positive: SetLocalInt\n");
580 if (!NSK_JVMTI_VERIFY(
581 jvmti->SetLocalInt(thread, 1, local_variable_table[i].slot, int_value)))
582 return NSK_FALSE;
583 } else if (strcmp(local_variable_table[i].name, "l") == 0) {
584 NSK_DISPLAY0("Checking positive: GetLocalLong\n");
585 if (!NSK_JVMTI_VERIFY(
586 jvmti->GetLocalLong(thread, 1, local_variable_table[i].slot, &long_value)))
587 return NSK_FALSE;
588
589 NSK_DISPLAY0("Checking positive: SetLocalLong\n");
590 if (!NSK_JVMTI_VERIFY(
591 jvmti->SetLocalLong(thread, 1, local_variable_table[i].slot, long_value)))
592 return NSK_FALSE;
593 } else if (strcmp(local_variable_table[i].name, "f") == 0) {
594 NSK_DISPLAY0("Checking positive: GetLocalFloat\n");
595 if (!NSK_JVMTI_VERIFY(
596 jvmti->GetLocalFloat(thread, 1, local_variable_table[i].slot, &float_value)))
597 return NSK_FALSE;
598
599 NSK_DISPLAY0("Checking positive: SetLocalFloat\n");
600 if (!NSK_JVMTI_VERIFY(
601 jvmti->SetLocalFloat(thread, 1, local_variable_table[i].slot, float_value)))
602 return NSK_FALSE;
603 } else if (strcmp(local_variable_table[i].name, "d") == 0) {
604 NSK_DISPLAY0("Checking positive: GetLocalDouble\n");
605 if (!NSK_JVMTI_VERIFY(
606 jvmti->GetLocalDouble(thread, 1, local_variable_table[i].slot, &double_value)))
607 return NSK_FALSE;
608
609 NSK_DISPLAY0("Checking positive: SetLocalDouble\n");
610 if (!NSK_JVMTI_VERIFY(
611 jvmti->SetLocalDouble(thread, 1, local_variable_table[i].slot, double_value)))
612 return NSK_FALSE;
613 }
614 }
615
616 /* DEBUG -- while 4913796 bug not fixed thread should be suspended
617 */
618 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
619 return NSK_FALSE;
620
621 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)local_variable_table)))
622 return NSK_FALSE;
623
624 return NSK_TRUE;
625 }
626
627 /* Check "can_suspend" functions
628 */
checkSuspend(jvmtiEnv * jvmti)629 static int checkSuspend(jvmtiEnv* jvmti) {
630 jvmtiError err;
631
632 NSK_DISPLAY0("Checking positive: SuspendThread\n");
633 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
634 return NSK_FALSE;
635
636 NSK_DISPLAY0("Checking positive: ResumeThread\n");
637 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
638 return NSK_FALSE;
639
640 NSK_DISPLAY0("Checking positive: SuspendThreadList\n");
641 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(1, &thread, &err)))
642 return NSK_FALSE;
643
644 NSK_DISPLAY0("Checking positive: ResumeThreadList\n");
645 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(1, &thread, &err)))
646 return NSK_FALSE;
647
648 return NSK_TRUE;
649 }
650
651 /* Check "can_pop_frame" function
652 */
checkPopFrame(jvmtiEnv * jvmti)653 static int checkPopFrame(jvmtiEnv* jvmti) {
654 int result = NSK_TRUE;
655 jvmtiError err;
656
657 NSK_DISPLAY0("Checking positive: PopFrame\n");
658 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
659 return NSK_FALSE;
660
661 // PopFrame is allowed to fail with JVMTI_ERROR_OPAQUE_FRAME.
662 // That will happen if we are in a native function,
663 // for example while waiting for a Condition.
664 // See JCK-5020108.
665 err = jvmti->PopFrame(thread);
666 if (err != JVMTI_ERROR_NONE && err != JVMTI_ERROR_OPAQUE_FRAME) {
667 result = NSK_FALSE;
668 NSK_DISPLAY1("jvmti error from PopFrame: %d\n", err);
669 }
670
671 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
672 result = NSK_FALSE;
673
674 return result;
675 }
676
677 /* Check "can_signal_thread" functions
678 */
checkSignalThread(jvmtiEnv * jvmti,JNIEnv * jni)679 static int checkSignalThread(jvmtiEnv* jvmti, JNIEnv* jni) {
680 const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath";
681 const char* THREAD_DEATH_CTOR_NAME = "<init>";
682 const char* THREAD_DEATH_CTOR_SIGNATURE = "()V";
683 jclass cls = NULL;
684 jmethodID ctor = NULL;
685 jobject exception = NULL;
686
687 if (!NSK_JNI_VERIFY(jni, (cls = jni->FindClass(THREAD_DEATH_CLASS_NAME)) != NULL))
688 return NSK_FALSE;
689
690 if (!NSK_JNI_VERIFY(jni, (ctor =
691 jni->GetMethodID(cls, THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL))
692 return NSK_FALSE;
693
694 if (!NSK_JNI_VERIFY(jni, (exception = jni->NewObject(cls, ctor)) != NULL))
695 return NSK_FALSE;
696
697 NSK_DISPLAY0("Checking positive: InterruptThread\n");
698 if (!NSK_JVMTI_VERIFY(jvmti->InterruptThread(thread)))
699 return NSK_FALSE;
700
701 NSK_DISPLAY0("Checking positive: StopThread\n");
702 if (!NSK_JVMTI_VERIFY(jvmti->StopThread(thread, exception)))
703 return NSK_FALSE;
704
705 return NSK_TRUE;
706 }
707
708 /* ========================================================================== */
709
710 /* Check generated events
711 */
checkGeneratedEvents()712 static int checkGeneratedEvents() {
713 int result = NSK_TRUE;
714
715 NSK_DISPLAY1("FieldAccess events received: %d\n",
716 FieldAccessEventsCount);
717 if (!NSK_VERIFY(FieldAccessEventsCount != 0))
718 result = NSK_FALSE;
719
720 NSK_DISPLAY1("FieldModification events received: %d\n",
721 FieldModificationEventsCount);
722 if (!NSK_VERIFY(FieldModificationEventsCount != 0))
723 result = NSK_FALSE;
724
725 NSK_DISPLAY1("SingleStep events received: %d\n",
726 SingleStepEventsCount);
727 if (!NSK_VERIFY(SingleStepEventsCount != 0))
728 result = NSK_FALSE;
729
730 NSK_DISPLAY1("Exception events received: %d\n",
731 ExceptionEventsCount);
732 if (!NSK_VERIFY(ExceptionEventsCount != 0))
733 result = NSK_FALSE;
734
735 NSK_DISPLAY1("ExceptionCatch events received: %d\n",
736 ExceptionCatchEventsCount);
737 if (!NSK_VERIFY(ExceptionCatchEventsCount != 0))
738 result = NSK_FALSE;
739
740 NSK_DISPLAY1("Breakpoint events received: %d\n",
741 BreakpointEventsCount);
742 if (!NSK_VERIFY(BreakpointEventsCount != 0))
743 result = NSK_FALSE;
744
745 NSK_DISPLAY1("FramePop events received: %d\n",
746 FramePopEventsCount);
747 if (!NSK_VERIFY(FramePopEventsCount != 0))
748 result = NSK_FALSE;
749
750 NSK_DISPLAY1("MethodEntry events received: %d\n",
751 MethodEntryEventsCount);
752 if (!NSK_VERIFY(MethodEntryEventsCount != 0))
753 result = NSK_FALSE;
754
755 NSK_DISPLAY1("MethodExit events received: %d\n",
756 MethodExitEventsCount);
757 if (!NSK_VERIFY(MethodExitEventsCount != 0))
758 result = NSK_FALSE;
759
760 return result;
761 }
762
763 /* ========================================================================== */
764
765 /* agent algorithm */
766 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)767 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
768
769 /* wait for initial sync */
770 if (!nsk_jvmti_waitForSync(timeout))
771 return;
772
773 if (!prepare(jvmti, jni)) {
774 nsk_jvmti_setFailStatus();
775 return;
776 }
777
778 NSK_DISPLAY0("Testcase #1: check if GetCapabilities returns the capabilities\n");
779 if (!checkGetCapabilities(jvmti)) {
780 nsk_jvmti_setFailStatus();
781 }
782
783 NSK_DISPLAY0("Testcase #2: check if correspondent functions work\n");
784 if (!checkGetBytecodes(jvmti))
785 nsk_jvmti_setFailStatus();
786 if (!checkIsSyntheticFunctions(jvmti))
787 nsk_jvmti_setFailStatus();
788 if (!checkRedefineClasses(jvmti))
789 nsk_jvmti_setFailStatus();
790 if (!checkGetSourceFileName(jvmti))
791 nsk_jvmti_setFailStatus();
792 if (!checkGetLineNumberTable(jvmti))
793 nsk_jvmti_setFailStatus();
794 if (!checkGetSourceDebugExtension(jvmti))
795 nsk_jvmti_setFailStatus();
796 if (!checkLocalVariableFunctions(jvmti))
797 nsk_jvmti_setFailStatus();
798 if (!checkSuspend(jvmti))
799 nsk_jvmti_setFailStatus();
800
801 if (!prepareEvents(jvmti, jni)) {
802 nsk_jvmti_setFailStatus();
803 return;
804 }
805
806 /* resume debugee and wait for sync */
807 if (!nsk_jvmti_resumeSync())
808 return;
809 if (!nsk_jvmti_waitForSync(timeout))
810 return;
811
812 if (!checkPopFrame(jvmti))
813 nsk_jvmti_setFailStatus();
814 if (!checkSignalThread(jvmti, jni))
815 nsk_jvmti_setFailStatus();
816
817 NSK_TRACE(jni->DeleteGlobalRef(thread));
818 NSK_TRACE(jni->DeleteGlobalRef(klass));
819
820 /* resume debugee and wait for sync */
821 if (!nsk_jvmti_resumeSync())
822 return;
823 if (!nsk_jvmti_waitForSync(timeout))
824 return;
825
826 NSK_DISPLAY0("Testcase #3: check if the events are generated\n");
827 if (!checkGeneratedEvents()) {
828 nsk_jvmti_setFailStatus();
829 }
830
831 /* resume debugee after last sync */
832 if (!nsk_jvmti_resumeSync())
833 return;
834 }
835
836 /* ========================================================================== */
837
838 /* agent library initialization */
839 #ifdef STATIC_BUILD
Agent_OnLoad_cm03t001(JavaVM * jvm,char * options,void * reserved)840 JNIEXPORT jint JNICALL Agent_OnLoad_cm03t001(JavaVM *jvm, char *options, void *reserved) {
841 return Agent_Initialize(jvm, options, reserved);
842 }
Agent_OnAttach_cm03t001(JavaVM * jvm,char * options,void * reserved)843 JNIEXPORT jint JNICALL Agent_OnAttach_cm03t001(JavaVM *jvm, char *options, void *reserved) {
844 return Agent_Initialize(jvm, options, reserved);
845 }
JNI_OnLoad_cm03t001(JavaVM * jvm,char * options,void * reserved)846 JNIEXPORT jint JNI_OnLoad_cm03t001(JavaVM *jvm, char *options, void *reserved) {
847 return JNI_VERSION_1_8;
848 }
849 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)850 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
851 jvmtiEnv* jvmti = NULL;
852 jvmtiCapabilities caps;
853 jvmtiEventCallbacks callbacks;
854
855 /* init framework and parse options */
856 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
857 return JNI_ERR;
858
859 timeout = nsk_jvmti_getWaitTime() * 60000;
860 NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
861
862 /* create JVMTI environment */
863 if (!NSK_VERIFY((jvmti =
864 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
865 return JNI_ERR;
866
867 /* add capabilities */
868 memset(&caps, 0, sizeof(caps));
869 caps.can_get_bytecodes = 1;
870 caps.can_get_synthetic_attribute = 1;
871 caps.can_pop_frame = 1;
872 caps.can_redefine_classes = 1;
873 caps.can_signal_thread = 1;
874 caps.can_get_source_file_name = 1;
875 caps.can_get_line_numbers = 1;
876 caps.can_get_source_debug_extension = 1;
877 caps.can_access_local_variables = 1;
878 caps.can_suspend = 1;
879 caps.can_generate_field_modification_events = 1;
880 caps.can_generate_field_access_events = 1;
881 caps.can_generate_single_step_events = 1;
882 caps.can_generate_exception_events = 1;
883 caps.can_generate_frame_pop_events = 1;
884 caps.can_generate_breakpoint_events = 1;
885 caps.can_generate_method_entry_events = 1;
886 caps.can_generate_method_exit_events = 1;
887 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
888 return JNI_ERR;
889
890 /* set event callbacks */
891 memset(&callbacks, 0, sizeof(callbacks));
892 callbacks.ClassFileLoadHook = &ClassFileLoadHook;
893 callbacks.FieldAccess = &FieldAccess;
894 callbacks.FieldModification = &FieldModification;
895 callbacks.SingleStep = &SingleStep;
896 callbacks.Exception = &Exception;
897 callbacks.ExceptionCatch = &ExceptionCatch;
898 callbacks.Breakpoint = &Breakpoint;
899 callbacks.FramePop = &FramePop;
900 callbacks.MethodEntry = &MethodEntry;
901 callbacks.MethodExit = &MethodExit;
902 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
903 return JNI_ERR;
904
905 /* enable events */
906 if (!NSK_JVMTI_VERIFY(
907 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
908 return JNI_ERR;
909 if (!NSK_JVMTI_VERIFY(
910 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FIELD_ACCESS, NULL)))
911 return JNI_ERR;
912 if (!NSK_JVMTI_VERIFY(
913 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FIELD_MODIFICATION, NULL)))
914 return JNI_ERR;
915 if (!NSK_JVMTI_VERIFY(
916 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
917 return JNI_ERR;
918 if (!NSK_JVMTI_VERIFY(
919 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL)))
920 return JNI_ERR;
921
922 /* register agent proc and arg */
923 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
924 return JNI_ERR;
925
926 return JNI_OK;
927 }
928
929 /* ========================================================================== */
930
931 }
932