1 /* 2 * Copyright (c) 2001, 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 package nsk.jdi.Value._itself_; 25 26 import nsk.share.*; 27 import nsk.share.jpda.*; 28 import nsk.share.jdi.*; 29 30 import com.sun.jdi.*; 31 import java.util.*; 32 import java.io.*; 33 34 import com.sun.jdi.event.*; 35 import com.sun.jdi.request.*; 36 37 /** 38 * The test for the implementation of an object of the type <BR> 39 * Value. <BR> 40 * <BR> 41 * The test checks up that a Value object, mirroring neither <BR> 42 * a primitive type value nor a void, is casted to both <BR> 43 * ObjectReference and a corresponding object reference; <BR> 44 * the tested are ArrayReference, ClassObjectReference, <BR> 45 * StringReference, ThreadReference, and ThreadGroupReference. <BR> 46 * <BR> 47 * The test contains two test cases with different ways to get <BR> 48 * a tested Value object: <BR> 49 * Value = ReferenceType.getValue(Field); <BR> 50 * tested types are all of five mentioned above <BR> 51 * Value = StackFrame.getValue(LocalVariable); <BR> 52 * tested types are String, Array, and ClassObject only <BR> 53 */ 54 55 public class value001 { 56 57 //----------------------------------------------------- templete section 58 static final int PASSED = 0; 59 static final int FAILED = 2; 60 static final int PASS_BASE = 95; 61 62 //----------------------------------------------------- templete parameters 63 static final String 64 sHeader1 = "\n==> nsk/jdi/Value/_itself_/value001 ", 65 sHeader2 = "--> debugger: ", 66 sHeader3 = "##> debugger: "; 67 68 //----------------------------------------------------- main method 69 main(String argv[])70 public static void main (String argv[]) { 71 int result = run(argv, System.out); 72 System.exit(result + PASS_BASE); 73 } 74 run(String argv[], PrintStream out)75 public static int run (String argv[], PrintStream out) { 76 return new value001().runThis(argv, out); 77 } 78 79 //-------------------------------------------------- log procedures 80 81 private static Log logHandler; 82 log1(String message)83 private static void log1(String message) { 84 logHandler.display(sHeader1 + message); 85 } log2(String message)86 private static void log2(String message) { 87 logHandler.display(sHeader2 + message); 88 } log3(String message)89 private static void log3(String message) { 90 logHandler.complain(sHeader3 + message); 91 } 92 93 // ************************************************ test parameters 94 95 private String debuggeeName = 96 "nsk.jdi.Value._itself_.value001a"; 97 98 private String testedClassName = 99 "nsk.jdi.Value._itself_.Threadvalue001a"; 100 101 //String mName = "nsk.jdi.Value._itself_"; 102 103 //====================================================== test program 104 //------------------------------------------------------ common section 105 106 static ArgumentHandler argsHandler; 107 108 static int waitTime; 109 110 static VirtualMachine vm = null; 111 112 static EventRequestManager eventRManager = null; 113 static EventQueue eventQueue = null; 114 static EventSet eventSet = null; 115 116 ReferenceType testedclass = null; 117 ThreadReference thread2 = null; 118 119 static int testExitCode = PASSED; 120 121 static final int returnCode0 = 0; 122 static final int returnCode1 = 1; 123 static final int returnCode2 = 2; 124 static final int returnCode3 = 3; 125 static final int returnCode4 = 4; 126 127 //------------------------------------------------------ methods 128 runThis(String argv[], PrintStream out)129 private int runThis (String argv[], PrintStream out) { 130 131 Debugee debuggee; 132 133 argsHandler = new ArgumentHandler(argv); 134 logHandler = new Log(out, argsHandler); 135 Binder binder = new Binder(argsHandler, logHandler); 136 137 if (argsHandler.verbose()) { 138 debuggee = binder.bindToDebugee(debuggeeName + " -vbs"); 139 } else { 140 debuggee = binder.bindToDebugee(debuggeeName); 141 } 142 143 waitTime = argsHandler.getWaitTime(); 144 145 146 IOPipe pipe = new IOPipe(debuggee); 147 148 debuggee.redirectStderr(out); 149 log2("issuspended002a debuggee launched"); 150 debuggee.resume(); 151 152 String line = pipe.readln(); 153 if ((line == null) || !line.equals("ready")) { 154 log3("signal received is not 'ready' but: " + line); 155 return FAILED; 156 } else { 157 log2("'ready' recieved"); 158 } 159 160 vm = debuggee.VM(); 161 162 //------------------------------------------------------ testing section 163 log1(" TESTING BEGINS"); 164 165 for (int i = 0; ; i++) { 166 167 pipe.println("newcheck"); 168 line = pipe.readln(); 169 170 if (line.equals("checkend")) { 171 log2(" : returned string is 'checkend'"); 172 break ; 173 } else if (!line.equals("checkready")) { 174 log3("ERROR: returned string is not 'checkready'"); 175 testExitCode = FAILED; 176 break ; 177 } 178 179 log1("new checkready: #" + i); 180 181 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part 182 183 log2("===> test case : Value = ReferenceType.getValue(Field);"); 184 185 String stringObjName = "str"; 186 String arrayObjName = "bArray"; 187 String threadObjName = "thread2"; 188 String threadgroupObjName = "threadGroupObj"; 189 String classObj1Name = "classObj1"; 190 String classObj2Name = "classObj2"; 191 192 ObjectReference stringObjRef = null; 193 ObjectReference arrayObjRef = null; 194 ObjectReference threadObjRef = null; 195 ObjectReference threadgroupObjRef = null; 196 ObjectReference classObj1Ref = null; 197 ObjectReference classObj2Ref = null; 198 199 ReferenceType debuggeeClass = null; 200 201 Value val = null; 202 203 204 List classes = null; 205 206 classes = vm.classesByName(debuggeeName); 207 debuggeeClass = (ReferenceType) classes.get(0); 208 209 210 log2("......check for a mirror of String object"); 211 val = debuggeeClass.getValue(debuggeeClass.fieldByName(stringObjName)); 212 try { 213 log2(" cast from Value to String"); 214 StringReference sRef = (StringReference) val; 215 } catch ( ClassCastException e ) { 216 log3("ERROR: ClassCastException"); 217 testExitCode = FAILED; 218 } 219 try { 220 log2(" cast from Value to ObjectReference"); 221 stringObjRef = (ObjectReference) val; 222 log2(" cast from ObjectReference to String"); 223 StringReference sRef = (StringReference) stringObjRef; 224 } catch ( ClassCastException e ) { 225 log3("ERROR: ClassCastException"); 226 testExitCode = FAILED; 227 } 228 229 230 log2("......check for a mirror of Array object"); 231 val = debuggeeClass.getValue(debuggeeClass.fieldByName(arrayObjName)); 232 try { 233 log2(" cast from Value to ArrayReference"); 234 ArrayReference aRef = (ArrayReference) val; 235 } catch ( ClassCastException e ) { 236 log3("ERROR: ClassCastException"); 237 testExitCode = FAILED; 238 } 239 try { 240 log2(" cast from Value to ObjectReference"); 241 arrayObjRef = (ObjectReference) val; 242 log2(" cast from ObjectReference to ArrayReference"); 243 ArrayReference aRef = (ArrayReference) arrayObjRef; 244 } catch ( ClassCastException e ) { 245 log3("ERROR: ClassCastException"); 246 testExitCode = FAILED; 247 } 248 249 250 log2("......check for a mirror of Thread object"); 251 val = debuggeeClass.getValue(debuggeeClass.fieldByName(threadObjName)); 252 try { 253 log2(" cast from Value to ThreadReference"); 254 ThreadReference tRef = (ThreadReference) val; 255 } catch ( ClassCastException e ) { 256 log3("ERROR: ClassCastException"); 257 testExitCode = FAILED; 258 } 259 try { 260 log2(" cast from Value to ObjectReference"); 261 threadObjRef = (ObjectReference) val; 262 log2(" cast from ObjectReference to ThreadReference"); 263 ThreadReference tRef = (ThreadReference) threadObjRef; 264 } catch ( ClassCastException e ) { 265 log3("ERROR: ClassCastException"); 266 testExitCode = FAILED; 267 } 268 269 270 log2("......check for a mirror of ThreadGroup object"); 271 val = debuggeeClass.getValue(debuggeeClass.fieldByName(threadgroupObjName)); 272 try { 273 log2(" cast from Value to ThreadGroupReference"); 274 ThreadGroupReference gRef = (ThreadGroupReference) val; 275 } catch ( ClassCastException e ) { 276 log3("ERROR: ClassCastException"); 277 testExitCode = FAILED; 278 } 279 try { 280 log2(" cast from Value to ObjectReference"); 281 threadgroupObjRef = (ObjectReference) val; 282 log2(" cast from ObjectReference to ThreadGroupReference"); 283 ThreadGroupReference gRef = (ThreadGroupReference) threadgroupObjRef; 284 } catch ( ClassCastException e ) { 285 log3("ERROR: ClassCastException"); 286 testExitCode = FAILED; 287 } 288 289 290 log2("......check for a mirror of Class object"); 291 val = debuggeeClass.getValue(debuggeeClass.fieldByName(classObj1Name)); 292 try { 293 log2(" cast from Value to ClassObjectReference"); 294 ClassObjectReference c1Ref = (ClassObjectReference) val; 295 } catch ( ClassCastException e ) { 296 log3("ERROR: ClassCastException"); 297 testExitCode = FAILED; 298 } 299 try { 300 log2(" cast from Value to ObjectReference"); 301 classObj1Ref = (ObjectReference) val; 302 log2(" cast from ObjectReference to ClassObjectReference"); 303 ClassObjectReference c1Ref = (ClassObjectReference) classObj1Ref; 304 } catch ( ClassCastException e ) { 305 log3("ERROR: ClassCastException"); 306 testExitCode = FAILED; 307 } 308 309 310 log2("......check for a mirror of Class object"); 311 val = debuggeeClass.getValue(debuggeeClass.fieldByName(classObj2Name)); 312 try { 313 log2(" cast from Value to ClassObjectReference"); 314 ClassObjectReference c2Ref = (ClassObjectReference) val; 315 } catch ( ClassCastException e ) { 316 log3("ERROR: ClassCastException"); 317 testExitCode = FAILED; 318 } 319 try { 320 log2(" cast from Value to ObjectReference"); 321 classObj2Ref = (ObjectReference) val; 322 log2(" cast from ObjectReference to ClassObjectReference"); 323 ClassObjectReference c2Ref = (ClassObjectReference) classObj2Ref; 324 } catch ( ClassCastException e ) { 325 log3("ERROR: ClassCastException"); 326 testExitCode = FAILED; 327 } 328 329 330 log2("===> test case : Value = StackFrame.getValue(LocalVariable);"); 331 332 eventRManager = vm.eventRequestManager(); 333 eventQueue = vm.eventQueue(); 334 335 String threadName = "Thread2"; 336 337 String breakpointMethod1 = "run"; 338 String bpLine1 = "breakpointLineNumber1"; 339 BreakpointRequest breakpRequest1 = null; 340 341 StackFrame stackFrame = null; 342 343 List allThreads = null; 344 ListIterator listIterator = null; 345 346 347 int expresult = returnCode0; 348 349 label0: { 350 351 log2("getting ThreadReference object"); 352 353 try { 354 allThreads = vm.allThreads(); 355 classes = vm.classesByName(testedClassName); 356 testedclass = (ReferenceType) classes.get(0); 357 } catch ( Exception e) { 358 log3("ERROR: Exception at very beginning !? : " + e); 359 expresult = returnCode1; 360 break label0; 361 } 362 363 listIterator = allThreads.listIterator(); 364 for (;;) { 365 try { 366 thread2 = (ThreadReference) listIterator.next(); 367 if (thread2.name().equals(threadName)) 368 break ; 369 } catch ( NoSuchElementException e ) { 370 log3("ERROR: NoSuchElementException for listIterator.next()"); 371 log3("ERROR: NO THREAD2 ?????????!!!!!!!"); 372 expresult = returnCode1; 373 break label0; 374 } 375 } 376 377 log2("setting up a breakpoint"); 378 379 breakpRequest1 = settingBreakpoint(breakpointMethod1, bpLine1, "one"); 380 if (breakpRequest1 == null) { 381 expresult = returnCode1; 382 break label0; 383 } 384 } 385 386 label1: { 387 388 if (expresult != returnCode0) 389 break label1; 390 391 log2(" enabling breakpRequest1"); 392 breakpRequest1.enable(); 393 394 log2(" forcing the main thread to leave synchronized block"); 395 pipe.println("continue"); 396 line = pipe.readln(); 397 if (!line.equals("docontinue")) { 398 log3("ERROR: returned string is not 'docontinue'"); 399 expresult = returnCode4; 400 break label1; 401 } 402 403 log2(" getting BreakpointEvent"); 404 expresult = breakpoint(); 405 if (expresult != returnCode0) 406 break label1; 407 408 409 log2(" getting thread2's StackFrame"); 410 try { 411 stackFrame = thread2.frame(0); 412 } catch ( Exception e ) { 413 log3("ERROR: Exception for stackFrame = thread2.frame(0) :" + e); 414 testExitCode = FAILED; 415 break label1; 416 } 417 418 String stringObj2Name = "str2"; 419 String arrayObj2Name = "bArray2"; 420 String classObj3Name = "classObj3"; 421 422 log2("......check for a mirror of String object"); 423 try { 424 val = stackFrame.getValue(stackFrame.visibleVariableByName(stringObj2Name)); 425 426 try { 427 log2(" cast from Value to String"); 428 StringReference sRef = (StringReference) val; 429 } catch ( ClassCastException e ) { 430 log3("ERROR: ClassCastException"); 431 testExitCode = FAILED; 432 } 433 try { 434 log2(" cast from Value to ObjectReference"); 435 stringObjRef = (ObjectReference) val; 436 log2(" cast from ObjectReference to String"); 437 StringReference sRef = (StringReference) stringObjRef; 438 } catch ( ClassCastException e ) { 439 log3("ERROR: ClassCastException"); 440 testExitCode = FAILED; 441 } 442 } catch ( AbsentInformationException e ) { 443 log3("ERROR: AbsentInformationException for: stackFrame.getValue()"); 444 expresult = returnCode1; 445 } 446 447 log2("......check for a mirror of Array object"); 448 try { 449 val = stackFrame.getValue(stackFrame.visibleVariableByName(arrayObj2Name)); 450 try { 451 log2(" cast from Value to ArrayReference"); 452 ArrayReference aRef = (ArrayReference) val; 453 } catch ( ClassCastException e ) { 454 log3("ERROR: ClassCastException"); 455 testExitCode = FAILED; 456 } 457 try { 458 log2(" cast from Value to ObjectReference"); 459 arrayObjRef = (ObjectReference) val; 460 log2(" cast from ObjectReference to ArrayReference"); 461 ArrayReference aRef = (ArrayReference) arrayObjRef; 462 } catch ( ClassCastException e ) { 463 log3("ERROR: ClassCastException"); 464 testExitCode = FAILED; 465 } 466 } catch ( AbsentInformationException e ) { 467 log3("ERROR: AbsentInformationException for: stackFrame.getValue()"); 468 expresult = returnCode1; 469 } 470 471 log2("......check for a mirror of Class object"); 472 try { 473 val = stackFrame.getValue(stackFrame.visibleVariableByName(classObj3Name)); 474 try { 475 log2(" cast from Value to ClassObjectReference"); 476 ClassObjectReference c1Ref = (ClassObjectReference) val; 477 } catch ( ClassCastException e ) { 478 log3("ERROR: ClassCastException"); 479 testExitCode = FAILED; 480 } 481 try { 482 log2(" cast from Value to ObjectReference"); 483 classObj1Ref = (ObjectReference) val; 484 log2(" cast from ObjectReference to ClassObjectReference"); 485 ClassObjectReference c1Ref = (ClassObjectReference) classObj1Ref; 486 } catch ( ClassCastException e ) { 487 log3("ERROR: ClassCastException"); 488 testExitCode = FAILED; 489 } 490 491 } catch ( AbsentInformationException e ) { 492 log3("ERROR: AbsentInformationException for: stackFrame.getValue()"); 493 expresult = returnCode1; 494 } 495 } 496 497 thread2.resume(); 498 499 if (expresult != returnCode0) 500 testExitCode = FAILED; 501 502 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 503 } 504 log1(" TESTING ENDS"); 505 506 //-------------------------------------------------- test summary section 507 //------------------------------------------------- standard end section 508 509 pipe.println("quit"); 510 log2("waiting for the debuggee to finish ..."); 511 debuggee.waitFor(); 512 513 int status = debuggee.getStatus(); 514 if (status != PASSED + PASS_BASE) { 515 log3("debuggee returned UNEXPECTED exit status: " + 516 status + " != PASS_BASE"); 517 testExitCode = FAILED; 518 } else { 519 log2("debuggee returned expected exit status: " + 520 status + " == PASS_BASE"); 521 } 522 523 if (testExitCode != PASSED) { 524 logHandler.complain("TEST FAILED"); 525 } 526 return testExitCode; 527 } 528 529 530 /* 531 * private BreakpointRequest settingBreakpoint(String, String, String) 532 * 533 * It sets up a breakpoint within a given method at given line number 534 * for the thread2 only. 535 * Third parameter is required for any case in future debugging, as if. 536 * 537 * Return codes: 538 * = BreakpointRequest object in case of success 539 * = null in case of an Exception thrown within the method 540 */ 541 settingBreakpoint( String methodName, String bpLine, String property)542 private BreakpointRequest settingBreakpoint ( String methodName, 543 String bpLine, 544 String property) { 545 546 log2("setting up a breakpoint: method: '" + methodName + "' line: " + bpLine ); 547 548 List alllineLocations = null; 549 Location lineLocation = null; 550 BreakpointRequest breakpRequest = null; 551 552 try { 553 Method method = (Method) testedclass.methodsByName(methodName).get(0); 554 555 alllineLocations = method.allLineLocations(); 556 557 int n = 558 ( (IntegerValue) testedclass.getValue(testedclass.fieldByName(bpLine) ) ).value(); 559 if (n > alllineLocations.size()) { 560 log3("ERROR: TEST_ERROR_IN_settingBreakpoint(): number is out of bound of method's lines"); 561 } else { 562 lineLocation = (Location) alllineLocations.get(n); 563 try { 564 breakpRequest = eventRManager.createBreakpointRequest(lineLocation); 565 breakpRequest.putProperty("number", property); 566 breakpRequest.addThreadFilter(thread2); 567 breakpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD); 568 } catch ( Exception e1 ) { 569 log3("ERROR: inner Exception within settingBreakpoint() : " + e1); 570 breakpRequest = null; 571 } 572 } 573 } catch ( Exception e2 ) { 574 log3("ERROR: ATTENTION: outer Exception within settingBreakpoint() : " + e2); 575 breakpRequest = null; 576 } 577 578 if (breakpRequest == null) 579 log2(" A BREAKPOINT HAS NOT BEEN SET UP"); 580 else 581 log2(" a breakpoint has been set up"); 582 583 return breakpRequest; 584 } 585 586 587 /* 588 * private int breakpoint () 589 * 590 * It removes events from EventQueue until gets first BreakpointEvent. 591 * To get next EventSet value, it uses the method 592 * EventQueue.remove(int timeout) 593 * The timeout argument passed to the method, is "waitTime*60000". 594 * Note: the value of waitTime is set up with 595 * the method ArgumentHandler.getWaitTime() at the beginning of the test. 596 * 597 * Return codes: 598 * = returnCode0 - success; 599 * = returnCode2 - Exception when "eventSet = eventQueue.remove()" is executed 600 * = returnCode3 - default case when loop of processing an event, that is, 601 * an unspecified event was taken from the EventQueue 602 */ 603 breakpoint()604 private int breakpoint () { 605 606 int returnCode = returnCode0; 607 608 log2(" waiting for BreakpointEvent"); 609 610 labelBP: 611 for (;;) { 612 613 log2(" new: eventSet = eventQueue.remove();"); 614 try { 615 eventSet = eventQueue.remove(waitTime*60000); 616 if (eventSet == null) { 617 log3("ERROR: timeout for waiting for a BreakpintEvent"); 618 returnCode = returnCode3; 619 break labelBP; 620 } 621 } catch ( Exception e ) { 622 log3("ERROR: Exception for eventSet = eventQueue.remove(); : " + e); 623 returnCode = 1; 624 break labelBP; 625 } 626 627 if (eventSet != null) { 628 629 log2(" : eventSet != null; size == " + eventSet.size()); 630 631 EventIterator eIter = eventSet.eventIterator(); 632 Event ev = null; 633 634 for (; eIter.hasNext(); ) { 635 636 if (returnCode != returnCode0) 637 break; 638 639 ev = eIter.nextEvent(); 640 641 ll: for (int ifor =0; ; ifor++) { 642 643 try { 644 switch (ifor) { 645 646 case 0: AccessWatchpointEvent awe = (AccessWatchpointEvent) ev; 647 log2(" AccessWatchpointEvent removed"); 648 break ll; 649 case 1: BreakpointEvent be = (BreakpointEvent) ev; 650 log2(" BreakpointEvent removed"); 651 break labelBP; 652 case 2: ClassPrepareEvent cpe = (ClassPrepareEvent) ev; 653 log2(" ClassPreparEvent removed"); 654 break ll; 655 case 3: ClassUnloadEvent cue = (ClassUnloadEvent) ev; 656 log2(" ClassUnloadEvent removed"); 657 break ll; 658 case 4: ExceptionEvent ee = (ExceptionEvent) ev; 659 log2(" ExceptionEvent removed"); 660 break ll; 661 case 5: MethodEntryEvent mene = (MethodEntryEvent) ev; 662 log2(" MethodEntryEvent removed"); 663 break ll; 664 case 6: MethodExitEvent mexe = (MethodExitEvent) ev; 665 log2(" MethodExiEvent removed"); 666 break ll; 667 case 7: ModificationWatchpointEvent mwe = (ModificationWatchpointEvent) ev; 668 log2(" ModificationWatchpointEvent removed"); 669 break ll; 670 case 8: StepEvent se = (StepEvent) ev; 671 log2(" StepEvent removed"); 672 break ll; 673 case 9: ThreadDeathEvent tde = (ThreadDeathEvent) ev; 674 log2(" ThreadDeathEvent removed"); 675 break ll; 676 case 10: ThreadStartEvent tse = (ThreadStartEvent) ev; 677 log2(" ThreadStartEvent removed"); 678 break ll; 679 case 11: VMDeathEvent vmde = (VMDeathEvent) ev; 680 log2(" VMDeathEvent removed"); 681 break ll; 682 case 12: VMStartEvent vmse = (VMStartEvent) ev; 683 log2(" VMStartEvent removed"); 684 break ll; 685 case 13: WatchpointEvent we = (WatchpointEvent) ev; 686 log2(" WatchpointEvent removed"); 687 break ll; 688 689 default: log3("ERROR: default case for casting event"); 690 returnCode = returnCode3; 691 break ll; 692 } // switch 693 } catch ( ClassCastException e ) { 694 } // try 695 } // ll: for (int ifor =0; ; ifor++) 696 } // for (; ev.hasNext(); ) 697 } 698 } 699 if (returnCode == returnCode0) 700 log2(" : eventSet == null: EventQueue is empty"); 701 702 return returnCode; 703 } 704 705 706 } 707