Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/resume/resume007.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 MethodExitEvent 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 MethodExitRequest, resumes <BR>73* the debuggee, and waits for the MethodExitEvent. <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 resume007 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 resume007().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.resume007a";110111private String testedClassName =112"nsk.jdi.EventSet.resume.resume007aTestClass";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;277278279for (int i = 0; ; i++) {280281breakpointForCommunication();282283int instruction = ((IntegerValue)284(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();285286if (instruction == 0) {287vm.resume();288break;289}290291log1(":::::: case: # " + i);292293//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part294295switch (i) {296297case 0:298testClassReference =299(ReferenceType) vm.classesByName(testedClassName).get(0);300301eventRequest1 = settingMethodExitRequest (mainThread,302testClassReference,303SUSPEND_NONE, "MethodExitRequest1");304eventRequest1.enable();305break;306307case 1:308eventRequest2 = settingMethodExitRequest (mainThread,309testClassReference,310SUSPEND_THREAD, "MethodExitRequest2");311eventRequest2.enable();312break;313314case 2:315eventRequest3 = settingMethodExitRequest (mainThread,316testClassReference,317SUSPEND_ALL, "MethodExitRequest3");318eventRequest3.enable();319break;320321322default:323throw new JDITestRuntimeException("** default case 2 **");324}325326log2("......waiting for new MethodExitEvent : " + i);327mainThread.resume();328getEventSet();329330Event newEvent = eventIterator.nextEvent();331if ( !(newEvent instanceof MethodExitEvent)) {332log3("ERROR: new event is not MethodExitEvent");333testExitCode = FAILED;334} else {335336String property = (String) newEvent.request().getProperty("number");337log2(" got new MethodExitEvent with propety 'number' == " + property);338339log2("......checking up on EventSet.resume()");340log2("......--> vm.suspend();");341vm.suspend();342343log2(" getting : Map<String, Integer> suspendsCounts1");344345Map<String, Integer> suspendsCounts1 = new HashMap<String, Integer>();346for (ThreadReference threadReference : vm.allThreads()) {347suspendsCounts1.put(threadReference.name(), threadReference.suspendCount());348}349350log2(" eventSet.resume;");351eventSet.resume();352353log2(" getting : Map<String, Integer> suspendsCounts2");354Map<String, Integer> suspendsCounts2 = new HashMap<String, Integer>();355for (ThreadReference threadReference : vm.allThreads()) {356suspendsCounts2.put(threadReference.name(), threadReference.suspendCount());357}358359log2(suspendsCounts1.toString());360log2(suspendsCounts2.toString());361362log2(" getting : int policy = eventSet.suspendPolicy();");363int policy = eventSet.suspendPolicy();364365switch (policy) {366367case SUSPEND_NONE :368log2(" case SUSPEND_NONE");369for (String threadName : suspendsCounts1.keySet()) {370log2(" checking " + threadName);371if (!suspendsCounts2.containsKey(threadName)) {372log3("ERROR: couldn't get ThreadReference for " + threadName);373testExitCode = FAILED;374break;375}376int count1 = suspendsCounts1.get(threadName);377int count2 = suspendsCounts2.get(threadName);378if (count1 != count2) {379log3("ERROR: suspendCounts don't match for : " + threadName);380log3("before resuming : " + count1);381log3("after resuming : " + count2);382testExitCode = FAILED;383break;384}385}386eventRequest1.disable();387break;388389case SUSPEND_THREAD :390log2(" case SUSPEND_THREAD");391for (String threadName : suspendsCounts1.keySet()) {392log2("checking " + threadName);393if (!suspendsCounts2.containsKey(threadName)) {394log3("ERROR: couldn't get ThreadReference for " + threadName);395testExitCode = FAILED;396break;397}398int count1 = suspendsCounts1.get(threadName);399int count2 = suspendsCounts2.get(threadName);400String eventThreadName = event.thread().name();401int expectedValue = count2 + (eventThreadName.equals(threadName) ? 1 : 0);402if (count1 != expectedValue) {403log3("ERROR: suspendCounts don't match for : " + threadName);404log3("before resuming : " + count1);405log3("after resuming : " + count2);406testExitCode = FAILED;407break;408}409}410eventRequest2.disable();411break;412413case SUSPEND_ALL :414415log2(" case SUSPEND_ALL");416for (String threadName : suspendsCounts1.keySet()) {417log2("checking " + threadName);418if (!event.request().equals(eventRequest3))419break;420if (!suspendsCounts2.containsKey(threadName)) {421log3("ERROR: couldn't get ThreadReference for " + threadName);422testExitCode = FAILED;423break;424}425int count1 = suspendsCounts1.get(threadName);426int count2 = suspendsCounts2.get(threadName);427if (count1 != count2 + 1) {428log3("ERROR: suspendCounts don't match for : " + threadName);429log3("before resuming : " + count1);430log3("after resuming : " + count2);431testExitCode = FAILED;432break;433}434}435eventRequest3.disable();436break;437438default: throw new JDITestRuntimeException("** default case 1 **");439}440informDebuggeeTestCase(i);441}442443log2("......--> vm.resume()");444vm.resume();445//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~446}447log1(" TESTING ENDS");448return;449}450451// ============================== test's additional methods452453private MethodExitRequest settingMethodExitRequest ( ThreadReference thread,454ReferenceType testedClass,455int suspendPolicy,456String property )457throws JDITestRuntimeException {458try {459log2("......setting up MethodExitRequest:");460log2(" thread: " + thread + "; class: " + testedClass + "; property: " + property);461462MethodExitRequest463mexr = eventRManager.createMethodExitRequest();464mexr.putProperty("number", property);465mexr.addThreadFilter(thread);466mexr.addClassFilter(testedClass);467mexr.setSuspendPolicy(suspendPolicy);468469log2(" MethodExitRequest has been set up");470return mexr;471} catch ( Exception e ) {472log3("ERROR: ATTENTION: Exception within settingMethodExitRequest() : " + e);473log3(" MethodExitRequest HAS NOT BEEN SET UP");474throw new JDITestRuntimeException("** FAILURE to set up MethodExitRequest **");475}476}477/**478* Inform debuggee which thread test the debugger has completed.479* Used for synchronization, so the debuggee does not move too quickly.480* @param testCase index of just completed test481*/482void informDebuggeeTestCase(int testCase) {483try {484((ClassType)debuggeeClass)485.setValue(debuggeeClass.fieldByName("testCase"),486vm.mirrorOf(testCase));487} catch (InvalidTypeException ite) {488throw new Failure("** FAILURE setting testCase **");489} catch (ClassNotLoadedException cnle) {490throw new Failure("** FAILURE notifying debuggee **");491} catch (VMDisconnectedException e) {492throw new Failure("** FAILURE debuggee connection **");493}494}495}496497498