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 /* get tested thread method 'delay' */
343 if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "delay", "()V")) != NULL))
344 return NSK_FALSE;
345
346 /* get tested thread field 'waitingFlag' */
347 if (!NSK_JNI_VERIFY(jni, (field = jni->GetFieldID(klass, "waitingFlag", "Z")) != NULL))
348 return NSK_FALSE;
349
350 return NSK_TRUE;
351 }
352
prepareEvents(jvmtiEnv * jvmti,JNIEnv * jni)353 static int prepareEvents(jvmtiEnv* jvmti, JNIEnv* jni) {
354 NSK_DISPLAY0("Prepare events ...\n");
355
356 /* get tested thread method 'letItGo' */
357 if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "letItGo", "()V")) != NULL))
358 return NSK_FALSE;
359
360 /* get tested thread field 'waitingFlag' */
361 if (!NSK_JNI_VERIFY(jni, (field = jni->GetFieldID(klass, "waitingFlag", "Z")) != NULL))
362 return NSK_FALSE;
363
364 if (!NSK_JVMTI_VERIFY(jvmti->SetFieldAccessWatch(klass, field)))
365 return NSK_FALSE;
366
367 if (!NSK_JVMTI_VERIFY(jvmti->SetFieldModificationWatch(klass, field)))
368 return NSK_FALSE;
369
370 if (!NSK_JVMTI_VERIFY(jvmti->SetBreakpoint(method, 0)))
371 return NSK_FALSE;
372
373 /* enable events */
374 if (!NSK_JVMTI_VERIFY(
375 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
376 return JNI_ERR;
377 if (!NSK_JVMTI_VERIFY(
378 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
379 return JNI_ERR;
380 if (!NSK_JVMTI_VERIFY(
381 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION_CATCH, NULL)))
382 return JNI_ERR;
383 if (!NSK_JVMTI_VERIFY(
384 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, thread)))
385 return NSK_FALSE;
386 if (!NSK_JVMTI_VERIFY(
387 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread)))
388 return NSK_FALSE;
389
390 return NSK_TRUE;
391 }
392
393 /* ========================================================================== */
394
395 /* Check GetCapabilities function
396 */
checkGetCapabilities(jvmtiEnv * jvmti)397 static int checkGetCapabilities(jvmtiEnv* jvmti) {
398 jvmtiCapabilities caps;
399
400 memset(&caps, 0, sizeof(caps));
401 if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
402 return NSK_FALSE;
403 if (!NSK_VERIFY(caps.can_get_bytecodes))
404 return NSK_FALSE;
405 if (!NSK_VERIFY(caps.can_get_synthetic_attribute))
406 return NSK_FALSE;
407 if (!NSK_VERIFY(caps.can_pop_frame))
408 return NSK_FALSE;
409 if (!NSK_VERIFY(caps.can_redefine_classes))
410 return NSK_FALSE;
411 if (!NSK_VERIFY(caps.can_signal_thread))
412 return NSK_FALSE;
413 if (!NSK_VERIFY(caps.can_get_source_file_name))
414 return NSK_FALSE;
415 if (!NSK_VERIFY(caps.can_get_line_numbers))
416 return NSK_FALSE;
417 if (!NSK_VERIFY(caps.can_get_source_debug_extension))
418 return NSK_FALSE;
419 if (!NSK_VERIFY(caps.can_access_local_variables))
420 return NSK_FALSE;
421 if (!NSK_VERIFY(caps.can_suspend))
422 return NSK_FALSE;
423 if (!NSK_VERIFY(caps.can_generate_field_modification_events))
424 return NSK_FALSE;
425 if (!NSK_VERIFY(caps.can_generate_field_access_events))
426 return NSK_FALSE;
427 if (!NSK_VERIFY(caps.can_generate_single_step_events))
428 return NSK_FALSE;
429 if (!NSK_VERIFY(caps.can_generate_exception_events))
430 return NSK_FALSE;
431 if (!NSK_VERIFY(caps.can_generate_frame_pop_events))
432 return NSK_FALSE;
433 if (!NSK_VERIFY(caps.can_generate_breakpoint_events))
434 return NSK_FALSE;
435 if (!NSK_VERIFY(caps.can_generate_method_entry_events))
436 return NSK_FALSE;
437 if (!NSK_VERIFY(caps.can_generate_method_exit_events))
438 return NSK_FALSE;
439
440 return NSK_TRUE;
441 }
442
443 /* ========================================================================== */
444
445 /* Check "can_get_bytecodes" function
446 */
checkGetBytecodes(jvmtiEnv * jvmti)447 static int checkGetBytecodes(jvmtiEnv* jvmti) {
448 jint count;
449 unsigned char *bytecodes;
450
451 NSK_DISPLAY0("Checking positive: GetBytecodes\n");
452 if (!NSK_JVMTI_VERIFY(jvmti->GetBytecodes(method, &count, &bytecodes)))
453 return NSK_FALSE;
454 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate(bytecodes)))
455 return NSK_FALSE;
456
457 return NSK_TRUE;
458 }
459
460 /* Check "can_get_synthetic_attribute" functions
461 */
checkIsSyntheticFunctions(jvmtiEnv * jvmti)462 static int checkIsSyntheticFunctions(jvmtiEnv* jvmti) {
463 jboolean is_synthetic;
464
465 NSK_DISPLAY0("Checking positive: IsFieldSynthetic\n");
466 if (!NSK_JVMTI_VERIFY(jvmti->IsFieldSynthetic(klass, field, &is_synthetic)))
467 return NSK_FALSE;
468
469 NSK_DISPLAY0("Checking positive: IsMethodSynthetic\n");
470 if (!NSK_JVMTI_VERIFY(jvmti->IsMethodSynthetic(method, &is_synthetic)))
471 return NSK_FALSE;
472
473 return NSK_TRUE;
474 }
475
476 /* Check "can_redefine_classes" functions
477 */
checkRedefineClasses(jvmtiEnv * jvmti)478 static int checkRedefineClasses(jvmtiEnv* jvmti) {
479 jvmtiClassDefinition class_def;
480 jboolean is_obsolete;
481
482 if (!NSK_VERIFY(klass_byte_count != 0 && klass_bytes != NULL))
483 return NSK_FALSE;
484
485 NSK_DISPLAY0("Checking positive: RedefineClasses\n");
486 class_def.klass = klass;
487 class_def.class_byte_count = klass_byte_count;
488 class_def.class_bytes = klass_bytes;
489 if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &class_def)))
490 return NSK_FALSE;
491
492 NSK_DISPLAY0("Checking positive: IsMethodObsolete\n");
493 if (!NSK_JVMTI_VERIFY(jvmti->IsMethodObsolete(method, &is_obsolete)))
494 return NSK_FALSE;
495
496 return NSK_TRUE;
497 }
498
499 /* Check "can_get_source_file_name" function
500 */
checkGetSourceFileName(jvmtiEnv * jvmti)501 static int checkGetSourceFileName(jvmtiEnv* jvmti) {
502 char *name;
503
504 NSK_DISPLAY0("Checking positive: GetSourceFileName\n");
505 if (!NSK_JVMTI_VERIFY(jvmti->GetSourceFileName(klass, &name)))
506 return NSK_FALSE;
507
508 return NSK_TRUE;
509 }
510
511 /* Check "can_get_line_numbers" function
512 */
checkGetLineNumberTable(jvmtiEnv * jvmti)513 static int checkGetLineNumberTable(jvmtiEnv* jvmti) {
514 jint count;
515 jvmtiLineNumberEntry *line_number_table = NULL;
516
517 NSK_DISPLAY0("Checking positive: GetLineNumberTable\n");
518 if (!NSK_JVMTI_VERIFY(jvmti->GetLineNumberTable(method, &count, &line_number_table)))
519 return NSK_FALSE;
520
521 return NSK_TRUE;
522 }
523
524 /* Check "can_get_source_debug_extension" function
525 */
checkGetSourceDebugExtension(jvmtiEnv * jvmti)526 static int checkGetSourceDebugExtension(jvmtiEnv* jvmti) {
527 char *name;
528
529 NSK_DISPLAY0("Checking positive: GetSourceDebugExtension\n");
530 if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_ABSENT_INFORMATION,
531 jvmti->GetSourceDebugExtension(klass, &name)))
532 return NSK_FALSE;
533
534 return NSK_TRUE;
535 }
536
537 /* Check "can_access_local_variables" functions
538 */
checkLocalVariableFunctions(jvmtiEnv * jvmti)539 static int checkLocalVariableFunctions(jvmtiEnv* jvmti) {
540 jint count;
541 jvmtiLocalVariableEntry *local_variable_table = NULL;
542 jobject object_value;
543 jint int_value;
544 jlong long_value;
545 jfloat float_value;
546 jdouble double_value;
547 int i;
548
549 NSK_DISPLAY0("Checking positive: GetLocalVariableTable\n");
550 if (!NSK_JVMTI_VERIFY(jvmti->GetLocalVariableTable(method, &count, &local_variable_table)))
551 return NSK_FALSE;
552
553 /* DEBUG -- while 4913796 bug not fixed thread should be suspended
554 */
555 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
556 return NSK_FALSE;
557
558 for (i = 0; i < count; i++) {
559 if (strcmp(local_variable_table[i].name, "o") == 0) {
560 NSK_DISPLAY0("Checking positive: GetLocalObject\n");
561 if (!NSK_JVMTI_VERIFY(
562 jvmti->GetLocalObject(thread, 1, local_variable_table[i].slot, &object_value)))
563 return NSK_FALSE;
564
565 NSK_DISPLAY0("Checking positive: SetLocalObject\n");
566 if (!NSK_JVMTI_VERIFY(
567 jvmti->SetLocalObject(thread, 1, local_variable_table[i].slot, object_value)))
568 return NSK_FALSE;
569 } else if (strcmp(local_variable_table[i].name, "i") == 0) {
570 NSK_DISPLAY0("Checking positive: GetLocalInt\n");
571 if (!NSK_JVMTI_VERIFY(
572 jvmti->GetLocalInt(thread, 1, local_variable_table[i].slot, &int_value)))
573 return NSK_FALSE;
574
575 NSK_DISPLAY0("Checking positive: SetLocalInt\n");
576 if (!NSK_JVMTI_VERIFY(
577 jvmti->SetLocalInt(thread, 1, local_variable_table[i].slot, int_value)))
578 return NSK_FALSE;
579 } else if (strcmp(local_variable_table[i].name, "l") == 0) {
580 NSK_DISPLAY0("Checking positive: GetLocalLong\n");
581 if (!NSK_JVMTI_VERIFY(
582 jvmti->GetLocalLong(thread, 1, local_variable_table[i].slot, &long_value)))
583 return NSK_FALSE;
584
585 NSK_DISPLAY0("Checking positive: SetLocalLong\n");
586 if (!NSK_JVMTI_VERIFY(
587 jvmti->SetLocalLong(thread, 1, local_variable_table[i].slot, long_value)))
588 return NSK_FALSE;
589 } else if (strcmp(local_variable_table[i].name, "f") == 0) {
590 NSK_DISPLAY0("Checking positive: GetLocalFloat\n");
591 if (!NSK_JVMTI_VERIFY(
592 jvmti->GetLocalFloat(thread, 1, local_variable_table[i].slot, &float_value)))
593 return NSK_FALSE;
594
595 NSK_DISPLAY0("Checking positive: SetLocalFloat\n");
596 if (!NSK_JVMTI_VERIFY(
597 jvmti->SetLocalFloat(thread, 1, local_variable_table[i].slot, float_value)))
598 return NSK_FALSE;
599 } else if (strcmp(local_variable_table[i].name, "d") == 0) {
600 NSK_DISPLAY0("Checking positive: GetLocalDouble\n");
601 if (!NSK_JVMTI_VERIFY(
602 jvmti->GetLocalDouble(thread, 1, local_variable_table[i].slot, &double_value)))
603 return NSK_FALSE;
604
605 NSK_DISPLAY0("Checking positive: SetLocalDouble\n");
606 if (!NSK_JVMTI_VERIFY(
607 jvmti->SetLocalDouble(thread, 1, local_variable_table[i].slot, double_value)))
608 return NSK_FALSE;
609 }
610 }
611
612 /* DEBUG -- while 4913796 bug not fixed thread should be suspended
613 */
614 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
615 return NSK_FALSE;
616
617 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)local_variable_table)))
618 return NSK_FALSE;
619
620 return NSK_TRUE;
621 }
622
623 /* Check "can_suspend" functions
624 */
checkSuspend(jvmtiEnv * jvmti)625 static int checkSuspend(jvmtiEnv* jvmti) {
626 jvmtiError err;
627
628 NSK_DISPLAY0("Checking positive: SuspendThread\n");
629 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
630 return NSK_FALSE;
631
632 NSK_DISPLAY0("Checking positive: ResumeThread\n");
633 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
634 return NSK_FALSE;
635
636 NSK_DISPLAY0("Checking positive: SuspendThreadList\n");
637 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(1, &thread, &err)))
638 return NSK_FALSE;
639
640 NSK_DISPLAY0("Checking positive: ResumeThreadList\n");
641 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(1, &thread, &err)))
642 return NSK_FALSE;
643
644 return NSK_TRUE;
645 }
646
647 /* Check "can_pop_frame" function
648 */
checkPopFrame(jvmtiEnv * jvmti)649 static int checkPopFrame(jvmtiEnv* jvmti) {
650 int result = NSK_TRUE;
651 jvmtiError err;
652
653 NSK_DISPLAY0("Checking positive: PopFrame\n");
654 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
655 return NSK_FALSE;
656
657 // PopFrame is allowed to fail with JVMTI_ERROR_OPAQUE_FRAME.
658 // That will happen if we are in a native function,
659 // for example while waiting for a Condition.
660 // See JCK-5020108.
661 err = jvmti->PopFrame(thread);
662 if (err != JVMTI_ERROR_NONE && err != JVMTI_ERROR_OPAQUE_FRAME) {
663 result = NSK_FALSE;
664 NSK_DISPLAY1("jvmti error from PopFrame: %d\n", err);
665 }
666
667 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
668 result = NSK_FALSE;
669
670 return result;
671 }
672
673 /* Check "can_signal_thread" functions
674 */
checkSignalThread(jvmtiEnv * jvmti,JNIEnv * jni)675 static int checkSignalThread(jvmtiEnv* jvmti, JNIEnv* jni) {
676 const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath";
677 const char* THREAD_DEATH_CTOR_NAME = "<init>";
678 const char* THREAD_DEATH_CTOR_SIGNATURE = "()V";
679 jclass cls = NULL;
680 jmethodID ctor = NULL;
681 jobject exception = NULL;
682
683 if (!NSK_JNI_VERIFY(jni, (cls = jni->FindClass(THREAD_DEATH_CLASS_NAME)) != NULL))
684 return NSK_FALSE;
685
686 if (!NSK_JNI_VERIFY(jni, (ctor =
687 jni->GetMethodID(cls, THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL))
688 return NSK_FALSE;
689
690 if (!NSK_JNI_VERIFY(jni, (exception = jni->NewObject(cls, ctor)) != NULL))
691 return NSK_FALSE;
692
693 NSK_DISPLAY0("Checking positive: InterruptThread\n");
694 if (!NSK_JVMTI_VERIFY(jvmti->InterruptThread(thread)))
695 return NSK_FALSE;
696
697 NSK_DISPLAY0("Checking positive: StopThread\n");
698 if (!NSK_JVMTI_VERIFY(jvmti->StopThread(thread, exception)))
699 return NSK_FALSE;
700
701 return NSK_TRUE;
702 }
703
704 /* ========================================================================== */
705
706 /* Check generated events
707 */
checkGeneratedEvents()708 static int checkGeneratedEvents() {
709 int result = NSK_TRUE;
710
711 NSK_DISPLAY1("FieldAccess events received: %d\n",
712 FieldAccessEventsCount);
713 if (!NSK_VERIFY(FieldAccessEventsCount != 0))
714 result = NSK_FALSE;
715
716 NSK_DISPLAY1("FieldModification events received: %d\n",
717 FieldModificationEventsCount);
718 if (!NSK_VERIFY(FieldModificationEventsCount != 0))
719 result = NSK_FALSE;
720
721 NSK_DISPLAY1("SingleStep events received: %d\n",
722 SingleStepEventsCount);
723 if (!NSK_VERIFY(SingleStepEventsCount != 0))
724 result = NSK_FALSE;
725
726 NSK_DISPLAY1("Exception events received: %d\n",
727 ExceptionEventsCount);
728 if (!NSK_VERIFY(ExceptionEventsCount != 0))
729 result = NSK_FALSE;
730
731 NSK_DISPLAY1("ExceptionCatch events received: %d\n",
732 ExceptionCatchEventsCount);
733 if (!NSK_VERIFY(ExceptionCatchEventsCount != 0))
734 result = NSK_FALSE;
735
736 NSK_DISPLAY1("Breakpoint events received: %d\n",
737 BreakpointEventsCount);
738 if (!NSK_VERIFY(BreakpointEventsCount != 0))
739 result = NSK_FALSE;
740
741 NSK_DISPLAY1("FramePop events received: %d\n",
742 FramePopEventsCount);
743 if (!NSK_VERIFY(FramePopEventsCount != 0))
744 result = NSK_FALSE;
745
746 NSK_DISPLAY1("MethodEntry events received: %d\n",
747 MethodEntryEventsCount);
748 if (!NSK_VERIFY(MethodEntryEventsCount != 0))
749 result = NSK_FALSE;
750
751 NSK_DISPLAY1("MethodExit events received: %d\n",
752 MethodExitEventsCount);
753 if (!NSK_VERIFY(MethodExitEventsCount != 0))
754 result = NSK_FALSE;
755
756 return result;
757 }
758
759 /* ========================================================================== */
760
761 /* agent algorithm */
762 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)763 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
764
765 /* wait for initial sync */
766 if (!nsk_jvmti_waitForSync(timeout))
767 return;
768
769 if (!prepare(jvmti, jni)) {
770 nsk_jvmti_setFailStatus();
771 return;
772 }
773
774 NSK_DISPLAY0("Testcase #1: check if GetCapabilities returns the capabilities\n");
775 if (!checkGetCapabilities(jvmti)) {
776 nsk_jvmti_setFailStatus();
777 }
778
779 NSK_DISPLAY0("Testcase #2: check if correspondent functions work\n");
780 if (!checkGetBytecodes(jvmti))
781 nsk_jvmti_setFailStatus();
782 if (!checkIsSyntheticFunctions(jvmti))
783 nsk_jvmti_setFailStatus();
784 if (!checkRedefineClasses(jvmti))
785 nsk_jvmti_setFailStatus();
786 if (!checkGetSourceFileName(jvmti))
787 nsk_jvmti_setFailStatus();
788 if (!checkGetLineNumberTable(jvmti))
789 nsk_jvmti_setFailStatus();
790 if (!checkGetSourceDebugExtension(jvmti))
791 nsk_jvmti_setFailStatus();
792 if (!checkLocalVariableFunctions(jvmti))
793 nsk_jvmti_setFailStatus();
794 if (!checkSuspend(jvmti))
795 nsk_jvmti_setFailStatus();
796
797 if (!prepareEvents(jvmti, jni)) {
798 nsk_jvmti_setFailStatus();
799 return;
800 }
801
802 /* resume debugee and wait for sync */
803 if (!nsk_jvmti_resumeSync())
804 return;
805 if (!nsk_jvmti_waitForSync(timeout))
806 return;
807
808 if (!checkPopFrame(jvmti))
809 nsk_jvmti_setFailStatus();
810 if (!checkSignalThread(jvmti, jni))
811 nsk_jvmti_setFailStatus();
812
813 NSK_TRACE(jni->DeleteGlobalRef(thread));
814
815 /* resume debugee and wait for sync */
816 if (!nsk_jvmti_resumeSync())
817 return;
818 if (!nsk_jvmti_waitForSync(timeout))
819 return;
820
821 NSK_DISPLAY0("Testcase #3: check if the events are generated\n");
822 if (!checkGeneratedEvents()) {
823 nsk_jvmti_setFailStatus();
824 }
825
826 /* resume debugee after last sync */
827 if (!nsk_jvmti_resumeSync())
828 return;
829 }
830
831 /* ========================================================================== */
832
833 /* agent library initialization */
834 #ifdef STATIC_BUILD
Agent_OnLoad_cm03t001(JavaVM * jvm,char * options,void * reserved)835 JNIEXPORT jint JNICALL Agent_OnLoad_cm03t001(JavaVM *jvm, char *options, void *reserved) {
836 return Agent_Initialize(jvm, options, reserved);
837 }
Agent_OnAttach_cm03t001(JavaVM * jvm,char * options,void * reserved)838 JNIEXPORT jint JNICALL Agent_OnAttach_cm03t001(JavaVM *jvm, char *options, void *reserved) {
839 return Agent_Initialize(jvm, options, reserved);
840 }
JNI_OnLoad_cm03t001(JavaVM * jvm,char * options,void * reserved)841 JNIEXPORT jint JNI_OnLoad_cm03t001(JavaVM *jvm, char *options, void *reserved) {
842 return JNI_VERSION_1_8;
843 }
844 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)845 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
846 jvmtiEnv* jvmti = NULL;
847 jvmtiCapabilities caps;
848 jvmtiEventCallbacks callbacks;
849
850 /* init framework and parse options */
851 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
852 return JNI_ERR;
853
854 timeout = nsk_jvmti_getWaitTime() * 60000;
855 NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
856
857 /* create JVMTI environment */
858 if (!NSK_VERIFY((jvmti =
859 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
860 return JNI_ERR;
861
862 /* add capabilities */
863 memset(&caps, 0, sizeof(caps));
864 caps.can_get_bytecodes = 1;
865 caps.can_get_synthetic_attribute = 1;
866 caps.can_pop_frame = 1;
867 caps.can_redefine_classes = 1;
868 caps.can_signal_thread = 1;
869 caps.can_get_source_file_name = 1;
870 caps.can_get_line_numbers = 1;
871 caps.can_get_source_debug_extension = 1;
872 caps.can_access_local_variables = 1;
873 caps.can_suspend = 1;
874 caps.can_generate_field_modification_events = 1;
875 caps.can_generate_field_access_events = 1;
876 caps.can_generate_single_step_events = 1;
877 caps.can_generate_exception_events = 1;
878 caps.can_generate_frame_pop_events = 1;
879 caps.can_generate_breakpoint_events = 1;
880 caps.can_generate_method_entry_events = 1;
881 caps.can_generate_method_exit_events = 1;
882 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
883 return JNI_ERR;
884
885 /* set event callbacks */
886 memset(&callbacks, 0, sizeof(callbacks));
887 callbacks.ClassFileLoadHook = &ClassFileLoadHook;
888 callbacks.FieldAccess = &FieldAccess;
889 callbacks.FieldModification = &FieldModification;
890 callbacks.SingleStep = &SingleStep;
891 callbacks.Exception = &Exception;
892 callbacks.ExceptionCatch = &ExceptionCatch;
893 callbacks.Breakpoint = &Breakpoint;
894 callbacks.FramePop = &FramePop;
895 callbacks.MethodEntry = &MethodEntry;
896 callbacks.MethodExit = &MethodExit;
897 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
898 return JNI_ERR;
899
900 /* enable events */
901 if (!NSK_JVMTI_VERIFY(
902 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
903 return JNI_ERR;
904 if (!NSK_JVMTI_VERIFY(
905 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FIELD_ACCESS, NULL)))
906 return JNI_ERR;
907 if (!NSK_JVMTI_VERIFY(
908 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FIELD_MODIFICATION, NULL)))
909 return JNI_ERR;
910 if (!NSK_JVMTI_VERIFY(
911 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
912 return JNI_ERR;
913 if (!NSK_JVMTI_VERIFY(
914 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL)))
915 return JNI_ERR;
916
917 /* register agent proc and arg */
918 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
919 return JNI_ERR;
920
921 return JNI_OK;
922 }
923
924 /* ========================================================================== */
925
926 }
927