Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/resume/resume010.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 StepEvent 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 StepRequest, resumes <BR>73* the debuggee, and waits for the StepEvent. <BR>74* - The debuggee invokes the special method <BR>75* 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 resume010 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 resume010().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.resume010a";110111private String testedClassName =112"nsk.jdi.EventSet.resume.resume010aTestClass";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;275276ReferenceType testClassReference = null;277278for (int i = 0; ; i++) {279280breakpointForCommunication();281282int instruction = ((IntegerValue)283(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();284285if (instruction == 0) {286vm.resume();287break;288}289290log1(":::::: case: # " + i);291292//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part293294switch (i) {295296case 0:297eventRequest1 = settingStepRequest (mainThread,298SUSPEND_NONE, "StepRequest1");299eventRequest1.enable();300break;301302case 1:303eventRequest2 = settingStepRequest (mainThread,304SUSPEND_THREAD, "StepRequest2");305eventRequest2.enable();306break;307308case 2:309eventRequest3 = settingStepRequest (mainThread,310311SUSPEND_ALL, "StepRequest3");312eventRequest3.enable();313break;314315316default:317throw new JDITestRuntimeException("** default case 2 **");318}319320log2("......waiting for new StepEvent : " + i);321mainThread.resume();322getEventSet();323324Event newEvent = eventIterator.nextEvent();325if ( !(newEvent instanceof StepEvent)) {326log3("ERROR: new event is not StepEvent");327testExitCode = FAILED;328} else {329330String property = (String) newEvent.request().getProperty("number");331log2(" got new StepEvent with propety 'number' == " + property);332333log2("......checking up on EventSet.resume()");334log2("......--> vm.suspend();");335vm.suspend();336337log2(" getting : Map<String, Integer> suspendsCounts1");338339Map<String, Integer> suspendsCounts1 = new HashMap<String, Integer>();340for (ThreadReference threadReference : vm.allThreads()) {341suspendsCounts1.put(threadReference.name(), threadReference.suspendCount());342}343344log2(" eventSet.resume;");345eventSet.resume();346347log2(" getting : Map<String, Integer> suspendsCounts2");348349Map<String, Integer> suspendsCounts2 = new HashMap<String, Integer>();350for (ThreadReference threadReference : vm.allThreads()) {351suspendsCounts2.put(threadReference.name(), threadReference.suspendCount());352}353354log2(suspendsCounts1.toString());355log2(suspendsCounts2.toString());356357log2(" getting : int policy = eventSet.suspendPolicy();");358int policy = eventSet.suspendPolicy();359360switch (policy) {361362case SUSPEND_NONE:363364log2(" case SUSPEND_NONE");365for (String threadName : suspendsCounts1.keySet()) {366log2(" checking " + threadName);367if (!suspendsCounts2.containsKey(threadName)) {368log3("ERROR: couldn't get ThreadReference for " + threadName);369testExitCode = FAILED;370break;371}372int count1 = suspendsCounts1.get(threadName);373int count2 = suspendsCounts2.get(threadName);374if (count1 != count2) {375log3("ERROR: suspendCounts don't match for : " + threadName);376log3("before resuming : " + count1);377log3("after resuming : " + count2);378testExitCode = FAILED;379break;380}381}382eventRequest1.disable();383break;384385case SUSPEND_THREAD:386387log2(" case SUSPEND_THREAD");388for (String threadName : suspendsCounts1.keySet()) {389log2("checking " + threadName);390if (!suspendsCounts2.containsKey(threadName)) {391log3("ERROR: couldn't get ThreadReference for " + threadName);392testExitCode = FAILED;393break;394}395int count1 = suspendsCounts1.get(threadName);396int count2 = suspendsCounts2.get(threadName);397String eventThreadName = ((StepEvent) newEvent).thread().name();398int expectedValue = count2 + (eventThreadName.equals(threadName) ? 1 : 0);399if (count1 != expectedValue) {400log3("ERROR: suspendCounts don't match for : " + threadName);401log3("before resuming : " + count1);402log3("after resuming : " + count2);403testExitCode = FAILED;404break;405}406}407eventRequest2.disable();408break;409410case SUSPEND_ALL:411412log2(" case SUSPEND_ALL");413for (String threadName : suspendsCounts1.keySet()) {414log2("checking " + threadName);415if (!event.request().equals(eventRequest3))416break;417if (!suspendsCounts2.containsKey(threadName)) {418log3("ERROR: couldn't get ThreadReference for " + threadName);419testExitCode = FAILED;420break;421}422int count1 = suspendsCounts1.get(threadName);423int count2 = suspendsCounts2.get(threadName);424if (count1 != count2 + 1) {425log3("ERROR: suspendCounts don't match for : " + threadName);426log3("before resuming : " + count1);427log3("after resuming : " + count2);428testExitCode = FAILED;429break;430}431}432eventRequest3.disable();433break;434435default:436throw new JDITestRuntimeException("** default case 1 **");437}438informDebuggeeTestCase(i);439}440441log2("......--> vm.resume()");442vm.resume();443//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~444}445log1(" TESTING ENDS");446return;447}448449// ============================== test's additional methods450451private StepRequest settingStepRequest ( ThreadReference thread,452int suspendPolicy,453String property )454throws JDITestRuntimeException {455try {456log2("......setting up StepRequest:");457log2(" thread: " + thread + "; property: " + property);458459StepRequest460str = eventRManager.createStepRequest(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER);461str.putProperty("number", property);462str.addCountFilter(1);463str.setSuspendPolicy(suspendPolicy);464465log2(" StepRequest has been set up");466return str;467} catch ( Exception e ) {468log3("ERROR: ATTENTION: Exception within settingStepRequest() : " + e);469log3(" StepRequest HAS NOT BEEN SET UP");470throw new JDITestRuntimeException("** FAILURE to set up StepRequest **");471}472}473/**474* Inform debuggee which thread test the debugger has completed.475* Used for synchronization, so the debuggee does not move too quickly.476* @param testCase index of just completed test477*/478void informDebuggeeTestCase(int testCase) {479try {480((ClassType)debuggeeClass)481.setValue(debuggeeClass.fieldByName("testCase"),482vm.mirrorOf(testCase));483} catch (InvalidTypeException ite) {484throw new Failure("** FAILURE setting testCase **");485} catch (ClassNotLoadedException cnle) {486throw new Failure("** FAILURE notifying debuggee **");487} catch (VMDisconnectedException e) {488throw new Failure("** FAILURE debuggee connection **");489}490}491}492493494