Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/resume/resume005.java
41161 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jdi.EventSet.resume;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import com.sun.jdi.event.*;31import com.sun.jdi.request.*;3233import java.util.*;34import java.io.*;3536/**37* The test for the implementation of an object of the type <BR>38* EventSet. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventSet.resume()</code> <BR>42* complies with its spec. <BR>43* <BR>44* Test cases include all three possible suspensions, NONE, <BR>45* EVENT_THREAD, and ALL, for ExceptionEvent sets. <BR>46* <BR>47* To check up on the method, a debugger, <BR>48* upon getting new set for the EventSet, <BR>49* suspends VM with the method VirtualMachine.suspend(), <BR>50* gets the List of debuggee's threads calling VM.allThreads(), <BR>51* invokes the method EventSet.resume(), and <BR>52* gets another List of debuggee's threads. <BR>53* The debugger then compares values of <BR>54* each thread's suspendCount from first and second Lists. <BR>55* <BR>56* The test has three phases and works as follows. <BR>57* <BR>58* In first phase, <BR>59* upon launching debuggee's VM which will be suspended, <BR>60* a debugger waits for the VMStartEvent within a predefined <BR>61* time interval. If no the VMStartEvent received, the test is FAILED. <BR>62* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>63* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>64* and waits for the event within the predefined time interval. <BR>65* If no the ClassPrepareEvent received, the test is FAILED. <BR>66* Upon getting the ClassPrepareEvent, <BR>67* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>68* within debuggee's special methodForCommunication(). <BR>69* <BR>70* In second phase to check the above, <BR>71* the debugger and the debuggee perform the following loop. <BR>72* - The debugger sets up a ExceptionRequest, resumes <BR>73* the debuggee, and waits for the ExceptionEvent. <BR>74* - The debuggee invokes the special method which makes access to <BR>75* a predefined variable to be resulting in the event. <BR>76* - Upon getting new event, the debugger <BR>77* performs the check corresponding to the event. <BR>78* <BR>79* Note. To inform each other of needed actions, the debugger and <BR>80* and the debuggee use debuggee's variable "instruction". <BR>81* In third phase, at the end, <BR>82* the debuggee changes the value of the "instruction" <BR>83* to inform the debugger of checks finished, and both end. <BR>84* <BR>85*/8687public class resume005 extends JDIBase {8889public static void main (String argv[]) {9091int result = run(argv, System.out);9293System.exit(result + PASS_BASE);94}9596public static int run (String argv[], PrintStream out) {9798int exitCode = new resume005().runThis(argv, out);99100if (exitCode != PASSED) {101System.out.println("TEST FAILED");102}103return testExitCode;104}105106// ************************************************ test parameters107108private String debuggeeName =109"nsk.jdi.EventSet.resume.resume005a";110111private String testedClassName =112"nsk.jdi.EventSet.resume.resume005aTestClass";113114//====================================================== test program115116private int runThis (String argv[], PrintStream out) {117118argsHandler = new ArgumentHandler(argv);119logHandler = new Log(out, argsHandler);120Binder binder = new Binder(argsHandler, logHandler);121122waitTime = argsHandler.getWaitTime() * 60000;123124try {125log2("launching a debuggee :");126log2(" " + debuggeeName);127if (argsHandler.verbose()) {128debuggee = binder.bindToDebugee(debuggeeName + " -vbs");129} else {130debuggee = binder.bindToDebugee(debuggeeName);131}132if (debuggee == null) {133log3("ERROR: no debuggee launched");134return FAILED;135}136log2("debuggee launched");137} catch ( Exception e ) {138log3("ERROR: Exception : " + e);139log2(" test cancelled");140return FAILED;141}142143debuggee.redirectOutput(logHandler);144145vm = debuggee.VM();146147eventQueue = vm.eventQueue();148if (eventQueue == null) {149log3("ERROR: eventQueue == null : TEST ABORTED");150vm.exit(PASS_BASE);151return FAILED;152}153154log2("invocation of the method runTest()");155switch (runTest()) {156157case 0 : log2("test phase has finished normally");158log2(" waiting for the debuggee to finish ...");159debuggee.waitFor();160161log2("......getting the debuggee's exit status");162int status = debuggee.getStatus();163if (status != PASS_BASE) {164log3("ERROR: debuggee returned UNEXPECTED exit status: " +165status + " != PASS_BASE");166testExitCode = FAILED;167} else {168log2("......debuggee returned expected exit status: " +169status + " == PASS_BASE");170}171break;172173default : log3("ERROR: runTest() returned unexpected value");174175case 1 : log3("test phase has not finished normally: debuggee is still alive");176log2("......forcing: vm.exit();");177testExitCode = FAILED;178try {179vm.exit(PASS_BASE);180} catch ( Exception e ) {181log3("ERROR: Exception : e");182}183break;184185case 2 : log3("test cancelled due to VMDisconnectedException");186log2("......trying: vm.process().destroy();");187testExitCode = FAILED;188try {189Process vmProcess = vm.process();190if (vmProcess != null) {191vmProcess.destroy();192}193} catch ( Exception e ) {194log3("ERROR: Exception : e");195}196break;197}198199return testExitCode;200}201202203/*204* Return value: 0 - normal end of the test205* 1 - ubnormal end of the test206* 2 - VMDisconnectedException while test phase207*/208209private int runTest() {210211try {212testRun();213214log2("waiting for VMDeathEvent");215getEventSet();216if (eventIterator.nextEvent() instanceof VMDeathEvent)217return 0;218219log3("ERROR: last event is not the VMDeathEvent");220return 1;221} catch ( VMDisconnectedException e ) {222log3("ERROR: VMDisconnectedException : " + e);223return 2;224} catch ( Exception e ) {225log3("ERROR: Exception : " + e);226return 1;227}228229}230231private void testRun()232throws JDITestRuntimeException, Exception {233234eventRManager = vm.eventRequestManager();235236ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();237cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);238cpRequest.addClassFilter(debuggeeName);239240cpRequest.enable();241vm.resume();242getEventSet();243cpRequest.disable();244245ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();246debuggeeClass = event.referenceType();247248if (!debuggeeClass.name().equals(debuggeeName))249throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");250251log2(" received: ClassPrepareEvent for debuggeeClass");252253String bPointMethod = "methodForCommunication";254String lineForComm = "lineForComm";255BreakpointRequest bpRequest;256ThreadReference mainThread = debuggee.threadByNameOrThrow("main");257bpRequest = settingBreakpoint(mainThread,258debuggeeClass,259bPointMethod, lineForComm, "zero");260bpRequest.enable();261262vm.resume();263264//------------------------------------------------------ testing section265266log1(" TESTING BEGINS");267268EventRequest eventRequest1 = null;269EventRequest eventRequest2 = null;270EventRequest eventRequest3 = null;271272final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;273final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;274final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;275276String methodName = "method";277String bpLineName = "breakpointLine";278279ReferenceType testClassReference = null;280281282for (int i = 0; ; i++) {283284breakpointForCommunication();285286int instruction = ((IntegerValue)287(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();288289if (instruction == 0) {290vm.resume();291break;292}293294log1(":::::: case: # " + i);295296//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part297298switch (i) {299300case 0:301testClassReference =302(ReferenceType) vm.classesByName(testedClassName).get(0);303304eventRequest1 = settingExceptionRequest (mainThread,305debuggeeClass, // testClassReference,306SUSPEND_NONE, "ExceptionRequest1");307eventRequest1.enable();308break;309310case 1:311eventRequest2 = settingExceptionRequest (mainThread,312debuggeeClass, // testClassReference,313SUSPEND_THREAD, "ExceptionRequest2");314eventRequest2.enable();315break;316317case 2:318eventRequest3 = settingExceptionRequest (mainThread,319debuggeeClass, // testClassReference,320SUSPEND_ALL, "ExceptionRequest3");321eventRequest3.enable();322break;323324325default:326throw new JDITestRuntimeException("** default case 2 **");327}328329log2("......waiting for new ExceptionEvent : " + i);330mainThread.resume();331getEventSet();332333Event newEvent = eventIterator.nextEvent();334if ( !(newEvent instanceof ExceptionEvent)) {335log3("ERROR: new event is not ExceptionEvent");336testExitCode = FAILED;337} else {338339String property = (String) newEvent.request().getProperty("number");340log2(" got new ExceptionEvent with propety 'number' == " + property);341342log2("......checking up on EventSet.resume()");343log2("......--> vm.suspend();");344vm.suspend();345346log2(" getting : Map<String, Integer> suspendsCounts1");347348Map<String, Integer> suspendsCounts1 = new HashMap<String, Integer>();349for (ThreadReference threadReference : vm.allThreads()) {350suspendsCounts1.put(threadReference.name(), threadReference.suspendCount());351}352353log2(" eventSet.resume;");354eventSet.resume();355356log2(" getting : Map<String, Integer> suspendsCounts2");357Map<String, Integer> suspendsCounts2 = new HashMap<String, Integer>();358for (ThreadReference threadReference : vm.allThreads()) {359suspendsCounts2.put(threadReference.name(), threadReference.suspendCount());360}361362log2(suspendsCounts1.toString());363log2(suspendsCounts2.toString());364365log2(" getting : int policy = eventSet.suspendPolicy();");366int policy = eventSet.suspendPolicy();367368switch (policy) {369370case SUSPEND_NONE :371log2(" case SUSPEND_NONE");372for (String threadName : suspendsCounts1.keySet()) {373log2(" checking " + threadName);374if (!suspendsCounts2.containsKey(threadName)) {375log3("ERROR: couldn't get ThreadReference for " + threadName);376testExitCode = FAILED;377break;378}379int count1 = suspendsCounts1.get(threadName);380int count2 = suspendsCounts2.get(threadName);381if (count1 != count2) {382log3("ERROR: suspendCounts don't match for : " + threadName);383log3("before resuming : " + count1);384log3("after resuming : " + count2);385testExitCode = FAILED;386break;387}388}389eventRequest1.disable();390break;391392case SUSPEND_THREAD :393log2(" case SUSPEND_THREAD");394for (String threadName : suspendsCounts1.keySet()) {395log2("checking " + threadName);396if (!suspendsCounts2.containsKey(threadName)) {397log3("ERROR: couldn't get ThreadReference for " + threadName);398testExitCode = FAILED;399break;400}401int count1 = suspendsCounts1.get(threadName);402int count2 = suspendsCounts2.get(threadName);403String eventThreadName = ((ExceptionEvent)newEvent).thread().name();404int expectedValue = count2 + (eventThreadName.equals(threadName) ? 1 : 0);405if (count1 != expectedValue) {406log3("ERROR: suspendCounts don't match for : " + threadName);407log3("before resuming : " + count1);408log3("after resuming : " + count2);409testExitCode = FAILED;410break;411}412}413eventRequest2.disable();414break;415416case SUSPEND_ALL :417418log2(" case SUSPEND_ALL");419for (String threadName : suspendsCounts1.keySet()) {420log2("checking " + threadName);421if (!event.request().equals(eventRequest3))422break;423if (!suspendsCounts2.containsKey(threadName)) {424log3("ERROR: couldn't get ThreadReference for " + threadName);425testExitCode = FAILED;426break;427}428int count1 = suspendsCounts1.get(threadName);429int count2 = suspendsCounts2.get(threadName);430if (count1 != count2 + 1) {431log3("ERROR: suspendCounts don't match for : " + threadName);432log3("before resuming : " + count1);433log3("after resuming : " + count2);434testExitCode = FAILED;435break;436}437}438eventRequest3.disable();439break;440441default: throw new JDITestRuntimeException("** default case 1 **");442}443informDebuggeeTestCase(i);444}445446log2("......--> vm.resume()");447vm.resume();448//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~449}450log1(" TESTING ENDS");451return;452}453454// ============================== test's additional methods455456private ExceptionRequest settingExceptionRequest ( ThreadReference thread,457ReferenceType testedClass,458int suspendPolicy,459String property )460throws JDITestRuntimeException {461try {462log2("......setting up ExceptionRequest:");463log2(" thread: " + thread + "; class: " + testedClass + "; property: " + property);464465ExceptionRequest466excr = eventRManager.createExceptionRequest(null, true, true);467excr.putProperty("number", property);468excr.addThreadFilter(thread);469excr.addClassFilter(testedClass);470excr.setSuspendPolicy(suspendPolicy);471472log2(" ExceptionRequest has been set up");473return excr;474} catch ( Exception e ) {475log3("ERROR: ATTENTION: Exception within settingExceptionRequest() : " + e);476log3(" ExceptionRequest HAS NOT BEEN SET UP");477throw new JDITestRuntimeException("** FAILURE to set up ExceptionRequest **");478}479}480/**481* Inform debuggee which thread test the debugger has completed.482* Used for synchronization, so the debuggee does not move too quickly.483* @param testCase index of just completed test484*/485void informDebuggeeTestCase(int testCase) {486try {487((ClassType)debuggeeClass)488.setValue(debuggeeClass.fieldByName("testCase"),489vm.mirrorOf(testCase));490} catch (InvalidTypeException ite) {491throw new Failure("** FAILURE setting testCase **");492} catch (ClassNotLoadedException cnle) {493throw new Failure("** FAILURE notifying debuggee **");494} catch (VMDisconnectedException e) {495throw new Failure("** FAILURE debuggee connection **");496}497}498}499500501