Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/resume/resume003.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 ModificationWatchpointEvent 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 ModificationWatchpointRequest, resumes <BR>73* the debuggee, and waits for the ModificationWatchpointEvent. <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 resume003 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 resume003().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.resume003a";110111private String testedClassName =112"nsk.jdi.EventSet.resume.resume003aTestClass";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 {233234if ( !vm.canWatchFieldModification() ) {235log2("......vm.canWatchFieldModification == false :: test cancelled");236vm.exit(PASS_BASE);237return;238}239240241eventRManager = vm.eventRequestManager();242243ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();244cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);245cpRequest.addClassFilter(debuggeeName);246247cpRequest.enable();248vm.resume();249getEventSet();250cpRequest.disable();251252ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();253debuggeeClass = event.referenceType();254255if (!debuggeeClass.name().equals(debuggeeName))256throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");257258log2(" received: ClassPrepareEvent for debuggeeClass");259260String bPointMethod = "methodForCommunication";261String lineForComm = "lineForComm";262BreakpointRequest bpRequest;263ThreadReference mainThread = debuggee.threadByNameOrThrow("main");264bpRequest = settingBreakpoint(mainThread,265debuggeeClass,266bPointMethod, lineForComm, "zero");267bpRequest.enable();268269vm.resume();270271//------------------------------------------------------ testing section272273log1(" TESTING BEGINS");274275EventRequest eventRequest1 = null;276EventRequest eventRequest2 = null;277EventRequest eventRequest3 = null;278279final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;280final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;281final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;282283String fieldName = "var1";284285ReferenceType testClassReference = null;286287288for (int i = 0; ; i++) {289290breakpointForCommunication();291292int instruction = ((IntegerValue)293(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();294295if (instruction == 0) {296vm.resume();297break;298}299300log1(":::::: case: # " + i);301302//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part303304switch (i) {305306case 0:307testClassReference =308(ReferenceType) vm.classesByName(testedClassName).get(0);309310eventRequest1 = settingModificationWatchpointRequest (mainThread,311testClassReference, fieldName,312SUSPEND_NONE, "ModificationWatchpointRequest1");313eventRequest1.enable();314break;315316case 1:317eventRequest2 = settingModificationWatchpointRequest (mainThread,318testClassReference, fieldName,319SUSPEND_THREAD, "ModificationWatchpointRequest2");320eventRequest2.enable();321break;322323case 2:324eventRequest3 = settingModificationWatchpointRequest (mainThread,325testClassReference, fieldName,326SUSPEND_ALL, "ModificationWatchpointRequest3");327eventRequest3.enable();328break;329330331default:332throw new JDITestRuntimeException("** default case 2 **");333}334335log2("......waiting for new ModificationWatchpointEvent : " + i);336mainThread.resume();337getEventSet();338339Event newEvent = eventIterator.nextEvent();340if ( !(newEvent instanceof ModificationWatchpointEvent)) {341log3("ERROR: new event is not ModificationWatchpointEvent");342testExitCode = FAILED;343} else {344345String property = (String) newEvent.request().getProperty("number");346log2(" got new ModificationWatchpointEvent with propety 'number' == " + property);347348log2("......checking up on EventSet.resume()");349log2("......--> vm.suspend();");350vm.suspend();351352log2(" getting : Map<String, Integer> suspendsCounts1");353354Map<String, Integer> suspendsCounts1 = new HashMap<String, Integer>();355for (ThreadReference threadReference : vm.allThreads()) {356suspendsCounts1.put(threadReference.name(), threadReference.suspendCount());357}358359log2(" eventSet.resume;");360eventSet.resume();361362log2(" getting : Map<String, Integer> suspendsCounts2");363Map<String, Integer> suspendsCounts2 = new HashMap<String, Integer>();364for (ThreadReference threadReference : vm.allThreads()) {365suspendsCounts2.put(threadReference.name(), threadReference.suspendCount());366}367368log2(suspendsCounts1.toString());369log2(suspendsCounts2.toString());370371log2(" getting : int policy = eventSet.suspendPolicy();");372int policy = eventSet.suspendPolicy();373374switch (policy) {375376case SUSPEND_NONE :377log2(" case SUSPEND_NONE");378for (String threadName : suspendsCounts1.keySet()) {379log2(" checking " + threadName);380if (!suspendsCounts2.containsKey(threadName)) {381log3("ERROR: couldn't get ThreadReference for " + threadName);382testExitCode = FAILED;383break;384}385int count1 = suspendsCounts1.get(threadName);386int count2 = suspendsCounts2.get(threadName);387if (count1 != count2) {388log3("ERROR: suspendCounts don't match for : " + threadName);389log3("before resuming : " + count1);390log3("after resuming : " + count2);391testExitCode = FAILED;392break;393}394}395eventRequest1.disable();396break;397398case SUSPEND_THREAD :399log2(" case SUSPEND_THREAD");400for (String threadName : suspendsCounts1.keySet()) {401log2("checking " + threadName);402if (!suspendsCounts2.containsKey(threadName)) {403log3("ERROR: couldn't get ThreadReference for " + threadName);404testExitCode = FAILED;405break;406}407int count1 = suspendsCounts1.get(threadName);408int count2 = suspendsCounts2.get(threadName);409String eventThreadName = ((ModificationWatchpointEvent)newEvent).thread().name();410int expectedValue = count2 + (eventThreadName.equals(threadName) ? 1 : 0);411if (count1 != expectedValue) {412log3("ERROR: suspendCounts don't match for : " + threadName);413log3("before resuming : " + count1);414log3("after resuming : " + count2);415testExitCode = FAILED;416break;417}418}419eventRequest2.disable();420break;421422case SUSPEND_ALL :423424log2(" case SUSPEND_ALL");425for (String threadName : suspendsCounts1.keySet()) {426log2("checking " + threadName);427if (!event.request().equals(eventRequest3))428break;429if (!suspendsCounts2.containsKey(threadName)) {430log3("ERROR: couldn't get ThreadReference for " + threadName);431testExitCode = FAILED;432break;433}434int count1 = suspendsCounts1.get(threadName);435int count2 = suspendsCounts2.get(threadName);436if (count1 != count2 + 1) {437log3("ERROR: suspendCounts don't match for : " + threadName);438log3("before resuming : " + count1);439log3("after resuming : " + count2);440testExitCode = FAILED;441break;442}443}444eventRequest3.disable();445break;446447default: throw new JDITestRuntimeException("** default case 1 **");448}449}450451log2("......--> vm.resume()");452vm.resume();453informDebuggeeTestCase(i);454455//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~456}457log1(" TESTING ENDS");458return;459}460461// ============================== test's additional methods462463private ModificationWatchpointRequest settingModificationWatchpointRequest (464ThreadReference thread,465ReferenceType testedClass,466String fieldName,467int suspendPolicy,468String property )469throws JDITestRuntimeException {470try {471log2("......setting up ModificationWatchpointRequest:");472log2(" thread: " + thread + "; class: " + testedClass + "; fieldName: " + fieldName);473Field field = testedClass.fieldByName(fieldName);474475ModificationWatchpointRequest476awr = eventRManager.createModificationWatchpointRequest(field);477awr.putProperty("number", property);478awr.addThreadFilter(thread);479awr.setSuspendPolicy(suspendPolicy);480481log2(" ModificationWatchpointRequest has been set up");482return awr;483} catch ( Exception e ) {484log3("ERROR: ATTENTION: Exception within settingModificationWatchpointRequest() : " + e);485log3(" ModificationWatchpointRequest HAS NOT BEEN SET UP");486throw new JDITestRuntimeException("** FAILURE to set up ModificationWatchpointRequest **");487}488}489/**490* Inform debuggee which thread test the debugger has completed.491* Used for synchronization, so the debuggee does not move too quickly.492* @param testCase index of just completed test493*/494void informDebuggeeTestCase(int testCase) {495try {496((ClassType)debuggeeClass)497.setValue(debuggeeClass.fieldByName("testCase"),498vm.mirrorOf(testCase));499} catch (InvalidTypeException ite) {500throw new Failure("** FAILURE setting testCase **");501} catch (ClassNotLoadedException cnle) {502throw new Failure("** FAILURE notifying debuggee **");503} catch (VMDisconnectedException e) {504throw new Failure("** FAILURE debuggee connection **");505}506}507}508509510